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