| 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, ®_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 | if (ret == 0) { |
| 169 | ret = sem_post(&constructor_wait); |
| 170 | assert(!ret); |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static |
| 176 | int handle_message(struct sock_info *sock_info, |
| 177 | int sock, struct lttcomm_ust_msg *lum) |
| 178 | { |
| 179 | int ret = 0; |
| 180 | const struct objd_ops *ops; |
| 181 | struct lttcomm_ust_reply lur; |
| 182 | |
| 183 | pthread_mutex_lock(<tng_ust_comm_mutex); |
| 184 | |
| 185 | memset(&lur, 0, sizeof(lur)); |
| 186 | |
| 187 | if (lttng_ust_comm_should_quit) { |
| 188 | ret = -EPERM; |
| 189 | goto end; |
| 190 | } |
| 191 | |
| 192 | ops = objd_ops(lum->handle); |
| 193 | if (!ops) { |
| 194 | ret = -ENOENT; |
| 195 | goto end; |
| 196 | } |
| 197 | |
| 198 | switch (lum->cmd) { |
| 199 | case LTTNG_UST_REGISTER_DONE: |
| 200 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) |
| 201 | ret = handle_register_done(sock_info); |
| 202 | else |
| 203 | ret = -EINVAL; |
| 204 | break; |
| 205 | case LTTNG_UST_RELEASE: |
| 206 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) |
| 207 | ret = -EPERM; |
| 208 | else |
| 209 | ret = objd_unref(lum->handle); |
| 210 | break; |
| 211 | default: |
| 212 | if (ops->cmd) |
| 213 | ret = ops->cmd(lum->handle, lum->cmd, |
| 214 | (unsigned long) &lum->u); |
| 215 | else |
| 216 | ret = -ENOSYS; |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | end: |
| 221 | lur.handle = lum->handle; |
| 222 | lur.cmd = lum->cmd; |
| 223 | lur.ret_val = ret; |
| 224 | if (ret >= 0) { |
| 225 | lur.ret_code = LTTCOMM_OK; |
| 226 | } else { |
| 227 | lur.ret_code = LTTCOMM_SESSION_FAIL; |
| 228 | } |
| 229 | ret = send_reply(sock, &lur); |
| 230 | |
| 231 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | static |
| 236 | void cleanup_sock_info(struct sock_info *sock_info) |
| 237 | { |
| 238 | int ret; |
| 239 | |
| 240 | if (sock_info->socket != -1) { |
| 241 | ret = close(sock_info->socket); |
| 242 | if (ret) { |
| 243 | ERR("Error closing local apps socket"); |
| 244 | } |
| 245 | sock_info->socket = -1; |
| 246 | } |
| 247 | if (sock_info->root_handle != -1) { |
| 248 | ret = objd_unref(sock_info->root_handle); |
| 249 | if (ret) { |
| 250 | ERR("Error unref root handle"); |
| 251 | } |
| 252 | sock_info->root_handle = -1; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * This thread does not allocate any resource, except within |
| 258 | * handle_message, within mutex protection. This mutex protects against |
| 259 | * fork and exit. |
| 260 | * The other moment it allocates resources is at socket connexion, which |
| 261 | * is also protected by the mutex. |
| 262 | */ |
| 263 | static |
| 264 | void *ust_listener_thread(void *arg) |
| 265 | { |
| 266 | struct sock_info *sock_info = arg; |
| 267 | int sock, ret; |
| 268 | |
| 269 | /* Restart trying to connect to the session daemon */ |
| 270 | restart: |
| 271 | pthread_mutex_lock(<tng_ust_comm_mutex); |
| 272 | |
| 273 | if (lttng_ust_comm_should_quit) { |
| 274 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
| 275 | goto quit; |
| 276 | } |
| 277 | |
| 278 | if (sock_info->socket != -1) { |
| 279 | ret = close(sock_info->socket); |
| 280 | if (ret) { |
| 281 | ERR("Error closing %s apps socket", sock_info->name); |
| 282 | } |
| 283 | sock_info->socket = -1; |
| 284 | } |
| 285 | |
| 286 | /* Check for sessiond availability with pipe TODO */ |
| 287 | |
| 288 | /* Register */ |
| 289 | ret = lttcomm_connect_unix_sock(sock_info->sock_path); |
| 290 | if (ret < 0) { |
| 291 | ERR("Error connecting to %s apps socket", sock_info->name); |
| 292 | /* |
| 293 | * If we cannot find the sessiond daemon, don't delay |
| 294 | * constructor execution. |
| 295 | */ |
| 296 | ret = handle_register_done(sock_info); |
| 297 | assert(!ret); |
| 298 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
| 299 | sleep(5); |
| 300 | goto restart; |
| 301 | } |
| 302 | |
| 303 | sock_info->socket = sock = ret; |
| 304 | |
| 305 | /* |
| 306 | * Create only one root handle per listener thread for the whole |
| 307 | * process lifetime. |
| 308 | */ |
| 309 | if (sock_info->root_handle == -1) { |
| 310 | ret = lttng_abi_create_root_handle(); |
| 311 | if (ret) { |
| 312 | ERR("Error creating root handle"); |
| 313 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
| 314 | goto quit; |
| 315 | } |
| 316 | sock_info->root_handle = ret; |
| 317 | } |
| 318 | |
| 319 | ret = register_app_to_sessiond(sock); |
| 320 | if (ret < 0) { |
| 321 | ERR("Error registering to %s apps socket", sock_info->name); |
| 322 | /* |
| 323 | * If we cannot register to the sessiond daemon, don't |
| 324 | * delay constructor execution. |
| 325 | */ |
| 326 | ret = handle_register_done(sock_info); |
| 327 | assert(!ret); |
| 328 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
| 329 | sleep(5); |
| 330 | goto restart; |
| 331 | } |
| 332 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
| 333 | |
| 334 | for (;;) { |
| 335 | ssize_t len; |
| 336 | struct lttcomm_ust_msg lum; |
| 337 | |
| 338 | len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum)); |
| 339 | switch (len) { |
| 340 | case 0: /* orderly shutdown */ |
| 341 | DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name); |
| 342 | goto end; |
| 343 | case sizeof(lum): |
| 344 | DBG("message received\n"); |
| 345 | ret = handle_message(sock_info, sock, &lum); |
| 346 | if (ret < 0) { |
| 347 | ERR("Error handling message for %s socket", sock_info->name); |
| 348 | } |
| 349 | continue; |
| 350 | case -1: |
| 351 | if (errno == ECONNRESET) { |
| 352 | ERR("%s remote end closed connection\n", sock_info->name); |
| 353 | goto end; |
| 354 | } |
| 355 | goto end; |
| 356 | default: |
| 357 | ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len); |
| 358 | continue; |
| 359 | } |
| 360 | |
| 361 | } |
| 362 | end: |
| 363 | goto restart; /* try to reconnect */ |
| 364 | quit: |
| 365 | return NULL; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Return values: -1: don't wait. 0: wait forever. 1: timeout wait. |
| 370 | */ |
| 371 | static |
| 372 | int get_timeout(struct timespec *constructor_timeout) |
| 373 | { |
| 374 | long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS; |
| 375 | char *str_delay; |
| 376 | int ret; |
| 377 | |
| 378 | str_delay = getenv("UST_REGISTER_TIMEOUT"); |
| 379 | if (str_delay) { |
| 380 | constructor_delay_ms = strtol(str_delay, NULL, 10); |
| 381 | } |
| 382 | |
| 383 | switch (constructor_delay_ms) { |
| 384 | case -1:/* fall-through */ |
| 385 | case 0: |
| 386 | return constructor_delay_ms; |
| 387 | default: |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * If we are unable to find the current time, don't wait. |
| 393 | */ |
| 394 | ret = clock_gettime(CLOCK_REALTIME, constructor_timeout); |
| 395 | if (ret) { |
| 396 | return -1; |
| 397 | } |
| 398 | constructor_timeout->tv_sec += constructor_delay_ms / 1000UL; |
| 399 | constructor_timeout->tv_nsec += |
| 400 | (constructor_delay_ms % 1000UL) * 1000000UL; |
| 401 | if (constructor_timeout->tv_nsec >= 1000000000UL) { |
| 402 | constructor_timeout->tv_sec++; |
| 403 | constructor_timeout->tv_nsec -= 1000000000UL; |
| 404 | } |
| 405 | return 1; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * sessiond monitoring thread: monitor presence of global and per-user |
| 410 | * sessiond by polling the application common named pipe. |
| 411 | */ |
| 412 | /* TODO */ |
| 413 | |
| 414 | void __attribute__((constructor)) lttng_ust_init(void) |
| 415 | { |
| 416 | struct timespec constructor_timeout; |
| 417 | int timeout_mode; |
| 418 | int ret; |
| 419 | |
| 420 | if (uatomic_xchg(&initialized, 1) == 1) |
| 421 | return; |
| 422 | |
| 423 | /* |
| 424 | * We want precise control over the order in which we construct |
| 425 | * our sub-libraries vs starting to receive commands from |
| 426 | * sessiond (otherwise leading to errors when trying to create |
| 427 | * sessiond before the init functions are completed). |
| 428 | */ |
| 429 | init_usterr(); |
| 430 | init_tracepoint(); |
| 431 | ltt_ring_buffer_metadata_client_init(); |
| 432 | ltt_ring_buffer_client_overwrite_init(); |
| 433 | ltt_ring_buffer_client_discard_init(); |
| 434 | |
| 435 | timeout_mode = get_timeout(&constructor_timeout); |
| 436 | |
| 437 | ret = sem_init(&constructor_wait, 0, 0); |
| 438 | assert(!ret); |
| 439 | |
| 440 | ret = setup_local_apps_socket(); |
| 441 | if (ret) { |
| 442 | ERR("Error setting up to local apps socket"); |
| 443 | } |
| 444 | |
| 445 | ret = pthread_create(&global_apps.ust_listener, NULL, |
| 446 | ust_listener_thread, &global_apps); |
| 447 | ret = pthread_create(&local_apps.ust_listener, NULL, |
| 448 | ust_listener_thread, &local_apps); |
| 449 | |
| 450 | switch (timeout_mode) { |
| 451 | case 1: /* timeout wait */ |
| 452 | do { |
| 453 | ret = sem_timedwait(&constructor_wait, |
| 454 | &constructor_timeout); |
| 455 | } while (ret < 0 && errno == EINTR); |
| 456 | if (ret < 0 && errno == ETIMEDOUT) { |
| 457 | ERR("Timed out waiting for ltt-sessiond"); |
| 458 | } else { |
| 459 | assert(!ret); |
| 460 | } |
| 461 | break; |
| 462 | case -1:/* wait forever */ |
| 463 | do { |
| 464 | ret = sem_wait(&constructor_wait); |
| 465 | } while (ret < 0 && errno == EINTR); |
| 466 | assert(!ret); |
| 467 | break; |
| 468 | case 0: /* no timeout */ |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | void __attribute__((destructor)) lttng_ust_exit(void) |
| 474 | { |
| 475 | int ret; |
| 476 | |
| 477 | /* |
| 478 | * Using pthread_cancel here because: |
| 479 | * A) we don't want to hang application teardown. |
| 480 | * B) the thread is not allocating any resource. |
| 481 | */ |
| 482 | |
| 483 | /* |
| 484 | * Require the communication thread to quit. Synchronize with |
| 485 | * mutexes to ensure it is not in a mutex critical section when |
| 486 | * pthread_cancel is later called. |
| 487 | */ |
| 488 | pthread_mutex_lock(<tng_ust_comm_mutex); |
| 489 | lttng_ust_comm_should_quit = 1; |
| 490 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
| 491 | |
| 492 | #if 0 |
| 493 | ret = pthread_cancel(global_apps.ust_listener); |
| 494 | if (ret) { |
| 495 | ERR("Error cancelling global ust listener thread"); |
| 496 | } |
| 497 | #endif //0 |
| 498 | |
| 499 | cleanup_sock_info(&global_apps); |
| 500 | |
| 501 | ret = pthread_cancel(local_apps.ust_listener); |
| 502 | if (ret) { |
| 503 | ERR("Error cancelling local ust listener thread"); |
| 504 | } |
| 505 | |
| 506 | cleanup_sock_info(&local_apps); |
| 507 | |
| 508 | lttng_ust_abi_exit(); |
| 509 | ltt_events_exit(); |
| 510 | ltt_ring_buffer_client_discard_exit(); |
| 511 | ltt_ring_buffer_client_overwrite_exit(); |
| 512 | ltt_ring_buffer_metadata_client_exit(); |
| 513 | exit_tracepoint(); |
| 514 | } |