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