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