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