| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License, version 2 only, |
| 7 | * as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <getopt.h> |
| 21 | #include <grp.h> |
| 22 | #include <limits.h> |
| 23 | #include <pthread.h> |
| 24 | #include <semaphore.h> |
| 25 | #include <signal.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/mman.h> |
| 30 | #include <sys/mount.h> |
| 31 | #include <sys/resource.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/wait.h> |
| 36 | #include <urcu/uatomic.h> |
| 37 | #include <unistd.h> |
| 38 | #include <config.h> |
| 39 | |
| 40 | #include <common/common.h> |
| 41 | #include <common/compat/poll.h> |
| 42 | #include <common/compat/socket.h> |
| 43 | #include <common/defaults.h> |
| 44 | #include <common/kernel-consumer/kernel-consumer.h> |
| 45 | #include <common/futex.h> |
| 46 | #include <common/relayd/relayd.h> |
| 47 | #include <common/utils.h> |
| 48 | |
| 49 | #include "lttng-sessiond.h" |
| 50 | #include "channel.h" |
| 51 | #include "consumer.h" |
| 52 | #include "context.h" |
| 53 | #include "event.h" |
| 54 | #include "kernel.h" |
| 55 | #include "kernel-consumer.h" |
| 56 | #include "modprobe.h" |
| 57 | #include "shm.h" |
| 58 | #include "ust-ctl.h" |
| 59 | #include "ust-consumer.h" |
| 60 | #include "utils.h" |
| 61 | #include "fd-limit.h" |
| 62 | #include "filter.h" |
| 63 | #include "health.h" |
| 64 | |
| 65 | #define CONSUMERD_FILE "lttng-consumerd" |
| 66 | |
| 67 | /* Const values */ |
| 68 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
| 69 | const char default_tracing_group[] = DEFAULT_TRACING_GROUP; |
| 70 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; |
| 71 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; |
| 72 | |
| 73 | const char *progname; |
| 74 | const char *opt_tracing_group; |
| 75 | static int opt_sig_parent; |
| 76 | static int opt_verbose_consumer; |
| 77 | static int opt_daemon; |
| 78 | static int opt_no_kernel; |
| 79 | static int is_root; /* Set to 1 if the daemon is running as root */ |
| 80 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
| 81 | static char *rundir; |
| 82 | |
| 83 | /* Consumer daemon specific control data */ |
| 84 | static struct consumer_data kconsumer_data = { |
| 85 | .type = LTTNG_CONSUMER_KERNEL, |
| 86 | .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH, |
| 87 | .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH, |
| 88 | .err_sock = -1, |
| 89 | .cmd_sock = -1, |
| 90 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
| 91 | .lock = PTHREAD_MUTEX_INITIALIZER, |
| 92 | }; |
| 93 | static struct consumer_data ustconsumer64_data = { |
| 94 | .type = LTTNG_CONSUMER64_UST, |
| 95 | .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, |
| 96 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, |
| 97 | .err_sock = -1, |
| 98 | .cmd_sock = -1, |
| 99 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
| 100 | .lock = PTHREAD_MUTEX_INITIALIZER, |
| 101 | }; |
| 102 | static struct consumer_data ustconsumer32_data = { |
| 103 | .type = LTTNG_CONSUMER32_UST, |
| 104 | .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, |
| 105 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, |
| 106 | .err_sock = -1, |
| 107 | .cmd_sock = -1, |
| 108 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
| 109 | .lock = PTHREAD_MUTEX_INITIALIZER, |
| 110 | }; |
| 111 | |
| 112 | /* Shared between threads */ |
| 113 | static int dispatch_thread_exit; |
| 114 | |
| 115 | /* Global application Unix socket path */ |
| 116 | static char apps_unix_sock_path[PATH_MAX]; |
| 117 | /* Global client Unix socket path */ |
| 118 | static char client_unix_sock_path[PATH_MAX]; |
| 119 | /* global wait shm path for UST */ |
| 120 | static char wait_shm_path[PATH_MAX]; |
| 121 | /* Global health check unix path */ |
| 122 | static char health_unix_sock_path[PATH_MAX]; |
| 123 | |
| 124 | /* Sockets and FDs */ |
| 125 | static int client_sock = -1; |
| 126 | static int apps_sock = -1; |
| 127 | static int kernel_tracer_fd = -1; |
| 128 | static int kernel_poll_pipe[2] = { -1, -1 }; |
| 129 | |
| 130 | /* |
| 131 | * Quit pipe for all threads. This permits a single cancellation point |
| 132 | * for all threads when receiving an event on the pipe. |
| 133 | */ |
| 134 | static int thread_quit_pipe[2] = { -1, -1 }; |
| 135 | |
| 136 | /* |
| 137 | * This pipe is used to inform the thread managing application communication |
| 138 | * that a command is queued and ready to be processed. |
| 139 | */ |
| 140 | static int apps_cmd_pipe[2] = { -1, -1 }; |
| 141 | |
| 142 | /* Pthread, Mutexes and Semaphores */ |
| 143 | static pthread_t apps_thread; |
| 144 | static pthread_t reg_apps_thread; |
| 145 | static pthread_t client_thread; |
| 146 | static pthread_t kernel_thread; |
| 147 | static pthread_t dispatch_thread; |
| 148 | static pthread_t health_thread; |
| 149 | |
| 150 | /* |
| 151 | * UST registration command queue. This queue is tied with a futex and uses a N |
| 152 | * wakers / 1 waiter implemented and detailed in futex.c/.h |
| 153 | * |
| 154 | * The thread_manage_apps and thread_dispatch_ust_registration interact with |
| 155 | * this queue and the wait/wake scheme. |
| 156 | */ |
| 157 | static struct ust_cmd_queue ust_cmd_queue; |
| 158 | |
| 159 | /* |
| 160 | * Pointer initialized before thread creation. |
| 161 | * |
| 162 | * This points to the tracing session list containing the session count and a |
| 163 | * mutex lock. The lock MUST be taken if you iterate over the list. The lock |
| 164 | * MUST NOT be taken if you call a public function in session.c. |
| 165 | * |
| 166 | * The lock is nested inside the structure: session_list_ptr->lock. Please use |
| 167 | * session_lock_list and session_unlock_list for lock acquisition. |
| 168 | */ |
| 169 | static struct ltt_session_list *session_list_ptr; |
| 170 | |
| 171 | int ust_consumerd64_fd = -1; |
| 172 | int ust_consumerd32_fd = -1; |
| 173 | |
| 174 | static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN; |
| 175 | static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN; |
| 176 | static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR; |
| 177 | static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR; |
| 178 | |
| 179 | /* |
| 180 | * Consumer daemon state which is changed when spawning it, killing it or in |
| 181 | * case of a fatal error. |
| 182 | */ |
| 183 | enum consumerd_state { |
| 184 | CONSUMER_STARTED = 1, |
| 185 | CONSUMER_STOPPED = 2, |
| 186 | CONSUMER_ERROR = 3, |
| 187 | }; |
| 188 | |
| 189 | /* |
| 190 | * This consumer daemon state is used to validate if a client command will be |
| 191 | * able to reach the consumer. If not, the client is informed. For instance, |
| 192 | * doing a "lttng start" when the consumer state is set to ERROR will return an |
| 193 | * error to the client. |
| 194 | * |
| 195 | * The following example shows a possible race condition of this scheme: |
| 196 | * |
| 197 | * consumer thread error happens |
| 198 | * client cmd arrives |
| 199 | * client cmd checks state -> still OK |
| 200 | * consumer thread exit, sets error |
| 201 | * client cmd try to talk to consumer |
| 202 | * ... |
| 203 | * |
| 204 | * However, since the consumer is a different daemon, we have no way of making |
| 205 | * sure the command will reach it safely even with this state flag. This is why |
| 206 | * we consider that up to the state validation during command processing, the |
| 207 | * command is safe. After that, we can not guarantee the correctness of the |
| 208 | * client request vis-a-vis the consumer. |
| 209 | */ |
| 210 | static enum consumerd_state ust_consumerd_state; |
| 211 | static enum consumerd_state kernel_consumerd_state; |
| 212 | |
| 213 | /* |
| 214 | * Used to keep a unique index for each relayd socket created where this value |
| 215 | * is associated with streams on the consumer so it can match the right relayd |
| 216 | * to send to. |
| 217 | * |
| 218 | * This value should be incremented atomically for safety purposes and future |
| 219 | * possible concurrent access. |
| 220 | */ |
| 221 | static unsigned int relayd_net_seq_idx; |
| 222 | |
| 223 | /* Used for the health monitoring of the session daemon. See health.h */ |
| 224 | struct health_state health_thread_cmd; |
| 225 | struct health_state health_thread_app_manage; |
| 226 | struct health_state health_thread_app_reg; |
| 227 | struct health_state health_thread_kernel; |
| 228 | |
| 229 | static |
| 230 | void setup_consumerd_path(void) |
| 231 | { |
| 232 | const char *bin, *libdir; |
| 233 | |
| 234 | /* |
| 235 | * Allow INSTALL_BIN_PATH to be used as a target path for the |
| 236 | * native architecture size consumer if CONFIG_CONSUMER*_PATH |
| 237 | * has not been defined. |
| 238 | */ |
| 239 | #if (CAA_BITS_PER_LONG == 32) |
| 240 | if (!consumerd32_bin[0]) { |
| 241 | consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; |
| 242 | } |
| 243 | if (!consumerd32_libdir[0]) { |
| 244 | consumerd32_libdir = INSTALL_LIB_PATH; |
| 245 | } |
| 246 | #elif (CAA_BITS_PER_LONG == 64) |
| 247 | if (!consumerd64_bin[0]) { |
| 248 | consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; |
| 249 | } |
| 250 | if (!consumerd64_libdir[0]) { |
| 251 | consumerd64_libdir = INSTALL_LIB_PATH; |
| 252 | } |
| 253 | #else |
| 254 | #error "Unknown bitness" |
| 255 | #endif |
| 256 | |
| 257 | /* |
| 258 | * runtime env. var. overrides the build default. |
| 259 | */ |
| 260 | bin = getenv("LTTNG_CONSUMERD32_BIN"); |
| 261 | if (bin) { |
| 262 | consumerd32_bin = bin; |
| 263 | } |
| 264 | bin = getenv("LTTNG_CONSUMERD64_BIN"); |
| 265 | if (bin) { |
| 266 | consumerd64_bin = bin; |
| 267 | } |
| 268 | libdir = getenv("LTTNG_CONSUMERD32_LIBDIR"); |
| 269 | if (libdir) { |
| 270 | consumerd32_libdir = libdir; |
| 271 | } |
| 272 | libdir = getenv("LTTNG_CONSUMERD64_LIBDIR"); |
| 273 | if (libdir) { |
| 274 | consumerd64_libdir = libdir; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. |
| 280 | */ |
| 281 | static int create_thread_poll_set(struct lttng_poll_event *events, |
| 282 | unsigned int size) |
| 283 | { |
| 284 | int ret; |
| 285 | |
| 286 | if (events == NULL || size == 0) { |
| 287 | ret = -1; |
| 288 | goto error; |
| 289 | } |
| 290 | |
| 291 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); |
| 292 | if (ret < 0) { |
| 293 | goto error; |
| 294 | } |
| 295 | |
| 296 | /* Add quit pipe */ |
| 297 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN); |
| 298 | if (ret < 0) { |
| 299 | goto error; |
| 300 | } |
| 301 | |
| 302 | return 0; |
| 303 | |
| 304 | error: |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Check if the thread quit pipe was triggered. |
| 310 | * |
| 311 | * Return 1 if it was triggered else 0; |
| 312 | */ |
| 313 | static int check_thread_quit_pipe(int fd, uint32_t events) |
| 314 | { |
| 315 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { |
| 316 | return 1; |
| 317 | } |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Return group ID of the tracing group or -1 if not found. |
| 324 | */ |
| 325 | static gid_t allowed_group(void) |
| 326 | { |
| 327 | struct group *grp; |
| 328 | |
| 329 | if (opt_tracing_group) { |
| 330 | grp = getgrnam(opt_tracing_group); |
| 331 | } else { |
| 332 | grp = getgrnam(default_tracing_group); |
| 333 | } |
| 334 | if (!grp) { |
| 335 | return -1; |
| 336 | } else { |
| 337 | return grp->gr_gid; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Init thread quit pipe. |
| 343 | * |
| 344 | * Return -1 on error or 0 if all pipes are created. |
| 345 | */ |
| 346 | static int init_thread_quit_pipe(void) |
| 347 | { |
| 348 | int ret, i; |
| 349 | |
| 350 | ret = pipe(thread_quit_pipe); |
| 351 | if (ret < 0) { |
| 352 | PERROR("thread quit pipe"); |
| 353 | goto error; |
| 354 | } |
| 355 | |
| 356 | for (i = 0; i < 2; i++) { |
| 357 | ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC); |
| 358 | if (ret < 0) { |
| 359 | PERROR("fcntl"); |
| 360 | goto error; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | error: |
| 365 | return ret; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Complete teardown of a kernel session. This free all data structure related |
| 370 | * to a kernel session and update counter. |
| 371 | */ |
| 372 | static void teardown_kernel_session(struct ltt_session *session) |
| 373 | { |
| 374 | int ret; |
| 375 | struct lttng_ht_iter iter; |
| 376 | struct ltt_kernel_session *ksess; |
| 377 | struct consumer_socket *socket; |
| 378 | |
| 379 | if (!session->kernel_session) { |
| 380 | DBG3("No kernel session when tearing down session"); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | ksess = session->kernel_session; |
| 385 | |
| 386 | DBG("Tearing down kernel session"); |
| 387 | |
| 388 | /* |
| 389 | * Destroy relayd associated with the session consumer. This action is |
| 390 | * valid since in order to destroy a session we must acquire the session |
| 391 | * lock. This means that there CAN NOT be stream(s) being sent to a |
| 392 | * consumer since this action also requires the session lock at any time. |
| 393 | * |
| 394 | * At this point, we are sure that not streams data will be lost after this |
| 395 | * command is issued. |
| 396 | */ |
| 397 | if (ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET) { |
| 398 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, socket, |
| 399 | node.node) { |
| 400 | ret = consumer_send_destroy_relayd(socket, ksess->consumer); |
| 401 | if (ret < 0) { |
| 402 | ERR("Unable to send destroy relayd command to consumer"); |
| 403 | /* Continue since we MUST delete everything at this point. */ |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * If a custom kernel consumer was registered, close the socket before |
| 410 | * tearing down the complete kernel session structure |
| 411 | */ |
| 412 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, socket, |
| 413 | node.node) { |
| 414 | if (socket->fd != kconsumer_data.cmd_sock) { |
| 415 | rcu_read_lock(); |
| 416 | consumer_del_socket(socket, ksess->consumer); |
| 417 | lttcomm_close_unix_sock(socket->fd); |
| 418 | consumer_destroy_socket(socket); |
| 419 | rcu_read_unlock(); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | trace_kernel_destroy_session(ksess); |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Complete teardown of all UST sessions. This will free everything on his path |
| 428 | * and destroy the core essence of all ust sessions :) |
| 429 | */ |
| 430 | static void teardown_ust_session(struct ltt_session *session) |
| 431 | { |
| 432 | int ret; |
| 433 | struct lttng_ht_iter iter; |
| 434 | struct ltt_ust_session *usess; |
| 435 | struct consumer_socket *socket; |
| 436 | |
| 437 | if (!session->ust_session) { |
| 438 | DBG3("No UST session when tearing down session"); |
| 439 | return; |
| 440 | } |
| 441 | usess = session->ust_session; |
| 442 | |
| 443 | DBG("Tearing down UST session(s)"); |
| 444 | |
| 445 | /* |
| 446 | * Destroy relayd associated with the session consumer. This action is |
| 447 | * valid since in order to destroy a session we must acquire the session |
| 448 | * lock. This means that there CAN NOT be stream(s) being sent to a |
| 449 | * consumer since this action also requires the session lock at any time. |
| 450 | * |
| 451 | * At this point, we are sure that no data will be lost after this command |
| 452 | * is issued. |
| 453 | */ |
| 454 | if (usess->consumer && usess->consumer->type == CONSUMER_DST_NET) { |
| 455 | cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter, socket, |
| 456 | node.node) { |
| 457 | ret = consumer_send_destroy_relayd(socket, usess->consumer); |
| 458 | if (ret < 0) { |
| 459 | ERR("Unable to send destroy relayd command to consumer"); |
| 460 | /* Continue since we MUST delete everything at this point. */ |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | ret = ust_app_destroy_trace_all(usess); |
| 466 | if (ret) { |
| 467 | ERR("Error in ust_app_destroy_trace_all"); |
| 468 | } |
| 469 | |
| 470 | trace_ust_destroy_session(usess); |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * Stop all threads by closing the thread quit pipe. |
| 475 | */ |
| 476 | static void stop_threads(void) |
| 477 | { |
| 478 | int ret; |
| 479 | |
| 480 | /* Stopping all threads */ |
| 481 | DBG("Terminating all threads"); |
| 482 | ret = notify_thread_pipe(thread_quit_pipe[1]); |
| 483 | if (ret < 0) { |
| 484 | ERR("write error on thread quit pipe"); |
| 485 | } |
| 486 | |
| 487 | /* Dispatch thread */ |
| 488 | CMM_STORE_SHARED(dispatch_thread_exit, 1); |
| 489 | futex_nto1_wake(&ust_cmd_queue.futex); |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Cleanup the daemon |
| 494 | */ |
| 495 | static void cleanup(void) |
| 496 | { |
| 497 | int ret; |
| 498 | char *cmd; |
| 499 | struct ltt_session *sess, *stmp; |
| 500 | |
| 501 | DBG("Cleaning up"); |
| 502 | |
| 503 | DBG("Removing %s directory", rundir); |
| 504 | ret = asprintf(&cmd, "rm -rf %s", rundir); |
| 505 | if (ret < 0) { |
| 506 | ERR("asprintf failed. Something is really wrong!"); |
| 507 | } |
| 508 | |
| 509 | /* Remove lttng run directory */ |
| 510 | ret = system(cmd); |
| 511 | if (ret < 0) { |
| 512 | ERR("Unable to clean %s", rundir); |
| 513 | } |
| 514 | free(cmd); |
| 515 | |
| 516 | DBG("Cleaning up all sessions"); |
| 517 | |
| 518 | /* Destroy session list mutex */ |
| 519 | if (session_list_ptr != NULL) { |
| 520 | pthread_mutex_destroy(&session_list_ptr->lock); |
| 521 | |
| 522 | /* Cleanup ALL session */ |
| 523 | cds_list_for_each_entry_safe(sess, stmp, |
| 524 | &session_list_ptr->head, list) { |
| 525 | teardown_kernel_session(sess); |
| 526 | teardown_ust_session(sess); |
| 527 | free(sess); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | DBG("Closing all UST sockets"); |
| 532 | ust_app_clean_list(); |
| 533 | |
| 534 | if (is_root && !opt_no_kernel) { |
| 535 | DBG2("Closing kernel fd"); |
| 536 | if (kernel_tracer_fd >= 0) { |
| 537 | ret = close(kernel_tracer_fd); |
| 538 | if (ret) { |
| 539 | PERROR("close"); |
| 540 | } |
| 541 | } |
| 542 | DBG("Unloading kernel modules"); |
| 543 | modprobe_remove_lttng_all(); |
| 544 | } |
| 545 | utils_close_pipe(kernel_poll_pipe); |
| 546 | utils_close_pipe(thread_quit_pipe); |
| 547 | utils_close_pipe(apps_cmd_pipe); |
| 548 | |
| 549 | /* <fun> */ |
| 550 | DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm" |
| 551 | "Matthew, BEET driven development works!%c[%dm", |
| 552 | 27, 1, 31, 27, 0, 27, 1, 33, 27, 0); |
| 553 | /* </fun> */ |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * Send data on a unix socket using the liblttsessiondcomm API. |
| 558 | * |
| 559 | * Return lttcomm error code. |
| 560 | */ |
| 561 | static int send_unix_sock(int sock, void *buf, size_t len) |
| 562 | { |
| 563 | /* Check valid length */ |
| 564 | if (len <= 0) { |
| 565 | return -1; |
| 566 | } |
| 567 | |
| 568 | return lttcomm_send_unix_sock(sock, buf, len); |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Free memory of a command context structure. |
| 573 | */ |
| 574 | static void clean_command_ctx(struct command_ctx **cmd_ctx) |
| 575 | { |
| 576 | DBG("Clean command context structure"); |
| 577 | if (*cmd_ctx) { |
| 578 | if ((*cmd_ctx)->llm) { |
| 579 | free((*cmd_ctx)->llm); |
| 580 | } |
| 581 | if ((*cmd_ctx)->lsm) { |
| 582 | free((*cmd_ctx)->lsm); |
| 583 | } |
| 584 | free(*cmd_ctx); |
| 585 | *cmd_ctx = NULL; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Notify UST applications using the shm mmap futex. |
| 591 | */ |
| 592 | static int notify_ust_apps(int active) |
| 593 | { |
| 594 | char *wait_shm_mmap; |
| 595 | |
| 596 | DBG("Notifying applications of session daemon state: %d", active); |
| 597 | |
| 598 | /* See shm.c for this call implying mmap, shm and futex calls */ |
| 599 | wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root); |
| 600 | if (wait_shm_mmap == NULL) { |
| 601 | goto error; |
| 602 | } |
| 603 | |
| 604 | /* Wake waiting process */ |
| 605 | futex_wait_update((int32_t *) wait_shm_mmap, active); |
| 606 | |
| 607 | /* Apps notified successfully */ |
| 608 | return 0; |
| 609 | |
| 610 | error: |
| 611 | return -1; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * Setup the outgoing data buffer for the response (llm) by allocating the |
| 616 | * right amount of memory and copying the original information from the lsm |
| 617 | * structure. |
| 618 | * |
| 619 | * Return total size of the buffer pointed by buf. |
| 620 | */ |
| 621 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
| 622 | { |
| 623 | int ret, buf_size; |
| 624 | |
| 625 | buf_size = size; |
| 626 | |
| 627 | cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size); |
| 628 | if (cmd_ctx->llm == NULL) { |
| 629 | PERROR("zmalloc"); |
| 630 | ret = -ENOMEM; |
| 631 | goto error; |
| 632 | } |
| 633 | |
| 634 | /* Copy common data */ |
| 635 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; |
| 636 | cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid; |
| 637 | |
| 638 | cmd_ctx->llm->data_size = size; |
| 639 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; |
| 640 | |
| 641 | return buf_size; |
| 642 | |
| 643 | error: |
| 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | * Update the kernel poll set of all channel fd available over all tracing |
| 649 | * session. Add the wakeup pipe at the end of the set. |
| 650 | */ |
| 651 | static int update_kernel_poll(struct lttng_poll_event *events) |
| 652 | { |
| 653 | int ret; |
| 654 | struct ltt_session *session; |
| 655 | struct ltt_kernel_channel *channel; |
| 656 | |
| 657 | DBG("Updating kernel poll set"); |
| 658 | |
| 659 | session_lock_list(); |
| 660 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
| 661 | session_lock(session); |
| 662 | if (session->kernel_session == NULL) { |
| 663 | session_unlock(session); |
| 664 | continue; |
| 665 | } |
| 666 | |
| 667 | cds_list_for_each_entry(channel, |
| 668 | &session->kernel_session->channel_list.head, list) { |
| 669 | /* Add channel fd to the kernel poll set */ |
| 670 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); |
| 671 | if (ret < 0) { |
| 672 | session_unlock(session); |
| 673 | goto error; |
| 674 | } |
| 675 | DBG("Channel fd %d added to kernel set", channel->fd); |
| 676 | } |
| 677 | session_unlock(session); |
| 678 | } |
| 679 | session_unlock_list(); |
| 680 | |
| 681 | return 0; |
| 682 | |
| 683 | error: |
| 684 | session_unlock_list(); |
| 685 | return -1; |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * Find the channel fd from 'fd' over all tracing session. When found, check |
| 690 | * for new channel stream and send those stream fds to the kernel consumer. |
| 691 | * |
| 692 | * Useful for CPU hotplug feature. |
| 693 | */ |
| 694 | static int update_kernel_stream(struct consumer_data *consumer_data, int fd) |
| 695 | { |
| 696 | int ret = 0; |
| 697 | struct ltt_session *session; |
| 698 | struct ltt_kernel_session *ksess; |
| 699 | struct ltt_kernel_channel *channel; |
| 700 | |
| 701 | DBG("Updating kernel streams for channel fd %d", fd); |
| 702 | |
| 703 | session_lock_list(); |
| 704 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
| 705 | session_lock(session); |
| 706 | if (session->kernel_session == NULL) { |
| 707 | session_unlock(session); |
| 708 | continue; |
| 709 | } |
| 710 | ksess = session->kernel_session; |
| 711 | |
| 712 | cds_list_for_each_entry(channel, &ksess->channel_list.head, list) { |
| 713 | if (channel->fd == fd) { |
| 714 | DBG("Channel found, updating kernel streams"); |
| 715 | ret = kernel_open_channel_stream(channel); |
| 716 | if (ret < 0) { |
| 717 | goto error; |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Have we already sent fds to the consumer? If yes, it means |
| 722 | * that tracing is started so it is safe to send our updated |
| 723 | * stream fds. |
| 724 | */ |
| 725 | if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) { |
| 726 | struct lttng_ht_iter iter; |
| 727 | struct consumer_socket *socket; |
| 728 | |
| 729 | |
| 730 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, |
| 731 | &iter.iter, socket, node.node) { |
| 732 | /* Code flow error */ |
| 733 | assert(socket->fd >= 0); |
| 734 | |
| 735 | pthread_mutex_lock(socket->lock); |
| 736 | ret = kernel_consumer_send_channel_stream(socket->fd, |
| 737 | channel, ksess); |
| 738 | pthread_mutex_unlock(socket->lock); |
| 739 | if (ret < 0) { |
| 740 | goto error; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | goto error; |
| 745 | } |
| 746 | } |
| 747 | session_unlock(session); |
| 748 | } |
| 749 | session_unlock_list(); |
| 750 | return ret; |
| 751 | |
| 752 | error: |
| 753 | session_unlock(session); |
| 754 | session_unlock_list(); |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * For each tracing session, update newly registered apps. |
| 760 | */ |
| 761 | static void update_ust_app(int app_sock) |
| 762 | { |
| 763 | struct ltt_session *sess, *stmp; |
| 764 | |
| 765 | session_lock_list(); |
| 766 | |
| 767 | /* For all tracing session(s) */ |
| 768 | cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) { |
| 769 | session_lock(sess); |
| 770 | if (sess->ust_session) { |
| 771 | ust_app_global_update(sess->ust_session, app_sock); |
| 772 | } |
| 773 | session_unlock(sess); |
| 774 | } |
| 775 | |
| 776 | session_unlock_list(); |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * This thread manage event coming from the kernel. |
| 781 | * |
| 782 | * Features supported in this thread: |
| 783 | * -) CPU Hotplug |
| 784 | */ |
| 785 | static void *thread_manage_kernel(void *data) |
| 786 | { |
| 787 | int ret, i, pollfd, update_poll_flag = 1, err = -1; |
| 788 | uint32_t revents, nb_fd; |
| 789 | char tmp; |
| 790 | struct lttng_poll_event events; |
| 791 | |
| 792 | DBG("Thread manage kernel started"); |
| 793 | |
| 794 | health_code_update(&health_thread_kernel); |
| 795 | |
| 796 | ret = create_thread_poll_set(&events, 2); |
| 797 | if (ret < 0) { |
| 798 | goto error_poll_create; |
| 799 | } |
| 800 | |
| 801 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); |
| 802 | if (ret < 0) { |
| 803 | goto error; |
| 804 | } |
| 805 | |
| 806 | while (1) { |
| 807 | health_code_update(&health_thread_kernel); |
| 808 | |
| 809 | if (update_poll_flag == 1) { |
| 810 | /* |
| 811 | * Reset number of fd in the poll set. Always 2 since there is the thread |
| 812 | * quit pipe and the kernel pipe. |
| 813 | */ |
| 814 | events.nb_fd = 2; |
| 815 | |
| 816 | ret = update_kernel_poll(&events); |
| 817 | if (ret < 0) { |
| 818 | goto error; |
| 819 | } |
| 820 | update_poll_flag = 0; |
| 821 | } |
| 822 | |
| 823 | nb_fd = LTTNG_POLL_GETNB(&events); |
| 824 | |
| 825 | DBG("Thread kernel polling on %d fds", nb_fd); |
| 826 | |
| 827 | /* Zeroed the poll events */ |
| 828 | lttng_poll_reset(&events); |
| 829 | |
| 830 | /* Poll infinite value of time */ |
| 831 | restart: |
| 832 | health_poll_update(&health_thread_kernel); |
| 833 | ret = lttng_poll_wait(&events, -1); |
| 834 | health_poll_update(&health_thread_kernel); |
| 835 | if (ret < 0) { |
| 836 | /* |
| 837 | * Restart interrupted system call. |
| 838 | */ |
| 839 | if (errno == EINTR) { |
| 840 | goto restart; |
| 841 | } |
| 842 | goto error; |
| 843 | } else if (ret == 0) { |
| 844 | /* Should not happen since timeout is infinite */ |
| 845 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
| 846 | "This should not have happened! Continuing..."); |
| 847 | continue; |
| 848 | } |
| 849 | |
| 850 | for (i = 0; i < nb_fd; i++) { |
| 851 | /* Fetch once the poll data */ |
| 852 | revents = LTTNG_POLL_GETEV(&events, i); |
| 853 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 854 | |
| 855 | health_code_update(&health_thread_kernel); |
| 856 | |
| 857 | /* Thread quit pipe has been closed. Killing thread. */ |
| 858 | ret = check_thread_quit_pipe(pollfd, revents); |
| 859 | if (ret) { |
| 860 | err = 0; |
| 861 | goto exit; |
| 862 | } |
| 863 | |
| 864 | /* Check for data on kernel pipe */ |
| 865 | if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) { |
| 866 | ret = read(kernel_poll_pipe[0], &tmp, 1); |
| 867 | update_poll_flag = 1; |
| 868 | continue; |
| 869 | } else { |
| 870 | /* |
| 871 | * New CPU detected by the kernel. Adding kernel stream to |
| 872 | * kernel session and updating the kernel consumer |
| 873 | */ |
| 874 | if (revents & LPOLLIN) { |
| 875 | ret = update_kernel_stream(&kconsumer_data, pollfd); |
| 876 | if (ret < 0) { |
| 877 | continue; |
| 878 | } |
| 879 | break; |
| 880 | /* |
| 881 | * TODO: We might want to handle the LPOLLERR | LPOLLHUP |
| 882 | * and unregister kernel stream at this point. |
| 883 | */ |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | exit: |
| 890 | error: |
| 891 | lttng_poll_clean(&events); |
| 892 | error_poll_create: |
| 893 | if (err) { |
| 894 | health_error(&health_thread_kernel); |
| 895 | ERR("Health error occurred in %s", __func__); |
| 896 | } |
| 897 | health_exit(&health_thread_kernel); |
| 898 | DBG("Kernel thread dying"); |
| 899 | return NULL; |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * This thread manage the consumer error sent back to the session daemon. |
| 904 | */ |
| 905 | static void *thread_manage_consumer(void *data) |
| 906 | { |
| 907 | int sock = -1, i, ret, pollfd, err = -1; |
| 908 | uint32_t revents, nb_fd; |
| 909 | enum lttcomm_return_code code; |
| 910 | struct lttng_poll_event events; |
| 911 | struct consumer_data *consumer_data = data; |
| 912 | |
| 913 | DBG("[thread] Manage consumer started"); |
| 914 | |
| 915 | health_code_update(&consumer_data->health); |
| 916 | |
| 917 | ret = lttcomm_listen_unix_sock(consumer_data->err_sock); |
| 918 | if (ret < 0) { |
| 919 | goto error_listen; |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock. |
| 924 | * Nothing more will be added to this poll set. |
| 925 | */ |
| 926 | ret = create_thread_poll_set(&events, 2); |
| 927 | if (ret < 0) { |
| 928 | goto error_poll; |
| 929 | } |
| 930 | |
| 931 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
| 932 | if (ret < 0) { |
| 933 | goto error; |
| 934 | } |
| 935 | |
| 936 | nb_fd = LTTNG_POLL_GETNB(&events); |
| 937 | |
| 938 | health_code_update(&consumer_data->health); |
| 939 | |
| 940 | /* Inifinite blocking call, waiting for transmission */ |
| 941 | restart: |
| 942 | health_poll_update(&consumer_data->health); |
| 943 | ret = lttng_poll_wait(&events, -1); |
| 944 | health_poll_update(&consumer_data->health); |
| 945 | if (ret < 0) { |
| 946 | /* |
| 947 | * Restart interrupted system call. |
| 948 | */ |
| 949 | if (errno == EINTR) { |
| 950 | goto restart; |
| 951 | } |
| 952 | goto error; |
| 953 | } |
| 954 | |
| 955 | for (i = 0; i < nb_fd; i++) { |
| 956 | /* Fetch once the poll data */ |
| 957 | revents = LTTNG_POLL_GETEV(&events, i); |
| 958 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 959 | |
| 960 | health_code_update(&consumer_data->health); |
| 961 | |
| 962 | /* Thread quit pipe has been closed. Killing thread. */ |
| 963 | ret = check_thread_quit_pipe(pollfd, revents); |
| 964 | if (ret) { |
| 965 | err = 0; |
| 966 | goto exit; |
| 967 | } |
| 968 | |
| 969 | /* Event on the registration socket */ |
| 970 | if (pollfd == consumer_data->err_sock) { |
| 971 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 972 | ERR("consumer err socket poll error"); |
| 973 | goto error; |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
| 979 | if (sock < 0) { |
| 980 | goto error; |
| 981 | } |
| 982 | |
| 983 | health_code_update(&consumer_data->health); |
| 984 | |
| 985 | DBG2("Receiving code from consumer err_sock"); |
| 986 | |
| 987 | /* Getting status code from kconsumerd */ |
| 988 | ret = lttcomm_recv_unix_sock(sock, &code, |
| 989 | sizeof(enum lttcomm_return_code)); |
| 990 | if (ret <= 0) { |
| 991 | goto error; |
| 992 | } |
| 993 | |
| 994 | health_code_update(&consumer_data->health); |
| 995 | |
| 996 | if (code == CONSUMERD_COMMAND_SOCK_READY) { |
| 997 | consumer_data->cmd_sock = |
| 998 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); |
| 999 | if (consumer_data->cmd_sock < 0) { |
| 1000 | sem_post(&consumer_data->sem); |
| 1001 | PERROR("consumer connect"); |
| 1002 | goto error; |
| 1003 | } |
| 1004 | /* Signal condition to tell that the kconsumerd is ready */ |
| 1005 | sem_post(&consumer_data->sem); |
| 1006 | DBG("consumer command socket ready"); |
| 1007 | } else { |
| 1008 | ERR("consumer error when waiting for SOCK_READY : %s", |
| 1009 | lttcomm_get_readable_code(-code)); |
| 1010 | goto error; |
| 1011 | } |
| 1012 | |
| 1013 | /* Remove the kconsumerd error sock since we've established a connexion */ |
| 1014 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
| 1015 | if (ret < 0) { |
| 1016 | goto error; |
| 1017 | } |
| 1018 | |
| 1019 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
| 1020 | if (ret < 0) { |
| 1021 | goto error; |
| 1022 | } |
| 1023 | |
| 1024 | health_code_update(&consumer_data->health); |
| 1025 | |
| 1026 | /* Update number of fd */ |
| 1027 | nb_fd = LTTNG_POLL_GETNB(&events); |
| 1028 | |
| 1029 | /* Inifinite blocking call, waiting for transmission */ |
| 1030 | restart_poll: |
| 1031 | health_poll_update(&consumer_data->health); |
| 1032 | ret = lttng_poll_wait(&events, -1); |
| 1033 | health_poll_update(&consumer_data->health); |
| 1034 | if (ret < 0) { |
| 1035 | /* |
| 1036 | * Restart interrupted system call. |
| 1037 | */ |
| 1038 | if (errno == EINTR) { |
| 1039 | goto restart_poll; |
| 1040 | } |
| 1041 | goto error; |
| 1042 | } |
| 1043 | |
| 1044 | for (i = 0; i < nb_fd; i++) { |
| 1045 | /* Fetch once the poll data */ |
| 1046 | revents = LTTNG_POLL_GETEV(&events, i); |
| 1047 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1048 | |
| 1049 | health_code_update(&consumer_data->health); |
| 1050 | |
| 1051 | /* Thread quit pipe has been closed. Killing thread. */ |
| 1052 | ret = check_thread_quit_pipe(pollfd, revents); |
| 1053 | if (ret) { |
| 1054 | err = 0; |
| 1055 | goto exit; |
| 1056 | } |
| 1057 | |
| 1058 | /* Event on the kconsumerd socket */ |
| 1059 | if (pollfd == sock) { |
| 1060 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1061 | ERR("consumer err socket second poll error"); |
| 1062 | goto error; |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | health_code_update(&consumer_data->health); |
| 1068 | |
| 1069 | /* Wait for any kconsumerd error */ |
| 1070 | ret = lttcomm_recv_unix_sock(sock, &code, |
| 1071 | sizeof(enum lttcomm_return_code)); |
| 1072 | if (ret <= 0) { |
| 1073 | ERR("consumer closed the command socket"); |
| 1074 | goto error; |
| 1075 | } |
| 1076 | |
| 1077 | ERR("consumer return code : %s", lttcomm_get_readable_code(-code)); |
| 1078 | |
| 1079 | exit: |
| 1080 | error: |
| 1081 | /* Immediately set the consumerd state to stopped */ |
| 1082 | if (consumer_data->type == LTTNG_CONSUMER_KERNEL) { |
| 1083 | uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR); |
| 1084 | } else if (consumer_data->type == LTTNG_CONSUMER64_UST || |
| 1085 | consumer_data->type == LTTNG_CONSUMER32_UST) { |
| 1086 | uatomic_set(&ust_consumerd_state, CONSUMER_ERROR); |
| 1087 | } else { |
| 1088 | /* Code flow error... */ |
| 1089 | assert(0); |
| 1090 | } |
| 1091 | |
| 1092 | if (consumer_data->err_sock >= 0) { |
| 1093 | ret = close(consumer_data->err_sock); |
| 1094 | if (ret) { |
| 1095 | PERROR("close"); |
| 1096 | } |
| 1097 | } |
| 1098 | if (consumer_data->cmd_sock >= 0) { |
| 1099 | ret = close(consumer_data->cmd_sock); |
| 1100 | if (ret) { |
| 1101 | PERROR("close"); |
| 1102 | } |
| 1103 | } |
| 1104 | if (sock >= 0) { |
| 1105 | ret = close(sock); |
| 1106 | if (ret) { |
| 1107 | PERROR("close"); |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | unlink(consumer_data->err_unix_sock_path); |
| 1112 | unlink(consumer_data->cmd_unix_sock_path); |
| 1113 | consumer_data->pid = 0; |
| 1114 | |
| 1115 | lttng_poll_clean(&events); |
| 1116 | error_poll: |
| 1117 | error_listen: |
| 1118 | if (err) { |
| 1119 | health_error(&consumer_data->health); |
| 1120 | ERR("Health error occurred in %s", __func__); |
| 1121 | } |
| 1122 | health_exit(&consumer_data->health); |
| 1123 | DBG("consumer thread cleanup completed"); |
| 1124 | |
| 1125 | return NULL; |
| 1126 | } |
| 1127 | |
| 1128 | /* |
| 1129 | * This thread manage application communication. |
| 1130 | */ |
| 1131 | static void *thread_manage_apps(void *data) |
| 1132 | { |
| 1133 | int i, ret, pollfd, err = -1; |
| 1134 | uint32_t revents, nb_fd; |
| 1135 | struct ust_command ust_cmd; |
| 1136 | struct lttng_poll_event events; |
| 1137 | |
| 1138 | DBG("[thread] Manage application started"); |
| 1139 | |
| 1140 | rcu_register_thread(); |
| 1141 | rcu_thread_online(); |
| 1142 | |
| 1143 | health_code_update(&health_thread_app_manage); |
| 1144 | |
| 1145 | ret = create_thread_poll_set(&events, 2); |
| 1146 | if (ret < 0) { |
| 1147 | goto error_poll_create; |
| 1148 | } |
| 1149 | |
| 1150 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
| 1151 | if (ret < 0) { |
| 1152 | goto error; |
| 1153 | } |
| 1154 | |
| 1155 | health_code_update(&health_thread_app_manage); |
| 1156 | |
| 1157 | while (1) { |
| 1158 | /* Zeroed the events structure */ |
| 1159 | lttng_poll_reset(&events); |
| 1160 | |
| 1161 | nb_fd = LTTNG_POLL_GETNB(&events); |
| 1162 | |
| 1163 | DBG("Apps thread polling on %d fds", nb_fd); |
| 1164 | |
| 1165 | /* Inifinite blocking call, waiting for transmission */ |
| 1166 | restart: |
| 1167 | health_poll_update(&health_thread_app_manage); |
| 1168 | ret = lttng_poll_wait(&events, -1); |
| 1169 | health_poll_update(&health_thread_app_manage); |
| 1170 | if (ret < 0) { |
| 1171 | /* |
| 1172 | * Restart interrupted system call. |
| 1173 | */ |
| 1174 | if (errno == EINTR) { |
| 1175 | goto restart; |
| 1176 | } |
| 1177 | goto error; |
| 1178 | } |
| 1179 | |
| 1180 | for (i = 0; i < nb_fd; i++) { |
| 1181 | /* Fetch once the poll data */ |
| 1182 | revents = LTTNG_POLL_GETEV(&events, i); |
| 1183 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1184 | |
| 1185 | health_code_update(&health_thread_app_manage); |
| 1186 | |
| 1187 | /* Thread quit pipe has been closed. Killing thread. */ |
| 1188 | ret = check_thread_quit_pipe(pollfd, revents); |
| 1189 | if (ret) { |
| 1190 | err = 0; |
| 1191 | goto exit; |
| 1192 | } |
| 1193 | |
| 1194 | /* Inspect the apps cmd pipe */ |
| 1195 | if (pollfd == apps_cmd_pipe[0]) { |
| 1196 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1197 | ERR("Apps command pipe error"); |
| 1198 | goto error; |
| 1199 | } else if (revents & LPOLLIN) { |
| 1200 | /* Empty pipe */ |
| 1201 | ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd)); |
| 1202 | if (ret < 0 || ret < sizeof(ust_cmd)) { |
| 1203 | PERROR("read apps cmd pipe"); |
| 1204 | goto error; |
| 1205 | } |
| 1206 | |
| 1207 | health_code_update(&health_thread_app_manage); |
| 1208 | |
| 1209 | /* Register applicaton to the session daemon */ |
| 1210 | ret = ust_app_register(&ust_cmd.reg_msg, |
| 1211 | ust_cmd.sock); |
| 1212 | if (ret == -ENOMEM) { |
| 1213 | goto error; |
| 1214 | } else if (ret < 0) { |
| 1215 | break; |
| 1216 | } |
| 1217 | |
| 1218 | health_code_update(&health_thread_app_manage); |
| 1219 | |
| 1220 | /* |
| 1221 | * Validate UST version compatibility. |
| 1222 | */ |
| 1223 | ret = ust_app_validate_version(ust_cmd.sock); |
| 1224 | if (ret >= 0) { |
| 1225 | /* |
| 1226 | * Add channel(s) and event(s) to newly registered apps |
| 1227 | * from lttng global UST domain. |
| 1228 | */ |
| 1229 | update_ust_app(ust_cmd.sock); |
| 1230 | } |
| 1231 | |
| 1232 | health_code_update(&health_thread_app_manage); |
| 1233 | |
| 1234 | ret = ust_app_register_done(ust_cmd.sock); |
| 1235 | if (ret < 0) { |
| 1236 | /* |
| 1237 | * If the registration is not possible, we simply |
| 1238 | * unregister the apps and continue |
| 1239 | */ |
| 1240 | ust_app_unregister(ust_cmd.sock); |
| 1241 | } else { |
| 1242 | /* |
| 1243 | * We just need here to monitor the close of the UST |
| 1244 | * socket and poll set monitor those by default. |
| 1245 | * Listen on POLLIN (even if we never expect any |
| 1246 | * data) to ensure that hangup wakes us. |
| 1247 | */ |
| 1248 | ret = lttng_poll_add(&events, ust_cmd.sock, LPOLLIN); |
| 1249 | if (ret < 0) { |
| 1250 | goto error; |
| 1251 | } |
| 1252 | |
| 1253 | DBG("Apps with sock %d added to poll set", |
| 1254 | ust_cmd.sock); |
| 1255 | } |
| 1256 | |
| 1257 | health_code_update(&health_thread_app_manage); |
| 1258 | |
| 1259 | break; |
| 1260 | } |
| 1261 | } else { |
| 1262 | /* |
| 1263 | * At this point, we know that a registered application made |
| 1264 | * the event at poll_wait. |
| 1265 | */ |
| 1266 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1267 | /* Removing from the poll set */ |
| 1268 | ret = lttng_poll_del(&events, pollfd); |
| 1269 | if (ret < 0) { |
| 1270 | goto error; |
| 1271 | } |
| 1272 | |
| 1273 | /* Socket closed on remote end. */ |
| 1274 | ust_app_unregister(pollfd); |
| 1275 | break; |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | health_code_update(&health_thread_app_manage); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | exit: |
| 1284 | error: |
| 1285 | lttng_poll_clean(&events); |
| 1286 | error_poll_create: |
| 1287 | if (err) { |
| 1288 | health_error(&health_thread_app_manage); |
| 1289 | ERR("Health error occurred in %s", __func__); |
| 1290 | } |
| 1291 | health_exit(&health_thread_app_manage); |
| 1292 | DBG("Application communication apps thread cleanup complete"); |
| 1293 | rcu_thread_offline(); |
| 1294 | rcu_unregister_thread(); |
| 1295 | return NULL; |
| 1296 | } |
| 1297 | |
| 1298 | /* |
| 1299 | * Dispatch request from the registration threads to the application |
| 1300 | * communication thread. |
| 1301 | */ |
| 1302 | static void *thread_dispatch_ust_registration(void *data) |
| 1303 | { |
| 1304 | int ret; |
| 1305 | struct cds_wfq_node *node; |
| 1306 | struct ust_command *ust_cmd = NULL; |
| 1307 | |
| 1308 | DBG("[thread] Dispatch UST command started"); |
| 1309 | |
| 1310 | while (!CMM_LOAD_SHARED(dispatch_thread_exit)) { |
| 1311 | /* Atomically prepare the queue futex */ |
| 1312 | futex_nto1_prepare(&ust_cmd_queue.futex); |
| 1313 | |
| 1314 | do { |
| 1315 | /* Dequeue command for registration */ |
| 1316 | node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue); |
| 1317 | if (node == NULL) { |
| 1318 | DBG("Woken up but nothing in the UST command queue"); |
| 1319 | /* Continue thread execution */ |
| 1320 | break; |
| 1321 | } |
| 1322 | |
| 1323 | ust_cmd = caa_container_of(node, struct ust_command, node); |
| 1324 | |
| 1325 | DBG("Dispatching UST registration pid:%d ppid:%d uid:%d" |
| 1326 | " gid:%d sock:%d name:%s (version %d.%d)", |
| 1327 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, |
| 1328 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, |
| 1329 | ust_cmd->sock, ust_cmd->reg_msg.name, |
| 1330 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); |
| 1331 | /* |
| 1332 | * Inform apps thread of the new application registration. This |
| 1333 | * call is blocking so we can be assured that the data will be read |
| 1334 | * at some point in time or wait to the end of the world :) |
| 1335 | */ |
| 1336 | ret = write(apps_cmd_pipe[1], ust_cmd, |
| 1337 | sizeof(struct ust_command)); |
| 1338 | if (ret < 0) { |
| 1339 | PERROR("write apps cmd pipe"); |
| 1340 | if (errno == EBADF) { |
| 1341 | /* |
| 1342 | * We can't inform the application thread to process |
| 1343 | * registration. We will exit or else application |
| 1344 | * registration will not occur and tracing will never |
| 1345 | * start. |
| 1346 | */ |
| 1347 | goto error; |
| 1348 | } |
| 1349 | } |
| 1350 | free(ust_cmd); |
| 1351 | } while (node != NULL); |
| 1352 | |
| 1353 | /* Futex wait on queue. Blocking call on futex() */ |
| 1354 | futex_nto1_wait(&ust_cmd_queue.futex); |
| 1355 | } |
| 1356 | |
| 1357 | error: |
| 1358 | DBG("Dispatch thread dying"); |
| 1359 | return NULL; |
| 1360 | } |
| 1361 | |
| 1362 | /* |
| 1363 | * This thread manage application registration. |
| 1364 | */ |
| 1365 | static void *thread_registration_apps(void *data) |
| 1366 | { |
| 1367 | int sock = -1, i, ret, pollfd, err = -1; |
| 1368 | uint32_t revents, nb_fd; |
| 1369 | struct lttng_poll_event events; |
| 1370 | /* |
| 1371 | * Get allocated in this thread, enqueued to a global queue, dequeued and |
| 1372 | * freed in the manage apps thread. |
| 1373 | */ |
| 1374 | struct ust_command *ust_cmd = NULL; |
| 1375 | |
| 1376 | DBG("[thread] Manage application registration started"); |
| 1377 | |
| 1378 | ret = lttcomm_listen_unix_sock(apps_sock); |
| 1379 | if (ret < 0) { |
| 1380 | goto error_listen; |
| 1381 | } |
| 1382 | |
| 1383 | /* |
| 1384 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing |
| 1385 | * more will be added to this poll set. |
| 1386 | */ |
| 1387 | ret = create_thread_poll_set(&events, 2); |
| 1388 | if (ret < 0) { |
| 1389 | goto error_create_poll; |
| 1390 | } |
| 1391 | |
| 1392 | /* Add the application registration socket */ |
| 1393 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); |
| 1394 | if (ret < 0) { |
| 1395 | goto error_poll_add; |
| 1396 | } |
| 1397 | |
| 1398 | /* Notify all applications to register */ |
| 1399 | ret = notify_ust_apps(1); |
| 1400 | if (ret < 0) { |
| 1401 | ERR("Failed to notify applications or create the wait shared memory.\n" |
| 1402 | "Execution continues but there might be problem for already\n" |
| 1403 | "running applications that wishes to register."); |
| 1404 | } |
| 1405 | |
| 1406 | while (1) { |
| 1407 | DBG("Accepting application registration"); |
| 1408 | |
| 1409 | nb_fd = LTTNG_POLL_GETNB(&events); |
| 1410 | |
| 1411 | /* Inifinite blocking call, waiting for transmission */ |
| 1412 | restart: |
| 1413 | health_poll_update(&health_thread_app_reg); |
| 1414 | ret = lttng_poll_wait(&events, -1); |
| 1415 | health_poll_update(&health_thread_app_reg); |
| 1416 | if (ret < 0) { |
| 1417 | /* |
| 1418 | * Restart interrupted system call. |
| 1419 | */ |
| 1420 | if (errno == EINTR) { |
| 1421 | goto restart; |
| 1422 | } |
| 1423 | goto error; |
| 1424 | } |
| 1425 | |
| 1426 | for (i = 0; i < nb_fd; i++) { |
| 1427 | health_code_update(&health_thread_app_reg); |
| 1428 | |
| 1429 | /* Fetch once the poll data */ |
| 1430 | revents = LTTNG_POLL_GETEV(&events, i); |
| 1431 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1432 | |
| 1433 | /* Thread quit pipe has been closed. Killing thread. */ |
| 1434 | ret = check_thread_quit_pipe(pollfd, revents); |
| 1435 | if (ret) { |
| 1436 | err = 0; |
| 1437 | goto exit; |
| 1438 | } |
| 1439 | |
| 1440 | /* Event on the registration socket */ |
| 1441 | if (pollfd == apps_sock) { |
| 1442 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1443 | ERR("Register apps socket poll error"); |
| 1444 | goto error; |
| 1445 | } else if (revents & LPOLLIN) { |
| 1446 | sock = lttcomm_accept_unix_sock(apps_sock); |
| 1447 | if (sock < 0) { |
| 1448 | goto error; |
| 1449 | } |
| 1450 | |
| 1451 | /* Create UST registration command for enqueuing */ |
| 1452 | ust_cmd = zmalloc(sizeof(struct ust_command)); |
| 1453 | if (ust_cmd == NULL) { |
| 1454 | PERROR("ust command zmalloc"); |
| 1455 | goto error; |
| 1456 | } |
| 1457 | |
| 1458 | /* |
| 1459 | * Using message-based transmissions to ensure we don't |
| 1460 | * have to deal with partially received messages. |
| 1461 | */ |
| 1462 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
| 1463 | if (ret < 0) { |
| 1464 | ERR("Exhausted file descriptors allowed for applications."); |
| 1465 | free(ust_cmd); |
| 1466 | ret = close(sock); |
| 1467 | if (ret) { |
| 1468 | PERROR("close"); |
| 1469 | } |
| 1470 | sock = -1; |
| 1471 | continue; |
| 1472 | } |
| 1473 | health_code_update(&health_thread_app_reg); |
| 1474 | ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg, |
| 1475 | sizeof(struct ust_register_msg)); |
| 1476 | if (ret < 0 || ret < sizeof(struct ust_register_msg)) { |
| 1477 | if (ret < 0) { |
| 1478 | PERROR("lttcomm_recv_unix_sock register apps"); |
| 1479 | } else { |
| 1480 | ERR("Wrong size received on apps register"); |
| 1481 | } |
| 1482 | free(ust_cmd); |
| 1483 | ret = close(sock); |
| 1484 | if (ret) { |
| 1485 | PERROR("close"); |
| 1486 | } |
| 1487 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 1488 | sock = -1; |
| 1489 | continue; |
| 1490 | } |
| 1491 | health_code_update(&health_thread_app_reg); |
| 1492 | |
| 1493 | ust_cmd->sock = sock; |
| 1494 | sock = -1; |
| 1495 | |
| 1496 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
| 1497 | " gid:%d sock:%d name:%s (version %d.%d)", |
| 1498 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, |
| 1499 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, |
| 1500 | ust_cmd->sock, ust_cmd->reg_msg.name, |
| 1501 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); |
| 1502 | |
| 1503 | /* |
| 1504 | * Lock free enqueue the registration request. The red pill |
| 1505 | * has been taken! This apps will be part of the *system*. |
| 1506 | */ |
| 1507 | cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node); |
| 1508 | |
| 1509 | /* |
| 1510 | * Wake the registration queue futex. Implicit memory |
| 1511 | * barrier with the exchange in cds_wfq_enqueue. |
| 1512 | */ |
| 1513 | futex_nto1_wake(&ust_cmd_queue.futex); |
| 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | exit: |
| 1520 | error: |
| 1521 | if (err) { |
| 1522 | health_error(&health_thread_app_reg); |
| 1523 | ERR("Health error occurred in %s", __func__); |
| 1524 | } |
| 1525 | health_exit(&health_thread_app_reg); |
| 1526 | |
| 1527 | /* Notify that the registration thread is gone */ |
| 1528 | notify_ust_apps(0); |
| 1529 | |
| 1530 | if (apps_sock >= 0) { |
| 1531 | ret = close(apps_sock); |
| 1532 | if (ret) { |
| 1533 | PERROR("close"); |
| 1534 | } |
| 1535 | } |
| 1536 | if (sock >= 0) { |
| 1537 | ret = close(sock); |
| 1538 | if (ret) { |
| 1539 | PERROR("close"); |
| 1540 | } |
| 1541 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 1542 | } |
| 1543 | unlink(apps_unix_sock_path); |
| 1544 | |
| 1545 | error_poll_add: |
| 1546 | lttng_poll_clean(&events); |
| 1547 | error_listen: |
| 1548 | error_create_poll: |
| 1549 | DBG("UST Registration thread cleanup complete"); |
| 1550 | |
| 1551 | return NULL; |
| 1552 | } |
| 1553 | |
| 1554 | /* |
| 1555 | * Start the thread_manage_consumer. This must be done after a lttng-consumerd |
| 1556 | * exec or it will fails. |
| 1557 | */ |
| 1558 | static int spawn_consumer_thread(struct consumer_data *consumer_data) |
| 1559 | { |
| 1560 | int ret; |
| 1561 | struct timespec timeout; |
| 1562 | |
| 1563 | timeout.tv_sec = DEFAULT_SEM_WAIT_TIMEOUT; |
| 1564 | timeout.tv_nsec = 0; |
| 1565 | |
| 1566 | /* Setup semaphore */ |
| 1567 | ret = sem_init(&consumer_data->sem, 0, 0); |
| 1568 | if (ret < 0) { |
| 1569 | PERROR("sem_init consumer semaphore"); |
| 1570 | goto error; |
| 1571 | } |
| 1572 | |
| 1573 | ret = pthread_create(&consumer_data->thread, NULL, |
| 1574 | thread_manage_consumer, consumer_data); |
| 1575 | if (ret != 0) { |
| 1576 | PERROR("pthread_create consumer"); |
| 1577 | ret = -1; |
| 1578 | goto error; |
| 1579 | } |
| 1580 | |
| 1581 | /* Get time for sem_timedwait absolute timeout */ |
| 1582 | ret = clock_gettime(CLOCK_REALTIME, &timeout); |
| 1583 | if (ret < 0) { |
| 1584 | PERROR("clock_gettime spawn consumer"); |
| 1585 | /* Infinite wait for the kconsumerd thread to be ready */ |
| 1586 | ret = sem_wait(&consumer_data->sem); |
| 1587 | } else { |
| 1588 | /* Normal timeout if the gettime was successful */ |
| 1589 | timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; |
| 1590 | ret = sem_timedwait(&consumer_data->sem, &timeout); |
| 1591 | } |
| 1592 | |
| 1593 | if (ret < 0) { |
| 1594 | if (errno == ETIMEDOUT) { |
| 1595 | /* |
| 1596 | * Call has timed out so we kill the kconsumerd_thread and return |
| 1597 | * an error. |
| 1598 | */ |
| 1599 | ERR("The consumer thread was never ready. Killing it"); |
| 1600 | ret = pthread_cancel(consumer_data->thread); |
| 1601 | if (ret < 0) { |
| 1602 | PERROR("pthread_cancel consumer thread"); |
| 1603 | } |
| 1604 | } else { |
| 1605 | PERROR("semaphore wait failed consumer thread"); |
| 1606 | } |
| 1607 | goto error; |
| 1608 | } |
| 1609 | |
| 1610 | pthread_mutex_lock(&consumer_data->pid_mutex); |
| 1611 | if (consumer_data->pid == 0) { |
| 1612 | ERR("Kconsumerd did not start"); |
| 1613 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 1614 | goto error; |
| 1615 | } |
| 1616 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 1617 | |
| 1618 | return 0; |
| 1619 | |
| 1620 | error: |
| 1621 | return ret; |
| 1622 | } |
| 1623 | |
| 1624 | /* |
| 1625 | * Join consumer thread |
| 1626 | */ |
| 1627 | static int join_consumer_thread(struct consumer_data *consumer_data) |
| 1628 | { |
| 1629 | void *status; |
| 1630 | int ret; |
| 1631 | |
| 1632 | /* Consumer pid must be a real one. */ |
| 1633 | if (consumer_data->pid > 0) { |
| 1634 | ret = kill(consumer_data->pid, SIGTERM); |
| 1635 | if (ret) { |
| 1636 | ERR("Error killing consumer daemon"); |
| 1637 | return ret; |
| 1638 | } |
| 1639 | return pthread_join(consumer_data->thread, &status); |
| 1640 | } else { |
| 1641 | return 0; |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | /* |
| 1646 | * Fork and exec a consumer daemon (consumerd). |
| 1647 | * |
| 1648 | * Return pid if successful else -1. |
| 1649 | */ |
| 1650 | static pid_t spawn_consumerd(struct consumer_data *consumer_data) |
| 1651 | { |
| 1652 | int ret; |
| 1653 | pid_t pid; |
| 1654 | const char *consumer_to_use; |
| 1655 | const char *verbosity; |
| 1656 | struct stat st; |
| 1657 | |
| 1658 | DBG("Spawning consumerd"); |
| 1659 | |
| 1660 | pid = fork(); |
| 1661 | if (pid == 0) { |
| 1662 | /* |
| 1663 | * Exec consumerd. |
| 1664 | */ |
| 1665 | if (opt_verbose_consumer) { |
| 1666 | verbosity = "--verbose"; |
| 1667 | } else { |
| 1668 | verbosity = "--quiet"; |
| 1669 | } |
| 1670 | switch (consumer_data->type) { |
| 1671 | case LTTNG_CONSUMER_KERNEL: |
| 1672 | /* |
| 1673 | * Find out which consumerd to execute. We will first try the |
| 1674 | * 64-bit path, then the sessiond's installation directory, and |
| 1675 | * fallback on the 32-bit one, |
| 1676 | */ |
| 1677 | DBG3("Looking for a kernel consumer at these locations:"); |
| 1678 | DBG3(" 1) %s", consumerd64_bin); |
| 1679 | DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE); |
| 1680 | DBG3(" 3) %s", consumerd32_bin); |
| 1681 | if (stat(consumerd64_bin, &st) == 0) { |
| 1682 | DBG3("Found location #1"); |
| 1683 | consumer_to_use = consumerd64_bin; |
| 1684 | } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) { |
| 1685 | DBG3("Found location #2"); |
| 1686 | consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE; |
| 1687 | } else if (stat(consumerd32_bin, &st) == 0) { |
| 1688 | DBG3("Found location #3"); |
| 1689 | consumer_to_use = consumerd32_bin; |
| 1690 | } else { |
| 1691 | DBG("Could not find any valid consumerd executable"); |
| 1692 | break; |
| 1693 | } |
| 1694 | DBG("Using kernel consumer at: %s", consumer_to_use); |
| 1695 | execl(consumer_to_use, |
| 1696 | "lttng-consumerd", verbosity, "-k", |
| 1697 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
| 1698 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, |
| 1699 | NULL); |
| 1700 | break; |
| 1701 | case LTTNG_CONSUMER64_UST: |
| 1702 | { |
| 1703 | char *tmpnew = NULL; |
| 1704 | |
| 1705 | if (consumerd64_libdir[0] != '\0') { |
| 1706 | char *tmp; |
| 1707 | size_t tmplen; |
| 1708 | |
| 1709 | tmp = getenv("LD_LIBRARY_PATH"); |
| 1710 | if (!tmp) { |
| 1711 | tmp = ""; |
| 1712 | } |
| 1713 | tmplen = strlen("LD_LIBRARY_PATH=") |
| 1714 | + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp); |
| 1715 | tmpnew = zmalloc(tmplen + 1 /* \0 */); |
| 1716 | if (!tmpnew) { |
| 1717 | ret = -ENOMEM; |
| 1718 | goto error; |
| 1719 | } |
| 1720 | strcpy(tmpnew, "LD_LIBRARY_PATH="); |
| 1721 | strcat(tmpnew, consumerd64_libdir); |
| 1722 | if (tmp[0] != '\0') { |
| 1723 | strcat(tmpnew, ":"); |
| 1724 | strcat(tmpnew, tmp); |
| 1725 | } |
| 1726 | ret = putenv(tmpnew); |
| 1727 | if (ret) { |
| 1728 | ret = -errno; |
| 1729 | goto error; |
| 1730 | } |
| 1731 | } |
| 1732 | DBG("Using 64-bit UST consumer at: %s", consumerd64_bin); |
| 1733 | ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u", |
| 1734 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
| 1735 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, |
| 1736 | NULL); |
| 1737 | if (consumerd64_libdir[0] != '\0') { |
| 1738 | free(tmpnew); |
| 1739 | } |
| 1740 | if (ret) { |
| 1741 | goto error; |
| 1742 | } |
| 1743 | break; |
| 1744 | } |
| 1745 | case LTTNG_CONSUMER32_UST: |
| 1746 | { |
| 1747 | char *tmpnew = NULL; |
| 1748 | |
| 1749 | if (consumerd32_libdir[0] != '\0') { |
| 1750 | char *tmp; |
| 1751 | size_t tmplen; |
| 1752 | |
| 1753 | tmp = getenv("LD_LIBRARY_PATH"); |
| 1754 | if (!tmp) { |
| 1755 | tmp = ""; |
| 1756 | } |
| 1757 | tmplen = strlen("LD_LIBRARY_PATH=") |
| 1758 | + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp); |
| 1759 | tmpnew = zmalloc(tmplen + 1 /* \0 */); |
| 1760 | if (!tmpnew) { |
| 1761 | ret = -ENOMEM; |
| 1762 | goto error; |
| 1763 | } |
| 1764 | strcpy(tmpnew, "LD_LIBRARY_PATH="); |
| 1765 | strcat(tmpnew, consumerd32_libdir); |
| 1766 | if (tmp[0] != '\0') { |
| 1767 | strcat(tmpnew, ":"); |
| 1768 | strcat(tmpnew, tmp); |
| 1769 | } |
| 1770 | ret = putenv(tmpnew); |
| 1771 | if (ret) { |
| 1772 | ret = -errno; |
| 1773 | goto error; |
| 1774 | } |
| 1775 | } |
| 1776 | DBG("Using 32-bit UST consumer at: %s", consumerd32_bin); |
| 1777 | ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u", |
| 1778 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
| 1779 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, |
| 1780 | NULL); |
| 1781 | if (consumerd32_libdir[0] != '\0') { |
| 1782 | free(tmpnew); |
| 1783 | } |
| 1784 | if (ret) { |
| 1785 | goto error; |
| 1786 | } |
| 1787 | break; |
| 1788 | } |
| 1789 | default: |
| 1790 | PERROR("unknown consumer type"); |
| 1791 | exit(EXIT_FAILURE); |
| 1792 | } |
| 1793 | if (errno != 0) { |
| 1794 | PERROR("kernel start consumer exec"); |
| 1795 | } |
| 1796 | exit(EXIT_FAILURE); |
| 1797 | } else if (pid > 0) { |
| 1798 | ret = pid; |
| 1799 | } else { |
| 1800 | PERROR("start consumer fork"); |
| 1801 | ret = -errno; |
| 1802 | } |
| 1803 | error: |
| 1804 | return ret; |
| 1805 | } |
| 1806 | |
| 1807 | /* |
| 1808 | * Spawn the consumerd daemon and session daemon thread. |
| 1809 | */ |
| 1810 | static int start_consumerd(struct consumer_data *consumer_data) |
| 1811 | { |
| 1812 | int ret; |
| 1813 | |
| 1814 | pthread_mutex_lock(&consumer_data->pid_mutex); |
| 1815 | if (consumer_data->pid != 0) { |
| 1816 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 1817 | goto end; |
| 1818 | } |
| 1819 | |
| 1820 | ret = spawn_consumerd(consumer_data); |
| 1821 | if (ret < 0) { |
| 1822 | ERR("Spawning consumerd failed"); |
| 1823 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 1824 | goto error; |
| 1825 | } |
| 1826 | |
| 1827 | /* Setting up the consumer_data pid */ |
| 1828 | consumer_data->pid = ret; |
| 1829 | DBG2("Consumer pid %d", consumer_data->pid); |
| 1830 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 1831 | |
| 1832 | DBG2("Spawning consumer control thread"); |
| 1833 | ret = spawn_consumer_thread(consumer_data); |
| 1834 | if (ret < 0) { |
| 1835 | ERR("Fatal error spawning consumer control thread"); |
| 1836 | goto error; |
| 1837 | } |
| 1838 | |
| 1839 | end: |
| 1840 | return 0; |
| 1841 | |
| 1842 | error: |
| 1843 | return ret; |
| 1844 | } |
| 1845 | |
| 1846 | /* |
| 1847 | * Compute health status of each consumer. If one of them is zero (bad |
| 1848 | * state), we return 0. |
| 1849 | */ |
| 1850 | static int check_consumer_health(void) |
| 1851 | { |
| 1852 | int ret; |
| 1853 | |
| 1854 | ret = health_check_state(&kconsumer_data.health) && |
| 1855 | health_check_state(&ustconsumer32_data.health) && |
| 1856 | health_check_state(&ustconsumer64_data.health); |
| 1857 | |
| 1858 | DBG3("Health consumer check %d", ret); |
| 1859 | |
| 1860 | return ret; |
| 1861 | } |
| 1862 | |
| 1863 | /* |
| 1864 | * Check version of the lttng-modules. |
| 1865 | */ |
| 1866 | static int validate_lttng_modules_version(void) |
| 1867 | { |
| 1868 | return kernel_validate_version(kernel_tracer_fd); |
| 1869 | } |
| 1870 | |
| 1871 | /* |
| 1872 | * Setup necessary data for kernel tracer action. |
| 1873 | */ |
| 1874 | static int init_kernel_tracer(void) |
| 1875 | { |
| 1876 | int ret; |
| 1877 | |
| 1878 | /* Modprobe lttng kernel modules */ |
| 1879 | ret = modprobe_lttng_control(); |
| 1880 | if (ret < 0) { |
| 1881 | goto error; |
| 1882 | } |
| 1883 | |
| 1884 | /* Open debugfs lttng */ |
| 1885 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); |
| 1886 | if (kernel_tracer_fd < 0) { |
| 1887 | DBG("Failed to open %s", module_proc_lttng); |
| 1888 | ret = -1; |
| 1889 | goto error_open; |
| 1890 | } |
| 1891 | |
| 1892 | /* Validate kernel version */ |
| 1893 | ret = validate_lttng_modules_version(); |
| 1894 | if (ret < 0) { |
| 1895 | goto error_version; |
| 1896 | } |
| 1897 | |
| 1898 | ret = modprobe_lttng_data(); |
| 1899 | if (ret < 0) { |
| 1900 | goto error_modules; |
| 1901 | } |
| 1902 | |
| 1903 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
| 1904 | return 0; |
| 1905 | |
| 1906 | error_version: |
| 1907 | modprobe_remove_lttng_control(); |
| 1908 | ret = close(kernel_tracer_fd); |
| 1909 | if (ret) { |
| 1910 | PERROR("close"); |
| 1911 | } |
| 1912 | kernel_tracer_fd = -1; |
| 1913 | return LTTCOMM_KERN_VERSION; |
| 1914 | |
| 1915 | error_modules: |
| 1916 | ret = close(kernel_tracer_fd); |
| 1917 | if (ret) { |
| 1918 | PERROR("close"); |
| 1919 | } |
| 1920 | |
| 1921 | error_open: |
| 1922 | modprobe_remove_lttng_control(); |
| 1923 | |
| 1924 | error: |
| 1925 | WARN("No kernel tracer available"); |
| 1926 | kernel_tracer_fd = -1; |
| 1927 | if (!is_root) { |
| 1928 | return LTTCOMM_NEED_ROOT_SESSIOND; |
| 1929 | } else { |
| 1930 | return LTTCOMM_KERN_NA; |
| 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | /* |
| 1935 | * Init tracing by creating trace directory and sending fds kernel consumer. |
| 1936 | */ |
| 1937 | static int init_kernel_tracing(struct ltt_kernel_session *session) |
| 1938 | { |
| 1939 | int ret = 0; |
| 1940 | struct lttng_ht_iter iter; |
| 1941 | struct consumer_socket *socket; |
| 1942 | |
| 1943 | assert(session); |
| 1944 | |
| 1945 | if (session->consumer_fds_sent == 0 && session->consumer != NULL) { |
| 1946 | cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter, |
| 1947 | socket, node.node) { |
| 1948 | /* Code flow error */ |
| 1949 | assert(socket->fd >= 0); |
| 1950 | |
| 1951 | pthread_mutex_lock(socket->lock); |
| 1952 | ret = kernel_consumer_send_session(socket->fd, session); |
| 1953 | pthread_mutex_unlock(socket->lock); |
| 1954 | if (ret < 0) { |
| 1955 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
| 1956 | goto error; |
| 1957 | } |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | error: |
| 1962 | return ret; |
| 1963 | } |
| 1964 | |
| 1965 | /* |
| 1966 | * Create a socket to the relayd using the URI. |
| 1967 | * |
| 1968 | * On success, the relayd_sock pointer is set to the created socket. |
| 1969 | * Else, it is untouched and an lttcomm error code is returned. |
| 1970 | */ |
| 1971 | static int create_connect_relayd(struct consumer_output *output, |
| 1972 | const char *session_name, struct lttng_uri *uri, |
| 1973 | struct lttcomm_sock **relayd_sock) |
| 1974 | { |
| 1975 | int ret; |
| 1976 | struct lttcomm_sock *sock; |
| 1977 | |
| 1978 | /* Create socket object from URI */ |
| 1979 | sock = lttcomm_alloc_sock_from_uri(uri); |
| 1980 | if (sock == NULL) { |
| 1981 | ret = LTTCOMM_FATAL; |
| 1982 | goto error; |
| 1983 | } |
| 1984 | |
| 1985 | ret = lttcomm_create_sock(sock); |
| 1986 | if (ret < 0) { |
| 1987 | ret = LTTCOMM_FATAL; |
| 1988 | goto error; |
| 1989 | } |
| 1990 | |
| 1991 | /* Connect to relayd so we can proceed with a session creation. */ |
| 1992 | ret = relayd_connect(sock); |
| 1993 | if (ret < 0) { |
| 1994 | ERR("Unable to reach lttng-relayd"); |
| 1995 | ret = LTTCOMM_RELAYD_SESSION_FAIL; |
| 1996 | goto free_sock; |
| 1997 | } |
| 1998 | |
| 1999 | /* Create socket for control stream. */ |
| 2000 | if (uri->stype == LTTNG_STREAM_CONTROL) { |
| 2001 | DBG3("Creating relayd stream socket from URI"); |
| 2002 | |
| 2003 | /* Check relayd version */ |
| 2004 | ret = relayd_version_check(sock, LTTNG_UST_COMM_MAJOR, 0); |
| 2005 | if (ret < 0) { |
| 2006 | ret = LTTCOMM_RELAYD_VERSION_FAIL; |
| 2007 | goto close_sock; |
| 2008 | } |
| 2009 | } else if (uri->stype == LTTNG_STREAM_DATA) { |
| 2010 | DBG3("Creating relayd data socket from URI"); |
| 2011 | } else { |
| 2012 | /* Command is not valid */ |
| 2013 | ERR("Relayd invalid stream type: %d", uri->stype); |
| 2014 | ret = LTTCOMM_INVALID; |
| 2015 | goto close_sock; |
| 2016 | } |
| 2017 | |
| 2018 | *relayd_sock = sock; |
| 2019 | |
| 2020 | return LTTCOMM_OK; |
| 2021 | |
| 2022 | close_sock: |
| 2023 | if (sock) { |
| 2024 | (void) relayd_close(sock); |
| 2025 | } |
| 2026 | free_sock: |
| 2027 | if (sock) { |
| 2028 | lttcomm_destroy_sock(sock); |
| 2029 | } |
| 2030 | error: |
| 2031 | return ret; |
| 2032 | } |
| 2033 | |
| 2034 | /* |
| 2035 | * Connect to the relayd using URI and send the socket to the right consumer. |
| 2036 | */ |
| 2037 | static int send_socket_relayd_consumer(int domain, struct ltt_session *session, |
| 2038 | struct lttng_uri *relayd_uri, struct consumer_output *consumer, |
| 2039 | int consumer_fd) |
| 2040 | { |
| 2041 | int ret; |
| 2042 | struct lttcomm_sock *sock = NULL; |
| 2043 | |
| 2044 | /* Set the network sequence index if not set. */ |
| 2045 | if (consumer->net_seq_index == -1) { |
| 2046 | /* |
| 2047 | * Increment net_seq_idx because we are about to transfer the |
| 2048 | * new relayd socket to the consumer. |
| 2049 | */ |
| 2050 | uatomic_inc(&relayd_net_seq_idx); |
| 2051 | /* Assign unique key so the consumer can match streams */ |
| 2052 | consumer->net_seq_index = uatomic_read(&relayd_net_seq_idx); |
| 2053 | } |
| 2054 | |
| 2055 | /* Connect to relayd and make version check if uri is the control. */ |
| 2056 | ret = create_connect_relayd(consumer, session->name, relayd_uri, &sock); |
| 2057 | if (ret != LTTCOMM_OK) { |
| 2058 | goto close_sock; |
| 2059 | } |
| 2060 | |
| 2061 | /* If the control socket is connected, network session is ready */ |
| 2062 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { |
| 2063 | session->net_handle = 1; |
| 2064 | } |
| 2065 | |
| 2066 | /* Send relayd socket to consumer. */ |
| 2067 | ret = consumer_send_relayd_socket(consumer_fd, sock, |
| 2068 | consumer, relayd_uri->stype); |
| 2069 | if (ret < 0) { |
| 2070 | ret = LTTCOMM_ENABLE_CONSUMER_FAIL; |
| 2071 | goto close_sock; |
| 2072 | } |
| 2073 | |
| 2074 | ret = LTTCOMM_OK; |
| 2075 | |
| 2076 | /* |
| 2077 | * Close socket which was dup on the consumer side. The session daemon does |
| 2078 | * NOT keep track of the relayd socket(s) once transfer to the consumer. |
| 2079 | */ |
| 2080 | |
| 2081 | close_sock: |
| 2082 | if (sock) { |
| 2083 | (void) relayd_close(sock); |
| 2084 | lttcomm_destroy_sock(sock); |
| 2085 | } |
| 2086 | |
| 2087 | return ret; |
| 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | * Send both relayd sockets to a specific consumer and domain. This is a |
| 2092 | * helper function to facilitate sending the information to the consumer for a |
| 2093 | * session. |
| 2094 | */ |
| 2095 | static int send_sockets_relayd_consumer(int domain, |
| 2096 | struct ltt_session *session, struct consumer_output *consumer, int fd) |
| 2097 | { |
| 2098 | int ret; |
| 2099 | |
| 2100 | assert(session); |
| 2101 | assert(consumer); |
| 2102 | |
| 2103 | /* Don't resend the sockets to the consumer. */ |
| 2104 | if (consumer->dst.net.relayd_socks_sent) { |
| 2105 | ret = LTTCOMM_OK; |
| 2106 | goto error; |
| 2107 | } |
| 2108 | |
| 2109 | /* Sending control relayd socket. */ |
| 2110 | ret = send_socket_relayd_consumer(domain, session, |
| 2111 | &consumer->dst.net.control, consumer, fd); |
| 2112 | if (ret != LTTCOMM_OK) { |
| 2113 | goto error; |
| 2114 | } |
| 2115 | |
| 2116 | /* Sending data relayd socket. */ |
| 2117 | ret = send_socket_relayd_consumer(domain, session, |
| 2118 | &consumer->dst.net.data, consumer, fd); |
| 2119 | if (ret != LTTCOMM_OK) { |
| 2120 | goto error; |
| 2121 | } |
| 2122 | |
| 2123 | /* Flag that all relayd sockets were sent to the consumer. */ |
| 2124 | consumer->dst.net.relayd_socks_sent = 1; |
| 2125 | |
| 2126 | error: |
| 2127 | return ret; |
| 2128 | } |
| 2129 | |
| 2130 | /* |
| 2131 | * Setup relayd connections for a tracing session. First creates the socket to |
| 2132 | * the relayd and send them to the right domain consumer. Consumer type MUST be |
| 2133 | * network. |
| 2134 | */ |
| 2135 | static int setup_relayd(struct ltt_session *session) |
| 2136 | { |
| 2137 | int ret = LTTCOMM_OK; |
| 2138 | struct ltt_ust_session *usess; |
| 2139 | struct ltt_kernel_session *ksess; |
| 2140 | struct consumer_socket *socket; |
| 2141 | struct lttng_ht_iter iter; |
| 2142 | |
| 2143 | assert(session); |
| 2144 | |
| 2145 | usess = session->ust_session; |
| 2146 | ksess = session->kernel_session; |
| 2147 | |
| 2148 | DBG2("Setting relayd for session %s", session->name); |
| 2149 | |
| 2150 | if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET |
| 2151 | && usess->consumer->enabled) { |
| 2152 | /* For each consumer socket, send relayd sockets */ |
| 2153 | cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter, |
| 2154 | socket, node.node) { |
| 2155 | /* Code flow error */ |
| 2156 | assert(socket->fd >= 0); |
| 2157 | |
| 2158 | pthread_mutex_lock(socket->lock); |
| 2159 | send_sockets_relayd_consumer(LTTNG_DOMAIN_UST, session, |
| 2160 | usess->consumer, socket->fd); |
| 2161 | pthread_mutex_unlock(socket->lock); |
| 2162 | if (ret != LTTCOMM_OK) { |
| 2163 | goto error; |
| 2164 | } |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET |
| 2169 | && ksess->consumer->enabled) { |
| 2170 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, |
| 2171 | socket, node.node) { |
| 2172 | /* Code flow error */ |
| 2173 | assert(socket->fd >= 0); |
| 2174 | |
| 2175 | pthread_mutex_lock(socket->lock); |
| 2176 | send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL, session, |
| 2177 | ksess->consumer, socket->fd); |
| 2178 | pthread_mutex_unlock(socket->lock); |
| 2179 | if (ret != LTTCOMM_OK) { |
| 2180 | goto error; |
| 2181 | } |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | error: |
| 2186 | return ret; |
| 2187 | } |
| 2188 | |
| 2189 | /* |
| 2190 | * Set consumer subdirectory using the session name and a generated datetime if |
| 2191 | * needed. This is appended to the current subdirectory. |
| 2192 | */ |
| 2193 | static int set_consumer_subdir(struct consumer_output *consumer, |
| 2194 | const char *session_name) |
| 2195 | { |
| 2196 | int ret = 0; |
| 2197 | unsigned int have_default_name = 0; |
| 2198 | char datetime[16], tmp_path[PATH_MAX]; |
| 2199 | time_t rawtime; |
| 2200 | struct tm *timeinfo; |
| 2201 | |
| 2202 | assert(consumer); |
| 2203 | assert(session_name); |
| 2204 | |
| 2205 | memset(tmp_path, 0, sizeof(tmp_path)); |
| 2206 | |
| 2207 | /* Flag if we have a default session. */ |
| 2208 | if (strncmp(session_name, DEFAULT_SESSION_NAME "-", |
| 2209 | strlen(DEFAULT_SESSION_NAME) + 1) == 0) { |
| 2210 | have_default_name = 1; |
| 2211 | } else { |
| 2212 | /* Get date and time for session path */ |
| 2213 | time(&rawtime); |
| 2214 | timeinfo = localtime(&rawtime); |
| 2215 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); |
| 2216 | } |
| 2217 | |
| 2218 | if (have_default_name) { |
| 2219 | ret = snprintf(tmp_path, sizeof(tmp_path), |
| 2220 | "%s/%s", consumer->subdir, session_name); |
| 2221 | } else { |
| 2222 | ret = snprintf(tmp_path, sizeof(tmp_path), |
| 2223 | "%s/%s-%s/", consumer->subdir, session_name, datetime); |
| 2224 | } |
| 2225 | if (ret < 0) { |
| 2226 | PERROR("snprintf session name date"); |
| 2227 | goto error; |
| 2228 | } |
| 2229 | |
| 2230 | strncpy(consumer->subdir, tmp_path, sizeof(consumer->subdir)); |
| 2231 | DBG2("Consumer subdir set to %s", consumer->subdir); |
| 2232 | |
| 2233 | error: |
| 2234 | return ret; |
| 2235 | } |
| 2236 | |
| 2237 | /* |
| 2238 | * Copy consumer output from the tracing session to the domain session. The |
| 2239 | * function also applies the right modification on a per domain basis for the |
| 2240 | * trace files destination directory. |
| 2241 | */ |
| 2242 | static int copy_session_consumer(int domain, struct ltt_session *session) |
| 2243 | { |
| 2244 | int ret; |
| 2245 | const char *dir_name; |
| 2246 | struct consumer_output *consumer; |
| 2247 | |
| 2248 | assert(session); |
| 2249 | assert(session->consumer); |
| 2250 | |
| 2251 | switch (domain) { |
| 2252 | case LTTNG_DOMAIN_KERNEL: |
| 2253 | DBG3("Copying tracing session consumer output in kernel session"); |
| 2254 | session->kernel_session->consumer = |
| 2255 | consumer_copy_output(session->consumer); |
| 2256 | /* Ease our life a bit for the next part */ |
| 2257 | consumer = session->kernel_session->consumer; |
| 2258 | dir_name = DEFAULT_KERNEL_TRACE_DIR; |
| 2259 | break; |
| 2260 | case LTTNG_DOMAIN_UST: |
| 2261 | DBG3("Copying tracing session consumer output in UST session"); |
| 2262 | session->ust_session->consumer = |
| 2263 | consumer_copy_output(session->consumer); |
| 2264 | /* Ease our life a bit for the next part */ |
| 2265 | consumer = session->ust_session->consumer; |
| 2266 | dir_name = DEFAULT_UST_TRACE_DIR; |
| 2267 | break; |
| 2268 | default: |
| 2269 | ret = LTTCOMM_UNKNOWN_DOMAIN; |
| 2270 | goto error; |
| 2271 | } |
| 2272 | |
| 2273 | ret = set_consumer_subdir(session->consumer, session->name); |
| 2274 | if (ret < 0) { |
| 2275 | ret = LTTCOMM_FATAL; |
| 2276 | goto error; |
| 2277 | } |
| 2278 | |
| 2279 | /* Append correct directory to subdir */ |
| 2280 | strncat(consumer->subdir, dir_name, sizeof(consumer->subdir)); |
| 2281 | DBG3("Copy session consumer subdir %s", consumer->subdir); |
| 2282 | |
| 2283 | ret = LTTCOMM_OK; |
| 2284 | |
| 2285 | error: |
| 2286 | return ret; |
| 2287 | } |
| 2288 | |
| 2289 | /* |
| 2290 | * Create an UST session and add it to the session ust list. |
| 2291 | */ |
| 2292 | static int create_ust_session(struct ltt_session *session, |
| 2293 | struct lttng_domain *domain) |
| 2294 | { |
| 2295 | int ret; |
| 2296 | struct ltt_ust_session *lus = NULL; |
| 2297 | |
| 2298 | assert(session); |
| 2299 | assert(domain); |
| 2300 | assert(session->consumer); |
| 2301 | |
| 2302 | switch (domain->type) { |
| 2303 | case LTTNG_DOMAIN_UST: |
| 2304 | break; |
| 2305 | default: |
| 2306 | ERR("Unknown UST domain on create session %d", domain->type); |
| 2307 | ret = LTTCOMM_UNKNOWN_DOMAIN; |
| 2308 | goto error; |
| 2309 | } |
| 2310 | |
| 2311 | DBG("Creating UST session"); |
| 2312 | |
| 2313 | lus = trace_ust_create_session(session->path, session->id, domain); |
| 2314 | if (lus == NULL) { |
| 2315 | ret = LTTCOMM_UST_SESS_FAIL; |
| 2316 | goto error; |
| 2317 | } |
| 2318 | |
| 2319 | lus->uid = session->uid; |
| 2320 | lus->gid = session->gid; |
| 2321 | session->ust_session = lus; |
| 2322 | |
| 2323 | /* Copy session output to the newly created UST session */ |
| 2324 | ret = copy_session_consumer(domain->type, session); |
| 2325 | if (ret != LTTCOMM_OK) { |
| 2326 | goto error; |
| 2327 | } |
| 2328 | |
| 2329 | return LTTCOMM_OK; |
| 2330 | |
| 2331 | error: |
| 2332 | free(lus); |
| 2333 | session->ust_session = NULL; |
| 2334 | return ret; |
| 2335 | } |
| 2336 | |
| 2337 | /* |
| 2338 | * Create a kernel tracer session then create the default channel. |
| 2339 | */ |
| 2340 | static int create_kernel_session(struct ltt_session *session) |
| 2341 | { |
| 2342 | int ret; |
| 2343 | |
| 2344 | DBG("Creating kernel session"); |
| 2345 | |
| 2346 | ret = kernel_create_session(session, kernel_tracer_fd); |
| 2347 | if (ret < 0) { |
| 2348 | ret = LTTCOMM_KERN_SESS_FAIL; |
| 2349 | goto error; |
| 2350 | } |
| 2351 | |
| 2352 | /* Code flow safety */ |
| 2353 | assert(session->kernel_session); |
| 2354 | |
| 2355 | /* Copy session output to the newly created Kernel session */ |
| 2356 | ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session); |
| 2357 | if (ret != LTTCOMM_OK) { |
| 2358 | goto error; |
| 2359 | } |
| 2360 | |
| 2361 | /* Create directory(ies) on local filesystem. */ |
| 2362 | if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL && |
| 2363 | strlen(session->kernel_session->consumer->dst.trace_path) > 0) { |
| 2364 | ret = run_as_mkdir_recursive( |
| 2365 | session->kernel_session->consumer->dst.trace_path, |
| 2366 | S_IRWXU | S_IRWXG, session->uid, session->gid); |
| 2367 | if (ret < 0) { |
| 2368 | if (ret != -EEXIST) { |
| 2369 | ERR("Trace directory creation error"); |
| 2370 | goto error; |
| 2371 | } |
| 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | session->kernel_session->uid = session->uid; |
| 2376 | session->kernel_session->gid = session->gid; |
| 2377 | |
| 2378 | return LTTCOMM_OK; |
| 2379 | |
| 2380 | error: |
| 2381 | trace_kernel_destroy_session(session->kernel_session); |
| 2382 | session->kernel_session = NULL; |
| 2383 | return ret; |
| 2384 | } |
| 2385 | |
| 2386 | /* |
| 2387 | * Check if the UID or GID match the session. Root user has access to all |
| 2388 | * sessions. |
| 2389 | */ |
| 2390 | static int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid) |
| 2391 | { |
| 2392 | if (uid != session->uid && gid != session->gid && uid != 0) { |
| 2393 | return 0; |
| 2394 | } else { |
| 2395 | return 1; |
| 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | /* |
| 2400 | * Count number of session permitted by uid/gid. |
| 2401 | */ |
| 2402 | static unsigned int lttng_sessions_count(uid_t uid, gid_t gid) |
| 2403 | { |
| 2404 | unsigned int i = 0; |
| 2405 | struct ltt_session *session; |
| 2406 | |
| 2407 | DBG("Counting number of available session for UID %d GID %d", |
| 2408 | uid, gid); |
| 2409 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
| 2410 | /* |
| 2411 | * Only list the sessions the user can control. |
| 2412 | */ |
| 2413 | if (!session_access_ok(session, uid, gid)) { |
| 2414 | continue; |
| 2415 | } |
| 2416 | i++; |
| 2417 | } |
| 2418 | return i; |
| 2419 | } |
| 2420 | |
| 2421 | /* |
| 2422 | * Create a session path used by list_lttng_sessions for the case that the |
| 2423 | * session consumer is on the network. |
| 2424 | */ |
| 2425 | static int build_network_session_path(char *dst, size_t size, |
| 2426 | struct ltt_session *session) |
| 2427 | { |
| 2428 | int ret, kdata_port, udata_port; |
| 2429 | struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL; |
| 2430 | char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX]; |
| 2431 | |
| 2432 | assert(session); |
| 2433 | assert(dst); |
| 2434 | |
| 2435 | memset(tmp_urls, 0, sizeof(tmp_urls)); |
| 2436 | memset(tmp_uurl, 0, sizeof(tmp_uurl)); |
| 2437 | |
| 2438 | kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT; |
| 2439 | |
| 2440 | if (session->kernel_session && session->kernel_session->consumer) { |
| 2441 | kuri = &session->kernel_session->consumer->dst.net.control; |
| 2442 | kdata_port = session->kernel_session->consumer->dst.net.data.port; |
| 2443 | } |
| 2444 | |
| 2445 | if (session->ust_session && session->ust_session->consumer) { |
| 2446 | uuri = &session->ust_session->consumer->dst.net.control; |
| 2447 | udata_port = session->ust_session->consumer->dst.net.data.port; |
| 2448 | } |
| 2449 | |
| 2450 | if (uuri == NULL && kuri == NULL) { |
| 2451 | uri = &session->consumer->dst.net.control; |
| 2452 | kdata_port = session->consumer->dst.net.data.port; |
| 2453 | } else if (kuri && uuri) { |
| 2454 | ret = uri_compare(kuri, uuri); |
| 2455 | if (ret) { |
| 2456 | /* Not Equal */ |
| 2457 | uri = kuri; |
| 2458 | /* Build uuri URL string */ |
| 2459 | ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl)); |
| 2460 | if (ret < 0) { |
| 2461 | goto error; |
| 2462 | } |
| 2463 | } else { |
| 2464 | uri = kuri; |
| 2465 | } |
| 2466 | } else if (kuri && uuri == NULL) { |
| 2467 | uri = kuri; |
| 2468 | } else if (uuri && kuri == NULL) { |
| 2469 | uri = uuri; |
| 2470 | } |
| 2471 | |
| 2472 | ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls)); |
| 2473 | if (ret < 0) { |
| 2474 | goto error; |
| 2475 | } |
| 2476 | |
| 2477 | if (strlen(tmp_uurl) > 0) { |
| 2478 | ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]", |
| 2479 | tmp_urls, kdata_port, tmp_uurl, udata_port); |
| 2480 | } else { |
| 2481 | ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, kdata_port); |
| 2482 | } |
| 2483 | |
| 2484 | error: |
| 2485 | return ret; |
| 2486 | } |
| 2487 | |
| 2488 | /* |
| 2489 | * Using the session list, filled a lttng_session array to send back to the |
| 2490 | * client for session listing. |
| 2491 | * |
| 2492 | * The session list lock MUST be acquired before calling this function. Use |
| 2493 | * session_lock_list() and session_unlock_list(). |
| 2494 | */ |
| 2495 | static void list_lttng_sessions(struct lttng_session *sessions, uid_t uid, |
| 2496 | gid_t gid) |
| 2497 | { |
| 2498 | int ret; |
| 2499 | unsigned int i = 0; |
| 2500 | struct ltt_session *session; |
| 2501 | |
| 2502 | DBG("Getting all available session for UID %d GID %d", |
| 2503 | uid, gid); |
| 2504 | /* |
| 2505 | * Iterate over session list and append data after the control struct in |
| 2506 | * the buffer. |
| 2507 | */ |
| 2508 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
| 2509 | /* |
| 2510 | * Only list the sessions the user can control. |
| 2511 | */ |
| 2512 | if (!session_access_ok(session, uid, gid)) { |
| 2513 | continue; |
| 2514 | } |
| 2515 | |
| 2516 | if (session->consumer->type == CONSUMER_DST_LOCAL && |
| 2517 | (!session->kernel_session && !session->ust_session)) { |
| 2518 | ret = snprintf(sessions[i].path, sizeof(session[i].path), "%s", |
| 2519 | session->consumer->dst.trace_path); |
| 2520 | } else { |
| 2521 | ret = build_network_session_path(sessions[i].path, |
| 2522 | sizeof(session[i].path), session); |
| 2523 | } |
| 2524 | if (ret < 0) { |
| 2525 | PERROR("snprintf session path"); |
| 2526 | continue; |
| 2527 | } |
| 2528 | |
| 2529 | strncpy(sessions[i].name, session->name, NAME_MAX); |
| 2530 | sessions[i].name[NAME_MAX - 1] = '\0'; |
| 2531 | sessions[i].enabled = session->enabled; |
| 2532 | i++; |
| 2533 | } |
| 2534 | } |
| 2535 | |
| 2536 | /* |
| 2537 | * Fill lttng_channel array of all channels. |
| 2538 | */ |
| 2539 | static void list_lttng_channels(int domain, struct ltt_session *session, |
| 2540 | struct lttng_channel *channels) |
| 2541 | { |
| 2542 | int i = 0; |
| 2543 | struct ltt_kernel_channel *kchan; |
| 2544 | |
| 2545 | DBG("Listing channels for session %s", session->name); |
| 2546 | |
| 2547 | switch (domain) { |
| 2548 | case LTTNG_DOMAIN_KERNEL: |
| 2549 | /* Kernel channels */ |
| 2550 | if (session->kernel_session != NULL) { |
| 2551 | cds_list_for_each_entry(kchan, |
| 2552 | &session->kernel_session->channel_list.head, list) { |
| 2553 | /* Copy lttng_channel struct to array */ |
| 2554 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); |
| 2555 | channels[i].enabled = kchan->enabled; |
| 2556 | i++; |
| 2557 | } |
| 2558 | } |
| 2559 | break; |
| 2560 | case LTTNG_DOMAIN_UST: |
| 2561 | { |
| 2562 | struct lttng_ht_iter iter; |
| 2563 | struct ltt_ust_channel *uchan; |
| 2564 | |
| 2565 | cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, |
| 2566 | &iter.iter, uchan, node.node) { |
| 2567 | strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN); |
| 2568 | channels[i].attr.overwrite = uchan->attr.overwrite; |
| 2569 | channels[i].attr.subbuf_size = uchan->attr.subbuf_size; |
| 2570 | channels[i].attr.num_subbuf = uchan->attr.num_subbuf; |
| 2571 | channels[i].attr.switch_timer_interval = |
| 2572 | uchan->attr.switch_timer_interval; |
| 2573 | channels[i].attr.read_timer_interval = |
| 2574 | uchan->attr.read_timer_interval; |
| 2575 | channels[i].enabled = uchan->enabled; |
| 2576 | switch (uchan->attr.output) { |
| 2577 | case LTTNG_UST_MMAP: |
| 2578 | default: |
| 2579 | channels[i].attr.output = LTTNG_EVENT_MMAP; |
| 2580 | break; |
| 2581 | } |
| 2582 | i++; |
| 2583 | } |
| 2584 | break; |
| 2585 | } |
| 2586 | default: |
| 2587 | break; |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | /* |
| 2592 | * Create a list of ust global domain events. |
| 2593 | */ |
| 2594 | static int list_lttng_ust_global_events(char *channel_name, |
| 2595 | struct ltt_ust_domain_global *ust_global, struct lttng_event **events) |
| 2596 | { |
| 2597 | int i = 0, ret = 0; |
| 2598 | unsigned int nb_event = 0; |
| 2599 | struct lttng_ht_iter iter; |
| 2600 | struct lttng_ht_node_str *node; |
| 2601 | struct ltt_ust_channel *uchan; |
| 2602 | struct ltt_ust_event *uevent; |
| 2603 | struct lttng_event *tmp; |
| 2604 | |
| 2605 | DBG("Listing UST global events for channel %s", channel_name); |
| 2606 | |
| 2607 | rcu_read_lock(); |
| 2608 | |
| 2609 | lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter); |
| 2610 | node = lttng_ht_iter_get_node_str(&iter); |
| 2611 | if (node == NULL) { |
| 2612 | ret = -LTTCOMM_UST_CHAN_NOT_FOUND; |
| 2613 | goto error; |
| 2614 | } |
| 2615 | |
| 2616 | uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); |
| 2617 | |
| 2618 | nb_event += lttng_ht_get_count(uchan->events); |
| 2619 | |
| 2620 | if (nb_event == 0) { |
| 2621 | ret = nb_event; |
| 2622 | goto error; |
| 2623 | } |
| 2624 | |
| 2625 | DBG3("Listing UST global %d events", nb_event); |
| 2626 | |
| 2627 | tmp = zmalloc(nb_event * sizeof(struct lttng_event)); |
| 2628 | if (tmp == NULL) { |
| 2629 | ret = -LTTCOMM_FATAL; |
| 2630 | goto error; |
| 2631 | } |
| 2632 | |
| 2633 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
| 2634 | strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN); |
| 2635 | tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 2636 | tmp[i].enabled = uevent->enabled; |
| 2637 | switch (uevent->attr.instrumentation) { |
| 2638 | case LTTNG_UST_TRACEPOINT: |
| 2639 | tmp[i].type = LTTNG_EVENT_TRACEPOINT; |
| 2640 | break; |
| 2641 | case LTTNG_UST_PROBE: |
| 2642 | tmp[i].type = LTTNG_EVENT_PROBE; |
| 2643 | break; |
| 2644 | case LTTNG_UST_FUNCTION: |
| 2645 | tmp[i].type = LTTNG_EVENT_FUNCTION; |
| 2646 | break; |
| 2647 | } |
| 2648 | tmp[i].loglevel = uevent->attr.loglevel; |
| 2649 | switch (uevent->attr.loglevel_type) { |
| 2650 | case LTTNG_UST_LOGLEVEL_ALL: |
| 2651 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 2652 | break; |
| 2653 | case LTTNG_UST_LOGLEVEL_RANGE: |
| 2654 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; |
| 2655 | break; |
| 2656 | case LTTNG_UST_LOGLEVEL_SINGLE: |
| 2657 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; |
| 2658 | break; |
| 2659 | } |
| 2660 | if (uevent->filter) { |
| 2661 | tmp[i].filter = 1; |
| 2662 | } |
| 2663 | i++; |
| 2664 | } |
| 2665 | |
| 2666 | ret = nb_event; |
| 2667 | *events = tmp; |
| 2668 | |
| 2669 | error: |
| 2670 | rcu_read_unlock(); |
| 2671 | return ret; |
| 2672 | } |
| 2673 | |
| 2674 | /* |
| 2675 | * Fill lttng_event array of all kernel events in the channel. |
| 2676 | */ |
| 2677 | static int list_lttng_kernel_events(char *channel_name, |
| 2678 | struct ltt_kernel_session *kernel_session, struct lttng_event **events) |
| 2679 | { |
| 2680 | int i = 0, ret; |
| 2681 | unsigned int nb_event; |
| 2682 | struct ltt_kernel_event *event; |
| 2683 | struct ltt_kernel_channel *kchan; |
| 2684 | |
| 2685 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); |
| 2686 | if (kchan == NULL) { |
| 2687 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 2688 | goto error; |
| 2689 | } |
| 2690 | |
| 2691 | nb_event = kchan->event_count; |
| 2692 | |
| 2693 | DBG("Listing events for channel %s", kchan->channel->name); |
| 2694 | |
| 2695 | if (nb_event == 0) { |
| 2696 | ret = nb_event; |
| 2697 | goto error; |
| 2698 | } |
| 2699 | |
| 2700 | *events = zmalloc(nb_event * sizeof(struct lttng_event)); |
| 2701 | if (*events == NULL) { |
| 2702 | ret = LTTCOMM_FATAL; |
| 2703 | goto error; |
| 2704 | } |
| 2705 | |
| 2706 | /* Kernel channels */ |
| 2707 | cds_list_for_each_entry(event, &kchan->events_list.head , list) { |
| 2708 | strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); |
| 2709 | (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 2710 | (*events)[i].enabled = event->enabled; |
| 2711 | switch (event->event->instrumentation) { |
| 2712 | case LTTNG_KERNEL_TRACEPOINT: |
| 2713 | (*events)[i].type = LTTNG_EVENT_TRACEPOINT; |
| 2714 | break; |
| 2715 | case LTTNG_KERNEL_KPROBE: |
| 2716 | case LTTNG_KERNEL_KRETPROBE: |
| 2717 | (*events)[i].type = LTTNG_EVENT_PROBE; |
| 2718 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, |
| 2719 | sizeof(struct lttng_kernel_kprobe)); |
| 2720 | break; |
| 2721 | case LTTNG_KERNEL_FUNCTION: |
| 2722 | (*events)[i].type = LTTNG_EVENT_FUNCTION; |
| 2723 | memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace, |
| 2724 | sizeof(struct lttng_kernel_function)); |
| 2725 | break; |
| 2726 | case LTTNG_KERNEL_NOOP: |
| 2727 | (*events)[i].type = LTTNG_EVENT_NOOP; |
| 2728 | break; |
| 2729 | case LTTNG_KERNEL_SYSCALL: |
| 2730 | (*events)[i].type = LTTNG_EVENT_SYSCALL; |
| 2731 | break; |
| 2732 | case LTTNG_KERNEL_ALL: |
| 2733 | assert(0); |
| 2734 | break; |
| 2735 | } |
| 2736 | i++; |
| 2737 | } |
| 2738 | |
| 2739 | return nb_event; |
| 2740 | |
| 2741 | error: |
| 2742 | return ret; |
| 2743 | } |
| 2744 | |
| 2745 | |
| 2746 | /* |
| 2747 | * Add URI so the consumer output object. Set the correct path depending on the |
| 2748 | * domain adding the default trace directory. |
| 2749 | */ |
| 2750 | static int add_uri_to_consumer(struct consumer_output *consumer, |
| 2751 | struct lttng_uri *uri, int domain, const char *session_name) |
| 2752 | { |
| 2753 | int ret = LTTCOMM_OK; |
| 2754 | const char *default_trace_dir; |
| 2755 | |
| 2756 | assert(uri); |
| 2757 | |
| 2758 | if (consumer == NULL) { |
| 2759 | DBG("No consumer detected. Don't add URI. Stopping."); |
| 2760 | ret = LTTCOMM_NO_CONSUMER; |
| 2761 | goto error; |
| 2762 | } |
| 2763 | |
| 2764 | switch (domain) { |
| 2765 | case LTTNG_DOMAIN_KERNEL: |
| 2766 | default_trace_dir = DEFAULT_KERNEL_TRACE_DIR; |
| 2767 | break; |
| 2768 | case LTTNG_DOMAIN_UST: |
| 2769 | default_trace_dir = DEFAULT_UST_TRACE_DIR; |
| 2770 | break; |
| 2771 | default: |
| 2772 | /* |
| 2773 | * This case is possible is we try to add the URI to the global tracing |
| 2774 | * session consumer object which in this case there is no subdir. |
| 2775 | */ |
| 2776 | default_trace_dir = ""; |
| 2777 | } |
| 2778 | |
| 2779 | switch (uri->dtype) { |
| 2780 | case LTTNG_DST_IPV4: |
| 2781 | case LTTNG_DST_IPV6: |
| 2782 | DBG2("Setting network URI to consumer"); |
| 2783 | |
| 2784 | /* Set URI into consumer output object */ |
| 2785 | ret = consumer_set_network_uri(consumer, uri); |
| 2786 | if (ret < 0) { |
| 2787 | ret = LTTCOMM_FATAL; |
| 2788 | goto error; |
| 2789 | } else if (ret == 1) { |
| 2790 | /* |
| 2791 | * URI was the same in the consumer so we do not append the subdir |
| 2792 | * again so to not duplicate output dir. |
| 2793 | */ |
| 2794 | goto error; |
| 2795 | } |
| 2796 | |
| 2797 | if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) { |
| 2798 | ret = set_consumer_subdir(consumer, session_name); |
| 2799 | if (ret < 0) { |
| 2800 | ret = LTTCOMM_FATAL; |
| 2801 | goto error; |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | if (uri->stype == LTTNG_STREAM_CONTROL) { |
| 2806 | /* On a new subdir, reappend the default trace dir. */ |
| 2807 | strncat(consumer->subdir, default_trace_dir, sizeof(consumer->subdir)); |
| 2808 | DBG3("Append domain trace name to subdir %s", consumer->subdir); |
| 2809 | } |
| 2810 | |
| 2811 | break; |
| 2812 | case LTTNG_DST_PATH: |
| 2813 | DBG2("Setting trace directory path from URI to %s", uri->dst.path); |
| 2814 | memset(consumer->dst.trace_path, 0, |
| 2815 | sizeof(consumer->dst.trace_path)); |
| 2816 | strncpy(consumer->dst.trace_path, uri->dst.path, |
| 2817 | sizeof(consumer->dst.trace_path)); |
| 2818 | /* Append default trace dir */ |
| 2819 | strncat(consumer->dst.trace_path, default_trace_dir, |
| 2820 | sizeof(consumer->dst.trace_path)); |
| 2821 | /* Flag consumer as local. */ |
| 2822 | consumer->type = CONSUMER_DST_LOCAL; |
| 2823 | break; |
| 2824 | } |
| 2825 | |
| 2826 | error: |
| 2827 | return ret; |
| 2828 | } |
| 2829 | |
| 2830 | /* |
| 2831 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. |
| 2832 | */ |
| 2833 | static int cmd_disable_channel(struct ltt_session *session, |
| 2834 | int domain, char *channel_name) |
| 2835 | { |
| 2836 | int ret; |
| 2837 | struct ltt_ust_session *usess; |
| 2838 | |
| 2839 | usess = session->ust_session; |
| 2840 | |
| 2841 | switch (domain) { |
| 2842 | case LTTNG_DOMAIN_KERNEL: |
| 2843 | { |
| 2844 | ret = channel_kernel_disable(session->kernel_session, |
| 2845 | channel_name); |
| 2846 | if (ret != LTTCOMM_OK) { |
| 2847 | goto error; |
| 2848 | } |
| 2849 | |
| 2850 | kernel_wait_quiescent(kernel_tracer_fd); |
| 2851 | break; |
| 2852 | } |
| 2853 | case LTTNG_DOMAIN_UST: |
| 2854 | { |
| 2855 | struct ltt_ust_channel *uchan; |
| 2856 | struct lttng_ht *chan_ht; |
| 2857 | |
| 2858 | chan_ht = usess->domain_global.channels; |
| 2859 | |
| 2860 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); |
| 2861 | if (uchan == NULL) { |
| 2862 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; |
| 2863 | goto error; |
| 2864 | } |
| 2865 | |
| 2866 | ret = channel_ust_disable(usess, domain, uchan); |
| 2867 | if (ret != LTTCOMM_OK) { |
| 2868 | goto error; |
| 2869 | } |
| 2870 | break; |
| 2871 | } |
| 2872 | #if 0 |
| 2873 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 2874 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 2875 | case LTTNG_DOMAIN_UST_PID: |
| 2876 | #endif |
| 2877 | default: |
| 2878 | ret = LTTCOMM_UNKNOWN_DOMAIN; |
| 2879 | goto error; |
| 2880 | } |
| 2881 | |
| 2882 | ret = LTTCOMM_OK; |
| 2883 | |
| 2884 | error: |
| 2885 | return ret; |
| 2886 | } |
| 2887 | |
| 2888 | /* |
| 2889 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. |
| 2890 | */ |
| 2891 | static int cmd_enable_channel(struct ltt_session *session, |
| 2892 | int domain, struct lttng_channel *attr) |
| 2893 | { |
| 2894 | int ret; |
| 2895 | struct ltt_ust_session *usess = session->ust_session; |
| 2896 | struct lttng_ht *chan_ht; |
| 2897 | |
| 2898 | DBG("Enabling channel %s for session %s", attr->name, session->name); |
| 2899 | |
| 2900 | switch (domain) { |
| 2901 | case LTTNG_DOMAIN_KERNEL: |
| 2902 | { |
| 2903 | struct ltt_kernel_channel *kchan; |
| 2904 | |
| 2905 | kchan = trace_kernel_get_channel_by_name(attr->name, |
| 2906 | session->kernel_session); |
| 2907 | if (kchan == NULL) { |
| 2908 | ret = channel_kernel_create(session->kernel_session, |
| 2909 | attr, kernel_poll_pipe[1]); |
| 2910 | } else { |
| 2911 | ret = channel_kernel_enable(session->kernel_session, kchan); |
| 2912 | } |
| 2913 | |
| 2914 | if (ret != LTTCOMM_OK) { |
| 2915 | goto error; |
| 2916 | } |
| 2917 | |
| 2918 | kernel_wait_quiescent(kernel_tracer_fd); |
| 2919 | break; |
| 2920 | } |
| 2921 | case LTTNG_DOMAIN_UST: |
| 2922 | { |
| 2923 | struct ltt_ust_channel *uchan; |
| 2924 | |
| 2925 | chan_ht = usess->domain_global.channels; |
| 2926 | |
| 2927 | uchan = trace_ust_find_channel_by_name(chan_ht, attr->name); |
| 2928 | if (uchan == NULL) { |
| 2929 | ret = channel_ust_create(usess, domain, attr); |
| 2930 | } else { |
| 2931 | ret = channel_ust_enable(usess, domain, uchan); |
| 2932 | } |
| 2933 | break; |
| 2934 | } |
| 2935 | #if 0 |
| 2936 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 2937 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 2938 | case LTTNG_DOMAIN_UST_PID: |
| 2939 | #endif |
| 2940 | default: |
| 2941 | ret = LTTCOMM_UNKNOWN_DOMAIN; |
| 2942 | goto error; |
| 2943 | } |
| 2944 | |
| 2945 | error: |
| 2946 | return ret; |
| 2947 | } |
| 2948 | |
| 2949 | /* |
| 2950 | * Command LTTNG_DISABLE_EVENT processed by the client thread. |
| 2951 | */ |
| 2952 | static int cmd_disable_event(struct ltt_session *session, int domain, |
| 2953 | char *channel_name, char *event_name) |
| 2954 | { |
| 2955 | int ret; |
| 2956 | |
| 2957 | switch (domain) { |
| 2958 | case LTTNG_DOMAIN_KERNEL: |
| 2959 | { |
| 2960 | struct ltt_kernel_channel *kchan; |
| 2961 | struct ltt_kernel_session *ksess; |
| 2962 | |
| 2963 | ksess = session->kernel_session; |
| 2964 | |
| 2965 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); |
| 2966 | if (kchan == NULL) { |
| 2967 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 2968 | goto error; |
| 2969 | } |
| 2970 | |
| 2971 | ret = event_kernel_disable_tracepoint(ksess, kchan, event_name); |
| 2972 | if (ret != LTTCOMM_OK) { |
| 2973 | goto error; |
| 2974 | } |
| 2975 | |
| 2976 | kernel_wait_quiescent(kernel_tracer_fd); |
| 2977 | break; |
| 2978 | } |
| 2979 | case LTTNG_DOMAIN_UST: |
| 2980 | { |
| 2981 | struct ltt_ust_channel *uchan; |
| 2982 | struct ltt_ust_session *usess; |
| 2983 | |
| 2984 | usess = session->ust_session; |
| 2985 | |
| 2986 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
| 2987 | channel_name); |
| 2988 | if (uchan == NULL) { |
| 2989 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; |
| 2990 | goto error; |
| 2991 | } |
| 2992 | |
| 2993 | ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name); |
| 2994 | if (ret != LTTCOMM_OK) { |
| 2995 | goto error; |
| 2996 | } |
| 2997 | |
| 2998 | DBG3("Disable UST event %s in channel %s completed", event_name, |
| 2999 | channel_name); |
| 3000 | break; |
| 3001 | } |
| 3002 | #if 0 |
| 3003 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 3004 | case LTTNG_DOMAIN_UST_PID: |
| 3005 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 3006 | #endif |
| 3007 | default: |
| 3008 | ret = LTTCOMM_UND; |
| 3009 | goto error; |
| 3010 | } |
| 3011 | |
| 3012 | ret = LTTCOMM_OK; |
| 3013 | |
| 3014 | error: |
| 3015 | return ret; |
| 3016 | } |
| 3017 | |
| 3018 | /* |
| 3019 | * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread. |
| 3020 | */ |
| 3021 | static int cmd_disable_event_all(struct ltt_session *session, int domain, |
| 3022 | char *channel_name) |
| 3023 | { |
| 3024 | int ret; |
| 3025 | |
| 3026 | switch (domain) { |
| 3027 | case LTTNG_DOMAIN_KERNEL: |
| 3028 | { |
| 3029 | struct ltt_kernel_session *ksess; |
| 3030 | struct ltt_kernel_channel *kchan; |
| 3031 | |
| 3032 | ksess = session->kernel_session; |
| 3033 | |
| 3034 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); |
| 3035 | if (kchan == NULL) { |
| 3036 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 3037 | goto error; |
| 3038 | } |
| 3039 | |
| 3040 | ret = event_kernel_disable_all(ksess, kchan); |
| 3041 | if (ret != LTTCOMM_OK) { |
| 3042 | goto error; |
| 3043 | } |
| 3044 | |
| 3045 | kernel_wait_quiescent(kernel_tracer_fd); |
| 3046 | break; |
| 3047 | } |
| 3048 | case LTTNG_DOMAIN_UST: |
| 3049 | { |
| 3050 | struct ltt_ust_session *usess; |
| 3051 | struct ltt_ust_channel *uchan; |
| 3052 | |
| 3053 | usess = session->ust_session; |
| 3054 | |
| 3055 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
| 3056 | channel_name); |
| 3057 | if (uchan == NULL) { |
| 3058 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; |
| 3059 | goto error; |
| 3060 | } |
| 3061 | |
| 3062 | ret = event_ust_disable_all_tracepoints(usess, domain, uchan); |
| 3063 | if (ret != 0) { |
| 3064 | goto error; |
| 3065 | } |
| 3066 | |
| 3067 | DBG3("Disable all UST events in channel %s completed", channel_name); |
| 3068 | |
| 3069 | break; |
| 3070 | } |
| 3071 | #if 0 |
| 3072 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 3073 | case LTTNG_DOMAIN_UST_PID: |
| 3074 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 3075 | #endif |
| 3076 | default: |
| 3077 | ret = LTTCOMM_UND; |
| 3078 | goto error; |
| 3079 | } |
| 3080 | |
| 3081 | ret = LTTCOMM_OK; |
| 3082 | |
| 3083 | error: |
| 3084 | return ret; |
| 3085 | } |
| 3086 | |
| 3087 | /* |
| 3088 | * Command LTTNG_ADD_CONTEXT processed by the client thread. |
| 3089 | */ |
| 3090 | static int cmd_add_context(struct ltt_session *session, int domain, |
| 3091 | char *channel_name, char *event_name, struct lttng_event_context *ctx) |
| 3092 | { |
| 3093 | int ret; |
| 3094 | |
| 3095 | switch (domain) { |
| 3096 | case LTTNG_DOMAIN_KERNEL: |
| 3097 | /* Add kernel context to kernel tracer */ |
| 3098 | ret = context_kernel_add(session->kernel_session, ctx, |
| 3099 | event_name, channel_name); |
| 3100 | if (ret != LTTCOMM_OK) { |
| 3101 | goto error; |
| 3102 | } |
| 3103 | break; |
| 3104 | case LTTNG_DOMAIN_UST: |
| 3105 | { |
| 3106 | struct ltt_ust_session *usess = session->ust_session; |
| 3107 | |
| 3108 | ret = context_ust_add(usess, domain, ctx, event_name, channel_name); |
| 3109 | if (ret != LTTCOMM_OK) { |
| 3110 | goto error; |
| 3111 | } |
| 3112 | break; |
| 3113 | } |
| 3114 | #if 0 |
| 3115 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 3116 | case LTTNG_DOMAIN_UST_PID: |
| 3117 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 3118 | #endif |
| 3119 | default: |
| 3120 | ret = LTTCOMM_UND; |
| 3121 | goto error; |
| 3122 | } |
| 3123 | |
| 3124 | ret = LTTCOMM_OK; |
| 3125 | |
| 3126 | error: |
| 3127 | return ret; |
| 3128 | } |
| 3129 | |
| 3130 | /* |
| 3131 | * Command LTTNG_SET_FILTER processed by the client thread. |
| 3132 | */ |
| 3133 | static int cmd_set_filter(struct ltt_session *session, int domain, |
| 3134 | char *channel_name, char *event_name, |
| 3135 | struct lttng_filter_bytecode *bytecode) |
| 3136 | { |
| 3137 | int ret; |
| 3138 | |
| 3139 | switch (domain) { |
| 3140 | case LTTNG_DOMAIN_KERNEL: |
| 3141 | ret = LTTCOMM_FATAL; |
| 3142 | break; |
| 3143 | case LTTNG_DOMAIN_UST: |
| 3144 | { |
| 3145 | struct ltt_ust_session *usess = session->ust_session; |
| 3146 | |
| 3147 | ret = filter_ust_set(usess, domain, bytecode, event_name, channel_name); |
| 3148 | if (ret != LTTCOMM_OK) { |
| 3149 | goto error; |
| 3150 | } |
| 3151 | break; |
| 3152 | } |
| 3153 | #if 0 |
| 3154 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 3155 | case LTTNG_DOMAIN_UST_PID: |
| 3156 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 3157 | #endif |
| 3158 | default: |
| 3159 | ret = LTTCOMM_UND; |
| 3160 | goto error; |
| 3161 | } |
| 3162 | |
| 3163 | ret = LTTCOMM_OK; |
| 3164 | |
| 3165 | error: |
| 3166 | return ret; |
| 3167 | |
| 3168 | } |
| 3169 | |
| 3170 | /* |
| 3171 | * Command LTTNG_ENABLE_EVENT processed by the client thread. |
| 3172 | */ |
| 3173 | static int cmd_enable_event(struct ltt_session *session, int domain, |
| 3174 | char *channel_name, struct lttng_event *event) |
| 3175 | { |
| 3176 | int ret; |
| 3177 | struct lttng_channel *attr; |
| 3178 | struct ltt_ust_session *usess = session->ust_session; |
| 3179 | |
| 3180 | switch (domain) { |
| 3181 | case LTTNG_DOMAIN_KERNEL: |
| 3182 | { |
| 3183 | struct ltt_kernel_channel *kchan; |
| 3184 | |
| 3185 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 3186 | session->kernel_session); |
| 3187 | if (kchan == NULL) { |
| 3188 | attr = channel_new_default_attr(domain); |
| 3189 | if (attr == NULL) { |
| 3190 | ret = LTTCOMM_FATAL; |
| 3191 | goto error; |
| 3192 | } |
| 3193 | snprintf(attr->name, NAME_MAX, "%s", channel_name); |
| 3194 | |
| 3195 | /* This call will notify the kernel thread */ |
| 3196 | ret = channel_kernel_create(session->kernel_session, |
| 3197 | attr, kernel_poll_pipe[1]); |
| 3198 | if (ret != LTTCOMM_OK) { |
| 3199 | free(attr); |
| 3200 | goto error; |
| 3201 | } |
| 3202 | free(attr); |
| 3203 | } |
| 3204 | |
| 3205 | /* Get the newly created kernel channel pointer */ |
| 3206 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 3207 | session->kernel_session); |
| 3208 | if (kchan == NULL) { |
| 3209 | /* This sould not happen... */ |
| 3210 | ret = LTTCOMM_FATAL; |
| 3211 | goto error; |
| 3212 | } |
| 3213 | |
| 3214 | ret = event_kernel_enable_tracepoint(session->kernel_session, kchan, |
| 3215 | event); |
| 3216 | if (ret != LTTCOMM_OK) { |
| 3217 | goto error; |
| 3218 | } |
| 3219 | |
| 3220 | kernel_wait_quiescent(kernel_tracer_fd); |
| 3221 | break; |
| 3222 | } |
| 3223 | case LTTNG_DOMAIN_UST: |
| 3224 | { |
| 3225 | struct lttng_channel *attr; |
| 3226 | struct ltt_ust_channel *uchan; |
| 3227 | |
| 3228 | /* Get channel from global UST domain */ |
| 3229 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
| 3230 | channel_name); |
| 3231 | if (uchan == NULL) { |
| 3232 | /* Create default channel */ |
| 3233 | attr = channel_new_default_attr(domain); |
| 3234 | if (attr == NULL) { |
| 3235 | ret = LTTCOMM_FATAL; |
| 3236 | goto error; |
| 3237 | } |
| 3238 | snprintf(attr->name, NAME_MAX, "%s", channel_name); |
| 3239 | attr->name[NAME_MAX - 1] = '\0'; |
| 3240 | |
| 3241 | ret = channel_ust_create(usess, domain, attr); |
| 3242 | if (ret != LTTCOMM_OK) { |
| 3243 | free(attr); |
| 3244 | goto error; |
| 3245 | } |
| 3246 | free(attr); |
| 3247 | |
| 3248 | /* Get the newly created channel reference back */ |
| 3249 | uchan = trace_ust_find_channel_by_name( |
| 3250 | usess->domain_global.channels, channel_name); |
| 3251 | if (uchan == NULL) { |
| 3252 | /* Something is really wrong */ |
| 3253 | ret = LTTCOMM_FATAL; |
| 3254 | goto error; |
| 3255 | } |
| 3256 | } |
| 3257 | |
| 3258 | /* At this point, the session and channel exist on the tracer */ |
| 3259 | ret = event_ust_enable_tracepoint(usess, domain, uchan, event); |
| 3260 | if (ret != LTTCOMM_OK) { |
| 3261 | goto error; |
| 3262 | } |
| 3263 | break; |
| 3264 | } |
| 3265 | #if 0 |
| 3266 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 3267 | case LTTNG_DOMAIN_UST_PID: |
| 3268 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 3269 | #endif |
| 3270 | default: |
| 3271 | ret = LTTCOMM_UND; |
| 3272 | goto error; |
| 3273 | } |
| 3274 | |
| 3275 | ret = LTTCOMM_OK; |
| 3276 | |
| 3277 | error: |
| 3278 | return ret; |
| 3279 | } |
| 3280 | |
| 3281 | /* |
| 3282 | * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread. |
| 3283 | */ |
| 3284 | static int cmd_enable_event_all(struct ltt_session *session, int domain, |
| 3285 | char *channel_name, int event_type) |
| 3286 | { |
| 3287 | int ret; |
| 3288 | struct ltt_kernel_channel *kchan; |
| 3289 | |
| 3290 | switch (domain) { |
| 3291 | case LTTNG_DOMAIN_KERNEL: |
| 3292 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 3293 | session->kernel_session); |
| 3294 | if (kchan == NULL) { |
| 3295 | /* This call will notify the kernel thread */ |
| 3296 | ret = channel_kernel_create(session->kernel_session, NULL, |
| 3297 | kernel_poll_pipe[1]); |
| 3298 | if (ret != LTTCOMM_OK) { |
| 3299 | goto error; |
| 3300 | } |
| 3301 | |
| 3302 | /* Get the newly created kernel channel pointer */ |
| 3303 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 3304 | session->kernel_session); |
| 3305 | if (kchan == NULL) { |
| 3306 | /* This sould not happen... */ |
| 3307 | ret = LTTCOMM_FATAL; |
| 3308 | goto error; |
| 3309 | } |
| 3310 | |
| 3311 | } |
| 3312 | |
| 3313 | switch (event_type) { |
| 3314 | case LTTNG_EVENT_SYSCALL: |
| 3315 | ret = event_kernel_enable_all_syscalls(session->kernel_session, |
| 3316 | kchan, kernel_tracer_fd); |
| 3317 | break; |
| 3318 | case LTTNG_EVENT_TRACEPOINT: |
| 3319 | /* |
| 3320 | * This call enables all LTTNG_KERNEL_TRACEPOINTS and |
| 3321 | * events already registered to the channel. |
| 3322 | */ |
| 3323 | ret = event_kernel_enable_all_tracepoints(session->kernel_session, |
| 3324 | kchan, kernel_tracer_fd); |
| 3325 | break; |
| 3326 | case LTTNG_EVENT_ALL: |
| 3327 | /* Enable syscalls and tracepoints */ |
| 3328 | ret = event_kernel_enable_all(session->kernel_session, |
| 3329 | kchan, kernel_tracer_fd); |
| 3330 | break; |
| 3331 | default: |
| 3332 | ret = LTTCOMM_KERN_ENABLE_FAIL; |
| 3333 | goto error; |
| 3334 | } |
| 3335 | |
| 3336 | /* Manage return value */ |
| 3337 | if (ret != LTTCOMM_OK) { |
| 3338 | goto error; |
| 3339 | } |
| 3340 | |
| 3341 | kernel_wait_quiescent(kernel_tracer_fd); |
| 3342 | break; |
| 3343 | case LTTNG_DOMAIN_UST: |
| 3344 | { |
| 3345 | struct lttng_channel *attr; |
| 3346 | struct ltt_ust_channel *uchan; |
| 3347 | struct ltt_ust_session *usess = session->ust_session; |
| 3348 | |
| 3349 | /* Get channel from global UST domain */ |
| 3350 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
| 3351 | channel_name); |
| 3352 | if (uchan == NULL) { |
| 3353 | /* Create default channel */ |
| 3354 | attr = channel_new_default_attr(domain); |
| 3355 | if (attr == NULL) { |
| 3356 | ret = LTTCOMM_FATAL; |
| 3357 | goto error; |
| 3358 | } |
| 3359 | snprintf(attr->name, NAME_MAX, "%s", channel_name); |
| 3360 | attr->name[NAME_MAX - 1] = '\0'; |
| 3361 | |
| 3362 | /* Use the internal command enable channel */ |
| 3363 | ret = channel_ust_create(usess, domain, attr); |
| 3364 | if (ret != LTTCOMM_OK) { |
| 3365 | free(attr); |
| 3366 | goto error; |
| 3367 | } |
| 3368 | free(attr); |
| 3369 | |
| 3370 | /* Get the newly created channel reference back */ |
| 3371 | uchan = trace_ust_find_channel_by_name( |
| 3372 | usess->domain_global.channels, channel_name); |
| 3373 | if (uchan == NULL) { |
| 3374 | /* Something is really wrong */ |
| 3375 | ret = LTTCOMM_FATAL; |
| 3376 | goto error; |
| 3377 | } |
| 3378 | } |
| 3379 | |
| 3380 | /* At this point, the session and channel exist on the tracer */ |
| 3381 | |
| 3382 | switch (event_type) { |
| 3383 | case LTTNG_EVENT_ALL: |
| 3384 | case LTTNG_EVENT_TRACEPOINT: |
| 3385 | ret = event_ust_enable_all_tracepoints(usess, domain, uchan); |
| 3386 | if (ret != LTTCOMM_OK) { |
| 3387 | goto error; |
| 3388 | } |
| 3389 | break; |
| 3390 | default: |
| 3391 | ret = LTTCOMM_UST_ENABLE_FAIL; |
| 3392 | goto error; |
| 3393 | } |
| 3394 | |
| 3395 | /* Manage return value */ |
| 3396 | if (ret != LTTCOMM_OK) { |
| 3397 | goto error; |
| 3398 | } |
| 3399 | |
| 3400 | break; |
| 3401 | } |
| 3402 | #if 0 |
| 3403 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 3404 | case LTTNG_DOMAIN_UST_PID: |
| 3405 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 3406 | #endif |
| 3407 | default: |
| 3408 | ret = LTTCOMM_UND; |
| 3409 | goto error; |
| 3410 | } |
| 3411 | |
| 3412 | ret = LTTCOMM_OK; |
| 3413 | |
| 3414 | error: |
| 3415 | return ret; |
| 3416 | } |
| 3417 | |
| 3418 | /* |
| 3419 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. |
| 3420 | */ |
| 3421 | static ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events) |
| 3422 | { |
| 3423 | int ret; |
| 3424 | ssize_t nb_events = 0; |
| 3425 | |
| 3426 | switch (domain) { |
| 3427 | case LTTNG_DOMAIN_KERNEL: |
| 3428 | nb_events = kernel_list_events(kernel_tracer_fd, events); |
| 3429 | if (nb_events < 0) { |
| 3430 | ret = LTTCOMM_KERN_LIST_FAIL; |
| 3431 | goto error; |
| 3432 | } |
| 3433 | break; |
| 3434 | case LTTNG_DOMAIN_UST: |
| 3435 | nb_events = ust_app_list_events(events); |
| 3436 | if (nb_events < 0) { |
| 3437 | ret = LTTCOMM_UST_LIST_FAIL; |
| 3438 | goto error; |
| 3439 | } |
| 3440 | break; |
| 3441 | default: |
| 3442 | ret = LTTCOMM_UND; |
| 3443 | goto error; |
| 3444 | } |
| 3445 | |
| 3446 | return nb_events; |
| 3447 | |
| 3448 | error: |
| 3449 | /* Return negative value to differentiate return code */ |
| 3450 | return -ret; |
| 3451 | } |
| 3452 | |
| 3453 | /* |
| 3454 | * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread. |
| 3455 | */ |
| 3456 | static ssize_t cmd_list_tracepoint_fields(int domain, |
| 3457 | struct lttng_event_field **fields) |
| 3458 | { |
| 3459 | int ret; |
| 3460 | ssize_t nb_fields = 0; |
| 3461 | |
| 3462 | switch (domain) { |
| 3463 | case LTTNG_DOMAIN_UST: |
| 3464 | nb_fields = ust_app_list_event_fields(fields); |
| 3465 | if (nb_fields < 0) { |
| 3466 | ret = LTTCOMM_UST_LIST_FAIL; |
| 3467 | goto error; |
| 3468 | } |
| 3469 | break; |
| 3470 | case LTTNG_DOMAIN_KERNEL: |
| 3471 | default: /* fall-through */ |
| 3472 | ret = LTTCOMM_UND; |
| 3473 | goto error; |
| 3474 | } |
| 3475 | |
| 3476 | return nb_fields; |
| 3477 | |
| 3478 | error: |
| 3479 | /* Return negative value to differentiate return code */ |
| 3480 | return -ret; |
| 3481 | } |
| 3482 | |
| 3483 | /* |
| 3484 | * Command LTTNG_START_TRACE processed by the client thread. |
| 3485 | */ |
| 3486 | static int cmd_start_trace(struct ltt_session *session) |
| 3487 | { |
| 3488 | int ret; |
| 3489 | struct ltt_kernel_session *ksession; |
| 3490 | struct ltt_ust_session *usess; |
| 3491 | struct ltt_kernel_channel *kchan; |
| 3492 | |
| 3493 | /* Ease our life a bit ;) */ |
| 3494 | ksession = session->kernel_session; |
| 3495 | usess = session->ust_session; |
| 3496 | |
| 3497 | if (session->enabled) { |
| 3498 | /* Already started. */ |
| 3499 | ret = LTTCOMM_TRACE_ALREADY_STARTED; |
| 3500 | goto error; |
| 3501 | } |
| 3502 | |
| 3503 | session->enabled = 1; |
| 3504 | |
| 3505 | ret = setup_relayd(session); |
| 3506 | if (ret != LTTCOMM_OK) { |
| 3507 | ERR("Error setting up relayd for session %s", session->name); |
| 3508 | goto error; |
| 3509 | } |
| 3510 | |
| 3511 | /* Kernel tracing */ |
| 3512 | if (ksession != NULL) { |
| 3513 | /* Open kernel metadata */ |
| 3514 | if (ksession->metadata == NULL) { |
| 3515 | ret = kernel_open_metadata(ksession); |
| 3516 | if (ret < 0) { |
| 3517 | ret = LTTCOMM_KERN_META_FAIL; |
| 3518 | goto error; |
| 3519 | } |
| 3520 | } |
| 3521 | |
| 3522 | /* Open kernel metadata stream */ |
| 3523 | if (ksession->metadata_stream_fd < 0) { |
| 3524 | ret = kernel_open_metadata_stream(ksession); |
| 3525 | if (ret < 0) { |
| 3526 | ERR("Kernel create metadata stream failed"); |
| 3527 | ret = LTTCOMM_KERN_STREAM_FAIL; |
| 3528 | goto error; |
| 3529 | } |
| 3530 | } |
| 3531 | |
| 3532 | /* For each channel */ |
| 3533 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
| 3534 | if (kchan->stream_count == 0) { |
| 3535 | ret = kernel_open_channel_stream(kchan); |
| 3536 | if (ret < 0) { |
| 3537 | ret = LTTCOMM_KERN_STREAM_FAIL; |
| 3538 | goto error; |
| 3539 | } |
| 3540 | /* Update the stream global counter */ |
| 3541 | ksession->stream_count_global += ret; |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | /* Setup kernel consumer socket and send fds to it */ |
| 3546 | ret = init_kernel_tracing(ksession); |
| 3547 | if (ret < 0) { |
| 3548 | ret = LTTCOMM_KERN_START_FAIL; |
| 3549 | goto error; |
| 3550 | } |
| 3551 | |
| 3552 | /* This start the kernel tracing */ |
| 3553 | ret = kernel_start_session(ksession); |
| 3554 | if (ret < 0) { |
| 3555 | ret = LTTCOMM_KERN_START_FAIL; |
| 3556 | goto error; |
| 3557 | } |
| 3558 | |
| 3559 | /* Quiescent wait after starting trace */ |
| 3560 | kernel_wait_quiescent(kernel_tracer_fd); |
| 3561 | } |
| 3562 | |
| 3563 | /* Flag session that trace should start automatically */ |
| 3564 | if (usess) { |
| 3565 | usess->start_trace = 1; |
| 3566 | |
| 3567 | ret = ust_app_start_trace_all(usess); |
| 3568 | if (ret < 0) { |
| 3569 | ret = LTTCOMM_UST_START_FAIL; |
| 3570 | goto error; |
| 3571 | } |
| 3572 | } |
| 3573 | |
| 3574 | ret = LTTCOMM_OK; |
| 3575 | |
| 3576 | error: |
| 3577 | return ret; |
| 3578 | } |
| 3579 | |
| 3580 | /* |
| 3581 | * Command LTTNG_STOP_TRACE processed by the client thread. |
| 3582 | */ |
| 3583 | static int cmd_stop_trace(struct ltt_session *session) |
| 3584 | { |
| 3585 | int ret; |
| 3586 | struct ltt_kernel_channel *kchan; |
| 3587 | struct ltt_kernel_session *ksession; |
| 3588 | struct ltt_ust_session *usess; |
| 3589 | |
| 3590 | /* Short cut */ |
| 3591 | ksession = session->kernel_session; |
| 3592 | usess = session->ust_session; |
| 3593 | |
| 3594 | if (!session->enabled) { |
| 3595 | ret = LTTCOMM_TRACE_ALREADY_STOPPED; |
| 3596 | goto error; |
| 3597 | } |
| 3598 | |
| 3599 | session->enabled = 0; |
| 3600 | |
| 3601 | /* Kernel tracer */ |
| 3602 | if (ksession != NULL) { |
| 3603 | DBG("Stop kernel tracing"); |
| 3604 | |
| 3605 | /* Flush metadata if exist */ |
| 3606 | if (ksession->metadata_stream_fd >= 0) { |
| 3607 | ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd); |
| 3608 | if (ret < 0) { |
| 3609 | ERR("Kernel metadata flush failed"); |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | /* Flush all buffers before stopping */ |
| 3614 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
| 3615 | ret = kernel_flush_buffer(kchan); |
| 3616 | if (ret < 0) { |
| 3617 | ERR("Kernel flush buffer error"); |
| 3618 | } |
| 3619 | } |
| 3620 | |
| 3621 | ret = kernel_stop_session(ksession); |
| 3622 | if (ret < 0) { |
| 3623 | ret = LTTCOMM_KERN_STOP_FAIL; |
| 3624 | goto error; |
| 3625 | } |
| 3626 | |
| 3627 | kernel_wait_quiescent(kernel_tracer_fd); |
| 3628 | } |
| 3629 | |
| 3630 | if (usess) { |
| 3631 | usess->start_trace = 0; |
| 3632 | |
| 3633 | ret = ust_app_stop_trace_all(usess); |
| 3634 | if (ret < 0) { |
| 3635 | ret = LTTCOMM_UST_STOP_FAIL; |
| 3636 | goto error; |
| 3637 | } |
| 3638 | } |
| 3639 | |
| 3640 | ret = LTTCOMM_OK; |
| 3641 | |
| 3642 | error: |
| 3643 | return ret; |
| 3644 | } |
| 3645 | |
| 3646 | /* |
| 3647 | * Command LTTNG_SET_CONSUMER_URI processed by the client thread. |
| 3648 | */ |
| 3649 | static int cmd_set_consumer_uri(int domain, struct ltt_session *session, |
| 3650 | size_t nb_uri, struct lttng_uri *uris) |
| 3651 | { |
| 3652 | int ret, i; |
| 3653 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 3654 | struct ltt_ust_session *usess = session->ust_session; |
| 3655 | struct consumer_output *consumer = NULL; |
| 3656 | |
| 3657 | assert(session); |
| 3658 | assert(uris); |
| 3659 | assert(nb_uri > 0); |
| 3660 | |
| 3661 | /* Can't enable consumer after session started. */ |
| 3662 | if (session->enabled) { |
| 3663 | ret = LTTCOMM_TRACE_ALREADY_STARTED; |
| 3664 | goto error; |
| 3665 | } |
| 3666 | |
| 3667 | if (!session->start_consumer) { |
| 3668 | ret = LTTCOMM_NO_CONSUMER; |
| 3669 | goto error; |
| 3670 | } |
| 3671 | |
| 3672 | /* |
| 3673 | * This case switch makes sure the domain session has a temporary consumer |
| 3674 | * so the URL can be set. |
| 3675 | */ |
| 3676 | switch (domain) { |
| 3677 | case 0: |
| 3678 | /* Code flow error. A session MUST always have a consumer object */ |
| 3679 | assert(session->consumer); |
| 3680 | /* |
| 3681 | * The URL will be added to the tracing session consumer instead of a |
| 3682 | * specific domain consumer. |
| 3683 | */ |
| 3684 | consumer = session->consumer; |
| 3685 | break; |
| 3686 | case LTTNG_DOMAIN_KERNEL: |
| 3687 | /* Code flow error if we don't have a kernel session here. */ |
| 3688 | assert(ksess); |
| 3689 | |
| 3690 | /* Create consumer output if none exists */ |
| 3691 | consumer = ksess->tmp_consumer; |
| 3692 | if (consumer == NULL) { |
| 3693 | consumer = consumer_copy_output(ksess->consumer); |
| 3694 | if (consumer == NULL) { |
| 3695 | ret = LTTCOMM_FATAL; |
| 3696 | goto error; |
| 3697 | } |
| 3698 | /* Trash the consumer subdir, we are about to set a new one. */ |
| 3699 | memset(consumer->subdir, 0, sizeof(consumer->subdir)); |
| 3700 | ksess->tmp_consumer = consumer; |
| 3701 | } |
| 3702 | |
| 3703 | break; |
| 3704 | case LTTNG_DOMAIN_UST: |
| 3705 | /* Code flow error if we don't have a kernel session here. */ |
| 3706 | assert(usess); |
| 3707 | |
| 3708 | /* Create consumer output if none exists */ |
| 3709 | consumer = usess->tmp_consumer; |
| 3710 | if (consumer == NULL) { |
| 3711 | consumer = consumer_copy_output(usess->consumer); |
| 3712 | if (consumer == NULL) { |
| 3713 | ret = LTTCOMM_FATAL; |
| 3714 | goto error; |
| 3715 | } |
| 3716 | /* Trash the consumer subdir, we are about to set a new one. */ |
| 3717 | memset(consumer->subdir, 0, sizeof(consumer->subdir)); |
| 3718 | usess->tmp_consumer = consumer; |
| 3719 | } |
| 3720 | |
| 3721 | break; |
| 3722 | } |
| 3723 | |
| 3724 | for (i = 0; i < nb_uri; i++) { |
| 3725 | struct consumer_socket *socket; |
| 3726 | struct lttng_ht_iter iter; |
| 3727 | |
| 3728 | ret = add_uri_to_consumer(consumer, &uris[i], domain, session->name); |
| 3729 | if (ret < 0) { |
| 3730 | goto error; |
| 3731 | } |
| 3732 | |
| 3733 | /* |
| 3734 | * Don't send relayd socket if URI is NOT remote or if the relayd |
| 3735 | * sockets for the session are already sent. |
| 3736 | */ |
| 3737 | if (uris[i].dtype == LTTNG_DST_PATH || |
| 3738 | consumer->dst.net.relayd_socks_sent) { |
| 3739 | continue; |
| 3740 | } |
| 3741 | |
| 3742 | /* Try to send relayd URI to the consumer if exist. */ |
| 3743 | cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, |
| 3744 | socket, node.node) { |
| 3745 | |
| 3746 | /* A socket in the HT should never have a negative fd */ |
| 3747 | assert(socket->fd >= 0); |
| 3748 | |
| 3749 | pthread_mutex_lock(socket->lock); |
| 3750 | ret = send_socket_relayd_consumer(domain, session, &uris[i], |
| 3751 | consumer, socket->fd); |
| 3752 | pthread_mutex_unlock(socket->lock); |
| 3753 | if (ret != LTTCOMM_OK) { |
| 3754 | goto error; |
| 3755 | } |
| 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | /* All good! */ |
| 3760 | ret = LTTCOMM_OK; |
| 3761 | |
| 3762 | error: |
| 3763 | return ret; |
| 3764 | } |
| 3765 | |
| 3766 | |
| 3767 | /* |
| 3768 | * Command LTTNG_CREATE_SESSION processed by the client thread. |
| 3769 | */ |
| 3770 | static int cmd_create_session_uri(char *name, struct lttng_uri *uris, |
| 3771 | size_t nb_uri, lttng_sock_cred *creds) |
| 3772 | { |
| 3773 | int ret; |
| 3774 | char *path = NULL; |
| 3775 | struct ltt_session *session; |
| 3776 | |
| 3777 | assert(name); |
| 3778 | |
| 3779 | /* |
| 3780 | * Verify if the session already exist |
| 3781 | * |
| 3782 | * XXX: There is no need for the session lock list here since the caller |
| 3783 | * (process_client_msg) is holding it. We might want to change that so a |
| 3784 | * single command does not lock the entire session list. |
| 3785 | */ |
| 3786 | session = session_find_by_name(name); |
| 3787 | if (session != NULL) { |
| 3788 | ret = LTTCOMM_EXIST_SESS; |
| 3789 | goto find_error; |
| 3790 | } |
| 3791 | |
| 3792 | /* Create tracing session in the registry */ |
| 3793 | ret = session_create(name, path, LTTNG_SOCK_GET_UID_CRED(creds), |
| 3794 | LTTNG_SOCK_GET_GID_CRED(creds)); |
| 3795 | if (ret != LTTCOMM_OK) { |
| 3796 | goto session_error; |
| 3797 | } |
| 3798 | |
| 3799 | /* |
| 3800 | * Get the newly created session pointer back |
| 3801 | * |
| 3802 | * XXX: There is no need for the session lock list here since the caller |
| 3803 | * (process_client_msg) is holding it. We might want to change that so a |
| 3804 | * single command does not lock the entire session list. |
| 3805 | */ |
| 3806 | session = session_find_by_name(name); |
| 3807 | assert(session); |
| 3808 | |
| 3809 | /* Create default consumer output for the session not yet created. */ |
| 3810 | session->consumer = consumer_create_output(CONSUMER_DST_LOCAL); |
| 3811 | if (session->consumer == NULL) { |
| 3812 | ret = LTTCOMM_FATAL; |
| 3813 | goto consumer_error; |
| 3814 | } |
| 3815 | |
| 3816 | /* |
| 3817 | * This means that the lttng_create_session call was called with the _path_ |
| 3818 | * argument set to NULL. |
| 3819 | */ |
| 3820 | if (uris == NULL) { |
| 3821 | /* |
| 3822 | * At this point, we'll skip the consumer URI setup and create a |
| 3823 | * session with a NULL path which will flag the session to NOT spawn a |
| 3824 | * consumer. |
| 3825 | */ |
| 3826 | DBG("Create session %s with NO uri, skipping consumer setup", name); |
| 3827 | goto end; |
| 3828 | } |
| 3829 | |
| 3830 | session->start_consumer = 1; |
| 3831 | |
| 3832 | ret = cmd_set_consumer_uri(0, session, nb_uri, uris); |
| 3833 | if (ret != LTTCOMM_OK) { |
| 3834 | goto consumer_error; |
| 3835 | } |
| 3836 | |
| 3837 | session->consumer->enabled = 1; |
| 3838 | |
| 3839 | end: |
| 3840 | return LTTCOMM_OK; |
| 3841 | |
| 3842 | consumer_error: |
| 3843 | session_destroy(session); |
| 3844 | session_error: |
| 3845 | find_error: |
| 3846 | return ret; |
| 3847 | } |
| 3848 | |
| 3849 | /* |
| 3850 | * Command LTTNG_DESTROY_SESSION processed by the client thread. |
| 3851 | */ |
| 3852 | static int cmd_destroy_session(struct ltt_session *session, char *name) |
| 3853 | { |
| 3854 | int ret; |
| 3855 | |
| 3856 | /* Safety net */ |
| 3857 | assert(session); |
| 3858 | |
| 3859 | /* Clean kernel session teardown */ |
| 3860 | teardown_kernel_session(session); |
| 3861 | /* UST session teardown */ |
| 3862 | teardown_ust_session(session); |
| 3863 | |
| 3864 | /* |
| 3865 | * Must notify the kernel thread here to update it's poll setin order |
| 3866 | * to remove the channel(s)' fd just destroyed. |
| 3867 | */ |
| 3868 | ret = notify_thread_pipe(kernel_poll_pipe[1]); |
| 3869 | if (ret < 0) { |
| 3870 | PERROR("write kernel poll pipe"); |
| 3871 | } |
| 3872 | |
| 3873 | ret = session_destroy(session); |
| 3874 | |
| 3875 | return ret; |
| 3876 | } |
| 3877 | |
| 3878 | /* |
| 3879 | * Command LTTNG_CALIBRATE processed by the client thread. |
| 3880 | */ |
| 3881 | static int cmd_calibrate(int domain, struct lttng_calibrate *calibrate) |
| 3882 | { |
| 3883 | int ret; |
| 3884 | |
| 3885 | switch (domain) { |
| 3886 | case LTTNG_DOMAIN_KERNEL: |
| 3887 | { |
| 3888 | struct lttng_kernel_calibrate kcalibrate; |
| 3889 | |
| 3890 | kcalibrate.type = calibrate->type; |
| 3891 | ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate); |
| 3892 | if (ret < 0) { |
| 3893 | ret = LTTCOMM_KERN_ENABLE_FAIL; |
| 3894 | goto error; |
| 3895 | } |
| 3896 | break; |
| 3897 | } |
| 3898 | case LTTNG_DOMAIN_UST: |
| 3899 | { |
| 3900 | struct lttng_ust_calibrate ucalibrate; |
| 3901 | |
| 3902 | ucalibrate.type = calibrate->type; |
| 3903 | ret = ust_app_calibrate_glb(&ucalibrate); |
| 3904 | if (ret < 0) { |
| 3905 | ret = LTTCOMM_UST_CALIBRATE_FAIL; |
| 3906 | goto error; |
| 3907 | } |
| 3908 | break; |
| 3909 | } |
| 3910 | default: |
| 3911 | ret = LTTCOMM_UND; |
| 3912 | goto error; |
| 3913 | } |
| 3914 | |
| 3915 | ret = LTTCOMM_OK; |
| 3916 | |
| 3917 | error: |
| 3918 | return ret; |
| 3919 | } |
| 3920 | |
| 3921 | /* |
| 3922 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. |
| 3923 | */ |
| 3924 | static int cmd_register_consumer(struct ltt_session *session, int domain, |
| 3925 | char *sock_path) |
| 3926 | { |
| 3927 | int ret, sock; |
| 3928 | struct consumer_socket *socket; |
| 3929 | |
| 3930 | switch (domain) { |
| 3931 | case LTTNG_DOMAIN_KERNEL: |
| 3932 | /* Can't register a consumer if there is already one */ |
| 3933 | if (session->kernel_session->consumer_fds_sent != 0) { |
| 3934 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
| 3935 | goto error; |
| 3936 | } |
| 3937 | |
| 3938 | sock = lttcomm_connect_unix_sock(sock_path); |
| 3939 | if (sock < 0) { |
| 3940 | ret = LTTCOMM_CONNECT_FAIL; |
| 3941 | goto error; |
| 3942 | } |
| 3943 | |
| 3944 | socket = consumer_allocate_socket(sock); |
| 3945 | if (socket == NULL) { |
| 3946 | ret = LTTCOMM_FATAL; |
| 3947 | close(sock); |
| 3948 | goto error; |
| 3949 | } |
| 3950 | |
| 3951 | socket->lock = zmalloc(sizeof(pthread_mutex_t)); |
| 3952 | if (socket->lock == NULL) { |
| 3953 | PERROR("zmalloc pthread mutex"); |
| 3954 | ret = LTTCOMM_FATAL; |
| 3955 | goto error; |
| 3956 | } |
| 3957 | pthread_mutex_init(socket->lock, NULL); |
| 3958 | |
| 3959 | rcu_read_lock(); |
| 3960 | consumer_add_socket(socket, session->kernel_session->consumer); |
| 3961 | rcu_read_unlock(); |
| 3962 | |
| 3963 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
| 3964 | kconsumer_data.pid = -1; |
| 3965 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
| 3966 | |
| 3967 | break; |
| 3968 | default: |
| 3969 | /* TODO: Userspace tracing */ |
| 3970 | ret = LTTCOMM_UND; |
| 3971 | goto error; |
| 3972 | } |
| 3973 | |
| 3974 | ret = LTTCOMM_OK; |
| 3975 | |
| 3976 | error: |
| 3977 | return ret; |
| 3978 | } |
| 3979 | |
| 3980 | /* |
| 3981 | * Command LTTNG_LIST_DOMAINS processed by the client thread. |
| 3982 | */ |
| 3983 | static ssize_t cmd_list_domains(struct ltt_session *session, |
| 3984 | struct lttng_domain **domains) |
| 3985 | { |
| 3986 | int ret, index = 0; |
| 3987 | ssize_t nb_dom = 0; |
| 3988 | |
| 3989 | if (session->kernel_session != NULL) { |
| 3990 | DBG3("Listing domains found kernel domain"); |
| 3991 | nb_dom++; |
| 3992 | } |
| 3993 | |
| 3994 | if (session->ust_session != NULL) { |
| 3995 | DBG3("Listing domains found UST global domain"); |
| 3996 | nb_dom++; |
| 3997 | } |
| 3998 | |
| 3999 | *domains = zmalloc(nb_dom * sizeof(struct lttng_domain)); |
| 4000 | if (*domains == NULL) { |
| 4001 | ret = -LTTCOMM_FATAL; |
| 4002 | goto error; |
| 4003 | } |
| 4004 | |
| 4005 | if (session->kernel_session != NULL) { |
| 4006 | (*domains)[index].type = LTTNG_DOMAIN_KERNEL; |
| 4007 | index++; |
| 4008 | } |
| 4009 | |
| 4010 | if (session->ust_session != NULL) { |
| 4011 | (*domains)[index].type = LTTNG_DOMAIN_UST; |
| 4012 | index++; |
| 4013 | } |
| 4014 | |
| 4015 | return nb_dom; |
| 4016 | |
| 4017 | error: |
| 4018 | return ret; |
| 4019 | } |
| 4020 | |
| 4021 | /* |
| 4022 | * Command LTTNG_LIST_CHANNELS processed by the client thread. |
| 4023 | */ |
| 4024 | static ssize_t cmd_list_channels(int domain, struct ltt_session *session, |
| 4025 | struct lttng_channel **channels) |
| 4026 | { |
| 4027 | int ret; |
| 4028 | ssize_t nb_chan = 0; |
| 4029 | |
| 4030 | switch (domain) { |
| 4031 | case LTTNG_DOMAIN_KERNEL: |
| 4032 | if (session->kernel_session != NULL) { |
| 4033 | nb_chan = session->kernel_session->channel_count; |
| 4034 | } |
| 4035 | DBG3("Number of kernel channels %zd", nb_chan); |
| 4036 | break; |
| 4037 | case LTTNG_DOMAIN_UST: |
| 4038 | if (session->ust_session != NULL) { |
| 4039 | nb_chan = lttng_ht_get_count( |
| 4040 | session->ust_session->domain_global.channels); |
| 4041 | } |
| 4042 | DBG3("Number of UST global channels %zd", nb_chan); |
| 4043 | break; |
| 4044 | default: |
| 4045 | *channels = NULL; |
| 4046 | ret = -LTTCOMM_UND; |
| 4047 | goto error; |
| 4048 | } |
| 4049 | |
| 4050 | if (nb_chan > 0) { |
| 4051 | *channels = zmalloc(nb_chan * sizeof(struct lttng_channel)); |
| 4052 | if (*channels == NULL) { |
| 4053 | ret = -LTTCOMM_FATAL; |
| 4054 | goto error; |
| 4055 | } |
| 4056 | |
| 4057 | list_lttng_channels(domain, session, *channels); |
| 4058 | } else { |
| 4059 | *channels = NULL; |
| 4060 | } |
| 4061 | |
| 4062 | return nb_chan; |
| 4063 | |
| 4064 | error: |
| 4065 | return ret; |
| 4066 | } |
| 4067 | |
| 4068 | /* |
| 4069 | * Command LTTNG_LIST_EVENTS processed by the client thread. |
| 4070 | */ |
| 4071 | static ssize_t cmd_list_events(int domain, struct ltt_session *session, |
| 4072 | char *channel_name, struct lttng_event **events) |
| 4073 | { |
| 4074 | int ret = 0; |
| 4075 | ssize_t nb_event = 0; |
| 4076 | |
| 4077 | switch (domain) { |
| 4078 | case LTTNG_DOMAIN_KERNEL: |
| 4079 | if (session->kernel_session != NULL) { |
| 4080 | nb_event = list_lttng_kernel_events(channel_name, |
| 4081 | session->kernel_session, events); |
| 4082 | } |
| 4083 | break; |
| 4084 | case LTTNG_DOMAIN_UST: |
| 4085 | { |
| 4086 | if (session->ust_session != NULL) { |
| 4087 | nb_event = list_lttng_ust_global_events(channel_name, |
| 4088 | &session->ust_session->domain_global, events); |
| 4089 | } |
| 4090 | break; |
| 4091 | } |
| 4092 | default: |
| 4093 | ret = -LTTCOMM_UND; |
| 4094 | goto error; |
| 4095 | } |
| 4096 | |
| 4097 | ret = nb_event; |
| 4098 | |
| 4099 | error: |
| 4100 | return ret; |
| 4101 | } |
| 4102 | |
| 4103 | /* |
| 4104 | * Command LTTNG_DISABLE_CONSUMER processed by the client thread. |
| 4105 | */ |
| 4106 | static int cmd_disable_consumer(int domain, struct ltt_session *session) |
| 4107 | { |
| 4108 | int ret; |
| 4109 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 4110 | struct ltt_ust_session *usess = session->ust_session; |
| 4111 | struct consumer_output *consumer; |
| 4112 | |
| 4113 | assert(session); |
| 4114 | |
| 4115 | if (session->enabled) { |
| 4116 | /* Can't disable consumer on an already started session */ |
| 4117 | ret = LTTCOMM_TRACE_ALREADY_STARTED; |
| 4118 | goto error; |
| 4119 | } |
| 4120 | |
| 4121 | if (!session->start_consumer) { |
| 4122 | ret = LTTCOMM_NO_CONSUMER; |
| 4123 | goto error; |
| 4124 | } |
| 4125 | |
| 4126 | switch (domain) { |
| 4127 | case 0: |
| 4128 | DBG("Disable tracing session %s consumer", session->name); |
| 4129 | consumer = session->consumer; |
| 4130 | break; |
| 4131 | case LTTNG_DOMAIN_KERNEL: |
| 4132 | /* Code flow error if we don't have a kernel session here. */ |
| 4133 | assert(ksess); |
| 4134 | |
| 4135 | DBG("Disabling kernel consumer"); |
| 4136 | consumer = ksess->consumer; |
| 4137 | |
| 4138 | break; |
| 4139 | case LTTNG_DOMAIN_UST: |
| 4140 | /* Code flow error if we don't have a UST session here. */ |
| 4141 | assert(usess); |
| 4142 | |
| 4143 | DBG("Disabling UST consumer"); |
| 4144 | consumer = usess->consumer; |
| 4145 | |
| 4146 | break; |
| 4147 | default: |
| 4148 | ret = LTTCOMM_UNKNOWN_DOMAIN; |
| 4149 | goto error; |
| 4150 | } |
| 4151 | |
| 4152 | if (consumer) { |
| 4153 | consumer->enabled = 0; |
| 4154 | /* Success at this point */ |
| 4155 | ret = LTTCOMM_OK; |
| 4156 | } else { |
| 4157 | ret = LTTCOMM_NO_CONSUMER; |
| 4158 | } |
| 4159 | |
| 4160 | error: |
| 4161 | return ret; |
| 4162 | } |
| 4163 | |
| 4164 | /* |
| 4165 | * Command LTTNG_ENABLE_CONSUMER processed by the client thread. |
| 4166 | */ |
| 4167 | static int cmd_enable_consumer(int domain, struct ltt_session *session) |
| 4168 | { |
| 4169 | int ret; |
| 4170 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 4171 | struct ltt_ust_session *usess = session->ust_session; |
| 4172 | struct consumer_output *consumer = NULL; |
| 4173 | |
| 4174 | assert(session); |
| 4175 | |
| 4176 | /* Can't enable consumer after session started. */ |
| 4177 | if (session->enabled) { |
| 4178 | ret = LTTCOMM_TRACE_ALREADY_STARTED; |
| 4179 | goto error; |
| 4180 | } |
| 4181 | |
| 4182 | if (!session->start_consumer) { |
| 4183 | ret = LTTCOMM_NO_CONSUMER; |
| 4184 | goto error; |
| 4185 | } |
| 4186 | |
| 4187 | switch (domain) { |
| 4188 | case 0: |
| 4189 | assert(session->consumer); |
| 4190 | consumer = session->consumer; |
| 4191 | break; |
| 4192 | case LTTNG_DOMAIN_KERNEL: |
| 4193 | /* Code flow error if we don't have a kernel session here. */ |
| 4194 | assert(ksess); |
| 4195 | |
| 4196 | /* |
| 4197 | * Check if we have already sent fds to the consumer. In that case, |
| 4198 | * the enable-consumer command can't be used because a start trace |
| 4199 | * had previously occured. |
| 4200 | */ |
| 4201 | if (ksess->consumer_fds_sent) { |
| 4202 | ret = LTTCOMM_ENABLE_CONSUMER_FAIL; |
| 4203 | goto error; |
| 4204 | } |
| 4205 | |
| 4206 | consumer = ksess->tmp_consumer; |
| 4207 | if (consumer == NULL) { |
| 4208 | ret = LTTCOMM_OK; |
| 4209 | /* No temp. consumer output exists. Using the current one. */ |
| 4210 | DBG3("No temporary consumer. Using default"); |
| 4211 | consumer = ksess->consumer; |
| 4212 | goto error; |
| 4213 | } |
| 4214 | |
| 4215 | switch (consumer->type) { |
| 4216 | case CONSUMER_DST_LOCAL: |
| 4217 | DBG2("Consumer output is local. Creating directory(ies)"); |
| 4218 | |
| 4219 | /* Create directory(ies) */ |
| 4220 | ret = run_as_mkdir_recursive(consumer->dst.trace_path, |
| 4221 | S_IRWXU | S_IRWXG, session->uid, session->gid); |
| 4222 | if (ret < 0) { |
| 4223 | if (ret != -EEXIST) { |
| 4224 | ERR("Trace directory creation error"); |
| 4225 | ret = LTTCOMM_FATAL; |
| 4226 | goto error; |
| 4227 | } |
| 4228 | } |
| 4229 | break; |
| 4230 | case CONSUMER_DST_NET: |
| 4231 | DBG2("Consumer output is network. Validating URIs"); |
| 4232 | /* Validate if we have both control and data path set. */ |
| 4233 | if (!consumer->dst.net.control_isset) { |
| 4234 | ret = LTTCOMM_URL_CTRL_MISS; |
| 4235 | goto error; |
| 4236 | } |
| 4237 | |
| 4238 | if (!consumer->dst.net.data_isset) { |
| 4239 | ret = LTTCOMM_URL_DATA_MISS; |
| 4240 | goto error; |
| 4241 | } |
| 4242 | |
| 4243 | /* Check established network session state */ |
| 4244 | if (session->net_handle == 0) { |
| 4245 | ret = LTTCOMM_ENABLE_CONSUMER_FAIL; |
| 4246 | ERR("Session network handle is not set on enable-consumer"); |
| 4247 | goto error; |
| 4248 | } |
| 4249 | |
| 4250 | break; |
| 4251 | } |
| 4252 | |
| 4253 | /* Append default kernel trace dir to subdir */ |
| 4254 | strncat(ksess->consumer->subdir, DEFAULT_KERNEL_TRACE_DIR, |
| 4255 | sizeof(ksess->consumer->subdir)); |
| 4256 | |
| 4257 | /* |
| 4258 | * @session-lock |
| 4259 | * This is race free for now since the session lock is acquired before |
| 4260 | * ending up in this function. No other threads can access this kernel |
| 4261 | * session without this lock hence freeing the consumer output object |
| 4262 | * is valid. |
| 4263 | */ |
| 4264 | rcu_read_lock(); |
| 4265 | consumer_destroy_output(ksess->consumer); |
| 4266 | rcu_read_unlock(); |
| 4267 | ksess->consumer = consumer; |
| 4268 | ksess->tmp_consumer = NULL; |
| 4269 | |
| 4270 | break; |
| 4271 | case LTTNG_DOMAIN_UST: |
| 4272 | /* Code flow error if we don't have a UST session here. */ |
| 4273 | assert(usess); |
| 4274 | |
| 4275 | /* |
| 4276 | * Check if we have already sent fds to the consumer. In that case, |
| 4277 | * the enable-consumer command can't be used because a start trace |
| 4278 | * had previously occured. |
| 4279 | */ |
| 4280 | if (usess->start_trace) { |
| 4281 | ret = LTTCOMM_ENABLE_CONSUMER_FAIL; |
| 4282 | goto error; |
| 4283 | } |
| 4284 | |
| 4285 | consumer = usess->tmp_consumer; |
| 4286 | if (consumer == NULL) { |
| 4287 | ret = LTTCOMM_OK; |
| 4288 | /* No temp. consumer output exists. Using the current one. */ |
| 4289 | DBG3("No temporary consumer. Using default"); |
| 4290 | consumer = usess->consumer; |
| 4291 | goto error; |
| 4292 | } |
| 4293 | |
| 4294 | switch (consumer->type) { |
| 4295 | case CONSUMER_DST_LOCAL: |
| 4296 | DBG2("Consumer output is local. Creating directory(ies)"); |
| 4297 | |
| 4298 | /* Create directory(ies) */ |
| 4299 | ret = run_as_mkdir_recursive(consumer->dst.trace_path, |
| 4300 | S_IRWXU | S_IRWXG, session->uid, session->gid); |
| 4301 | if (ret < 0) { |
| 4302 | if (ret != -EEXIST) { |
| 4303 | ERR("Trace directory creation error"); |
| 4304 | ret = LTTCOMM_FATAL; |
| 4305 | goto error; |
| 4306 | } |
| 4307 | } |
| 4308 | break; |
| 4309 | case CONSUMER_DST_NET: |
| 4310 | DBG2("Consumer output is network. Validating URIs"); |
| 4311 | /* Validate if we have both control and data path set. */ |
| 4312 | if (!consumer->dst.net.control_isset) { |
| 4313 | ret = LTTCOMM_URL_CTRL_MISS; |
| 4314 | goto error; |
| 4315 | } |
| 4316 | |
| 4317 | if (!consumer->dst.net.data_isset) { |
| 4318 | ret = LTTCOMM_URL_DATA_MISS; |
| 4319 | goto error; |
| 4320 | } |
| 4321 | |
| 4322 | /* Check established network session state */ |
| 4323 | if (session->net_handle == 0) { |
| 4324 | ret = LTTCOMM_ENABLE_CONSUMER_FAIL; |
| 4325 | DBG2("Session network handle is not set on enable-consumer"); |
| 4326 | goto error; |
| 4327 | } |
| 4328 | |
| 4329 | if (consumer->net_seq_index == -1) { |
| 4330 | ret = LTTCOMM_ENABLE_CONSUMER_FAIL; |
| 4331 | DBG2("Network index is not set on the consumer"); |
| 4332 | goto error; |
| 4333 | } |
| 4334 | |
| 4335 | break; |
| 4336 | } |
| 4337 | |
| 4338 | /* Append default kernel trace dir to subdir */ |
| 4339 | strncat(usess->consumer->subdir, DEFAULT_UST_TRACE_DIR, |
| 4340 | sizeof(usess->consumer->subdir)); |
| 4341 | |
| 4342 | /* |
| 4343 | * @session-lock |
| 4344 | * This is race free for now since the session lock is acquired before |
| 4345 | * ending up in this function. No other threads can access this kernel |
| 4346 | * session without this lock hence freeing the consumer output object |
| 4347 | * is valid. |
| 4348 | */ |
| 4349 | rcu_read_lock(); |
| 4350 | consumer_destroy_output(usess->consumer); |
| 4351 | rcu_read_unlock(); |
| 4352 | usess->consumer = consumer; |
| 4353 | usess->tmp_consumer = NULL; |
| 4354 | |
| 4355 | break; |
| 4356 | } |
| 4357 | |
| 4358 | /* Enable it */ |
| 4359 | if (consumer) { |
| 4360 | consumer->enabled = 1; |
| 4361 | /* Success at this point */ |
| 4362 | ret = LTTCOMM_OK; |
| 4363 | } else { |
| 4364 | /* Should not really happend... */ |
| 4365 | ret = LTTCOMM_NO_CONSUMER; |
| 4366 | } |
| 4367 | |
| 4368 | error: |
| 4369 | return ret; |
| 4370 | } |
| 4371 | |
| 4372 | /* |
| 4373 | * Process the command requested by the lttng client within the command |
| 4374 | * context structure. This function make sure that the return structure (llm) |
| 4375 | * is set and ready for transmission before returning. |
| 4376 | * |
| 4377 | * Return any error encountered or 0 for success. |
| 4378 | * |
| 4379 | * "sock" is only used for special-case var. len data. |
| 4380 | */ |
| 4381 | static int process_client_msg(struct command_ctx *cmd_ctx, int sock, |
| 4382 | int *sock_error) |
| 4383 | { |
| 4384 | int ret = LTTCOMM_OK; |
| 4385 | int need_tracing_session = 1; |
| 4386 | int need_domain; |
| 4387 | |
| 4388 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); |
| 4389 | |
| 4390 | *sock_error = 0; |
| 4391 | |
| 4392 | switch (cmd_ctx->lsm->cmd_type) { |
| 4393 | case LTTNG_CREATE_SESSION: |
| 4394 | case LTTNG_DESTROY_SESSION: |
| 4395 | case LTTNG_LIST_SESSIONS: |
| 4396 | case LTTNG_LIST_DOMAINS: |
| 4397 | case LTTNG_START_TRACE: |
| 4398 | case LTTNG_STOP_TRACE: |
| 4399 | need_domain = 0; |
| 4400 | break; |
| 4401 | default: |
| 4402 | need_domain = 1; |
| 4403 | } |
| 4404 | |
| 4405 | if (opt_no_kernel && need_domain |
| 4406 | && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) { |
| 4407 | if (!is_root) { |
| 4408 | ret = LTTCOMM_NEED_ROOT_SESSIOND; |
| 4409 | } else { |
| 4410 | ret = LTTCOMM_KERN_NA; |
| 4411 | } |
| 4412 | goto error; |
| 4413 | } |
| 4414 | |
| 4415 | /* Deny register consumer if we already have a spawned consumer. */ |
| 4416 | if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) { |
| 4417 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
| 4418 | if (kconsumer_data.pid > 0) { |
| 4419 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
| 4420 | goto error; |
| 4421 | } |
| 4422 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
| 4423 | } |
| 4424 | |
| 4425 | /* |
| 4426 | * Check for command that don't needs to allocate a returned payload. We do |
| 4427 | * this here so we don't have to make the call for no payload at each |
| 4428 | * command. |
| 4429 | */ |
| 4430 | switch(cmd_ctx->lsm->cmd_type) { |
| 4431 | case LTTNG_LIST_SESSIONS: |
| 4432 | case LTTNG_LIST_TRACEPOINTS: |
| 4433 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
| 4434 | case LTTNG_LIST_DOMAINS: |
| 4435 | case LTTNG_LIST_CHANNELS: |
| 4436 | case LTTNG_LIST_EVENTS: |
| 4437 | break; |
| 4438 | default: |
| 4439 | /* Setup lttng message with no payload */ |
| 4440 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 4441 | if (ret < 0) { |
| 4442 | /* This label does not try to unlock the session */ |
| 4443 | goto init_setup_error; |
| 4444 | } |
| 4445 | } |
| 4446 | |
| 4447 | /* Commands that DO NOT need a session. */ |
| 4448 | switch (cmd_ctx->lsm->cmd_type) { |
| 4449 | case LTTNG_CREATE_SESSION: |
| 4450 | case LTTNG_CALIBRATE: |
| 4451 | case LTTNG_LIST_SESSIONS: |
| 4452 | case LTTNG_LIST_TRACEPOINTS: |
| 4453 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
| 4454 | need_tracing_session = 0; |
| 4455 | break; |
| 4456 | default: |
| 4457 | DBG("Getting session %s by name", cmd_ctx->lsm->session.name); |
| 4458 | /* |
| 4459 | * We keep the session list lock across _all_ commands |
| 4460 | * for now, because the per-session lock does not |
| 4461 | * handle teardown properly. |
| 4462 | */ |
| 4463 | session_lock_list(); |
| 4464 | cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name); |
| 4465 | if (cmd_ctx->session == NULL) { |
| 4466 | if (cmd_ctx->lsm->session.name != NULL) { |
| 4467 | ret = LTTCOMM_SESS_NOT_FOUND; |
| 4468 | } else { |
| 4469 | /* If no session name specified */ |
| 4470 | ret = LTTCOMM_SELECT_SESS; |
| 4471 | } |
| 4472 | goto error; |
| 4473 | } else { |
| 4474 | /* Acquire lock for the session */ |
| 4475 | session_lock(cmd_ctx->session); |
| 4476 | } |
| 4477 | break; |
| 4478 | } |
| 4479 | |
| 4480 | if (!need_domain) { |
| 4481 | goto skip_domain; |
| 4482 | } |
| 4483 | |
| 4484 | /* |
| 4485 | * Check domain type for specific "pre-action". |
| 4486 | */ |
| 4487 | switch (cmd_ctx->lsm->domain.type) { |
| 4488 | case LTTNG_DOMAIN_KERNEL: |
| 4489 | if (!is_root) { |
| 4490 | ret = LTTCOMM_NEED_ROOT_SESSIOND; |
| 4491 | goto error; |
| 4492 | } |
| 4493 | |
| 4494 | /* Kernel tracer check */ |
| 4495 | if (kernel_tracer_fd == -1) { |
| 4496 | /* Basically, load kernel tracer modules */ |
| 4497 | ret = init_kernel_tracer(); |
| 4498 | if (ret != 0) { |
| 4499 | goto error; |
| 4500 | } |
| 4501 | } |
| 4502 | |
| 4503 | /* Consumer is in an ERROR state. Report back to client */ |
| 4504 | if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) { |
| 4505 | ret = LTTCOMM_NO_KERNCONSUMERD; |
| 4506 | goto error; |
| 4507 | } |
| 4508 | |
| 4509 | /* Need a session for kernel command */ |
| 4510 | if (need_tracing_session) { |
| 4511 | if (cmd_ctx->session->kernel_session == NULL) { |
| 4512 | ret = create_kernel_session(cmd_ctx->session); |
| 4513 | if (ret < 0) { |
| 4514 | ret = LTTCOMM_KERN_SESS_FAIL; |
| 4515 | goto error; |
| 4516 | } |
| 4517 | } |
| 4518 | |
| 4519 | /* Start the kernel consumer daemon */ |
| 4520 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
| 4521 | if (kconsumer_data.pid == 0 && |
| 4522 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER && |
| 4523 | cmd_ctx->session->start_consumer) { |
| 4524 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
| 4525 | ret = start_consumerd(&kconsumer_data); |
| 4526 | if (ret < 0) { |
| 4527 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
| 4528 | goto error; |
| 4529 | } |
| 4530 | uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED); |
| 4531 | } else { |
| 4532 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
| 4533 | } |
| 4534 | |
| 4535 | /* |
| 4536 | * The consumer was just spawned so we need to add the socket to |
| 4537 | * the consumer output of the session if exist. |
| 4538 | */ |
| 4539 | ret = consumer_create_socket(&kconsumer_data, |
| 4540 | cmd_ctx->session->kernel_session->consumer); |
| 4541 | if (ret < 0) { |
| 4542 | goto error; |
| 4543 | } |
| 4544 | } |
| 4545 | |
| 4546 | break; |
| 4547 | case LTTNG_DOMAIN_UST: |
| 4548 | { |
| 4549 | /* Consumer is in an ERROR state. Report back to client */ |
| 4550 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { |
| 4551 | ret = LTTCOMM_NO_USTCONSUMERD; |
| 4552 | goto error; |
| 4553 | } |
| 4554 | |
| 4555 | if (need_tracing_session) { |
| 4556 | /* Create UST session if none exist. */ |
| 4557 | if (cmd_ctx->session->ust_session == NULL) { |
| 4558 | ret = create_ust_session(cmd_ctx->session, |
| 4559 | &cmd_ctx->lsm->domain); |
| 4560 | if (ret != LTTCOMM_OK) { |
| 4561 | goto error; |
| 4562 | } |
| 4563 | } |
| 4564 | |
| 4565 | /* Start the UST consumer daemons */ |
| 4566 | /* 64-bit */ |
| 4567 | pthread_mutex_lock(&ustconsumer64_data.pid_mutex); |
| 4568 | if (consumerd64_bin[0] != '\0' && |
| 4569 | ustconsumer64_data.pid == 0 && |
| 4570 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER && |
| 4571 | cmd_ctx->session->start_consumer) { |
| 4572 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
| 4573 | ret = start_consumerd(&ustconsumer64_data); |
| 4574 | if (ret < 0) { |
| 4575 | ret = LTTCOMM_UST_CONSUMER64_FAIL; |
| 4576 | uatomic_set(&ust_consumerd64_fd, -EINVAL); |
| 4577 | goto error; |
| 4578 | } |
| 4579 | |
| 4580 | uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock); |
| 4581 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
| 4582 | } else { |
| 4583 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
| 4584 | } |
| 4585 | |
| 4586 | /* |
| 4587 | * Setup socket for consumer 64 bit. No need for atomic access |
| 4588 | * since it was set above and can ONLY be set in this thread. |
| 4589 | */ |
| 4590 | ret = consumer_create_socket(&ustconsumer64_data, |
| 4591 | cmd_ctx->session->ust_session->consumer); |
| 4592 | if (ret < 0) { |
| 4593 | goto error; |
| 4594 | } |
| 4595 | |
| 4596 | /* 32-bit */ |
| 4597 | if (consumerd32_bin[0] != '\0' && |
| 4598 | ustconsumer32_data.pid == 0 && |
| 4599 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER && |
| 4600 | cmd_ctx->session->start_consumer) { |
| 4601 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); |
| 4602 | ret = start_consumerd(&ustconsumer32_data); |
| 4603 | if (ret < 0) { |
| 4604 | ret = LTTCOMM_UST_CONSUMER32_FAIL; |
| 4605 | uatomic_set(&ust_consumerd32_fd, -EINVAL); |
| 4606 | goto error; |
| 4607 | } |
| 4608 | |
| 4609 | uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock); |
| 4610 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
| 4611 | } else { |
| 4612 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); |
| 4613 | } |
| 4614 | |
| 4615 | /* |
| 4616 | * Setup socket for consumer 64 bit. No need for atomic access |
| 4617 | * since it was set above and can ONLY be set in this thread. |
| 4618 | */ |
| 4619 | ret = consumer_create_socket(&ustconsumer32_data, |
| 4620 | cmd_ctx->session->ust_session->consumer); |
| 4621 | if (ret < 0) { |
| 4622 | goto error; |
| 4623 | } |
| 4624 | } |
| 4625 | break; |
| 4626 | } |
| 4627 | default: |
| 4628 | break; |
| 4629 | } |
| 4630 | skip_domain: |
| 4631 | |
| 4632 | /* Validate consumer daemon state when start/stop trace command */ |
| 4633 | if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE || |
| 4634 | cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) { |
| 4635 | switch (cmd_ctx->lsm->domain.type) { |
| 4636 | case LTTNG_DOMAIN_UST: |
| 4637 | if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) { |
| 4638 | ret = LTTCOMM_NO_USTCONSUMERD; |
| 4639 | goto error; |
| 4640 | } |
| 4641 | break; |
| 4642 | case LTTNG_DOMAIN_KERNEL: |
| 4643 | if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) { |
| 4644 | ret = LTTCOMM_NO_KERNCONSUMERD; |
| 4645 | goto error; |
| 4646 | } |
| 4647 | break; |
| 4648 | } |
| 4649 | } |
| 4650 | |
| 4651 | /* |
| 4652 | * Check that the UID or GID match that of the tracing session. |
| 4653 | * The root user can interact with all sessions. |
| 4654 | */ |
| 4655 | if (need_tracing_session) { |
| 4656 | if (!session_access_ok(cmd_ctx->session, |
| 4657 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
| 4658 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) { |
| 4659 | ret = LTTCOMM_EPERM; |
| 4660 | goto error; |
| 4661 | } |
| 4662 | } |
| 4663 | |
| 4664 | /* Process by command type */ |
| 4665 | switch (cmd_ctx->lsm->cmd_type) { |
| 4666 | case LTTNG_ADD_CONTEXT: |
| 4667 | { |
| 4668 | ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 4669 | cmd_ctx->lsm->u.context.channel_name, |
| 4670 | cmd_ctx->lsm->u.context.event_name, |
| 4671 | &cmd_ctx->lsm->u.context.ctx); |
| 4672 | break; |
| 4673 | } |
| 4674 | case LTTNG_DISABLE_CHANNEL: |
| 4675 | { |
| 4676 | ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 4677 | cmd_ctx->lsm->u.disable.channel_name); |
| 4678 | break; |
| 4679 | } |
| 4680 | case LTTNG_DISABLE_EVENT: |
| 4681 | { |
| 4682 | ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 4683 | cmd_ctx->lsm->u.disable.channel_name, |
| 4684 | cmd_ctx->lsm->u.disable.name); |
| 4685 | break; |
| 4686 | } |
| 4687 | case LTTNG_DISABLE_ALL_EVENT: |
| 4688 | { |
| 4689 | DBG("Disabling all events"); |
| 4690 | |
| 4691 | ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 4692 | cmd_ctx->lsm->u.disable.channel_name); |
| 4693 | break; |
| 4694 | } |
| 4695 | case LTTNG_DISABLE_CONSUMER: |
| 4696 | { |
| 4697 | ret = cmd_disable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session); |
| 4698 | break; |
| 4699 | } |
| 4700 | case LTTNG_ENABLE_CHANNEL: |
| 4701 | { |
| 4702 | ret = cmd_enable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 4703 | &cmd_ctx->lsm->u.channel.chan); |
| 4704 | break; |
| 4705 | } |
| 4706 | case LTTNG_ENABLE_CONSUMER: |
| 4707 | { |
| 4708 | /* |
| 4709 | * XXX: 0 means that this URI should be applied on the session. Should |
| 4710 | * be a DOMAIN enuam. |
| 4711 | */ |
| 4712 | ret = cmd_enable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session); |
| 4713 | if (ret != LTTCOMM_OK) { |
| 4714 | goto error; |
| 4715 | } |
| 4716 | |
| 4717 | if (cmd_ctx->lsm->domain.type == 0) { |
| 4718 | /* Add the URI for the UST session if a consumer is present. */ |
| 4719 | if (cmd_ctx->session->ust_session && |
| 4720 | cmd_ctx->session->ust_session->consumer) { |
| 4721 | ret = cmd_enable_consumer(LTTNG_DOMAIN_UST, cmd_ctx->session); |
| 4722 | } else if (cmd_ctx->session->kernel_session && |
| 4723 | cmd_ctx->session->kernel_session->consumer) { |
| 4724 | ret = cmd_enable_consumer(LTTNG_DOMAIN_KERNEL, |
| 4725 | cmd_ctx->session); |
| 4726 | } |
| 4727 | } |
| 4728 | break; |
| 4729 | } |
| 4730 | case LTTNG_ENABLE_EVENT: |
| 4731 | { |
| 4732 | ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 4733 | cmd_ctx->lsm->u.enable.channel_name, |
| 4734 | &cmd_ctx->lsm->u.enable.event); |
| 4735 | break; |
| 4736 | } |
| 4737 | case LTTNG_ENABLE_ALL_EVENT: |
| 4738 | { |
| 4739 | DBG("Enabling all events"); |
| 4740 | |
| 4741 | ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 4742 | cmd_ctx->lsm->u.enable.channel_name, |
| 4743 | cmd_ctx->lsm->u.enable.event.type); |
| 4744 | break; |
| 4745 | } |
| 4746 | case LTTNG_LIST_TRACEPOINTS: |
| 4747 | { |
| 4748 | struct lttng_event *events; |
| 4749 | ssize_t nb_events; |
| 4750 | |
| 4751 | nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events); |
| 4752 | if (nb_events < 0) { |
| 4753 | ret = -nb_events; |
| 4754 | goto error; |
| 4755 | } |
| 4756 | |
| 4757 | /* |
| 4758 | * Setup lttng message with payload size set to the event list size in |
| 4759 | * bytes and then copy list into the llm payload. |
| 4760 | */ |
| 4761 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events); |
| 4762 | if (ret < 0) { |
| 4763 | free(events); |
| 4764 | goto setup_error; |
| 4765 | } |
| 4766 | |
| 4767 | /* Copy event list into message payload */ |
| 4768 | memcpy(cmd_ctx->llm->payload, events, |
| 4769 | sizeof(struct lttng_event) * nb_events); |
| 4770 | |
| 4771 | free(events); |
| 4772 | |
| 4773 | ret = LTTCOMM_OK; |
| 4774 | break; |
| 4775 | } |
| 4776 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
| 4777 | { |
| 4778 | struct lttng_event_field *fields; |
| 4779 | ssize_t nb_fields; |
| 4780 | |
| 4781 | nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type, |
| 4782 | &fields); |
| 4783 | if (nb_fields < 0) { |
| 4784 | ret = -nb_fields; |
| 4785 | goto error; |
| 4786 | } |
| 4787 | |
| 4788 | /* |
| 4789 | * Setup lttng message with payload size set to the event list size in |
| 4790 | * bytes and then copy list into the llm payload. |
| 4791 | */ |
| 4792 | ret = setup_lttng_msg(cmd_ctx, |
| 4793 | sizeof(struct lttng_event_field) * nb_fields); |
| 4794 | if (ret < 0) { |
| 4795 | free(fields); |
| 4796 | goto setup_error; |
| 4797 | } |
| 4798 | |
| 4799 | /* Copy event list into message payload */ |
| 4800 | memcpy(cmd_ctx->llm->payload, fields, |
| 4801 | sizeof(struct lttng_event_field) * nb_fields); |
| 4802 | |
| 4803 | free(fields); |
| 4804 | |
| 4805 | ret = LTTCOMM_OK; |
| 4806 | break; |
| 4807 | } |
| 4808 | case LTTNG_SET_CONSUMER_URI: |
| 4809 | { |
| 4810 | size_t nb_uri, len; |
| 4811 | struct lttng_uri *uris; |
| 4812 | |
| 4813 | nb_uri = cmd_ctx->lsm->u.uri.size; |
| 4814 | len = nb_uri * sizeof(struct lttng_uri); |
| 4815 | |
| 4816 | if (nb_uri == 0) { |
| 4817 | ret = LTTCOMM_INVALID; |
| 4818 | goto error; |
| 4819 | } |
| 4820 | |
| 4821 | uris = zmalloc(len); |
| 4822 | if (uris == NULL) { |
| 4823 | ret = LTTCOMM_FATAL; |
| 4824 | goto error; |
| 4825 | } |
| 4826 | |
| 4827 | /* Receive variable len data */ |
| 4828 | DBG("Receiving %zu URI(s) from client ...", nb_uri); |
| 4829 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
| 4830 | if (ret <= 0) { |
| 4831 | DBG("No URIs received from client... continuing"); |
| 4832 | *sock_error = 1; |
| 4833 | ret = LTTCOMM_SESSION_FAIL; |
| 4834 | goto error; |
| 4835 | } |
| 4836 | |
| 4837 | ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session, |
| 4838 | nb_uri, uris); |
| 4839 | if (ret != LTTCOMM_OK) { |
| 4840 | goto error; |
| 4841 | } |
| 4842 | |
| 4843 | /* |
| 4844 | * XXX: 0 means that this URI should be applied on the session. Should |
| 4845 | * be a DOMAIN enuam. |
| 4846 | */ |
| 4847 | if (cmd_ctx->lsm->domain.type == 0) { |
| 4848 | /* Add the URI for the UST session if a consumer is present. */ |
| 4849 | if (cmd_ctx->session->ust_session && |
| 4850 | cmd_ctx->session->ust_session->consumer) { |
| 4851 | ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session, |
| 4852 | nb_uri, uris); |
| 4853 | } else if (cmd_ctx->session->kernel_session && |
| 4854 | cmd_ctx->session->kernel_session->consumer) { |
| 4855 | ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL, |
| 4856 | cmd_ctx->session, nb_uri, uris); |
| 4857 | } |
| 4858 | } |
| 4859 | |
| 4860 | break; |
| 4861 | } |
| 4862 | case LTTNG_START_TRACE: |
| 4863 | { |
| 4864 | ret = cmd_start_trace(cmd_ctx->session); |
| 4865 | break; |
| 4866 | } |
| 4867 | case LTTNG_STOP_TRACE: |
| 4868 | { |
| 4869 | ret = cmd_stop_trace(cmd_ctx->session); |
| 4870 | break; |
| 4871 | } |
| 4872 | case LTTNG_CREATE_SESSION: |
| 4873 | { |
| 4874 | size_t nb_uri, len; |
| 4875 | struct lttng_uri *uris = NULL; |
| 4876 | |
| 4877 | nb_uri = cmd_ctx->lsm->u.uri.size; |
| 4878 | len = nb_uri * sizeof(struct lttng_uri); |
| 4879 | |
| 4880 | if (nb_uri > 0) { |
| 4881 | uris = zmalloc(len); |
| 4882 | if (uris == NULL) { |
| 4883 | ret = LTTCOMM_FATAL; |
| 4884 | goto error; |
| 4885 | } |
| 4886 | |
| 4887 | /* Receive variable len data */ |
| 4888 | DBG("Waiting for %zu URIs from client ...", nb_uri); |
| 4889 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
| 4890 | if (ret <= 0) { |
| 4891 | DBG("No URIs received from client... continuing"); |
| 4892 | *sock_error = 1; |
| 4893 | ret = LTTCOMM_SESSION_FAIL; |
| 4894 | goto error; |
| 4895 | } |
| 4896 | |
| 4897 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { |
| 4898 | DBG("Creating session with ONE network URI is a bad call"); |
| 4899 | ret = LTTCOMM_SESSION_FAIL; |
| 4900 | goto error; |
| 4901 | } |
| 4902 | } |
| 4903 | |
| 4904 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri, |
| 4905 | &cmd_ctx->creds); |
| 4906 | |
| 4907 | break; |
| 4908 | } |
| 4909 | case LTTNG_DESTROY_SESSION: |
| 4910 | { |
| 4911 | ret = cmd_destroy_session(cmd_ctx->session, |
| 4912 | cmd_ctx->lsm->session.name); |
| 4913 | |
| 4914 | /* Set session to NULL so we do not unlock it after free. */ |
| 4915 | cmd_ctx->session = NULL; |
| 4916 | break; |
| 4917 | } |
| 4918 | case LTTNG_LIST_DOMAINS: |
| 4919 | { |
| 4920 | ssize_t nb_dom; |
| 4921 | struct lttng_domain *domains; |
| 4922 | |
| 4923 | nb_dom = cmd_list_domains(cmd_ctx->session, &domains); |
| 4924 | if (nb_dom < 0) { |
| 4925 | ret = -nb_dom; |
| 4926 | goto error; |
| 4927 | } |
| 4928 | |
| 4929 | ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain)); |
| 4930 | if (ret < 0) { |
| 4931 | goto setup_error; |
| 4932 | } |
| 4933 | |
| 4934 | /* Copy event list into message payload */ |
| 4935 | memcpy(cmd_ctx->llm->payload, domains, |
| 4936 | nb_dom * sizeof(struct lttng_domain)); |
| 4937 | |
| 4938 | free(domains); |
| 4939 | |
| 4940 | ret = LTTCOMM_OK; |
| 4941 | break; |
| 4942 | } |
| 4943 | case LTTNG_LIST_CHANNELS: |
| 4944 | { |
| 4945 | int nb_chan; |
| 4946 | struct lttng_channel *channels; |
| 4947 | |
| 4948 | nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type, |
| 4949 | cmd_ctx->session, &channels); |
| 4950 | if (nb_chan < 0) { |
| 4951 | ret = -nb_chan; |
| 4952 | goto error; |
| 4953 | } |
| 4954 | |
| 4955 | ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel)); |
| 4956 | if (ret < 0) { |
| 4957 | goto setup_error; |
| 4958 | } |
| 4959 | |
| 4960 | /* Copy event list into message payload */ |
| 4961 | memcpy(cmd_ctx->llm->payload, channels, |
| 4962 | nb_chan * sizeof(struct lttng_channel)); |
| 4963 | |
| 4964 | free(channels); |
| 4965 | |
| 4966 | ret = LTTCOMM_OK; |
| 4967 | break; |
| 4968 | } |
| 4969 | case LTTNG_LIST_EVENTS: |
| 4970 | { |
| 4971 | ssize_t nb_event; |
| 4972 | struct lttng_event *events = NULL; |
| 4973 | |
| 4974 | nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session, |
| 4975 | cmd_ctx->lsm->u.list.channel_name, &events); |
| 4976 | if (nb_event < 0) { |
| 4977 | ret = -nb_event; |
| 4978 | goto error; |
| 4979 | } |
| 4980 | |
| 4981 | ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event)); |
| 4982 | if (ret < 0) { |
| 4983 | goto setup_error; |
| 4984 | } |
| 4985 | |
| 4986 | /* Copy event list into message payload */ |
| 4987 | memcpy(cmd_ctx->llm->payload, events, |
| 4988 | nb_event * sizeof(struct lttng_event)); |
| 4989 | |
| 4990 | free(events); |
| 4991 | |
| 4992 | ret = LTTCOMM_OK; |
| 4993 | break; |
| 4994 | } |
| 4995 | case LTTNG_LIST_SESSIONS: |
| 4996 | { |
| 4997 | unsigned int nr_sessions; |
| 4998 | |
| 4999 | session_lock_list(); |
| 5000 | nr_sessions = lttng_sessions_count( |
| 5001 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
| 5002 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); |
| 5003 | |
| 5004 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions); |
| 5005 | if (ret < 0) { |
| 5006 | session_unlock_list(); |
| 5007 | goto setup_error; |
| 5008 | } |
| 5009 | |
| 5010 | /* Filled the session array */ |
| 5011 | list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload), |
| 5012 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
| 5013 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); |
| 5014 | |
| 5015 | session_unlock_list(); |
| 5016 | |
| 5017 | ret = LTTCOMM_OK; |
| 5018 | break; |
| 5019 | } |
| 5020 | case LTTNG_CALIBRATE: |
| 5021 | { |
| 5022 | ret = cmd_calibrate(cmd_ctx->lsm->domain.type, |
| 5023 | &cmd_ctx->lsm->u.calibrate); |
| 5024 | break; |
| 5025 | } |
| 5026 | case LTTNG_REGISTER_CONSUMER: |
| 5027 | { |
| 5028 | ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 5029 | cmd_ctx->lsm->u.reg.path); |
| 5030 | break; |
| 5031 | } |
| 5032 | case LTTNG_SET_FILTER: |
| 5033 | { |
| 5034 | struct lttng_filter_bytecode *bytecode; |
| 5035 | |
| 5036 | if (cmd_ctx->lsm->u.filter.bytecode_len > 65336) { |
| 5037 | ret = LTTCOMM_FILTER_INVAL; |
| 5038 | goto error; |
| 5039 | } |
| 5040 | bytecode = zmalloc(cmd_ctx->lsm->u.filter.bytecode_len); |
| 5041 | if (!bytecode) { |
| 5042 | ret = LTTCOMM_FILTER_NOMEM; |
| 5043 | goto error; |
| 5044 | } |
| 5045 | /* Receive var. len. data */ |
| 5046 | DBG("Receiving var len data from client ..."); |
| 5047 | ret = lttcomm_recv_unix_sock(sock, bytecode, |
| 5048 | cmd_ctx->lsm->u.filter.bytecode_len); |
| 5049 | if (ret <= 0) { |
| 5050 | DBG("Nothing recv() from client var len data... continuing"); |
| 5051 | *sock_error = 1; |
| 5052 | ret = LTTCOMM_FILTER_INVAL; |
| 5053 | goto error; |
| 5054 | } |
| 5055 | |
| 5056 | if (bytecode->len + sizeof(*bytecode) |
| 5057 | != cmd_ctx->lsm->u.filter.bytecode_len) { |
| 5058 | free(bytecode); |
| 5059 | ret = LTTCOMM_FILTER_INVAL; |
| 5060 | goto error; |
| 5061 | } |
| 5062 | |
| 5063 | ret = cmd_set_filter(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 5064 | cmd_ctx->lsm->u.filter.channel_name, |
| 5065 | cmd_ctx->lsm->u.filter.event_name, |
| 5066 | bytecode); |
| 5067 | break; |
| 5068 | } |
| 5069 | default: |
| 5070 | ret = LTTCOMM_UND; |
| 5071 | break; |
| 5072 | } |
| 5073 | |
| 5074 | error: |
| 5075 | if (cmd_ctx->llm == NULL) { |
| 5076 | DBG("Missing llm structure. Allocating one."); |
| 5077 | if (setup_lttng_msg(cmd_ctx, 0) < 0) { |
| 5078 | goto setup_error; |
| 5079 | } |
| 5080 | } |
| 5081 | /* Set return code */ |
| 5082 | cmd_ctx->llm->ret_code = ret; |
| 5083 | setup_error: |
| 5084 | if (cmd_ctx->session) { |
| 5085 | session_unlock(cmd_ctx->session); |
| 5086 | } |
| 5087 | if (need_tracing_session) { |
| 5088 | session_unlock_list(); |
| 5089 | } |
| 5090 | init_setup_error: |
| 5091 | return ret; |
| 5092 | } |
| 5093 | |
| 5094 | /* |
| 5095 | * Thread managing health check socket. |
| 5096 | */ |
| 5097 | static void *thread_manage_health(void *data) |
| 5098 | { |
| 5099 | int sock = -1, new_sock = -1, ret, i, pollfd, err = -1; |
| 5100 | uint32_t revents, nb_fd; |
| 5101 | struct lttng_poll_event events; |
| 5102 | struct lttcomm_health_msg msg; |
| 5103 | struct lttcomm_health_data reply; |
| 5104 | |
| 5105 | DBG("[thread] Manage health check started"); |
| 5106 | |
| 5107 | rcu_register_thread(); |
| 5108 | |
| 5109 | /* Create unix socket */ |
| 5110 | sock = lttcomm_create_unix_sock(health_unix_sock_path); |
| 5111 | if (sock < 0) { |
| 5112 | ERR("Unable to create health check Unix socket"); |
| 5113 | ret = -1; |
| 5114 | goto error; |
| 5115 | } |
| 5116 | |
| 5117 | ret = lttcomm_listen_unix_sock(sock); |
| 5118 | if (ret < 0) { |
| 5119 | goto error; |
| 5120 | } |
| 5121 | |
| 5122 | /* |
| 5123 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing |
| 5124 | * more will be added to this poll set. |
| 5125 | */ |
| 5126 | ret = create_thread_poll_set(&events, 2); |
| 5127 | if (ret < 0) { |
| 5128 | goto error; |
| 5129 | } |
| 5130 | |
| 5131 | /* Add the application registration socket */ |
| 5132 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI); |
| 5133 | if (ret < 0) { |
| 5134 | goto error; |
| 5135 | } |
| 5136 | |
| 5137 | while (1) { |
| 5138 | DBG("Health check ready"); |
| 5139 | |
| 5140 | nb_fd = LTTNG_POLL_GETNB(&events); |
| 5141 | |
| 5142 | /* Inifinite blocking call, waiting for transmission */ |
| 5143 | restart: |
| 5144 | ret = lttng_poll_wait(&events, -1); |
| 5145 | if (ret < 0) { |
| 5146 | /* |
| 5147 | * Restart interrupted system call. |
| 5148 | */ |
| 5149 | if (errno == EINTR) { |
| 5150 | goto restart; |
| 5151 | } |
| 5152 | goto error; |
| 5153 | } |
| 5154 | |
| 5155 | for (i = 0; i < nb_fd; i++) { |
| 5156 | /* Fetch once the poll data */ |
| 5157 | revents = LTTNG_POLL_GETEV(&events, i); |
| 5158 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 5159 | |
| 5160 | /* Thread quit pipe has been closed. Killing thread. */ |
| 5161 | ret = check_thread_quit_pipe(pollfd, revents); |
| 5162 | if (ret) { |
| 5163 | err = 0; |
| 5164 | goto exit; |
| 5165 | } |
| 5166 | |
| 5167 | /* Event on the registration socket */ |
| 5168 | if (pollfd == sock) { |
| 5169 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 5170 | ERR("Health socket poll error"); |
| 5171 | goto error; |
| 5172 | } |
| 5173 | } |
| 5174 | } |
| 5175 | |
| 5176 | new_sock = lttcomm_accept_unix_sock(sock); |
| 5177 | if (new_sock < 0) { |
| 5178 | goto error; |
| 5179 | } |
| 5180 | |
| 5181 | DBG("Receiving data from client for health..."); |
| 5182 | ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg)); |
| 5183 | if (ret <= 0) { |
| 5184 | DBG("Nothing recv() from client... continuing"); |
| 5185 | ret = close(new_sock); |
| 5186 | if (ret) { |
| 5187 | PERROR("close"); |
| 5188 | } |
| 5189 | new_sock = -1; |
| 5190 | continue; |
| 5191 | } |
| 5192 | |
| 5193 | rcu_thread_online(); |
| 5194 | |
| 5195 | switch (msg.component) { |
| 5196 | case LTTNG_HEALTH_CMD: |
| 5197 | reply.ret_code = health_check_state(&health_thread_cmd); |
| 5198 | break; |
| 5199 | case LTTNG_HEALTH_APP_MANAGE: |
| 5200 | reply.ret_code = health_check_state(&health_thread_app_manage); |
| 5201 | break; |
| 5202 | case LTTNG_HEALTH_APP_REG: |
| 5203 | reply.ret_code = health_check_state(&health_thread_app_reg); |
| 5204 | break; |
| 5205 | case LTTNG_HEALTH_KERNEL: |
| 5206 | reply.ret_code = health_check_state(&health_thread_kernel); |
| 5207 | break; |
| 5208 | case LTTNG_HEALTH_CONSUMER: |
| 5209 | reply.ret_code = check_consumer_health(); |
| 5210 | break; |
| 5211 | case LTTNG_HEALTH_ALL: |
| 5212 | reply.ret_code = |
| 5213 | health_check_state(&health_thread_app_manage) && |
| 5214 | health_check_state(&health_thread_app_reg) && |
| 5215 | health_check_state(&health_thread_cmd) && |
| 5216 | health_check_state(&health_thread_kernel) && |
| 5217 | check_consumer_health(); |
| 5218 | break; |
| 5219 | default: |
| 5220 | reply.ret_code = LTTCOMM_UND; |
| 5221 | break; |
| 5222 | } |
| 5223 | |
| 5224 | /* |
| 5225 | * Flip ret value since 0 is a success and 1 indicates a bad health for |
| 5226 | * the client where in the sessiond it is the opposite. Again, this is |
| 5227 | * just to make things easier for us poor developer which enjoy a lot |
| 5228 | * lazyness. |
| 5229 | */ |
| 5230 | if (reply.ret_code == 0 || reply.ret_code == 1) { |
| 5231 | reply.ret_code = !reply.ret_code; |
| 5232 | } |
| 5233 | |
| 5234 | DBG2("Health check return value %d", reply.ret_code); |
| 5235 | |
| 5236 | ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply)); |
| 5237 | if (ret < 0) { |
| 5238 | ERR("Failed to send health data back to client"); |
| 5239 | } |
| 5240 | |
| 5241 | /* End of transmission */ |
| 5242 | ret = close(new_sock); |
| 5243 | if (ret) { |
| 5244 | PERROR("close"); |
| 5245 | } |
| 5246 | new_sock = -1; |
| 5247 | } |
| 5248 | |
| 5249 | exit: |
| 5250 | error: |
| 5251 | if (err) { |
| 5252 | ERR("Health error occurred in %s", __func__); |
| 5253 | } |
| 5254 | DBG("Health check thread dying"); |
| 5255 | unlink(health_unix_sock_path); |
| 5256 | if (sock >= 0) { |
| 5257 | ret = close(sock); |
| 5258 | if (ret) { |
| 5259 | PERROR("close"); |
| 5260 | } |
| 5261 | } |
| 5262 | if (new_sock >= 0) { |
| 5263 | ret = close(new_sock); |
| 5264 | if (ret) { |
| 5265 | PERROR("close"); |
| 5266 | } |
| 5267 | } |
| 5268 | |
| 5269 | lttng_poll_clean(&events); |
| 5270 | |
| 5271 | rcu_unregister_thread(); |
| 5272 | return NULL; |
| 5273 | } |
| 5274 | |
| 5275 | /* |
| 5276 | * This thread manage all clients request using the unix client socket for |
| 5277 | * communication. |
| 5278 | */ |
| 5279 | static void *thread_manage_clients(void *data) |
| 5280 | { |
| 5281 | int sock = -1, ret, i, pollfd, err = -1; |
| 5282 | int sock_error; |
| 5283 | uint32_t revents, nb_fd; |
| 5284 | struct command_ctx *cmd_ctx = NULL; |
| 5285 | struct lttng_poll_event events; |
| 5286 | |
| 5287 | DBG("[thread] Manage client started"); |
| 5288 | |
| 5289 | rcu_register_thread(); |
| 5290 | |
| 5291 | health_code_update(&health_thread_cmd); |
| 5292 | |
| 5293 | ret = lttcomm_listen_unix_sock(client_sock); |
| 5294 | if (ret < 0) { |
| 5295 | goto error; |
| 5296 | } |
| 5297 | |
| 5298 | /* |
| 5299 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing |
| 5300 | * more will be added to this poll set. |
| 5301 | */ |
| 5302 | ret = create_thread_poll_set(&events, 2); |
| 5303 | if (ret < 0) { |
| 5304 | goto error; |
| 5305 | } |
| 5306 | |
| 5307 | /* Add the application registration socket */ |
| 5308 | ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI); |
| 5309 | if (ret < 0) { |
| 5310 | goto error; |
| 5311 | } |
| 5312 | |
| 5313 | /* |
| 5314 | * Notify parent pid that we are ready to accept command for client side. |
| 5315 | */ |
| 5316 | if (opt_sig_parent) { |
| 5317 | kill(ppid, SIGUSR1); |
| 5318 | } |
| 5319 | |
| 5320 | health_code_update(&health_thread_cmd); |
| 5321 | |
| 5322 | while (1) { |
| 5323 | DBG("Accepting client command ..."); |
| 5324 | |
| 5325 | nb_fd = LTTNG_POLL_GETNB(&events); |
| 5326 | |
| 5327 | /* Inifinite blocking call, waiting for transmission */ |
| 5328 | restart: |
| 5329 | health_poll_update(&health_thread_cmd); |
| 5330 | ret = lttng_poll_wait(&events, -1); |
| 5331 | health_poll_update(&health_thread_cmd); |
| 5332 | if (ret < 0) { |
| 5333 | /* |
| 5334 | * Restart interrupted system call. |
| 5335 | */ |
| 5336 | if (errno == EINTR) { |
| 5337 | goto restart; |
| 5338 | } |
| 5339 | goto error; |
| 5340 | } |
| 5341 | |
| 5342 | for (i = 0; i < nb_fd; i++) { |
| 5343 | /* Fetch once the poll data */ |
| 5344 | revents = LTTNG_POLL_GETEV(&events, i); |
| 5345 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 5346 | |
| 5347 | health_code_update(&health_thread_cmd); |
| 5348 | |
| 5349 | /* Thread quit pipe has been closed. Killing thread. */ |
| 5350 | ret = check_thread_quit_pipe(pollfd, revents); |
| 5351 | if (ret) { |
| 5352 | err = 0; |
| 5353 | goto exit; |
| 5354 | } |
| 5355 | |
| 5356 | /* Event on the registration socket */ |
| 5357 | if (pollfd == client_sock) { |
| 5358 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 5359 | ERR("Client socket poll error"); |
| 5360 | goto error; |
| 5361 | } |
| 5362 | } |
| 5363 | } |
| 5364 | |
| 5365 | DBG("Wait for client response"); |
| 5366 | |
| 5367 | health_code_update(&health_thread_cmd); |
| 5368 | |
| 5369 | sock = lttcomm_accept_unix_sock(client_sock); |
| 5370 | if (sock < 0) { |
| 5371 | goto error; |
| 5372 | } |
| 5373 | |
| 5374 | /* Set socket option for credentials retrieval */ |
| 5375 | ret = lttcomm_setsockopt_creds_unix_sock(sock); |
| 5376 | if (ret < 0) { |
| 5377 | goto error; |
| 5378 | } |
| 5379 | |
| 5380 | /* Allocate context command to process the client request */ |
| 5381 | cmd_ctx = zmalloc(sizeof(struct command_ctx)); |
| 5382 | if (cmd_ctx == NULL) { |
| 5383 | PERROR("zmalloc cmd_ctx"); |
| 5384 | goto error; |
| 5385 | } |
| 5386 | |
| 5387 | /* Allocate data buffer for reception */ |
| 5388 | cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg)); |
| 5389 | if (cmd_ctx->lsm == NULL) { |
| 5390 | PERROR("zmalloc cmd_ctx->lsm"); |
| 5391 | goto error; |
| 5392 | } |
| 5393 | |
| 5394 | cmd_ctx->llm = NULL; |
| 5395 | cmd_ctx->session = NULL; |
| 5396 | |
| 5397 | health_code_update(&health_thread_cmd); |
| 5398 | |
| 5399 | /* |
| 5400 | * Data is received from the lttng client. The struct |
| 5401 | * lttcomm_session_msg (lsm) contains the command and data request of |
| 5402 | * the client. |
| 5403 | */ |
| 5404 | DBG("Receiving data from client ..."); |
| 5405 | ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm, |
| 5406 | sizeof(struct lttcomm_session_msg), &cmd_ctx->creds); |
| 5407 | if (ret <= 0) { |
| 5408 | DBG("Nothing recv() from client... continuing"); |
| 5409 | ret = close(sock); |
| 5410 | if (ret) { |
| 5411 | PERROR("close"); |
| 5412 | } |
| 5413 | sock = -1; |
| 5414 | clean_command_ctx(&cmd_ctx); |
| 5415 | continue; |
| 5416 | } |
| 5417 | |
| 5418 | health_code_update(&health_thread_cmd); |
| 5419 | |
| 5420 | // TODO: Validate cmd_ctx including sanity check for |
| 5421 | // security purpose. |
| 5422 | |
| 5423 | rcu_thread_online(); |
| 5424 | /* |
| 5425 | * This function dispatch the work to the kernel or userspace tracer |
| 5426 | * libs and fill the lttcomm_lttng_msg data structure of all the needed |
| 5427 | * informations for the client. The command context struct contains |
| 5428 | * everything this function may needs. |
| 5429 | */ |
| 5430 | ret = process_client_msg(cmd_ctx, sock, &sock_error); |
| 5431 | rcu_thread_offline(); |
| 5432 | if (ret < 0) { |
| 5433 | if (sock_error) { |
| 5434 | ret = close(sock); |
| 5435 | if (ret) { |
| 5436 | PERROR("close"); |
| 5437 | } |
| 5438 | sock = -1; |
| 5439 | } |
| 5440 | /* |
| 5441 | * TODO: Inform client somehow of the fatal error. At |
| 5442 | * this point, ret < 0 means that a zmalloc failed |
| 5443 | * (ENOMEM). Error detected but still accept |
| 5444 | * command, unless a socket error has been |
| 5445 | * detected. |
| 5446 | */ |
| 5447 | clean_command_ctx(&cmd_ctx); |
| 5448 | continue; |
| 5449 | } |
| 5450 | |
| 5451 | health_code_update(&health_thread_cmd); |
| 5452 | |
| 5453 | DBG("Sending response (size: %d, retcode: %s)", |
| 5454 | cmd_ctx->lttng_msg_size, |
| 5455 | lttng_strerror(-cmd_ctx->llm->ret_code)); |
| 5456 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); |
| 5457 | if (ret < 0) { |
| 5458 | ERR("Failed to send data back to client"); |
| 5459 | } |
| 5460 | |
| 5461 | /* End of transmission */ |
| 5462 | ret = close(sock); |
| 5463 | if (ret) { |
| 5464 | PERROR("close"); |
| 5465 | } |
| 5466 | sock = -1; |
| 5467 | |
| 5468 | clean_command_ctx(&cmd_ctx); |
| 5469 | |
| 5470 | health_code_update(&health_thread_cmd); |
| 5471 | } |
| 5472 | |
| 5473 | exit: |
| 5474 | error: |
| 5475 | if (err) { |
| 5476 | health_error(&health_thread_cmd); |
| 5477 | ERR("Health error occurred in %s", __func__); |
| 5478 | } |
| 5479 | health_exit(&health_thread_cmd); |
| 5480 | |
| 5481 | DBG("Client thread dying"); |
| 5482 | unlink(client_unix_sock_path); |
| 5483 | if (client_sock >= 0) { |
| 5484 | ret = close(client_sock); |
| 5485 | if (ret) { |
| 5486 | PERROR("close"); |
| 5487 | } |
| 5488 | } |
| 5489 | if (sock >= 0) { |
| 5490 | ret = close(sock); |
| 5491 | if (ret) { |
| 5492 | PERROR("close"); |
| 5493 | } |
| 5494 | } |
| 5495 | |
| 5496 | lttng_poll_clean(&events); |
| 5497 | clean_command_ctx(&cmd_ctx); |
| 5498 | |
| 5499 | rcu_unregister_thread(); |
| 5500 | return NULL; |
| 5501 | } |
| 5502 | |
| 5503 | |
| 5504 | /* |
| 5505 | * usage function on stderr |
| 5506 | */ |
| 5507 | static void usage(void) |
| 5508 | { |
| 5509 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
| 5510 | fprintf(stderr, " -h, --help Display this usage.\n"); |
| 5511 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); |
| 5512 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n"); |
| 5513 | fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n"); |
| 5514 | fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n"); |
| 5515 | fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n"); |
| 5516 | fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n"); |
| 5517 | fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n"); |
| 5518 | fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n"); |
| 5519 | fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n"); |
| 5520 | fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n"); |
| 5521 | fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n"); |
| 5522 | fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n"); |
| 5523 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); |
| 5524 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); |
| 5525 | fprintf(stderr, " -V, --version Show version number.\n"); |
| 5526 | fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n"); |
| 5527 | fprintf(stderr, " -q, --quiet No output at all.\n"); |
| 5528 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); |
| 5529 | fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n"); |
| 5530 | fprintf(stderr, " --no-kernel Disable kernel tracer\n"); |
| 5531 | } |
| 5532 | |
| 5533 | /* |
| 5534 | * daemon argument parsing |
| 5535 | */ |
| 5536 | static int parse_args(int argc, char **argv) |
| 5537 | { |
| 5538 | int c; |
| 5539 | |
| 5540 | static struct option long_options[] = { |
| 5541 | { "client-sock", 1, 0, 'c' }, |
| 5542 | { "apps-sock", 1, 0, 'a' }, |
| 5543 | { "kconsumerd-cmd-sock", 1, 0, 'C' }, |
| 5544 | { "kconsumerd-err-sock", 1, 0, 'E' }, |
| 5545 | { "ustconsumerd32-cmd-sock", 1, 0, 'G' }, |
| 5546 | { "ustconsumerd32-err-sock", 1, 0, 'H' }, |
| 5547 | { "ustconsumerd64-cmd-sock", 1, 0, 'D' }, |
| 5548 | { "ustconsumerd64-err-sock", 1, 0, 'F' }, |
| 5549 | { "consumerd32-path", 1, 0, 'u' }, |
| 5550 | { "consumerd32-libdir", 1, 0, 'U' }, |
| 5551 | { "consumerd64-path", 1, 0, 't' }, |
| 5552 | { "consumerd64-libdir", 1, 0, 'T' }, |
| 5553 | { "daemonize", 0, 0, 'd' }, |
| 5554 | { "sig-parent", 0, 0, 'S' }, |
| 5555 | { "help", 0, 0, 'h' }, |
| 5556 | { "group", 1, 0, 'g' }, |
| 5557 | { "version", 0, 0, 'V' }, |
| 5558 | { "quiet", 0, 0, 'q' }, |
| 5559 | { "verbose", 0, 0, 'v' }, |
| 5560 | { "verbose-consumer", 0, 0, 'Z' }, |
| 5561 | { "no-kernel", 0, 0, 'N' }, |
| 5562 | { NULL, 0, 0, 0 } |
| 5563 | }; |
| 5564 | |
| 5565 | while (1) { |
| 5566 | int option_index = 0; |
| 5567 | c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t", |
| 5568 | long_options, &option_index); |
| 5569 | if (c == -1) { |
| 5570 | break; |
| 5571 | } |
| 5572 | |
| 5573 | switch (c) { |
| 5574 | case 0: |
| 5575 | fprintf(stderr, "option %s", long_options[option_index].name); |
| 5576 | if (optarg) { |
| 5577 | fprintf(stderr, " with arg %s\n", optarg); |
| 5578 | } |
| 5579 | break; |
| 5580 | case 'c': |
| 5581 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
| 5582 | break; |
| 5583 | case 'a': |
| 5584 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); |
| 5585 | break; |
| 5586 | case 'd': |
| 5587 | opt_daemon = 1; |
| 5588 | break; |
| 5589 | case 'g': |
| 5590 | opt_tracing_group = optarg; |
| 5591 | break; |
| 5592 | case 'h': |
| 5593 | usage(); |
| 5594 | exit(EXIT_FAILURE); |
| 5595 | case 'V': |
| 5596 | fprintf(stdout, "%s\n", VERSION); |
| 5597 | exit(EXIT_SUCCESS); |
| 5598 | case 'S': |
| 5599 | opt_sig_parent = 1; |
| 5600 | break; |
| 5601 | case 'E': |
| 5602 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
| 5603 | break; |
| 5604 | case 'C': |
| 5605 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
| 5606 | break; |
| 5607 | case 'F': |
| 5608 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
| 5609 | break; |
| 5610 | case 'D': |
| 5611 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
| 5612 | break; |
| 5613 | case 'H': |
| 5614 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
| 5615 | break; |
| 5616 | case 'G': |
| 5617 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
| 5618 | break; |
| 5619 | case 'N': |
| 5620 | opt_no_kernel = 1; |
| 5621 | break; |
| 5622 | case 'q': |
| 5623 | lttng_opt_quiet = 1; |
| 5624 | break; |
| 5625 | case 'v': |
| 5626 | /* Verbose level can increase using multiple -v */ |
| 5627 | lttng_opt_verbose += 1; |
| 5628 | break; |
| 5629 | case 'Z': |
| 5630 | opt_verbose_consumer += 1; |
| 5631 | break; |
| 5632 | case 'u': |
| 5633 | consumerd32_bin= optarg; |
| 5634 | break; |
| 5635 | case 'U': |
| 5636 | consumerd32_libdir = optarg; |
| 5637 | break; |
| 5638 | case 't': |
| 5639 | consumerd64_bin = optarg; |
| 5640 | break; |
| 5641 | case 'T': |
| 5642 | consumerd64_libdir = optarg; |
| 5643 | break; |
| 5644 | default: |
| 5645 | /* Unknown option or other error. |
| 5646 | * Error is printed by getopt, just return */ |
| 5647 | return -1; |
| 5648 | } |
| 5649 | } |
| 5650 | |
| 5651 | return 0; |
| 5652 | } |
| 5653 | |
| 5654 | /* |
| 5655 | * Creates the two needed socket by the daemon. |
| 5656 | * apps_sock - The communication socket for all UST apps. |
| 5657 | * client_sock - The communication of the cli tool (lttng). |
| 5658 | */ |
| 5659 | static int init_daemon_socket(void) |
| 5660 | { |
| 5661 | int ret = 0; |
| 5662 | mode_t old_umask; |
| 5663 | |
| 5664 | old_umask = umask(0); |
| 5665 | |
| 5666 | /* Create client tool unix socket */ |
| 5667 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
| 5668 | if (client_sock < 0) { |
| 5669 | ERR("Create unix sock failed: %s", client_unix_sock_path); |
| 5670 | ret = -1; |
| 5671 | goto end; |
| 5672 | } |
| 5673 | |
| 5674 | /* File permission MUST be 660 */ |
| 5675 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 5676 | if (ret < 0) { |
| 5677 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
| 5678 | PERROR("chmod"); |
| 5679 | goto end; |
| 5680 | } |
| 5681 | |
| 5682 | /* Create the application unix socket */ |
| 5683 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
| 5684 | if (apps_sock < 0) { |
| 5685 | ERR("Create unix sock failed: %s", apps_unix_sock_path); |
| 5686 | ret = -1; |
| 5687 | goto end; |
| 5688 | } |
| 5689 | |
| 5690 | /* File permission MUST be 666 */ |
| 5691 | ret = chmod(apps_unix_sock_path, |
| 5692 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 5693 | if (ret < 0) { |
| 5694 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
| 5695 | PERROR("chmod"); |
| 5696 | goto end; |
| 5697 | } |
| 5698 | |
| 5699 | end: |
| 5700 | umask(old_umask); |
| 5701 | return ret; |
| 5702 | } |
| 5703 | |
| 5704 | /* |
| 5705 | * Check if the global socket is available, and if a daemon is answering at the |
| 5706 | * other side. If yes, error is returned. |
| 5707 | */ |
| 5708 | static int check_existing_daemon(void) |
| 5709 | { |
| 5710 | /* Is there anybody out there ? */ |
| 5711 | if (lttng_session_daemon_alive()) { |
| 5712 | return -EEXIST; |
| 5713 | } |
| 5714 | |
| 5715 | return 0; |
| 5716 | } |
| 5717 | |
| 5718 | /* |
| 5719 | * Set the tracing group gid onto the client socket. |
| 5720 | * |
| 5721 | * Race window between mkdir and chown is OK because we are going from more |
| 5722 | * permissive (root.root) to less permissive (root.tracing). |
| 5723 | */ |
| 5724 | static int set_permissions(char *rundir) |
| 5725 | { |
| 5726 | int ret; |
| 5727 | gid_t gid; |
| 5728 | |
| 5729 | ret = allowed_group(); |
| 5730 | if (ret < 0) { |
| 5731 | WARN("No tracing group detected"); |
| 5732 | ret = 0; |
| 5733 | goto end; |
| 5734 | } |
| 5735 | |
| 5736 | gid = ret; |
| 5737 | |
| 5738 | /* Set lttng run dir */ |
| 5739 | ret = chown(rundir, 0, gid); |
| 5740 | if (ret < 0) { |
| 5741 | ERR("Unable to set group on %s", rundir); |
| 5742 | PERROR("chown"); |
| 5743 | } |
| 5744 | |
| 5745 | /* Ensure tracing group can search the run dir */ |
| 5746 | ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH); |
| 5747 | if (ret < 0) { |
| 5748 | ERR("Unable to set permissions on %s", rundir); |
| 5749 | PERROR("chmod"); |
| 5750 | } |
| 5751 | |
| 5752 | /* lttng client socket path */ |
| 5753 | ret = chown(client_unix_sock_path, 0, gid); |
| 5754 | if (ret < 0) { |
| 5755 | ERR("Unable to set group on %s", client_unix_sock_path); |
| 5756 | PERROR("chown"); |
| 5757 | } |
| 5758 | |
| 5759 | /* kconsumer error socket path */ |
| 5760 | ret = chown(kconsumer_data.err_unix_sock_path, 0, gid); |
| 5761 | if (ret < 0) { |
| 5762 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
| 5763 | PERROR("chown"); |
| 5764 | } |
| 5765 | |
| 5766 | /* 64-bit ustconsumer error socket path */ |
| 5767 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid); |
| 5768 | if (ret < 0) { |
| 5769 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); |
| 5770 | PERROR("chown"); |
| 5771 | } |
| 5772 | |
| 5773 | /* 32-bit ustconsumer compat32 error socket path */ |
| 5774 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid); |
| 5775 | if (ret < 0) { |
| 5776 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
| 5777 | PERROR("chown"); |
| 5778 | } |
| 5779 | |
| 5780 | DBG("All permissions are set"); |
| 5781 | |
| 5782 | end: |
| 5783 | return ret; |
| 5784 | } |
| 5785 | |
| 5786 | /* |
| 5787 | * Create the lttng run directory needed for all global sockets and pipe. |
| 5788 | */ |
| 5789 | static int create_lttng_rundir(const char *rundir) |
| 5790 | { |
| 5791 | int ret; |
| 5792 | |
| 5793 | DBG3("Creating LTTng run directory: %s", rundir); |
| 5794 | |
| 5795 | ret = mkdir(rundir, S_IRWXU); |
| 5796 | if (ret < 0) { |
| 5797 | if (errno != EEXIST) { |
| 5798 | ERR("Unable to create %s", rundir); |
| 5799 | goto error; |
| 5800 | } else { |
| 5801 | ret = 0; |
| 5802 | } |
| 5803 | } |
| 5804 | |
| 5805 | error: |
| 5806 | return ret; |
| 5807 | } |
| 5808 | |
| 5809 | /* |
| 5810 | * Setup sockets and directory needed by the kconsumerd communication with the |
| 5811 | * session daemon. |
| 5812 | */ |
| 5813 | static int set_consumer_sockets(struct consumer_data *consumer_data, |
| 5814 | const char *rundir) |
| 5815 | { |
| 5816 | int ret; |
| 5817 | char path[PATH_MAX]; |
| 5818 | |
| 5819 | switch (consumer_data->type) { |
| 5820 | case LTTNG_CONSUMER_KERNEL: |
| 5821 | snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir); |
| 5822 | break; |
| 5823 | case LTTNG_CONSUMER64_UST: |
| 5824 | snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir); |
| 5825 | break; |
| 5826 | case LTTNG_CONSUMER32_UST: |
| 5827 | snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir); |
| 5828 | break; |
| 5829 | default: |
| 5830 | ERR("Consumer type unknown"); |
| 5831 | ret = -EINVAL; |
| 5832 | goto error; |
| 5833 | } |
| 5834 | |
| 5835 | DBG2("Creating consumer directory: %s", path); |
| 5836 | |
| 5837 | ret = mkdir(path, S_IRWXU); |
| 5838 | if (ret < 0) { |
| 5839 | if (errno != EEXIST) { |
| 5840 | PERROR("mkdir"); |
| 5841 | ERR("Failed to create %s", path); |
| 5842 | goto error; |
| 5843 | } |
| 5844 | ret = -1; |
| 5845 | } |
| 5846 | |
| 5847 | /* Create the kconsumerd error unix socket */ |
| 5848 | consumer_data->err_sock = |
| 5849 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); |
| 5850 | if (consumer_data->err_sock < 0) { |
| 5851 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); |
| 5852 | ret = -1; |
| 5853 | goto error; |
| 5854 | } |
| 5855 | |
| 5856 | /* File permission MUST be 660 */ |
| 5857 | ret = chmod(consumer_data->err_unix_sock_path, |
| 5858 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 5859 | if (ret < 0) { |
| 5860 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
| 5861 | PERROR("chmod"); |
| 5862 | goto error; |
| 5863 | } |
| 5864 | |
| 5865 | error: |
| 5866 | return ret; |
| 5867 | } |
| 5868 | |
| 5869 | /* |
| 5870 | * Signal handler for the daemon |
| 5871 | * |
| 5872 | * Simply stop all worker threads, leaving main() return gracefully after |
| 5873 | * joining all threads and calling cleanup(). |
| 5874 | */ |
| 5875 | static void sighandler(int sig) |
| 5876 | { |
| 5877 | switch (sig) { |
| 5878 | case SIGPIPE: |
| 5879 | DBG("SIGPIPE caught"); |
| 5880 | return; |
| 5881 | case SIGINT: |
| 5882 | DBG("SIGINT caught"); |
| 5883 | stop_threads(); |
| 5884 | break; |
| 5885 | case SIGTERM: |
| 5886 | DBG("SIGTERM caught"); |
| 5887 | stop_threads(); |
| 5888 | break; |
| 5889 | default: |
| 5890 | break; |
| 5891 | } |
| 5892 | } |
| 5893 | |
| 5894 | /* |
| 5895 | * Setup signal handler for : |
| 5896 | * SIGINT, SIGTERM, SIGPIPE |
| 5897 | */ |
| 5898 | static int set_signal_handler(void) |
| 5899 | { |
| 5900 | int ret = 0; |
| 5901 | struct sigaction sa; |
| 5902 | sigset_t sigset; |
| 5903 | |
| 5904 | if ((ret = sigemptyset(&sigset)) < 0) { |
| 5905 | PERROR("sigemptyset"); |
| 5906 | return ret; |
| 5907 | } |
| 5908 | |
| 5909 | sa.sa_handler = sighandler; |
| 5910 | sa.sa_mask = sigset; |
| 5911 | sa.sa_flags = 0; |
| 5912 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
| 5913 | PERROR("sigaction"); |
| 5914 | return ret; |
| 5915 | } |
| 5916 | |
| 5917 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
| 5918 | PERROR("sigaction"); |
| 5919 | return ret; |
| 5920 | } |
| 5921 | |
| 5922 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
| 5923 | PERROR("sigaction"); |
| 5924 | return ret; |
| 5925 | } |
| 5926 | |
| 5927 | DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT"); |
| 5928 | |
| 5929 | return ret; |
| 5930 | } |
| 5931 | |
| 5932 | /* |
| 5933 | * Set open files limit to unlimited. This daemon can open a large number of |
| 5934 | * file descriptors in order to consumer multiple kernel traces. |
| 5935 | */ |
| 5936 | static void set_ulimit(void) |
| 5937 | { |
| 5938 | int ret; |
| 5939 | struct rlimit lim; |
| 5940 | |
| 5941 | /* The kernel does not allowed an infinite limit for open files */ |
| 5942 | lim.rlim_cur = 65535; |
| 5943 | lim.rlim_max = 65535; |
| 5944 | |
| 5945 | ret = setrlimit(RLIMIT_NOFILE, &lim); |
| 5946 | if (ret < 0) { |
| 5947 | PERROR("failed to set open files limit"); |
| 5948 | } |
| 5949 | } |
| 5950 | |
| 5951 | /* |
| 5952 | * main |
| 5953 | */ |
| 5954 | int main(int argc, char **argv) |
| 5955 | { |
| 5956 | int ret = 0; |
| 5957 | void *status; |
| 5958 | const char *home_path; |
| 5959 | |
| 5960 | init_kernel_workarounds(); |
| 5961 | |
| 5962 | rcu_register_thread(); |
| 5963 | |
| 5964 | setup_consumerd_path(); |
| 5965 | |
| 5966 | /* Parse arguments */ |
| 5967 | progname = argv[0]; |
| 5968 | if ((ret = parse_args(argc, argv) < 0)) { |
| 5969 | goto error; |
| 5970 | } |
| 5971 | |
| 5972 | /* Daemonize */ |
| 5973 | if (opt_daemon) { |
| 5974 | int i; |
| 5975 | |
| 5976 | /* |
| 5977 | * fork |
| 5978 | * child: setsid, close FD 0, 1, 2, chdir / |
| 5979 | * parent: exit (if fork is successful) |
| 5980 | */ |
| 5981 | ret = daemon(0, 0); |
| 5982 | if (ret < 0) { |
| 5983 | PERROR("daemon"); |
| 5984 | goto error; |
| 5985 | } |
| 5986 | /* |
| 5987 | * We are in the child. Make sure all other file |
| 5988 | * descriptors are closed, in case we are called with |
| 5989 | * more opened file descriptors than the standard ones. |
| 5990 | */ |
| 5991 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { |
| 5992 | (void) close(i); |
| 5993 | } |
| 5994 | } |
| 5995 | |
| 5996 | /* Create thread quit pipe */ |
| 5997 | if ((ret = init_thread_quit_pipe()) < 0) { |
| 5998 | goto error; |
| 5999 | } |
| 6000 | |
| 6001 | /* Check if daemon is UID = 0 */ |
| 6002 | is_root = !getuid(); |
| 6003 | |
| 6004 | if (is_root) { |
| 6005 | rundir = strdup(DEFAULT_LTTNG_RUNDIR); |
| 6006 | |
| 6007 | /* Create global run dir with root access */ |
| 6008 | ret = create_lttng_rundir(rundir); |
| 6009 | if (ret < 0) { |
| 6010 | goto error; |
| 6011 | } |
| 6012 | |
| 6013 | if (strlen(apps_unix_sock_path) == 0) { |
| 6014 | snprintf(apps_unix_sock_path, PATH_MAX, |
| 6015 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); |
| 6016 | } |
| 6017 | |
| 6018 | if (strlen(client_unix_sock_path) == 0) { |
| 6019 | snprintf(client_unix_sock_path, PATH_MAX, |
| 6020 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); |
| 6021 | } |
| 6022 | |
| 6023 | /* Set global SHM for ust */ |
| 6024 | if (strlen(wait_shm_path) == 0) { |
| 6025 | snprintf(wait_shm_path, PATH_MAX, |
| 6026 | DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH); |
| 6027 | } |
| 6028 | |
| 6029 | if (strlen(health_unix_sock_path) == 0) { |
| 6030 | snprintf(health_unix_sock_path, sizeof(health_unix_sock_path), |
| 6031 | DEFAULT_GLOBAL_HEALTH_UNIX_SOCK); |
| 6032 | } |
| 6033 | |
| 6034 | /* Setup kernel consumerd path */ |
| 6035 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, |
| 6036 | DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir); |
| 6037 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, |
| 6038 | DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir); |
| 6039 | |
| 6040 | DBG2("Kernel consumer err path: %s", |
| 6041 | kconsumer_data.err_unix_sock_path); |
| 6042 | DBG2("Kernel consumer cmd path: %s", |
| 6043 | kconsumer_data.cmd_unix_sock_path); |
| 6044 | } else { |
| 6045 | home_path = get_home_dir(); |
| 6046 | if (home_path == NULL) { |
| 6047 | /* TODO: Add --socket PATH option */ |
| 6048 | ERR("Can't get HOME directory for sockets creation."); |
| 6049 | ret = -EPERM; |
| 6050 | goto error; |
| 6051 | } |
| 6052 | |
| 6053 | /* |
| 6054 | * Create rundir from home path. This will create something like |
| 6055 | * $HOME/.lttng |
| 6056 | */ |
| 6057 | ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path); |
| 6058 | if (ret < 0) { |
| 6059 | ret = -ENOMEM; |
| 6060 | goto error; |
| 6061 | } |
| 6062 | |
| 6063 | ret = create_lttng_rundir(rundir); |
| 6064 | if (ret < 0) { |
| 6065 | goto error; |
| 6066 | } |
| 6067 | |
| 6068 | if (strlen(apps_unix_sock_path) == 0) { |
| 6069 | snprintf(apps_unix_sock_path, PATH_MAX, |
| 6070 | DEFAULT_HOME_APPS_UNIX_SOCK, home_path); |
| 6071 | } |
| 6072 | |
| 6073 | /* Set the cli tool unix socket path */ |
| 6074 | if (strlen(client_unix_sock_path) == 0) { |
| 6075 | snprintf(client_unix_sock_path, PATH_MAX, |
| 6076 | DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path); |
| 6077 | } |
| 6078 | |
| 6079 | /* Set global SHM for ust */ |
| 6080 | if (strlen(wait_shm_path) == 0) { |
| 6081 | snprintf(wait_shm_path, PATH_MAX, |
| 6082 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, geteuid()); |
| 6083 | } |
| 6084 | |
| 6085 | /* Set health check Unix path */ |
| 6086 | if (strlen(health_unix_sock_path) == 0) { |
| 6087 | snprintf(health_unix_sock_path, sizeof(health_unix_sock_path), |
| 6088 | DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path); |
| 6089 | } |
| 6090 | } |
| 6091 | |
| 6092 | /* Set consumer initial state */ |
| 6093 | kernel_consumerd_state = CONSUMER_STOPPED; |
| 6094 | ust_consumerd_state = CONSUMER_STOPPED; |
| 6095 | |
| 6096 | DBG("Client socket path %s", client_unix_sock_path); |
| 6097 | DBG("Application socket path %s", apps_unix_sock_path); |
| 6098 | DBG("LTTng run directory path: %s", rundir); |
| 6099 | |
| 6100 | /* 32 bits consumerd path setup */ |
| 6101 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, |
| 6102 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir); |
| 6103 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, |
| 6104 | DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir); |
| 6105 | |
| 6106 | DBG2("UST consumer 32 bits err path: %s", |
| 6107 | ustconsumer32_data.err_unix_sock_path); |
| 6108 | DBG2("UST consumer 32 bits cmd path: %s", |
| 6109 | ustconsumer32_data.cmd_unix_sock_path); |
| 6110 | |
| 6111 | /* 64 bits consumerd path setup */ |
| 6112 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, |
| 6113 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir); |
| 6114 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, |
| 6115 | DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir); |
| 6116 | |
| 6117 | DBG2("UST consumer 64 bits err path: %s", |
| 6118 | ustconsumer64_data.err_unix_sock_path); |
| 6119 | DBG2("UST consumer 64 bits cmd path: %s", |
| 6120 | ustconsumer64_data.cmd_unix_sock_path); |
| 6121 | |
| 6122 | /* |
| 6123 | * See if daemon already exist. |
| 6124 | */ |
| 6125 | if ((ret = check_existing_daemon()) < 0) { |
| 6126 | ERR("Already running daemon.\n"); |
| 6127 | /* |
| 6128 | * We do not goto exit because we must not cleanup() |
| 6129 | * because a daemon is already running. |
| 6130 | */ |
| 6131 | goto error; |
| 6132 | } |
| 6133 | |
| 6134 | /* |
| 6135 | * Init UST app hash table. Alloc hash table before this point since |
| 6136 | * cleanup() can get called after that point. |
| 6137 | */ |
| 6138 | ust_app_ht_alloc(); |
| 6139 | |
| 6140 | /* After this point, we can safely call cleanup() with "goto exit" */ |
| 6141 | |
| 6142 | /* |
| 6143 | * These actions must be executed as root. We do that *after* setting up |
| 6144 | * the sockets path because we MUST make the check for another daemon using |
| 6145 | * those paths *before* trying to set the kernel consumer sockets and init |
| 6146 | * kernel tracer. |
| 6147 | */ |
| 6148 | if (is_root) { |
| 6149 | ret = set_consumer_sockets(&kconsumer_data, rundir); |
| 6150 | if (ret < 0) { |
| 6151 | goto exit; |
| 6152 | } |
| 6153 | |
| 6154 | /* Setup kernel tracer */ |
| 6155 | if (!opt_no_kernel) { |
| 6156 | init_kernel_tracer(); |
| 6157 | } |
| 6158 | |
| 6159 | /* Set ulimit for open files */ |
| 6160 | set_ulimit(); |
| 6161 | } |
| 6162 | /* init lttng_fd tracking must be done after set_ulimit. */ |
| 6163 | lttng_fd_init(); |
| 6164 | |
| 6165 | ret = set_consumer_sockets(&ustconsumer64_data, rundir); |
| 6166 | if (ret < 0) { |
| 6167 | goto exit; |
| 6168 | } |
| 6169 | |
| 6170 | ret = set_consumer_sockets(&ustconsumer32_data, rundir); |
| 6171 | if (ret < 0) { |
| 6172 | goto exit; |
| 6173 | } |
| 6174 | |
| 6175 | if ((ret = set_signal_handler()) < 0) { |
| 6176 | goto exit; |
| 6177 | } |
| 6178 | |
| 6179 | /* Setup the needed unix socket */ |
| 6180 | if ((ret = init_daemon_socket()) < 0) { |
| 6181 | goto exit; |
| 6182 | } |
| 6183 | |
| 6184 | /* Set credentials to socket */ |
| 6185 | if (is_root && ((ret = set_permissions(rundir)) < 0)) { |
| 6186 | goto exit; |
| 6187 | } |
| 6188 | |
| 6189 | /* Get parent pid if -S, --sig-parent is specified. */ |
| 6190 | if (opt_sig_parent) { |
| 6191 | ppid = getppid(); |
| 6192 | } |
| 6193 | |
| 6194 | /* Setup the kernel pipe for waking up the kernel thread */ |
| 6195 | if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) { |
| 6196 | goto exit; |
| 6197 | } |
| 6198 | |
| 6199 | /* Setup the thread apps communication pipe. */ |
| 6200 | if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) { |
| 6201 | goto exit; |
| 6202 | } |
| 6203 | |
| 6204 | /* Init UST command queue. */ |
| 6205 | cds_wfq_init(&ust_cmd_queue.queue); |
| 6206 | |
| 6207 | /* |
| 6208 | * Get session list pointer. This pointer MUST NOT be free(). This list is |
| 6209 | * statically declared in session.c |
| 6210 | */ |
| 6211 | session_list_ptr = session_get_list(); |
| 6212 | |
| 6213 | /* Set up max poll set size */ |
| 6214 | lttng_poll_set_max_size(); |
| 6215 | |
| 6216 | /* |
| 6217 | * Set network sequence index to 1 for streams to match a relayd socket on |
| 6218 | * the consumer side. |
| 6219 | */ |
| 6220 | uatomic_set(&relayd_net_seq_idx, 1); |
| 6221 | |
| 6222 | /* Init all health thread counters. */ |
| 6223 | health_init(&health_thread_cmd); |
| 6224 | health_init(&health_thread_kernel); |
| 6225 | health_init(&health_thread_app_manage); |
| 6226 | health_init(&health_thread_app_reg); |
| 6227 | |
| 6228 | /* |
| 6229 | * Init health counters of the consumer thread. We do a quick hack here to |
| 6230 | * the state of the consumer health is fine even if the thread is not |
| 6231 | * started. This is simply to ease our life and has no cost what so ever. |
| 6232 | */ |
| 6233 | health_init(&kconsumer_data.health); |
| 6234 | health_poll_update(&kconsumer_data.health); |
| 6235 | health_init(&ustconsumer32_data.health); |
| 6236 | health_poll_update(&ustconsumer32_data.health); |
| 6237 | health_init(&ustconsumer64_data.health); |
| 6238 | health_poll_update(&ustconsumer64_data.health); |
| 6239 | |
| 6240 | /* Create thread to manage the client socket */ |
| 6241 | ret = pthread_create(&health_thread, NULL, |
| 6242 | thread_manage_health, (void *) NULL); |
| 6243 | if (ret != 0) { |
| 6244 | PERROR("pthread_create health"); |
| 6245 | goto exit_health; |
| 6246 | } |
| 6247 | |
| 6248 | /* Create thread to manage the client socket */ |
| 6249 | ret = pthread_create(&client_thread, NULL, |
| 6250 | thread_manage_clients, (void *) NULL); |
| 6251 | if (ret != 0) { |
| 6252 | PERROR("pthread_create clients"); |
| 6253 | goto exit_client; |
| 6254 | } |
| 6255 | |
| 6256 | /* Create thread to dispatch registration */ |
| 6257 | ret = pthread_create(&dispatch_thread, NULL, |
| 6258 | thread_dispatch_ust_registration, (void *) NULL); |
| 6259 | if (ret != 0) { |
| 6260 | PERROR("pthread_create dispatch"); |
| 6261 | goto exit_dispatch; |
| 6262 | } |
| 6263 | |
| 6264 | /* Create thread to manage application registration. */ |
| 6265 | ret = pthread_create(®_apps_thread, NULL, |
| 6266 | thread_registration_apps, (void *) NULL); |
| 6267 | if (ret != 0) { |
| 6268 | PERROR("pthread_create registration"); |
| 6269 | goto exit_reg_apps; |
| 6270 | } |
| 6271 | |
| 6272 | /* Create thread to manage application socket */ |
| 6273 | ret = pthread_create(&apps_thread, NULL, |
| 6274 | thread_manage_apps, (void *) NULL); |
| 6275 | if (ret != 0) { |
| 6276 | PERROR("pthread_create apps"); |
| 6277 | goto exit_apps; |
| 6278 | } |
| 6279 | |
| 6280 | /* Create kernel thread to manage kernel event */ |
| 6281 | ret = pthread_create(&kernel_thread, NULL, |
| 6282 | thread_manage_kernel, (void *) NULL); |
| 6283 | if (ret != 0) { |
| 6284 | PERROR("pthread_create kernel"); |
| 6285 | goto exit_kernel; |
| 6286 | } |
| 6287 | |
| 6288 | ret = pthread_join(kernel_thread, &status); |
| 6289 | if (ret != 0) { |
| 6290 | PERROR("pthread_join"); |
| 6291 | goto error; /* join error, exit without cleanup */ |
| 6292 | } |
| 6293 | |
| 6294 | exit_kernel: |
| 6295 | ret = pthread_join(apps_thread, &status); |
| 6296 | if (ret != 0) { |
| 6297 | PERROR("pthread_join"); |
| 6298 | goto error; /* join error, exit without cleanup */ |
| 6299 | } |
| 6300 | |
| 6301 | exit_apps: |
| 6302 | ret = pthread_join(reg_apps_thread, &status); |
| 6303 | if (ret != 0) { |
| 6304 | PERROR("pthread_join"); |
| 6305 | goto error; /* join error, exit without cleanup */ |
| 6306 | } |
| 6307 | |
| 6308 | exit_reg_apps: |
| 6309 | ret = pthread_join(dispatch_thread, &status); |
| 6310 | if (ret != 0) { |
| 6311 | PERROR("pthread_join"); |
| 6312 | goto error; /* join error, exit without cleanup */ |
| 6313 | } |
| 6314 | |
| 6315 | exit_dispatch: |
| 6316 | ret = pthread_join(client_thread, &status); |
| 6317 | if (ret != 0) { |
| 6318 | PERROR("pthread_join"); |
| 6319 | goto error; /* join error, exit without cleanup */ |
| 6320 | } |
| 6321 | |
| 6322 | ret = join_consumer_thread(&kconsumer_data); |
| 6323 | if (ret != 0) { |
| 6324 | PERROR("join_consumer"); |
| 6325 | goto error; /* join error, exit without cleanup */ |
| 6326 | } |
| 6327 | |
| 6328 | exit_client: |
| 6329 | exit_health: |
| 6330 | exit: |
| 6331 | /* |
| 6332 | * cleanup() is called when no other thread is running. |
| 6333 | */ |
| 6334 | rcu_thread_online(); |
| 6335 | cleanup(); |
| 6336 | rcu_thread_offline(); |
| 6337 | rcu_unregister_thread(); |
| 6338 | if (!ret) { |
| 6339 | exit(EXIT_SUCCESS); |
| 6340 | } |
| 6341 | error: |
| 6342 | exit(EXIT_FAILURE); |
| 6343 | } |