Commit | Line | Data |
---|---|---|
2691221a MD |
1 | /* |
2 | * lttng-ust-comm.c | |
3 | * | |
4 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> | |
5 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
6 | * | |
7 | * This library is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; only | |
10 | * version 2.1 of the License. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
18 | * License along with this library; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
80e2814b | 22 | #define _LGPL_SOURCE |
2691221a MD |
23 | #include <sys/types.h> |
24 | #include <sys/socket.h> | |
7fc90dca MD |
25 | #include <sys/mman.h> |
26 | #include <sys/stat.h> | |
58d4b2a2 MD |
27 | #include <sys/types.h> |
28 | #include <sys/wait.h> | |
7fc90dca | 29 | #include <fcntl.h> |
2691221a MD |
30 | #include <unistd.h> |
31 | #include <errno.h> | |
d9e99d10 | 32 | #include <pthread.h> |
11ff9c7d MD |
33 | #include <semaphore.h> |
34 | #include <time.h> | |
1ea11eab | 35 | #include <assert.h> |
e822f505 | 36 | #include <signal.h> |
95259bd0 | 37 | #include <urcu/uatomic.h> |
80e2814b | 38 | #include <urcu/futex.h> |
c117fb1b | 39 | #include <urcu/compiler.h> |
1ea11eab | 40 | |
4318ae1b | 41 | #include <lttng/ust-events.h> |
4318ae1b | 42 | #include <lttng/ust-abi.h> |
4318ae1b | 43 | #include <lttng/ust.h> |
7bc53e94 | 44 | #include <lttng/ust-error.h> |
74d81a6c | 45 | #include <lttng/ust-ctl.h> |
8c90a710 | 46 | #include <urcu/tls-compat.h> |
44c72f10 MD |
47 | #include <ust-comm.h> |
48 | #include <usterr-signal-safe.h> | |
cd54f6d9 | 49 | #include <helper.h> |
44c72f10 | 50 | #include "tracepoint-internal.h" |
7dd08bec | 51 | #include "lttng-tracer-core.h" |
08114193 | 52 | #include "compat.h" |
f645cfa7 | 53 | #include "../libringbuffer/tlsfixup.h" |
394598c1 | 54 | #include "lttng-ust-baddr.h" |
edaa1431 MD |
55 | |
56 | /* | |
57 | * Has lttng ust comm constructor been called ? | |
58 | */ | |
59 | static int initialized; | |
60 | ||
1ea11eab | 61 | /* |
17dfb34b MD |
62 | * The ust_lock/ust_unlock lock is used as a communication thread mutex. |
63 | * Held when handling a command, also held by fork() to deal with | |
64 | * removal of threads, and by exit path. | |
3327ac33 MD |
65 | * |
66 | * The UST lock is the centralized mutex across UST tracing control and | |
67 | * probe registration. | |
68 | * | |
69 | * ust_exit_mutex must never nest in ust_mutex. | |
70 | */ | |
71 | static pthread_mutex_t ust_mutex = PTHREAD_MUTEX_INITIALIZER; | |
72 | ||
73 | /* | |
74 | * ust_exit_mutex protects thread_active variable wrt thread exit. It | |
75 | * cannot be done by ust_mutex because pthread_cancel(), which takes an | |
76 | * internal libc lock, cannot nest within ust_mutex. | |
77 | * | |
78 | * It never nests within a ust_mutex. | |
1ea11eab | 79 | */ |
3327ac33 | 80 | static pthread_mutex_t ust_exit_mutex = PTHREAD_MUTEX_INITIALIZER; |
1ea11eab MD |
81 | |
82 | /* Should the ust comm thread quit ? */ | |
83 | static int lttng_ust_comm_should_quit; | |
84 | ||
3327ac33 MD |
85 | /* |
86 | * Return 0 on success, -1 if should quilt. | |
87 | * The lock is taken in both cases. | |
88 | */ | |
89 | int ust_lock(void) | |
90 | { | |
91 | pthread_mutex_lock(&ust_mutex); | |
92 | if (lttng_ust_comm_should_quit) { | |
93 | return -1; | |
94 | } else { | |
95 | return 0; | |
96 | } | |
97 | } | |
98 | ||
99 | /* | |
100 | * ust_lock_nocheck() can be used in constructors/destructors, because | |
101 | * they are already nested within the dynamic loader lock, and therefore | |
102 | * have exclusive access against execution of liblttng-ust destructor. | |
103 | */ | |
104 | void ust_lock_nocheck(void) | |
105 | { | |
106 | pthread_mutex_lock(&ust_mutex); | |
107 | } | |
108 | ||
109 | void ust_unlock(void) | |
110 | { | |
111 | pthread_mutex_unlock(&ust_mutex); | |
112 | } | |
113 | ||
11ff9c7d MD |
114 | /* |
115 | * Wait for either of these before continuing to the main | |
116 | * program: | |
117 | * - the register_done message from sessiond daemon | |
118 | * (will let the sessiond daemon enable sessions before main | |
119 | * starts.) | |
120 | * - sessiond daemon is not reachable. | |
121 | * - timeout (ensuring applications are resilient to session | |
122 | * daemon problems). | |
123 | */ | |
124 | static sem_t constructor_wait; | |
950aab0c MD |
125 | /* |
126 | * Doing this for both the global and local sessiond. | |
127 | */ | |
95259bd0 | 128 | static int sem_count = { 2 }; |
11ff9c7d | 129 | |
e8508a49 MD |
130 | /* |
131 | * Counting nesting within lttng-ust. Used to ensure that calling fork() | |
132 | * from liblttng-ust does not execute the pre/post fork handlers. | |
133 | */ | |
8c90a710 | 134 | static DEFINE_URCU_TLS(int, lttng_ust_nest_count); |
e8508a49 | 135 | |
1ea11eab MD |
136 | /* |
137 | * Info about socket and associated listener thread. | |
138 | */ | |
139 | struct sock_info { | |
11ff9c7d | 140 | const char *name; |
1ea11eab | 141 | pthread_t ust_listener; /* listener thread */ |
46050b1a | 142 | int root_handle; |
8d20bf54 MD |
143 | int constructor_sem_posted; |
144 | int allowed; | |
44e073f5 | 145 | int global; |
e33f3265 | 146 | int thread_active; |
7fc90dca MD |
147 | |
148 | char sock_path[PATH_MAX]; | |
149 | int socket; | |
32ce8569 | 150 | int notify_socket; |
7fc90dca MD |
151 | |
152 | char wait_shm_path[PATH_MAX]; | |
153 | char *wait_shm_mmap; | |
37dddb65 MD |
154 | /* Keep track of lazy state dump not performed yet. */ |
155 | int statedump_pending; | |
1ea11eab | 156 | }; |
2691221a MD |
157 | |
158 | /* Socket from app (connect) to session daemon (listen) for communication */ | |
1ea11eab | 159 | struct sock_info global_apps = { |
11ff9c7d | 160 | .name = "global", |
44e073f5 | 161 | .global = 1, |
7fc90dca | 162 | |
46050b1a | 163 | .root_handle = -1, |
8d20bf54 | 164 | .allowed = 1, |
e33f3265 | 165 | .thread_active = 0, |
7fc90dca | 166 | |
32ce8569 | 167 | .sock_path = LTTNG_DEFAULT_RUNDIR "/" LTTNG_UST_SOCK_FILENAME, |
7fc90dca | 168 | .socket = -1, |
32ce8569 | 169 | .notify_socket = -1, |
7fc90dca | 170 | |
32ce8569 | 171 | .wait_shm_path = "/" LTTNG_UST_WAIT_FILENAME, |
95c25348 | 172 | |
37dddb65 | 173 | .statedump_pending = 0, |
1ea11eab | 174 | }; |
2691221a MD |
175 | |
176 | /* TODO: allow global_apps_sock_path override */ | |
177 | ||
1ea11eab | 178 | struct sock_info local_apps = { |
11ff9c7d | 179 | .name = "local", |
44e073f5 | 180 | .global = 0, |
46050b1a | 181 | .root_handle = -1, |
8d20bf54 | 182 | .allowed = 0, /* Check setuid bit first */ |
e33f3265 | 183 | .thread_active = 0, |
7fc90dca MD |
184 | |
185 | .socket = -1, | |
32ce8569 | 186 | .notify_socket = -1, |
95c25348 | 187 | |
37dddb65 | 188 | .statedump_pending = 0, |
1ea11eab | 189 | }; |
2691221a | 190 | |
37ed587a MD |
191 | static int wait_poll_fallback; |
192 | ||
74d81a6c MD |
193 | static const char *cmd_name_mapping[] = { |
194 | [ LTTNG_UST_RELEASE ] = "Release", | |
195 | [ LTTNG_UST_SESSION ] = "Create Session", | |
196 | [ LTTNG_UST_TRACER_VERSION ] = "Get Tracer Version", | |
197 | ||
198 | [ LTTNG_UST_TRACEPOINT_LIST ] = "Create Tracepoint List", | |
199 | [ LTTNG_UST_WAIT_QUIESCENT ] = "Wait for Quiescent State", | |
200 | [ LTTNG_UST_REGISTER_DONE ] = "Registration Done", | |
201 | [ LTTNG_UST_TRACEPOINT_FIELD_LIST ] = "Create Tracepoint Field List", | |
202 | ||
203 | /* Session FD commands */ | |
204 | [ LTTNG_UST_CHANNEL ] = "Create Channel", | |
205 | [ LTTNG_UST_SESSION_START ] = "Start Session", | |
206 | [ LTTNG_UST_SESSION_STOP ] = "Stop Session", | |
207 | ||
208 | /* Channel FD commands */ | |
209 | [ LTTNG_UST_STREAM ] = "Create Stream", | |
210 | [ LTTNG_UST_EVENT ] = "Create Event", | |
211 | ||
212 | /* Event and Channel FD commands */ | |
213 | [ LTTNG_UST_CONTEXT ] = "Create Context", | |
214 | [ LTTNG_UST_FLUSH_BUFFER ] = "Flush Buffer", | |
215 | ||
216 | /* Event, Channel and Session commands */ | |
217 | [ LTTNG_UST_ENABLE ] = "Enable", | |
218 | [ LTTNG_UST_DISABLE ] = "Disable", | |
219 | ||
220 | /* Tracepoint list commands */ | |
221 | [ LTTNG_UST_TRACEPOINT_LIST_GET ] = "List Next Tracepoint", | |
222 | [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET ] = "List Next Tracepoint Field", | |
223 | ||
224 | /* Event FD commands */ | |
225 | [ LTTNG_UST_FILTER ] = "Create Filter", | |
75582b3a | 226 | [ LTTNG_UST_EXCLUSION ] = "Add exclusions to event", |
74d81a6c MD |
227 | }; |
228 | ||
ff517991 MD |
229 | static const char *str_timeout; |
230 | static int got_timeout_env; | |
231 | ||
7dd08bec | 232 | extern void lttng_ring_buffer_client_overwrite_init(void); |
34a91bdb | 233 | extern void lttng_ring_buffer_client_overwrite_rt_init(void); |
7dd08bec | 234 | extern void lttng_ring_buffer_client_discard_init(void); |
34a91bdb | 235 | extern void lttng_ring_buffer_client_discard_rt_init(void); |
7dd08bec MD |
236 | extern void lttng_ring_buffer_metadata_client_init(void); |
237 | extern void lttng_ring_buffer_client_overwrite_exit(void); | |
34a91bdb | 238 | extern void lttng_ring_buffer_client_overwrite_rt_exit(void); |
7dd08bec | 239 | extern void lttng_ring_buffer_client_discard_exit(void); |
34a91bdb | 240 | extern void lttng_ring_buffer_client_discard_rt_exit(void); |
7dd08bec | 241 | extern void lttng_ring_buffer_metadata_client_exit(void); |
edaa1431 | 242 | |
3c6f6263 AM |
243 | /* |
244 | * Returns the HOME directory path. Caller MUST NOT free(3) the returned | |
245 | * pointer. | |
246 | */ | |
247 | static | |
248 | const char *get_lttng_home_dir(void) | |
249 | { | |
250 | const char *val; | |
251 | ||
252 | val = (const char *) getenv("LTTNG_HOME"); | |
253 | if (val != NULL) { | |
254 | return val; | |
255 | } | |
256 | return (const char *) getenv("HOME"); | |
257 | } | |
258 | ||
a903623f MD |
259 | /* |
260 | * Force a read (imply TLS fixup for dlopen) of TLS variables. | |
261 | */ | |
262 | static | |
263 | void lttng_fixup_nest_count_tls(void) | |
264 | { | |
8c90a710 | 265 | asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count))); |
a903623f MD |
266 | } |
267 | ||
32ce8569 MD |
268 | int lttng_get_notify_socket(void *owner) |
269 | { | |
270 | struct sock_info *info = owner; | |
271 | ||
272 | return info->notify_socket; | |
273 | } | |
274 | ||
74d81a6c MD |
275 | static |
276 | void print_cmd(int cmd, int handle) | |
277 | { | |
278 | const char *cmd_name = "Unknown"; | |
279 | ||
fd67a004 MD |
280 | if (cmd >= 0 && cmd < LTTNG_ARRAY_SIZE(cmd_name_mapping) |
281 | && cmd_name_mapping[cmd]) { | |
74d81a6c MD |
282 | cmd_name = cmd_name_mapping[cmd]; |
283 | } | |
fd67a004 MD |
284 | DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)", |
285 | cmd_name, cmd, | |
74d81a6c MD |
286 | lttng_ust_obj_get_name(handle), handle); |
287 | } | |
288 | ||
2691221a | 289 | static |
8d20bf54 | 290 | int setup_local_apps(void) |
2691221a MD |
291 | { |
292 | const char *home_dir; | |
7fc90dca | 293 | uid_t uid; |
2691221a | 294 | |
7fc90dca | 295 | uid = getuid(); |
8d20bf54 MD |
296 | /* |
297 | * Disallow per-user tracing for setuid binaries. | |
298 | */ | |
7fc90dca | 299 | if (uid != geteuid()) { |
9ec6895c | 300 | assert(local_apps.allowed == 0); |
d0a1ae63 | 301 | return 0; |
8d20bf54 | 302 | } |
3c6f6263 | 303 | home_dir = get_lttng_home_dir(); |
9ec6895c MD |
304 | if (!home_dir) { |
305 | WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing."); | |
306 | assert(local_apps.allowed == 0); | |
2691221a | 307 | return -ENOENT; |
9ec6895c MD |
308 | } |
309 | local_apps.allowed = 1; | |
32ce8569 MD |
310 | snprintf(local_apps.sock_path, PATH_MAX, "%s/%s/%s", |
311 | home_dir, | |
312 | LTTNG_DEFAULT_HOME_RUNDIR, | |
313 | LTTNG_UST_SOCK_FILENAME); | |
314 | snprintf(local_apps.wait_shm_path, PATH_MAX, "/%s-%u", | |
315 | LTTNG_UST_WAIT_FILENAME, | |
316 | uid); | |
2691221a MD |
317 | return 0; |
318 | } | |
319 | ||
ff517991 MD |
320 | /* |
321 | * Get notify_sock timeout, in ms. | |
322 | * -1: don't wait. 0: wait forever. >0: timeout, in ms. | |
323 | */ | |
324 | static | |
325 | long get_timeout(void) | |
326 | { | |
327 | long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS; | |
328 | ||
329 | if (!got_timeout_env) { | |
330 | str_timeout = getenv("LTTNG_UST_REGISTER_TIMEOUT"); | |
331 | got_timeout_env = 1; | |
332 | } | |
333 | if (str_timeout) | |
334 | constructor_delay_ms = strtol(str_timeout, NULL, 10); | |
335 | return constructor_delay_ms; | |
336 | } | |
337 | ||
338 | static | |
339 | long get_notify_sock_timeout(void) | |
340 | { | |
341 | return get_timeout(); | |
342 | } | |
343 | ||
344 | /* | |
345 | * Return values: -1: don't wait. 0: wait forever. 1: timeout wait. | |
346 | */ | |
347 | static | |
348 | int get_constructor_timeout(struct timespec *constructor_timeout) | |
349 | { | |
350 | long constructor_delay_ms; | |
351 | int ret; | |
352 | ||
353 | constructor_delay_ms = get_timeout(); | |
354 | ||
355 | switch (constructor_delay_ms) { | |
356 | case -1:/* fall-through */ | |
357 | case 0: | |
358 | return constructor_delay_ms; | |
359 | default: | |
360 | break; | |
361 | } | |
362 | ||
363 | /* | |
364 | * If we are unable to find the current time, don't wait. | |
365 | */ | |
366 | ret = clock_gettime(CLOCK_REALTIME, constructor_timeout); | |
367 | if (ret) { | |
368 | return -1; | |
369 | } | |
370 | constructor_timeout->tv_sec += constructor_delay_ms / 1000UL; | |
371 | constructor_timeout->tv_nsec += | |
372 | (constructor_delay_ms % 1000UL) * 1000000UL; | |
373 | if (constructor_timeout->tv_nsec >= 1000000000UL) { | |
374 | constructor_timeout->tv_sec++; | |
375 | constructor_timeout->tv_nsec -= 1000000000UL; | |
376 | } | |
377 | return 1; | |
378 | } | |
379 | ||
2691221a | 380 | static |
32ce8569 | 381 | int register_to_sessiond(int socket, enum ustctl_socket_type type) |
2691221a | 382 | { |
32ce8569 MD |
383 | return ustcomm_send_reg_msg(socket, |
384 | type, | |
385 | CAA_BITS_PER_LONG, | |
386 | lttng_alignof(uint8_t) * CHAR_BIT, | |
387 | lttng_alignof(uint16_t) * CHAR_BIT, | |
388 | lttng_alignof(uint32_t) * CHAR_BIT, | |
389 | lttng_alignof(uint64_t) * CHAR_BIT, | |
390 | lttng_alignof(unsigned long) * CHAR_BIT); | |
2691221a MD |
391 | } |
392 | ||
d9e99d10 | 393 | static |
57773204 | 394 | int send_reply(int sock, struct ustcomm_ust_reply *lur) |
d9e99d10 | 395 | { |
9eb62b9c | 396 | ssize_t len; |
d3a492d1 | 397 | |
57773204 | 398 | len = ustcomm_send_unix_sock(sock, lur, sizeof(*lur)); |
d3a492d1 | 399 | switch (len) { |
a4be8962 | 400 | case sizeof(*lur): |
d3a492d1 MD |
401 | DBG("message successfully sent"); |
402 | return 0; | |
7bc53e94 MD |
403 | default: |
404 | if (len == -ECONNRESET) { | |
405 | DBG("remote end closed connection"); | |
d3a492d1 MD |
406 | return 0; |
407 | } | |
7bc53e94 MD |
408 | if (len < 0) |
409 | return len; | |
410 | DBG("incorrect message size: %zd", len); | |
411 | return -EINVAL; | |
d3a492d1 MD |
412 | } |
413 | } | |
414 | ||
415 | static | |
edaa1431 | 416 | int handle_register_done(struct sock_info *sock_info) |
11ff9c7d MD |
417 | { |
418 | int ret; | |
419 | ||
edaa1431 MD |
420 | if (sock_info->constructor_sem_posted) |
421 | return 0; | |
422 | sock_info->constructor_sem_posted = 1; | |
56cd7e2f MD |
423 | if (uatomic_read(&sem_count) <= 0) { |
424 | return 0; | |
425 | } | |
95259bd0 MD |
426 | ret = uatomic_add_return(&sem_count, -1); |
427 | if (ret == 0) { | |
428 | ret = sem_post(&constructor_wait); | |
429 | assert(!ret); | |
430 | } | |
11ff9c7d MD |
431 | return 0; |
432 | } | |
433 | ||
37dddb65 MD |
434 | /* |
435 | * Only execute pending statedump after the constructor semaphore has | |
436 | * been posted by each listener thread. This means statedump will only | |
437 | * be performed after the "registration done" command is received from | |
438 | * each session daemon the application is connected to. | |
439 | * | |
440 | * This ensures we don't run into deadlock issues with the dynamic | |
441 | * loader mutex, which is held while the constructor is called and | |
442 | * waiting on the constructor semaphore. All operations requiring this | |
443 | * dynamic loader lock need to be postponed using this mechanism. | |
444 | */ | |
445 | static | |
446 | void handle_pending_statedump(struct sock_info *sock_info) | |
447 | { | |
448 | int ctor_passed = sock_info->constructor_sem_posted; | |
449 | ||
450 | if (ctor_passed && sock_info->statedump_pending) { | |
451 | sock_info->statedump_pending = 0; | |
452 | lttng_handle_pending_statedump(sock_info); | |
453 | } | |
454 | } | |
455 | ||
11ff9c7d MD |
456 | static |
457 | int handle_message(struct sock_info *sock_info, | |
57773204 | 458 | int sock, struct ustcomm_ust_msg *lum) |
d3a492d1 | 459 | { |
1ea11eab | 460 | int ret = 0; |
b61ce3b2 | 461 | const struct lttng_ust_objd_ops *ops; |
57773204 | 462 | struct ustcomm_ust_reply lur; |
ef9ff354 | 463 | union ust_args args; |
40003310 | 464 | ssize_t len; |
1ea11eab | 465 | |
46050b1a MD |
466 | memset(&lur, 0, sizeof(lur)); |
467 | ||
3327ac33 | 468 | if (ust_lock()) { |
74d81a6c | 469 | ret = -LTTNG_UST_ERR_EXITING; |
1ea11eab MD |
470 | goto end; |
471 | } | |
9eb62b9c | 472 | |
46050b1a MD |
473 | ops = objd_ops(lum->handle); |
474 | if (!ops) { | |
475 | ret = -ENOENT; | |
476 | goto end; | |
1ea11eab | 477 | } |
46050b1a MD |
478 | |
479 | switch (lum->cmd) { | |
11ff9c7d MD |
480 | case LTTNG_UST_REGISTER_DONE: |
481 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
edaa1431 | 482 | ret = handle_register_done(sock_info); |
11ff9c7d MD |
483 | else |
484 | ret = -EINVAL; | |
485 | break; | |
46050b1a MD |
486 | case LTTNG_UST_RELEASE: |
487 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
488 | ret = -EPERM; | |
489 | else | |
1849ef7c | 490 | ret = lttng_ust_objd_unref(lum->handle, 1); |
d9e99d10 | 491 | break; |
2d78951a MD |
492 | case LTTNG_UST_FILTER: |
493 | { | |
494 | /* Receive filter data */ | |
f488575f | 495 | struct lttng_ust_filter_bytecode_node *bytecode; |
2d78951a | 496 | |
cd54f6d9 | 497 | if (lum->u.filter.data_size > FILTER_BYTECODE_MAX_LEN) { |
7bc53e94 | 498 | ERR("Filter data size is too large: %u bytes", |
2d78951a MD |
499 | lum->u.filter.data_size); |
500 | ret = -EINVAL; | |
501 | goto error; | |
502 | } | |
2734ca65 | 503 | |
885b1dfd | 504 | if (lum->u.filter.reloc_offset > lum->u.filter.data_size) { |
7bc53e94 | 505 | ERR("Filter reloc offset %u is not within data", |
2734ca65 CB |
506 | lum->u.filter.reloc_offset); |
507 | ret = -EINVAL; | |
508 | goto error; | |
509 | } | |
510 | ||
cd54f6d9 MD |
511 | bytecode = zmalloc(sizeof(*bytecode) + lum->u.filter.data_size); |
512 | if (!bytecode) { | |
513 | ret = -ENOMEM; | |
514 | goto error; | |
515 | } | |
f488575f | 516 | len = ustcomm_recv_unix_sock(sock, bytecode->bc.data, |
2d78951a MD |
517 | lum->u.filter.data_size); |
518 | switch (len) { | |
519 | case 0: /* orderly shutdown */ | |
520 | ret = 0; | |
cd54f6d9 | 521 | free(bytecode); |
2d78951a | 522 | goto error; |
2d78951a MD |
523 | default: |
524 | if (len == lum->u.filter.data_size) { | |
7bc53e94 | 525 | DBG("filter data received"); |
2d78951a | 526 | break; |
7bc53e94 MD |
527 | } else if (len < 0) { |
528 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); | |
529 | if (len == -ECONNRESET) { | |
530 | ERR("%s remote end closed connection", sock_info->name); | |
531 | ret = len; | |
532 | free(bytecode); | |
533 | goto error; | |
534 | } | |
535 | ret = len; | |
eb8bf361 | 536 | free(bytecode); |
7bc53e94 | 537 | goto end; |
2d78951a | 538 | } else { |
7bc53e94 | 539 | DBG("incorrect filter data message size: %zd", len); |
2d78951a | 540 | ret = -EINVAL; |
cd54f6d9 | 541 | free(bytecode); |
2d78951a MD |
542 | goto end; |
543 | } | |
544 | } | |
f488575f MD |
545 | bytecode->bc.len = lum->u.filter.data_size; |
546 | bytecode->bc.reloc_offset = lum->u.filter.reloc_offset; | |
3f6fd224 | 547 | bytecode->bc.seqnum = lum->u.filter.seqnum; |
cd54f6d9 | 548 | if (ops->cmd) { |
2d78951a | 549 | ret = ops->cmd(lum->handle, lum->cmd, |
cd54f6d9 | 550 | (unsigned long) bytecode, |
f59ed768 | 551 | &args, sock_info); |
cd54f6d9 MD |
552 | if (ret) { |
553 | free(bytecode); | |
554 | } | |
555 | /* don't free bytecode if everything went fine. */ | |
556 | } else { | |
2d78951a | 557 | ret = -ENOSYS; |
cd54f6d9 MD |
558 | free(bytecode); |
559 | } | |
2d78951a MD |
560 | break; |
561 | } | |
86e36163 JI |
562 | case LTTNG_UST_EXCLUSION: |
563 | { | |
564 | /* Receive exclusion names */ | |
565 | struct lttng_ust_excluder_node *node; | |
566 | unsigned int count; | |
567 | ||
568 | count = lum->u.exclusion.count; | |
569 | if (count == 0) { | |
570 | /* There are no names to read */ | |
571 | ret = 0; | |
572 | goto error; | |
573 | } | |
574 | node = zmalloc(sizeof(*node) + | |
575 | count * LTTNG_UST_SYM_NAME_LEN); | |
576 | if (!node) { | |
577 | ret = -ENOMEM; | |
578 | goto error; | |
579 | } | |
580 | node->excluder.count = count; | |
581 | len = ustcomm_recv_unix_sock(sock, node->excluder.names, | |
582 | count * LTTNG_UST_SYM_NAME_LEN); | |
583 | switch (len) { | |
584 | case 0: /* orderly shutdown */ | |
585 | ret = 0; | |
586 | free(node); | |
587 | goto error; | |
588 | default: | |
589 | if (len == count * LTTNG_UST_SYM_NAME_LEN) { | |
590 | DBG("Exclusion data received"); | |
591 | break; | |
592 | } else if (len < 0) { | |
593 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); | |
594 | if (len == -ECONNRESET) { | |
595 | ERR("%s remote end closed connection", sock_info->name); | |
596 | ret = len; | |
597 | free(node); | |
598 | goto error; | |
599 | } | |
600 | ret = len; | |
601 | free(node); | |
602 | goto end; | |
603 | } else { | |
604 | DBG("Incorrect exclusion data message size: %zd", len); | |
605 | ret = -EINVAL; | |
606 | free(node); | |
607 | goto end; | |
608 | } | |
609 | } | |
610 | if (ops->cmd) { | |
611 | ret = ops->cmd(lum->handle, lum->cmd, | |
612 | (unsigned long) node, | |
613 | &args, sock_info); | |
614 | if (ret) { | |
615 | free(node); | |
616 | } | |
617 | /* Don't free exclusion data if everything went fine. */ | |
618 | } else { | |
619 | ret = -ENOSYS; | |
620 | free(node); | |
621 | } | |
622 | break; | |
623 | } | |
74d81a6c MD |
624 | case LTTNG_UST_CHANNEL: |
625 | { | |
626 | void *chan_data; | |
ff0f5728 | 627 | int wakeup_fd; |
74d81a6c MD |
628 | |
629 | len = ustcomm_recv_channel_from_sessiond(sock, | |
ff0f5728 MD |
630 | &chan_data, lum->u.channel.len, |
631 | &wakeup_fd); | |
74d81a6c MD |
632 | switch (len) { |
633 | case 0: /* orderly shutdown */ | |
634 | ret = 0; | |
635 | goto error; | |
636 | default: | |
637 | if (len == lum->u.channel.len) { | |
638 | DBG("channel data received"); | |
639 | break; | |
640 | } else if (len < 0) { | |
641 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); | |
642 | if (len == -ECONNRESET) { | |
643 | ERR("%s remote end closed connection", sock_info->name); | |
644 | ret = len; | |
645 | goto error; | |
646 | } | |
647 | ret = len; | |
648 | goto end; | |
649 | } else { | |
650 | DBG("incorrect channel data message size: %zd", len); | |
651 | ret = -EINVAL; | |
652 | goto end; | |
653 | } | |
654 | } | |
655 | args.channel.chan_data = chan_data; | |
ff0f5728 | 656 | args.channel.wakeup_fd = wakeup_fd; |
74d81a6c MD |
657 | if (ops->cmd) |
658 | ret = ops->cmd(lum->handle, lum->cmd, | |
659 | (unsigned long) &lum->u, | |
660 | &args, sock_info); | |
661 | else | |
662 | ret = -ENOSYS; | |
663 | break; | |
664 | } | |
665 | case LTTNG_UST_STREAM: | |
666 | { | |
667 | /* Receive shm_fd, wakeup_fd */ | |
668 | ret = ustcomm_recv_stream_from_sessiond(sock, | |
669 | &lum->u.stream.len, | |
670 | &args.stream.shm_fd, | |
671 | &args.stream.wakeup_fd); | |
672 | if (ret) { | |
673 | goto end; | |
674 | } | |
675 | if (ops->cmd) | |
676 | ret = ops->cmd(lum->handle, lum->cmd, | |
677 | (unsigned long) &lum->u, | |
678 | &args, sock_info); | |
679 | else | |
680 | ret = -ENOSYS; | |
681 | break; | |
682 | } | |
d9e99d10 | 683 | default: |
46050b1a MD |
684 | if (ops->cmd) |
685 | ret = ops->cmd(lum->handle, lum->cmd, | |
ef9ff354 | 686 | (unsigned long) &lum->u, |
f59ed768 | 687 | &args, sock_info); |
46050b1a MD |
688 | else |
689 | ret = -ENOSYS; | |
690 | break; | |
d9e99d10 | 691 | } |
46050b1a | 692 | |
1ea11eab | 693 | end: |
46050b1a MD |
694 | lur.handle = lum->handle; |
695 | lur.cmd = lum->cmd; | |
696 | lur.ret_val = ret; | |
697 | if (ret >= 0) { | |
7bc53e94 | 698 | lur.ret_code = LTTNG_UST_OK; |
46050b1a | 699 | } else { |
7bc53e94 MD |
700 | /* |
701 | * Use -LTTNG_UST_ERR as wildcard for UST internal | |
702 | * error that are not caused by the transport, except if | |
703 | * we already have a more precise error message to | |
704 | * report. | |
705 | */ | |
64b2564e DG |
706 | if (ret > -LTTNG_UST_ERR) { |
707 | /* Translate code to UST error. */ | |
708 | switch (ret) { | |
709 | case -EEXIST: | |
710 | lur.ret_code = -LTTNG_UST_ERR_EXIST; | |
711 | break; | |
712 | case -EINVAL: | |
713 | lur.ret_code = -LTTNG_UST_ERR_INVAL; | |
714 | break; | |
715 | case -ENOENT: | |
716 | lur.ret_code = -LTTNG_UST_ERR_NOENT; | |
717 | break; | |
718 | case -EPERM: | |
719 | lur.ret_code = -LTTNG_UST_ERR_PERM; | |
720 | break; | |
721 | case -ENOSYS: | |
722 | lur.ret_code = -LTTNG_UST_ERR_NOSYS; | |
723 | break; | |
724 | default: | |
725 | lur.ret_code = -LTTNG_UST_ERR; | |
726 | break; | |
727 | } | |
728 | } else { | |
7bc53e94 | 729 | lur.ret_code = ret; |
64b2564e | 730 | } |
46050b1a | 731 | } |
e6ea14c5 MD |
732 | if (ret >= 0) { |
733 | switch (lum->cmd) { | |
e6ea14c5 MD |
734 | case LTTNG_UST_TRACER_VERSION: |
735 | lur.u.version = lum->u.version; | |
736 | break; | |
737 | case LTTNG_UST_TRACEPOINT_LIST_GET: | |
738 | memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint)); | |
739 | break; | |
740 | } | |
381c0f1e | 741 | } |
74d81a6c | 742 | DBG("Return value: %d", lur.ret_val); |
46050b1a | 743 | ret = send_reply(sock, &lur); |
193183fb | 744 | if (ret < 0) { |
7bc53e94 | 745 | DBG("error sending reply"); |
193183fb MD |
746 | goto error; |
747 | } | |
46050b1a | 748 | |
40003310 MD |
749 | /* |
750 | * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field | |
751 | * after the reply. | |
752 | */ | |
7bc53e94 | 753 | if (lur.ret_code == LTTNG_UST_OK) { |
40003310 MD |
754 | switch (lum->cmd) { |
755 | case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET: | |
756 | len = ustcomm_send_unix_sock(sock, | |
757 | &args.field_list.entry, | |
758 | sizeof(args.field_list.entry)); | |
7bc53e94 MD |
759 | if (len < 0) { |
760 | ret = len; | |
761 | goto error; | |
762 | } | |
40003310 | 763 | if (len != sizeof(args.field_list.entry)) { |
7bc53e94 | 764 | ret = -EINVAL; |
40003310 MD |
765 | goto error; |
766 | } | |
767 | } | |
768 | } | |
ef9ff354 | 769 | |
381c0f1e | 770 | error: |
17dfb34b | 771 | ust_unlock(); |
d9e99d10 | 772 | |
37dddb65 MD |
773 | /* |
774 | * Performed delayed statedump operations outside of the UST | |
775 | * lock. We need to take the dynamic loader lock before we take | |
776 | * the UST lock internally within handle_pending_statedump(). | |
777 | */ | |
778 | handle_pending_statedump(sock_info); | |
246be17e | 779 | |
37dddb65 | 780 | return ret; |
246be17e PW |
781 | } |
782 | ||
46050b1a | 783 | static |
efe0de09 | 784 | void cleanup_sock_info(struct sock_info *sock_info, int exiting) |
46050b1a MD |
785 | { |
786 | int ret; | |
787 | ||
5b14aab3 MD |
788 | if (sock_info->root_handle != -1) { |
789 | ret = lttng_ust_objd_unref(sock_info->root_handle, 1); | |
790 | if (ret) { | |
791 | ERR("Error unref root handle"); | |
792 | } | |
793 | sock_info->root_handle = -1; | |
794 | } | |
795 | sock_info->constructor_sem_posted = 0; | |
796 | ||
797 | /* | |
798 | * wait_shm_mmap, socket and notify socket are used by listener | |
799 | * threads outside of the ust lock, so we cannot tear them down | |
800 | * ourselves, because we cannot join on these threads. Leave | |
801 | * responsibility of cleaning up these resources to the OS | |
802 | * process exit. | |
803 | */ | |
804 | if (exiting) | |
805 | return; | |
806 | ||
46050b1a | 807 | if (sock_info->socket != -1) { |
e6973a89 | 808 | ret = ustcomm_close_unix_sock(sock_info->socket); |
46050b1a | 809 | if (ret) { |
32ce8569 | 810 | ERR("Error closing ust cmd socket"); |
46050b1a MD |
811 | } |
812 | sock_info->socket = -1; | |
813 | } | |
32ce8569 MD |
814 | if (sock_info->notify_socket != -1) { |
815 | ret = ustcomm_close_unix_sock(sock_info->notify_socket); | |
816 | if (ret) { | |
817 | ERR("Error closing ust notify socket"); | |
818 | } | |
819 | sock_info->notify_socket = -1; | |
820 | } | |
5b14aab3 | 821 | if (sock_info->wait_shm_mmap) { |
7fc90dca MD |
822 | ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE)); |
823 | if (ret) { | |
824 | ERR("Error unmapping wait shm"); | |
825 | } | |
826 | sock_info->wait_shm_mmap = NULL; | |
827 | } | |
828 | } | |
829 | ||
58d4b2a2 | 830 | /* |
33bbeb90 MD |
831 | * Using fork to set umask in the child process (not multi-thread safe). |
832 | * We deal with the shm_open vs ftruncate race (happening when the | |
833 | * sessiond owns the shm and does not let everybody modify it, to ensure | |
834 | * safety against shm_unlink) by simply letting the mmap fail and | |
835 | * retrying after a few seconds. | |
836 | * For global shm, everybody has rw access to it until the sessiond | |
837 | * starts. | |
58d4b2a2 | 838 | */ |
7fc90dca | 839 | static |
58d4b2a2 | 840 | int get_wait_shm(struct sock_info *sock_info, size_t mmap_size) |
7fc90dca | 841 | { |
7fc90dca | 842 | int wait_shm_fd, ret; |
58d4b2a2 | 843 | pid_t pid; |
44e073f5 | 844 | |
58d4b2a2 | 845 | /* |
33bbeb90 | 846 | * Try to open read-only. |
58d4b2a2 | 847 | */ |
33bbeb90 | 848 | wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0); |
58d4b2a2 | 849 | if (wait_shm_fd >= 0) { |
7aa76730 MD |
850 | int32_t tmp_read; |
851 | ssize_t len; | |
852 | size_t bytes_read = 0; | |
853 | ||
854 | /* | |
855 | * Try to read the fd. If unable to do so, try opening | |
856 | * it in write mode. | |
857 | */ | |
858 | do { | |
859 | len = read(wait_shm_fd, | |
860 | &((char *) &tmp_read)[bytes_read], | |
861 | sizeof(tmp_read) - bytes_read); | |
862 | if (len > 0) { | |
863 | bytes_read += len; | |
864 | } | |
865 | } while ((len < 0 && errno == EINTR) | |
866 | || (len > 0 && bytes_read < sizeof(tmp_read))); | |
867 | if (bytes_read != sizeof(tmp_read)) { | |
868 | ret = close(wait_shm_fd); | |
869 | if (ret) { | |
870 | ERR("close wait_shm_fd"); | |
871 | } | |
872 | goto open_write; | |
873 | } | |
58d4b2a2 MD |
874 | goto end; |
875 | } else if (wait_shm_fd < 0 && errno != ENOENT) { | |
876 | /* | |
33bbeb90 MD |
877 | * Real-only open did not work, and it's not because the |
878 | * entry was not present. It's a failure that prohibits | |
879 | * using shm. | |
58d4b2a2 | 880 | */ |
7fc90dca | 881 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
58d4b2a2 | 882 | goto end; |
7fc90dca | 883 | } |
7aa76730 MD |
884 | |
885 | open_write: | |
7fc90dca | 886 | /* |
7aa76730 MD |
887 | * If the open failed because the file did not exist, or because |
888 | * the file was not truncated yet, try creating it ourself. | |
7fc90dca | 889 | */ |
8c90a710 | 890 | URCU_TLS(lttng_ust_nest_count)++; |
58d4b2a2 | 891 | pid = fork(); |
8c90a710 | 892 | URCU_TLS(lttng_ust_nest_count)--; |
58d4b2a2 MD |
893 | if (pid > 0) { |
894 | int status; | |
895 | ||
896 | /* | |
897 | * Parent: wait for child to return, in which case the | |
898 | * shared memory map will have been created. | |
899 | */ | |
900 | pid = wait(&status); | |
b7d3cb32 | 901 | if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
58d4b2a2 MD |
902 | wait_shm_fd = -1; |
903 | goto end; | |
7fc90dca | 904 | } |
58d4b2a2 MD |
905 | /* |
906 | * Try to open read-only again after creation. | |
907 | */ | |
33bbeb90 | 908 | wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0); |
58d4b2a2 MD |
909 | if (wait_shm_fd < 0) { |
910 | /* | |
911 | * Real-only open did not work. It's a failure | |
912 | * that prohibits using shm. | |
913 | */ | |
914 | ERR("Error opening shm %s", sock_info->wait_shm_path); | |
915 | goto end; | |
916 | } | |
917 | goto end; | |
918 | } else if (pid == 0) { | |
919 | int create_mode; | |
920 | ||
921 | /* Child */ | |
33bbeb90 | 922 | create_mode = S_IRUSR | S_IWUSR | S_IRGRP; |
58d4b2a2 | 923 | if (sock_info->global) |
33bbeb90 | 924 | create_mode |= S_IROTH | S_IWGRP | S_IWOTH; |
58d4b2a2 MD |
925 | /* |
926 | * We're alone in a child process, so we can modify the | |
927 | * process-wide umask. | |
928 | */ | |
33bbeb90 | 929 | umask(~create_mode); |
58d4b2a2 | 930 | /* |
33bbeb90 MD |
931 | * Try creating shm (or get rw access). |
932 | * We don't do an exclusive open, because we allow other | |
933 | * processes to create+ftruncate it concurrently. | |
58d4b2a2 MD |
934 | */ |
935 | wait_shm_fd = shm_open(sock_info->wait_shm_path, | |
936 | O_RDWR | O_CREAT, create_mode); | |
937 | if (wait_shm_fd >= 0) { | |
938 | ret = ftruncate(wait_shm_fd, mmap_size); | |
939 | if (ret) { | |
940 | PERROR("ftruncate"); | |
b0c1425d | 941 | _exit(EXIT_FAILURE); |
58d4b2a2 | 942 | } |
b0c1425d | 943 | _exit(EXIT_SUCCESS); |
58d4b2a2 | 944 | } |
33bbeb90 MD |
945 | /* |
946 | * For local shm, we need to have rw access to accept | |
947 | * opening it: this means the local sessiond will be | |
948 | * able to wake us up. For global shm, we open it even | |
949 | * if rw access is not granted, because the root.root | |
950 | * sessiond will be able to override all rights and wake | |
951 | * us up. | |
952 | */ | |
953 | if (!sock_info->global && errno != EACCES) { | |
58d4b2a2 | 954 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
5d3bc5ed | 955 | _exit(EXIT_FAILURE); |
58d4b2a2 MD |
956 | } |
957 | /* | |
33bbeb90 MD |
958 | * The shm exists, but we cannot open it RW. Report |
959 | * success. | |
58d4b2a2 | 960 | */ |
5d3bc5ed | 961 | _exit(EXIT_SUCCESS); |
58d4b2a2 MD |
962 | } else { |
963 | return -1; | |
7fc90dca | 964 | } |
58d4b2a2 | 965 | end: |
33bbeb90 MD |
966 | if (wait_shm_fd >= 0 && !sock_info->global) { |
967 | struct stat statbuf; | |
968 | ||
969 | /* | |
970 | * Ensure that our user is the owner of the shm file for | |
971 | * local shm. If we do not own the file, it means our | |
972 | * sessiond will not have access to wake us up (there is | |
973 | * probably a rogue process trying to fake our | |
974 | * sessiond). Fallback to polling method in this case. | |
975 | */ | |
976 | ret = fstat(wait_shm_fd, &statbuf); | |
977 | if (ret) { | |
978 | PERROR("fstat"); | |
979 | goto error_close; | |
980 | } | |
981 | if (statbuf.st_uid != getuid()) | |
982 | goto error_close; | |
983 | } | |
58d4b2a2 | 984 | return wait_shm_fd; |
33bbeb90 MD |
985 | |
986 | error_close: | |
987 | ret = close(wait_shm_fd); | |
988 | if (ret) { | |
989 | PERROR("Error closing fd"); | |
990 | } | |
991 | return -1; | |
58d4b2a2 MD |
992 | } |
993 | ||
994 | static | |
995 | char *get_map_shm(struct sock_info *sock_info) | |
996 | { | |
997 | size_t mmap_size = sysconf(_SC_PAGE_SIZE); | |
998 | int wait_shm_fd, ret; | |
999 | char *wait_shm_mmap; | |
1000 | ||
1001 | wait_shm_fd = get_wait_shm(sock_info, mmap_size); | |
1002 | if (wait_shm_fd < 0) { | |
1003 | goto error; | |
44e073f5 | 1004 | } |
7fc90dca MD |
1005 | wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ, |
1006 | MAP_SHARED, wait_shm_fd, 0); | |
7fc90dca MD |
1007 | /* close shm fd immediately after taking the mmap reference */ |
1008 | ret = close(wait_shm_fd); | |
1009 | if (ret) { | |
33bbeb90 MD |
1010 | PERROR("Error closing fd"); |
1011 | } | |
1012 | if (wait_shm_mmap == MAP_FAILED) { | |
1013 | DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode."); | |
1014 | goto error; | |
7fc90dca MD |
1015 | } |
1016 | return wait_shm_mmap; | |
1017 | ||
1018 | error: | |
1019 | return NULL; | |
1020 | } | |
1021 | ||
1022 | static | |
1023 | void wait_for_sessiond(struct sock_info *sock_info) | |
1024 | { | |
efe0de09 | 1025 | int ret; |
80e2814b | 1026 | |
3327ac33 | 1027 | if (ust_lock()) { |
7fc90dca MD |
1028 | goto quit; |
1029 | } | |
37ed587a MD |
1030 | if (wait_poll_fallback) { |
1031 | goto error; | |
1032 | } | |
7fc90dca MD |
1033 | if (!sock_info->wait_shm_mmap) { |
1034 | sock_info->wait_shm_mmap = get_map_shm(sock_info); | |
1035 | if (!sock_info->wait_shm_mmap) | |
1036 | goto error; | |
1037 | } | |
1038 | ust_unlock(); | |
1039 | ||
1040 | DBG("Waiting for %s apps sessiond", sock_info->name); | |
80e2814b MD |
1041 | /* Wait for futex wakeup */ |
1042 | if (uatomic_read((int32_t *) sock_info->wait_shm_mmap) == 0) { | |
1043 | ret = futex_async((int32_t *) sock_info->wait_shm_mmap, | |
1044 | FUTEX_WAIT, 0, NULL, NULL, 0); | |
80e2814b | 1045 | if (ret < 0) { |
37ed587a MD |
1046 | if (errno == EFAULT) { |
1047 | wait_poll_fallback = 1; | |
a8b870ad | 1048 | DBG( |
37ed587a MD |
1049 | "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) " |
1050 | "do not support FUTEX_WAKE on read-only memory mappings correctly. " | |
1051 | "Please upgrade your kernel " | |
1052 | "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel " | |
1053 | "mainline). LTTng-UST will use polling mode fallback."); | |
cd27263b MD |
1054 | if (ust_debug()) |
1055 | PERROR("futex"); | |
37ed587a | 1056 | } |
80e2814b MD |
1057 | } |
1058 | } | |
7fc90dca MD |
1059 | return; |
1060 | ||
1061 | quit: | |
1062 | ust_unlock(); | |
1063 | return; | |
1064 | ||
1065 | error: | |
1066 | ust_unlock(); | |
7fc90dca | 1067 | return; |
46050b1a MD |
1068 | } |
1069 | ||
1ea11eab MD |
1070 | /* |
1071 | * This thread does not allocate any resource, except within | |
1072 | * handle_message, within mutex protection. This mutex protects against | |
1073 | * fork and exit. | |
98bf993f | 1074 | * The other moment it allocates resources is at socket connection, which |
1ea11eab MD |
1075 | * is also protected by the mutex. |
1076 | */ | |
d9e99d10 MD |
1077 | static |
1078 | void *ust_listener_thread(void *arg) | |
1079 | { | |
1ea11eab | 1080 | struct sock_info *sock_info = arg; |
c0eedf81 | 1081 | int sock, ret, prev_connect_failed = 0, has_waited = 0; |
ff517991 | 1082 | long timeout; |
d9e99d10 | 1083 | |
9eb62b9c MD |
1084 | /* Restart trying to connect to the session daemon */ |
1085 | restart: | |
c0eedf81 MD |
1086 | if (prev_connect_failed) { |
1087 | /* Wait for sessiond availability with pipe */ | |
1088 | wait_for_sessiond(sock_info); | |
1089 | if (has_waited) { | |
1090 | has_waited = 0; | |
1091 | /* | |
1092 | * Sleep for 5 seconds before retrying after a | |
1093 | * sequence of failure / wait / failure. This | |
1094 | * deals with a killed or broken session daemon. | |
1095 | */ | |
1096 | sleep(5); | |
1097 | } | |
1098 | has_waited = 1; | |
1099 | prev_connect_failed = 0; | |
1100 | } | |
9eb62b9c | 1101 | |
1ea11eab | 1102 | if (sock_info->socket != -1) { |
e6973a89 | 1103 | ret = ustcomm_close_unix_sock(sock_info->socket); |
1ea11eab | 1104 | if (ret) { |
32ce8569 MD |
1105 | ERR("Error closing %s ust cmd socket", |
1106 | sock_info->name); | |
1ea11eab MD |
1107 | } |
1108 | sock_info->socket = -1; | |
1109 | } | |
32ce8569 MD |
1110 | if (sock_info->notify_socket != -1) { |
1111 | ret = ustcomm_close_unix_sock(sock_info->notify_socket); | |
1112 | if (ret) { | |
1113 | ERR("Error closing %s ust notify socket", | |
1114 | sock_info->name); | |
1115 | } | |
1116 | sock_info->notify_socket = -1; | |
1117 | } | |
46050b1a | 1118 | |
321f2351 MD |
1119 | /* |
1120 | * Register. We need to perform both connect and sending | |
1121 | * registration message before doing the next connect otherwise | |
1122 | * we may reach unix socket connect queue max limits and block | |
1123 | * on the 2nd connect while the session daemon is awaiting the | |
1124 | * first connect registration message. | |
1125 | */ | |
1126 | /* Connect cmd socket */ | |
1127 | ret = ustcomm_connect_unix_sock(sock_info->sock_path); | |
1128 | if (ret < 0) { | |
1129 | DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name); | |
1130 | prev_connect_failed = 1; | |
5b14aab3 | 1131 | |
3327ac33 | 1132 | if (ust_lock()) { |
321f2351 | 1133 | goto quit; |
32ce8569 | 1134 | } |
46050b1a | 1135 | |
e3426ddc | 1136 | /* |
321f2351 MD |
1137 | * If we cannot find the sessiond daemon, don't delay |
1138 | * constructor execution. | |
e3426ddc | 1139 | */ |
321f2351 MD |
1140 | ret = handle_register_done(sock_info); |
1141 | assert(!ret); | |
1142 | ust_unlock(); | |
1143 | goto restart; | |
27fe9f21 | 1144 | } |
321f2351 | 1145 | sock_info->socket = ret; |
27fe9f21 | 1146 | |
3327ac33 | 1147 | if (ust_lock()) { |
5b14aab3 MD |
1148 | goto quit; |
1149 | } | |
1150 | ||
46050b1a MD |
1151 | /* |
1152 | * Create only one root handle per listener thread for the whole | |
f59ed768 MD |
1153 | * process lifetime, so we ensure we get ID which is statically |
1154 | * assigned to the root handle. | |
46050b1a MD |
1155 | */ |
1156 | if (sock_info->root_handle == -1) { | |
1157 | ret = lttng_abi_create_root_handle(); | |
a51070bb | 1158 | if (ret < 0) { |
46050b1a | 1159 | ERR("Error creating root handle"); |
46050b1a MD |
1160 | goto quit; |
1161 | } | |
1162 | sock_info->root_handle = ret; | |
9eb62b9c | 1163 | } |
1ea11eab | 1164 | |
32ce8569 | 1165 | ret = register_to_sessiond(sock_info->socket, USTCTL_SOCKET_CMD); |
9eb62b9c | 1166 | if (ret < 0) { |
32ce8569 MD |
1167 | ERR("Error registering to %s ust cmd socket", |
1168 | sock_info->name); | |
c0eedf81 | 1169 | prev_connect_failed = 1; |
11ff9c7d MD |
1170 | /* |
1171 | * If we cannot register to the sessiond daemon, don't | |
1172 | * delay constructor execution. | |
1173 | */ | |
edaa1431 | 1174 | ret = handle_register_done(sock_info); |
11ff9c7d | 1175 | assert(!ret); |
17dfb34b | 1176 | ust_unlock(); |
9eb62b9c MD |
1177 | goto restart; |
1178 | } | |
321f2351 MD |
1179 | |
1180 | ust_unlock(); | |
1181 | ||
1182 | /* Connect notify socket */ | |
1183 | ret = ustcomm_connect_unix_sock(sock_info->sock_path); | |
1184 | if (ret < 0) { | |
1185 | DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name); | |
1186 | prev_connect_failed = 1; | |
1187 | ||
3327ac33 | 1188 | if (ust_lock()) { |
321f2351 MD |
1189 | goto quit; |
1190 | } | |
1191 | ||
1192 | /* | |
1193 | * If we cannot find the sessiond daemon, don't delay | |
1194 | * constructor execution. | |
1195 | */ | |
1196 | ret = handle_register_done(sock_info); | |
1197 | assert(!ret); | |
1198 | ust_unlock(); | |
1199 | goto restart; | |
1200 | } | |
1201 | sock_info->notify_socket = ret; | |
1202 | ||
1203 | timeout = get_notify_sock_timeout(); | |
1204 | if (timeout >= 0) { | |
1205 | /* | |
1206 | * Give at least 10ms to sessiond to reply to | |
1207 | * notifications. | |
1208 | */ | |
1209 | if (timeout < 10) | |
1210 | timeout = 10; | |
1211 | ret = ustcomm_setsockopt_rcv_timeout(sock_info->notify_socket, | |
1212 | timeout); | |
1213 | if (ret < 0) { | |
1214 | WARN("Error setting socket receive timeout"); | |
1215 | } | |
1216 | ret = ustcomm_setsockopt_snd_timeout(sock_info->notify_socket, | |
1217 | timeout); | |
1218 | if (ret < 0) { | |
1219 | WARN("Error setting socket send timeout"); | |
1220 | } | |
1221 | } else if (timeout < -1) { | |
1222 | WARN("Unsupported timeout value %ld", timeout); | |
1223 | } | |
1224 | ||
3327ac33 | 1225 | if (ust_lock()) { |
321f2351 MD |
1226 | goto quit; |
1227 | } | |
1228 | ||
32ce8569 MD |
1229 | ret = register_to_sessiond(sock_info->notify_socket, |
1230 | USTCTL_SOCKET_NOTIFY); | |
1231 | if (ret < 0) { | |
1232 | ERR("Error registering to %s ust notify socket", | |
1233 | sock_info->name); | |
1234 | prev_connect_failed = 1; | |
1235 | /* | |
1236 | * If we cannot register to the sessiond daemon, don't | |
1237 | * delay constructor execution. | |
1238 | */ | |
1239 | ret = handle_register_done(sock_info); | |
1240 | assert(!ret); | |
1241 | ust_unlock(); | |
1242 | goto restart; | |
1243 | } | |
1244 | sock = sock_info->socket; | |
1245 | ||
17dfb34b | 1246 | ust_unlock(); |
46050b1a | 1247 | |
d9e99d10 MD |
1248 | for (;;) { |
1249 | ssize_t len; | |
57773204 | 1250 | struct ustcomm_ust_msg lum; |
d9e99d10 | 1251 | |
57773204 | 1252 | len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum)); |
d9e99d10 MD |
1253 | switch (len) { |
1254 | case 0: /* orderly shutdown */ | |
7dd08bec | 1255 | DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info->name); |
3327ac33 | 1256 | if (ust_lock()) { |
d5e1fea6 MD |
1257 | goto quit; |
1258 | } | |
8236ba10 MD |
1259 | /* |
1260 | * Either sessiond has shutdown or refused us by closing the socket. | |
1261 | * In either case, we don't want to delay construction execution, | |
1262 | * and we need to wait before retry. | |
1263 | */ | |
1264 | prev_connect_failed = 1; | |
1265 | /* | |
1266 | * If we cannot register to the sessiond daemon, don't | |
1267 | * delay constructor execution. | |
1268 | */ | |
1269 | ret = handle_register_done(sock_info); | |
1270 | assert(!ret); | |
1271 | ust_unlock(); | |
d9e99d10 | 1272 | goto end; |
e7723462 | 1273 | case sizeof(lum): |
74d81a6c | 1274 | print_cmd(lum.cmd, lum.handle); |
11ff9c7d | 1275 | ret = handle_message(sock_info, sock, &lum); |
7bc53e94 | 1276 | if (ret) { |
11ff9c7d | 1277 | ERR("Error handling message for %s socket", sock_info->name); |
d9e99d10 MD |
1278 | } |
1279 | continue; | |
7bc53e94 MD |
1280 | default: |
1281 | if (len < 0) { | |
1282 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); | |
1283 | } else { | |
1284 | DBG("incorrect message size (%s socket): %zd", sock_info->name, len); | |
1285 | } | |
1286 | if (len == -ECONNRESET) { | |
1287 | DBG("%s remote end closed connection", sock_info->name); | |
d9e99d10 MD |
1288 | goto end; |
1289 | } | |
1290 | goto end; | |
d9e99d10 MD |
1291 | } |
1292 | ||
1293 | } | |
1294 | end: | |
3327ac33 | 1295 | if (ust_lock()) { |
d5e1fea6 MD |
1296 | goto quit; |
1297 | } | |
f59ed768 MD |
1298 | /* Cleanup socket handles before trying to reconnect */ |
1299 | lttng_ust_objd_table_owner_cleanup(sock_info); | |
1300 | ust_unlock(); | |
9eb62b9c | 1301 | goto restart; /* try to reconnect */ |
e33f3265 | 1302 | |
1ea11eab | 1303 | quit: |
e33f3265 | 1304 | ust_unlock(); |
3327ac33 MD |
1305 | |
1306 | pthread_mutex_lock(&ust_exit_mutex); | |
1307 | sock_info->thread_active = 0; | |
1308 | pthread_mutex_unlock(&ust_exit_mutex); | |
d9e99d10 MD |
1309 | return NULL; |
1310 | } | |
1311 | ||
2594a5b4 MD |
1312 | /* |
1313 | * Weak symbol to call when the ust malloc wrapper is not loaded. | |
1314 | */ | |
1315 | __attribute__((weak)) | |
1316 | void lttng_ust_malloc_wrapper_init(void) | |
1317 | { | |
1318 | } | |
1319 | ||
2691221a MD |
1320 | /* |
1321 | * sessiond monitoring thread: monitor presence of global and per-user | |
1322 | * sessiond by polling the application common named pipe. | |
1323 | */ | |
edaa1431 | 1324 | void __attribute__((constructor)) lttng_ust_init(void) |
2691221a | 1325 | { |
11ff9c7d | 1326 | struct timespec constructor_timeout; |
ae6a58bf | 1327 | sigset_t sig_all_blocked, orig_parent_mask; |
1879f67f | 1328 | pthread_attr_t thread_attr; |
cf12a773 | 1329 | int timeout_mode; |
2691221a MD |
1330 | int ret; |
1331 | ||
edaa1431 MD |
1332 | if (uatomic_xchg(&initialized, 1) == 1) |
1333 | return; | |
1334 | ||
eddd8d5d MD |
1335 | /* |
1336 | * Fixup interdependency between TLS fixup mutex (which happens | |
1337 | * to be the dynamic linker mutex) and ust_lock, taken within | |
1338 | * the ust lock. | |
1339 | */ | |
f645cfa7 | 1340 | lttng_fixup_ringbuffer_tls(); |
4158a15a | 1341 | lttng_fixup_vtid_tls(); |
a903623f | 1342 | lttng_fixup_nest_count_tls(); |
009745db | 1343 | lttng_fixup_procname_tls(); |
eddd8d5d | 1344 | |
edaa1431 MD |
1345 | /* |
1346 | * We want precise control over the order in which we construct | |
1347 | * our sub-libraries vs starting to receive commands from | |
1348 | * sessiond (otherwise leading to errors when trying to create | |
1349 | * sessiond before the init functions are completed). | |
1350 | */ | |
2691221a | 1351 | init_usterr(); |
edaa1431 | 1352 | init_tracepoint(); |
bd703713 | 1353 | lttng_ust_baddr_statedump_init(); |
7dd08bec MD |
1354 | lttng_ring_buffer_metadata_client_init(); |
1355 | lttng_ring_buffer_client_overwrite_init(); | |
34a91bdb | 1356 | lttng_ring_buffer_client_overwrite_rt_init(); |
7dd08bec | 1357 | lttng_ring_buffer_client_discard_init(); |
34a91bdb | 1358 | lttng_ring_buffer_client_discard_rt_init(); |
a0a3bef9 | 1359 | lttng_context_init(); |
2594a5b4 MD |
1360 | /* |
1361 | * Invoke ust malloc wrapper init before starting other threads. | |
1362 | */ | |
1363 | lttng_ust_malloc_wrapper_init(); | |
2691221a | 1364 | |
ff517991 | 1365 | timeout_mode = get_constructor_timeout(&constructor_timeout); |
11ff9c7d | 1366 | |
95259bd0 | 1367 | ret = sem_init(&constructor_wait, 0, 0); |
11ff9c7d MD |
1368 | assert(!ret); |
1369 | ||
8d20bf54 | 1370 | ret = setup_local_apps(); |
2691221a | 1371 | if (ret) { |
9ec6895c | 1372 | DBG("local apps setup returned %d", ret); |
2691221a | 1373 | } |
ae6a58bf WP |
1374 | |
1375 | /* A new thread created by pthread_create inherits the signal mask | |
1376 | * from the parent. To avoid any signal being received by the | |
1377 | * listener thread, we block all signals temporarily in the parent, | |
1378 | * while we create the listener thread. | |
1379 | */ | |
1380 | sigfillset(&sig_all_blocked); | |
1381 | ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask); | |
1382 | if (ret) { | |
d94d802c | 1383 | ERR("pthread_sigmask: %s", strerror(ret)); |
ae6a58bf WP |
1384 | } |
1385 | ||
1879f67f MG |
1386 | ret = pthread_attr_init(&thread_attr); |
1387 | if (ret) { | |
1388 | ERR("pthread_attr_init: %s", strerror(ret)); | |
1389 | } | |
1390 | ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); | |
1391 | if (ret) { | |
1392 | ERR("pthread_attr_setdetachstate: %s", strerror(ret)); | |
1393 | } | |
1394 | ||
c0bbbd5a | 1395 | pthread_mutex_lock(&ust_exit_mutex); |
1879f67f | 1396 | ret = pthread_create(&global_apps.ust_listener, &thread_attr, |
dde70ea0 | 1397 | ust_listener_thread, &global_apps); |
d94d802c MD |
1398 | if (ret) { |
1399 | ERR("pthread_create global: %s", strerror(ret)); | |
1400 | } | |
e33f3265 | 1401 | global_apps.thread_active = 1; |
c0bbbd5a | 1402 | pthread_mutex_unlock(&ust_exit_mutex); |
e33f3265 | 1403 | |
8d20bf54 | 1404 | if (local_apps.allowed) { |
c0bbbd5a | 1405 | pthread_mutex_lock(&ust_exit_mutex); |
1879f67f | 1406 | ret = pthread_create(&local_apps.ust_listener, &thread_attr, |
dde70ea0 | 1407 | ust_listener_thread, &local_apps); |
d94d802c MD |
1408 | if (ret) { |
1409 | ERR("pthread_create local: %s", strerror(ret)); | |
1410 | } | |
e33f3265 | 1411 | local_apps.thread_active = 1; |
c0bbbd5a | 1412 | pthread_mutex_unlock(&ust_exit_mutex); |
8d20bf54 MD |
1413 | } else { |
1414 | handle_register_done(&local_apps); | |
1415 | } | |
1879f67f MG |
1416 | ret = pthread_attr_destroy(&thread_attr); |
1417 | if (ret) { | |
1418 | ERR("pthread_attr_destroy: %s", strerror(ret)); | |
1419 | } | |
8d20bf54 | 1420 | |
ae6a58bf WP |
1421 | /* Restore original signal mask in parent */ |
1422 | ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL); | |
1423 | if (ret) { | |
d94d802c | 1424 | ERR("pthread_sigmask: %s", strerror(ret)); |
ae6a58bf WP |
1425 | } |
1426 | ||
cf12a773 MD |
1427 | switch (timeout_mode) { |
1428 | case 1: /* timeout wait */ | |
95259bd0 MD |
1429 | do { |
1430 | ret = sem_timedwait(&constructor_wait, | |
1431 | &constructor_timeout); | |
1432 | } while (ret < 0 && errno == EINTR); | |
cf12a773 | 1433 | if (ret < 0 && errno == ETIMEDOUT) { |
7dd08bec | 1434 | ERR("Timed out waiting for lttng-sessiond"); |
cf12a773 MD |
1435 | } else { |
1436 | assert(!ret); | |
1437 | } | |
1438 | break; | |
7b766b16 | 1439 | case -1:/* wait forever */ |
95259bd0 MD |
1440 | do { |
1441 | ret = sem_wait(&constructor_wait); | |
1442 | } while (ret < 0 && errno == EINTR); | |
11ff9c7d | 1443 | assert(!ret); |
cf12a773 | 1444 | break; |
7b766b16 | 1445 | case 0: /* no timeout */ |
cf12a773 | 1446 | break; |
11ff9c7d | 1447 | } |
2691221a MD |
1448 | } |
1449 | ||
17dfb34b MD |
1450 | static |
1451 | void lttng_ust_cleanup(int exiting) | |
1452 | { | |
efe0de09 | 1453 | cleanup_sock_info(&global_apps, exiting); |
17dfb34b | 1454 | if (local_apps.allowed) { |
efe0de09 | 1455 | cleanup_sock_info(&local_apps, exiting); |
17dfb34b | 1456 | } |
efe0de09 MD |
1457 | /* |
1458 | * The teardown in this function all affect data structures | |
1459 | * accessed under the UST lock by the listener thread. This | |
1460 | * lock, along with the lttng_ust_comm_should_quit flag, ensure | |
1461 | * that none of these threads are accessing this data at this | |
1462 | * point. | |
1463 | */ | |
17dfb34b | 1464 | lttng_ust_abi_exit(); |
003fedf4 | 1465 | lttng_ust_events_exit(); |
a0a3bef9 | 1466 | lttng_context_exit(); |
34a91bdb | 1467 | lttng_ring_buffer_client_discard_rt_exit(); |
7dd08bec | 1468 | lttng_ring_buffer_client_discard_exit(); |
34a91bdb | 1469 | lttng_ring_buffer_client_overwrite_rt_exit(); |
7dd08bec MD |
1470 | lttng_ring_buffer_client_overwrite_exit(); |
1471 | lttng_ring_buffer_metadata_client_exit(); | |
bd703713 | 1472 | lttng_ust_baddr_statedump_destroy(); |
17dfb34b MD |
1473 | exit_tracepoint(); |
1474 | if (!exiting) { | |
1475 | /* Reinitialize values for fork */ | |
1476 | sem_count = 2; | |
1477 | lttng_ust_comm_should_quit = 0; | |
1478 | initialized = 0; | |
1479 | } | |
1480 | } | |
1481 | ||
edaa1431 | 1482 | void __attribute__((destructor)) lttng_ust_exit(void) |
2691221a MD |
1483 | { |
1484 | int ret; | |
1485 | ||
9eb62b9c MD |
1486 | /* |
1487 | * Using pthread_cancel here because: | |
1488 | * A) we don't want to hang application teardown. | |
1489 | * B) the thread is not allocating any resource. | |
1490 | */ | |
1ea11eab MD |
1491 | |
1492 | /* | |
1493 | * Require the communication thread to quit. Synchronize with | |
1494 | * mutexes to ensure it is not in a mutex critical section when | |
1495 | * pthread_cancel is later called. | |
1496 | */ | |
3327ac33 | 1497 | ust_lock_nocheck(); |
1ea11eab | 1498 | lttng_ust_comm_should_quit = 1; |
3327ac33 | 1499 | ust_unlock(); |
1ea11eab | 1500 | |
3327ac33 | 1501 | pthread_mutex_lock(&ust_exit_mutex); |
f5f94532 | 1502 | /* cancel threads */ |
e33f3265 MD |
1503 | if (global_apps.thread_active) { |
1504 | ret = pthread_cancel(global_apps.ust_listener); | |
1505 | if (ret) { | |
1506 | ERR("Error cancelling global ust listener thread: %s", | |
1507 | strerror(ret)); | |
1508 | } else { | |
1509 | global_apps.thread_active = 0; | |
1510 | } | |
2691221a | 1511 | } |
e33f3265 | 1512 | if (local_apps.thread_active) { |
8d20bf54 MD |
1513 | ret = pthread_cancel(local_apps.ust_listener); |
1514 | if (ret) { | |
d94d802c MD |
1515 | ERR("Error cancelling local ust listener thread: %s", |
1516 | strerror(ret)); | |
e33f3265 MD |
1517 | } else { |
1518 | local_apps.thread_active = 0; | |
8d20bf54 | 1519 | } |
8d20bf54 | 1520 | } |
3327ac33 | 1521 | pthread_mutex_unlock(&ust_exit_mutex); |
e33f3265 | 1522 | |
efe0de09 MD |
1523 | /* |
1524 | * Do NOT join threads: use of sys_futex makes it impossible to | |
1525 | * join the threads without using async-cancel, but async-cancel | |
1526 | * is delivered by a signal, which could hit the target thread | |
1527 | * anywhere in its code path, including while the ust_lock() is | |
1528 | * held, causing a deadlock for the other thread. Let the OS | |
1529 | * cleanup the threads if there are stalled in a syscall. | |
1530 | */ | |
17dfb34b | 1531 | lttng_ust_cleanup(1); |
2691221a | 1532 | } |
e822f505 MD |
1533 | |
1534 | /* | |
1535 | * We exclude the worker threads across fork and clone (except | |
1536 | * CLONE_VM), because these system calls only keep the forking thread | |
1537 | * running in the child. Therefore, we don't want to call fork or clone | |
1538 | * in the middle of an tracepoint or ust tracing state modification. | |
1539 | * Holding this mutex protects these structures across fork and clone. | |
1540 | */ | |
b728d87e | 1541 | void ust_before_fork(sigset_t *save_sigset) |
e822f505 MD |
1542 | { |
1543 | /* | |
1544 | * Disable signals. This is to avoid that the child intervenes | |
1545 | * before it is properly setup for tracing. It is safer to | |
1546 | * disable all signals, because then we know we are not breaking | |
1547 | * anything by restoring the original mask. | |
1548 | */ | |
1549 | sigset_t all_sigs; | |
1550 | int ret; | |
1551 | ||
8c90a710 | 1552 | if (URCU_TLS(lttng_ust_nest_count)) |
e8508a49 | 1553 | return; |
e822f505 MD |
1554 | /* Disable signals */ |
1555 | sigfillset(&all_sigs); | |
b728d87e | 1556 | ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset); |
e822f505 MD |
1557 | if (ret == -1) { |
1558 | PERROR("sigprocmask"); | |
1559 | } | |
3327ac33 | 1560 | ust_lock_nocheck(); |
e822f505 MD |
1561 | rcu_bp_before_fork(); |
1562 | } | |
1563 | ||
b728d87e | 1564 | static void ust_after_fork_common(sigset_t *restore_sigset) |
e822f505 MD |
1565 | { |
1566 | int ret; | |
1567 | ||
17dfb34b MD |
1568 | DBG("process %d", getpid()); |
1569 | ust_unlock(); | |
e822f505 | 1570 | /* Restore signals */ |
23c8854a | 1571 | ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL); |
e822f505 MD |
1572 | if (ret == -1) { |
1573 | PERROR("sigprocmask"); | |
1574 | } | |
1575 | } | |
1576 | ||
b728d87e | 1577 | void ust_after_fork_parent(sigset_t *restore_sigset) |
e822f505 | 1578 | { |
8c90a710 | 1579 | if (URCU_TLS(lttng_ust_nest_count)) |
e8508a49 | 1580 | return; |
17dfb34b | 1581 | DBG("process %d", getpid()); |
e822f505 MD |
1582 | rcu_bp_after_fork_parent(); |
1583 | /* Release mutexes and reenable signals */ | |
b728d87e | 1584 | ust_after_fork_common(restore_sigset); |
e822f505 MD |
1585 | } |
1586 | ||
17dfb34b MD |
1587 | /* |
1588 | * After fork, in the child, we need to cleanup all the leftover state, | |
1589 | * except the worker thread which already magically disappeared thanks | |
1590 | * to the weird Linux fork semantics. After tyding up, we call | |
1591 | * lttng_ust_init() again to start over as a new PID. | |
1592 | * | |
1593 | * This is meant for forks() that have tracing in the child between the | |
1594 | * fork and following exec call (if there is any). | |
1595 | */ | |
b728d87e | 1596 | void ust_after_fork_child(sigset_t *restore_sigset) |
e822f505 | 1597 | { |
8c90a710 | 1598 | if (URCU_TLS(lttng_ust_nest_count)) |
e8508a49 | 1599 | return; |
17dfb34b | 1600 | DBG("process %d", getpid()); |
e822f505 MD |
1601 | /* Release urcu mutexes */ |
1602 | rcu_bp_after_fork_child(); | |
17dfb34b | 1603 | lttng_ust_cleanup(0); |
a93bfc45 | 1604 | lttng_context_vtid_reset(); |
e822f505 | 1605 | /* Release mutexes and reenable signals */ |
b728d87e | 1606 | ust_after_fork_common(restore_sigset); |
318dfea9 | 1607 | lttng_ust_init(); |
e822f505 | 1608 | } |
95c25348 | 1609 | |
246be17e | 1610 | void lttng_ust_sockinfo_session_enabled(void *owner) |
95c25348 PW |
1611 | { |
1612 | struct sock_info *sock_info = owner; | |
37dddb65 | 1613 | sock_info->statedump_pending = 1; |
95c25348 | 1614 | } |