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