Add register done command
[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
11ff9c7d
MD
342static
343int get_timeout(struct timespec *constructor_timeout)
344{
345 struct timespec constructor_delay =
346 {
347 .tv_sec = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_S,
348 .tv_nsec = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_NS,
349 };
350 struct timespec realtime;
351 int ret;
352
353 ret = clock_gettime(CLOCK_REALTIME, &realtime);
354 if (ret)
355 return ret;
356
357 constructor_timeout->tv_sec =
358 realtime.tv_sec + constructor_delay.tv_sec;
359 constructor_timeout->tv_nsec =
360 constructor_delay.tv_nsec + realtime.tv_nsec;
361 if (constructor_timeout->tv_nsec >= 1000000000UL) {
362 constructor_timeout->tv_sec++;
363 constructor_timeout->tv_nsec -= 1000000000UL;
364 }
365 return 0;
366}
d9e99d10 367
2691221a
MD
368/*
369 * sessiond monitoring thread: monitor presence of global and per-user
370 * sessiond by polling the application common named pipe.
371 */
372/* TODO */
373
374void __attribute__((constructor)) lttng_ust_comm_init(void)
375{
11ff9c7d 376 struct timespec constructor_timeout;
2691221a
MD
377 int ret;
378
379 init_usterr();
380
11ff9c7d
MD
381 ret = get_timeout(&constructor_timeout);
382 assert(!ret);
383
384 ret = sem_init(&constructor_wait, 0, 2);
385 assert(!ret);
386
9eb62b9c 387 ret = setup_local_apps_socket();
2691221a 388 if (ret) {
9eb62b9c 389 ERR("Error setting up to local apps socket");
2691221a 390 }
11ff9c7d
MD
391
392 /*
393 * Wait for the pthread cond to let us continue to main program
394 * execution. Hold mutex across thread creation, so we start
395 * waiting for the condition before the threads can signal its
396 * completion.
397 */
398 pthread_mutex_lock(&lttng_ust_comm_mutex);
1ea11eab
MD
399 ret = pthread_create(&global_apps.ust_listener, NULL,
400 ust_listener_thread, &global_apps);
1ea11eab
MD
401 ret = pthread_create(&local_apps.ust_listener, NULL,
402 ust_listener_thread, &local_apps);
11ff9c7d
MD
403
404 ret = sem_timedwait(&constructor_wait, &constructor_timeout);
405 if (ret < 0 && errno == ETIMEDOUT) {
406 ERR("Timed out waiting for ltt-sessiond");
407 } else {
408 assert(!ret);
409 }
410 pthread_mutex_unlock(&lttng_ust_comm_mutex);
411
2691221a
MD
412}
413
414void __attribute__((destructor)) lttng_ust_comm_exit(void)
415{
416 int ret;
417
9eb62b9c
MD
418 /*
419 * Using pthread_cancel here because:
420 * A) we don't want to hang application teardown.
421 * B) the thread is not allocating any resource.
422 */
1ea11eab
MD
423
424 /*
425 * Require the communication thread to quit. Synchronize with
426 * mutexes to ensure it is not in a mutex critical section when
427 * pthread_cancel is later called.
428 */
429 pthread_mutex_lock(&lttng_ust_comm_mutex);
430 lttng_ust_comm_should_quit = 1;
431 pthread_mutex_unlock(&lttng_ust_comm_mutex);
432
433#if 0
434 ret = pthread_cancel(global_apps.ust_listener);
9eb62b9c
MD
435 if (ret) {
436 ERR("Error cancelling global ust listener thread");
2691221a 437 }
1ea11eab 438#endif //0
46050b1a
MD
439
440 cleanup_sock_info(&global_apps);
1ea11eab
MD
441
442 ret = pthread_cancel(local_apps.ust_listener);
9eb62b9c
MD
443 if (ret) {
444 ERR("Error cancelling local ust listener thread");
2691221a 445 }
1ea11eab 446
46050b1a 447 cleanup_sock_info(&local_apps);
1ea11eab 448
b35d179d 449 lttng_ust_abi_exit();
1ea11eab 450 ltt_events_exit();
2691221a 451}
This page took 0.04393 seconds and 4 git commands to generate.