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