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 | ||
22 | #include <sys/types.h> | |
23 | #include <sys/socket.h> | |
2629549e | 24 | #include <sys/prctl.h> |
2691221a MD |
25 | #include <unistd.h> |
26 | #include <errno.h> | |
d9e99d10 | 27 | #include <pthread.h> |
11ff9c7d MD |
28 | #include <semaphore.h> |
29 | #include <time.h> | |
1ea11eab | 30 | #include <assert.h> |
95259bd0 | 31 | #include <urcu/uatomic.h> |
1ea11eab | 32 | |
edaa1431 MD |
33 | #include <lttng-ust-comm.h> |
34 | #include <ust/usterr-signal-safe.h> | |
35 | #include <ust/lttng-ust-abi.h> | |
36 | #include <ust/tracepoint.h> | |
37 | ||
38 | /* | |
39 | * Has lttng ust comm constructor been called ? | |
40 | */ | |
41 | static int initialized; | |
42 | ||
1ea11eab MD |
43 | /* |
44 | * communication thread mutex. Held when handling a command, also held | |
45 | * by fork() to deal with removal of threads, and by exit path. | |
46 | */ | |
47 | static pthread_mutex_t lttng_ust_comm_mutex = PTHREAD_MUTEX_INITIALIZER; | |
48 | ||
49 | /* Should the ust comm thread quit ? */ | |
50 | static int lttng_ust_comm_should_quit; | |
51 | ||
11ff9c7d MD |
52 | /* |
53 | * Wait for either of these before continuing to the main | |
54 | * program: | |
55 | * - the register_done message from sessiond daemon | |
56 | * (will let the sessiond daemon enable sessions before main | |
57 | * starts.) | |
58 | * - sessiond daemon is not reachable. | |
59 | * - timeout (ensuring applications are resilient to session | |
60 | * daemon problems). | |
61 | */ | |
62 | static sem_t constructor_wait; | |
950aab0c MD |
63 | /* |
64 | * Doing this for both the global and local sessiond. | |
65 | */ | |
95259bd0 | 66 | static int sem_count = { 2 }; |
11ff9c7d | 67 | |
1ea11eab MD |
68 | /* |
69 | * Info about socket and associated listener thread. | |
70 | */ | |
71 | struct sock_info { | |
11ff9c7d | 72 | const char *name; |
1ea11eab MD |
73 | char sock_path[PATH_MAX]; |
74 | int socket; | |
75 | pthread_t ust_listener; /* listener thread */ | |
46050b1a | 76 | int root_handle; |
edaa1431 | 77 | int constructor_sem_posted;; |
1ea11eab | 78 | }; |
2691221a MD |
79 | |
80 | /* Socket from app (connect) to session daemon (listen) for communication */ | |
1ea11eab | 81 | struct sock_info global_apps = { |
11ff9c7d | 82 | .name = "global", |
1ea11eab MD |
83 | .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK, |
84 | .socket = -1, | |
46050b1a | 85 | .root_handle = -1, |
1ea11eab | 86 | }; |
2691221a MD |
87 | |
88 | /* TODO: allow global_apps_sock_path override */ | |
89 | ||
1ea11eab | 90 | struct sock_info local_apps = { |
11ff9c7d | 91 | .name = "local", |
1ea11eab | 92 | .socket = -1, |
46050b1a | 93 | .root_handle = -1, |
1ea11eab | 94 | }; |
2691221a | 95 | |
edaa1431 MD |
96 | extern void ltt_ring_buffer_client_overwrite_init(void); |
97 | extern void ltt_ring_buffer_client_discard_init(void); | |
98 | extern void ltt_ring_buffer_metadata_client_init(void); | |
99 | extern void ltt_ring_buffer_client_overwrite_exit(void); | |
100 | extern void ltt_ring_buffer_client_discard_exit(void); | |
101 | extern void ltt_ring_buffer_metadata_client_exit(void); | |
102 | ||
2691221a | 103 | static |
9eb62b9c | 104 | int setup_local_apps_socket(void) |
2691221a MD |
105 | { |
106 | const char *home_dir; | |
2691221a MD |
107 | |
108 | home_dir = (const char *) getenv("HOME"); | |
109 | if (!home_dir) | |
110 | return -ENOENT; | |
1ea11eab | 111 | snprintf(local_apps.sock_path, PATH_MAX, |
2691221a | 112 | DEFAULT_HOME_APPS_UNIX_SOCK, home_dir); |
2691221a MD |
113 | return 0; |
114 | } | |
115 | ||
116 | static | |
117 | int register_app_to_sessiond(int socket) | |
118 | { | |
119 | ssize_t ret; | |
2629549e | 120 | int prctl_ret; |
2691221a | 121 | struct { |
e44418f3 MD |
122 | uint32_t major; |
123 | uint32_t minor; | |
2691221a MD |
124 | pid_t pid; |
125 | uid_t uid; | |
2629549e | 126 | char name[16]; /* process name */ |
2691221a MD |
127 | } reg_msg; |
128 | ||
e44418f3 MD |
129 | reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR; |
130 | reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR; | |
2691221a MD |
131 | reg_msg.pid = getpid(); |
132 | reg_msg.uid = getuid(); | |
2629549e MD |
133 | prctl_ret = prctl(PR_GET_NAME, (unsigned long) reg_msg.name, 0, 0, 0); |
134 | if (prctl_ret) { | |
135 | ERR("Error executing prctl"); | |
136 | return -errno; | |
137 | } | |
2691221a MD |
138 | |
139 | ret = lttcomm_send_unix_sock(socket, ®_msg, sizeof(reg_msg)); | |
140 | if (ret >= 0 && ret != sizeof(reg_msg)) | |
141 | return -EIO; | |
142 | return ret; | |
143 | } | |
144 | ||
d9e99d10 | 145 | static |
d3a492d1 | 146 | int send_reply(int sock, struct lttcomm_ust_reply *lur) |
d9e99d10 | 147 | { |
9eb62b9c | 148 | ssize_t len; |
d3a492d1 | 149 | |
a4be8962 | 150 | len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur)); |
d3a492d1 | 151 | switch (len) { |
a4be8962 | 152 | case sizeof(*lur): |
d3a492d1 MD |
153 | DBG("message successfully sent"); |
154 | return 0; | |
155 | case -1: | |
156 | if (errno == ECONNRESET) { | |
157 | printf("remote end closed connection\n"); | |
158 | return 0; | |
159 | } | |
160 | return -1; | |
161 | default: | |
162 | printf("incorrect message size: %zd\n", len); | |
163 | return -1; | |
164 | } | |
165 | } | |
166 | ||
167 | static | |
edaa1431 | 168 | int handle_register_done(struct sock_info *sock_info) |
11ff9c7d MD |
169 | { |
170 | int ret; | |
171 | ||
edaa1431 MD |
172 | if (sock_info->constructor_sem_posted) |
173 | return 0; | |
174 | sock_info->constructor_sem_posted = 1; | |
95259bd0 MD |
175 | ret = uatomic_add_return(&sem_count, -1); |
176 | if (ret == 0) { | |
177 | ret = sem_post(&constructor_wait); | |
178 | assert(!ret); | |
179 | } | |
11ff9c7d MD |
180 | return 0; |
181 | } | |
182 | ||
183 | static | |
184 | int handle_message(struct sock_info *sock_info, | |
185 | int sock, struct lttcomm_ust_msg *lum) | |
d3a492d1 | 186 | { |
1ea11eab | 187 | int ret = 0; |
46050b1a MD |
188 | const struct objd_ops *ops; |
189 | struct lttcomm_ust_reply lur; | |
1ea11eab MD |
190 | |
191 | pthread_mutex_lock(<tng_ust_comm_mutex); | |
192 | ||
46050b1a MD |
193 | memset(&lur, 0, sizeof(lur)); |
194 | ||
1ea11eab | 195 | if (lttng_ust_comm_should_quit) { |
46050b1a | 196 | ret = -EPERM; |
1ea11eab MD |
197 | goto end; |
198 | } | |
9eb62b9c | 199 | |
46050b1a MD |
200 | ops = objd_ops(lum->handle); |
201 | if (!ops) { | |
202 | ret = -ENOENT; | |
203 | goto end; | |
1ea11eab | 204 | } |
46050b1a MD |
205 | |
206 | switch (lum->cmd) { | |
11ff9c7d MD |
207 | case LTTNG_UST_REGISTER_DONE: |
208 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
edaa1431 | 209 | ret = handle_register_done(sock_info); |
11ff9c7d MD |
210 | else |
211 | ret = -EINVAL; | |
212 | break; | |
46050b1a MD |
213 | case LTTNG_UST_RELEASE: |
214 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
215 | ret = -EPERM; | |
216 | else | |
217 | ret = objd_unref(lum->handle); | |
d9e99d10 MD |
218 | break; |
219 | default: | |
46050b1a MD |
220 | if (ops->cmd) |
221 | ret = ops->cmd(lum->handle, lum->cmd, | |
222 | (unsigned long) &lum->u); | |
223 | else | |
224 | ret = -ENOSYS; | |
225 | break; | |
d9e99d10 | 226 | } |
46050b1a | 227 | |
1ea11eab | 228 | end: |
46050b1a MD |
229 | lur.handle = lum->handle; |
230 | lur.cmd = lum->cmd; | |
231 | lur.ret_val = ret; | |
232 | if (ret >= 0) { | |
233 | lur.ret_code = LTTCOMM_OK; | |
234 | } else { | |
235 | lur.ret_code = LTTCOMM_SESSION_FAIL; | |
236 | } | |
237 | ret = send_reply(sock, &lur); | |
238 | ||
1ea11eab MD |
239 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
240 | return ret; | |
d9e99d10 MD |
241 | } |
242 | ||
46050b1a MD |
243 | static |
244 | void cleanup_sock_info(struct sock_info *sock_info) | |
245 | { | |
246 | int ret; | |
247 | ||
248 | if (sock_info->socket != -1) { | |
249 | ret = close(sock_info->socket); | |
250 | if (ret) { | |
251 | ERR("Error closing local apps socket"); | |
252 | } | |
253 | sock_info->socket = -1; | |
254 | } | |
255 | if (sock_info->root_handle != -1) { | |
256 | ret = objd_unref(sock_info->root_handle); | |
257 | if (ret) { | |
258 | ERR("Error unref root handle"); | |
259 | } | |
260 | sock_info->root_handle = -1; | |
261 | } | |
262 | } | |
263 | ||
1ea11eab MD |
264 | /* |
265 | * This thread does not allocate any resource, except within | |
266 | * handle_message, within mutex protection. This mutex protects against | |
267 | * fork and exit. | |
268 | * The other moment it allocates resources is at socket connexion, which | |
269 | * is also protected by the mutex. | |
270 | */ | |
d9e99d10 MD |
271 | static |
272 | void *ust_listener_thread(void *arg) | |
273 | { | |
1ea11eab MD |
274 | struct sock_info *sock_info = arg; |
275 | int sock, ret; | |
d9e99d10 | 276 | |
9eb62b9c MD |
277 | /* Restart trying to connect to the session daemon */ |
278 | restart: | |
1ea11eab MD |
279 | pthread_mutex_lock(<tng_ust_comm_mutex); |
280 | ||
281 | if (lttng_ust_comm_should_quit) { | |
282 | pthread_mutex_unlock(<tng_ust_comm_mutex); | |
283 | goto quit; | |
284 | } | |
9eb62b9c | 285 | |
1ea11eab MD |
286 | if (sock_info->socket != -1) { |
287 | ret = close(sock_info->socket); | |
288 | if (ret) { | |
11ff9c7d | 289 | ERR("Error closing %s apps socket", sock_info->name); |
1ea11eab MD |
290 | } |
291 | sock_info->socket = -1; | |
292 | } | |
46050b1a | 293 | |
9eb62b9c MD |
294 | /* Check for sessiond availability with pipe TODO */ |
295 | ||
296 | /* Register */ | |
1ea11eab | 297 | ret = lttcomm_connect_unix_sock(sock_info->sock_path); |
9eb62b9c | 298 | if (ret < 0) { |
11ff9c7d MD |
299 | ERR("Error connecting to %s apps socket", sock_info->name); |
300 | /* | |
301 | * If we cannot find the sessiond daemon, don't delay | |
302 | * constructor execution. | |
303 | */ | |
edaa1431 | 304 | ret = handle_register_done(sock_info); |
11ff9c7d | 305 | assert(!ret); |
1ea11eab | 306 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
4eef2998 | 307 | sleep(5); |
1ea11eab | 308 | goto restart; |
46050b1a MD |
309 | } |
310 | ||
311 | sock_info->socket = sock = ret; | |
312 | ||
313 | /* | |
314 | * Create only one root handle per listener thread for the whole | |
315 | * process lifetime. | |
316 | */ | |
317 | if (sock_info->root_handle == -1) { | |
318 | ret = lttng_abi_create_root_handle(); | |
319 | if (ret) { | |
320 | ERR("Error creating root handle"); | |
321 | pthread_mutex_unlock(<tng_ust_comm_mutex); | |
322 | goto quit; | |
323 | } | |
324 | sock_info->root_handle = ret; | |
9eb62b9c | 325 | } |
1ea11eab | 326 | |
9eb62b9c MD |
327 | ret = register_app_to_sessiond(sock); |
328 | if (ret < 0) { | |
11ff9c7d MD |
329 | ERR("Error registering to %s apps socket", sock_info->name); |
330 | /* | |
331 | * If we cannot register to the sessiond daemon, don't | |
332 | * delay constructor execution. | |
333 | */ | |
edaa1431 | 334 | ret = handle_register_done(sock_info); |
11ff9c7d | 335 | assert(!ret); |
46050b1a | 336 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
9eb62b9c MD |
337 | sleep(5); |
338 | goto restart; | |
339 | } | |
46050b1a MD |
340 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
341 | ||
d9e99d10 MD |
342 | for (;;) { |
343 | ssize_t len; | |
e7723462 | 344 | struct lttcomm_ust_msg lum; |
d9e99d10 | 345 | |
e7723462 | 346 | len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum)); |
d9e99d10 MD |
347 | switch (len) { |
348 | case 0: /* orderly shutdown */ | |
11ff9c7d | 349 | DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name); |
d9e99d10 | 350 | goto end; |
e7723462 | 351 | case sizeof(lum): |
d9e99d10 | 352 | DBG("message received\n"); |
11ff9c7d | 353 | ret = handle_message(sock_info, sock, &lum); |
2a80c9d8 | 354 | if (ret < 0) { |
11ff9c7d | 355 | ERR("Error handling message for %s socket", sock_info->name); |
d9e99d10 MD |
356 | } |
357 | continue; | |
358 | case -1: | |
359 | if (errno == ECONNRESET) { | |
11ff9c7d | 360 | ERR("%s remote end closed connection\n", sock_info->name); |
d9e99d10 MD |
361 | goto end; |
362 | } | |
363 | goto end; | |
364 | default: | |
11ff9c7d | 365 | ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len); |
d9e99d10 MD |
366 | continue; |
367 | } | |
368 | ||
369 | } | |
370 | end: | |
9eb62b9c | 371 | goto restart; /* try to reconnect */ |
1ea11eab | 372 | quit: |
d9e99d10 MD |
373 | return NULL; |
374 | } | |
375 | ||
cf12a773 MD |
376 | /* |
377 | * Return values: -1: don't wait. 0: wait forever. 1: timeout wait. | |
378 | */ | |
11ff9c7d MD |
379 | static |
380 | int get_timeout(struct timespec *constructor_timeout) | |
381 | { | |
cf12a773 MD |
382 | long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS; |
383 | char *str_delay; | |
11ff9c7d MD |
384 | int ret; |
385 | ||
cf12a773 MD |
386 | str_delay = getenv("UST_REGISTER_TIMEOUT"); |
387 | if (str_delay) { | |
388 | constructor_delay_ms = strtol(str_delay, NULL, 10); | |
389 | } | |
390 | ||
391 | switch (constructor_delay_ms) { | |
392 | case -1:/* fall-through */ | |
393 | case 0: | |
394 | return constructor_delay_ms; | |
395 | default: | |
396 | break; | |
397 | } | |
398 | ||
399 | /* | |
400 | * If we are unable to find the current time, don't wait. | |
401 | */ | |
402 | ret = clock_gettime(CLOCK_REALTIME, constructor_timeout); | |
403 | if (ret) { | |
404 | return -1; | |
405 | } | |
95259bd0 MD |
406 | constructor_timeout->tv_sec += constructor_delay_ms / 1000UL; |
407 | constructor_timeout->tv_nsec += | |
408 | (constructor_delay_ms % 1000UL) * 1000000UL; | |
11ff9c7d MD |
409 | if (constructor_timeout->tv_nsec >= 1000000000UL) { |
410 | constructor_timeout->tv_sec++; | |
411 | constructor_timeout->tv_nsec -= 1000000000UL; | |
412 | } | |
cf12a773 | 413 | return 1; |
11ff9c7d | 414 | } |
d9e99d10 | 415 | |
2691221a MD |
416 | /* |
417 | * sessiond monitoring thread: monitor presence of global and per-user | |
418 | * sessiond by polling the application common named pipe. | |
419 | */ | |
420 | /* TODO */ | |
421 | ||
edaa1431 | 422 | void __attribute__((constructor)) lttng_ust_init(void) |
2691221a | 423 | { |
11ff9c7d | 424 | struct timespec constructor_timeout; |
cf12a773 | 425 | int timeout_mode; |
2691221a MD |
426 | int ret; |
427 | ||
edaa1431 MD |
428 | if (uatomic_xchg(&initialized, 1) == 1) |
429 | return; | |
430 | ||
431 | /* | |
432 | * We want precise control over the order in which we construct | |
433 | * our sub-libraries vs starting to receive commands from | |
434 | * sessiond (otherwise leading to errors when trying to create | |
435 | * sessiond before the init functions are completed). | |
436 | */ | |
2691221a | 437 | init_usterr(); |
edaa1431 MD |
438 | init_tracepoint(); |
439 | ltt_ring_buffer_metadata_client_init(); | |
440 | ltt_ring_buffer_client_overwrite_init(); | |
441 | ltt_ring_buffer_client_discard_init(); | |
2691221a | 442 | |
cf12a773 | 443 | timeout_mode = get_timeout(&constructor_timeout); |
11ff9c7d | 444 | |
95259bd0 | 445 | ret = sem_init(&constructor_wait, 0, 0); |
11ff9c7d MD |
446 | assert(!ret); |
447 | ||
9eb62b9c | 448 | ret = setup_local_apps_socket(); |
2691221a | 449 | if (ret) { |
9eb62b9c | 450 | ERR("Error setting up to local apps socket"); |
2691221a | 451 | } |
11ff9c7d | 452 | |
1ea11eab MD |
453 | ret = pthread_create(&global_apps.ust_listener, NULL, |
454 | ust_listener_thread, &global_apps); | |
1ea11eab MD |
455 | ret = pthread_create(&local_apps.ust_listener, NULL, |
456 | ust_listener_thread, &local_apps); | |
11ff9c7d | 457 | |
cf12a773 MD |
458 | switch (timeout_mode) { |
459 | case 1: /* timeout wait */ | |
95259bd0 MD |
460 | do { |
461 | ret = sem_timedwait(&constructor_wait, | |
462 | &constructor_timeout); | |
463 | } while (ret < 0 && errno == EINTR); | |
cf12a773 MD |
464 | if (ret < 0 && errno == ETIMEDOUT) { |
465 | ERR("Timed out waiting for ltt-sessiond"); | |
466 | } else { | |
467 | assert(!ret); | |
468 | } | |
469 | break; | |
7b766b16 | 470 | case -1:/* wait forever */ |
95259bd0 MD |
471 | do { |
472 | ret = sem_wait(&constructor_wait); | |
473 | } while (ret < 0 && errno == EINTR); | |
11ff9c7d | 474 | assert(!ret); |
cf12a773 | 475 | break; |
7b766b16 | 476 | case 0: /* no timeout */ |
cf12a773 | 477 | break; |
11ff9c7d | 478 | } |
2691221a MD |
479 | } |
480 | ||
edaa1431 | 481 | void __attribute__((destructor)) lttng_ust_exit(void) |
2691221a MD |
482 | { |
483 | int ret; | |
484 | ||
9eb62b9c MD |
485 | /* |
486 | * Using pthread_cancel here because: | |
487 | * A) we don't want to hang application teardown. | |
488 | * B) the thread is not allocating any resource. | |
489 | */ | |
1ea11eab MD |
490 | |
491 | /* | |
492 | * Require the communication thread to quit. Synchronize with | |
493 | * mutexes to ensure it is not in a mutex critical section when | |
494 | * pthread_cancel is later called. | |
495 | */ | |
496 | pthread_mutex_lock(<tng_ust_comm_mutex); | |
497 | lttng_ust_comm_should_quit = 1; | |
498 | pthread_mutex_unlock(<tng_ust_comm_mutex); | |
499 | ||
500 | #if 0 | |
501 | ret = pthread_cancel(global_apps.ust_listener); | |
9eb62b9c MD |
502 | if (ret) { |
503 | ERR("Error cancelling global ust listener thread"); | |
2691221a | 504 | } |
1ea11eab | 505 | #endif //0 |
46050b1a MD |
506 | |
507 | cleanup_sock_info(&global_apps); | |
1ea11eab MD |
508 | |
509 | ret = pthread_cancel(local_apps.ust_listener); | |
9eb62b9c MD |
510 | if (ret) { |
511 | ERR("Error cancelling local ust listener thread"); | |
2691221a | 512 | } |
1ea11eab | 513 | |
46050b1a | 514 | cleanup_sock_info(&local_apps); |
1ea11eab | 515 | |
b35d179d | 516 | lttng_ust_abi_exit(); |
1ea11eab | 517 | ltt_events_exit(); |
edaa1431 MD |
518 | ltt_ring_buffer_client_discard_exit(); |
519 | ltt_ring_buffer_client_overwrite_exit(); | |
520 | ltt_ring_buffer_metadata_client_exit(); | |
521 | exit_tracepoint(); | |
2691221a | 522 | } |