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> |
5d65beab | 31 | #include <lttng/location-internal.h> |
e32d7f27 JG |
32 | #include "lttng-sessiond.h" |
33 | #include "kernel.h" | |
1e307fab | 34 | |
5b74c7b1 | 35 | #include "session.h" |
23324029 | 36 | #include "utils.h" |
dd73d57b | 37 | #include "trace-ust.h" |
a7333da7 | 38 | #include "timer.h" |
5b74c7b1 | 39 | |
8c0faa1d | 40 | /* |
b5541356 | 41 | * NOTES: |
8c0faa1d | 42 | * |
b5541356 DG |
43 | * No ltt_session.lock is taken here because those data structure are widely |
44 | * spread across the lttng-tools code base so before caling functions below | |
45 | * that can read/write a session, the caller MUST acquire the session lock | |
54d01ffb | 46 | * using session_lock() and session_unlock(). |
8c0faa1d | 47 | */ |
8c0faa1d | 48 | |
5b74c7b1 | 49 | /* |
b5541356 | 50 | * Init tracing session list. |
5b74c7b1 | 51 | * |
b5541356 | 52 | * Please see session.h for more explanation and correct usage of the list. |
5b74c7b1 | 53 | */ |
b5541356 DG |
54 | static struct ltt_session_list ltt_session_list = { |
55 | .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), | |
56 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a24f7994 | 57 | .next_uuid = 0, |
b5541356 | 58 | }; |
5b74c7b1 | 59 | |
1c1c3634 DG |
60 | /* These characters are forbidden in a session name. Used by validate_name. */ |
61 | static const char *forbidden_name_chars = "/"; | |
62 | ||
23324029 JD |
63 | /* Global hash table to keep the sessions, indexed by id. */ |
64 | static struct lttng_ht *ltt_sessions_ht_by_id = NULL; | |
65 | ||
1c1c3634 DG |
66 | /* |
67 | * Validate the session name for forbidden characters. | |
68 | * | |
69 | * Return 0 on success else -1 meaning a forbidden char. has been found. | |
70 | */ | |
71 | static int validate_name(const char *name) | |
72 | { | |
73 | int ret; | |
74 | char *tok, *tmp_name; | |
75 | ||
76 | assert(name); | |
77 | ||
78 | tmp_name = strdup(name); | |
79 | if (!tmp_name) { | |
80 | /* ENOMEM here. */ | |
81 | ret = -1; | |
82 | goto error; | |
83 | } | |
84 | ||
85 | tok = strpbrk(tmp_name, forbidden_name_chars); | |
86 | if (tok) { | |
87 | DBG("Session name %s contains a forbidden character", name); | |
88 | /* Forbidden character has been found. */ | |
89 | ret = -1; | |
90 | goto error; | |
91 | } | |
92 | ret = 0; | |
93 | ||
94 | error: | |
95 | free(tmp_name); | |
96 | return ret; | |
97 | } | |
98 | ||
5b74c7b1 | 99 | /* |
050349bb | 100 | * Add a ltt_session structure to the global list. |
5b74c7b1 | 101 | * |
050349bb | 102 | * The caller MUST acquire the session list lock before. |
44e96653 | 103 | * Returns the unique identifier for the session. |
5b74c7b1 | 104 | */ |
d022620a | 105 | static uint64_t add_session_list(struct ltt_session *ls) |
5b74c7b1 | 106 | { |
0525e9ae DG |
107 | assert(ls); |
108 | ||
5b74c7b1 | 109 | cds_list_add(&ls->list, <t_session_list.head); |
a24f7994 | 110 | return ltt_session_list.next_uuid++; |
5b74c7b1 DG |
111 | } |
112 | ||
113 | /* | |
050349bb | 114 | * Delete a ltt_session structure to the global list. |
b5541356 | 115 | * |
050349bb | 116 | * The caller MUST acquire the session list lock before. |
5b74c7b1 DG |
117 | */ |
118 | static void del_session_list(struct ltt_session *ls) | |
119 | { | |
0525e9ae DG |
120 | assert(ls); |
121 | ||
5b74c7b1 | 122 | cds_list_del(&ls->list); |
5b74c7b1 DG |
123 | } |
124 | ||
b5541356 | 125 | /* |
050349bb | 126 | * Return a pointer to the session list. |
b5541356 | 127 | */ |
54d01ffb | 128 | struct ltt_session_list *session_get_list(void) |
b5541356 DG |
129 | { |
130 | return <t_session_list; | |
131 | } | |
132 | ||
133 | /* | |
6c9cc2ab | 134 | * Acquire session list lock |
b5541356 | 135 | */ |
54d01ffb | 136 | void session_lock_list(void) |
b5541356 | 137 | { |
6c9cc2ab | 138 | pthread_mutex_lock(<t_session_list.lock); |
b5541356 DG |
139 | } |
140 | ||
71e0a100 JG |
141 | /* |
142 | * Try to acquire session list lock | |
143 | */ | |
144 | int session_trylock_list(void) | |
145 | { | |
146 | return pthread_mutex_trylock(<t_session_list.lock); | |
147 | } | |
148 | ||
b5541356 | 149 | /* |
6c9cc2ab | 150 | * Release session list lock |
b5541356 | 151 | */ |
54d01ffb | 152 | void session_unlock_list(void) |
b5541356 | 153 | { |
6c9cc2ab | 154 | pthread_mutex_unlock(<t_session_list.lock); |
b5541356 DG |
155 | } |
156 | ||
dd73d57b JG |
157 | /* |
158 | * Get the session's consumer destination type. | |
159 | * | |
160 | * The caller must hold the session lock. | |
161 | */ | |
162 | enum consumer_dst_type session_get_consumer_destination_type( | |
163 | const struct ltt_session *session) | |
164 | { | |
165 | /* | |
166 | * The output information is duplicated in both of those session types. | |
167 | * Hence, it doesn't matter from which it is retrieved. However, it is | |
168 | * possible for only one of them to be set. | |
169 | */ | |
170 | return session->kernel_session ? | |
171 | session->kernel_session->consumer->type : | |
172 | session->ust_session->consumer->type; | |
173 | } | |
174 | ||
175 | /* | |
176 | * Get the session's consumer network hostname. | |
177 | * The caller must ensure that the destination is of type "net". | |
178 | * | |
179 | * The caller must hold the session lock. | |
180 | */ | |
181 | const char *session_get_net_consumer_hostname(const struct ltt_session *session) | |
182 | { | |
183 | const char *hostname = NULL; | |
184 | const struct consumer_output *output; | |
185 | ||
186 | output = session->kernel_session ? | |
187 | session->kernel_session->consumer : | |
188 | session->ust_session->consumer; | |
189 | ||
190 | /* | |
191 | * hostname is assumed to be the same for both control and data | |
192 | * connections. | |
193 | */ | |
194 | switch (output->dst.net.control.dtype) { | |
195 | case LTTNG_DST_IPV4: | |
196 | hostname = output->dst.net.control.dst.ipv4; | |
197 | break; | |
198 | case LTTNG_DST_IPV6: | |
199 | hostname = output->dst.net.control.dst.ipv6; | |
200 | break; | |
201 | default: | |
202 | abort(); | |
203 | } | |
204 | return hostname; | |
205 | } | |
206 | ||
207 | /* | |
208 | * Get the session's consumer network control and data ports. | |
209 | * The caller must ensure that the destination is of type "net". | |
210 | * | |
211 | * The caller must hold the session lock. | |
212 | */ | |
213 | void session_get_net_consumer_ports(const struct ltt_session *session, | |
214 | uint16_t *control_port, uint16_t *data_port) | |
215 | { | |
216 | const struct consumer_output *output; | |
217 | ||
218 | output = session->kernel_session ? | |
219 | session->kernel_session->consumer : | |
220 | session->ust_session->consumer; | |
221 | *control_port = output->dst.net.control.port; | |
222 | *data_port = output->dst.net.data.port; | |
223 | } | |
224 | ||
5d65beab JG |
225 | /* |
226 | * Get the location of the latest trace archive produced by a rotation. | |
227 | * | |
228 | * The caller must hold the session lock. | |
229 | */ | |
230 | struct lttng_trace_archive_location *session_get_trace_archive_location( | |
231 | struct ltt_session *session) | |
232 | { | |
233 | struct lttng_trace_archive_location *location = NULL; | |
234 | ||
235 | if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) { | |
236 | goto end; | |
237 | } | |
238 | ||
239 | switch (session_get_consumer_destination_type(session)) { | |
240 | case CONSUMER_DST_LOCAL: | |
241 | location = lttng_trace_archive_location_local_create( | |
242 | session->rotation_chunk.current_rotate_path); | |
243 | break; | |
244 | case CONSUMER_DST_NET: | |
245 | { | |
246 | const char *hostname; | |
247 | uint16_t control_port, data_port; | |
248 | ||
249 | hostname = session_get_net_consumer_hostname(session); | |
250 | session_get_net_consumer_ports(session, | |
251 | &control_port, | |
252 | &data_port); | |
253 | location = lttng_trace_archive_location_relay_create( | |
254 | hostname, | |
255 | LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP, | |
256 | control_port, data_port, | |
257 | session->rotation_chunk.current_rotate_path); | |
258 | break; | |
259 | } | |
260 | default: | |
261 | abort(); | |
262 | } | |
263 | end: | |
264 | return location; | |
265 | } | |
266 | ||
23324029 JD |
267 | /* |
268 | * Allocate the ltt_sessions_ht_by_id HT. | |
9c6518bc JG |
269 | * |
270 | * The session list lock must be held. | |
23324029 JD |
271 | */ |
272 | int ltt_sessions_ht_alloc(void) | |
273 | { | |
274 | int ret = 0; | |
275 | ||
276 | DBG("Allocating ltt_sessions_ht_by_id"); | |
277 | ltt_sessions_ht_by_id = lttng_ht_new(0, LTTNG_HT_TYPE_U64); | |
278 | if (!ltt_sessions_ht_by_id) { | |
279 | ret = -1; | |
280 | ERR("Failed to allocate ltt_sessions_ht_by_id"); | |
281 | goto end; | |
282 | } | |
283 | end: | |
284 | return ret; | |
285 | } | |
286 | ||
287 | /* | |
288 | * Destroy the ltt_sessions_ht_by_id HT. | |
9c6518bc JG |
289 | * |
290 | * The session list lock must be held. | |
23324029 | 291 | */ |
accdc9bf | 292 | static void ltt_sessions_ht_destroy(void) |
23324029 JD |
293 | { |
294 | if (!ltt_sessions_ht_by_id) { | |
295 | return; | |
296 | } | |
297 | ht_cleanup_push(ltt_sessions_ht_by_id); | |
298 | ltt_sessions_ht_by_id = NULL; | |
299 | } | |
300 | ||
301 | /* | |
302 | * Add a ltt_session to the ltt_sessions_ht_by_id. | |
303 | * If unallocated, the ltt_sessions_ht_by_id HT is allocated. | |
304 | * The session list lock must be held. | |
305 | */ | |
306 | static void add_session_ht(struct ltt_session *ls) | |
307 | { | |
308 | int ret; | |
309 | ||
310 | assert(ls); | |
311 | ||
312 | if (!ltt_sessions_ht_by_id) { | |
313 | ret = ltt_sessions_ht_alloc(); | |
314 | if (ret) { | |
315 | ERR("Error allocating the sessions HT"); | |
316 | goto end; | |
317 | } | |
318 | } | |
319 | lttng_ht_node_init_u64(&ls->node, ls->id); | |
320 | lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node); | |
321 | ||
322 | end: | |
323 | return; | |
324 | } | |
325 | ||
326 | /* | |
327 | * Test if ltt_sessions_ht_by_id is empty. | |
328 | * Return 1 if empty, 0 if not empty. | |
329 | * The session list lock must be held. | |
330 | */ | |
def88971 | 331 | static int ltt_sessions_ht_empty(void) |
23324029 JD |
332 | { |
333 | int ret; | |
334 | ||
335 | if (!ltt_sessions_ht_by_id) { | |
336 | ret = 1; | |
337 | goto end; | |
338 | } | |
339 | ||
340 | ret = lttng_ht_get_count(ltt_sessions_ht_by_id) ? 0 : 1; | |
341 | end: | |
342 | return ret; | |
343 | } | |
344 | ||
345 | /* | |
346 | * Remove a ltt_session from the ltt_sessions_ht_by_id. | |
347 | * If empty, the ltt_sessions_ht_by_id HT is freed. | |
348 | * The session list lock must be held. | |
349 | */ | |
350 | static void del_session_ht(struct ltt_session *ls) | |
351 | { | |
352 | struct lttng_ht_iter iter; | |
353 | int ret; | |
354 | ||
355 | assert(ls); | |
356 | assert(ltt_sessions_ht_by_id); | |
357 | ||
358 | iter.iter.node = &ls->node.node; | |
359 | ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter); | |
360 | assert(!ret); | |
361 | ||
362 | if (ltt_sessions_ht_empty()) { | |
363 | DBG("Empty ltt_sessions_ht_by_id, destroying it"); | |
364 | ltt_sessions_ht_destroy(); | |
365 | } | |
366 | } | |
367 | ||
b5541356 | 368 | /* |
6c9cc2ab | 369 | * Acquire session lock |
b5541356 | 370 | */ |
54d01ffb | 371 | void session_lock(struct ltt_session *session) |
b5541356 | 372 | { |
0525e9ae DG |
373 | assert(session); |
374 | ||
6c9cc2ab DG |
375 | pthread_mutex_lock(&session->lock); |
376 | } | |
b5541356 | 377 | |
6c9cc2ab DG |
378 | /* |
379 | * Release session lock | |
380 | */ | |
54d01ffb | 381 | void session_unlock(struct ltt_session *session) |
6c9cc2ab | 382 | { |
0525e9ae DG |
383 | assert(session); |
384 | ||
6c9cc2ab | 385 | pthread_mutex_unlock(&session->lock); |
b5541356 DG |
386 | } |
387 | ||
e32d7f27 JG |
388 | static |
389 | void session_release(struct urcu_ref *ref) | |
390 | { | |
391 | int ret; | |
392 | struct ltt_ust_session *usess; | |
393 | struct ltt_kernel_session *ksess; | |
394 | struct ltt_session *session = container_of(ref, typeof(*session), ref); | |
395 | ||
396 | usess = session->ust_session; | |
397 | ksess = session->kernel_session; | |
398 | ||
399 | /* Clean kernel session teardown */ | |
400 | kernel_destroy_session(ksess); | |
401 | ||
402 | /* UST session teardown */ | |
403 | if (usess) { | |
404 | /* Close any relayd session */ | |
405 | consumer_output_send_destroy_relayd(usess->consumer); | |
406 | ||
407 | /* Destroy every UST application related to this session. */ | |
408 | ret = ust_app_destroy_trace_all(usess); | |
409 | if (ret) { | |
410 | ERR("Error in ust_app_destroy_trace_all"); | |
411 | } | |
412 | ||
413 | /* Clean up the rest. */ | |
414 | trace_ust_destroy_session(usess); | |
415 | } | |
416 | ||
417 | /* | |
418 | * Must notify the kernel thread here to update it's poll set in order to | |
419 | * remove the channel(s)' fd just destroyed. | |
420 | */ | |
421 | ret = notify_thread_pipe(kernel_poll_pipe[1]); | |
422 | if (ret < 0) { | |
423 | PERROR("write kernel poll pipe"); | |
424 | } | |
425 | ||
426 | DBG("Destroying session %s (id %" PRIu64 ")", session->name, session->id); | |
427 | pthread_mutex_destroy(&session->lock); | |
428 | ||
429 | consumer_output_put(session->consumer); | |
430 | snapshot_destroy(&session->snapshot); | |
431 | del_session_list(session); | |
432 | del_session_ht(session); | |
433 | free(session); | |
434 | } | |
435 | ||
436 | /* | |
437 | * Acquire a reference to a session. | |
438 | * This function may fail (return false); its return value must be checked. | |
439 | */ | |
440 | bool session_get(struct ltt_session *session) | |
441 | { | |
442 | return urcu_ref_get_unless_zero(&session->ref); | |
443 | } | |
444 | ||
445 | /* | |
446 | * Release a reference to a session. | |
447 | */ | |
448 | void session_put(struct ltt_session *session) | |
449 | { | |
450 | /* | |
451 | * The session list lock must be held as any session_put() | |
452 | * may cause the removal of the session from the session_list. | |
453 | */ | |
454 | ASSERT_LOCKED(ltt_session_list.lock); | |
455 | assert(session->ref.refcount); | |
456 | urcu_ref_put(&session->ref, session_release); | |
457 | } | |
458 | ||
459 | /* | |
460 | * Destroy a session. | |
461 | * | |
462 | * This method does not immediately release/free the session as other | |
463 | * components may still hold a reference to the session. However, | |
464 | * the session should no longer be presented to the user. | |
465 | * | |
466 | * Releases the session list's reference to the session | |
467 | * and marks it as destroyed. Iterations on the session list should be | |
468 | * mindful of the "destroyed" flag. | |
469 | */ | |
470 | void session_destroy(struct ltt_session *session) | |
471 | { | |
472 | assert(!session->destroyed); | |
473 | session->destroyed = true; | |
474 | session_put(session); | |
475 | } | |
476 | ||
5b74c7b1 | 477 | /* |
74babd95 | 478 | * Return a ltt_session structure ptr that matches name. If no session found, |
23324029 | 479 | * NULL is returned. This must be called with the session list lock held using |
74babd95 | 480 | * session_lock_list and session_unlock_list. |
e32d7f27 | 481 | * A reference to the session is implicitly acquired by this function. |
5b74c7b1 | 482 | */ |
58a1a227 | 483 | struct ltt_session *session_find_by_name(const char *name) |
5b74c7b1 | 484 | { |
5b74c7b1 DG |
485 | struct ltt_session *iter; |
486 | ||
0525e9ae | 487 | assert(name); |
e32d7f27 | 488 | ASSERT_LOCKED(ltt_session_list.lock); |
0525e9ae | 489 | |
5f822d0a DG |
490 | DBG2("Trying to find session by name %s", name); |
491 | ||
5b74c7b1 | 492 | cds_list_for_each_entry(iter, <t_session_list.head, list) { |
e32d7f27 JG |
493 | if (!strncmp(iter->name, name, NAME_MAX) && |
494 | !iter->destroyed) { | |
74babd95 | 495 | goto found; |
5b74c7b1 DG |
496 | } |
497 | } | |
498 | ||
e32d7f27 | 499 | return NULL; |
74babd95 | 500 | found: |
e32d7f27 | 501 | return session_get(iter) ? iter : NULL; |
5b74c7b1 DG |
502 | } |
503 | ||
23324029 JD |
504 | /* |
505 | * Return an ltt_session that matches the id. If no session is found, | |
506 | * NULL is returned. This must be called with rcu_read_lock and | |
507 | * session list lock held (to guarantee the lifetime of the session). | |
508 | */ | |
509 | struct ltt_session *session_find_by_id(uint64_t id) | |
510 | { | |
511 | struct lttng_ht_node_u64 *node; | |
512 | struct lttng_ht_iter iter; | |
513 | struct ltt_session *ls; | |
514 | ||
e32d7f27 JG |
515 | ASSERT_LOCKED(ltt_session_list.lock); |
516 | ||
d68ec974 JG |
517 | if (!ltt_sessions_ht_by_id) { |
518 | goto end; | |
519 | } | |
520 | ||
23324029 JD |
521 | lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter); |
522 | node = lttng_ht_iter_get_node_u64(&iter); | |
523 | if (node == NULL) { | |
d68ec974 | 524 | goto end; |
23324029 JD |
525 | } |
526 | ls = caa_container_of(node, struct ltt_session, node); | |
527 | ||
528 | DBG3("Session %" PRIu64 " found by id.", id); | |
e32d7f27 | 529 | return session_get(ls) ? ls : NULL; |
23324029 | 530 | |
d68ec974 | 531 | end: |
23324029 JD |
532 | DBG3("Session %" PRIu64 " NOT found by id", id); |
533 | return NULL; | |
534 | } | |
535 | ||
5b74c7b1 | 536 | /* |
050349bb | 537 | * Create a brand new session and add it to the session list. |
5b74c7b1 | 538 | */ |
dec56f6c | 539 | int session_create(char *name, uid_t uid, gid_t gid) |
5b74c7b1 | 540 | { |
f3ed775e | 541 | int ret; |
5b74c7b1 | 542 | struct ltt_session *new_session; |
e07ae692 | 543 | |
5b74c7b1 | 544 | /* Allocate session data structure */ |
ba7f0ae5 | 545 | new_session = zmalloc(sizeof(struct ltt_session)); |
5b74c7b1 | 546 | if (new_session == NULL) { |
df0f840b | 547 | PERROR("zmalloc"); |
f73fabfd | 548 | ret = LTTNG_ERR_FATAL; |
f3ed775e | 549 | goto error_malloc; |
5b74c7b1 DG |
550 | } |
551 | ||
e32d7f27 JG |
552 | urcu_ref_init(&new_session->ref); |
553 | ||
f3ed775e | 554 | /* Define session name */ |
5b74c7b1 | 555 | if (name != NULL) { |
74babd95 | 556 | if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) { |
f73fabfd | 557 | ret = LTTNG_ERR_FATAL; |
f3ed775e | 558 | goto error_asprintf; |
5b74c7b1 DG |
559 | } |
560 | } else { | |
f3ed775e | 561 | ERR("No session name given"); |
f73fabfd | 562 | ret = LTTNG_ERR_FATAL; |
f3ed775e DG |
563 | goto error; |
564 | } | |
565 | ||
1c1c3634 DG |
566 | ret = validate_name(name); |
567 | if (ret < 0) { | |
568 | ret = LTTNG_ERR_SESSION_INVALID_CHAR; | |
569 | goto error; | |
570 | } | |
571 | ||
d3e2ba59 | 572 | ret = gethostname(new_session->hostname, sizeof(new_session->hostname)); |
73184835 DG |
573 | if (ret < 0) { |
574 | if (errno == ENAMETOOLONG) { | |
575 | new_session->hostname[sizeof(new_session->hostname) - 1] = '\0'; | |
576 | } else { | |
577 | ret = LTTNG_ERR_FATAL; | |
578 | goto error; | |
579 | } | |
d3e2ba59 JD |
580 | } |
581 | ||
1d4b027a DG |
582 | /* Init kernel session */ |
583 | new_session->kernel_session = NULL; | |
f6a9efaa | 584 | new_session->ust_session = NULL; |
1657e9bb | 585 | |
0177d773 DG |
586 | /* Init lock */ |
587 | pthread_mutex_init(&new_session->lock, NULL); | |
5b74c7b1 | 588 | |
6df2e2c9 MD |
589 | new_session->uid = uid; |
590 | new_session->gid = gid; | |
591 | ||
6dc3064a DG |
592 | ret = snapshot_init(&new_session->snapshot); |
593 | if (ret < 0) { | |
594 | ret = LTTNG_ERR_NOMEM; | |
595 | goto error; | |
596 | } | |
597 | ||
92816cc3 JG |
598 | new_session->rotation_pending_local = false; |
599 | new_session->rotation_pending_relay = false; | |
4f23c583 | 600 | new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION; |
92816cc3 JG |
601 | |
602 | new_session->rotation_pending_check_timer_enabled = false; | |
603 | new_session->rotation_schedule_timer_enabled = false; | |
5c408ad8 | 604 | |
b5541356 | 605 | /* Add new session to the session list */ |
54d01ffb | 606 | session_lock_list(); |
a991f516 | 607 | new_session->id = add_session_list(new_session); |
23324029 JD |
608 | /* |
609 | * Add the new session to the ltt_sessions_ht_by_id. | |
610 | * No ownership is taken by the hash table; it is merely | |
611 | * a wrapper around the session list used for faster access | |
612 | * by session id. | |
613 | */ | |
614 | add_session_ht(new_session); | |
54d01ffb | 615 | session_unlock_list(); |
b5541356 | 616 | |
a4b92340 DG |
617 | /* |
618 | * Consumer is let to NULL since the create_session_uri command will set it | |
619 | * up and, if valid, assign it to the session. | |
620 | */ | |
d022620a DG |
621 | DBG("Tracing session %s created with ID %" PRIu64 " by UID %d GID %d", |
622 | name, new_session->id, new_session->uid, new_session->gid); | |
b082db07 | 623 | |
f73fabfd | 624 | return LTTNG_OK; |
5b74c7b1 DG |
625 | |
626 | error: | |
f3ed775e | 627 | error_asprintf: |
0e428499 | 628 | free(new_session); |
5b74c7b1 | 629 | |
f3ed775e DG |
630 | error_malloc: |
631 | return ret; | |
5b74c7b1 | 632 | } |
2f77fc4b DG |
633 | |
634 | /* | |
635 | * Check if the UID or GID match the session. Root user has access to all | |
636 | * sessions. | |
637 | */ | |
638 | int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid) | |
639 | { | |
640 | assert(session); | |
641 | ||
642 | if (uid != session->uid && gid != session->gid && uid != 0) { | |
643 | return 0; | |
644 | } else { | |
645 | return 1; | |
646 | } | |
647 | } | |
2961f09e JG |
648 | |
649 | /* | |
650 | * Set a session's rotation state and reset all associated state. | |
651 | * | |
652 | * This function resets the rotation state (check timers, pending | |
653 | * flags, etc.) and sets the result of the last rotation. The result | |
654 | * can be queries by a liblttng-ctl client. | |
655 | * | |
656 | * Be careful of the result passed to this function. For instance, | |
657 | * on failure to launch a rotation, a client will expect the rotation | |
658 | * state to be set to "NO_ROTATION". If an error occured while the | |
659 | * rotation was "ONGOING", result should be set to "ERROR", which will | |
660 | * allow a client to report it. | |
661 | * | |
662 | * Must be called with the session and session_list locks held. | |
663 | */ | |
664 | int session_reset_rotation_state(struct ltt_session *session, | |
665 | enum lttng_rotation_state result) | |
666 | { | |
667 | int ret = 0; | |
668 | ||
669 | ASSERT_LOCKED(ltt_session_list.lock); | |
670 | ASSERT_LOCKED(session->lock); | |
671 | ||
672 | session->rotation_pending_local = false; | |
673 | session->rotation_pending_relay = false; | |
674 | session->rotated_after_last_stop = false; | |
675 | session->rotation_state = result; | |
676 | if (session->rotation_pending_check_timer_enabled) { | |
677 | ret = timer_session_rotation_pending_check_stop(session); | |
678 | } | |
679 | return ret; | |
680 | } |