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