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