update fork run script
[lttng-ust.git] / libust / lttng-ust-comm.c
... / ...
CommitLineData
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>
24#include <sys/prctl.h>
25#include <unistd.h>
26#include <errno.h>
27#include <pthread.h>
28#include <semaphore.h>
29#include <time.h>
30#include <assert.h>
31#include <signal.h>
32#include <urcu/uatomic.h>
33
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>
38#include <ust/tracepoint-internal.h>
39#include <ust/ust.h>
40#include "ltt-tracer-core.h"
41
42/*
43 * Has lttng ust comm constructor been called ?
44 */
45static int initialized;
46
47/*
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.
51 */
52
53/* Should the ust comm thread quit ? */
54static int lttng_ust_comm_should_quit;
55
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;
67/*
68 * Doing this for both the global and local sessiond.
69 */
70static int sem_count = { 2 };
71
72/*
73 * Info about socket and associated listener thread.
74 */
75struct sock_info {
76 const char *name;
77 char sock_path[PATH_MAX];
78 int socket;
79 pthread_t ust_listener; /* listener thread */
80 int root_handle;
81 int constructor_sem_posted;
82 int allowed;
83};
84
85/* Socket from app (connect) to session daemon (listen) for communication */
86struct sock_info global_apps = {
87 .name = "global",
88 .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK,
89 .socket = -1,
90 .root_handle = -1,
91 .allowed = 1,
92};
93
94/* TODO: allow global_apps_sock_path override */
95
96struct sock_info local_apps = {
97 .name = "local",
98 .socket = -1,
99 .root_handle = -1,
100 .allowed = 0, /* Check setuid bit first */
101};
102
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
110static
111int setup_local_apps(void)
112{
113 const char *home_dir;
114
115 /*
116 * Disallow per-user tracing for setuid binaries.
117 */
118 if (getuid() != geteuid()) {
119 local_apps.allowed = 0;
120 return 0;
121 } else {
122 local_apps.allowed = 1;
123 }
124 home_dir = (const char *) getenv("HOME");
125 if (!home_dir)
126 return -ENOENT;
127 snprintf(local_apps.sock_path, PATH_MAX,
128 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
129 return 0;
130}
131
132static
133int register_app_to_sessiond(int socket)
134{
135 ssize_t ret;
136 int prctl_ret;
137 struct {
138 uint32_t major;
139 uint32_t minor;
140 pid_t pid;
141 pid_t ppid;
142 uid_t uid;
143 gid_t gid;
144 char name[16]; /* process name */
145 } reg_msg;
146
147 reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR;
148 reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR;
149 reg_msg.pid = getpid();
150 reg_msg.ppid = getppid();
151 reg_msg.uid = getuid();
152 reg_msg.gid = getgid();
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 }
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
165static
166int send_reply(int sock, struct lttcomm_ust_reply *lur)
167{
168 ssize_t len;
169
170 len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur));
171 switch (len) {
172 case sizeof(*lur):
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
188int handle_register_done(struct sock_info *sock_info)
189{
190 int ret;
191
192 if (sock_info->constructor_sem_posted)
193 return 0;
194 sock_info->constructor_sem_posted = 1;
195 ret = uatomic_add_return(&sem_count, -1);
196 if (ret == 0) {
197 ret = sem_post(&constructor_wait);
198 assert(!ret);
199 }
200 return 0;
201}
202
203static
204int handle_message(struct sock_info *sock_info,
205 int sock, struct lttcomm_ust_msg *lum)
206{
207 int ret = 0;
208 const struct objd_ops *ops;
209 struct lttcomm_ust_reply lur;
210
211 ust_lock();
212
213 memset(&lur, 0, sizeof(lur));
214
215 if (lttng_ust_comm_should_quit) {
216 ret = -EPERM;
217 goto end;
218 }
219
220 ops = objd_ops(lum->handle);
221 if (!ops) {
222 ret = -ENOENT;
223 goto end;
224 }
225
226 switch (lum->cmd) {
227 case LTTNG_UST_REGISTER_DONE:
228 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
229 ret = handle_register_done(sock_info);
230 else
231 ret = -EINVAL;
232 break;
233 case LTTNG_UST_RELEASE:
234 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
235 ret = -EPERM;
236 else
237 ret = objd_unref(lum->handle);
238 break;
239 default:
240 if (ops->cmd)
241 ret = ops->cmd(lum->handle, lum->cmd,
242 (unsigned long) &lum->u);
243 else
244 ret = -ENOSYS;
245 break;
246 }
247
248end:
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
259 ust_unlock();
260 return ret;
261}
262
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 }
282}
283
284/*
285 * This thread does not allocate any resource, except within
286 * handle_message, within mutex protection. This mutex protects against
287 * fork and exit.
288 * The other moment it allocates resources is at socket connexion, which
289 * is also protected by the mutex.
290 */
291static
292void *ust_listener_thread(void *arg)
293{
294 struct sock_info *sock_info = arg;
295 int sock, ret;
296
297 /* Restart trying to connect to the session daemon */
298restart:
299 ust_lock();
300
301 if (lttng_ust_comm_should_quit) {
302 ust_unlock();
303 goto quit;
304 }
305
306 if (sock_info->socket != -1) {
307 ret = close(sock_info->socket);
308 if (ret) {
309 ERR("Error closing %s apps socket", sock_info->name);
310 }
311 sock_info->socket = -1;
312 }
313
314 /* Check for sessiond availability with pipe TODO */
315
316 /* Register */
317 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
318 if (ret < 0) {
319 ERR("Error connecting to %s apps socket", sock_info->name);
320 /*
321 * If we cannot find the sessiond daemon, don't delay
322 * constructor execution.
323 */
324 ret = handle_register_done(sock_info);
325 assert(!ret);
326 ust_unlock();
327 sleep(5);
328 goto restart;
329 }
330
331 sock_info->socket = sock = ret;
332
333 /*
334 * Create only one root handle per listener thread for the whole
335 * process lifetime.
336 */
337 if (sock_info->root_handle == -1) {
338 ret = lttng_abi_create_root_handle();
339 if (ret) {
340 ERR("Error creating root handle");
341 ust_unlock();
342 goto quit;
343 }
344 sock_info->root_handle = ret;
345 }
346
347 ret = register_app_to_sessiond(sock);
348 if (ret < 0) {
349 ERR("Error registering to %s apps socket", sock_info->name);
350 /*
351 * If we cannot register to the sessiond daemon, don't
352 * delay constructor execution.
353 */
354 ret = handle_register_done(sock_info);
355 assert(!ret);
356 ust_unlock();
357 sleep(5);
358 goto restart;
359 }
360 ust_unlock();
361
362 for (;;) {
363 ssize_t len;
364 struct lttcomm_ust_msg lum;
365
366 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
367 switch (len) {
368 case 0: /* orderly shutdown */
369 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name);
370 goto end;
371 case sizeof(lum):
372 DBG("message received\n");
373 ret = handle_message(sock_info, sock, &lum);
374 if (ret < 0) {
375 ERR("Error handling message for %s socket", sock_info->name);
376 }
377 continue;
378 case -1:
379 if (errno == ECONNRESET) {
380 ERR("%s remote end closed connection\n", sock_info->name);
381 goto end;
382 }
383 goto end;
384 default:
385 ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len);
386 continue;
387 }
388
389 }
390end:
391 goto restart; /* try to reconnect */
392quit:
393 return NULL;
394}
395
396/*
397 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
398 */
399static
400int get_timeout(struct timespec *constructor_timeout)
401{
402 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
403 char *str_delay;
404 int ret;
405
406 str_delay = getenv("UST_REGISTER_TIMEOUT");
407 if (str_delay) {
408 constructor_delay_ms = strtol(str_delay, NULL, 10);
409 }
410
411 switch (constructor_delay_ms) {
412 case -1:/* fall-through */
413 case 0:
414 return constructor_delay_ms;
415 default:
416 break;
417 }
418
419 /*
420 * If we are unable to find the current time, don't wait.
421 */
422 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
423 if (ret) {
424 return -1;
425 }
426 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
427 constructor_timeout->tv_nsec +=
428 (constructor_delay_ms % 1000UL) * 1000000UL;
429 if (constructor_timeout->tv_nsec >= 1000000000UL) {
430 constructor_timeout->tv_sec++;
431 constructor_timeout->tv_nsec -= 1000000000UL;
432 }
433 return 1;
434}
435
436/*
437 * sessiond monitoring thread: monitor presence of global and per-user
438 * sessiond by polling the application common named pipe.
439 */
440/* TODO */
441
442void __attribute__((constructor)) lttng_ust_init(void)
443{
444 struct timespec constructor_timeout;
445 int timeout_mode;
446 int ret;
447
448 if (uatomic_xchg(&initialized, 1) == 1)
449 return;
450
451 /*
452 * We want precise control over the order in which we construct
453 * our sub-libraries vs starting to receive commands from
454 * sessiond (otherwise leading to errors when trying to create
455 * sessiond before the init functions are completed).
456 */
457 init_usterr();
458 init_tracepoint();
459 ltt_ring_buffer_metadata_client_init();
460 ltt_ring_buffer_client_overwrite_init();
461 ltt_ring_buffer_client_discard_init();
462
463 timeout_mode = get_timeout(&constructor_timeout);
464
465 ret = sem_init(&constructor_wait, 0, 0);
466 assert(!ret);
467
468 ret = setup_local_apps();
469 if (ret) {
470 ERR("Error setting up to local apps");
471 }
472 ret = pthread_create(&local_apps.ust_listener, NULL,
473 ust_listener_thread, &local_apps);
474
475 if (local_apps.allowed) {
476 ret = pthread_create(&global_apps.ust_listener, NULL,
477 ust_listener_thread, &global_apps);
478 } else {
479 handle_register_done(&local_apps);
480 }
481
482 switch (timeout_mode) {
483 case 1: /* timeout wait */
484 do {
485 ret = sem_timedwait(&constructor_wait,
486 &constructor_timeout);
487 } while (ret < 0 && errno == EINTR);
488 if (ret < 0 && errno == ETIMEDOUT) {
489 ERR("Timed out waiting for ltt-sessiond");
490 } else {
491 assert(!ret);
492 }
493 break;
494 case -1:/* wait forever */
495 do {
496 ret = sem_wait(&constructor_wait);
497 } while (ret < 0 && errno == EINTR);
498 assert(!ret);
499 break;
500 case 0: /* no timeout */
501 break;
502 }
503}
504
505static
506void lttng_ust_cleanup(int exiting)
507{
508 cleanup_sock_info(&global_apps);
509 if (local_apps.allowed) {
510 cleanup_sock_info(&local_apps);
511 }
512 lttng_ust_abi_exit();
513 ltt_events_exit();
514 ltt_ring_buffer_client_discard_exit();
515 ltt_ring_buffer_client_overwrite_exit();
516 ltt_ring_buffer_metadata_client_exit();
517 exit_tracepoint();
518 if (!exiting) {
519 /* Reinitialize values for fork */
520 sem_count = 2;
521 lttng_ust_comm_should_quit = 0;
522 initialized = 0;
523 }
524}
525
526void __attribute__((destructor)) lttng_ust_exit(void)
527{
528 int ret;
529
530 /*
531 * Using pthread_cancel here because:
532 * A) we don't want to hang application teardown.
533 * B) the thread is not allocating any resource.
534 */
535
536 /*
537 * Require the communication thread to quit. Synchronize with
538 * mutexes to ensure it is not in a mutex critical section when
539 * pthread_cancel is later called.
540 */
541 ust_lock();
542 lttng_ust_comm_should_quit = 1;
543 ust_unlock();
544
545 ret = pthread_cancel(global_apps.ust_listener);
546 if (ret) {
547 ERR("Error cancelling global ust listener thread");
548 }
549 if (local_apps.allowed) {
550 ret = pthread_cancel(local_apps.ust_listener);
551 if (ret) {
552 ERR("Error cancelling local ust listener thread");
553 }
554 }
555 lttng_ust_cleanup(1);
556}
557
558/*
559 * We exclude the worker threads across fork and clone (except
560 * CLONE_VM), because these system calls only keep the forking thread
561 * running in the child. Therefore, we don't want to call fork or clone
562 * in the middle of an tracepoint or ust tracing state modification.
563 * Holding this mutex protects these structures across fork and clone.
564 */
565void ust_before_fork(ust_fork_info_t *fork_info)
566{
567 /*
568 * Disable signals. This is to avoid that the child intervenes
569 * before it is properly setup for tracing. It is safer to
570 * disable all signals, because then we know we are not breaking
571 * anything by restoring the original mask.
572 */
573 sigset_t all_sigs;
574 int ret;
575
576 /* Disable signals */
577 sigfillset(&all_sigs);
578 ret = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
579 if (ret == -1) {
580 PERROR("sigprocmask");
581 }
582 ust_lock();
583 rcu_bp_before_fork();
584}
585
586static void ust_after_fork_common(ust_fork_info_t *fork_info)
587{
588 int ret;
589
590 DBG("process %d", getpid());
591 ust_unlock();
592 /* Restore signals */
593 ret = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
594 if (ret == -1) {
595 PERROR("sigprocmask");
596 }
597}
598
599void ust_after_fork_parent(ust_fork_info_t *fork_info)
600{
601 DBG("process %d", getpid());
602 rcu_bp_after_fork_parent();
603 /* Release mutexes and reenable signals */
604 ust_after_fork_common(fork_info);
605}
606
607/*
608 * After fork, in the child, we need to cleanup all the leftover state,
609 * except the worker thread which already magically disappeared thanks
610 * to the weird Linux fork semantics. After tyding up, we call
611 * lttng_ust_init() again to start over as a new PID.
612 *
613 * This is meant for forks() that have tracing in the child between the
614 * fork and following exec call (if there is any).
615 */
616void ust_after_fork_child(ust_fork_info_t *fork_info)
617{
618 DBG("process %d", getpid());
619 /* Release urcu mutexes */
620 rcu_bp_after_fork_child();
621 lttng_ust_cleanup(0);
622 lttng_ust_init();
623 /* Release mutexes and reenable signals */
624 ust_after_fork_common(fork_info);
625}
This page took 0.027644 seconds and 4 git commands to generate.