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