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