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