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