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> |
44c72f10 MD |
46 | #include <ust-comm.h> |
47 | #include <usterr-signal-safe.h> | |
cd54f6d9 | 48 | #include <helper.h> |
44c72f10 | 49 | #include "tracepoint-internal.h" |
7dd08bec | 50 | #include "lttng-tracer-core.h" |
08114193 | 51 | #include "compat.h" |
f645cfa7 | 52 | #include "../libringbuffer/tlsfixup.h" |
edaa1431 MD |
53 | |
54 | /* | |
55 | * Has lttng ust comm constructor been called ? | |
56 | */ | |
57 | static int initialized; | |
58 | ||
1ea11eab | 59 | /* |
17dfb34b MD |
60 | * The ust_lock/ust_unlock lock is used as a communication thread mutex. |
61 | * Held when handling a command, also held by fork() to deal with | |
62 | * removal of threads, and by exit path. | |
1ea11eab | 63 | */ |
1ea11eab MD |
64 | |
65 | /* Should the ust comm thread quit ? */ | |
66 | static int lttng_ust_comm_should_quit; | |
67 | ||
11ff9c7d MD |
68 | /* |
69 | * Wait for either of these before continuing to the main | |
70 | * program: | |
71 | * - the register_done message from sessiond daemon | |
72 | * (will let the sessiond daemon enable sessions before main | |
73 | * starts.) | |
74 | * - sessiond daemon is not reachable. | |
75 | * - timeout (ensuring applications are resilient to session | |
76 | * daemon problems). | |
77 | */ | |
78 | static sem_t constructor_wait; | |
950aab0c MD |
79 | /* |
80 | * Doing this for both the global and local sessiond. | |
81 | */ | |
95259bd0 | 82 | static int sem_count = { 2 }; |
11ff9c7d | 83 | |
e8508a49 MD |
84 | /* |
85 | * Counting nesting within lttng-ust. Used to ensure that calling fork() | |
86 | * from liblttng-ust does not execute the pre/post fork handlers. | |
87 | */ | |
88 | static int __thread lttng_ust_nest_count; | |
89 | ||
1ea11eab MD |
90 | /* |
91 | * Info about socket and associated listener thread. | |
92 | */ | |
93 | struct sock_info { | |
11ff9c7d | 94 | const char *name; |
1ea11eab | 95 | pthread_t ust_listener; /* listener thread */ |
46050b1a | 96 | int root_handle; |
8d20bf54 MD |
97 | int constructor_sem_posted; |
98 | int allowed; | |
44e073f5 | 99 | int global; |
e33f3265 | 100 | int thread_active; |
7fc90dca MD |
101 | |
102 | char sock_path[PATH_MAX]; | |
103 | int socket; | |
104 | ||
105 | char wait_shm_path[PATH_MAX]; | |
106 | char *wait_shm_mmap; | |
1ea11eab | 107 | }; |
2691221a MD |
108 | |
109 | /* Socket from app (connect) to session daemon (listen) for communication */ | |
1ea11eab | 110 | struct sock_info global_apps = { |
11ff9c7d | 111 | .name = "global", |
44e073f5 | 112 | .global = 1, |
7fc90dca | 113 | |
46050b1a | 114 | .root_handle = -1, |
8d20bf54 | 115 | .allowed = 1, |
e33f3265 | 116 | .thread_active = 0, |
7fc90dca MD |
117 | |
118 | .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK, | |
119 | .socket = -1, | |
120 | ||
121 | .wait_shm_path = DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH, | |
1ea11eab | 122 | }; |
2691221a MD |
123 | |
124 | /* TODO: allow global_apps_sock_path override */ | |
125 | ||
1ea11eab | 126 | struct sock_info local_apps = { |
11ff9c7d | 127 | .name = "local", |
44e073f5 | 128 | .global = 0, |
46050b1a | 129 | .root_handle = -1, |
8d20bf54 | 130 | .allowed = 0, /* Check setuid bit first */ |
e33f3265 | 131 | .thread_active = 0, |
7fc90dca MD |
132 | |
133 | .socket = -1, | |
1ea11eab | 134 | }; |
2691221a | 135 | |
37ed587a MD |
136 | static int wait_poll_fallback; |
137 | ||
74d81a6c MD |
138 | static const char *cmd_name_mapping[] = { |
139 | [ LTTNG_UST_RELEASE ] = "Release", | |
140 | [ LTTNG_UST_SESSION ] = "Create Session", | |
141 | [ LTTNG_UST_TRACER_VERSION ] = "Get Tracer Version", | |
142 | ||
143 | [ LTTNG_UST_TRACEPOINT_LIST ] = "Create Tracepoint List", | |
144 | [ LTTNG_UST_WAIT_QUIESCENT ] = "Wait for Quiescent State", | |
145 | [ LTTNG_UST_REGISTER_DONE ] = "Registration Done", | |
146 | [ LTTNG_UST_TRACEPOINT_FIELD_LIST ] = "Create Tracepoint Field List", | |
147 | ||
148 | /* Session FD commands */ | |
149 | [ LTTNG_UST_CHANNEL ] = "Create Channel", | |
150 | [ LTTNG_UST_SESSION_START ] = "Start Session", | |
151 | [ LTTNG_UST_SESSION_STOP ] = "Stop Session", | |
152 | ||
153 | /* Channel FD commands */ | |
154 | [ LTTNG_UST_STREAM ] = "Create Stream", | |
155 | [ LTTNG_UST_EVENT ] = "Create Event", | |
156 | ||
157 | /* Event and Channel FD commands */ | |
158 | [ LTTNG_UST_CONTEXT ] = "Create Context", | |
159 | [ LTTNG_UST_FLUSH_BUFFER ] = "Flush Buffer", | |
160 | ||
161 | /* Event, Channel and Session commands */ | |
162 | [ LTTNG_UST_ENABLE ] = "Enable", | |
163 | [ LTTNG_UST_DISABLE ] = "Disable", | |
164 | ||
165 | /* Tracepoint list commands */ | |
166 | [ LTTNG_UST_TRACEPOINT_LIST_GET ] = "List Next Tracepoint", | |
167 | [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET ] = "List Next Tracepoint Field", | |
168 | ||
169 | /* Event FD commands */ | |
170 | [ LTTNG_UST_FILTER ] = "Create Filter", | |
171 | }; | |
172 | ||
7dd08bec MD |
173 | extern void lttng_ring_buffer_client_overwrite_init(void); |
174 | extern void lttng_ring_buffer_client_discard_init(void); | |
175 | extern void lttng_ring_buffer_metadata_client_init(void); | |
176 | extern void lttng_ring_buffer_client_overwrite_exit(void); | |
177 | extern void lttng_ring_buffer_client_discard_exit(void); | |
178 | extern void lttng_ring_buffer_metadata_client_exit(void); | |
edaa1431 | 179 | |
a903623f MD |
180 | /* |
181 | * Force a read (imply TLS fixup for dlopen) of TLS variables. | |
182 | */ | |
183 | static | |
184 | void lttng_fixup_nest_count_tls(void) | |
185 | { | |
186 | asm volatile ("" : : "m" (lttng_ust_nest_count)); | |
187 | } | |
188 | ||
74d81a6c MD |
189 | static |
190 | void print_cmd(int cmd, int handle) | |
191 | { | |
192 | const char *cmd_name = "Unknown"; | |
193 | ||
194 | if (cmd_name_mapping[cmd]) { | |
195 | cmd_name = cmd_name_mapping[cmd]; | |
196 | } | |
197 | DBG("Message Received \"%s\", Handle \"%s\" (%d)", cmd_name, | |
198 | lttng_ust_obj_get_name(handle), handle); | |
199 | } | |
200 | ||
2691221a | 201 | static |
8d20bf54 | 202 | int setup_local_apps(void) |
2691221a MD |
203 | { |
204 | const char *home_dir; | |
7fc90dca | 205 | uid_t uid; |
2691221a | 206 | |
7fc90dca | 207 | uid = getuid(); |
8d20bf54 MD |
208 | /* |
209 | * Disallow per-user tracing for setuid binaries. | |
210 | */ | |
7fc90dca | 211 | if (uid != geteuid()) { |
9ec6895c | 212 | assert(local_apps.allowed == 0); |
d0a1ae63 | 213 | return 0; |
8d20bf54 | 214 | } |
2691221a | 215 | home_dir = (const char *) getenv("HOME"); |
9ec6895c MD |
216 | if (!home_dir) { |
217 | WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing."); | |
218 | assert(local_apps.allowed == 0); | |
2691221a | 219 | return -ENOENT; |
9ec6895c MD |
220 | } |
221 | local_apps.allowed = 1; | |
1ea11eab | 222 | snprintf(local_apps.sock_path, PATH_MAX, |
2691221a | 223 | DEFAULT_HOME_APPS_UNIX_SOCK, home_dir); |
7fc90dca MD |
224 | snprintf(local_apps.wait_shm_path, PATH_MAX, |
225 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, uid); | |
2691221a MD |
226 | return 0; |
227 | } | |
228 | ||
229 | static | |
230 | int register_app_to_sessiond(int socket) | |
231 | { | |
232 | ssize_t ret; | |
233 | struct { | |
e44418f3 MD |
234 | uint32_t major; |
235 | uint32_t minor; | |
2691221a | 236 | pid_t pid; |
5c33bde8 | 237 | pid_t ppid; |
2691221a | 238 | uid_t uid; |
83610856 | 239 | gid_t gid; |
c117fb1b | 240 | uint32_t bits_per_long; |
2629549e | 241 | char name[16]; /* process name */ |
2691221a MD |
242 | } reg_msg; |
243 | ||
e44418f3 MD |
244 | reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR; |
245 | reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR; | |
2691221a | 246 | reg_msg.pid = getpid(); |
5c33bde8 | 247 | reg_msg.ppid = getppid(); |
2691221a | 248 | reg_msg.uid = getuid(); |
83610856 | 249 | reg_msg.gid = getgid(); |
c117fb1b | 250 | reg_msg.bits_per_long = CAA_BITS_PER_LONG; |
08114193 | 251 | lttng_ust_getprocname(reg_msg.name); |
2691221a | 252 | |
57773204 | 253 | ret = ustcomm_send_unix_sock(socket, ®_msg, sizeof(reg_msg)); |
2691221a MD |
254 | if (ret >= 0 && ret != sizeof(reg_msg)) |
255 | return -EIO; | |
256 | return ret; | |
257 | } | |
258 | ||
d9e99d10 | 259 | static |
57773204 | 260 | int send_reply(int sock, struct ustcomm_ust_reply *lur) |
d9e99d10 | 261 | { |
9eb62b9c | 262 | ssize_t len; |
d3a492d1 | 263 | |
57773204 | 264 | len = ustcomm_send_unix_sock(sock, lur, sizeof(*lur)); |
d3a492d1 | 265 | switch (len) { |
a4be8962 | 266 | case sizeof(*lur): |
d3a492d1 MD |
267 | DBG("message successfully sent"); |
268 | return 0; | |
7bc53e94 MD |
269 | default: |
270 | if (len == -ECONNRESET) { | |
271 | DBG("remote end closed connection"); | |
d3a492d1 MD |
272 | return 0; |
273 | } | |
7bc53e94 MD |
274 | if (len < 0) |
275 | return len; | |
276 | DBG("incorrect message size: %zd", len); | |
277 | return -EINVAL; | |
d3a492d1 MD |
278 | } |
279 | } | |
280 | ||
281 | static | |
edaa1431 | 282 | int handle_register_done(struct sock_info *sock_info) |
11ff9c7d MD |
283 | { |
284 | int ret; | |
285 | ||
edaa1431 MD |
286 | if (sock_info->constructor_sem_posted) |
287 | return 0; | |
288 | sock_info->constructor_sem_posted = 1; | |
56cd7e2f MD |
289 | if (uatomic_read(&sem_count) <= 0) { |
290 | return 0; | |
291 | } | |
95259bd0 MD |
292 | ret = uatomic_add_return(&sem_count, -1); |
293 | if (ret == 0) { | |
294 | ret = sem_post(&constructor_wait); | |
295 | assert(!ret); | |
296 | } | |
11ff9c7d MD |
297 | return 0; |
298 | } | |
299 | ||
300 | static | |
301 | int handle_message(struct sock_info *sock_info, | |
57773204 | 302 | int sock, struct ustcomm_ust_msg *lum) |
d3a492d1 | 303 | { |
1ea11eab | 304 | int ret = 0; |
b61ce3b2 | 305 | const struct lttng_ust_objd_ops *ops; |
57773204 | 306 | struct ustcomm_ust_reply lur; |
ef9ff354 | 307 | union ust_args args; |
40003310 | 308 | ssize_t len; |
1ea11eab | 309 | |
17dfb34b | 310 | ust_lock(); |
1ea11eab | 311 | |
46050b1a MD |
312 | memset(&lur, 0, sizeof(lur)); |
313 | ||
1ea11eab | 314 | if (lttng_ust_comm_should_quit) { |
74d81a6c | 315 | ret = -LTTNG_UST_ERR_EXITING; |
1ea11eab MD |
316 | goto end; |
317 | } | |
9eb62b9c | 318 | |
46050b1a MD |
319 | ops = objd_ops(lum->handle); |
320 | if (!ops) { | |
321 | ret = -ENOENT; | |
322 | goto end; | |
1ea11eab | 323 | } |
46050b1a MD |
324 | |
325 | switch (lum->cmd) { | |
11ff9c7d MD |
326 | case LTTNG_UST_REGISTER_DONE: |
327 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
edaa1431 | 328 | ret = handle_register_done(sock_info); |
11ff9c7d MD |
329 | else |
330 | ret = -EINVAL; | |
331 | break; | |
46050b1a MD |
332 | case LTTNG_UST_RELEASE: |
333 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
334 | ret = -EPERM; | |
335 | else | |
d4419b81 | 336 | ret = lttng_ust_objd_unref(lum->handle); |
d9e99d10 | 337 | break; |
2d78951a MD |
338 | case LTTNG_UST_FILTER: |
339 | { | |
340 | /* Receive filter data */ | |
f488575f | 341 | struct lttng_ust_filter_bytecode_node *bytecode; |
2d78951a | 342 | |
cd54f6d9 | 343 | if (lum->u.filter.data_size > FILTER_BYTECODE_MAX_LEN) { |
7bc53e94 | 344 | ERR("Filter data size is too large: %u bytes", |
2d78951a MD |
345 | lum->u.filter.data_size); |
346 | ret = -EINVAL; | |
347 | goto error; | |
348 | } | |
2734ca65 | 349 | |
885b1dfd | 350 | if (lum->u.filter.reloc_offset > lum->u.filter.data_size) { |
7bc53e94 | 351 | ERR("Filter reloc offset %u is not within data", |
2734ca65 CB |
352 | lum->u.filter.reloc_offset); |
353 | ret = -EINVAL; | |
354 | goto error; | |
355 | } | |
356 | ||
cd54f6d9 MD |
357 | bytecode = zmalloc(sizeof(*bytecode) + lum->u.filter.data_size); |
358 | if (!bytecode) { | |
359 | ret = -ENOMEM; | |
360 | goto error; | |
361 | } | |
f488575f | 362 | len = ustcomm_recv_unix_sock(sock, bytecode->bc.data, |
2d78951a MD |
363 | lum->u.filter.data_size); |
364 | switch (len) { | |
365 | case 0: /* orderly shutdown */ | |
366 | ret = 0; | |
cd54f6d9 | 367 | free(bytecode); |
2d78951a | 368 | goto error; |
2d78951a MD |
369 | default: |
370 | if (len == lum->u.filter.data_size) { | |
7bc53e94 | 371 | DBG("filter data received"); |
2d78951a | 372 | break; |
7bc53e94 MD |
373 | } else if (len < 0) { |
374 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); | |
375 | if (len == -ECONNRESET) { | |
376 | ERR("%s remote end closed connection", sock_info->name); | |
377 | ret = len; | |
378 | free(bytecode); | |
379 | goto error; | |
380 | } | |
381 | ret = len; | |
382 | goto end; | |
2d78951a | 383 | } else { |
7bc53e94 | 384 | DBG("incorrect filter data message size: %zd", len); |
2d78951a | 385 | ret = -EINVAL; |
cd54f6d9 | 386 | free(bytecode); |
2d78951a MD |
387 | goto end; |
388 | } | |
389 | } | |
f488575f MD |
390 | bytecode->bc.len = lum->u.filter.data_size; |
391 | bytecode->bc.reloc_offset = lum->u.filter.reloc_offset; | |
3f6fd224 | 392 | bytecode->bc.seqnum = lum->u.filter.seqnum; |
cd54f6d9 | 393 | if (ops->cmd) { |
2d78951a | 394 | ret = ops->cmd(lum->handle, lum->cmd, |
cd54f6d9 | 395 | (unsigned long) bytecode, |
f59ed768 | 396 | &args, sock_info); |
cd54f6d9 MD |
397 | if (ret) { |
398 | free(bytecode); | |
399 | } | |
400 | /* don't free bytecode if everything went fine. */ | |
401 | } else { | |
2d78951a | 402 | ret = -ENOSYS; |
cd54f6d9 MD |
403 | free(bytecode); |
404 | } | |
2d78951a MD |
405 | break; |
406 | } | |
74d81a6c MD |
407 | case LTTNG_UST_CHANNEL: |
408 | { | |
409 | void *chan_data; | |
410 | ||
411 | len = ustcomm_recv_channel_from_sessiond(sock, | |
412 | &chan_data, lum->u.channel.len); | |
413 | switch (len) { | |
414 | case 0: /* orderly shutdown */ | |
415 | ret = 0; | |
416 | goto error; | |
417 | default: | |
418 | if (len == lum->u.channel.len) { | |
419 | DBG("channel data received"); | |
420 | break; | |
421 | } else if (len < 0) { | |
422 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); | |
423 | if (len == -ECONNRESET) { | |
424 | ERR("%s remote end closed connection", sock_info->name); | |
425 | ret = len; | |
426 | goto error; | |
427 | } | |
428 | ret = len; | |
429 | goto end; | |
430 | } else { | |
431 | DBG("incorrect channel data message size: %zd", len); | |
432 | ret = -EINVAL; | |
433 | goto end; | |
434 | } | |
435 | } | |
436 | args.channel.chan_data = chan_data; | |
437 | if (ops->cmd) | |
438 | ret = ops->cmd(lum->handle, lum->cmd, | |
439 | (unsigned long) &lum->u, | |
440 | &args, sock_info); | |
441 | else | |
442 | ret = -ENOSYS; | |
443 | break; | |
444 | } | |
445 | case LTTNG_UST_STREAM: | |
446 | { | |
447 | /* Receive shm_fd, wakeup_fd */ | |
448 | ret = ustcomm_recv_stream_from_sessiond(sock, | |
449 | &lum->u.stream.len, | |
450 | &args.stream.shm_fd, | |
451 | &args.stream.wakeup_fd); | |
452 | if (ret) { | |
453 | goto end; | |
454 | } | |
455 | if (ops->cmd) | |
456 | ret = ops->cmd(lum->handle, lum->cmd, | |
457 | (unsigned long) &lum->u, | |
458 | &args, sock_info); | |
459 | else | |
460 | ret = -ENOSYS; | |
461 | break; | |
462 | } | |
d9e99d10 | 463 | default: |
46050b1a MD |
464 | if (ops->cmd) |
465 | ret = ops->cmd(lum->handle, lum->cmd, | |
ef9ff354 | 466 | (unsigned long) &lum->u, |
f59ed768 | 467 | &args, sock_info); |
46050b1a MD |
468 | else |
469 | ret = -ENOSYS; | |
470 | break; | |
d9e99d10 | 471 | } |
46050b1a | 472 | |
1ea11eab | 473 | end: |
46050b1a MD |
474 | lur.handle = lum->handle; |
475 | lur.cmd = lum->cmd; | |
476 | lur.ret_val = ret; | |
477 | if (ret >= 0) { | |
7bc53e94 | 478 | lur.ret_code = LTTNG_UST_OK; |
46050b1a | 479 | } else { |
7bc53e94 MD |
480 | /* |
481 | * Use -LTTNG_UST_ERR as wildcard for UST internal | |
482 | * error that are not caused by the transport, except if | |
483 | * we already have a more precise error message to | |
484 | * report. | |
485 | */ | |
64b2564e DG |
486 | if (ret > -LTTNG_UST_ERR) { |
487 | /* Translate code to UST error. */ | |
488 | switch (ret) { | |
489 | case -EEXIST: | |
490 | lur.ret_code = -LTTNG_UST_ERR_EXIST; | |
491 | break; | |
492 | case -EINVAL: | |
493 | lur.ret_code = -LTTNG_UST_ERR_INVAL; | |
494 | break; | |
495 | case -ENOENT: | |
496 | lur.ret_code = -LTTNG_UST_ERR_NOENT; | |
497 | break; | |
498 | case -EPERM: | |
499 | lur.ret_code = -LTTNG_UST_ERR_PERM; | |
500 | break; | |
501 | case -ENOSYS: | |
502 | lur.ret_code = -LTTNG_UST_ERR_NOSYS; | |
503 | break; | |
504 | default: | |
505 | lur.ret_code = -LTTNG_UST_ERR; | |
506 | break; | |
507 | } | |
508 | } else { | |
7bc53e94 | 509 | lur.ret_code = ret; |
64b2564e | 510 | } |
46050b1a | 511 | } |
e6ea14c5 MD |
512 | if (ret >= 0) { |
513 | switch (lum->cmd) { | |
e6ea14c5 MD |
514 | case LTTNG_UST_TRACER_VERSION: |
515 | lur.u.version = lum->u.version; | |
516 | break; | |
517 | case LTTNG_UST_TRACEPOINT_LIST_GET: | |
518 | memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint)); | |
519 | break; | |
520 | } | |
381c0f1e | 521 | } |
74d81a6c | 522 | DBG("Return value: %d", lur.ret_val); |
46050b1a | 523 | ret = send_reply(sock, &lur); |
193183fb | 524 | if (ret < 0) { |
7bc53e94 | 525 | DBG("error sending reply"); |
193183fb MD |
526 | goto error; |
527 | } | |
46050b1a | 528 | |
40003310 MD |
529 | /* |
530 | * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field | |
531 | * after the reply. | |
532 | */ | |
7bc53e94 | 533 | if (lur.ret_code == LTTNG_UST_OK) { |
40003310 MD |
534 | switch (lum->cmd) { |
535 | case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET: | |
536 | len = ustcomm_send_unix_sock(sock, | |
537 | &args.field_list.entry, | |
538 | sizeof(args.field_list.entry)); | |
7bc53e94 MD |
539 | if (len < 0) { |
540 | ret = len; | |
541 | goto error; | |
542 | } | |
40003310 | 543 | if (len != sizeof(args.field_list.entry)) { |
7bc53e94 | 544 | ret = -EINVAL; |
40003310 MD |
545 | goto error; |
546 | } | |
547 | } | |
548 | } | |
ef9ff354 | 549 | |
381c0f1e | 550 | error: |
17dfb34b | 551 | ust_unlock(); |
1ea11eab | 552 | return ret; |
d9e99d10 MD |
553 | } |
554 | ||
46050b1a | 555 | static |
efe0de09 | 556 | void cleanup_sock_info(struct sock_info *sock_info, int exiting) |
46050b1a MD |
557 | { |
558 | int ret; | |
559 | ||
560 | if (sock_info->socket != -1) { | |
e6973a89 | 561 | ret = ustcomm_close_unix_sock(sock_info->socket); |
46050b1a | 562 | if (ret) { |
7fc90dca | 563 | ERR("Error closing apps socket"); |
46050b1a MD |
564 | } |
565 | sock_info->socket = -1; | |
566 | } | |
567 | if (sock_info->root_handle != -1) { | |
d4419b81 | 568 | ret = lttng_ust_objd_unref(sock_info->root_handle); |
46050b1a MD |
569 | if (ret) { |
570 | ERR("Error unref root handle"); | |
571 | } | |
572 | sock_info->root_handle = -1; | |
573 | } | |
318dfea9 | 574 | sock_info->constructor_sem_posted = 0; |
efe0de09 MD |
575 | /* |
576 | * wait_shm_mmap is used by listener threads outside of the | |
577 | * ust lock, so we cannot tear it down ourselves, because we | |
578 | * cannot join on these threads. Leave this task to the OS | |
579 | * process exit. | |
580 | */ | |
581 | if (!exiting && sock_info->wait_shm_mmap) { | |
7fc90dca MD |
582 | ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE)); |
583 | if (ret) { | |
584 | ERR("Error unmapping wait shm"); | |
585 | } | |
586 | sock_info->wait_shm_mmap = NULL; | |
587 | } | |
588 | } | |
589 | ||
58d4b2a2 | 590 | /* |
33bbeb90 MD |
591 | * Using fork to set umask in the child process (not multi-thread safe). |
592 | * We deal with the shm_open vs ftruncate race (happening when the | |
593 | * sessiond owns the shm and does not let everybody modify it, to ensure | |
594 | * safety against shm_unlink) by simply letting the mmap fail and | |
595 | * retrying after a few seconds. | |
596 | * For global shm, everybody has rw access to it until the sessiond | |
597 | * starts. | |
58d4b2a2 | 598 | */ |
7fc90dca | 599 | static |
58d4b2a2 | 600 | int get_wait_shm(struct sock_info *sock_info, size_t mmap_size) |
7fc90dca | 601 | { |
7fc90dca | 602 | int wait_shm_fd, ret; |
58d4b2a2 | 603 | pid_t pid; |
44e073f5 | 604 | |
58d4b2a2 | 605 | /* |
33bbeb90 | 606 | * Try to open read-only. |
58d4b2a2 | 607 | */ |
33bbeb90 | 608 | wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0); |
58d4b2a2 MD |
609 | if (wait_shm_fd >= 0) { |
610 | goto end; | |
611 | } else if (wait_shm_fd < 0 && errno != ENOENT) { | |
612 | /* | |
33bbeb90 MD |
613 | * Real-only open did not work, and it's not because the |
614 | * entry was not present. It's a failure that prohibits | |
615 | * using shm. | |
58d4b2a2 | 616 | */ |
7fc90dca | 617 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
58d4b2a2 | 618 | goto end; |
7fc90dca MD |
619 | } |
620 | /* | |
58d4b2a2 MD |
621 | * If the open failed because the file did not exist, try |
622 | * creating it ourself. | |
7fc90dca | 623 | */ |
e8508a49 | 624 | lttng_ust_nest_count++; |
58d4b2a2 | 625 | pid = fork(); |
e8508a49 | 626 | lttng_ust_nest_count--; |
58d4b2a2 MD |
627 | if (pid > 0) { |
628 | int status; | |
629 | ||
630 | /* | |
631 | * Parent: wait for child to return, in which case the | |
632 | * shared memory map will have been created. | |
633 | */ | |
634 | pid = wait(&status); | |
b7d3cb32 | 635 | if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
58d4b2a2 MD |
636 | wait_shm_fd = -1; |
637 | goto end; | |
7fc90dca | 638 | } |
58d4b2a2 MD |
639 | /* |
640 | * Try to open read-only again after creation. | |
641 | */ | |
33bbeb90 | 642 | wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0); |
58d4b2a2 MD |
643 | if (wait_shm_fd < 0) { |
644 | /* | |
645 | * Real-only open did not work. It's a failure | |
646 | * that prohibits using shm. | |
647 | */ | |
648 | ERR("Error opening shm %s", sock_info->wait_shm_path); | |
649 | goto end; | |
650 | } | |
651 | goto end; | |
652 | } else if (pid == 0) { | |
653 | int create_mode; | |
654 | ||
655 | /* Child */ | |
33bbeb90 | 656 | create_mode = S_IRUSR | S_IWUSR | S_IRGRP; |
58d4b2a2 | 657 | if (sock_info->global) |
33bbeb90 | 658 | create_mode |= S_IROTH | S_IWGRP | S_IWOTH; |
58d4b2a2 MD |
659 | /* |
660 | * We're alone in a child process, so we can modify the | |
661 | * process-wide umask. | |
662 | */ | |
33bbeb90 | 663 | umask(~create_mode); |
58d4b2a2 | 664 | /* |
33bbeb90 MD |
665 | * Try creating shm (or get rw access). |
666 | * We don't do an exclusive open, because we allow other | |
667 | * processes to create+ftruncate it concurrently. | |
58d4b2a2 MD |
668 | */ |
669 | wait_shm_fd = shm_open(sock_info->wait_shm_path, | |
670 | O_RDWR | O_CREAT, create_mode); | |
671 | if (wait_shm_fd >= 0) { | |
672 | ret = ftruncate(wait_shm_fd, mmap_size); | |
673 | if (ret) { | |
674 | PERROR("ftruncate"); | |
b0c1425d | 675 | _exit(EXIT_FAILURE); |
58d4b2a2 | 676 | } |
b0c1425d | 677 | _exit(EXIT_SUCCESS); |
58d4b2a2 | 678 | } |
33bbeb90 MD |
679 | /* |
680 | * For local shm, we need to have rw access to accept | |
681 | * opening it: this means the local sessiond will be | |
682 | * able to wake us up. For global shm, we open it even | |
683 | * if rw access is not granted, because the root.root | |
684 | * sessiond will be able to override all rights and wake | |
685 | * us up. | |
686 | */ | |
687 | if (!sock_info->global && errno != EACCES) { | |
58d4b2a2 | 688 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
5d3bc5ed | 689 | _exit(EXIT_FAILURE); |
58d4b2a2 MD |
690 | } |
691 | /* | |
33bbeb90 MD |
692 | * The shm exists, but we cannot open it RW. Report |
693 | * success. | |
58d4b2a2 | 694 | */ |
5d3bc5ed | 695 | _exit(EXIT_SUCCESS); |
58d4b2a2 MD |
696 | } else { |
697 | return -1; | |
7fc90dca | 698 | } |
58d4b2a2 | 699 | end: |
33bbeb90 MD |
700 | if (wait_shm_fd >= 0 && !sock_info->global) { |
701 | struct stat statbuf; | |
702 | ||
703 | /* | |
704 | * Ensure that our user is the owner of the shm file for | |
705 | * local shm. If we do not own the file, it means our | |
706 | * sessiond will not have access to wake us up (there is | |
707 | * probably a rogue process trying to fake our | |
708 | * sessiond). Fallback to polling method in this case. | |
709 | */ | |
710 | ret = fstat(wait_shm_fd, &statbuf); | |
711 | if (ret) { | |
712 | PERROR("fstat"); | |
713 | goto error_close; | |
714 | } | |
715 | if (statbuf.st_uid != getuid()) | |
716 | goto error_close; | |
717 | } | |
58d4b2a2 | 718 | return wait_shm_fd; |
33bbeb90 MD |
719 | |
720 | error_close: | |
721 | ret = close(wait_shm_fd); | |
722 | if (ret) { | |
723 | PERROR("Error closing fd"); | |
724 | } | |
725 | return -1; | |
58d4b2a2 MD |
726 | } |
727 | ||
728 | static | |
729 | char *get_map_shm(struct sock_info *sock_info) | |
730 | { | |
731 | size_t mmap_size = sysconf(_SC_PAGE_SIZE); | |
732 | int wait_shm_fd, ret; | |
733 | char *wait_shm_mmap; | |
734 | ||
735 | wait_shm_fd = get_wait_shm(sock_info, mmap_size); | |
736 | if (wait_shm_fd < 0) { | |
737 | goto error; | |
44e073f5 | 738 | } |
7fc90dca MD |
739 | wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ, |
740 | MAP_SHARED, wait_shm_fd, 0); | |
7fc90dca MD |
741 | /* close shm fd immediately after taking the mmap reference */ |
742 | ret = close(wait_shm_fd); | |
743 | if (ret) { | |
33bbeb90 MD |
744 | PERROR("Error closing fd"); |
745 | } | |
746 | if (wait_shm_mmap == MAP_FAILED) { | |
747 | DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode."); | |
748 | goto error; | |
7fc90dca MD |
749 | } |
750 | return wait_shm_mmap; | |
751 | ||
752 | error: | |
753 | return NULL; | |
754 | } | |
755 | ||
756 | static | |
757 | void wait_for_sessiond(struct sock_info *sock_info) | |
758 | { | |
efe0de09 | 759 | int ret; |
80e2814b | 760 | |
7fc90dca MD |
761 | ust_lock(); |
762 | if (lttng_ust_comm_should_quit) { | |
763 | goto quit; | |
764 | } | |
37ed587a MD |
765 | if (wait_poll_fallback) { |
766 | goto error; | |
767 | } | |
7fc90dca MD |
768 | if (!sock_info->wait_shm_mmap) { |
769 | sock_info->wait_shm_mmap = get_map_shm(sock_info); | |
770 | if (!sock_info->wait_shm_mmap) | |
771 | goto error; | |
772 | } | |
773 | ust_unlock(); | |
774 | ||
775 | DBG("Waiting for %s apps sessiond", sock_info->name); | |
80e2814b MD |
776 | /* Wait for futex wakeup */ |
777 | if (uatomic_read((int32_t *) sock_info->wait_shm_mmap) == 0) { | |
778 | ret = futex_async((int32_t *) sock_info->wait_shm_mmap, | |
779 | FUTEX_WAIT, 0, NULL, NULL, 0); | |
80e2814b | 780 | if (ret < 0) { |
37ed587a MD |
781 | if (errno == EFAULT) { |
782 | wait_poll_fallback = 1; | |
a8b870ad | 783 | DBG( |
37ed587a MD |
784 | "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) " |
785 | "do not support FUTEX_WAKE on read-only memory mappings correctly. " | |
786 | "Please upgrade your kernel " | |
787 | "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel " | |
788 | "mainline). LTTng-UST will use polling mode fallback."); | |
cd27263b MD |
789 | if (ust_debug()) |
790 | PERROR("futex"); | |
37ed587a | 791 | } |
80e2814b MD |
792 | } |
793 | } | |
7fc90dca MD |
794 | return; |
795 | ||
796 | quit: | |
797 | ust_unlock(); | |
798 | return; | |
799 | ||
800 | error: | |
801 | ust_unlock(); | |
7fc90dca | 802 | return; |
46050b1a MD |
803 | } |
804 | ||
1ea11eab MD |
805 | /* |
806 | * This thread does not allocate any resource, except within | |
807 | * handle_message, within mutex protection. This mutex protects against | |
808 | * fork and exit. | |
98bf993f | 809 | * The other moment it allocates resources is at socket connection, which |
1ea11eab MD |
810 | * is also protected by the mutex. |
811 | */ | |
d9e99d10 MD |
812 | static |
813 | void *ust_listener_thread(void *arg) | |
814 | { | |
1ea11eab | 815 | struct sock_info *sock_info = arg; |
c0eedf81 | 816 | int sock, ret, prev_connect_failed = 0, has_waited = 0; |
d9e99d10 | 817 | |
9eb62b9c MD |
818 | /* Restart trying to connect to the session daemon */ |
819 | restart: | |
c0eedf81 MD |
820 | if (prev_connect_failed) { |
821 | /* Wait for sessiond availability with pipe */ | |
822 | wait_for_sessiond(sock_info); | |
823 | if (has_waited) { | |
824 | has_waited = 0; | |
825 | /* | |
826 | * Sleep for 5 seconds before retrying after a | |
827 | * sequence of failure / wait / failure. This | |
828 | * deals with a killed or broken session daemon. | |
829 | */ | |
830 | sleep(5); | |
831 | } | |
832 | has_waited = 1; | |
833 | prev_connect_failed = 0; | |
834 | } | |
17dfb34b | 835 | ust_lock(); |
1ea11eab MD |
836 | |
837 | if (lttng_ust_comm_should_quit) { | |
1ea11eab MD |
838 | goto quit; |
839 | } | |
9eb62b9c | 840 | |
1ea11eab | 841 | if (sock_info->socket != -1) { |
e6973a89 | 842 | ret = ustcomm_close_unix_sock(sock_info->socket); |
1ea11eab | 843 | if (ret) { |
11ff9c7d | 844 | ERR("Error closing %s apps socket", sock_info->name); |
1ea11eab MD |
845 | } |
846 | sock_info->socket = -1; | |
847 | } | |
46050b1a | 848 | |
9eb62b9c | 849 | /* Register */ |
57773204 | 850 | ret = ustcomm_connect_unix_sock(sock_info->sock_path); |
9eb62b9c | 851 | if (ret < 0) { |
4d3c9523 | 852 | DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name); |
c0eedf81 | 853 | prev_connect_failed = 1; |
11ff9c7d MD |
854 | /* |
855 | * If we cannot find the sessiond daemon, don't delay | |
856 | * constructor execution. | |
857 | */ | |
edaa1431 | 858 | ret = handle_register_done(sock_info); |
11ff9c7d | 859 | assert(!ret); |
17dfb34b | 860 | ust_unlock(); |
1ea11eab | 861 | goto restart; |
46050b1a MD |
862 | } |
863 | ||
864 | sock_info->socket = sock = ret; | |
865 | ||
866 | /* | |
867 | * Create only one root handle per listener thread for the whole | |
f59ed768 MD |
868 | * process lifetime, so we ensure we get ID which is statically |
869 | * assigned to the root handle. | |
46050b1a MD |
870 | */ |
871 | if (sock_info->root_handle == -1) { | |
872 | ret = lttng_abi_create_root_handle(); | |
a51070bb | 873 | if (ret < 0) { |
46050b1a | 874 | ERR("Error creating root handle"); |
46050b1a MD |
875 | goto quit; |
876 | } | |
877 | sock_info->root_handle = ret; | |
9eb62b9c | 878 | } |
1ea11eab | 879 | |
9eb62b9c MD |
880 | ret = register_app_to_sessiond(sock); |
881 | if (ret < 0) { | |
11ff9c7d | 882 | ERR("Error registering to %s apps socket", sock_info->name); |
c0eedf81 | 883 | prev_connect_failed = 1; |
11ff9c7d MD |
884 | /* |
885 | * If we cannot register to the sessiond daemon, don't | |
886 | * delay constructor execution. | |
887 | */ | |
edaa1431 | 888 | ret = handle_register_done(sock_info); |
11ff9c7d | 889 | assert(!ret); |
17dfb34b | 890 | ust_unlock(); |
9eb62b9c MD |
891 | goto restart; |
892 | } | |
17dfb34b | 893 | ust_unlock(); |
46050b1a | 894 | |
d9e99d10 MD |
895 | for (;;) { |
896 | ssize_t len; | |
57773204 | 897 | struct ustcomm_ust_msg lum; |
d9e99d10 | 898 | |
57773204 | 899 | len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum)); |
d9e99d10 MD |
900 | switch (len) { |
901 | case 0: /* orderly shutdown */ | |
7dd08bec | 902 | DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info->name); |
8236ba10 | 903 | ust_lock(); |
d5e1fea6 | 904 | if (lttng_ust_comm_should_quit) { |
d5e1fea6 MD |
905 | goto quit; |
906 | } | |
8236ba10 MD |
907 | /* |
908 | * Either sessiond has shutdown or refused us by closing the socket. | |
909 | * In either case, we don't want to delay construction execution, | |
910 | * and we need to wait before retry. | |
911 | */ | |
912 | prev_connect_failed = 1; | |
913 | /* | |
914 | * If we cannot register to the sessiond daemon, don't | |
915 | * delay constructor execution. | |
916 | */ | |
917 | ret = handle_register_done(sock_info); | |
918 | assert(!ret); | |
919 | ust_unlock(); | |
d9e99d10 | 920 | goto end; |
e7723462 | 921 | case sizeof(lum): |
74d81a6c | 922 | print_cmd(lum.cmd, lum.handle); |
11ff9c7d | 923 | ret = handle_message(sock_info, sock, &lum); |
7bc53e94 | 924 | if (ret) { |
11ff9c7d | 925 | ERR("Error handling message for %s socket", sock_info->name); |
d9e99d10 MD |
926 | } |
927 | continue; | |
7bc53e94 MD |
928 | default: |
929 | if (len < 0) { | |
930 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); | |
931 | } else { | |
932 | DBG("incorrect message size (%s socket): %zd", sock_info->name, len); | |
933 | } | |
934 | if (len == -ECONNRESET) { | |
935 | DBG("%s remote end closed connection", sock_info->name); | |
d9e99d10 MD |
936 | goto end; |
937 | } | |
938 | goto end; | |
d9e99d10 MD |
939 | } |
940 | ||
941 | } | |
942 | end: | |
f59ed768 | 943 | ust_lock(); |
d5e1fea6 | 944 | if (lttng_ust_comm_should_quit) { |
d5e1fea6 MD |
945 | goto quit; |
946 | } | |
f59ed768 MD |
947 | /* Cleanup socket handles before trying to reconnect */ |
948 | lttng_ust_objd_table_owner_cleanup(sock_info); | |
949 | ust_unlock(); | |
9eb62b9c | 950 | goto restart; /* try to reconnect */ |
e33f3265 | 951 | |
1ea11eab | 952 | quit: |
e33f3265 MD |
953 | sock_info->thread_active = 0; |
954 | ust_unlock(); | |
d9e99d10 MD |
955 | return NULL; |
956 | } | |
957 | ||
cf12a773 MD |
958 | /* |
959 | * Return values: -1: don't wait. 0: wait forever. 1: timeout wait. | |
960 | */ | |
11ff9c7d MD |
961 | static |
962 | int get_timeout(struct timespec *constructor_timeout) | |
963 | { | |
cf12a773 MD |
964 | long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS; |
965 | char *str_delay; | |
11ff9c7d MD |
966 | int ret; |
967 | ||
69400ac4 | 968 | str_delay = getenv("LTTNG_UST_REGISTER_TIMEOUT"); |
cf12a773 MD |
969 | if (str_delay) { |
970 | constructor_delay_ms = strtol(str_delay, NULL, 10); | |
971 | } | |
972 | ||
973 | switch (constructor_delay_ms) { | |
974 | case -1:/* fall-through */ | |
975 | case 0: | |
976 | return constructor_delay_ms; | |
977 | default: | |
978 | break; | |
979 | } | |
980 | ||
981 | /* | |
982 | * If we are unable to find the current time, don't wait. | |
983 | */ | |
984 | ret = clock_gettime(CLOCK_REALTIME, constructor_timeout); | |
985 | if (ret) { | |
986 | return -1; | |
987 | } | |
95259bd0 MD |
988 | constructor_timeout->tv_sec += constructor_delay_ms / 1000UL; |
989 | constructor_timeout->tv_nsec += | |
990 | (constructor_delay_ms % 1000UL) * 1000000UL; | |
11ff9c7d MD |
991 | if (constructor_timeout->tv_nsec >= 1000000000UL) { |
992 | constructor_timeout->tv_sec++; | |
993 | constructor_timeout->tv_nsec -= 1000000000UL; | |
994 | } | |
cf12a773 | 995 | return 1; |
11ff9c7d | 996 | } |
d9e99d10 | 997 | |
2691221a MD |
998 | /* |
999 | * sessiond monitoring thread: monitor presence of global and per-user | |
1000 | * sessiond by polling the application common named pipe. | |
1001 | */ | |
edaa1431 | 1002 | void __attribute__((constructor)) lttng_ust_init(void) |
2691221a | 1003 | { |
11ff9c7d | 1004 | struct timespec constructor_timeout; |
ae6a58bf | 1005 | sigset_t sig_all_blocked, orig_parent_mask; |
1879f67f | 1006 | pthread_attr_t thread_attr; |
cf12a773 | 1007 | int timeout_mode; |
2691221a MD |
1008 | int ret; |
1009 | ||
edaa1431 MD |
1010 | if (uatomic_xchg(&initialized, 1) == 1) |
1011 | return; | |
1012 | ||
eddd8d5d MD |
1013 | /* |
1014 | * Fixup interdependency between TLS fixup mutex (which happens | |
1015 | * to be the dynamic linker mutex) and ust_lock, taken within | |
1016 | * the ust lock. | |
1017 | */ | |
f645cfa7 | 1018 | lttng_fixup_ringbuffer_tls(); |
4158a15a | 1019 | lttng_fixup_vtid_tls(); |
a903623f | 1020 | lttng_fixup_nest_count_tls(); |
009745db | 1021 | lttng_fixup_procname_tls(); |
eddd8d5d | 1022 | |
edaa1431 MD |
1023 | /* |
1024 | * We want precise control over the order in which we construct | |
1025 | * our sub-libraries vs starting to receive commands from | |
1026 | * sessiond (otherwise leading to errors when trying to create | |
1027 | * sessiond before the init functions are completed). | |
1028 | */ | |
2691221a | 1029 | init_usterr(); |
edaa1431 | 1030 | init_tracepoint(); |
7dd08bec MD |
1031 | lttng_ring_buffer_metadata_client_init(); |
1032 | lttng_ring_buffer_client_overwrite_init(); | |
1033 | lttng_ring_buffer_client_discard_init(); | |
2691221a | 1034 | |
cf12a773 | 1035 | timeout_mode = get_timeout(&constructor_timeout); |
11ff9c7d | 1036 | |
95259bd0 | 1037 | ret = sem_init(&constructor_wait, 0, 0); |
11ff9c7d MD |
1038 | assert(!ret); |
1039 | ||
8d20bf54 | 1040 | ret = setup_local_apps(); |
2691221a | 1041 | if (ret) { |
9ec6895c | 1042 | DBG("local apps setup returned %d", ret); |
2691221a | 1043 | } |
ae6a58bf WP |
1044 | |
1045 | /* A new thread created by pthread_create inherits the signal mask | |
1046 | * from the parent. To avoid any signal being received by the | |
1047 | * listener thread, we block all signals temporarily in the parent, | |
1048 | * while we create the listener thread. | |
1049 | */ | |
1050 | sigfillset(&sig_all_blocked); | |
1051 | ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask); | |
1052 | if (ret) { | |
d94d802c | 1053 | ERR("pthread_sigmask: %s", strerror(ret)); |
ae6a58bf WP |
1054 | } |
1055 | ||
1879f67f MG |
1056 | ret = pthread_attr_init(&thread_attr); |
1057 | if (ret) { | |
1058 | ERR("pthread_attr_init: %s", strerror(ret)); | |
1059 | } | |
1060 | ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); | |
1061 | if (ret) { | |
1062 | ERR("pthread_attr_setdetachstate: %s", strerror(ret)); | |
1063 | } | |
1064 | ||
e33f3265 | 1065 | ust_lock(); |
1879f67f | 1066 | ret = pthread_create(&global_apps.ust_listener, &thread_attr, |
dde70ea0 | 1067 | ust_listener_thread, &global_apps); |
d94d802c MD |
1068 | if (ret) { |
1069 | ERR("pthread_create global: %s", strerror(ret)); | |
1070 | } | |
e33f3265 MD |
1071 | global_apps.thread_active = 1; |
1072 | ust_unlock(); | |
1073 | ||
8d20bf54 | 1074 | if (local_apps.allowed) { |
e33f3265 | 1075 | ust_lock(); |
1879f67f | 1076 | ret = pthread_create(&local_apps.ust_listener, &thread_attr, |
dde70ea0 | 1077 | ust_listener_thread, &local_apps); |
d94d802c MD |
1078 | if (ret) { |
1079 | ERR("pthread_create local: %s", strerror(ret)); | |
1080 | } | |
e33f3265 MD |
1081 | local_apps.thread_active = 1; |
1082 | ust_unlock(); | |
8d20bf54 MD |
1083 | } else { |
1084 | handle_register_done(&local_apps); | |
1085 | } | |
1879f67f MG |
1086 | ret = pthread_attr_destroy(&thread_attr); |
1087 | if (ret) { | |
1088 | ERR("pthread_attr_destroy: %s", strerror(ret)); | |
1089 | } | |
8d20bf54 | 1090 | |
ae6a58bf WP |
1091 | /* Restore original signal mask in parent */ |
1092 | ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL); | |
1093 | if (ret) { | |
d94d802c | 1094 | ERR("pthread_sigmask: %s", strerror(ret)); |
ae6a58bf WP |
1095 | } |
1096 | ||
cf12a773 MD |
1097 | switch (timeout_mode) { |
1098 | case 1: /* timeout wait */ | |
95259bd0 MD |
1099 | do { |
1100 | ret = sem_timedwait(&constructor_wait, | |
1101 | &constructor_timeout); | |
1102 | } while (ret < 0 && errno == EINTR); | |
cf12a773 | 1103 | if (ret < 0 && errno == ETIMEDOUT) { |
7dd08bec | 1104 | ERR("Timed out waiting for lttng-sessiond"); |
cf12a773 MD |
1105 | } else { |
1106 | assert(!ret); | |
1107 | } | |
1108 | break; | |
7b766b16 | 1109 | case -1:/* wait forever */ |
95259bd0 MD |
1110 | do { |
1111 | ret = sem_wait(&constructor_wait); | |
1112 | } while (ret < 0 && errno == EINTR); | |
11ff9c7d | 1113 | assert(!ret); |
cf12a773 | 1114 | break; |
7b766b16 | 1115 | case 0: /* no timeout */ |
cf12a773 | 1116 | break; |
11ff9c7d | 1117 | } |
2691221a MD |
1118 | } |
1119 | ||
17dfb34b MD |
1120 | static |
1121 | void lttng_ust_cleanup(int exiting) | |
1122 | { | |
efe0de09 | 1123 | cleanup_sock_info(&global_apps, exiting); |
17dfb34b | 1124 | if (local_apps.allowed) { |
efe0de09 | 1125 | cleanup_sock_info(&local_apps, exiting); |
17dfb34b | 1126 | } |
efe0de09 MD |
1127 | /* |
1128 | * The teardown in this function all affect data structures | |
1129 | * accessed under the UST lock by the listener thread. This | |
1130 | * lock, along with the lttng_ust_comm_should_quit flag, ensure | |
1131 | * that none of these threads are accessing this data at this | |
1132 | * point. | |
1133 | */ | |
17dfb34b | 1134 | lttng_ust_abi_exit(); |
003fedf4 | 1135 | lttng_ust_events_exit(); |
7dd08bec MD |
1136 | lttng_ring_buffer_client_discard_exit(); |
1137 | lttng_ring_buffer_client_overwrite_exit(); | |
1138 | lttng_ring_buffer_metadata_client_exit(); | |
17dfb34b MD |
1139 | exit_tracepoint(); |
1140 | if (!exiting) { | |
1141 | /* Reinitialize values for fork */ | |
1142 | sem_count = 2; | |
1143 | lttng_ust_comm_should_quit = 0; | |
1144 | initialized = 0; | |
1145 | } | |
1146 | } | |
1147 | ||
edaa1431 | 1148 | void __attribute__((destructor)) lttng_ust_exit(void) |
2691221a MD |
1149 | { |
1150 | int ret; | |
1151 | ||
9eb62b9c MD |
1152 | /* |
1153 | * Using pthread_cancel here because: | |
1154 | * A) we don't want to hang application teardown. | |
1155 | * B) the thread is not allocating any resource. | |
1156 | */ | |
1ea11eab MD |
1157 | |
1158 | /* | |
1159 | * Require the communication thread to quit. Synchronize with | |
1160 | * mutexes to ensure it is not in a mutex critical section when | |
1161 | * pthread_cancel is later called. | |
1162 | */ | |
17dfb34b | 1163 | ust_lock(); |
1ea11eab | 1164 | lttng_ust_comm_should_quit = 1; |
1ea11eab | 1165 | |
f5f94532 | 1166 | /* cancel threads */ |
e33f3265 MD |
1167 | if (global_apps.thread_active) { |
1168 | ret = pthread_cancel(global_apps.ust_listener); | |
1169 | if (ret) { | |
1170 | ERR("Error cancelling global ust listener thread: %s", | |
1171 | strerror(ret)); | |
1172 | } else { | |
1173 | global_apps.thread_active = 0; | |
1174 | } | |
2691221a | 1175 | } |
e33f3265 | 1176 | if (local_apps.thread_active) { |
8d20bf54 MD |
1177 | ret = pthread_cancel(local_apps.ust_listener); |
1178 | if (ret) { | |
d94d802c MD |
1179 | ERR("Error cancelling local ust listener thread: %s", |
1180 | strerror(ret)); | |
e33f3265 MD |
1181 | } else { |
1182 | local_apps.thread_active = 0; | |
8d20bf54 | 1183 | } |
8d20bf54 | 1184 | } |
e33f3265 MD |
1185 | ust_unlock(); |
1186 | ||
efe0de09 MD |
1187 | /* |
1188 | * Do NOT join threads: use of sys_futex makes it impossible to | |
1189 | * join the threads without using async-cancel, but async-cancel | |
1190 | * is delivered by a signal, which could hit the target thread | |
1191 | * anywhere in its code path, including while the ust_lock() is | |
1192 | * held, causing a deadlock for the other thread. Let the OS | |
1193 | * cleanup the threads if there are stalled in a syscall. | |
1194 | */ | |
17dfb34b | 1195 | lttng_ust_cleanup(1); |
2691221a | 1196 | } |
e822f505 MD |
1197 | |
1198 | /* | |
1199 | * We exclude the worker threads across fork and clone (except | |
1200 | * CLONE_VM), because these system calls only keep the forking thread | |
1201 | * running in the child. Therefore, we don't want to call fork or clone | |
1202 | * in the middle of an tracepoint or ust tracing state modification. | |
1203 | * Holding this mutex protects these structures across fork and clone. | |
1204 | */ | |
b728d87e | 1205 | void ust_before_fork(sigset_t *save_sigset) |
e822f505 MD |
1206 | { |
1207 | /* | |
1208 | * Disable signals. This is to avoid that the child intervenes | |
1209 | * before it is properly setup for tracing. It is safer to | |
1210 | * disable all signals, because then we know we are not breaking | |
1211 | * anything by restoring the original mask. | |
1212 | */ | |
1213 | sigset_t all_sigs; | |
1214 | int ret; | |
1215 | ||
e8508a49 MD |
1216 | if (lttng_ust_nest_count) |
1217 | return; | |
e822f505 MD |
1218 | /* Disable signals */ |
1219 | sigfillset(&all_sigs); | |
b728d87e | 1220 | ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset); |
e822f505 MD |
1221 | if (ret == -1) { |
1222 | PERROR("sigprocmask"); | |
1223 | } | |
17dfb34b | 1224 | ust_lock(); |
e822f505 MD |
1225 | rcu_bp_before_fork(); |
1226 | } | |
1227 | ||
b728d87e | 1228 | static void ust_after_fork_common(sigset_t *restore_sigset) |
e822f505 MD |
1229 | { |
1230 | int ret; | |
1231 | ||
17dfb34b MD |
1232 | DBG("process %d", getpid()); |
1233 | ust_unlock(); | |
e822f505 | 1234 | /* Restore signals */ |
23c8854a | 1235 | ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL); |
e822f505 MD |
1236 | if (ret == -1) { |
1237 | PERROR("sigprocmask"); | |
1238 | } | |
1239 | } | |
1240 | ||
b728d87e | 1241 | void ust_after_fork_parent(sigset_t *restore_sigset) |
e822f505 | 1242 | { |
e8508a49 MD |
1243 | if (lttng_ust_nest_count) |
1244 | return; | |
17dfb34b | 1245 | DBG("process %d", getpid()); |
e822f505 MD |
1246 | rcu_bp_after_fork_parent(); |
1247 | /* Release mutexes and reenable signals */ | |
b728d87e | 1248 | ust_after_fork_common(restore_sigset); |
e822f505 MD |
1249 | } |
1250 | ||
17dfb34b MD |
1251 | /* |
1252 | * After fork, in the child, we need to cleanup all the leftover state, | |
1253 | * except the worker thread which already magically disappeared thanks | |
1254 | * to the weird Linux fork semantics. After tyding up, we call | |
1255 | * lttng_ust_init() again to start over as a new PID. | |
1256 | * | |
1257 | * This is meant for forks() that have tracing in the child between the | |
1258 | * fork and following exec call (if there is any). | |
1259 | */ | |
b728d87e | 1260 | void ust_after_fork_child(sigset_t *restore_sigset) |
e822f505 | 1261 | { |
e8508a49 MD |
1262 | if (lttng_ust_nest_count) |
1263 | return; | |
17dfb34b | 1264 | DBG("process %d", getpid()); |
e822f505 MD |
1265 | /* Release urcu mutexes */ |
1266 | rcu_bp_after_fork_child(); | |
17dfb34b | 1267 | lttng_ust_cleanup(0); |
a93bfc45 | 1268 | lttng_context_vtid_reset(); |
e822f505 | 1269 | /* Release mutexes and reenable signals */ |
b728d87e | 1270 | ust_after_fork_common(restore_sigset); |
318dfea9 | 1271 | lttng_ust_init(); |
e822f505 | 1272 | } |