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