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