Commit | Line | Data |
---|---|---|
5b74c7b1 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
91d76f53 | 7 | * |
5b74c7b1 DG |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
d14d33bf | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5b74c7b1 DG |
11 | * GNU General Public License for more details. |
12 | * | |
d14d33bf AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
5b74c7b1 DG |
16 | */ |
17 | ||
18 | #define _GNU_SOURCE | |
6c9cc2ab | 19 | #include <limits.h> |
d022620a | 20 | #include <inttypes.h> |
5b74c7b1 DG |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
a304c14c | 24 | #include <sys/stat.h> |
f6a9efaa | 25 | #include <urcu.h> |
5b74c7b1 | 26 | |
990570ed | 27 | #include <common/common.h> |
db758600 | 28 | #include <common/sessiond-comm/sessiond-comm.h> |
1e307fab | 29 | |
5b74c7b1 DG |
30 | #include "session.h" |
31 | ||
8c0faa1d | 32 | /* |
b5541356 | 33 | * NOTES: |
8c0faa1d | 34 | * |
b5541356 DG |
35 | * No ltt_session.lock is taken here because those data structure are widely |
36 | * spread across the lttng-tools code base so before caling functions below | |
37 | * that can read/write a session, the caller MUST acquire the session lock | |
54d01ffb | 38 | * using session_lock() and session_unlock(). |
8c0faa1d | 39 | */ |
8c0faa1d | 40 | |
5b74c7b1 | 41 | /* |
b5541356 | 42 | * Init tracing session list. |
5b74c7b1 | 43 | * |
b5541356 | 44 | * Please see session.h for more explanation and correct usage of the list. |
5b74c7b1 | 45 | */ |
b5541356 DG |
46 | static struct ltt_session_list ltt_session_list = { |
47 | .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), | |
48 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a24f7994 | 49 | .next_uuid = 0, |
b5541356 | 50 | }; |
5b74c7b1 | 51 | |
1c1c3634 DG |
52 | /* These characters are forbidden in a session name. Used by validate_name. */ |
53 | static const char *forbidden_name_chars = "/"; | |
54 | ||
55 | /* | |
56 | * Validate the session name for forbidden characters. | |
57 | * | |
58 | * Return 0 on success else -1 meaning a forbidden char. has been found. | |
59 | */ | |
60 | static int validate_name(const char *name) | |
61 | { | |
62 | int ret; | |
63 | char *tok, *tmp_name; | |
64 | ||
65 | assert(name); | |
66 | ||
67 | tmp_name = strdup(name); | |
68 | if (!tmp_name) { | |
69 | /* ENOMEM here. */ | |
70 | ret = -1; | |
71 | goto error; | |
72 | } | |
73 | ||
74 | tok = strpbrk(tmp_name, forbidden_name_chars); | |
75 | if (tok) { | |
76 | DBG("Session name %s contains a forbidden character", name); | |
77 | /* Forbidden character has been found. */ | |
78 | ret = -1; | |
79 | goto error; | |
80 | } | |
81 | ret = 0; | |
82 | ||
83 | error: | |
84 | free(tmp_name); | |
85 | return ret; | |
86 | } | |
87 | ||
5b74c7b1 | 88 | /* |
050349bb | 89 | * Add a ltt_session structure to the global list. |
5b74c7b1 | 90 | * |
050349bb | 91 | * The caller MUST acquire the session list lock before. |
44e96653 | 92 | * Returns the unique identifier for the session. |
5b74c7b1 | 93 | */ |
d022620a | 94 | static uint64_t add_session_list(struct ltt_session *ls) |
5b74c7b1 | 95 | { |
0525e9ae DG |
96 | assert(ls); |
97 | ||
5b74c7b1 | 98 | cds_list_add(&ls->list, <t_session_list.head); |
a24f7994 | 99 | return ltt_session_list.next_uuid++; |
5b74c7b1 DG |
100 | } |
101 | ||
102 | /* | |
050349bb | 103 | * Delete a ltt_session structure to the global list. |
b5541356 | 104 | * |
050349bb | 105 | * The caller MUST acquire the session list lock before. |
5b74c7b1 DG |
106 | */ |
107 | static void del_session_list(struct ltt_session *ls) | |
108 | { | |
0525e9ae DG |
109 | assert(ls); |
110 | ||
5b74c7b1 | 111 | cds_list_del(&ls->list); |
5b74c7b1 DG |
112 | } |
113 | ||
b5541356 | 114 | /* |
050349bb | 115 | * Return a pointer to the session list. |
b5541356 | 116 | */ |
54d01ffb | 117 | struct ltt_session_list *session_get_list(void) |
b5541356 DG |
118 | { |
119 | return <t_session_list; | |
120 | } | |
121 | ||
122 | /* | |
6c9cc2ab | 123 | * Acquire session list lock |
b5541356 | 124 | */ |
54d01ffb | 125 | void session_lock_list(void) |
b5541356 | 126 | { |
6c9cc2ab | 127 | pthread_mutex_lock(<t_session_list.lock); |
b5541356 DG |
128 | } |
129 | ||
130 | /* | |
6c9cc2ab | 131 | * Release session list lock |
b5541356 | 132 | */ |
54d01ffb | 133 | void session_unlock_list(void) |
b5541356 | 134 | { |
6c9cc2ab | 135 | pthread_mutex_unlock(<t_session_list.lock); |
b5541356 DG |
136 | } |
137 | ||
138 | /* | |
6c9cc2ab | 139 | * Acquire session lock |
b5541356 | 140 | */ |
54d01ffb | 141 | void session_lock(struct ltt_session *session) |
b5541356 | 142 | { |
0525e9ae DG |
143 | assert(session); |
144 | ||
6c9cc2ab DG |
145 | pthread_mutex_lock(&session->lock); |
146 | } | |
b5541356 | 147 | |
6c9cc2ab DG |
148 | /* |
149 | * Release session lock | |
150 | */ | |
54d01ffb | 151 | void session_unlock(struct ltt_session *session) |
6c9cc2ab | 152 | { |
0525e9ae DG |
153 | assert(session); |
154 | ||
6c9cc2ab | 155 | pthread_mutex_unlock(&session->lock); |
b5541356 DG |
156 | } |
157 | ||
5b74c7b1 | 158 | /* |
74babd95 DG |
159 | * Return a ltt_session structure ptr that matches name. If no session found, |
160 | * NULL is returned. This must be called with the session lock held using | |
161 | * session_lock_list and session_unlock_list. | |
5b74c7b1 | 162 | */ |
58a1a227 | 163 | struct ltt_session *session_find_by_name(const char *name) |
5b74c7b1 | 164 | { |
5b74c7b1 DG |
165 | struct ltt_session *iter; |
166 | ||
0525e9ae DG |
167 | assert(name); |
168 | ||
5f822d0a DG |
169 | DBG2("Trying to find session by name %s", name); |
170 | ||
5b74c7b1 | 171 | cds_list_for_each_entry(iter, <t_session_list.head, list) { |
1b110e1b | 172 | if (strncmp(iter->name, name, NAME_MAX) == 0) { |
74babd95 | 173 | goto found; |
5b74c7b1 DG |
174 | } |
175 | } | |
176 | ||
74babd95 | 177 | iter = NULL; |
5b74c7b1 | 178 | |
74babd95 | 179 | found: |
5b74c7b1 DG |
180 | return iter; |
181 | } | |
182 | ||
183 | /* | |
050349bb | 184 | * Delete session from the session list and free the memory. |
5b74c7b1 | 185 | * |
050349bb | 186 | * Return -1 if no session is found. On success, return 1; |
36b588ed | 187 | * Should *NOT* be called with RCU read-side lock held. |
5b74c7b1 | 188 | */ |
271933a4 | 189 | int session_destroy(struct ltt_session *session) |
5b74c7b1 | 190 | { |
271933a4 | 191 | /* Safety check */ |
0525e9ae | 192 | assert(session); |
271933a4 DG |
193 | |
194 | DBG("Destroying session %s", session->name); | |
195 | del_session_list(session); | |
271933a4 | 196 | pthread_mutex_destroy(&session->lock); |
07424f16 | 197 | |
07424f16 | 198 | consumer_destroy_output(session->consumer); |
6dc3064a | 199 | snapshot_destroy(&session->snapshot); |
271933a4 | 200 | free(session); |
5b74c7b1 | 201 | |
f73fabfd | 202 | return LTTNG_OK; |
5b74c7b1 DG |
203 | } |
204 | ||
205 | /* | |
050349bb | 206 | * Create a brand new session and add it to the session list. |
5b74c7b1 | 207 | */ |
dec56f6c | 208 | int session_create(char *name, uid_t uid, gid_t gid) |
5b74c7b1 | 209 | { |
f3ed775e | 210 | int ret; |
5b74c7b1 | 211 | struct ltt_session *new_session; |
e07ae692 | 212 | |
5b74c7b1 | 213 | /* Allocate session data structure */ |
ba7f0ae5 | 214 | new_session = zmalloc(sizeof(struct ltt_session)); |
5b74c7b1 | 215 | if (new_session == NULL) { |
df0f840b | 216 | PERROR("zmalloc"); |
f73fabfd | 217 | ret = LTTNG_ERR_FATAL; |
f3ed775e | 218 | goto error_malloc; |
5b74c7b1 DG |
219 | } |
220 | ||
f3ed775e | 221 | /* Define session name */ |
5b74c7b1 | 222 | if (name != NULL) { |
74babd95 | 223 | if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) { |
f73fabfd | 224 | ret = LTTNG_ERR_FATAL; |
f3ed775e | 225 | goto error_asprintf; |
5b74c7b1 DG |
226 | } |
227 | } else { | |
f3ed775e | 228 | ERR("No session name given"); |
f73fabfd | 229 | ret = LTTNG_ERR_FATAL; |
f3ed775e DG |
230 | goto error; |
231 | } | |
232 | ||
1c1c3634 DG |
233 | ret = validate_name(name); |
234 | if (ret < 0) { | |
235 | ret = LTTNG_ERR_SESSION_INVALID_CHAR; | |
236 | goto error; | |
237 | } | |
238 | ||
d3e2ba59 | 239 | ret = gethostname(new_session->hostname, sizeof(new_session->hostname)); |
73184835 DG |
240 | if (ret < 0) { |
241 | if (errno == ENAMETOOLONG) { | |
242 | new_session->hostname[sizeof(new_session->hostname) - 1] = '\0'; | |
243 | } else { | |
244 | ret = LTTNG_ERR_FATAL; | |
245 | goto error; | |
246 | } | |
d3e2ba59 JD |
247 | } |
248 | ||
1d4b027a DG |
249 | /* Init kernel session */ |
250 | new_session->kernel_session = NULL; | |
f6a9efaa | 251 | new_session->ust_session = NULL; |
1657e9bb | 252 | |
0177d773 DG |
253 | /* Init lock */ |
254 | pthread_mutex_init(&new_session->lock, NULL); | |
5b74c7b1 | 255 | |
6df2e2c9 MD |
256 | new_session->uid = uid; |
257 | new_session->gid = gid; | |
258 | ||
6dc3064a DG |
259 | ret = snapshot_init(&new_session->snapshot); |
260 | if (ret < 0) { | |
261 | ret = LTTNG_ERR_NOMEM; | |
262 | goto error; | |
263 | } | |
264 | ||
b5541356 | 265 | /* Add new session to the session list */ |
54d01ffb | 266 | session_lock_list(); |
a991f516 | 267 | new_session->id = add_session_list(new_session); |
54d01ffb | 268 | session_unlock_list(); |
b5541356 | 269 | |
a4b92340 DG |
270 | /* |
271 | * Consumer is let to NULL since the create_session_uri command will set it | |
272 | * up and, if valid, assign it to the session. | |
273 | */ | |
274 | ||
d022620a DG |
275 | DBG("Tracing session %s created with ID %" PRIu64 " by UID %d GID %d", |
276 | name, new_session->id, new_session->uid, new_session->gid); | |
b082db07 | 277 | |
f73fabfd | 278 | return LTTNG_OK; |
5b74c7b1 DG |
279 | |
280 | error: | |
f3ed775e | 281 | error_asprintf: |
0e428499 | 282 | free(new_session); |
5b74c7b1 | 283 | |
f3ed775e DG |
284 | error_malloc: |
285 | return ret; | |
5b74c7b1 | 286 | } |
2f77fc4b DG |
287 | |
288 | /* | |
289 | * Check if the UID or GID match the session. Root user has access to all | |
290 | * sessions. | |
291 | */ | |
292 | int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid) | |
293 | { | |
294 | assert(session); | |
295 | ||
296 | if (uid != session->uid && gid != session->gid && uid != 0) { | |
297 | return 0; | |
298 | } else { | |
299 | return 1; | |
300 | } | |
301 | } |