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