Add env var UST_REGISTER_TIMEOUT
[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 <ust/lttng-ust-abi.h>
27 #include <lttng-ust-comm.h>
28 #include <ust/usterr-signal-safe.h>
29 #include <pthread.h>
30 #include <semaphore.h>
31 #include <time.h>
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 */
38 static pthread_mutex_t lttng_ust_comm_mutex = PTHREAD_MUTEX_INITIALIZER;
39
40 /* Should the ust comm thread quit ? */
41 static int lttng_ust_comm_should_quit;
42
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 */
53 static sem_t constructor_wait;
54
55 /*
56 * Info about socket and associated listener thread.
57 */
58 struct sock_info {
59 const char *name;
60 char sock_path[PATH_MAX];
61 int socket;
62 pthread_t ust_listener; /* listener thread */
63 int root_handle;
64 };
65
66 /* Socket from app (connect) to session daemon (listen) for communication */
67 struct sock_info global_apps = {
68 .name = "global",
69 .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK,
70 .socket = -1,
71 .root_handle = -1,
72 };
73
74 /* TODO: allow global_apps_sock_path override */
75
76 struct sock_info local_apps = {
77 .name = "local",
78 .socket = -1,
79 .root_handle = -1,
80 };
81
82 static
83 int setup_local_apps_socket(void)
84 {
85 const char *home_dir;
86
87 home_dir = (const char *) getenv("HOME");
88 if (!home_dir)
89 return -ENOENT;
90 snprintf(local_apps.sock_path, PATH_MAX,
91 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
92 return 0;
93 }
94
95 static
96 int register_app_to_sessiond(int socket)
97 {
98 ssize_t ret;
99 struct {
100 uint32_t major;
101 uint32_t minor;
102 pid_t pid;
103 uid_t uid;
104 } reg_msg;
105
106 reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR;
107 reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR;
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
117 static
118 int send_reply(int sock, struct lttcomm_ust_reply *lur)
119 {
120 ssize_t len;
121
122 len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur));
123 switch (len) {
124 case sizeof(*lur):
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
139 static
140 int handle_register_done(void)
141 {
142 int ret;
143
144 ret = sem_post(&constructor_wait);
145 assert(!ret);
146 return 0;
147 }
148
149 static
150 int handle_message(struct sock_info *sock_info,
151 int sock, struct lttcomm_ust_msg *lum)
152 {
153 int ret = 0;
154 const struct objd_ops *ops;
155 struct lttcomm_ust_reply lur;
156
157 pthread_mutex_lock(&lttng_ust_comm_mutex);
158
159 memset(&lur, 0, sizeof(lur));
160
161 if (lttng_ust_comm_should_quit) {
162 ret = -EPERM;
163 goto end;
164 }
165
166 ops = objd_ops(lum->handle);
167 if (!ops) {
168 ret = -ENOENT;
169 goto end;
170 }
171
172 switch (lum->cmd) {
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;
179 case LTTNG_UST_RELEASE:
180 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
181 ret = -EPERM;
182 else
183 ret = objd_unref(lum->handle);
184 break;
185 default:
186 if (ops->cmd)
187 ret = ops->cmd(lum->handle, lum->cmd,
188 (unsigned long) &lum->u);
189 else
190 ret = -ENOSYS;
191 break;
192 }
193
194 end:
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
205 pthread_mutex_unlock(&lttng_ust_comm_mutex);
206 return ret;
207 }
208
209 static
210 void 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
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 */
237 static
238 void *ust_listener_thread(void *arg)
239 {
240 struct sock_info *sock_info = arg;
241 int sock, ret;
242
243 /* Restart trying to connect to the session daemon */
244 restart:
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 }
251
252 if (sock_info->socket != -1) {
253 ret = close(sock_info->socket);
254 if (ret) {
255 ERR("Error closing %s apps socket", sock_info->name);
256 }
257 sock_info->socket = -1;
258 }
259
260 /* Check for sessiond availability with pipe TODO */
261
262 /* Register */
263 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
264 if (ret < 0) {
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);
272 pthread_mutex_unlock(&lttng_ust_comm_mutex);
273 sleep(5);
274 goto restart;
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;
291 }
292
293 ret = register_app_to_sessiond(sock);
294 if (ret < 0) {
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);
302 pthread_mutex_unlock(&lttng_ust_comm_mutex);
303 sleep(5);
304 goto restart;
305 }
306 pthread_mutex_unlock(&lttng_ust_comm_mutex);
307
308 for (;;) {
309 ssize_t len;
310 struct lttcomm_ust_msg lum;
311
312 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
313 switch (len) {
314 case 0: /* orderly shutdown */
315 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name);
316 goto end;
317 case sizeof(lum):
318 DBG("message received\n");
319 ret = handle_message(sock_info, sock, &lum);
320 if (ret < 0) {
321 ERR("Error handling message for %s socket", sock_info->name);
322 }
323 continue;
324 case -1:
325 if (errno == ECONNRESET) {
326 ERR("%s remote end closed connection\n", sock_info->name);
327 goto end;
328 }
329 goto end;
330 default:
331 ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len);
332 continue;
333 }
334
335 }
336 end:
337 goto restart; /* try to reconnect */
338 quit:
339 return NULL;
340 }
341
342 /*
343 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
344 */
345 static
346 int get_timeout(struct timespec *constructor_timeout)
347 {
348 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
349 char *str_delay;
350 int ret;
351
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 }
372
373 constructor_timeout->tv_nsec =
374 constructor_timeout->tv_nsec + (constructor_delay_ms * 1000000UL);
375 if (constructor_timeout->tv_nsec >= 1000000000UL) {
376 constructor_timeout->tv_sec++;
377 constructor_timeout->tv_nsec -= 1000000000UL;
378 }
379 return 1;
380 }
381
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
388 void __attribute__((constructor)) lttng_ust_comm_init(void)
389 {
390 struct timespec constructor_timeout;
391 int timeout_mode;
392 int ret;
393
394 init_usterr();
395
396 timeout_mode = get_timeout(&constructor_timeout);
397
398 ret = sem_init(&constructor_wait, 0, 2);
399 assert(!ret);
400
401 ret = setup_local_apps_socket();
402 if (ret) {
403 ERR("Error setting up to local apps socket");
404 }
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);
413 ret = pthread_create(&global_apps.ust_listener, NULL,
414 ust_listener_thread, &global_apps);
415 ret = pthread_create(&local_apps.ust_listener, NULL,
416 ust_listener_thread, &local_apps);
417
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);
429 assert(!ret);
430 break;
431 case -1:/* no timeout */
432 break;
433 }
434 pthread_mutex_unlock(&lttng_ust_comm_mutex);
435 }
436
437 void __attribute__((destructor)) lttng_ust_comm_exit(void)
438 {
439 int ret;
440
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 */
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);
458 if (ret) {
459 ERR("Error cancelling global ust listener thread");
460 }
461 #endif //0
462
463 cleanup_sock_info(&global_apps);
464
465 ret = pthread_cancel(local_apps.ust_listener);
466 if (ret) {
467 ERR("Error cancelling local ust listener thread");
468 }
469
470 cleanup_sock_info(&local_apps);
471
472 lttng_ust_abi_exit();
473 ltt_events_exit();
474 }
This page took 0.03876 seconds and 4 git commands to generate.