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