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