Add ppid field to registration
[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 char name[16]; /* process name */
128 } reg_msg;
129
130 reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR;
131 reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR;
132 reg_msg.pid = getpid();
133 reg_msg.ppid = getppid();
134 reg_msg.uid = getuid();
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 }
140
141 ret = lttcomm_send_unix_sock(socket, &reg_msg, sizeof(reg_msg));
142 if (ret >= 0 && ret != sizeof(reg_msg))
143 return -EIO;
144 return ret;
145 }
146
147 static
148 int send_reply(int sock, struct lttcomm_ust_reply *lur)
149 {
150 ssize_t len;
151
152 len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur));
153 switch (len) {
154 case sizeof(*lur):
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
170 int handle_register_done(struct sock_info *sock_info)
171 {
172 int ret;
173
174 if (sock_info->constructor_sem_posted)
175 return 0;
176 sock_info->constructor_sem_posted = 1;
177 ret = uatomic_add_return(&sem_count, -1);
178 if (ret == 0) {
179 ret = sem_post(&constructor_wait);
180 assert(!ret);
181 }
182 return 0;
183 }
184
185 static
186 int handle_message(struct sock_info *sock_info,
187 int sock, struct lttcomm_ust_msg *lum)
188 {
189 int ret = 0;
190 const struct objd_ops *ops;
191 struct lttcomm_ust_reply lur;
192
193 pthread_mutex_lock(&lttng_ust_comm_mutex);
194
195 memset(&lur, 0, sizeof(lur));
196
197 if (lttng_ust_comm_should_quit) {
198 ret = -EPERM;
199 goto end;
200 }
201
202 ops = objd_ops(lum->handle);
203 if (!ops) {
204 ret = -ENOENT;
205 goto end;
206 }
207
208 switch (lum->cmd) {
209 case LTTNG_UST_REGISTER_DONE:
210 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
211 ret = handle_register_done(sock_info);
212 else
213 ret = -EINVAL;
214 break;
215 case LTTNG_UST_RELEASE:
216 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
217 ret = -EPERM;
218 else
219 ret = objd_unref(lum->handle);
220 break;
221 default:
222 if (ops->cmd)
223 ret = ops->cmd(lum->handle, lum->cmd,
224 (unsigned long) &lum->u);
225 else
226 ret = -ENOSYS;
227 break;
228 }
229
230 end:
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
241 pthread_mutex_unlock(&lttng_ust_comm_mutex);
242 return ret;
243 }
244
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
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 */
273 static
274 void *ust_listener_thread(void *arg)
275 {
276 struct sock_info *sock_info = arg;
277 int sock, ret;
278
279 /* Restart trying to connect to the session daemon */
280 restart:
281 pthread_mutex_lock(&lttng_ust_comm_mutex);
282
283 if (lttng_ust_comm_should_quit) {
284 pthread_mutex_unlock(&lttng_ust_comm_mutex);
285 goto quit;
286 }
287
288 if (sock_info->socket != -1) {
289 ret = close(sock_info->socket);
290 if (ret) {
291 ERR("Error closing %s apps socket", sock_info->name);
292 }
293 sock_info->socket = -1;
294 }
295
296 /* Check for sessiond availability with pipe TODO */
297
298 /* Register */
299 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
300 if (ret < 0) {
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 */
306 ret = handle_register_done(sock_info);
307 assert(!ret);
308 pthread_mutex_unlock(&lttng_ust_comm_mutex);
309 sleep(5);
310 goto restart;
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(&lttng_ust_comm_mutex);
324 goto quit;
325 }
326 sock_info->root_handle = ret;
327 }
328
329 ret = register_app_to_sessiond(sock);
330 if (ret < 0) {
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 */
336 ret = handle_register_done(sock_info);
337 assert(!ret);
338 pthread_mutex_unlock(&lttng_ust_comm_mutex);
339 sleep(5);
340 goto restart;
341 }
342 pthread_mutex_unlock(&lttng_ust_comm_mutex);
343
344 for (;;) {
345 ssize_t len;
346 struct lttcomm_ust_msg lum;
347
348 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
349 switch (len) {
350 case 0: /* orderly shutdown */
351 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name);
352 goto end;
353 case sizeof(lum):
354 DBG("message received\n");
355 ret = handle_message(sock_info, sock, &lum);
356 if (ret < 0) {
357 ERR("Error handling message for %s socket", sock_info->name);
358 }
359 continue;
360 case -1:
361 if (errno == ECONNRESET) {
362 ERR("%s remote end closed connection\n", sock_info->name);
363 goto end;
364 }
365 goto end;
366 default:
367 ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len);
368 continue;
369 }
370
371 }
372 end:
373 goto restart; /* try to reconnect */
374 quit:
375 return NULL;
376 }
377
378 /*
379 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
380 */
381 static
382 int get_timeout(struct timespec *constructor_timeout)
383 {
384 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
385 char *str_delay;
386 int ret;
387
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 }
408 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
409 constructor_timeout->tv_nsec +=
410 (constructor_delay_ms % 1000UL) * 1000000UL;
411 if (constructor_timeout->tv_nsec >= 1000000000UL) {
412 constructor_timeout->tv_sec++;
413 constructor_timeout->tv_nsec -= 1000000000UL;
414 }
415 return 1;
416 }
417
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
424 void __attribute__((constructor)) lttng_ust_init(void)
425 {
426 struct timespec constructor_timeout;
427 int timeout_mode;
428 int ret;
429
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 */
439 init_usterr();
440 init_tracepoint();
441 ltt_ring_buffer_metadata_client_init();
442 ltt_ring_buffer_client_overwrite_init();
443 ltt_ring_buffer_client_discard_init();
444
445 timeout_mode = get_timeout(&constructor_timeout);
446
447 ret = sem_init(&constructor_wait, 0, 0);
448 assert(!ret);
449
450 ret = setup_local_apps_socket();
451 if (ret) {
452 ERR("Error setting up to local apps socket");
453 }
454
455 ret = pthread_create(&global_apps.ust_listener, NULL,
456 ust_listener_thread, &global_apps);
457 ret = pthread_create(&local_apps.ust_listener, NULL,
458 ust_listener_thread, &local_apps);
459
460 switch (timeout_mode) {
461 case 1: /* timeout wait */
462 do {
463 ret = sem_timedwait(&constructor_wait,
464 &constructor_timeout);
465 } while (ret < 0 && errno == EINTR);
466 if (ret < 0 && errno == ETIMEDOUT) {
467 ERR("Timed out waiting for ltt-sessiond");
468 } else {
469 assert(!ret);
470 }
471 break;
472 case -1:/* wait forever */
473 do {
474 ret = sem_wait(&constructor_wait);
475 } while (ret < 0 && errno == EINTR);
476 assert(!ret);
477 break;
478 case 0: /* no timeout */
479 break;
480 }
481 }
482
483 void __attribute__((destructor)) lttng_ust_exit(void)
484 {
485 int ret;
486
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 */
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(&lttng_ust_comm_mutex);
499 lttng_ust_comm_should_quit = 1;
500 pthread_mutex_unlock(&lttng_ust_comm_mutex);
501
502 #if 0
503 ret = pthread_cancel(global_apps.ust_listener);
504 if (ret) {
505 ERR("Error cancelling global ust listener thread");
506 }
507 #endif //0
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.041525 seconds and 5 git commands to generate.