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