Remove stale comment
[lttng-ust.git] / libust / lttng-ust-comm.c
CommitLineData
2691221a
MD
1/*
2 * lttng-ust-comm.c
3 *
4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; only
10 * version 2.1 of the License.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <sys/types.h>
23#include <sys/socket.h>
2629549e 24#include <sys/prctl.h>
2691221a
MD
25#include <unistd.h>
26#include <errno.h>
d9e99d10 27#include <pthread.h>
11ff9c7d
MD
28#include <semaphore.h>
29#include <time.h>
1ea11eab 30#include <assert.h>
e822f505 31#include <signal.h>
95259bd0 32#include <urcu/uatomic.h>
1ea11eab 33
edaa1431
MD
34#include <lttng-ust-comm.h>
35#include <ust/usterr-signal-safe.h>
36#include <ust/lttng-ust-abi.h>
37#include <ust/tracepoint.h>
d0a1ae63 38#include <ust/tracepoint-internal.h>
e822f505 39#include <ust/ust.h>
b751f722 40#include "ltt-tracer-core.h"
edaa1431
MD
41
42/*
43 * Has lttng ust comm constructor been called ?
44 */
45static int initialized;
46
1ea11eab 47/*
17dfb34b
MD
48 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
49 * Held when handling a command, also held by fork() to deal with
50 * removal of threads, and by exit path.
1ea11eab 51 */
1ea11eab
MD
52
53/* Should the ust comm thread quit ? */
54static int lttng_ust_comm_should_quit;
55
11ff9c7d
MD
56/*
57 * Wait for either of these before continuing to the main
58 * program:
59 * - the register_done message from sessiond daemon
60 * (will let the sessiond daemon enable sessions before main
61 * starts.)
62 * - sessiond daemon is not reachable.
63 * - timeout (ensuring applications are resilient to session
64 * daemon problems).
65 */
66static sem_t constructor_wait;
950aab0c
MD
67/*
68 * Doing this for both the global and local sessiond.
69 */
95259bd0 70static int sem_count = { 2 };
11ff9c7d 71
1ea11eab
MD
72/*
73 * Info about socket and associated listener thread.
74 */
75struct sock_info {
11ff9c7d 76 const char *name;
1ea11eab
MD
77 char sock_path[PATH_MAX];
78 int socket;
79 pthread_t ust_listener; /* listener thread */
46050b1a 80 int root_handle;
8d20bf54
MD
81 int constructor_sem_posted;
82 int allowed;
1ea11eab 83};
2691221a
MD
84
85/* Socket from app (connect) to session daemon (listen) for communication */
1ea11eab 86struct sock_info global_apps = {
11ff9c7d 87 .name = "global",
1ea11eab
MD
88 .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK,
89 .socket = -1,
46050b1a 90 .root_handle = -1,
8d20bf54 91 .allowed = 1,
1ea11eab 92};
2691221a
MD
93
94/* TODO: allow global_apps_sock_path override */
95
1ea11eab 96struct sock_info local_apps = {
11ff9c7d 97 .name = "local",
1ea11eab 98 .socket = -1,
46050b1a 99 .root_handle = -1,
8d20bf54 100 .allowed = 0, /* Check setuid bit first */
1ea11eab 101};
2691221a 102
edaa1431
MD
103extern void ltt_ring_buffer_client_overwrite_init(void);
104extern void ltt_ring_buffer_client_discard_init(void);
105extern void ltt_ring_buffer_metadata_client_init(void);
106extern void ltt_ring_buffer_client_overwrite_exit(void);
107extern void ltt_ring_buffer_client_discard_exit(void);
108extern void ltt_ring_buffer_metadata_client_exit(void);
109
2691221a 110static
8d20bf54 111int setup_local_apps(void)
2691221a
MD
112{
113 const char *home_dir;
2691221a 114
8d20bf54
MD
115 /*
116 * Disallow per-user tracing for setuid binaries.
117 */
118 if (getuid() != geteuid()) {
119 local_apps.allowed = 0;
d0a1ae63 120 return 0;
8d20bf54
MD
121 } else {
122 local_apps.allowed = 1;
123 }
2691221a
MD
124 home_dir = (const char *) getenv("HOME");
125 if (!home_dir)
126 return -ENOENT;
1ea11eab 127 snprintf(local_apps.sock_path, PATH_MAX,
2691221a 128 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
2691221a
MD
129 return 0;
130}
131
132static
133int register_app_to_sessiond(int socket)
134{
135 ssize_t ret;
2629549e 136 int prctl_ret;
2691221a 137 struct {
e44418f3
MD
138 uint32_t major;
139 uint32_t minor;
2691221a 140 pid_t pid;
5c33bde8 141 pid_t ppid;
2691221a 142 uid_t uid;
83610856 143 gid_t gid;
2629549e 144 char name[16]; /* process name */
2691221a
MD
145 } reg_msg;
146
e44418f3
MD
147 reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR;
148 reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR;
2691221a 149 reg_msg.pid = getpid();
5c33bde8 150 reg_msg.ppid = getppid();
2691221a 151 reg_msg.uid = getuid();
83610856 152 reg_msg.gid = getgid();
2629549e
MD
153 prctl_ret = prctl(PR_GET_NAME, (unsigned long) reg_msg.name, 0, 0, 0);
154 if (prctl_ret) {
155 ERR("Error executing prctl");
156 return -errno;
157 }
2691221a
MD
158
159 ret = lttcomm_send_unix_sock(socket, &reg_msg, sizeof(reg_msg));
160 if (ret >= 0 && ret != sizeof(reg_msg))
161 return -EIO;
162 return ret;
163}
164
d9e99d10 165static
d3a492d1 166int send_reply(int sock, struct lttcomm_ust_reply *lur)
d9e99d10 167{
9eb62b9c 168 ssize_t len;
d3a492d1 169
a4be8962 170 len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur));
d3a492d1 171 switch (len) {
a4be8962 172 case sizeof(*lur):
d3a492d1
MD
173 DBG("message successfully sent");
174 return 0;
175 case -1:
176 if (errno == ECONNRESET) {
177 printf("remote end closed connection\n");
178 return 0;
179 }
180 return -1;
181 default:
182 printf("incorrect message size: %zd\n", len);
183 return -1;
184 }
185}
186
187static
edaa1431 188int handle_register_done(struct sock_info *sock_info)
11ff9c7d
MD
189{
190 int ret;
191
edaa1431
MD
192 if (sock_info->constructor_sem_posted)
193 return 0;
194 sock_info->constructor_sem_posted = 1;
95259bd0
MD
195 ret = uatomic_add_return(&sem_count, -1);
196 if (ret == 0) {
197 ret = sem_post(&constructor_wait);
198 assert(!ret);
199 }
11ff9c7d
MD
200 return 0;
201}
202
203static
204int handle_message(struct sock_info *sock_info,
205 int sock, struct lttcomm_ust_msg *lum)
d3a492d1 206{
1ea11eab 207 int ret = 0;
46050b1a
MD
208 const struct objd_ops *ops;
209 struct lttcomm_ust_reply lur;
1ea11eab 210
17dfb34b 211 ust_lock();
1ea11eab 212
46050b1a
MD
213 memset(&lur, 0, sizeof(lur));
214
1ea11eab 215 if (lttng_ust_comm_should_quit) {
46050b1a 216 ret = -EPERM;
1ea11eab
MD
217 goto end;
218 }
9eb62b9c 219
46050b1a
MD
220 ops = objd_ops(lum->handle);
221 if (!ops) {
222 ret = -ENOENT;
223 goto end;
1ea11eab 224 }
46050b1a
MD
225
226 switch (lum->cmd) {
11ff9c7d
MD
227 case LTTNG_UST_REGISTER_DONE:
228 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
edaa1431 229 ret = handle_register_done(sock_info);
11ff9c7d
MD
230 else
231 ret = -EINVAL;
232 break;
46050b1a
MD
233 case LTTNG_UST_RELEASE:
234 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
235 ret = -EPERM;
236 else
237 ret = objd_unref(lum->handle);
d9e99d10
MD
238 break;
239 default:
46050b1a
MD
240 if (ops->cmd)
241 ret = ops->cmd(lum->handle, lum->cmd,
242 (unsigned long) &lum->u);
243 else
244 ret = -ENOSYS;
245 break;
d9e99d10 246 }
46050b1a 247
1ea11eab 248end:
46050b1a
MD
249 lur.handle = lum->handle;
250 lur.cmd = lum->cmd;
251 lur.ret_val = ret;
252 if (ret >= 0) {
253 lur.ret_code = LTTCOMM_OK;
254 } else {
255 lur.ret_code = LTTCOMM_SESSION_FAIL;
256 }
257 ret = send_reply(sock, &lur);
258
17dfb34b 259 ust_unlock();
1ea11eab 260 return ret;
d9e99d10
MD
261}
262
46050b1a
MD
263static
264void cleanup_sock_info(struct sock_info *sock_info)
265{
266 int ret;
267
268 if (sock_info->socket != -1) {
269 ret = close(sock_info->socket);
270 if (ret) {
271 ERR("Error closing local apps socket");
272 }
273 sock_info->socket = -1;
274 }
275 if (sock_info->root_handle != -1) {
276 ret = objd_unref(sock_info->root_handle);
277 if (ret) {
278 ERR("Error unref root handle");
279 }
280 sock_info->root_handle = -1;
281 }
318dfea9 282 sock_info->constructor_sem_posted = 0;
46050b1a
MD
283}
284
1ea11eab
MD
285/*
286 * This thread does not allocate any resource, except within
287 * handle_message, within mutex protection. This mutex protects against
288 * fork and exit.
289 * The other moment it allocates resources is at socket connexion, which
290 * is also protected by the mutex.
291 */
d9e99d10
MD
292static
293void *ust_listener_thread(void *arg)
294{
1ea11eab
MD
295 struct sock_info *sock_info = arg;
296 int sock, ret;
d9e99d10 297
9eb62b9c
MD
298 /* Restart trying to connect to the session daemon */
299restart:
17dfb34b 300 ust_lock();
1ea11eab
MD
301
302 if (lttng_ust_comm_should_quit) {
17dfb34b 303 ust_unlock();
1ea11eab
MD
304 goto quit;
305 }
9eb62b9c 306
1ea11eab
MD
307 if (sock_info->socket != -1) {
308 ret = close(sock_info->socket);
309 if (ret) {
11ff9c7d 310 ERR("Error closing %s apps socket", sock_info->name);
1ea11eab
MD
311 }
312 sock_info->socket = -1;
313 }
46050b1a 314
9eb62b9c
MD
315 /* Check for sessiond availability with pipe TODO */
316
317 /* Register */
1ea11eab 318 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
9eb62b9c 319 if (ret < 0) {
11ff9c7d
MD
320 ERR("Error connecting to %s apps socket", sock_info->name);
321 /*
322 * If we cannot find the sessiond daemon, don't delay
323 * constructor execution.
324 */
edaa1431 325 ret = handle_register_done(sock_info);
11ff9c7d 326 assert(!ret);
17dfb34b 327 ust_unlock();
4eef2998 328 sleep(5);
1ea11eab 329 goto restart;
46050b1a
MD
330 }
331
332 sock_info->socket = sock = ret;
333
334 /*
335 * Create only one root handle per listener thread for the whole
336 * process lifetime.
337 */
338 if (sock_info->root_handle == -1) {
339 ret = lttng_abi_create_root_handle();
340 if (ret) {
341 ERR("Error creating root handle");
17dfb34b 342 ust_unlock();
46050b1a
MD
343 goto quit;
344 }
345 sock_info->root_handle = ret;
9eb62b9c 346 }
1ea11eab 347
9eb62b9c
MD
348 ret = register_app_to_sessiond(sock);
349 if (ret < 0) {
11ff9c7d
MD
350 ERR("Error registering to %s apps socket", sock_info->name);
351 /*
352 * If we cannot register to the sessiond daemon, don't
353 * delay constructor execution.
354 */
edaa1431 355 ret = handle_register_done(sock_info);
11ff9c7d 356 assert(!ret);
17dfb34b 357 ust_unlock();
9eb62b9c
MD
358 sleep(5);
359 goto restart;
360 }
17dfb34b 361 ust_unlock();
46050b1a 362
d9e99d10
MD
363 for (;;) {
364 ssize_t len;
e7723462 365 struct lttcomm_ust_msg lum;
d9e99d10 366
e7723462 367 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
368 switch (len) {
369 case 0: /* orderly shutdown */
11ff9c7d 370 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name);
d9e99d10 371 goto end;
e7723462 372 case sizeof(lum):
d9e99d10 373 DBG("message received\n");
11ff9c7d 374 ret = handle_message(sock_info, sock, &lum);
2a80c9d8 375 if (ret < 0) {
11ff9c7d 376 ERR("Error handling message for %s socket", sock_info->name);
d9e99d10
MD
377 }
378 continue;
379 case -1:
380 if (errno == ECONNRESET) {
11ff9c7d 381 ERR("%s remote end closed connection\n", sock_info->name);
d9e99d10
MD
382 goto end;
383 }
384 goto end;
385 default:
11ff9c7d 386 ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len);
d9e99d10
MD
387 continue;
388 }
389
390 }
391end:
9eb62b9c 392 goto restart; /* try to reconnect */
1ea11eab 393quit:
d9e99d10
MD
394 return NULL;
395}
396
cf12a773
MD
397/*
398 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
399 */
11ff9c7d
MD
400static
401int get_timeout(struct timespec *constructor_timeout)
402{
cf12a773
MD
403 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
404 char *str_delay;
11ff9c7d
MD
405 int ret;
406
cf12a773
MD
407 str_delay = getenv("UST_REGISTER_TIMEOUT");
408 if (str_delay) {
409 constructor_delay_ms = strtol(str_delay, NULL, 10);
410 }
411
412 switch (constructor_delay_ms) {
413 case -1:/* fall-through */
414 case 0:
415 return constructor_delay_ms;
416 default:
417 break;
418 }
419
420 /*
421 * If we are unable to find the current time, don't wait.
422 */
423 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
424 if (ret) {
425 return -1;
426 }
95259bd0
MD
427 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
428 constructor_timeout->tv_nsec +=
429 (constructor_delay_ms % 1000UL) * 1000000UL;
11ff9c7d
MD
430 if (constructor_timeout->tv_nsec >= 1000000000UL) {
431 constructor_timeout->tv_sec++;
432 constructor_timeout->tv_nsec -= 1000000000UL;
433 }
cf12a773 434 return 1;
11ff9c7d 435}
d9e99d10 436
2691221a
MD
437/*
438 * sessiond monitoring thread: monitor presence of global and per-user
439 * sessiond by polling the application common named pipe.
440 */
441/* TODO */
442
edaa1431 443void __attribute__((constructor)) lttng_ust_init(void)
2691221a 444{
11ff9c7d 445 struct timespec constructor_timeout;
cf12a773 446 int timeout_mode;
2691221a
MD
447 int ret;
448
edaa1431
MD
449 if (uatomic_xchg(&initialized, 1) == 1)
450 return;
451
452 /*
453 * We want precise control over the order in which we construct
454 * our sub-libraries vs starting to receive commands from
455 * sessiond (otherwise leading to errors when trying to create
456 * sessiond before the init functions are completed).
457 */
2691221a 458 init_usterr();
edaa1431
MD
459 init_tracepoint();
460 ltt_ring_buffer_metadata_client_init();
461 ltt_ring_buffer_client_overwrite_init();
462 ltt_ring_buffer_client_discard_init();
2691221a 463
cf12a773 464 timeout_mode = get_timeout(&constructor_timeout);
11ff9c7d 465
95259bd0 466 ret = sem_init(&constructor_wait, 0, 0);
11ff9c7d
MD
467 assert(!ret);
468
8d20bf54 469 ret = setup_local_apps();
2691221a 470 if (ret) {
8d20bf54 471 ERR("Error setting up to local apps");
2691221a 472 }
1ea11eab
MD
473 ret = pthread_create(&local_apps.ust_listener, NULL,
474 ust_listener_thread, &local_apps);
11ff9c7d 475
8d20bf54
MD
476 if (local_apps.allowed) {
477 ret = pthread_create(&global_apps.ust_listener, NULL,
478 ust_listener_thread, &global_apps);
479 } else {
480 handle_register_done(&local_apps);
481 }
482
cf12a773
MD
483 switch (timeout_mode) {
484 case 1: /* timeout wait */
95259bd0
MD
485 do {
486 ret = sem_timedwait(&constructor_wait,
487 &constructor_timeout);
488 } while (ret < 0 && errno == EINTR);
cf12a773
MD
489 if (ret < 0 && errno == ETIMEDOUT) {
490 ERR("Timed out waiting for ltt-sessiond");
491 } else {
492 assert(!ret);
493 }
494 break;
7b766b16 495 case -1:/* wait forever */
95259bd0
MD
496 do {
497 ret = sem_wait(&constructor_wait);
498 } while (ret < 0 && errno == EINTR);
11ff9c7d 499 assert(!ret);
cf12a773 500 break;
7b766b16 501 case 0: /* no timeout */
cf12a773 502 break;
11ff9c7d 503 }
2691221a
MD
504}
505
17dfb34b
MD
506static
507void lttng_ust_cleanup(int exiting)
508{
509 cleanup_sock_info(&global_apps);
510 if (local_apps.allowed) {
511 cleanup_sock_info(&local_apps);
512 }
513 lttng_ust_abi_exit();
514 ltt_events_exit();
515 ltt_ring_buffer_client_discard_exit();
516 ltt_ring_buffer_client_overwrite_exit();
517 ltt_ring_buffer_metadata_client_exit();
518 exit_tracepoint();
519 if (!exiting) {
520 /* Reinitialize values for fork */
521 sem_count = 2;
522 lttng_ust_comm_should_quit = 0;
523 initialized = 0;
524 }
525}
526
edaa1431 527void __attribute__((destructor)) lttng_ust_exit(void)
2691221a
MD
528{
529 int ret;
530
9eb62b9c
MD
531 /*
532 * Using pthread_cancel here because:
533 * A) we don't want to hang application teardown.
534 * B) the thread is not allocating any resource.
535 */
1ea11eab
MD
536
537 /*
538 * Require the communication thread to quit. Synchronize with
539 * mutexes to ensure it is not in a mutex critical section when
540 * pthread_cancel is later called.
541 */
17dfb34b 542 ust_lock();
1ea11eab 543 lttng_ust_comm_should_quit = 1;
17dfb34b 544 ust_unlock();
1ea11eab 545
1ea11eab 546 ret = pthread_cancel(global_apps.ust_listener);
9eb62b9c
MD
547 if (ret) {
548 ERR("Error cancelling global ust listener thread");
2691221a 549 }
8d20bf54
MD
550 if (local_apps.allowed) {
551 ret = pthread_cancel(local_apps.ust_listener);
552 if (ret) {
553 ERR("Error cancelling local ust listener thread");
554 }
8d20bf54 555 }
17dfb34b 556 lttng_ust_cleanup(1);
2691221a 557}
e822f505
MD
558
559/*
560 * We exclude the worker threads across fork and clone (except
561 * CLONE_VM), because these system calls only keep the forking thread
562 * running in the child. Therefore, we don't want to call fork or clone
563 * in the middle of an tracepoint or ust tracing state modification.
564 * Holding this mutex protects these structures across fork and clone.
565 */
566void ust_before_fork(ust_fork_info_t *fork_info)
567{
568 /*
569 * Disable signals. This is to avoid that the child intervenes
570 * before it is properly setup for tracing. It is safer to
571 * disable all signals, because then we know we are not breaking
572 * anything by restoring the original mask.
573 */
574 sigset_t all_sigs;
575 int ret;
576
577 /* Disable signals */
578 sigfillset(&all_sigs);
579 ret = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
580 if (ret == -1) {
581 PERROR("sigprocmask");
582 }
17dfb34b 583 ust_lock();
e822f505
MD
584 rcu_bp_before_fork();
585}
586
587static void ust_after_fork_common(ust_fork_info_t *fork_info)
588{
589 int ret;
590
17dfb34b
MD
591 DBG("process %d", getpid());
592 ust_unlock();
e822f505
MD
593 /* Restore signals */
594 ret = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
595 if (ret == -1) {
596 PERROR("sigprocmask");
597 }
598}
599
600void ust_after_fork_parent(ust_fork_info_t *fork_info)
601{
17dfb34b 602 DBG("process %d", getpid());
e822f505
MD
603 rcu_bp_after_fork_parent();
604 /* Release mutexes and reenable signals */
605 ust_after_fork_common(fork_info);
606}
607
17dfb34b
MD
608/*
609 * After fork, in the child, we need to cleanup all the leftover state,
610 * except the worker thread which already magically disappeared thanks
611 * to the weird Linux fork semantics. After tyding up, we call
612 * lttng_ust_init() again to start over as a new PID.
613 *
614 * This is meant for forks() that have tracing in the child between the
615 * fork and following exec call (if there is any).
616 */
e822f505
MD
617void ust_after_fork_child(ust_fork_info_t *fork_info)
618{
17dfb34b 619 DBG("process %d", getpid());
e822f505
MD
620 /* Release urcu mutexes */
621 rcu_bp_after_fork_child();
17dfb34b 622 lttng_ust_cleanup(0);
e822f505
MD
623 /* Release mutexes and reenable signals */
624 ust_after_fork_common(fork_info);
318dfea9 625 lttng_ust_init();
e822f505 626}
This page took 0.053145 seconds and 4 git commands to generate.