Remove now unused metadata printf code
[lttng-ust.git] / liblttng-ust / 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
80e2814b 22#define _LGPL_SOURCE
2691221a
MD
23#include <sys/types.h>
24#include <sys/socket.h>
7fc90dca
MD
25#include <sys/mman.h>
26#include <sys/stat.h>
58d4b2a2
MD
27#include <sys/types.h>
28#include <sys/wait.h>
7fc90dca 29#include <fcntl.h>
2691221a
MD
30#include <unistd.h>
31#include <errno.h>
d9e99d10 32#include <pthread.h>
11ff9c7d
MD
33#include <semaphore.h>
34#include <time.h>
1ea11eab 35#include <assert.h>
e822f505 36#include <signal.h>
95259bd0 37#include <urcu/uatomic.h>
80e2814b 38#include <urcu/futex.h>
c117fb1b 39#include <urcu/compiler.h>
1ea11eab 40
4318ae1b 41#include <lttng/ust-events.h>
4318ae1b 42#include <lttng/ust-abi.h>
4318ae1b 43#include <lttng/ust.h>
7bc53e94 44#include <lttng/ust-error.h>
74d81a6c 45#include <lttng/ust-ctl.h>
8c90a710 46#include <urcu/tls-compat.h>
44c72f10
MD
47#include <ust-comm.h>
48#include <usterr-signal-safe.h>
cd54f6d9 49#include <helper.h>
44c72f10 50#include "tracepoint-internal.h"
7dd08bec 51#include "lttng-tracer-core.h"
08114193 52#include "compat.h"
f645cfa7 53#include "../libringbuffer/tlsfixup.h"
edaa1431
MD
54
55/*
56 * Has lttng ust comm constructor been called ?
57 */
58static int initialized;
59
1ea11eab 60/*
17dfb34b
MD
61 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
62 * Held when handling a command, also held by fork() to deal with
63 * removal of threads, and by exit path.
1ea11eab 64 */
1ea11eab
MD
65
66/* Should the ust comm thread quit ? */
67static int lttng_ust_comm_should_quit;
68
11ff9c7d
MD
69/*
70 * Wait for either of these before continuing to the main
71 * program:
72 * - the register_done message from sessiond daemon
73 * (will let the sessiond daemon enable sessions before main
74 * starts.)
75 * - sessiond daemon is not reachable.
76 * - timeout (ensuring applications are resilient to session
77 * daemon problems).
78 */
79static sem_t constructor_wait;
950aab0c
MD
80/*
81 * Doing this for both the global and local sessiond.
82 */
95259bd0 83static int sem_count = { 2 };
11ff9c7d 84
e8508a49
MD
85/*
86 * Counting nesting within lttng-ust. Used to ensure that calling fork()
87 * from liblttng-ust does not execute the pre/post fork handlers.
88 */
8c90a710 89static DEFINE_URCU_TLS(int, lttng_ust_nest_count);
e8508a49 90
1ea11eab
MD
91/*
92 * Info about socket and associated listener thread.
93 */
94struct sock_info {
11ff9c7d 95 const char *name;
1ea11eab 96 pthread_t ust_listener; /* listener thread */
46050b1a 97 int root_handle;
8d20bf54
MD
98 int constructor_sem_posted;
99 int allowed;
44e073f5 100 int global;
e33f3265 101 int thread_active;
7fc90dca
MD
102
103 char sock_path[PATH_MAX];
104 int socket;
32ce8569 105 int notify_socket;
7fc90dca
MD
106
107 char wait_shm_path[PATH_MAX];
108 char *wait_shm_mmap;
1ea11eab 109};
2691221a
MD
110
111/* Socket from app (connect) to session daemon (listen) for communication */
1ea11eab 112struct sock_info global_apps = {
11ff9c7d 113 .name = "global",
44e073f5 114 .global = 1,
7fc90dca 115
46050b1a 116 .root_handle = -1,
8d20bf54 117 .allowed = 1,
e33f3265 118 .thread_active = 0,
7fc90dca 119
32ce8569 120 .sock_path = LTTNG_DEFAULT_RUNDIR "/" LTTNG_UST_SOCK_FILENAME,
7fc90dca 121 .socket = -1,
32ce8569 122 .notify_socket = -1,
7fc90dca 123
32ce8569 124 .wait_shm_path = "/" LTTNG_UST_WAIT_FILENAME,
1ea11eab 125};
2691221a
MD
126
127/* TODO: allow global_apps_sock_path override */
128
1ea11eab 129struct sock_info local_apps = {
11ff9c7d 130 .name = "local",
44e073f5 131 .global = 0,
46050b1a 132 .root_handle = -1,
8d20bf54 133 .allowed = 0, /* Check setuid bit first */
e33f3265 134 .thread_active = 0,
7fc90dca
MD
135
136 .socket = -1,
32ce8569 137 .notify_socket = -1,
1ea11eab 138};
2691221a 139
37ed587a
MD
140static int wait_poll_fallback;
141
74d81a6c
MD
142static const char *cmd_name_mapping[] = {
143 [ LTTNG_UST_RELEASE ] = "Release",
144 [ LTTNG_UST_SESSION ] = "Create Session",
145 [ LTTNG_UST_TRACER_VERSION ] = "Get Tracer Version",
146
147 [ LTTNG_UST_TRACEPOINT_LIST ] = "Create Tracepoint List",
148 [ LTTNG_UST_WAIT_QUIESCENT ] = "Wait for Quiescent State",
149 [ LTTNG_UST_REGISTER_DONE ] = "Registration Done",
150 [ LTTNG_UST_TRACEPOINT_FIELD_LIST ] = "Create Tracepoint Field List",
151
152 /* Session FD commands */
153 [ LTTNG_UST_CHANNEL ] = "Create Channel",
154 [ LTTNG_UST_SESSION_START ] = "Start Session",
155 [ LTTNG_UST_SESSION_STOP ] = "Stop Session",
156
157 /* Channel FD commands */
158 [ LTTNG_UST_STREAM ] = "Create Stream",
159 [ LTTNG_UST_EVENT ] = "Create Event",
160
161 /* Event and Channel FD commands */
162 [ LTTNG_UST_CONTEXT ] = "Create Context",
163 [ LTTNG_UST_FLUSH_BUFFER ] = "Flush Buffer",
164
165 /* Event, Channel and Session commands */
166 [ LTTNG_UST_ENABLE ] = "Enable",
167 [ LTTNG_UST_DISABLE ] = "Disable",
168
169 /* Tracepoint list commands */
170 [ LTTNG_UST_TRACEPOINT_LIST_GET ] = "List Next Tracepoint",
171 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET ] = "List Next Tracepoint Field",
172
173 /* Event FD commands */
174 [ LTTNG_UST_FILTER ] = "Create Filter",
175};
176
7dd08bec
MD
177extern void lttng_ring_buffer_client_overwrite_init(void);
178extern void lttng_ring_buffer_client_discard_init(void);
179extern void lttng_ring_buffer_metadata_client_init(void);
180extern void lttng_ring_buffer_client_overwrite_exit(void);
181extern void lttng_ring_buffer_client_discard_exit(void);
182extern void lttng_ring_buffer_metadata_client_exit(void);
edaa1431 183
a903623f
MD
184/*
185 * Force a read (imply TLS fixup for dlopen) of TLS variables.
186 */
187static
188void lttng_fixup_nest_count_tls(void)
189{
8c90a710 190 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count)));
a903623f
MD
191}
192
32ce8569
MD
193int lttng_get_notify_socket(void *owner)
194{
195 struct sock_info *info = owner;
196
197 return info->notify_socket;
198}
199
74d81a6c
MD
200static
201void print_cmd(int cmd, int handle)
202{
203 const char *cmd_name = "Unknown";
204
205 if (cmd_name_mapping[cmd]) {
206 cmd_name = cmd_name_mapping[cmd];
207 }
208 DBG("Message Received \"%s\", Handle \"%s\" (%d)", cmd_name,
209 lttng_ust_obj_get_name(handle), handle);
210}
211
2691221a 212static
8d20bf54 213int setup_local_apps(void)
2691221a
MD
214{
215 const char *home_dir;
7fc90dca 216 uid_t uid;
2691221a 217
7fc90dca 218 uid = getuid();
8d20bf54
MD
219 /*
220 * Disallow per-user tracing for setuid binaries.
221 */
7fc90dca 222 if (uid != geteuid()) {
9ec6895c 223 assert(local_apps.allowed == 0);
d0a1ae63 224 return 0;
8d20bf54 225 }
2691221a 226 home_dir = (const char *) getenv("HOME");
9ec6895c
MD
227 if (!home_dir) {
228 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
229 assert(local_apps.allowed == 0);
2691221a 230 return -ENOENT;
9ec6895c
MD
231 }
232 local_apps.allowed = 1;
32ce8569
MD
233 snprintf(local_apps.sock_path, PATH_MAX, "%s/%s/%s",
234 home_dir,
235 LTTNG_DEFAULT_HOME_RUNDIR,
236 LTTNG_UST_SOCK_FILENAME);
237 snprintf(local_apps.wait_shm_path, PATH_MAX, "/%s-%u",
238 LTTNG_UST_WAIT_FILENAME,
239 uid);
2691221a
MD
240 return 0;
241}
242
243static
32ce8569 244int register_to_sessiond(int socket, enum ustctl_socket_type type)
2691221a 245{
32ce8569
MD
246 return ustcomm_send_reg_msg(socket,
247 type,
248 CAA_BITS_PER_LONG,
249 lttng_alignof(uint8_t) * CHAR_BIT,
250 lttng_alignof(uint16_t) * CHAR_BIT,
251 lttng_alignof(uint32_t) * CHAR_BIT,
252 lttng_alignof(uint64_t) * CHAR_BIT,
253 lttng_alignof(unsigned long) * CHAR_BIT);
2691221a
MD
254}
255
d9e99d10 256static
57773204 257int send_reply(int sock, struct ustcomm_ust_reply *lur)
d9e99d10 258{
9eb62b9c 259 ssize_t len;
d3a492d1 260
57773204 261 len = ustcomm_send_unix_sock(sock, lur, sizeof(*lur));
d3a492d1 262 switch (len) {
a4be8962 263 case sizeof(*lur):
d3a492d1
MD
264 DBG("message successfully sent");
265 return 0;
7bc53e94
MD
266 default:
267 if (len == -ECONNRESET) {
268 DBG("remote end closed connection");
d3a492d1
MD
269 return 0;
270 }
7bc53e94
MD
271 if (len < 0)
272 return len;
273 DBG("incorrect message size: %zd", len);
274 return -EINVAL;
d3a492d1
MD
275 }
276}
277
278static
edaa1431 279int handle_register_done(struct sock_info *sock_info)
11ff9c7d
MD
280{
281 int ret;
282
edaa1431
MD
283 if (sock_info->constructor_sem_posted)
284 return 0;
285 sock_info->constructor_sem_posted = 1;
56cd7e2f
MD
286 if (uatomic_read(&sem_count) <= 0) {
287 return 0;
288 }
95259bd0
MD
289 ret = uatomic_add_return(&sem_count, -1);
290 if (ret == 0) {
291 ret = sem_post(&constructor_wait);
292 assert(!ret);
293 }
11ff9c7d
MD
294 return 0;
295}
296
297static
298int handle_message(struct sock_info *sock_info,
57773204 299 int sock, struct ustcomm_ust_msg *lum)
d3a492d1 300{
1ea11eab 301 int ret = 0;
b61ce3b2 302 const struct lttng_ust_objd_ops *ops;
57773204 303 struct ustcomm_ust_reply lur;
ef9ff354 304 union ust_args args;
40003310 305 ssize_t len;
1ea11eab 306
17dfb34b 307 ust_lock();
1ea11eab 308
46050b1a
MD
309 memset(&lur, 0, sizeof(lur));
310
1ea11eab 311 if (lttng_ust_comm_should_quit) {
74d81a6c 312 ret = -LTTNG_UST_ERR_EXITING;
1ea11eab
MD
313 goto end;
314 }
9eb62b9c 315
46050b1a
MD
316 ops = objd_ops(lum->handle);
317 if (!ops) {
318 ret = -ENOENT;
319 goto end;
1ea11eab 320 }
46050b1a
MD
321
322 switch (lum->cmd) {
11ff9c7d
MD
323 case LTTNG_UST_REGISTER_DONE:
324 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
edaa1431 325 ret = handle_register_done(sock_info);
11ff9c7d
MD
326 else
327 ret = -EINVAL;
328 break;
46050b1a
MD
329 case LTTNG_UST_RELEASE:
330 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
331 ret = -EPERM;
332 else
d4419b81 333 ret = lttng_ust_objd_unref(lum->handle);
d9e99d10 334 break;
2d78951a
MD
335 case LTTNG_UST_FILTER:
336 {
337 /* Receive filter data */
f488575f 338 struct lttng_ust_filter_bytecode_node *bytecode;
2d78951a 339
cd54f6d9 340 if (lum->u.filter.data_size > FILTER_BYTECODE_MAX_LEN) {
7bc53e94 341 ERR("Filter data size is too large: %u bytes",
2d78951a
MD
342 lum->u.filter.data_size);
343 ret = -EINVAL;
344 goto error;
345 }
2734ca65 346
885b1dfd 347 if (lum->u.filter.reloc_offset > lum->u.filter.data_size) {
7bc53e94 348 ERR("Filter reloc offset %u is not within data",
2734ca65
CB
349 lum->u.filter.reloc_offset);
350 ret = -EINVAL;
351 goto error;
352 }
353
cd54f6d9
MD
354 bytecode = zmalloc(sizeof(*bytecode) + lum->u.filter.data_size);
355 if (!bytecode) {
356 ret = -ENOMEM;
357 goto error;
358 }
f488575f 359 len = ustcomm_recv_unix_sock(sock, bytecode->bc.data,
2d78951a
MD
360 lum->u.filter.data_size);
361 switch (len) {
362 case 0: /* orderly shutdown */
363 ret = 0;
cd54f6d9 364 free(bytecode);
2d78951a 365 goto error;
2d78951a
MD
366 default:
367 if (len == lum->u.filter.data_size) {
7bc53e94 368 DBG("filter data received");
2d78951a 369 break;
7bc53e94
MD
370 } else if (len < 0) {
371 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
372 if (len == -ECONNRESET) {
373 ERR("%s remote end closed connection", sock_info->name);
374 ret = len;
375 free(bytecode);
376 goto error;
377 }
378 ret = len;
379 goto end;
2d78951a 380 } else {
7bc53e94 381 DBG("incorrect filter data message size: %zd", len);
2d78951a 382 ret = -EINVAL;
cd54f6d9 383 free(bytecode);
2d78951a
MD
384 goto end;
385 }
386 }
f488575f
MD
387 bytecode->bc.len = lum->u.filter.data_size;
388 bytecode->bc.reloc_offset = lum->u.filter.reloc_offset;
3f6fd224 389 bytecode->bc.seqnum = lum->u.filter.seqnum;
cd54f6d9 390 if (ops->cmd) {
2d78951a 391 ret = ops->cmd(lum->handle, lum->cmd,
cd54f6d9 392 (unsigned long) bytecode,
f59ed768 393 &args, sock_info);
cd54f6d9
MD
394 if (ret) {
395 free(bytecode);
396 }
397 /* don't free bytecode if everything went fine. */
398 } else {
2d78951a 399 ret = -ENOSYS;
cd54f6d9
MD
400 free(bytecode);
401 }
2d78951a
MD
402 break;
403 }
74d81a6c
MD
404 case LTTNG_UST_CHANNEL:
405 {
406 void *chan_data;
407
408 len = ustcomm_recv_channel_from_sessiond(sock,
409 &chan_data, lum->u.channel.len);
410 switch (len) {
411 case 0: /* orderly shutdown */
412 ret = 0;
413 goto error;
414 default:
415 if (len == lum->u.channel.len) {
416 DBG("channel data received");
417 break;
418 } else if (len < 0) {
419 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
420 if (len == -ECONNRESET) {
421 ERR("%s remote end closed connection", sock_info->name);
422 ret = len;
423 goto error;
424 }
425 ret = len;
426 goto end;
427 } else {
428 DBG("incorrect channel data message size: %zd", len);
429 ret = -EINVAL;
430 goto end;
431 }
432 }
433 args.channel.chan_data = chan_data;
434 if (ops->cmd)
435 ret = ops->cmd(lum->handle, lum->cmd,
436 (unsigned long) &lum->u,
437 &args, sock_info);
438 else
439 ret = -ENOSYS;
440 break;
441 }
442 case LTTNG_UST_STREAM:
443 {
444 /* Receive shm_fd, wakeup_fd */
445 ret = ustcomm_recv_stream_from_sessiond(sock,
446 &lum->u.stream.len,
447 &args.stream.shm_fd,
448 &args.stream.wakeup_fd);
449 if (ret) {
450 goto end;
451 }
452 if (ops->cmd)
453 ret = ops->cmd(lum->handle, lum->cmd,
454 (unsigned long) &lum->u,
455 &args, sock_info);
456 else
457 ret = -ENOSYS;
458 break;
459 }
d9e99d10 460 default:
46050b1a
MD
461 if (ops->cmd)
462 ret = ops->cmd(lum->handle, lum->cmd,
ef9ff354 463 (unsigned long) &lum->u,
f59ed768 464 &args, sock_info);
46050b1a
MD
465 else
466 ret = -ENOSYS;
467 break;
d9e99d10 468 }
46050b1a 469
1ea11eab 470end:
46050b1a
MD
471 lur.handle = lum->handle;
472 lur.cmd = lum->cmd;
473 lur.ret_val = ret;
474 if (ret >= 0) {
7bc53e94 475 lur.ret_code = LTTNG_UST_OK;
46050b1a 476 } else {
7bc53e94
MD
477 /*
478 * Use -LTTNG_UST_ERR as wildcard for UST internal
479 * error that are not caused by the transport, except if
480 * we already have a more precise error message to
481 * report.
482 */
64b2564e
DG
483 if (ret > -LTTNG_UST_ERR) {
484 /* Translate code to UST error. */
485 switch (ret) {
486 case -EEXIST:
487 lur.ret_code = -LTTNG_UST_ERR_EXIST;
488 break;
489 case -EINVAL:
490 lur.ret_code = -LTTNG_UST_ERR_INVAL;
491 break;
492 case -ENOENT:
493 lur.ret_code = -LTTNG_UST_ERR_NOENT;
494 break;
495 case -EPERM:
496 lur.ret_code = -LTTNG_UST_ERR_PERM;
497 break;
498 case -ENOSYS:
499 lur.ret_code = -LTTNG_UST_ERR_NOSYS;
500 break;
501 default:
502 lur.ret_code = -LTTNG_UST_ERR;
503 break;
504 }
505 } else {
7bc53e94 506 lur.ret_code = ret;
64b2564e 507 }
46050b1a 508 }
e6ea14c5
MD
509 if (ret >= 0) {
510 switch (lum->cmd) {
e6ea14c5
MD
511 case LTTNG_UST_TRACER_VERSION:
512 lur.u.version = lum->u.version;
513 break;
514 case LTTNG_UST_TRACEPOINT_LIST_GET:
515 memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint));
516 break;
517 }
381c0f1e 518 }
74d81a6c 519 DBG("Return value: %d", lur.ret_val);
46050b1a 520 ret = send_reply(sock, &lur);
193183fb 521 if (ret < 0) {
7bc53e94 522 DBG("error sending reply");
193183fb
MD
523 goto error;
524 }
46050b1a 525
40003310
MD
526 /*
527 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
528 * after the reply.
529 */
7bc53e94 530 if (lur.ret_code == LTTNG_UST_OK) {
40003310
MD
531 switch (lum->cmd) {
532 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
533 len = ustcomm_send_unix_sock(sock,
534 &args.field_list.entry,
535 sizeof(args.field_list.entry));
7bc53e94
MD
536 if (len < 0) {
537 ret = len;
538 goto error;
539 }
40003310 540 if (len != sizeof(args.field_list.entry)) {
7bc53e94 541 ret = -EINVAL;
40003310
MD
542 goto error;
543 }
544 }
545 }
ef9ff354 546
381c0f1e 547error:
17dfb34b 548 ust_unlock();
1ea11eab 549 return ret;
d9e99d10
MD
550}
551
46050b1a 552static
efe0de09 553void cleanup_sock_info(struct sock_info *sock_info, int exiting)
46050b1a
MD
554{
555 int ret;
556
557 if (sock_info->socket != -1) {
e6973a89 558 ret = ustcomm_close_unix_sock(sock_info->socket);
46050b1a 559 if (ret) {
32ce8569 560 ERR("Error closing ust cmd socket");
46050b1a
MD
561 }
562 sock_info->socket = -1;
563 }
32ce8569
MD
564 if (sock_info->notify_socket != -1) {
565 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
566 if (ret) {
567 ERR("Error closing ust notify socket");
568 }
569 sock_info->notify_socket = -1;
570 }
46050b1a 571 if (sock_info->root_handle != -1) {
d4419b81 572 ret = lttng_ust_objd_unref(sock_info->root_handle);
46050b1a
MD
573 if (ret) {
574 ERR("Error unref root handle");
575 }
576 sock_info->root_handle = -1;
577 }
318dfea9 578 sock_info->constructor_sem_posted = 0;
efe0de09
MD
579 /*
580 * wait_shm_mmap is used by listener threads outside of the
581 * ust lock, so we cannot tear it down ourselves, because we
582 * cannot join on these threads. Leave this task to the OS
583 * process exit.
584 */
585 if (!exiting && sock_info->wait_shm_mmap) {
7fc90dca
MD
586 ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE));
587 if (ret) {
588 ERR("Error unmapping wait shm");
589 }
590 sock_info->wait_shm_mmap = NULL;
591 }
592}
593
58d4b2a2 594/*
33bbeb90
MD
595 * Using fork to set umask in the child process (not multi-thread safe).
596 * We deal with the shm_open vs ftruncate race (happening when the
597 * sessiond owns the shm and does not let everybody modify it, to ensure
598 * safety against shm_unlink) by simply letting the mmap fail and
599 * retrying after a few seconds.
600 * For global shm, everybody has rw access to it until the sessiond
601 * starts.
58d4b2a2 602 */
7fc90dca 603static
58d4b2a2 604int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
7fc90dca 605{
7fc90dca 606 int wait_shm_fd, ret;
58d4b2a2 607 pid_t pid;
44e073f5 608
58d4b2a2 609 /*
33bbeb90 610 * Try to open read-only.
58d4b2a2 611 */
33bbeb90 612 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
613 if (wait_shm_fd >= 0) {
614 goto end;
615 } else if (wait_shm_fd < 0 && errno != ENOENT) {
616 /*
33bbeb90
MD
617 * Real-only open did not work, and it's not because the
618 * entry was not present. It's a failure that prohibits
619 * using shm.
58d4b2a2 620 */
7fc90dca 621 ERR("Error opening shm %s", sock_info->wait_shm_path);
58d4b2a2 622 goto end;
7fc90dca
MD
623 }
624 /*
58d4b2a2
MD
625 * If the open failed because the file did not exist, try
626 * creating it ourself.
7fc90dca 627 */
8c90a710 628 URCU_TLS(lttng_ust_nest_count)++;
58d4b2a2 629 pid = fork();
8c90a710 630 URCU_TLS(lttng_ust_nest_count)--;
58d4b2a2
MD
631 if (pid > 0) {
632 int status;
633
634 /*
635 * Parent: wait for child to return, in which case the
636 * shared memory map will have been created.
637 */
638 pid = wait(&status);
b7d3cb32 639 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
58d4b2a2
MD
640 wait_shm_fd = -1;
641 goto end;
7fc90dca 642 }
58d4b2a2
MD
643 /*
644 * Try to open read-only again after creation.
645 */
33bbeb90 646 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
647 if (wait_shm_fd < 0) {
648 /*
649 * Real-only open did not work. It's a failure
650 * that prohibits using shm.
651 */
652 ERR("Error opening shm %s", sock_info->wait_shm_path);
653 goto end;
654 }
655 goto end;
656 } else if (pid == 0) {
657 int create_mode;
658
659 /* Child */
33bbeb90 660 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
58d4b2a2 661 if (sock_info->global)
33bbeb90 662 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
58d4b2a2
MD
663 /*
664 * We're alone in a child process, so we can modify the
665 * process-wide umask.
666 */
33bbeb90 667 umask(~create_mode);
58d4b2a2 668 /*
33bbeb90
MD
669 * Try creating shm (or get rw access).
670 * We don't do an exclusive open, because we allow other
671 * processes to create+ftruncate it concurrently.
58d4b2a2
MD
672 */
673 wait_shm_fd = shm_open(sock_info->wait_shm_path,
674 O_RDWR | O_CREAT, create_mode);
675 if (wait_shm_fd >= 0) {
676 ret = ftruncate(wait_shm_fd, mmap_size);
677 if (ret) {
678 PERROR("ftruncate");
b0c1425d 679 _exit(EXIT_FAILURE);
58d4b2a2 680 }
b0c1425d 681 _exit(EXIT_SUCCESS);
58d4b2a2 682 }
33bbeb90
MD
683 /*
684 * For local shm, we need to have rw access to accept
685 * opening it: this means the local sessiond will be
686 * able to wake us up. For global shm, we open it even
687 * if rw access is not granted, because the root.root
688 * sessiond will be able to override all rights and wake
689 * us up.
690 */
691 if (!sock_info->global && errno != EACCES) {
58d4b2a2 692 ERR("Error opening shm %s", sock_info->wait_shm_path);
5d3bc5ed 693 _exit(EXIT_FAILURE);
58d4b2a2
MD
694 }
695 /*
33bbeb90
MD
696 * The shm exists, but we cannot open it RW. Report
697 * success.
58d4b2a2 698 */
5d3bc5ed 699 _exit(EXIT_SUCCESS);
58d4b2a2
MD
700 } else {
701 return -1;
7fc90dca 702 }
58d4b2a2 703end:
33bbeb90
MD
704 if (wait_shm_fd >= 0 && !sock_info->global) {
705 struct stat statbuf;
706
707 /*
708 * Ensure that our user is the owner of the shm file for
709 * local shm. If we do not own the file, it means our
710 * sessiond will not have access to wake us up (there is
711 * probably a rogue process trying to fake our
712 * sessiond). Fallback to polling method in this case.
713 */
714 ret = fstat(wait_shm_fd, &statbuf);
715 if (ret) {
716 PERROR("fstat");
717 goto error_close;
718 }
719 if (statbuf.st_uid != getuid())
720 goto error_close;
721 }
58d4b2a2 722 return wait_shm_fd;
33bbeb90
MD
723
724error_close:
725 ret = close(wait_shm_fd);
726 if (ret) {
727 PERROR("Error closing fd");
728 }
729 return -1;
58d4b2a2
MD
730}
731
732static
733char *get_map_shm(struct sock_info *sock_info)
734{
735 size_t mmap_size = sysconf(_SC_PAGE_SIZE);
736 int wait_shm_fd, ret;
737 char *wait_shm_mmap;
738
739 wait_shm_fd = get_wait_shm(sock_info, mmap_size);
740 if (wait_shm_fd < 0) {
741 goto error;
44e073f5 742 }
7fc90dca
MD
743 wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ,
744 MAP_SHARED, wait_shm_fd, 0);
7fc90dca
MD
745 /* close shm fd immediately after taking the mmap reference */
746 ret = close(wait_shm_fd);
747 if (ret) {
33bbeb90
MD
748 PERROR("Error closing fd");
749 }
750 if (wait_shm_mmap == MAP_FAILED) {
751 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
752 goto error;
7fc90dca
MD
753 }
754 return wait_shm_mmap;
755
756error:
757 return NULL;
758}
759
760static
761void wait_for_sessiond(struct sock_info *sock_info)
762{
efe0de09 763 int ret;
80e2814b 764
7fc90dca
MD
765 ust_lock();
766 if (lttng_ust_comm_should_quit) {
767 goto quit;
768 }
37ed587a
MD
769 if (wait_poll_fallback) {
770 goto error;
771 }
7fc90dca
MD
772 if (!sock_info->wait_shm_mmap) {
773 sock_info->wait_shm_mmap = get_map_shm(sock_info);
774 if (!sock_info->wait_shm_mmap)
775 goto error;
776 }
777 ust_unlock();
778
779 DBG("Waiting for %s apps sessiond", sock_info->name);
80e2814b
MD
780 /* Wait for futex wakeup */
781 if (uatomic_read((int32_t *) sock_info->wait_shm_mmap) == 0) {
782 ret = futex_async((int32_t *) sock_info->wait_shm_mmap,
783 FUTEX_WAIT, 0, NULL, NULL, 0);
80e2814b 784 if (ret < 0) {
37ed587a
MD
785 if (errno == EFAULT) {
786 wait_poll_fallback = 1;
a8b870ad 787 DBG(
37ed587a
MD
788"Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
789"do not support FUTEX_WAKE on read-only memory mappings correctly. "
790"Please upgrade your kernel "
791"(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
792"mainline). LTTng-UST will use polling mode fallback.");
cd27263b
MD
793 if (ust_debug())
794 PERROR("futex");
37ed587a 795 }
80e2814b
MD
796 }
797 }
7fc90dca
MD
798 return;
799
800quit:
801 ust_unlock();
802 return;
803
804error:
805 ust_unlock();
7fc90dca 806 return;
46050b1a
MD
807}
808
1ea11eab
MD
809/*
810 * This thread does not allocate any resource, except within
811 * handle_message, within mutex protection. This mutex protects against
812 * fork and exit.
98bf993f 813 * The other moment it allocates resources is at socket connection, which
1ea11eab
MD
814 * is also protected by the mutex.
815 */
d9e99d10
MD
816static
817void *ust_listener_thread(void *arg)
818{
1ea11eab 819 struct sock_info *sock_info = arg;
c0eedf81 820 int sock, ret, prev_connect_failed = 0, has_waited = 0;
32ce8569
MD
821 int open_sock[2];
822 int i;
d9e99d10 823
9eb62b9c
MD
824 /* Restart trying to connect to the session daemon */
825restart:
c0eedf81
MD
826 if (prev_connect_failed) {
827 /* Wait for sessiond availability with pipe */
828 wait_for_sessiond(sock_info);
829 if (has_waited) {
830 has_waited = 0;
831 /*
832 * Sleep for 5 seconds before retrying after a
833 * sequence of failure / wait / failure. This
834 * deals with a killed or broken session daemon.
835 */
836 sleep(5);
837 }
838 has_waited = 1;
839 prev_connect_failed = 0;
840 }
17dfb34b 841 ust_lock();
1ea11eab
MD
842
843 if (lttng_ust_comm_should_quit) {
1ea11eab
MD
844 goto quit;
845 }
9eb62b9c 846
1ea11eab 847 if (sock_info->socket != -1) {
e6973a89 848 ret = ustcomm_close_unix_sock(sock_info->socket);
1ea11eab 849 if (ret) {
32ce8569
MD
850 ERR("Error closing %s ust cmd socket",
851 sock_info->name);
1ea11eab
MD
852 }
853 sock_info->socket = -1;
854 }
32ce8569
MD
855 if (sock_info->notify_socket != -1) {
856 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
857 if (ret) {
858 ERR("Error closing %s ust notify socket",
859 sock_info->name);
860 }
861 sock_info->notify_socket = -1;
862 }
46050b1a 863
9eb62b9c 864 /* Register */
32ce8569
MD
865 for (i = 0; i < 2; i++) {
866 ret = ustcomm_connect_unix_sock(sock_info->sock_path);
867 if (ret < 0) {
868 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
869 prev_connect_failed = 1;
870 /*
871 * If we cannot find the sessiond daemon, don't delay
872 * constructor execution.
873 */
874 ret = handle_register_done(sock_info);
875 assert(!ret);
876 ust_unlock();
877 goto restart;
878 }
879 open_sock[i] = ret;
46050b1a
MD
880 }
881
32ce8569
MD
882 sock_info->socket = open_sock[0];
883 sock_info->notify_socket = open_sock[1];
46050b1a
MD
884
885 /*
886 * Create only one root handle per listener thread for the whole
f59ed768
MD
887 * process lifetime, so we ensure we get ID which is statically
888 * assigned to the root handle.
46050b1a
MD
889 */
890 if (sock_info->root_handle == -1) {
891 ret = lttng_abi_create_root_handle();
a51070bb 892 if (ret < 0) {
46050b1a 893 ERR("Error creating root handle");
46050b1a
MD
894 goto quit;
895 }
896 sock_info->root_handle = ret;
9eb62b9c 897 }
1ea11eab 898
32ce8569 899 ret = register_to_sessiond(sock_info->socket, USTCTL_SOCKET_CMD);
9eb62b9c 900 if (ret < 0) {
32ce8569
MD
901 ERR("Error registering to %s ust cmd socket",
902 sock_info->name);
c0eedf81 903 prev_connect_failed = 1;
11ff9c7d
MD
904 /*
905 * If we cannot register to the sessiond daemon, don't
906 * delay constructor execution.
907 */
edaa1431 908 ret = handle_register_done(sock_info);
11ff9c7d 909 assert(!ret);
17dfb34b 910 ust_unlock();
9eb62b9c
MD
911 goto restart;
912 }
32ce8569
MD
913 ret = register_to_sessiond(sock_info->notify_socket,
914 USTCTL_SOCKET_NOTIFY);
915 if (ret < 0) {
916 ERR("Error registering to %s ust notify socket",
917 sock_info->name);
918 prev_connect_failed = 1;
919 /*
920 * If we cannot register to the sessiond daemon, don't
921 * delay constructor execution.
922 */
923 ret = handle_register_done(sock_info);
924 assert(!ret);
925 ust_unlock();
926 goto restart;
927 }
928 sock = sock_info->socket;
929
17dfb34b 930 ust_unlock();
46050b1a 931
d9e99d10
MD
932 for (;;) {
933 ssize_t len;
57773204 934 struct ustcomm_ust_msg lum;
d9e99d10 935
57773204 936 len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
937 switch (len) {
938 case 0: /* orderly shutdown */
7dd08bec 939 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info->name);
8236ba10 940 ust_lock();
d5e1fea6 941 if (lttng_ust_comm_should_quit) {
d5e1fea6
MD
942 goto quit;
943 }
8236ba10
MD
944 /*
945 * Either sessiond has shutdown or refused us by closing the socket.
946 * In either case, we don't want to delay construction execution,
947 * and we need to wait before retry.
948 */
949 prev_connect_failed = 1;
950 /*
951 * If we cannot register to the sessiond daemon, don't
952 * delay constructor execution.
953 */
954 ret = handle_register_done(sock_info);
955 assert(!ret);
956 ust_unlock();
d9e99d10 957 goto end;
e7723462 958 case sizeof(lum):
74d81a6c 959 print_cmd(lum.cmd, lum.handle);
11ff9c7d 960 ret = handle_message(sock_info, sock, &lum);
7bc53e94 961 if (ret) {
11ff9c7d 962 ERR("Error handling message for %s socket", sock_info->name);
d9e99d10
MD
963 }
964 continue;
7bc53e94
MD
965 default:
966 if (len < 0) {
967 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
968 } else {
969 DBG("incorrect message size (%s socket): %zd", sock_info->name, len);
970 }
971 if (len == -ECONNRESET) {
972 DBG("%s remote end closed connection", sock_info->name);
d9e99d10
MD
973 goto end;
974 }
975 goto end;
d9e99d10
MD
976 }
977
978 }
979end:
f59ed768 980 ust_lock();
d5e1fea6 981 if (lttng_ust_comm_should_quit) {
d5e1fea6
MD
982 goto quit;
983 }
f59ed768
MD
984 /* Cleanup socket handles before trying to reconnect */
985 lttng_ust_objd_table_owner_cleanup(sock_info);
986 ust_unlock();
9eb62b9c 987 goto restart; /* try to reconnect */
e33f3265 988
1ea11eab 989quit:
e33f3265
MD
990 sock_info->thread_active = 0;
991 ust_unlock();
d9e99d10
MD
992 return NULL;
993}
994
cf12a773
MD
995/*
996 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
997 */
11ff9c7d
MD
998static
999int get_timeout(struct timespec *constructor_timeout)
1000{
cf12a773
MD
1001 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
1002 char *str_delay;
11ff9c7d
MD
1003 int ret;
1004
69400ac4 1005 str_delay = getenv("LTTNG_UST_REGISTER_TIMEOUT");
cf12a773
MD
1006 if (str_delay) {
1007 constructor_delay_ms = strtol(str_delay, NULL, 10);
1008 }
1009
1010 switch (constructor_delay_ms) {
1011 case -1:/* fall-through */
1012 case 0:
1013 return constructor_delay_ms;
1014 default:
1015 break;
1016 }
1017
1018 /*
1019 * If we are unable to find the current time, don't wait.
1020 */
1021 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
1022 if (ret) {
1023 return -1;
1024 }
95259bd0
MD
1025 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
1026 constructor_timeout->tv_nsec +=
1027 (constructor_delay_ms % 1000UL) * 1000000UL;
11ff9c7d
MD
1028 if (constructor_timeout->tv_nsec >= 1000000000UL) {
1029 constructor_timeout->tv_sec++;
1030 constructor_timeout->tv_nsec -= 1000000000UL;
1031 }
cf12a773 1032 return 1;
11ff9c7d 1033}
d9e99d10 1034
2691221a
MD
1035/*
1036 * sessiond monitoring thread: monitor presence of global and per-user
1037 * sessiond by polling the application common named pipe.
1038 */
edaa1431 1039void __attribute__((constructor)) lttng_ust_init(void)
2691221a 1040{
11ff9c7d 1041 struct timespec constructor_timeout;
ae6a58bf 1042 sigset_t sig_all_blocked, orig_parent_mask;
1879f67f 1043 pthread_attr_t thread_attr;
cf12a773 1044 int timeout_mode;
2691221a
MD
1045 int ret;
1046
edaa1431
MD
1047 if (uatomic_xchg(&initialized, 1) == 1)
1048 return;
1049
eddd8d5d
MD
1050 /*
1051 * Fixup interdependency between TLS fixup mutex (which happens
1052 * to be the dynamic linker mutex) and ust_lock, taken within
1053 * the ust lock.
1054 */
f645cfa7 1055 lttng_fixup_ringbuffer_tls();
4158a15a 1056 lttng_fixup_vtid_tls();
a903623f 1057 lttng_fixup_nest_count_tls();
009745db 1058 lttng_fixup_procname_tls();
eddd8d5d 1059
edaa1431
MD
1060 /*
1061 * We want precise control over the order in which we construct
1062 * our sub-libraries vs starting to receive commands from
1063 * sessiond (otherwise leading to errors when trying to create
1064 * sessiond before the init functions are completed).
1065 */
2691221a 1066 init_usterr();
edaa1431 1067 init_tracepoint();
7dd08bec
MD
1068 lttng_ring_buffer_metadata_client_init();
1069 lttng_ring_buffer_client_overwrite_init();
1070 lttng_ring_buffer_client_discard_init();
2691221a 1071
cf12a773 1072 timeout_mode = get_timeout(&constructor_timeout);
11ff9c7d 1073
95259bd0 1074 ret = sem_init(&constructor_wait, 0, 0);
11ff9c7d
MD
1075 assert(!ret);
1076
8d20bf54 1077 ret = setup_local_apps();
2691221a 1078 if (ret) {
9ec6895c 1079 DBG("local apps setup returned %d", ret);
2691221a 1080 }
ae6a58bf
WP
1081
1082 /* A new thread created by pthread_create inherits the signal mask
1083 * from the parent. To avoid any signal being received by the
1084 * listener thread, we block all signals temporarily in the parent,
1085 * while we create the listener thread.
1086 */
1087 sigfillset(&sig_all_blocked);
1088 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask);
1089 if (ret) {
d94d802c 1090 ERR("pthread_sigmask: %s", strerror(ret));
ae6a58bf
WP
1091 }
1092
1879f67f
MG
1093 ret = pthread_attr_init(&thread_attr);
1094 if (ret) {
1095 ERR("pthread_attr_init: %s", strerror(ret));
1096 }
1097 ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
1098 if (ret) {
1099 ERR("pthread_attr_setdetachstate: %s", strerror(ret));
1100 }
1101
e33f3265 1102 ust_lock();
1879f67f 1103 ret = pthread_create(&global_apps.ust_listener, &thread_attr,
dde70ea0 1104 ust_listener_thread, &global_apps);
d94d802c
MD
1105 if (ret) {
1106 ERR("pthread_create global: %s", strerror(ret));
1107 }
e33f3265
MD
1108 global_apps.thread_active = 1;
1109 ust_unlock();
1110
8d20bf54 1111 if (local_apps.allowed) {
e33f3265 1112 ust_lock();
1879f67f 1113 ret = pthread_create(&local_apps.ust_listener, &thread_attr,
dde70ea0 1114 ust_listener_thread, &local_apps);
d94d802c
MD
1115 if (ret) {
1116 ERR("pthread_create local: %s", strerror(ret));
1117 }
e33f3265
MD
1118 local_apps.thread_active = 1;
1119 ust_unlock();
8d20bf54
MD
1120 } else {
1121 handle_register_done(&local_apps);
1122 }
1879f67f
MG
1123 ret = pthread_attr_destroy(&thread_attr);
1124 if (ret) {
1125 ERR("pthread_attr_destroy: %s", strerror(ret));
1126 }
8d20bf54 1127
ae6a58bf
WP
1128 /* Restore original signal mask in parent */
1129 ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL);
1130 if (ret) {
d94d802c 1131 ERR("pthread_sigmask: %s", strerror(ret));
ae6a58bf
WP
1132 }
1133
cf12a773
MD
1134 switch (timeout_mode) {
1135 case 1: /* timeout wait */
95259bd0
MD
1136 do {
1137 ret = sem_timedwait(&constructor_wait,
1138 &constructor_timeout);
1139 } while (ret < 0 && errno == EINTR);
cf12a773 1140 if (ret < 0 && errno == ETIMEDOUT) {
7dd08bec 1141 ERR("Timed out waiting for lttng-sessiond");
cf12a773
MD
1142 } else {
1143 assert(!ret);
1144 }
1145 break;
7b766b16 1146 case -1:/* wait forever */
95259bd0
MD
1147 do {
1148 ret = sem_wait(&constructor_wait);
1149 } while (ret < 0 && errno == EINTR);
11ff9c7d 1150 assert(!ret);
cf12a773 1151 break;
7b766b16 1152 case 0: /* no timeout */
cf12a773 1153 break;
11ff9c7d 1154 }
2691221a
MD
1155}
1156
17dfb34b
MD
1157static
1158void lttng_ust_cleanup(int exiting)
1159{
efe0de09 1160 cleanup_sock_info(&global_apps, exiting);
17dfb34b 1161 if (local_apps.allowed) {
efe0de09 1162 cleanup_sock_info(&local_apps, exiting);
17dfb34b 1163 }
efe0de09
MD
1164 /*
1165 * The teardown in this function all affect data structures
1166 * accessed under the UST lock by the listener thread. This
1167 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1168 * that none of these threads are accessing this data at this
1169 * point.
1170 */
17dfb34b 1171 lttng_ust_abi_exit();
003fedf4 1172 lttng_ust_events_exit();
7dd08bec
MD
1173 lttng_ring_buffer_client_discard_exit();
1174 lttng_ring_buffer_client_overwrite_exit();
1175 lttng_ring_buffer_metadata_client_exit();
17dfb34b
MD
1176 exit_tracepoint();
1177 if (!exiting) {
1178 /* Reinitialize values for fork */
1179 sem_count = 2;
1180 lttng_ust_comm_should_quit = 0;
1181 initialized = 0;
1182 }
1183}
1184
edaa1431 1185void __attribute__((destructor)) lttng_ust_exit(void)
2691221a
MD
1186{
1187 int ret;
1188
9eb62b9c
MD
1189 /*
1190 * Using pthread_cancel here because:
1191 * A) we don't want to hang application teardown.
1192 * B) the thread is not allocating any resource.
1193 */
1ea11eab
MD
1194
1195 /*
1196 * Require the communication thread to quit. Synchronize with
1197 * mutexes to ensure it is not in a mutex critical section when
1198 * pthread_cancel is later called.
1199 */
17dfb34b 1200 ust_lock();
1ea11eab 1201 lttng_ust_comm_should_quit = 1;
1ea11eab 1202
f5f94532 1203 /* cancel threads */
e33f3265
MD
1204 if (global_apps.thread_active) {
1205 ret = pthread_cancel(global_apps.ust_listener);
1206 if (ret) {
1207 ERR("Error cancelling global ust listener thread: %s",
1208 strerror(ret));
1209 } else {
1210 global_apps.thread_active = 0;
1211 }
2691221a 1212 }
e33f3265 1213 if (local_apps.thread_active) {
8d20bf54
MD
1214 ret = pthread_cancel(local_apps.ust_listener);
1215 if (ret) {
d94d802c
MD
1216 ERR("Error cancelling local ust listener thread: %s",
1217 strerror(ret));
e33f3265
MD
1218 } else {
1219 local_apps.thread_active = 0;
8d20bf54 1220 }
8d20bf54 1221 }
e33f3265
MD
1222 ust_unlock();
1223
efe0de09
MD
1224 /*
1225 * Do NOT join threads: use of sys_futex makes it impossible to
1226 * join the threads without using async-cancel, but async-cancel
1227 * is delivered by a signal, which could hit the target thread
1228 * anywhere in its code path, including while the ust_lock() is
1229 * held, causing a deadlock for the other thread. Let the OS
1230 * cleanup the threads if there are stalled in a syscall.
1231 */
17dfb34b 1232 lttng_ust_cleanup(1);
2691221a 1233}
e822f505
MD
1234
1235/*
1236 * We exclude the worker threads across fork and clone (except
1237 * CLONE_VM), because these system calls only keep the forking thread
1238 * running in the child. Therefore, we don't want to call fork or clone
1239 * in the middle of an tracepoint or ust tracing state modification.
1240 * Holding this mutex protects these structures across fork and clone.
1241 */
b728d87e 1242void ust_before_fork(sigset_t *save_sigset)
e822f505
MD
1243{
1244 /*
1245 * Disable signals. This is to avoid that the child intervenes
1246 * before it is properly setup for tracing. It is safer to
1247 * disable all signals, because then we know we are not breaking
1248 * anything by restoring the original mask.
1249 */
1250 sigset_t all_sigs;
1251 int ret;
1252
8c90a710 1253 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 1254 return;
e822f505
MD
1255 /* Disable signals */
1256 sigfillset(&all_sigs);
b728d87e 1257 ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset);
e822f505
MD
1258 if (ret == -1) {
1259 PERROR("sigprocmask");
1260 }
17dfb34b 1261 ust_lock();
e822f505
MD
1262 rcu_bp_before_fork();
1263}
1264
b728d87e 1265static void ust_after_fork_common(sigset_t *restore_sigset)
e822f505
MD
1266{
1267 int ret;
1268
17dfb34b
MD
1269 DBG("process %d", getpid());
1270 ust_unlock();
e822f505 1271 /* Restore signals */
23c8854a 1272 ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL);
e822f505
MD
1273 if (ret == -1) {
1274 PERROR("sigprocmask");
1275 }
1276}
1277
b728d87e 1278void ust_after_fork_parent(sigset_t *restore_sigset)
e822f505 1279{
8c90a710 1280 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 1281 return;
17dfb34b 1282 DBG("process %d", getpid());
e822f505
MD
1283 rcu_bp_after_fork_parent();
1284 /* Release mutexes and reenable signals */
b728d87e 1285 ust_after_fork_common(restore_sigset);
e822f505
MD
1286}
1287
17dfb34b
MD
1288/*
1289 * After fork, in the child, we need to cleanup all the leftover state,
1290 * except the worker thread which already magically disappeared thanks
1291 * to the weird Linux fork semantics. After tyding up, we call
1292 * lttng_ust_init() again to start over as a new PID.
1293 *
1294 * This is meant for forks() that have tracing in the child between the
1295 * fork and following exec call (if there is any).
1296 */
b728d87e 1297void ust_after_fork_child(sigset_t *restore_sigset)
e822f505 1298{
8c90a710 1299 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 1300 return;
17dfb34b 1301 DBG("process %d", getpid());
e822f505
MD
1302 /* Release urcu mutexes */
1303 rcu_bp_after_fork_child();
17dfb34b 1304 lttng_ust_cleanup(0);
a93bfc45 1305 lttng_context_vtid_reset();
e822f505 1306 /* Release mutexes and reenable signals */
b728d87e 1307 ust_after_fork_common(restore_sigset);
318dfea9 1308 lttng_ust_init();
e822f505 1309}
This page took 0.097117 seconds and 4 git commands to generate.