cancel global thread at exit
[lttng-ust.git] / libust / lttng-ust-comm.c
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 <sys/prctl.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <pthread.h>
28 #include <semaphore.h>
29 #include <time.h>
30 #include <assert.h>
31 #include <urcu/uatomic.h>
32
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
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
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;
63 /*
64 * Doing this for both the global and local sessiond.
65 */
66 static int sem_count = { 2 };
67
68 /*
69 * Info about socket and associated listener thread.
70 */
71 struct sock_info {
72 const char *name;
73 char sock_path[PATH_MAX];
74 int socket;
75 pthread_t ust_listener; /* listener thread */
76 int root_handle;
77 int constructor_sem_posted;;
78 };
79
80 /* Socket from app (connect) to session daemon (listen) for communication */
81 struct sock_info global_apps = {
82 .name = "global",
83 .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK,
84 .socket = -1,
85 .root_handle = -1,
86 };
87
88 /* TODO: allow global_apps_sock_path override */
89
90 struct sock_info local_apps = {
91 .name = "local",
92 .socket = -1,
93 .root_handle = -1,
94 };
95
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
103 static
104 int setup_local_apps_socket(void)
105 {
106 const char *home_dir;
107
108 home_dir = (const char *) getenv("HOME");
109 if (!home_dir)
110 return -ENOENT;
111 snprintf(local_apps.sock_path, PATH_MAX,
112 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
113 return 0;
114 }
115
116 static
117 int register_app_to_sessiond(int socket)
118 {
119 ssize_t ret;
120 int prctl_ret;
121 struct {
122 uint32_t major;
123 uint32_t minor;
124 pid_t pid;
125 pid_t ppid;
126 uid_t uid;
127 gid_t gid;
128 char name[16]; /* process name */
129 } reg_msg;
130
131 reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR;
132 reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR;
133 reg_msg.pid = getpid();
134 reg_msg.ppid = getppid();
135 reg_msg.uid = getuid();
136 reg_msg.gid = getgid();
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 }
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
149 static
150 int send_reply(int sock, struct lttcomm_ust_reply *lur)
151 {
152 ssize_t len;
153
154 len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur));
155 switch (len) {
156 case sizeof(*lur):
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
171 static
172 int handle_register_done(struct sock_info *sock_info)
173 {
174 int ret;
175
176 if (sock_info->constructor_sem_posted)
177 return 0;
178 sock_info->constructor_sem_posted = 1;
179 ret = uatomic_add_return(&sem_count, -1);
180 if (ret == 0) {
181 ret = sem_post(&constructor_wait);
182 assert(!ret);
183 }
184 return 0;
185 }
186
187 static
188 int handle_message(struct sock_info *sock_info,
189 int sock, struct lttcomm_ust_msg *lum)
190 {
191 int ret = 0;
192 const struct objd_ops *ops;
193 struct lttcomm_ust_reply lur;
194
195 pthread_mutex_lock(&lttng_ust_comm_mutex);
196
197 memset(&lur, 0, sizeof(lur));
198
199 if (lttng_ust_comm_should_quit) {
200 ret = -EPERM;
201 goto end;
202 }
203
204 ops = objd_ops(lum->handle);
205 if (!ops) {
206 ret = -ENOENT;
207 goto end;
208 }
209
210 switch (lum->cmd) {
211 case LTTNG_UST_REGISTER_DONE:
212 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
213 ret = handle_register_done(sock_info);
214 else
215 ret = -EINVAL;
216 break;
217 case LTTNG_UST_RELEASE:
218 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
219 ret = -EPERM;
220 else
221 ret = objd_unref(lum->handle);
222 break;
223 default:
224 if (ops->cmd)
225 ret = ops->cmd(lum->handle, lum->cmd,
226 (unsigned long) &lum->u);
227 else
228 ret = -ENOSYS;
229 break;
230 }
231
232 end:
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
243 pthread_mutex_unlock(&lttng_ust_comm_mutex);
244 return ret;
245 }
246
247 static
248 void 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
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 */
275 static
276 void *ust_listener_thread(void *arg)
277 {
278 struct sock_info *sock_info = arg;
279 int sock, ret;
280
281 /* Restart trying to connect to the session daemon */
282 restart:
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 }
289
290 if (sock_info->socket != -1) {
291 ret = close(sock_info->socket);
292 if (ret) {
293 ERR("Error closing %s apps socket", sock_info->name);
294 }
295 sock_info->socket = -1;
296 }
297
298 /* Check for sessiond availability with pipe TODO */
299
300 /* Register */
301 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
302 if (ret < 0) {
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 */
308 ret = handle_register_done(sock_info);
309 assert(!ret);
310 pthread_mutex_unlock(&lttng_ust_comm_mutex);
311 sleep(5);
312 goto restart;
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;
329 }
330
331 ret = register_app_to_sessiond(sock);
332 if (ret < 0) {
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 */
338 ret = handle_register_done(sock_info);
339 assert(!ret);
340 pthread_mutex_unlock(&lttng_ust_comm_mutex);
341 sleep(5);
342 goto restart;
343 }
344 pthread_mutex_unlock(&lttng_ust_comm_mutex);
345
346 for (;;) {
347 ssize_t len;
348 struct lttcomm_ust_msg lum;
349
350 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
351 switch (len) {
352 case 0: /* orderly shutdown */
353 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name);
354 goto end;
355 case sizeof(lum):
356 DBG("message received\n");
357 ret = handle_message(sock_info, sock, &lum);
358 if (ret < 0) {
359 ERR("Error handling message for %s socket", sock_info->name);
360 }
361 continue;
362 case -1:
363 if (errno == ECONNRESET) {
364 ERR("%s remote end closed connection\n", sock_info->name);
365 goto end;
366 }
367 goto end;
368 default:
369 ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len);
370 continue;
371 }
372
373 }
374 end:
375 goto restart; /* try to reconnect */
376 quit:
377 return NULL;
378 }
379
380 /*
381 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
382 */
383 static
384 int get_timeout(struct timespec *constructor_timeout)
385 {
386 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
387 char *str_delay;
388 int ret;
389
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 }
410 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
411 constructor_timeout->tv_nsec +=
412 (constructor_delay_ms % 1000UL) * 1000000UL;
413 if (constructor_timeout->tv_nsec >= 1000000000UL) {
414 constructor_timeout->tv_sec++;
415 constructor_timeout->tv_nsec -= 1000000000UL;
416 }
417 return 1;
418 }
419
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
426 void __attribute__((constructor)) lttng_ust_init(void)
427 {
428 struct timespec constructor_timeout;
429 int timeout_mode;
430 int ret;
431
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 */
441 init_usterr();
442 init_tracepoint();
443 ltt_ring_buffer_metadata_client_init();
444 ltt_ring_buffer_client_overwrite_init();
445 ltt_ring_buffer_client_discard_init();
446
447 timeout_mode = get_timeout(&constructor_timeout);
448
449 ret = sem_init(&constructor_wait, 0, 0);
450 assert(!ret);
451
452 ret = setup_local_apps_socket();
453 if (ret) {
454 ERR("Error setting up to local apps socket");
455 }
456
457 ret = pthread_create(&global_apps.ust_listener, NULL,
458 ust_listener_thread, &global_apps);
459 ret = pthread_create(&local_apps.ust_listener, NULL,
460 ust_listener_thread, &local_apps);
461
462 switch (timeout_mode) {
463 case 1: /* timeout wait */
464 do {
465 ret = sem_timedwait(&constructor_wait,
466 &constructor_timeout);
467 } while (ret < 0 && errno == EINTR);
468 if (ret < 0 && errno == ETIMEDOUT) {
469 ERR("Timed out waiting for ltt-sessiond");
470 } else {
471 assert(!ret);
472 }
473 break;
474 case -1:/* wait forever */
475 do {
476 ret = sem_wait(&constructor_wait);
477 } while (ret < 0 && errno == EINTR);
478 assert(!ret);
479 break;
480 case 0: /* no timeout */
481 break;
482 }
483 }
484
485 void __attribute__((destructor)) lttng_ust_exit(void)
486 {
487 int ret;
488
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 */
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
504 ret = pthread_cancel(global_apps.ust_listener);
505 if (ret) {
506 ERR("Error cancelling global ust listener thread");
507 }
508
509 cleanup_sock_info(&global_apps);
510
511 ret = pthread_cancel(local_apps.ust_listener);
512 if (ret) {
513 ERR("Error cancelling local ust listener thread");
514 }
515
516 cleanup_sock_info(&local_apps);
517
518 lttng_ust_abi_exit();
519 ltt_events_exit();
520 ltt_ring_buffer_client_discard_exit();
521 ltt_ring_buffer_client_overwrite_exit();
522 ltt_ring_buffer_metadata_client_exit();
523 exit_tracepoint();
524 }
This page took 0.04099 seconds and 5 git commands to generate.