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 | ||
6c1c0768 | 18 | #define _LGPL_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> |
3d071855 MD |
26 | #include <dirent.h> |
27 | #include <sys/types.h> | |
5b74c7b1 | 28 | |
990570ed | 29 | #include <common/common.h> |
db758600 | 30 | #include <common/sessiond-comm/sessiond-comm.h> |
1e307fab | 31 | |
5b74c7b1 | 32 | #include "session.h" |
23324029 | 33 | #include "utils.h" |
dd73d57b | 34 | #include "trace-ust.h" |
5b74c7b1 | 35 | |
8c0faa1d | 36 | /* |
b5541356 | 37 | * NOTES: |
8c0faa1d | 38 | * |
b5541356 DG |
39 | * No ltt_session.lock is taken here because those data structure are widely |
40 | * spread across the lttng-tools code base so before caling functions below | |
41 | * that can read/write a session, the caller MUST acquire the session lock | |
54d01ffb | 42 | * using session_lock() and session_unlock(). |
8c0faa1d | 43 | */ |
8c0faa1d | 44 | |
5b74c7b1 | 45 | /* |
b5541356 | 46 | * Init tracing session list. |
5b74c7b1 | 47 | * |
b5541356 | 48 | * Please see session.h for more explanation and correct usage of the list. |
5b74c7b1 | 49 | */ |
b5541356 DG |
50 | static struct ltt_session_list ltt_session_list = { |
51 | .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), | |
52 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a24f7994 | 53 | .next_uuid = 0, |
b5541356 | 54 | }; |
5b74c7b1 | 55 | |
1c1c3634 DG |
56 | /* These characters are forbidden in a session name. Used by validate_name. */ |
57 | static const char *forbidden_name_chars = "/"; | |
58 | ||
23324029 JD |
59 | /* Global hash table to keep the sessions, indexed by id. */ |
60 | static struct lttng_ht *ltt_sessions_ht_by_id = NULL; | |
61 | ||
1c1c3634 DG |
62 | /* |
63 | * Validate the session name for forbidden characters. | |
64 | * | |
65 | * Return 0 on success else -1 meaning a forbidden char. has been found. | |
66 | */ | |
67 | static int validate_name(const char *name) | |
68 | { | |
69 | int ret; | |
70 | char *tok, *tmp_name; | |
71 | ||
72 | assert(name); | |
73 | ||
74 | tmp_name = strdup(name); | |
75 | if (!tmp_name) { | |
76 | /* ENOMEM here. */ | |
77 | ret = -1; | |
78 | goto error; | |
79 | } | |
80 | ||
81 | tok = strpbrk(tmp_name, forbidden_name_chars); | |
82 | if (tok) { | |
83 | DBG("Session name %s contains a forbidden character", name); | |
84 | /* Forbidden character has been found. */ | |
85 | ret = -1; | |
86 | goto error; | |
87 | } | |
88 | ret = 0; | |
89 | ||
90 | error: | |
91 | free(tmp_name); | |
92 | return ret; | |
93 | } | |
94 | ||
5b74c7b1 | 95 | /* |
050349bb | 96 | * Add a ltt_session structure to the global list. |
5b74c7b1 | 97 | * |
050349bb | 98 | * The caller MUST acquire the session list lock before. |
44e96653 | 99 | * Returns the unique identifier for the session. |
5b74c7b1 | 100 | */ |
d022620a | 101 | static uint64_t add_session_list(struct ltt_session *ls) |
5b74c7b1 | 102 | { |
0525e9ae DG |
103 | assert(ls); |
104 | ||
5b74c7b1 | 105 | cds_list_add(&ls->list, <t_session_list.head); |
a24f7994 | 106 | return ltt_session_list.next_uuid++; |
5b74c7b1 DG |
107 | } |
108 | ||
109 | /* | |
050349bb | 110 | * Delete a ltt_session structure to the global list. |
b5541356 | 111 | * |
050349bb | 112 | * The caller MUST acquire the session list lock before. |
5b74c7b1 DG |
113 | */ |
114 | static void del_session_list(struct ltt_session *ls) | |
115 | { | |
0525e9ae DG |
116 | assert(ls); |
117 | ||
5b74c7b1 | 118 | cds_list_del(&ls->list); |
5b74c7b1 DG |
119 | } |
120 | ||
b5541356 | 121 | /* |
050349bb | 122 | * Return a pointer to the session list. |
b5541356 | 123 | */ |
54d01ffb | 124 | struct ltt_session_list *session_get_list(void) |
b5541356 DG |
125 | { |
126 | return <t_session_list; | |
127 | } | |
128 | ||
129 | /* | |
6c9cc2ab | 130 | * Acquire session list lock |
b5541356 | 131 | */ |
54d01ffb | 132 | void session_lock_list(void) |
b5541356 | 133 | { |
6c9cc2ab | 134 | pthread_mutex_lock(<t_session_list.lock); |
b5541356 DG |
135 | } |
136 | ||
71e0a100 JG |
137 | /* |
138 | * Try to acquire session list lock | |
139 | */ | |
140 | int session_trylock_list(void) | |
141 | { | |
142 | return pthread_mutex_trylock(<t_session_list.lock); | |
143 | } | |
144 | ||
b5541356 | 145 | /* |
6c9cc2ab | 146 | * Release session list lock |
b5541356 | 147 | */ |
54d01ffb | 148 | void session_unlock_list(void) |
b5541356 | 149 | { |
6c9cc2ab | 150 | pthread_mutex_unlock(<t_session_list.lock); |
b5541356 DG |
151 | } |
152 | ||
dd73d57b JG |
153 | /* |
154 | * Get the session's consumer destination type. | |
155 | * | |
156 | * The caller must hold the session lock. | |
157 | */ | |
158 | enum consumer_dst_type session_get_consumer_destination_type( | |
159 | const struct ltt_session *session) | |
160 | { | |
161 | /* | |
162 | * The output information is duplicated in both of those session types. | |
163 | * Hence, it doesn't matter from which it is retrieved. However, it is | |
164 | * possible for only one of them to be set. | |
165 | */ | |
166 | return session->kernel_session ? | |
167 | session->kernel_session->consumer->type : | |
168 | session->ust_session->consumer->type; | |
169 | } | |
170 | ||
171 | /* | |
172 | * Get the session's consumer network hostname. | |
173 | * The caller must ensure that the destination is of type "net". | |
174 | * | |
175 | * The caller must hold the session lock. | |
176 | */ | |
177 | const char *session_get_net_consumer_hostname(const struct ltt_session *session) | |
178 | { | |
179 | const char *hostname = NULL; | |
180 | const struct consumer_output *output; | |
181 | ||
182 | output = session->kernel_session ? | |
183 | session->kernel_session->consumer : | |
184 | session->ust_session->consumer; | |
185 | ||
186 | /* | |
187 | * hostname is assumed to be the same for both control and data | |
188 | * connections. | |
189 | */ | |
190 | switch (output->dst.net.control.dtype) { | |
191 | case LTTNG_DST_IPV4: | |
192 | hostname = output->dst.net.control.dst.ipv4; | |
193 | break; | |
194 | case LTTNG_DST_IPV6: | |
195 | hostname = output->dst.net.control.dst.ipv6; | |
196 | break; | |
197 | default: | |
198 | abort(); | |
199 | } | |
200 | return hostname; | |
201 | } | |
202 | ||
203 | /* | |
204 | * Get the session's consumer network control and data ports. | |
205 | * The caller must ensure that the destination is of type "net". | |
206 | * | |
207 | * The caller must hold the session lock. | |
208 | */ | |
209 | void session_get_net_consumer_ports(const struct ltt_session *session, | |
210 | uint16_t *control_port, uint16_t *data_port) | |
211 | { | |
212 | const struct consumer_output *output; | |
213 | ||
214 | output = session->kernel_session ? | |
215 | session->kernel_session->consumer : | |
216 | session->ust_session->consumer; | |
217 | *control_port = output->dst.net.control.port; | |
218 | *data_port = output->dst.net.data.port; | |
219 | } | |
220 | ||
23324029 JD |
221 | /* |
222 | * Allocate the ltt_sessions_ht_by_id HT. | |
9c6518bc JG |
223 | * |
224 | * The session list lock must be held. | |
23324029 JD |
225 | */ |
226 | int ltt_sessions_ht_alloc(void) | |
227 | { | |
228 | int ret = 0; | |
229 | ||
230 | DBG("Allocating ltt_sessions_ht_by_id"); | |
231 | ltt_sessions_ht_by_id = lttng_ht_new(0, LTTNG_HT_TYPE_U64); | |
232 | if (!ltt_sessions_ht_by_id) { | |
233 | ret = -1; | |
234 | ERR("Failed to allocate ltt_sessions_ht_by_id"); | |
235 | goto end; | |
236 | } | |
237 | end: | |
238 | return ret; | |
239 | } | |
240 | ||
241 | /* | |
242 | * Destroy the ltt_sessions_ht_by_id HT. | |
9c6518bc JG |
243 | * |
244 | * The session list lock must be held. | |
23324029 | 245 | */ |
accdc9bf | 246 | static void ltt_sessions_ht_destroy(void) |
23324029 JD |
247 | { |
248 | if (!ltt_sessions_ht_by_id) { | |
249 | return; | |
250 | } | |
251 | ht_cleanup_push(ltt_sessions_ht_by_id); | |
252 | ltt_sessions_ht_by_id = NULL; | |
253 | } | |
254 | ||
255 | /* | |
256 | * Add a ltt_session to the ltt_sessions_ht_by_id. | |
257 | * If unallocated, the ltt_sessions_ht_by_id HT is allocated. | |
258 | * The session list lock must be held. | |
259 | */ | |
260 | static void add_session_ht(struct ltt_session *ls) | |
261 | { | |
262 | int ret; | |
263 | ||
264 | assert(ls); | |
265 | ||
266 | if (!ltt_sessions_ht_by_id) { | |
267 | ret = ltt_sessions_ht_alloc(); | |
268 | if (ret) { | |
269 | ERR("Error allocating the sessions HT"); | |
270 | goto end; | |
271 | } | |
272 | } | |
273 | lttng_ht_node_init_u64(&ls->node, ls->id); | |
274 | lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node); | |
275 | ||
276 | end: | |
277 | return; | |
278 | } | |
279 | ||
280 | /* | |
281 | * Test if ltt_sessions_ht_by_id is empty. | |
282 | * Return 1 if empty, 0 if not empty. | |
283 | * The session list lock must be held. | |
284 | */ | |
def88971 | 285 | static int ltt_sessions_ht_empty(void) |
23324029 JD |
286 | { |
287 | int ret; | |
288 | ||
289 | if (!ltt_sessions_ht_by_id) { | |
290 | ret = 1; | |
291 | goto end; | |
292 | } | |
293 | ||
294 | ret = lttng_ht_get_count(ltt_sessions_ht_by_id) ? 0 : 1; | |
295 | end: | |
296 | return ret; | |
297 | } | |
298 | ||
299 | /* | |
300 | * Remove a ltt_session from the ltt_sessions_ht_by_id. | |
301 | * If empty, the ltt_sessions_ht_by_id HT is freed. | |
302 | * The session list lock must be held. | |
303 | */ | |
304 | static void del_session_ht(struct ltt_session *ls) | |
305 | { | |
306 | struct lttng_ht_iter iter; | |
307 | int ret; | |
308 | ||
309 | assert(ls); | |
310 | assert(ltt_sessions_ht_by_id); | |
311 | ||
312 | iter.iter.node = &ls->node.node; | |
313 | ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter); | |
314 | assert(!ret); | |
315 | ||
316 | if (ltt_sessions_ht_empty()) { | |
317 | DBG("Empty ltt_sessions_ht_by_id, destroying it"); | |
318 | ltt_sessions_ht_destroy(); | |
319 | } | |
320 | } | |
321 | ||
b5541356 | 322 | /* |
6c9cc2ab | 323 | * Acquire session lock |
b5541356 | 324 | */ |
54d01ffb | 325 | void session_lock(struct ltt_session *session) |
b5541356 | 326 | { |
0525e9ae DG |
327 | assert(session); |
328 | ||
6c9cc2ab DG |
329 | pthread_mutex_lock(&session->lock); |
330 | } | |
b5541356 | 331 | |
6c9cc2ab DG |
332 | /* |
333 | * Release session lock | |
334 | */ | |
54d01ffb | 335 | void session_unlock(struct ltt_session *session) |
6c9cc2ab | 336 | { |
0525e9ae DG |
337 | assert(session); |
338 | ||
6c9cc2ab | 339 | pthread_mutex_unlock(&session->lock); |
b5541356 DG |
340 | } |
341 | ||
5b74c7b1 | 342 | /* |
74babd95 | 343 | * Return a ltt_session structure ptr that matches name. If no session found, |
23324029 | 344 | * NULL is returned. This must be called with the session list lock held using |
74babd95 | 345 | * session_lock_list and session_unlock_list. |
5b74c7b1 | 346 | */ |
58a1a227 | 347 | struct ltt_session *session_find_by_name(const char *name) |
5b74c7b1 | 348 | { |
5b74c7b1 DG |
349 | struct ltt_session *iter; |
350 | ||
0525e9ae DG |
351 | assert(name); |
352 | ||
5f822d0a DG |
353 | DBG2("Trying to find session by name %s", name); |
354 | ||
5b74c7b1 | 355 | cds_list_for_each_entry(iter, <t_session_list.head, list) { |
1b110e1b | 356 | if (strncmp(iter->name, name, NAME_MAX) == 0) { |
74babd95 | 357 | goto found; |
5b74c7b1 DG |
358 | } |
359 | } | |
360 | ||
74babd95 | 361 | iter = NULL; |
5b74c7b1 | 362 | |
74babd95 | 363 | found: |
5b74c7b1 DG |
364 | return iter; |
365 | } | |
366 | ||
23324029 JD |
367 | /* |
368 | * Return an ltt_session that matches the id. If no session is found, | |
369 | * NULL is returned. This must be called with rcu_read_lock and | |
370 | * session list lock held (to guarantee the lifetime of the session). | |
371 | */ | |
372 | struct ltt_session *session_find_by_id(uint64_t id) | |
373 | { | |
374 | struct lttng_ht_node_u64 *node; | |
375 | struct lttng_ht_iter iter; | |
376 | struct ltt_session *ls; | |
377 | ||
d68ec974 JG |
378 | if (!ltt_sessions_ht_by_id) { |
379 | goto end; | |
380 | } | |
381 | ||
23324029 JD |
382 | lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter); |
383 | node = lttng_ht_iter_get_node_u64(&iter); | |
384 | if (node == NULL) { | |
d68ec974 | 385 | goto end; |
23324029 JD |
386 | } |
387 | ls = caa_container_of(node, struct ltt_session, node); | |
388 | ||
389 | DBG3("Session %" PRIu64 " found by id.", id); | |
390 | return ls; | |
391 | ||
d68ec974 | 392 | end: |
23324029 JD |
393 | DBG3("Session %" PRIu64 " NOT found by id", id); |
394 | return NULL; | |
395 | } | |
396 | ||
5b74c7b1 | 397 | /* |
050349bb | 398 | * Delete session from the session list and free the memory. |
5b74c7b1 | 399 | * |
050349bb | 400 | * Return -1 if no session is found. On success, return 1; |
36b588ed | 401 | * Should *NOT* be called with RCU read-side lock held. |
5b74c7b1 | 402 | */ |
271933a4 | 403 | int session_destroy(struct ltt_session *session) |
5b74c7b1 | 404 | { |
271933a4 | 405 | /* Safety check */ |
0525e9ae | 406 | assert(session); |
271933a4 | 407 | |
5c408ad8 | 408 | DBG("Destroying session %s (id %" PRIu64 ")", session->name, session->id); |
271933a4 | 409 | del_session_list(session); |
271933a4 | 410 | pthread_mutex_destroy(&session->lock); |
23324029 | 411 | del_session_ht(session); |
07424f16 | 412 | |
6addfa37 | 413 | consumer_output_put(session->consumer); |
6dc3064a | 414 | snapshot_destroy(&session->snapshot); |
271933a4 | 415 | free(session); |
5b74c7b1 | 416 | |
f73fabfd | 417 | return LTTNG_OK; |
5b74c7b1 DG |
418 | } |
419 | ||
420 | /* | |
050349bb | 421 | * Create a brand new session and add it to the session list. |
5b74c7b1 | 422 | */ |
dec56f6c | 423 | int session_create(char *name, uid_t uid, gid_t gid) |
5b74c7b1 | 424 | { |
f3ed775e | 425 | int ret; |
5b74c7b1 | 426 | struct ltt_session *new_session; |
e07ae692 | 427 | |
5b74c7b1 | 428 | /* Allocate session data structure */ |
ba7f0ae5 | 429 | new_session = zmalloc(sizeof(struct ltt_session)); |
5b74c7b1 | 430 | if (new_session == NULL) { |
df0f840b | 431 | PERROR("zmalloc"); |
f73fabfd | 432 | ret = LTTNG_ERR_FATAL; |
f3ed775e | 433 | goto error_malloc; |
5b74c7b1 DG |
434 | } |
435 | ||
f3ed775e | 436 | /* Define session name */ |
5b74c7b1 | 437 | if (name != NULL) { |
74babd95 | 438 | if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) { |
f73fabfd | 439 | ret = LTTNG_ERR_FATAL; |
f3ed775e | 440 | goto error_asprintf; |
5b74c7b1 DG |
441 | } |
442 | } else { | |
f3ed775e | 443 | ERR("No session name given"); |
f73fabfd | 444 | ret = LTTNG_ERR_FATAL; |
f3ed775e DG |
445 | goto error; |
446 | } | |
447 | ||
1c1c3634 DG |
448 | ret = validate_name(name); |
449 | if (ret < 0) { | |
450 | ret = LTTNG_ERR_SESSION_INVALID_CHAR; | |
451 | goto error; | |
452 | } | |
453 | ||
d3e2ba59 | 454 | ret = gethostname(new_session->hostname, sizeof(new_session->hostname)); |
73184835 DG |
455 | if (ret < 0) { |
456 | if (errno == ENAMETOOLONG) { | |
457 | new_session->hostname[sizeof(new_session->hostname) - 1] = '\0'; | |
458 | } else { | |
459 | ret = LTTNG_ERR_FATAL; | |
460 | goto error; | |
461 | } | |
d3e2ba59 JD |
462 | } |
463 | ||
1d4b027a DG |
464 | /* Init kernel session */ |
465 | new_session->kernel_session = NULL; | |
f6a9efaa | 466 | new_session->ust_session = NULL; |
1657e9bb | 467 | |
0177d773 DG |
468 | /* Init lock */ |
469 | pthread_mutex_init(&new_session->lock, NULL); | |
5b74c7b1 | 470 | |
6df2e2c9 MD |
471 | new_session->uid = uid; |
472 | new_session->gid = gid; | |
473 | ||
6dc3064a DG |
474 | ret = snapshot_init(&new_session->snapshot); |
475 | if (ret < 0) { | |
476 | ret = LTTNG_ERR_NOMEM; | |
477 | goto error; | |
478 | } | |
479 | ||
5c408ad8 | 480 | new_session->rotate_pending = false; |
4f23c583 | 481 | new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION; |
5c408ad8 | 482 | new_session->rotate_pending_relay = false; |
d88744a4 | 483 | new_session->rotate_relay_pending_timer_enabled = false; |
259c2674 | 484 | new_session->rotate_timer = false; |
5c408ad8 | 485 | |
b5541356 | 486 | /* Add new session to the session list */ |
54d01ffb | 487 | session_lock_list(); |
a991f516 | 488 | new_session->id = add_session_list(new_session); |
23324029 JD |
489 | /* |
490 | * Add the new session to the ltt_sessions_ht_by_id. | |
491 | * No ownership is taken by the hash table; it is merely | |
492 | * a wrapper around the session list used for faster access | |
493 | * by session id. | |
494 | */ | |
495 | add_session_ht(new_session); | |
54d01ffb | 496 | session_unlock_list(); |
b5541356 | 497 | |
a4b92340 DG |
498 | /* |
499 | * Consumer is let to NULL since the create_session_uri command will set it | |
500 | * up and, if valid, assign it to the session. | |
501 | */ | |
d022620a DG |
502 | DBG("Tracing session %s created with ID %" PRIu64 " by UID %d GID %d", |
503 | name, new_session->id, new_session->uid, new_session->gid); | |
b082db07 | 504 | |
f73fabfd | 505 | return LTTNG_OK; |
5b74c7b1 DG |
506 | |
507 | error: | |
f3ed775e | 508 | error_asprintf: |
0e428499 | 509 | free(new_session); |
5b74c7b1 | 510 | |
f3ed775e DG |
511 | error_malloc: |
512 | return ret; | |
5b74c7b1 | 513 | } |
2f77fc4b DG |
514 | |
515 | /* | |
516 | * Check if the UID or GID match the session. Root user has access to all | |
517 | * sessions. | |
518 | */ | |
519 | int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid) | |
520 | { | |
521 | assert(session); | |
522 | ||
523 | if (uid != session->uid && gid != session->gid && uid != 0) { | |
524 | return 0; | |
525 | } else { | |
526 | return 1; | |
527 | } | |
528 | } |