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> | |
2629549e | 25 | #include <sys/prctl.h> |
7fc90dca MD |
26 | #include <sys/mman.h> |
27 | #include <sys/stat.h> | |
58d4b2a2 MD |
28 | #include <sys/types.h> |
29 | #include <sys/wait.h> | |
7fc90dca | 30 | #include <fcntl.h> |
2691221a MD |
31 | #include <unistd.h> |
32 | #include <errno.h> | |
d9e99d10 | 33 | #include <pthread.h> |
11ff9c7d MD |
34 | #include <semaphore.h> |
35 | #include <time.h> | |
1ea11eab | 36 | #include <assert.h> |
e822f505 | 37 | #include <signal.h> |
95259bd0 | 38 | #include <urcu/uatomic.h> |
80e2814b | 39 | #include <urcu/futex.h> |
1ea11eab | 40 | |
edaa1431 MD |
41 | #include <lttng-ust-comm.h> |
42 | #include <ust/usterr-signal-safe.h> | |
43 | #include <ust/lttng-ust-abi.h> | |
44 | #include <ust/tracepoint.h> | |
d0a1ae63 | 45 | #include <ust/tracepoint-internal.h> |
e822f505 | 46 | #include <ust/ust.h> |
b751f722 | 47 | #include "ltt-tracer-core.h" |
edaa1431 MD |
48 | |
49 | /* | |
50 | * Has lttng ust comm constructor been called ? | |
51 | */ | |
52 | static int initialized; | |
53 | ||
1ea11eab | 54 | /* |
17dfb34b MD |
55 | * The ust_lock/ust_unlock lock is used as a communication thread mutex. |
56 | * Held when handling a command, also held by fork() to deal with | |
57 | * removal of threads, and by exit path. | |
1ea11eab | 58 | */ |
1ea11eab MD |
59 | |
60 | /* Should the ust comm thread quit ? */ | |
61 | static int lttng_ust_comm_should_quit; | |
62 | ||
11ff9c7d MD |
63 | /* |
64 | * Wait for either of these before continuing to the main | |
65 | * program: | |
66 | * - the register_done message from sessiond daemon | |
67 | * (will let the sessiond daemon enable sessions before main | |
68 | * starts.) | |
69 | * - sessiond daemon is not reachable. | |
70 | * - timeout (ensuring applications are resilient to session | |
71 | * daemon problems). | |
72 | */ | |
73 | static sem_t constructor_wait; | |
950aab0c MD |
74 | /* |
75 | * Doing this for both the global and local sessiond. | |
76 | */ | |
95259bd0 | 77 | static int sem_count = { 2 }; |
11ff9c7d | 78 | |
1ea11eab MD |
79 | /* |
80 | * Info about socket and associated listener thread. | |
81 | */ | |
82 | struct sock_info { | |
11ff9c7d | 83 | const char *name; |
1ea11eab | 84 | pthread_t ust_listener; /* listener thread */ |
46050b1a | 85 | int root_handle; |
8d20bf54 MD |
86 | int constructor_sem_posted; |
87 | int allowed; | |
44e073f5 | 88 | int global; |
7fc90dca MD |
89 | |
90 | char sock_path[PATH_MAX]; | |
91 | int socket; | |
92 | ||
93 | char wait_shm_path[PATH_MAX]; | |
94 | char *wait_shm_mmap; | |
1ea11eab | 95 | }; |
2691221a MD |
96 | |
97 | /* Socket from app (connect) to session daemon (listen) for communication */ | |
1ea11eab | 98 | struct sock_info global_apps = { |
11ff9c7d | 99 | .name = "global", |
44e073f5 | 100 | .global = 1, |
7fc90dca | 101 | |
46050b1a | 102 | .root_handle = -1, |
8d20bf54 | 103 | .allowed = 1, |
7fc90dca MD |
104 | |
105 | .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK, | |
106 | .socket = -1, | |
107 | ||
108 | .wait_shm_path = DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH, | |
1ea11eab | 109 | }; |
2691221a MD |
110 | |
111 | /* TODO: allow global_apps_sock_path override */ | |
112 | ||
1ea11eab | 113 | struct sock_info local_apps = { |
11ff9c7d | 114 | .name = "local", |
44e073f5 | 115 | .global = 0, |
46050b1a | 116 | .root_handle = -1, |
8d20bf54 | 117 | .allowed = 0, /* Check setuid bit first */ |
7fc90dca MD |
118 | |
119 | .socket = -1, | |
1ea11eab | 120 | }; |
2691221a | 121 | |
37ed587a MD |
122 | static int wait_poll_fallback; |
123 | ||
edaa1431 MD |
124 | extern void ltt_ring_buffer_client_overwrite_init(void); |
125 | extern void ltt_ring_buffer_client_discard_init(void); | |
126 | extern void ltt_ring_buffer_metadata_client_init(void); | |
127 | extern void ltt_ring_buffer_client_overwrite_exit(void); | |
128 | extern void ltt_ring_buffer_client_discard_exit(void); | |
129 | extern void ltt_ring_buffer_metadata_client_exit(void); | |
130 | ||
2691221a | 131 | static |
8d20bf54 | 132 | int setup_local_apps(void) |
2691221a MD |
133 | { |
134 | const char *home_dir; | |
7fc90dca | 135 | uid_t uid; |
2691221a | 136 | |
7fc90dca | 137 | uid = getuid(); |
8d20bf54 MD |
138 | /* |
139 | * Disallow per-user tracing for setuid binaries. | |
140 | */ | |
7fc90dca | 141 | if (uid != geteuid()) { |
8d20bf54 | 142 | local_apps.allowed = 0; |
d0a1ae63 | 143 | return 0; |
8d20bf54 MD |
144 | } else { |
145 | local_apps.allowed = 1; | |
146 | } | |
2691221a MD |
147 | home_dir = (const char *) getenv("HOME"); |
148 | if (!home_dir) | |
149 | return -ENOENT; | |
1ea11eab | 150 | snprintf(local_apps.sock_path, PATH_MAX, |
2691221a | 151 | DEFAULT_HOME_APPS_UNIX_SOCK, home_dir); |
7fc90dca MD |
152 | snprintf(local_apps.wait_shm_path, PATH_MAX, |
153 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, uid); | |
2691221a MD |
154 | return 0; |
155 | } | |
156 | ||
157 | static | |
158 | int register_app_to_sessiond(int socket) | |
159 | { | |
160 | ssize_t ret; | |
2629549e | 161 | int prctl_ret; |
2691221a | 162 | struct { |
e44418f3 MD |
163 | uint32_t major; |
164 | uint32_t minor; | |
2691221a | 165 | pid_t pid; |
5c33bde8 | 166 | pid_t ppid; |
2691221a | 167 | uid_t uid; |
83610856 | 168 | gid_t gid; |
2629549e | 169 | char name[16]; /* process name */ |
2691221a MD |
170 | } reg_msg; |
171 | ||
e44418f3 MD |
172 | reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR; |
173 | reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR; | |
2691221a | 174 | reg_msg.pid = getpid(); |
5c33bde8 | 175 | reg_msg.ppid = getppid(); |
2691221a | 176 | reg_msg.uid = getuid(); |
83610856 | 177 | reg_msg.gid = getgid(); |
2629549e MD |
178 | prctl_ret = prctl(PR_GET_NAME, (unsigned long) reg_msg.name, 0, 0, 0); |
179 | if (prctl_ret) { | |
180 | ERR("Error executing prctl"); | |
181 | return -errno; | |
182 | } | |
2691221a MD |
183 | |
184 | ret = lttcomm_send_unix_sock(socket, ®_msg, sizeof(reg_msg)); | |
185 | if (ret >= 0 && ret != sizeof(reg_msg)) | |
186 | return -EIO; | |
187 | return ret; | |
188 | } | |
189 | ||
d9e99d10 | 190 | static |
d3a492d1 | 191 | int send_reply(int sock, struct lttcomm_ust_reply *lur) |
d9e99d10 | 192 | { |
9eb62b9c | 193 | ssize_t len; |
d3a492d1 | 194 | |
a4be8962 | 195 | len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur)); |
d3a492d1 | 196 | switch (len) { |
a4be8962 | 197 | case sizeof(*lur): |
d3a492d1 MD |
198 | DBG("message successfully sent"); |
199 | return 0; | |
200 | case -1: | |
201 | if (errno == ECONNRESET) { | |
202 | printf("remote end closed connection\n"); | |
203 | return 0; | |
204 | } | |
205 | return -1; | |
206 | default: | |
207 | printf("incorrect message size: %zd\n", len); | |
208 | return -1; | |
209 | } | |
210 | } | |
211 | ||
212 | static | |
edaa1431 | 213 | int handle_register_done(struct sock_info *sock_info) |
11ff9c7d MD |
214 | { |
215 | int ret; | |
216 | ||
edaa1431 MD |
217 | if (sock_info->constructor_sem_posted) |
218 | return 0; | |
219 | sock_info->constructor_sem_posted = 1; | |
95259bd0 MD |
220 | ret = uatomic_add_return(&sem_count, -1); |
221 | if (ret == 0) { | |
222 | ret = sem_post(&constructor_wait); | |
223 | assert(!ret); | |
224 | } | |
11ff9c7d MD |
225 | return 0; |
226 | } | |
227 | ||
228 | static | |
229 | int handle_message(struct sock_info *sock_info, | |
230 | int sock, struct lttcomm_ust_msg *lum) | |
d3a492d1 | 231 | { |
1ea11eab | 232 | int ret = 0; |
46050b1a MD |
233 | const struct objd_ops *ops; |
234 | struct lttcomm_ust_reply lur; | |
1ea11eab | 235 | |
17dfb34b | 236 | ust_lock(); |
1ea11eab | 237 | |
46050b1a MD |
238 | memset(&lur, 0, sizeof(lur)); |
239 | ||
1ea11eab | 240 | if (lttng_ust_comm_should_quit) { |
46050b1a | 241 | ret = -EPERM; |
1ea11eab MD |
242 | goto end; |
243 | } | |
9eb62b9c | 244 | |
46050b1a MD |
245 | ops = objd_ops(lum->handle); |
246 | if (!ops) { | |
247 | ret = -ENOENT; | |
248 | goto end; | |
1ea11eab | 249 | } |
46050b1a MD |
250 | |
251 | switch (lum->cmd) { | |
11ff9c7d MD |
252 | case LTTNG_UST_REGISTER_DONE: |
253 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
edaa1431 | 254 | ret = handle_register_done(sock_info); |
11ff9c7d MD |
255 | else |
256 | ret = -EINVAL; | |
257 | break; | |
46050b1a MD |
258 | case LTTNG_UST_RELEASE: |
259 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
260 | ret = -EPERM; | |
261 | else | |
262 | ret = objd_unref(lum->handle); | |
d9e99d10 MD |
263 | break; |
264 | default: | |
46050b1a MD |
265 | if (ops->cmd) |
266 | ret = ops->cmd(lum->handle, lum->cmd, | |
267 | (unsigned long) &lum->u); | |
268 | else | |
269 | ret = -ENOSYS; | |
270 | break; | |
d9e99d10 | 271 | } |
46050b1a | 272 | |
1ea11eab | 273 | end: |
46050b1a MD |
274 | lur.handle = lum->handle; |
275 | lur.cmd = lum->cmd; | |
276 | lur.ret_val = ret; | |
277 | if (ret >= 0) { | |
278 | lur.ret_code = LTTCOMM_OK; | |
279 | } else { | |
280 | lur.ret_code = LTTCOMM_SESSION_FAIL; | |
281 | } | |
282 | ret = send_reply(sock, &lur); | |
283 | ||
17dfb34b | 284 | ust_unlock(); |
1ea11eab | 285 | return ret; |
d9e99d10 MD |
286 | } |
287 | ||
46050b1a MD |
288 | static |
289 | void cleanup_sock_info(struct sock_info *sock_info) | |
290 | { | |
291 | int ret; | |
292 | ||
293 | if (sock_info->socket != -1) { | |
294 | ret = close(sock_info->socket); | |
295 | if (ret) { | |
7fc90dca | 296 | ERR("Error closing apps socket"); |
46050b1a MD |
297 | } |
298 | sock_info->socket = -1; | |
299 | } | |
300 | if (sock_info->root_handle != -1) { | |
301 | ret = objd_unref(sock_info->root_handle); | |
302 | if (ret) { | |
303 | ERR("Error unref root handle"); | |
304 | } | |
305 | sock_info->root_handle = -1; | |
306 | } | |
318dfea9 | 307 | sock_info->constructor_sem_posted = 0; |
7fc90dca MD |
308 | if (sock_info->wait_shm_mmap) { |
309 | ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE)); | |
310 | if (ret) { | |
311 | ERR("Error unmapping wait shm"); | |
312 | } | |
313 | sock_info->wait_shm_mmap = NULL; | |
314 | } | |
315 | } | |
316 | ||
58d4b2a2 | 317 | /* |
33bbeb90 MD |
318 | * Using fork to set umask in the child process (not multi-thread safe). |
319 | * We deal with the shm_open vs ftruncate race (happening when the | |
320 | * sessiond owns the shm and does not let everybody modify it, to ensure | |
321 | * safety against shm_unlink) by simply letting the mmap fail and | |
322 | * retrying after a few seconds. | |
323 | * For global shm, everybody has rw access to it until the sessiond | |
324 | * starts. | |
58d4b2a2 | 325 | */ |
7fc90dca | 326 | static |
58d4b2a2 | 327 | int get_wait_shm(struct sock_info *sock_info, size_t mmap_size) |
7fc90dca | 328 | { |
7fc90dca | 329 | int wait_shm_fd, ret; |
58d4b2a2 | 330 | pid_t pid; |
44e073f5 | 331 | |
58d4b2a2 | 332 | /* |
33bbeb90 | 333 | * Try to open read-only. |
58d4b2a2 | 334 | */ |
33bbeb90 | 335 | wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0); |
58d4b2a2 MD |
336 | if (wait_shm_fd >= 0) { |
337 | goto end; | |
338 | } else if (wait_shm_fd < 0 && errno != ENOENT) { | |
339 | /* | |
33bbeb90 MD |
340 | * Real-only open did not work, and it's not because the |
341 | * entry was not present. It's a failure that prohibits | |
342 | * using shm. | |
58d4b2a2 | 343 | */ |
7fc90dca | 344 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
58d4b2a2 | 345 | goto end; |
7fc90dca MD |
346 | } |
347 | /* | |
58d4b2a2 MD |
348 | * If the open failed because the file did not exist, try |
349 | * creating it ourself. | |
7fc90dca | 350 | */ |
58d4b2a2 MD |
351 | pid = fork(); |
352 | if (pid > 0) { | |
353 | int status; | |
354 | ||
355 | /* | |
356 | * Parent: wait for child to return, in which case the | |
357 | * shared memory map will have been created. | |
358 | */ | |
359 | pid = wait(&status); | |
360 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { | |
361 | wait_shm_fd = -1; | |
362 | goto end; | |
7fc90dca | 363 | } |
58d4b2a2 MD |
364 | /* |
365 | * Try to open read-only again after creation. | |
366 | */ | |
33bbeb90 | 367 | wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0); |
58d4b2a2 MD |
368 | if (wait_shm_fd < 0) { |
369 | /* | |
370 | * Real-only open did not work. It's a failure | |
371 | * that prohibits using shm. | |
372 | */ | |
373 | ERR("Error opening shm %s", sock_info->wait_shm_path); | |
374 | goto end; | |
375 | } | |
376 | goto end; | |
377 | } else if (pid == 0) { | |
378 | int create_mode; | |
379 | ||
380 | /* Child */ | |
33bbeb90 | 381 | create_mode = S_IRUSR | S_IWUSR | S_IRGRP; |
58d4b2a2 | 382 | if (sock_info->global) |
33bbeb90 | 383 | create_mode |= S_IROTH | S_IWGRP | S_IWOTH; |
58d4b2a2 MD |
384 | /* |
385 | * We're alone in a child process, so we can modify the | |
386 | * process-wide umask. | |
387 | */ | |
33bbeb90 | 388 | umask(~create_mode); |
58d4b2a2 | 389 | /* |
33bbeb90 MD |
390 | * Try creating shm (or get rw access). |
391 | * We don't do an exclusive open, because we allow other | |
392 | * processes to create+ftruncate it concurrently. | |
58d4b2a2 MD |
393 | */ |
394 | wait_shm_fd = shm_open(sock_info->wait_shm_path, | |
395 | O_RDWR | O_CREAT, create_mode); | |
396 | if (wait_shm_fd >= 0) { | |
397 | ret = ftruncate(wait_shm_fd, mmap_size); | |
398 | if (ret) { | |
399 | PERROR("ftruncate"); | |
400 | exit(EXIT_FAILURE); | |
401 | } | |
58d4b2a2 MD |
402 | exit(EXIT_SUCCESS); |
403 | } | |
33bbeb90 MD |
404 | /* |
405 | * For local shm, we need to have rw access to accept | |
406 | * opening it: this means the local sessiond will be | |
407 | * able to wake us up. For global shm, we open it even | |
408 | * if rw access is not granted, because the root.root | |
409 | * sessiond will be able to override all rights and wake | |
410 | * us up. | |
411 | */ | |
412 | if (!sock_info->global && errno != EACCES) { | |
58d4b2a2 MD |
413 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
414 | exit(EXIT_FAILURE); | |
415 | } | |
416 | /* | |
33bbeb90 MD |
417 | * The shm exists, but we cannot open it RW. Report |
418 | * success. | |
58d4b2a2 MD |
419 | */ |
420 | exit(EXIT_SUCCESS); | |
421 | } else { | |
422 | return -1; | |
7fc90dca | 423 | } |
58d4b2a2 | 424 | end: |
33bbeb90 MD |
425 | if (wait_shm_fd >= 0 && !sock_info->global) { |
426 | struct stat statbuf; | |
427 | ||
428 | /* | |
429 | * Ensure that our user is the owner of the shm file for | |
430 | * local shm. If we do not own the file, it means our | |
431 | * sessiond will not have access to wake us up (there is | |
432 | * probably a rogue process trying to fake our | |
433 | * sessiond). Fallback to polling method in this case. | |
434 | */ | |
435 | ret = fstat(wait_shm_fd, &statbuf); | |
436 | if (ret) { | |
437 | PERROR("fstat"); | |
438 | goto error_close; | |
439 | } | |
440 | if (statbuf.st_uid != getuid()) | |
441 | goto error_close; | |
442 | } | |
58d4b2a2 | 443 | return wait_shm_fd; |
33bbeb90 MD |
444 | |
445 | error_close: | |
446 | ret = close(wait_shm_fd); | |
447 | if (ret) { | |
448 | PERROR("Error closing fd"); | |
449 | } | |
450 | return -1; | |
58d4b2a2 MD |
451 | } |
452 | ||
453 | static | |
454 | char *get_map_shm(struct sock_info *sock_info) | |
455 | { | |
456 | size_t mmap_size = sysconf(_SC_PAGE_SIZE); | |
457 | int wait_shm_fd, ret; | |
458 | char *wait_shm_mmap; | |
459 | ||
460 | wait_shm_fd = get_wait_shm(sock_info, mmap_size); | |
461 | if (wait_shm_fd < 0) { | |
462 | goto error; | |
44e073f5 | 463 | } |
7fc90dca MD |
464 | wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ, |
465 | MAP_SHARED, wait_shm_fd, 0); | |
7fc90dca MD |
466 | /* close shm fd immediately after taking the mmap reference */ |
467 | ret = close(wait_shm_fd); | |
468 | if (ret) { | |
33bbeb90 MD |
469 | PERROR("Error closing fd"); |
470 | } | |
471 | if (wait_shm_mmap == MAP_FAILED) { | |
472 | DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode."); | |
473 | goto error; | |
7fc90dca MD |
474 | } |
475 | return wait_shm_mmap; | |
476 | ||
477 | error: | |
478 | return NULL; | |
479 | } | |
480 | ||
481 | static | |
482 | void wait_for_sessiond(struct sock_info *sock_info) | |
483 | { | |
80e2814b MD |
484 | int ret; |
485 | ||
7fc90dca MD |
486 | ust_lock(); |
487 | if (lttng_ust_comm_should_quit) { | |
488 | goto quit; | |
489 | } | |
37ed587a MD |
490 | if (wait_poll_fallback) { |
491 | goto error; | |
492 | } | |
7fc90dca MD |
493 | if (!sock_info->wait_shm_mmap) { |
494 | sock_info->wait_shm_mmap = get_map_shm(sock_info); | |
495 | if (!sock_info->wait_shm_mmap) | |
496 | goto error; | |
497 | } | |
498 | ust_unlock(); | |
499 | ||
500 | DBG("Waiting for %s apps sessiond", sock_info->name); | |
80e2814b MD |
501 | /* Wait for futex wakeup */ |
502 | if (uatomic_read((int32_t *) sock_info->wait_shm_mmap) == 0) { | |
503 | ret = futex_async((int32_t *) sock_info->wait_shm_mmap, | |
504 | FUTEX_WAIT, 0, NULL, NULL, 0); | |
80e2814b | 505 | if (ret < 0) { |
37ed587a MD |
506 | if (errno == EFAULT) { |
507 | wait_poll_fallback = 1; | |
508 | ERR( | |
509 | "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) " | |
510 | "do not support FUTEX_WAKE on read-only memory mappings correctly. " | |
511 | "Please upgrade your kernel " | |
512 | "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel " | |
513 | "mainline). LTTng-UST will use polling mode fallback."); | |
514 | } | |
80e2814b | 515 | PERROR("futex"); |
80e2814b MD |
516 | } |
517 | } | |
7fc90dca MD |
518 | return; |
519 | ||
520 | quit: | |
521 | ust_unlock(); | |
522 | return; | |
523 | ||
524 | error: | |
525 | ust_unlock(); | |
7fc90dca | 526 | return; |
46050b1a MD |
527 | } |
528 | ||
1ea11eab MD |
529 | /* |
530 | * This thread does not allocate any resource, except within | |
531 | * handle_message, within mutex protection. This mutex protects against | |
532 | * fork and exit. | |
533 | * The other moment it allocates resources is at socket connexion, which | |
534 | * is also protected by the mutex. | |
535 | */ | |
d9e99d10 MD |
536 | static |
537 | void *ust_listener_thread(void *arg) | |
538 | { | |
1ea11eab | 539 | struct sock_info *sock_info = arg; |
c0eedf81 | 540 | int sock, ret, prev_connect_failed = 0, has_waited = 0; |
d9e99d10 | 541 | |
9eb62b9c MD |
542 | /* Restart trying to connect to the session daemon */ |
543 | restart: | |
c0eedf81 MD |
544 | if (prev_connect_failed) { |
545 | /* Wait for sessiond availability with pipe */ | |
546 | wait_for_sessiond(sock_info); | |
547 | if (has_waited) { | |
548 | has_waited = 0; | |
549 | /* | |
550 | * Sleep for 5 seconds before retrying after a | |
551 | * sequence of failure / wait / failure. This | |
552 | * deals with a killed or broken session daemon. | |
553 | */ | |
554 | sleep(5); | |
555 | } | |
556 | has_waited = 1; | |
557 | prev_connect_failed = 0; | |
558 | } | |
17dfb34b | 559 | ust_lock(); |
1ea11eab MD |
560 | |
561 | if (lttng_ust_comm_should_quit) { | |
17dfb34b | 562 | ust_unlock(); |
1ea11eab MD |
563 | goto quit; |
564 | } | |
9eb62b9c | 565 | |
1ea11eab MD |
566 | if (sock_info->socket != -1) { |
567 | ret = close(sock_info->socket); | |
568 | if (ret) { | |
11ff9c7d | 569 | ERR("Error closing %s apps socket", sock_info->name); |
1ea11eab MD |
570 | } |
571 | sock_info->socket = -1; | |
572 | } | |
46050b1a | 573 | |
9eb62b9c | 574 | /* Register */ |
1ea11eab | 575 | ret = lttcomm_connect_unix_sock(sock_info->sock_path); |
9eb62b9c | 576 | if (ret < 0) { |
11ff9c7d | 577 | ERR("Error connecting to %s apps socket", sock_info->name); |
c0eedf81 | 578 | prev_connect_failed = 1; |
11ff9c7d MD |
579 | /* |
580 | * If we cannot find the sessiond daemon, don't delay | |
581 | * constructor execution. | |
582 | */ | |
edaa1431 | 583 | ret = handle_register_done(sock_info); |
11ff9c7d | 584 | assert(!ret); |
17dfb34b | 585 | ust_unlock(); |
1ea11eab | 586 | goto restart; |
46050b1a MD |
587 | } |
588 | ||
589 | sock_info->socket = sock = ret; | |
590 | ||
591 | /* | |
592 | * Create only one root handle per listener thread for the whole | |
593 | * process lifetime. | |
594 | */ | |
595 | if (sock_info->root_handle == -1) { | |
596 | ret = lttng_abi_create_root_handle(); | |
597 | if (ret) { | |
598 | ERR("Error creating root handle"); | |
17dfb34b | 599 | ust_unlock(); |
46050b1a MD |
600 | goto quit; |
601 | } | |
602 | sock_info->root_handle = ret; | |
9eb62b9c | 603 | } |
1ea11eab | 604 | |
9eb62b9c MD |
605 | ret = register_app_to_sessiond(sock); |
606 | if (ret < 0) { | |
11ff9c7d | 607 | ERR("Error registering to %s apps socket", sock_info->name); |
c0eedf81 | 608 | prev_connect_failed = 1; |
11ff9c7d MD |
609 | /* |
610 | * If we cannot register to the sessiond daemon, don't | |
611 | * delay constructor execution. | |
612 | */ | |
edaa1431 | 613 | ret = handle_register_done(sock_info); |
11ff9c7d | 614 | assert(!ret); |
17dfb34b | 615 | ust_unlock(); |
9eb62b9c MD |
616 | goto restart; |
617 | } | |
17dfb34b | 618 | ust_unlock(); |
46050b1a | 619 | |
d9e99d10 MD |
620 | for (;;) { |
621 | ssize_t len; | |
e7723462 | 622 | struct lttcomm_ust_msg lum; |
d9e99d10 | 623 | |
e7723462 | 624 | len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum)); |
d9e99d10 MD |
625 | switch (len) { |
626 | case 0: /* orderly shutdown */ | |
11ff9c7d | 627 | DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name); |
d9e99d10 | 628 | goto end; |
e7723462 | 629 | case sizeof(lum): |
d9e99d10 | 630 | DBG("message received\n"); |
11ff9c7d | 631 | ret = handle_message(sock_info, sock, &lum); |
2a80c9d8 | 632 | if (ret < 0) { |
11ff9c7d | 633 | ERR("Error handling message for %s socket", sock_info->name); |
d9e99d10 MD |
634 | } |
635 | continue; | |
636 | case -1: | |
637 | if (errno == ECONNRESET) { | |
11ff9c7d | 638 | ERR("%s remote end closed connection\n", sock_info->name); |
d9e99d10 MD |
639 | goto end; |
640 | } | |
641 | goto end; | |
642 | default: | |
11ff9c7d | 643 | ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len); |
d9e99d10 MD |
644 | continue; |
645 | } | |
646 | ||
647 | } | |
648 | end: | |
9eb62b9c | 649 | goto restart; /* try to reconnect */ |
1ea11eab | 650 | quit: |
d9e99d10 MD |
651 | return NULL; |
652 | } | |
653 | ||
cf12a773 MD |
654 | /* |
655 | * Return values: -1: don't wait. 0: wait forever. 1: timeout wait. | |
656 | */ | |
11ff9c7d MD |
657 | static |
658 | int get_timeout(struct timespec *constructor_timeout) | |
659 | { | |
cf12a773 MD |
660 | long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS; |
661 | char *str_delay; | |
11ff9c7d MD |
662 | int ret; |
663 | ||
cf12a773 MD |
664 | str_delay = getenv("UST_REGISTER_TIMEOUT"); |
665 | if (str_delay) { | |
666 | constructor_delay_ms = strtol(str_delay, NULL, 10); | |
667 | } | |
668 | ||
669 | switch (constructor_delay_ms) { | |
670 | case -1:/* fall-through */ | |
671 | case 0: | |
672 | return constructor_delay_ms; | |
673 | default: | |
674 | break; | |
675 | } | |
676 | ||
677 | /* | |
678 | * If we are unable to find the current time, don't wait. | |
679 | */ | |
680 | ret = clock_gettime(CLOCK_REALTIME, constructor_timeout); | |
681 | if (ret) { | |
682 | return -1; | |
683 | } | |
95259bd0 MD |
684 | constructor_timeout->tv_sec += constructor_delay_ms / 1000UL; |
685 | constructor_timeout->tv_nsec += | |
686 | (constructor_delay_ms % 1000UL) * 1000000UL; | |
11ff9c7d MD |
687 | if (constructor_timeout->tv_nsec >= 1000000000UL) { |
688 | constructor_timeout->tv_sec++; | |
689 | constructor_timeout->tv_nsec -= 1000000000UL; | |
690 | } | |
cf12a773 | 691 | return 1; |
11ff9c7d | 692 | } |
d9e99d10 | 693 | |
2691221a MD |
694 | /* |
695 | * sessiond monitoring thread: monitor presence of global and per-user | |
696 | * sessiond by polling the application common named pipe. | |
697 | */ | |
698 | /* TODO */ | |
699 | ||
edaa1431 | 700 | void __attribute__((constructor)) lttng_ust_init(void) |
2691221a | 701 | { |
11ff9c7d | 702 | struct timespec constructor_timeout; |
cf12a773 | 703 | int timeout_mode; |
2691221a MD |
704 | int ret; |
705 | ||
edaa1431 MD |
706 | if (uatomic_xchg(&initialized, 1) == 1) |
707 | return; | |
708 | ||
709 | /* | |
710 | * We want precise control over the order in which we construct | |
711 | * our sub-libraries vs starting to receive commands from | |
712 | * sessiond (otherwise leading to errors when trying to create | |
713 | * sessiond before the init functions are completed). | |
714 | */ | |
2691221a | 715 | init_usterr(); |
edaa1431 MD |
716 | init_tracepoint(); |
717 | ltt_ring_buffer_metadata_client_init(); | |
718 | ltt_ring_buffer_client_overwrite_init(); | |
719 | ltt_ring_buffer_client_discard_init(); | |
2691221a | 720 | |
cf12a773 | 721 | timeout_mode = get_timeout(&constructor_timeout); |
11ff9c7d | 722 | |
95259bd0 | 723 | ret = sem_init(&constructor_wait, 0, 0); |
11ff9c7d MD |
724 | assert(!ret); |
725 | ||
8d20bf54 | 726 | ret = setup_local_apps(); |
2691221a | 727 | if (ret) { |
8d20bf54 | 728 | ERR("Error setting up to local apps"); |
2691221a | 729 | } |
1ea11eab MD |
730 | ret = pthread_create(&local_apps.ust_listener, NULL, |
731 | ust_listener_thread, &local_apps); | |
11ff9c7d | 732 | |
8d20bf54 MD |
733 | if (local_apps.allowed) { |
734 | ret = pthread_create(&global_apps.ust_listener, NULL, | |
735 | ust_listener_thread, &global_apps); | |
736 | } else { | |
737 | handle_register_done(&local_apps); | |
738 | } | |
739 | ||
cf12a773 MD |
740 | switch (timeout_mode) { |
741 | case 1: /* timeout wait */ | |
95259bd0 MD |
742 | do { |
743 | ret = sem_timedwait(&constructor_wait, | |
744 | &constructor_timeout); | |
745 | } while (ret < 0 && errno == EINTR); | |
cf12a773 MD |
746 | if (ret < 0 && errno == ETIMEDOUT) { |
747 | ERR("Timed out waiting for ltt-sessiond"); | |
748 | } else { | |
749 | assert(!ret); | |
750 | } | |
751 | break; | |
7b766b16 | 752 | case -1:/* wait forever */ |
95259bd0 MD |
753 | do { |
754 | ret = sem_wait(&constructor_wait); | |
755 | } while (ret < 0 && errno == EINTR); | |
11ff9c7d | 756 | assert(!ret); |
cf12a773 | 757 | break; |
7b766b16 | 758 | case 0: /* no timeout */ |
cf12a773 | 759 | break; |
11ff9c7d | 760 | } |
2691221a MD |
761 | } |
762 | ||
17dfb34b MD |
763 | static |
764 | void lttng_ust_cleanup(int exiting) | |
765 | { | |
766 | cleanup_sock_info(&global_apps); | |
767 | if (local_apps.allowed) { | |
768 | cleanup_sock_info(&local_apps); | |
769 | } | |
770 | lttng_ust_abi_exit(); | |
771 | ltt_events_exit(); | |
772 | ltt_ring_buffer_client_discard_exit(); | |
773 | ltt_ring_buffer_client_overwrite_exit(); | |
774 | ltt_ring_buffer_metadata_client_exit(); | |
775 | exit_tracepoint(); | |
776 | if (!exiting) { | |
777 | /* Reinitialize values for fork */ | |
778 | sem_count = 2; | |
779 | lttng_ust_comm_should_quit = 0; | |
780 | initialized = 0; | |
781 | } | |
782 | } | |
783 | ||
edaa1431 | 784 | void __attribute__((destructor)) lttng_ust_exit(void) |
2691221a MD |
785 | { |
786 | int ret; | |
787 | ||
9eb62b9c MD |
788 | /* |
789 | * Using pthread_cancel here because: | |
790 | * A) we don't want to hang application teardown. | |
791 | * B) the thread is not allocating any resource. | |
792 | */ | |
1ea11eab MD |
793 | |
794 | /* | |
795 | * Require the communication thread to quit. Synchronize with | |
796 | * mutexes to ensure it is not in a mutex critical section when | |
797 | * pthread_cancel is later called. | |
798 | */ | |
17dfb34b | 799 | ust_lock(); |
1ea11eab | 800 | lttng_ust_comm_should_quit = 1; |
17dfb34b | 801 | ust_unlock(); |
1ea11eab | 802 | |
1ea11eab | 803 | ret = pthread_cancel(global_apps.ust_listener); |
9eb62b9c MD |
804 | if (ret) { |
805 | ERR("Error cancelling global ust listener thread"); | |
2691221a | 806 | } |
8d20bf54 MD |
807 | if (local_apps.allowed) { |
808 | ret = pthread_cancel(local_apps.ust_listener); | |
809 | if (ret) { | |
810 | ERR("Error cancelling local ust listener thread"); | |
811 | } | |
8d20bf54 | 812 | } |
17dfb34b | 813 | lttng_ust_cleanup(1); |
2691221a | 814 | } |
e822f505 MD |
815 | |
816 | /* | |
817 | * We exclude the worker threads across fork and clone (except | |
818 | * CLONE_VM), because these system calls only keep the forking thread | |
819 | * running in the child. Therefore, we don't want to call fork or clone | |
820 | * in the middle of an tracepoint or ust tracing state modification. | |
821 | * Holding this mutex protects these structures across fork and clone. | |
822 | */ | |
823 | void ust_before_fork(ust_fork_info_t *fork_info) | |
824 | { | |
825 | /* | |
826 | * Disable signals. This is to avoid that the child intervenes | |
827 | * before it is properly setup for tracing. It is safer to | |
828 | * disable all signals, because then we know we are not breaking | |
829 | * anything by restoring the original mask. | |
830 | */ | |
831 | sigset_t all_sigs; | |
832 | int ret; | |
833 | ||
834 | /* Disable signals */ | |
835 | sigfillset(&all_sigs); | |
836 | ret = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs); | |
837 | if (ret == -1) { | |
838 | PERROR("sigprocmask"); | |
839 | } | |
17dfb34b | 840 | ust_lock(); |
e822f505 MD |
841 | rcu_bp_before_fork(); |
842 | } | |
843 | ||
844 | static void ust_after_fork_common(ust_fork_info_t *fork_info) | |
845 | { | |
846 | int ret; | |
847 | ||
17dfb34b MD |
848 | DBG("process %d", getpid()); |
849 | ust_unlock(); | |
e822f505 MD |
850 | /* Restore signals */ |
851 | ret = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL); | |
852 | if (ret == -1) { | |
853 | PERROR("sigprocmask"); | |
854 | } | |
855 | } | |
856 | ||
857 | void ust_after_fork_parent(ust_fork_info_t *fork_info) | |
858 | { | |
17dfb34b | 859 | DBG("process %d", getpid()); |
e822f505 MD |
860 | rcu_bp_after_fork_parent(); |
861 | /* Release mutexes and reenable signals */ | |
862 | ust_after_fork_common(fork_info); | |
863 | } | |
864 | ||
17dfb34b MD |
865 | /* |
866 | * After fork, in the child, we need to cleanup all the leftover state, | |
867 | * except the worker thread which already magically disappeared thanks | |
868 | * to the weird Linux fork semantics. After tyding up, we call | |
869 | * lttng_ust_init() again to start over as a new PID. | |
870 | * | |
871 | * This is meant for forks() that have tracing in the child between the | |
872 | * fork and following exec call (if there is any). | |
873 | */ | |
e822f505 MD |
874 | void ust_after_fork_child(ust_fork_info_t *fork_info) |
875 | { | |
17dfb34b | 876 | DBG("process %d", getpid()); |
e822f505 MD |
877 | /* Release urcu mutexes */ |
878 | rcu_bp_after_fork_child(); | |
17dfb34b | 879 | lttng_ust_cleanup(0); |
e822f505 MD |
880 | /* Release mutexes and reenable signals */ |
881 | ust_after_fork_common(fork_info); | |
318dfea9 | 882 | lttng_ust_init(); |
e822f505 | 883 | } |