| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License, version 2 only, |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #define _GNU_SOURCE |
| 21 | #include <getopt.h> |
| 22 | #include <grp.h> |
| 23 | #include <limits.h> |
| 24 | #include <paths.h> |
| 25 | #include <pthread.h> |
| 26 | #include <signal.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <inttypes.h> |
| 31 | #include <sys/mman.h> |
| 32 | #include <sys/mount.h> |
| 33 | #include <sys/resource.h> |
| 34 | #include <sys/socket.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/wait.h> |
| 38 | #include <urcu/uatomic.h> |
| 39 | #include <unistd.h> |
| 40 | #include <config.h> |
| 41 | |
| 42 | #include <common/common.h> |
| 43 | #include <common/compat/socket.h> |
| 44 | #include <common/defaults.h> |
| 45 | #include <common/kernel-consumer/kernel-consumer.h> |
| 46 | #include <common/futex.h> |
| 47 | #include <common/relayd/relayd.h> |
| 48 | #include <common/utils.h> |
| 49 | #include <common/config/config.h> |
| 50 | |
| 51 | #include "lttng-sessiond.h" |
| 52 | #include "buffer-registry.h" |
| 53 | #include "channel.h" |
| 54 | #include "cmd.h" |
| 55 | #include "consumer.h" |
| 56 | #include "context.h" |
| 57 | #include "event.h" |
| 58 | #include "kernel.h" |
| 59 | #include "kernel-consumer.h" |
| 60 | #include "modprobe.h" |
| 61 | #include "shm.h" |
| 62 | #include "ust-ctl.h" |
| 63 | #include "ust-consumer.h" |
| 64 | #include "utils.h" |
| 65 | #include "fd-limit.h" |
| 66 | #include "health-sessiond.h" |
| 67 | #include "testpoint.h" |
| 68 | #include "ust-thread.h" |
| 69 | #include "jul-thread.h" |
| 70 | |
| 71 | #define CONSUMERD_FILE "lttng-consumerd" |
| 72 | |
| 73 | const char *progname; |
| 74 | static const char *tracing_group_name = DEFAULT_TRACING_GROUP; |
| 75 | static int tracing_group_name_override; |
| 76 | static char *opt_pidfile; |
| 77 | static int opt_sig_parent; |
| 78 | static int opt_verbose_consumer; |
| 79 | static int opt_daemon; |
| 80 | static int opt_no_kernel; |
| 81 | static int is_root; /* Set to 1 if the daemon is running as root */ |
| 82 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
| 83 | static pid_t child_ppid; /* Internal parent PID use with daemonize. */ |
| 84 | static char *rundir; |
| 85 | |
| 86 | /* Set to 1 when a SIGUSR1 signal is received. */ |
| 87 | static int recv_child_signal; |
| 88 | |
| 89 | /* |
| 90 | * Consumer daemon specific control data. Every value not initialized here is |
| 91 | * set to 0 by the static definition. |
| 92 | */ |
| 93 | static struct consumer_data kconsumer_data = { |
| 94 | .type = LTTNG_CONSUMER_KERNEL, |
| 95 | .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH, |
| 96 | .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH, |
| 97 | .err_sock = -1, |
| 98 | .cmd_sock = -1, |
| 99 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
| 100 | .lock = PTHREAD_MUTEX_INITIALIZER, |
| 101 | .cond = PTHREAD_COND_INITIALIZER, |
| 102 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, |
| 103 | }; |
| 104 | static struct consumer_data ustconsumer64_data = { |
| 105 | .type = LTTNG_CONSUMER64_UST, |
| 106 | .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, |
| 107 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, |
| 108 | .err_sock = -1, |
| 109 | .cmd_sock = -1, |
| 110 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
| 111 | .lock = PTHREAD_MUTEX_INITIALIZER, |
| 112 | .cond = PTHREAD_COND_INITIALIZER, |
| 113 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, |
| 114 | }; |
| 115 | static struct consumer_data ustconsumer32_data = { |
| 116 | .type = LTTNG_CONSUMER32_UST, |
| 117 | .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, |
| 118 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, |
| 119 | .err_sock = -1, |
| 120 | .cmd_sock = -1, |
| 121 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
| 122 | .lock = PTHREAD_MUTEX_INITIALIZER, |
| 123 | .cond = PTHREAD_COND_INITIALIZER, |
| 124 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, |
| 125 | }; |
| 126 | |
| 127 | /* Command line options */ |
| 128 | static const struct option long_options[] = { |
| 129 | { "client-sock", 1, 0, 'c' }, |
| 130 | { "apps-sock", 1, 0, 'a' }, |
| 131 | { "kconsumerd-cmd-sock", 1, 0, 'C' }, |
| 132 | { "kconsumerd-err-sock", 1, 0, 'E' }, |
| 133 | { "ustconsumerd32-cmd-sock", 1, 0, 'G' }, |
| 134 | { "ustconsumerd32-err-sock", 1, 0, 'H' }, |
| 135 | { "ustconsumerd64-cmd-sock", 1, 0, 'D' }, |
| 136 | { "ustconsumerd64-err-sock", 1, 0, 'F' }, |
| 137 | { "consumerd32-path", 1, 0, 'u' }, |
| 138 | { "consumerd32-libdir", 1, 0, 'U' }, |
| 139 | { "consumerd64-path", 1, 0, 't' }, |
| 140 | { "consumerd64-libdir", 1, 0, 'T' }, |
| 141 | { "daemonize", 0, 0, 'd' }, |
| 142 | { "sig-parent", 0, 0, 'S' }, |
| 143 | { "help", 0, 0, 'h' }, |
| 144 | { "group", 1, 0, 'g' }, |
| 145 | { "version", 0, 0, 'V' }, |
| 146 | { "quiet", 0, 0, 'q' }, |
| 147 | { "verbose", 0, 0, 'v' }, |
| 148 | { "verbose-consumer", 0, 0, 'Z' }, |
| 149 | { "no-kernel", 0, 0, 'N' }, |
| 150 | { "pidfile", 1, 0, 'p' }, |
| 151 | { "jul-tcp-port", 1, 0, 'J' }, |
| 152 | { "config", 1, 0, 'f' }, |
| 153 | { NULL, 0, 0, 0 } |
| 154 | }; |
| 155 | |
| 156 | /* Command line options to ignore from configuration file */ |
| 157 | static const char *config_ignore_options[] = { "help", "version", "config" }; |
| 158 | |
| 159 | /* Shared between threads */ |
| 160 | static int dispatch_thread_exit; |
| 161 | |
| 162 | /* Global application Unix socket path */ |
| 163 | static char apps_unix_sock_path[PATH_MAX]; |
| 164 | /* Global client Unix socket path */ |
| 165 | static char client_unix_sock_path[PATH_MAX]; |
| 166 | /* global wait shm path for UST */ |
| 167 | static char wait_shm_path[PATH_MAX]; |
| 168 | /* Global health check unix path */ |
| 169 | static char health_unix_sock_path[PATH_MAX]; |
| 170 | |
| 171 | /* Sockets and FDs */ |
| 172 | static int client_sock = -1; |
| 173 | static int apps_sock = -1; |
| 174 | int kernel_tracer_fd = -1; |
| 175 | static int kernel_poll_pipe[2] = { -1, -1 }; |
| 176 | |
| 177 | /* |
| 178 | * Quit pipe for all threads. This permits a single cancellation point |
| 179 | * for all threads when receiving an event on the pipe. |
| 180 | */ |
| 181 | static int thread_quit_pipe[2] = { -1, -1 }; |
| 182 | |
| 183 | /* |
| 184 | * This pipe is used to inform the thread managing application communication |
| 185 | * that a command is queued and ready to be processed. |
| 186 | */ |
| 187 | static int apps_cmd_pipe[2] = { -1, -1 }; |
| 188 | |
| 189 | int apps_cmd_notify_pipe[2] = { -1, -1 }; |
| 190 | |
| 191 | /* Pthread, Mutexes and Semaphores */ |
| 192 | static pthread_t apps_thread; |
| 193 | static pthread_t apps_notify_thread; |
| 194 | static pthread_t reg_apps_thread; |
| 195 | static pthread_t client_thread; |
| 196 | static pthread_t kernel_thread; |
| 197 | static pthread_t dispatch_thread; |
| 198 | static pthread_t health_thread; |
| 199 | static pthread_t ht_cleanup_thread; |
| 200 | static pthread_t jul_reg_thread; |
| 201 | |
| 202 | /* |
| 203 | * UST registration command queue. This queue is tied with a futex and uses a N |
| 204 | * wakers / 1 waiter implemented and detailed in futex.c/.h |
| 205 | * |
| 206 | * The thread_manage_apps and thread_dispatch_ust_registration interact with |
| 207 | * this queue and the wait/wake scheme. |
| 208 | */ |
| 209 | static struct ust_cmd_queue ust_cmd_queue; |
| 210 | |
| 211 | /* |
| 212 | * Pointer initialized before thread creation. |
| 213 | * |
| 214 | * This points to the tracing session list containing the session count and a |
| 215 | * mutex lock. The lock MUST be taken if you iterate over the list. The lock |
| 216 | * MUST NOT be taken if you call a public function in session.c. |
| 217 | * |
| 218 | * The lock is nested inside the structure: session_list_ptr->lock. Please use |
| 219 | * session_lock_list and session_unlock_list for lock acquisition. |
| 220 | */ |
| 221 | static struct ltt_session_list *session_list_ptr; |
| 222 | |
| 223 | int ust_consumerd64_fd = -1; |
| 224 | int ust_consumerd32_fd = -1; |
| 225 | |
| 226 | static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN; |
| 227 | static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN; |
| 228 | static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR; |
| 229 | static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR; |
| 230 | static int consumerd32_bin_override; |
| 231 | static int consumerd64_bin_override; |
| 232 | static int consumerd32_libdir_override; |
| 233 | static int consumerd64_libdir_override; |
| 234 | |
| 235 | static const char *module_proc_lttng = "/proc/lttng"; |
| 236 | |
| 237 | /* |
| 238 | * Consumer daemon state which is changed when spawning it, killing it or in |
| 239 | * case of a fatal error. |
| 240 | */ |
| 241 | enum consumerd_state { |
| 242 | CONSUMER_STARTED = 1, |
| 243 | CONSUMER_STOPPED = 2, |
| 244 | CONSUMER_ERROR = 3, |
| 245 | }; |
| 246 | |
| 247 | /* |
| 248 | * This consumer daemon state is used to validate if a client command will be |
| 249 | * able to reach the consumer. If not, the client is informed. For instance, |
| 250 | * doing a "lttng start" when the consumer state is set to ERROR will return an |
| 251 | * error to the client. |
| 252 | * |
| 253 | * The following example shows a possible race condition of this scheme: |
| 254 | * |
| 255 | * consumer thread error happens |
| 256 | * client cmd arrives |
| 257 | * client cmd checks state -> still OK |
| 258 | * consumer thread exit, sets error |
| 259 | * client cmd try to talk to consumer |
| 260 | * ... |
| 261 | * |
| 262 | * However, since the consumer is a different daemon, we have no way of making |
| 263 | * sure the command will reach it safely even with this state flag. This is why |
| 264 | * we consider that up to the state validation during command processing, the |
| 265 | * command is safe. After that, we can not guarantee the correctness of the |
| 266 | * client request vis-a-vis the consumer. |
| 267 | */ |
| 268 | static enum consumerd_state ust_consumerd_state; |
| 269 | static enum consumerd_state kernel_consumerd_state; |
| 270 | |
| 271 | /* |
| 272 | * Socket timeout for receiving and sending in seconds. |
| 273 | */ |
| 274 | static int app_socket_timeout; |
| 275 | |
| 276 | /* Set in main() with the current page size. */ |
| 277 | long page_size; |
| 278 | |
| 279 | /* Application health monitoring */ |
| 280 | struct health_app *health_sessiond; |
| 281 | |
| 282 | /* JUL TCP port for registration. Used by the JUL thread. */ |
| 283 | unsigned int jul_tcp_port = DEFAULT_JUL_TCP_PORT; |
| 284 | |
| 285 | const char * const config_section_name = "sessiond"; |
| 286 | |
| 287 | static |
| 288 | void setup_consumerd_path(void) |
| 289 | { |
| 290 | const char *bin, *libdir; |
| 291 | |
| 292 | /* |
| 293 | * Allow INSTALL_BIN_PATH to be used as a target path for the |
| 294 | * native architecture size consumer if CONFIG_CONSUMER*_PATH |
| 295 | * has not been defined. |
| 296 | */ |
| 297 | #if (CAA_BITS_PER_LONG == 32) |
| 298 | if (!consumerd32_bin[0]) { |
| 299 | consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; |
| 300 | } |
| 301 | if (!consumerd32_libdir[0]) { |
| 302 | consumerd32_libdir = INSTALL_LIB_PATH; |
| 303 | } |
| 304 | #elif (CAA_BITS_PER_LONG == 64) |
| 305 | if (!consumerd64_bin[0]) { |
| 306 | consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; |
| 307 | } |
| 308 | if (!consumerd64_libdir[0]) { |
| 309 | consumerd64_libdir = INSTALL_LIB_PATH; |
| 310 | } |
| 311 | #else |
| 312 | #error "Unknown bitness" |
| 313 | #endif |
| 314 | |
| 315 | /* |
| 316 | * runtime env. var. overrides the build default. |
| 317 | */ |
| 318 | bin = getenv("LTTNG_CONSUMERD32_BIN"); |
| 319 | if (bin) { |
| 320 | consumerd32_bin = bin; |
| 321 | } |
| 322 | bin = getenv("LTTNG_CONSUMERD64_BIN"); |
| 323 | if (bin) { |
| 324 | consumerd64_bin = bin; |
| 325 | } |
| 326 | libdir = getenv("LTTNG_CONSUMERD32_LIBDIR"); |
| 327 | if (libdir) { |
| 328 | consumerd32_libdir = libdir; |
| 329 | } |
| 330 | libdir = getenv("LTTNG_CONSUMERD64_LIBDIR"); |
| 331 | if (libdir) { |
| 332 | consumerd64_libdir = libdir; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. |
| 338 | */ |
| 339 | int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size) |
| 340 | { |
| 341 | int ret; |
| 342 | |
| 343 | assert(events); |
| 344 | |
| 345 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); |
| 346 | if (ret < 0) { |
| 347 | goto error; |
| 348 | } |
| 349 | |
| 350 | /* Add quit pipe */ |
| 351 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR); |
| 352 | if (ret < 0) { |
| 353 | goto error; |
| 354 | } |
| 355 | |
| 356 | return 0; |
| 357 | |
| 358 | error: |
| 359 | return ret; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Check if the thread quit pipe was triggered. |
| 364 | * |
| 365 | * Return 1 if it was triggered else 0; |
| 366 | */ |
| 367 | int sessiond_check_thread_quit_pipe(int fd, uint32_t events) |
| 368 | { |
| 369 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { |
| 370 | return 1; |
| 371 | } |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Init thread quit pipe. |
| 378 | * |
| 379 | * Return -1 on error or 0 if all pipes are created. |
| 380 | */ |
| 381 | static int init_thread_quit_pipe(void) |
| 382 | { |
| 383 | int ret, i; |
| 384 | |
| 385 | ret = pipe(thread_quit_pipe); |
| 386 | if (ret < 0) { |
| 387 | PERROR("thread quit pipe"); |
| 388 | goto error; |
| 389 | } |
| 390 | |
| 391 | for (i = 0; i < 2; i++) { |
| 392 | ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC); |
| 393 | if (ret < 0) { |
| 394 | PERROR("fcntl"); |
| 395 | goto error; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | error: |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Stop all threads by closing the thread quit pipe. |
| 405 | */ |
| 406 | static void stop_threads(void) |
| 407 | { |
| 408 | int ret; |
| 409 | |
| 410 | /* Stopping all threads */ |
| 411 | DBG("Terminating all threads"); |
| 412 | ret = notify_thread_pipe(thread_quit_pipe[1]); |
| 413 | if (ret < 0) { |
| 414 | ERR("write error on thread quit pipe"); |
| 415 | } |
| 416 | |
| 417 | /* Dispatch thread */ |
| 418 | CMM_STORE_SHARED(dispatch_thread_exit, 1); |
| 419 | futex_nto1_wake(&ust_cmd_queue.futex); |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * Close every consumer sockets. |
| 424 | */ |
| 425 | static void close_consumer_sockets(void) |
| 426 | { |
| 427 | int ret; |
| 428 | |
| 429 | if (kconsumer_data.err_sock >= 0) { |
| 430 | ret = close(kconsumer_data.err_sock); |
| 431 | if (ret < 0) { |
| 432 | PERROR("kernel consumer err_sock close"); |
| 433 | } |
| 434 | } |
| 435 | if (ustconsumer32_data.err_sock >= 0) { |
| 436 | ret = close(ustconsumer32_data.err_sock); |
| 437 | if (ret < 0) { |
| 438 | PERROR("UST consumerd32 err_sock close"); |
| 439 | } |
| 440 | } |
| 441 | if (ustconsumer64_data.err_sock >= 0) { |
| 442 | ret = close(ustconsumer64_data.err_sock); |
| 443 | if (ret < 0) { |
| 444 | PERROR("UST consumerd64 err_sock close"); |
| 445 | } |
| 446 | } |
| 447 | if (kconsumer_data.cmd_sock >= 0) { |
| 448 | ret = close(kconsumer_data.cmd_sock); |
| 449 | if (ret < 0) { |
| 450 | PERROR("kernel consumer cmd_sock close"); |
| 451 | } |
| 452 | } |
| 453 | if (ustconsumer32_data.cmd_sock >= 0) { |
| 454 | ret = close(ustconsumer32_data.cmd_sock); |
| 455 | if (ret < 0) { |
| 456 | PERROR("UST consumerd32 cmd_sock close"); |
| 457 | } |
| 458 | } |
| 459 | if (ustconsumer64_data.cmd_sock >= 0) { |
| 460 | ret = close(ustconsumer64_data.cmd_sock); |
| 461 | if (ret < 0) { |
| 462 | PERROR("UST consumerd64 cmd_sock close"); |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * Cleanup the daemon |
| 469 | */ |
| 470 | static void cleanup(void) |
| 471 | { |
| 472 | int ret; |
| 473 | struct ltt_session *sess, *stmp; |
| 474 | char path[PATH_MAX]; |
| 475 | |
| 476 | DBG("Cleaning up"); |
| 477 | |
| 478 | /* |
| 479 | * Close the thread quit pipe. It has already done its job, |
| 480 | * since we are now called. |
| 481 | */ |
| 482 | utils_close_pipe(thread_quit_pipe); |
| 483 | |
| 484 | /* |
| 485 | * If opt_pidfile is undefined, the default file will be wiped when |
| 486 | * removing the rundir. |
| 487 | */ |
| 488 | if (opt_pidfile) { |
| 489 | ret = remove(opt_pidfile); |
| 490 | if (ret < 0) { |
| 491 | PERROR("remove pidfile %s", opt_pidfile); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | DBG("Removing sessiond and consumerd content of directory %s", rundir); |
| 496 | |
| 497 | /* sessiond */ |
| 498 | snprintf(path, PATH_MAX, |
| 499 | "%s/%s", |
| 500 | rundir, DEFAULT_LTTNG_SESSIOND_PIDFILE); |
| 501 | DBG("Removing %s", path); |
| 502 | (void) unlink(path); |
| 503 | |
| 504 | snprintf(path, PATH_MAX, "%s/%s", rundir, |
| 505 | DEFAULT_LTTNG_SESSIOND_JULPORT_FILE); |
| 506 | DBG("Removing %s", path); |
| 507 | (void) unlink(path); |
| 508 | |
| 509 | /* kconsumerd */ |
| 510 | snprintf(path, PATH_MAX, |
| 511 | DEFAULT_KCONSUMERD_ERR_SOCK_PATH, |
| 512 | rundir); |
| 513 | DBG("Removing %s", path); |
| 514 | (void) unlink(path); |
| 515 | |
| 516 | snprintf(path, PATH_MAX, |
| 517 | DEFAULT_KCONSUMERD_PATH, |
| 518 | rundir); |
| 519 | DBG("Removing directory %s", path); |
| 520 | (void) rmdir(path); |
| 521 | |
| 522 | /* ust consumerd 32 */ |
| 523 | snprintf(path, PATH_MAX, |
| 524 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, |
| 525 | rundir); |
| 526 | DBG("Removing %s", path); |
| 527 | (void) unlink(path); |
| 528 | |
| 529 | snprintf(path, PATH_MAX, |
| 530 | DEFAULT_USTCONSUMERD32_PATH, |
| 531 | rundir); |
| 532 | DBG("Removing directory %s", path); |
| 533 | (void) rmdir(path); |
| 534 | |
| 535 | /* ust consumerd 64 */ |
| 536 | snprintf(path, PATH_MAX, |
| 537 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, |
| 538 | rundir); |
| 539 | DBG("Removing %s", path); |
| 540 | (void) unlink(path); |
| 541 | |
| 542 | snprintf(path, PATH_MAX, |
| 543 | DEFAULT_USTCONSUMERD64_PATH, |
| 544 | rundir); |
| 545 | DBG("Removing directory %s", path); |
| 546 | (void) rmdir(path); |
| 547 | |
| 548 | /* |
| 549 | * We do NOT rmdir rundir because there are other processes |
| 550 | * using it, for instance lttng-relayd, which can start in |
| 551 | * parallel with this teardown. |
| 552 | */ |
| 553 | |
| 554 | free(rundir); |
| 555 | |
| 556 | DBG("Cleaning up all sessions"); |
| 557 | |
| 558 | /* Destroy session list mutex */ |
| 559 | if (session_list_ptr != NULL) { |
| 560 | pthread_mutex_destroy(&session_list_ptr->lock); |
| 561 | |
| 562 | /* Cleanup ALL session */ |
| 563 | cds_list_for_each_entry_safe(sess, stmp, |
| 564 | &session_list_ptr->head, list) { |
| 565 | cmd_destroy_session(sess, kernel_poll_pipe[1]); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | DBG("Closing all UST sockets"); |
| 570 | ust_app_clean_list(); |
| 571 | buffer_reg_destroy_registries(); |
| 572 | |
| 573 | if (is_root && !opt_no_kernel) { |
| 574 | DBG2("Closing kernel fd"); |
| 575 | if (kernel_tracer_fd >= 0) { |
| 576 | ret = close(kernel_tracer_fd); |
| 577 | if (ret) { |
| 578 | PERROR("close"); |
| 579 | } |
| 580 | } |
| 581 | DBG("Unloading kernel modules"); |
| 582 | modprobe_remove_lttng_all(); |
| 583 | } |
| 584 | |
| 585 | close_consumer_sockets(); |
| 586 | |
| 587 | /* |
| 588 | * If the override option is set, the pointer points to a *non* const thus |
| 589 | * freeing it even though the variable type is set to const. |
| 590 | */ |
| 591 | if (tracing_group_name_override) { |
| 592 | free((void *) tracing_group_name); |
| 593 | } |
| 594 | if (consumerd32_bin_override) { |
| 595 | free((void *) consumerd32_bin); |
| 596 | } |
| 597 | if (consumerd64_bin_override) { |
| 598 | free((void *) consumerd64_bin); |
| 599 | } |
| 600 | if (consumerd32_libdir_override) { |
| 601 | free((void *) consumerd32_libdir); |
| 602 | } |
| 603 | if (consumerd64_libdir_override) { |
| 604 | free((void *) consumerd64_libdir); |
| 605 | } |
| 606 | |
| 607 | if (opt_pidfile) { |
| 608 | free(opt_pidfile); |
| 609 | } |
| 610 | |
| 611 | /* <fun> */ |
| 612 | DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm" |
| 613 | "Matthew, BEET driven development works!%c[%dm", |
| 614 | 27, 1, 31, 27, 0, 27, 1, 33, 27, 0); |
| 615 | /* </fun> */ |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Send data on a unix socket using the liblttsessiondcomm API. |
| 620 | * |
| 621 | * Return lttcomm error code. |
| 622 | */ |
| 623 | static int send_unix_sock(int sock, void *buf, size_t len) |
| 624 | { |
| 625 | /* Check valid length */ |
| 626 | if (len == 0) { |
| 627 | return -1; |
| 628 | } |
| 629 | |
| 630 | return lttcomm_send_unix_sock(sock, buf, len); |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * Free memory of a command context structure. |
| 635 | */ |
| 636 | static void clean_command_ctx(struct command_ctx **cmd_ctx) |
| 637 | { |
| 638 | DBG("Clean command context structure"); |
| 639 | if (*cmd_ctx) { |
| 640 | if ((*cmd_ctx)->llm) { |
| 641 | free((*cmd_ctx)->llm); |
| 642 | } |
| 643 | if ((*cmd_ctx)->lsm) { |
| 644 | free((*cmd_ctx)->lsm); |
| 645 | } |
| 646 | free(*cmd_ctx); |
| 647 | *cmd_ctx = NULL; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Notify UST applications using the shm mmap futex. |
| 653 | */ |
| 654 | static int notify_ust_apps(int active) |
| 655 | { |
| 656 | char *wait_shm_mmap; |
| 657 | |
| 658 | DBG("Notifying applications of session daemon state: %d", active); |
| 659 | |
| 660 | /* See shm.c for this call implying mmap, shm and futex calls */ |
| 661 | wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root); |
| 662 | if (wait_shm_mmap == NULL) { |
| 663 | goto error; |
| 664 | } |
| 665 | |
| 666 | /* Wake waiting process */ |
| 667 | futex_wait_update((int32_t *) wait_shm_mmap, active); |
| 668 | |
| 669 | /* Apps notified successfully */ |
| 670 | return 0; |
| 671 | |
| 672 | error: |
| 673 | return -1; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * Setup the outgoing data buffer for the response (llm) by allocating the |
| 678 | * right amount of memory and copying the original information from the lsm |
| 679 | * structure. |
| 680 | * |
| 681 | * Return total size of the buffer pointed by buf. |
| 682 | */ |
| 683 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
| 684 | { |
| 685 | int ret, buf_size; |
| 686 | |
| 687 | buf_size = size; |
| 688 | |
| 689 | cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size); |
| 690 | if (cmd_ctx->llm == NULL) { |
| 691 | PERROR("zmalloc"); |
| 692 | ret = -ENOMEM; |
| 693 | goto error; |
| 694 | } |
| 695 | |
| 696 | /* Copy common data */ |
| 697 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; |
| 698 | cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid; |
| 699 | |
| 700 | cmd_ctx->llm->data_size = size; |
| 701 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; |
| 702 | |
| 703 | return buf_size; |
| 704 | |
| 705 | error: |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | /* |
| 710 | * Update the kernel poll set of all channel fd available over all tracing |
| 711 | * session. Add the wakeup pipe at the end of the set. |
| 712 | */ |
| 713 | static int update_kernel_poll(struct lttng_poll_event *events) |
| 714 | { |
| 715 | int ret; |
| 716 | struct ltt_session *session; |
| 717 | struct ltt_kernel_channel *channel; |
| 718 | |
| 719 | DBG("Updating kernel poll set"); |
| 720 | |
| 721 | session_lock_list(); |
| 722 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
| 723 | session_lock(session); |
| 724 | if (session->kernel_session == NULL) { |
| 725 | session_unlock(session); |
| 726 | continue; |
| 727 | } |
| 728 | |
| 729 | cds_list_for_each_entry(channel, |
| 730 | &session->kernel_session->channel_list.head, list) { |
| 731 | /* Add channel fd to the kernel poll set */ |
| 732 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); |
| 733 | if (ret < 0) { |
| 734 | session_unlock(session); |
| 735 | goto error; |
| 736 | } |
| 737 | DBG("Channel fd %d added to kernel set", channel->fd); |
| 738 | } |
| 739 | session_unlock(session); |
| 740 | } |
| 741 | session_unlock_list(); |
| 742 | |
| 743 | return 0; |
| 744 | |
| 745 | error: |
| 746 | session_unlock_list(); |
| 747 | return -1; |
| 748 | } |
| 749 | |
| 750 | /* |
| 751 | * Find the channel fd from 'fd' over all tracing session. When found, check |
| 752 | * for new channel stream and send those stream fds to the kernel consumer. |
| 753 | * |
| 754 | * Useful for CPU hotplug feature. |
| 755 | */ |
| 756 | static int update_kernel_stream(struct consumer_data *consumer_data, int fd) |
| 757 | { |
| 758 | int ret = 0; |
| 759 | struct ltt_session *session; |
| 760 | struct ltt_kernel_session *ksess; |
| 761 | struct ltt_kernel_channel *channel; |
| 762 | |
| 763 | DBG("Updating kernel streams for channel fd %d", fd); |
| 764 | |
| 765 | session_lock_list(); |
| 766 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
| 767 | session_lock(session); |
| 768 | if (session->kernel_session == NULL) { |
| 769 | session_unlock(session); |
| 770 | continue; |
| 771 | } |
| 772 | ksess = session->kernel_session; |
| 773 | |
| 774 | cds_list_for_each_entry(channel, &ksess->channel_list.head, list) { |
| 775 | if (channel->fd == fd) { |
| 776 | DBG("Channel found, updating kernel streams"); |
| 777 | ret = kernel_open_channel_stream(channel); |
| 778 | if (ret < 0) { |
| 779 | goto error; |
| 780 | } |
| 781 | /* Update the stream global counter */ |
| 782 | ksess->stream_count_global += ret; |
| 783 | |
| 784 | /* |
| 785 | * Have we already sent fds to the consumer? If yes, it means |
| 786 | * that tracing is started so it is safe to send our updated |
| 787 | * stream fds. |
| 788 | */ |
| 789 | if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) { |
| 790 | struct lttng_ht_iter iter; |
| 791 | struct consumer_socket *socket; |
| 792 | |
| 793 | rcu_read_lock(); |
| 794 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, |
| 795 | &iter.iter, socket, node.node) { |
| 796 | pthread_mutex_lock(socket->lock); |
| 797 | ret = kernel_consumer_send_channel_stream(socket, |
| 798 | channel, ksess, |
| 799 | session->output_traces ? 1 : 0); |
| 800 | pthread_mutex_unlock(socket->lock); |
| 801 | if (ret < 0) { |
| 802 | rcu_read_unlock(); |
| 803 | goto error; |
| 804 | } |
| 805 | } |
| 806 | rcu_read_unlock(); |
| 807 | } |
| 808 | goto error; |
| 809 | } |
| 810 | } |
| 811 | session_unlock(session); |
| 812 | } |
| 813 | session_unlock_list(); |
| 814 | return ret; |
| 815 | |
| 816 | error: |
| 817 | session_unlock(session); |
| 818 | session_unlock_list(); |
| 819 | return ret; |
| 820 | } |
| 821 | |
| 822 | /* |
| 823 | * For each tracing session, update newly registered apps. The session list |
| 824 | * lock MUST be acquired before calling this. |
| 825 | */ |
| 826 | static void update_ust_app(int app_sock) |
| 827 | { |
| 828 | struct ltt_session *sess, *stmp; |
| 829 | |
| 830 | /* Consumer is in an ERROR state. Stop any application update. */ |
| 831 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { |
| 832 | /* Stop the update process since the consumer is dead. */ |
| 833 | return; |
| 834 | } |
| 835 | |
| 836 | /* For all tracing session(s) */ |
| 837 | cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) { |
| 838 | session_lock(sess); |
| 839 | if (sess->ust_session) { |
| 840 | ust_app_global_update(sess->ust_session, app_sock); |
| 841 | } |
| 842 | session_unlock(sess); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * This thread manage event coming from the kernel. |
| 848 | * |
| 849 | * Features supported in this thread: |
| 850 | * -) CPU Hotplug |
| 851 | */ |
| 852 | static void *thread_manage_kernel(void *data) |
| 853 | { |
| 854 | int ret, i, pollfd, update_poll_flag = 1, err = -1; |
| 855 | uint32_t revents, nb_fd; |
| 856 | char tmp; |
| 857 | struct lttng_poll_event events; |
| 858 | |
| 859 | DBG("[thread] Thread manage kernel started"); |
| 860 | |
| 861 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL); |
| 862 | |
| 863 | /* |
| 864 | * This first step of the while is to clean this structure which could free |
| 865 | * non NULL pointers so initialize it before the loop. |
| 866 | */ |
| 867 | lttng_poll_init(&events); |
| 868 | |
| 869 | if (testpoint(sessiond_thread_manage_kernel)) { |
| 870 | goto error_testpoint; |
| 871 | } |
| 872 | |
| 873 | health_code_update(); |
| 874 | |
| 875 | if (testpoint(sessiond_thread_manage_kernel_before_loop)) { |
| 876 | goto error_testpoint; |
| 877 | } |
| 878 | |
| 879 | while (1) { |
| 880 | health_code_update(); |
| 881 | |
| 882 | if (update_poll_flag == 1) { |
| 883 | /* Clean events object. We are about to populate it again. */ |
| 884 | lttng_poll_clean(&events); |
| 885 | |
| 886 | ret = sessiond_set_thread_pollset(&events, 2); |
| 887 | if (ret < 0) { |
| 888 | goto error_poll_create; |
| 889 | } |
| 890 | |
| 891 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); |
| 892 | if (ret < 0) { |
| 893 | goto error; |
| 894 | } |
| 895 | |
| 896 | /* This will add the available kernel channel if any. */ |
| 897 | ret = update_kernel_poll(&events); |
| 898 | if (ret < 0) { |
| 899 | goto error; |
| 900 | } |
| 901 | update_poll_flag = 0; |
| 902 | } |
| 903 | |
| 904 | DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events)); |
| 905 | |
| 906 | /* Poll infinite value of time */ |
| 907 | restart: |
| 908 | health_poll_entry(); |
| 909 | ret = lttng_poll_wait(&events, -1); |
| 910 | health_poll_exit(); |
| 911 | if (ret < 0) { |
| 912 | /* |
| 913 | * Restart interrupted system call. |
| 914 | */ |
| 915 | if (errno == EINTR) { |
| 916 | goto restart; |
| 917 | } |
| 918 | goto error; |
| 919 | } else if (ret == 0) { |
| 920 | /* Should not happen since timeout is infinite */ |
| 921 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
| 922 | "This should not have happened! Continuing..."); |
| 923 | continue; |
| 924 | } |
| 925 | |
| 926 | nb_fd = ret; |
| 927 | |
| 928 | for (i = 0; i < nb_fd; i++) { |
| 929 | /* Fetch once the poll data */ |
| 930 | revents = LTTNG_POLL_GETEV(&events, i); |
| 931 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 932 | |
| 933 | health_code_update(); |
| 934 | |
| 935 | /* Thread quit pipe has been closed. Killing thread. */ |
| 936 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
| 937 | if (ret) { |
| 938 | err = 0; |
| 939 | goto exit; |
| 940 | } |
| 941 | |
| 942 | /* Check for data on kernel pipe */ |
| 943 | if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) { |
| 944 | (void) lttng_read(kernel_poll_pipe[0], |
| 945 | &tmp, 1); |
| 946 | /* |
| 947 | * Ret value is useless here, if this pipe gets any actions an |
| 948 | * update is required anyway. |
| 949 | */ |
| 950 | update_poll_flag = 1; |
| 951 | continue; |
| 952 | } else { |
| 953 | /* |
| 954 | * New CPU detected by the kernel. Adding kernel stream to |
| 955 | * kernel session and updating the kernel consumer |
| 956 | */ |
| 957 | if (revents & LPOLLIN) { |
| 958 | ret = update_kernel_stream(&kconsumer_data, pollfd); |
| 959 | if (ret < 0) { |
| 960 | continue; |
| 961 | } |
| 962 | break; |
| 963 | /* |
| 964 | * TODO: We might want to handle the LPOLLERR | LPOLLHUP |
| 965 | * and unregister kernel stream at this point. |
| 966 | */ |
| 967 | } |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | exit: |
| 973 | error: |
| 974 | lttng_poll_clean(&events); |
| 975 | error_poll_create: |
| 976 | error_testpoint: |
| 977 | utils_close_pipe(kernel_poll_pipe); |
| 978 | kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1; |
| 979 | if (err) { |
| 980 | health_error(); |
| 981 | ERR("Health error occurred in %s", __func__); |
| 982 | WARN("Kernel thread died unexpectedly. " |
| 983 | "Kernel tracing can continue but CPU hotplug is disabled."); |
| 984 | } |
| 985 | health_unregister(health_sessiond); |
| 986 | DBG("Kernel thread dying"); |
| 987 | return NULL; |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | * Signal pthread condition of the consumer data that the thread. |
| 992 | */ |
| 993 | static void signal_consumer_condition(struct consumer_data *data, int state) |
| 994 | { |
| 995 | pthread_mutex_lock(&data->cond_mutex); |
| 996 | |
| 997 | /* |
| 998 | * The state is set before signaling. It can be any value, it's the waiter |
| 999 | * job to correctly interpret this condition variable associated to the |
| 1000 | * consumer pthread_cond. |
| 1001 | * |
| 1002 | * A value of 0 means that the corresponding thread of the consumer data |
| 1003 | * was not started. 1 indicates that the thread has started and is ready |
| 1004 | * for action. A negative value means that there was an error during the |
| 1005 | * thread bootstrap. |
| 1006 | */ |
| 1007 | data->consumer_thread_is_ready = state; |
| 1008 | (void) pthread_cond_signal(&data->cond); |
| 1009 | |
| 1010 | pthread_mutex_unlock(&data->cond_mutex); |
| 1011 | } |
| 1012 | |
| 1013 | /* |
| 1014 | * This thread manage the consumer error sent back to the session daemon. |
| 1015 | */ |
| 1016 | static void *thread_manage_consumer(void *data) |
| 1017 | { |
| 1018 | int sock = -1, i, ret, pollfd, err = -1; |
| 1019 | uint32_t revents, nb_fd; |
| 1020 | enum lttcomm_return_code code; |
| 1021 | struct lttng_poll_event events; |
| 1022 | struct consumer_data *consumer_data = data; |
| 1023 | |
| 1024 | DBG("[thread] Manage consumer started"); |
| 1025 | |
| 1026 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER); |
| 1027 | |
| 1028 | health_code_update(); |
| 1029 | |
| 1030 | /* |
| 1031 | * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the |
| 1032 | * metadata_sock. Nothing more will be added to this poll set. |
| 1033 | */ |
| 1034 | ret = sessiond_set_thread_pollset(&events, 3); |
| 1035 | if (ret < 0) { |
| 1036 | goto error_poll; |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * The error socket here is already in a listening state which was done |
| 1041 | * just before spawning this thread to avoid a race between the consumer |
| 1042 | * daemon exec trying to connect and the listen() call. |
| 1043 | */ |
| 1044 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
| 1045 | if (ret < 0) { |
| 1046 | goto error; |
| 1047 | } |
| 1048 | |
| 1049 | health_code_update(); |
| 1050 | |
| 1051 | /* Infinite blocking call, waiting for transmission */ |
| 1052 | restart: |
| 1053 | health_poll_entry(); |
| 1054 | |
| 1055 | if (testpoint(sessiond_thread_manage_consumer)) { |
| 1056 | goto error; |
| 1057 | } |
| 1058 | |
| 1059 | ret = lttng_poll_wait(&events, -1); |
| 1060 | health_poll_exit(); |
| 1061 | if (ret < 0) { |
| 1062 | /* |
| 1063 | * Restart interrupted system call. |
| 1064 | */ |
| 1065 | if (errno == EINTR) { |
| 1066 | goto restart; |
| 1067 | } |
| 1068 | goto error; |
| 1069 | } |
| 1070 | |
| 1071 | nb_fd = ret; |
| 1072 | |
| 1073 | for (i = 0; i < nb_fd; i++) { |
| 1074 | /* Fetch once the poll data */ |
| 1075 | revents = LTTNG_POLL_GETEV(&events, i); |
| 1076 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1077 | |
| 1078 | health_code_update(); |
| 1079 | |
| 1080 | /* Thread quit pipe has been closed. Killing thread. */ |
| 1081 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
| 1082 | if (ret) { |
| 1083 | err = 0; |
| 1084 | goto exit; |
| 1085 | } |
| 1086 | |
| 1087 | /* Event on the registration socket */ |
| 1088 | if (pollfd == consumer_data->err_sock) { |
| 1089 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1090 | ERR("consumer err socket poll error"); |
| 1091 | goto error; |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
| 1097 | if (sock < 0) { |
| 1098 | goto error; |
| 1099 | } |
| 1100 | |
| 1101 | /* |
| 1102 | * Set the CLOEXEC flag. Return code is useless because either way, the |
| 1103 | * show must go on. |
| 1104 | */ |
| 1105 | (void) utils_set_fd_cloexec(sock); |
| 1106 | |
| 1107 | health_code_update(); |
| 1108 | |
| 1109 | DBG2("Receiving code from consumer err_sock"); |
| 1110 | |
| 1111 | /* Getting status code from kconsumerd */ |
| 1112 | ret = lttcomm_recv_unix_sock(sock, &code, |
| 1113 | sizeof(enum lttcomm_return_code)); |
| 1114 | if (ret <= 0) { |
| 1115 | goto error; |
| 1116 | } |
| 1117 | |
| 1118 | health_code_update(); |
| 1119 | |
| 1120 | if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) { |
| 1121 | /* Connect both socket, command and metadata. */ |
| 1122 | consumer_data->cmd_sock = |
| 1123 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); |
| 1124 | consumer_data->metadata_fd = |
| 1125 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); |
| 1126 | if (consumer_data->cmd_sock < 0 |
| 1127 | || consumer_data->metadata_fd < 0) { |
| 1128 | PERROR("consumer connect cmd socket"); |
| 1129 | /* On error, signal condition and quit. */ |
| 1130 | signal_consumer_condition(consumer_data, -1); |
| 1131 | goto error; |
| 1132 | } |
| 1133 | consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd; |
| 1134 | /* Create metadata socket lock. */ |
| 1135 | consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t)); |
| 1136 | if (consumer_data->metadata_sock.lock == NULL) { |
| 1137 | PERROR("zmalloc pthread mutex"); |
| 1138 | ret = -1; |
| 1139 | goto error; |
| 1140 | } |
| 1141 | pthread_mutex_init(consumer_data->metadata_sock.lock, NULL); |
| 1142 | |
| 1143 | signal_consumer_condition(consumer_data, 1); |
| 1144 | DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock); |
| 1145 | DBG("Consumer metadata socket ready (fd: %d)", |
| 1146 | consumer_data->metadata_fd); |
| 1147 | } else { |
| 1148 | ERR("consumer error when waiting for SOCK_READY : %s", |
| 1149 | lttcomm_get_readable_code(-code)); |
| 1150 | goto error; |
| 1151 | } |
| 1152 | |
| 1153 | /* Remove the consumerd error sock since we've established a connexion */ |
| 1154 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
| 1155 | if (ret < 0) { |
| 1156 | goto error; |
| 1157 | } |
| 1158 | |
| 1159 | /* Add new accepted error socket. */ |
| 1160 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
| 1161 | if (ret < 0) { |
| 1162 | goto error; |
| 1163 | } |
| 1164 | |
| 1165 | /* Add metadata socket that is successfully connected. */ |
| 1166 | ret = lttng_poll_add(&events, consumer_data->metadata_fd, |
| 1167 | LPOLLIN | LPOLLRDHUP); |
| 1168 | if (ret < 0) { |
| 1169 | goto error; |
| 1170 | } |
| 1171 | |
| 1172 | health_code_update(); |
| 1173 | |
| 1174 | /* Infinite blocking call, waiting for transmission */ |
| 1175 | restart_poll: |
| 1176 | while (1) { |
| 1177 | health_poll_entry(); |
| 1178 | ret = lttng_poll_wait(&events, -1); |
| 1179 | health_poll_exit(); |
| 1180 | if (ret < 0) { |
| 1181 | /* |
| 1182 | * Restart interrupted system call. |
| 1183 | */ |
| 1184 | if (errno == EINTR) { |
| 1185 | goto restart_poll; |
| 1186 | } |
| 1187 | goto error; |
| 1188 | } |
| 1189 | |
| 1190 | nb_fd = ret; |
| 1191 | |
| 1192 | for (i = 0; i < nb_fd; i++) { |
| 1193 | /* Fetch once the poll data */ |
| 1194 | revents = LTTNG_POLL_GETEV(&events, i); |
| 1195 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1196 | |
| 1197 | health_code_update(); |
| 1198 | |
| 1199 | /* Thread quit pipe has been closed. Killing thread. */ |
| 1200 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
| 1201 | if (ret) { |
| 1202 | err = 0; |
| 1203 | goto exit; |
| 1204 | } |
| 1205 | |
| 1206 | if (pollfd == sock) { |
| 1207 | /* Event on the consumerd socket */ |
| 1208 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1209 | ERR("consumer err socket second poll error"); |
| 1210 | goto error; |
| 1211 | } |
| 1212 | health_code_update(); |
| 1213 | /* Wait for any kconsumerd error */ |
| 1214 | ret = lttcomm_recv_unix_sock(sock, &code, |
| 1215 | sizeof(enum lttcomm_return_code)); |
| 1216 | if (ret <= 0) { |
| 1217 | ERR("consumer closed the command socket"); |
| 1218 | goto error; |
| 1219 | } |
| 1220 | |
| 1221 | ERR("consumer return code : %s", |
| 1222 | lttcomm_get_readable_code(-code)); |
| 1223 | |
| 1224 | goto exit; |
| 1225 | } else if (pollfd == consumer_data->metadata_fd) { |
| 1226 | /* UST metadata requests */ |
| 1227 | ret = ust_consumer_metadata_request( |
| 1228 | &consumer_data->metadata_sock); |
| 1229 | if (ret < 0) { |
| 1230 | ERR("Handling metadata request"); |
| 1231 | goto error; |
| 1232 | } |
| 1233 | break; |
| 1234 | } else { |
| 1235 | ERR("Unknown pollfd"); |
| 1236 | goto error; |
| 1237 | } |
| 1238 | } |
| 1239 | health_code_update(); |
| 1240 | } |
| 1241 | |
| 1242 | exit: |
| 1243 | error: |
| 1244 | /* |
| 1245 | * We lock here because we are about to close the sockets and some other |
| 1246 | * thread might be using them so get exclusive access which will abort all |
| 1247 | * other consumer command by other threads. |
| 1248 | */ |
| 1249 | pthread_mutex_lock(&consumer_data->lock); |
| 1250 | |
| 1251 | /* Immediately set the consumerd state to stopped */ |
| 1252 | if (consumer_data->type == LTTNG_CONSUMER_KERNEL) { |
| 1253 | uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR); |
| 1254 | } else if (consumer_data->type == LTTNG_CONSUMER64_UST || |
| 1255 | consumer_data->type == LTTNG_CONSUMER32_UST) { |
| 1256 | uatomic_set(&ust_consumerd_state, CONSUMER_ERROR); |
| 1257 | } else { |
| 1258 | /* Code flow error... */ |
| 1259 | assert(0); |
| 1260 | } |
| 1261 | |
| 1262 | if (consumer_data->err_sock >= 0) { |
| 1263 | ret = close(consumer_data->err_sock); |
| 1264 | if (ret) { |
| 1265 | PERROR("close"); |
| 1266 | } |
| 1267 | consumer_data->err_sock = -1; |
| 1268 | } |
| 1269 | if (consumer_data->cmd_sock >= 0) { |
| 1270 | ret = close(consumer_data->cmd_sock); |
| 1271 | if (ret) { |
| 1272 | PERROR("close"); |
| 1273 | } |
| 1274 | consumer_data->cmd_sock = -1; |
| 1275 | } |
| 1276 | if (*consumer_data->metadata_sock.fd_ptr >= 0) { |
| 1277 | ret = close(*consumer_data->metadata_sock.fd_ptr); |
| 1278 | if (ret) { |
| 1279 | PERROR("close"); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | if (sock >= 0) { |
| 1284 | ret = close(sock); |
| 1285 | if (ret) { |
| 1286 | PERROR("close"); |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | unlink(consumer_data->err_unix_sock_path); |
| 1291 | unlink(consumer_data->cmd_unix_sock_path); |
| 1292 | consumer_data->pid = 0; |
| 1293 | pthread_mutex_unlock(&consumer_data->lock); |
| 1294 | |
| 1295 | /* Cleanup metadata socket mutex. */ |
| 1296 | pthread_mutex_destroy(consumer_data->metadata_sock.lock); |
| 1297 | free(consumer_data->metadata_sock.lock); |
| 1298 | |
| 1299 | lttng_poll_clean(&events); |
| 1300 | error_poll: |
| 1301 | if (err) { |
| 1302 | health_error(); |
| 1303 | ERR("Health error occurred in %s", __func__); |
| 1304 | } |
| 1305 | health_unregister(health_sessiond); |
| 1306 | DBG("consumer thread cleanup completed"); |
| 1307 | |
| 1308 | return NULL; |
| 1309 | } |
| 1310 | |
| 1311 | /* |
| 1312 | * This thread manage application communication. |
| 1313 | */ |
| 1314 | static void *thread_manage_apps(void *data) |
| 1315 | { |
| 1316 | int i, ret, pollfd, err = -1; |
| 1317 | ssize_t size_ret; |
| 1318 | uint32_t revents, nb_fd; |
| 1319 | struct lttng_poll_event events; |
| 1320 | |
| 1321 | DBG("[thread] Manage application started"); |
| 1322 | |
| 1323 | rcu_register_thread(); |
| 1324 | rcu_thread_online(); |
| 1325 | |
| 1326 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE); |
| 1327 | |
| 1328 | if (testpoint(sessiond_thread_manage_apps)) { |
| 1329 | goto error_testpoint; |
| 1330 | } |
| 1331 | |
| 1332 | health_code_update(); |
| 1333 | |
| 1334 | ret = sessiond_set_thread_pollset(&events, 2); |
| 1335 | if (ret < 0) { |
| 1336 | goto error_poll_create; |
| 1337 | } |
| 1338 | |
| 1339 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
| 1340 | if (ret < 0) { |
| 1341 | goto error; |
| 1342 | } |
| 1343 | |
| 1344 | if (testpoint(sessiond_thread_manage_apps_before_loop)) { |
| 1345 | goto error; |
| 1346 | } |
| 1347 | |
| 1348 | health_code_update(); |
| 1349 | |
| 1350 | while (1) { |
| 1351 | DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events)); |
| 1352 | |
| 1353 | /* Inifinite blocking call, waiting for transmission */ |
| 1354 | restart: |
| 1355 | health_poll_entry(); |
| 1356 | ret = lttng_poll_wait(&events, -1); |
| 1357 | health_poll_exit(); |
| 1358 | if (ret < 0) { |
| 1359 | /* |
| 1360 | * Restart interrupted system call. |
| 1361 | */ |
| 1362 | if (errno == EINTR) { |
| 1363 | goto restart; |
| 1364 | } |
| 1365 | goto error; |
| 1366 | } |
| 1367 | |
| 1368 | nb_fd = ret; |
| 1369 | |
| 1370 | for (i = 0; i < nb_fd; i++) { |
| 1371 | /* Fetch once the poll data */ |
| 1372 | revents = LTTNG_POLL_GETEV(&events, i); |
| 1373 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1374 | |
| 1375 | health_code_update(); |
| 1376 | |
| 1377 | /* Thread quit pipe has been closed. Killing thread. */ |
| 1378 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
| 1379 | if (ret) { |
| 1380 | err = 0; |
| 1381 | goto exit; |
| 1382 | } |
| 1383 | |
| 1384 | /* Inspect the apps cmd pipe */ |
| 1385 | if (pollfd == apps_cmd_pipe[0]) { |
| 1386 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1387 | ERR("Apps command pipe error"); |
| 1388 | goto error; |
| 1389 | } else if (revents & LPOLLIN) { |
| 1390 | int sock; |
| 1391 | |
| 1392 | /* Empty pipe */ |
| 1393 | size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock)); |
| 1394 | if (size_ret < sizeof(sock)) { |
| 1395 | PERROR("read apps cmd pipe"); |
| 1396 | goto error; |
| 1397 | } |
| 1398 | |
| 1399 | health_code_update(); |
| 1400 | |
| 1401 | /* |
| 1402 | * We only monitor the error events of the socket. This |
| 1403 | * thread does not handle any incoming data from UST |
| 1404 | * (POLLIN). |
| 1405 | */ |
| 1406 | ret = lttng_poll_add(&events, sock, |
| 1407 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); |
| 1408 | if (ret < 0) { |
| 1409 | goto error; |
| 1410 | } |
| 1411 | |
| 1412 | DBG("Apps with sock %d added to poll set", sock); |
| 1413 | } |
| 1414 | } else { |
| 1415 | /* |
| 1416 | * At this point, we know that a registered application made |
| 1417 | * the event at poll_wait. |
| 1418 | */ |
| 1419 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1420 | /* Removing from the poll set */ |
| 1421 | ret = lttng_poll_del(&events, pollfd); |
| 1422 | if (ret < 0) { |
| 1423 | goto error; |
| 1424 | } |
| 1425 | |
| 1426 | /* Socket closed on remote end. */ |
| 1427 | ust_app_unregister(pollfd); |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | health_code_update(); |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | exit: |
| 1436 | error: |
| 1437 | lttng_poll_clean(&events); |
| 1438 | error_poll_create: |
| 1439 | error_testpoint: |
| 1440 | utils_close_pipe(apps_cmd_pipe); |
| 1441 | apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1; |
| 1442 | |
| 1443 | /* |
| 1444 | * We don't clean the UST app hash table here since already registered |
| 1445 | * applications can still be controlled so let them be until the session |
| 1446 | * daemon dies or the applications stop. |
| 1447 | */ |
| 1448 | |
| 1449 | if (err) { |
| 1450 | health_error(); |
| 1451 | ERR("Health error occurred in %s", __func__); |
| 1452 | } |
| 1453 | health_unregister(health_sessiond); |
| 1454 | DBG("Application communication apps thread cleanup complete"); |
| 1455 | rcu_thread_offline(); |
| 1456 | rcu_unregister_thread(); |
| 1457 | return NULL; |
| 1458 | } |
| 1459 | |
| 1460 | /* |
| 1461 | * Send a socket to a thread This is called from the dispatch UST registration |
| 1462 | * thread once all sockets are set for the application. |
| 1463 | * |
| 1464 | * The sock value can be invalid, we don't really care, the thread will handle |
| 1465 | * it and make the necessary cleanup if so. |
| 1466 | * |
| 1467 | * On success, return 0 else a negative value being the errno message of the |
| 1468 | * write(). |
| 1469 | */ |
| 1470 | static int send_socket_to_thread(int fd, int sock) |
| 1471 | { |
| 1472 | ssize_t ret; |
| 1473 | |
| 1474 | /* |
| 1475 | * It's possible that the FD is set as invalid with -1 concurrently just |
| 1476 | * before calling this function being a shutdown state of the thread. |
| 1477 | */ |
| 1478 | if (fd < 0) { |
| 1479 | ret = -EBADF; |
| 1480 | goto error; |
| 1481 | } |
| 1482 | |
| 1483 | ret = lttng_write(fd, &sock, sizeof(sock)); |
| 1484 | if (ret < sizeof(sock)) { |
| 1485 | PERROR("write apps pipe %d", fd); |
| 1486 | if (ret < 0) { |
| 1487 | ret = -errno; |
| 1488 | } |
| 1489 | goto error; |
| 1490 | } |
| 1491 | |
| 1492 | /* All good. Don't send back the write positive ret value. */ |
| 1493 | ret = 0; |
| 1494 | error: |
| 1495 | return (int) ret; |
| 1496 | } |
| 1497 | |
| 1498 | /* |
| 1499 | * Sanitize the wait queue of the dispatch registration thread meaning removing |
| 1500 | * invalid nodes from it. This is to avoid memory leaks for the case the UST |
| 1501 | * notify socket is never received. |
| 1502 | */ |
| 1503 | static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue) |
| 1504 | { |
| 1505 | int ret, nb_fd = 0, i; |
| 1506 | unsigned int fd_added = 0; |
| 1507 | struct lttng_poll_event events; |
| 1508 | struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node; |
| 1509 | |
| 1510 | assert(wait_queue); |
| 1511 | |
| 1512 | lttng_poll_init(&events); |
| 1513 | |
| 1514 | /* Just skip everything for an empty queue. */ |
| 1515 | if (!wait_queue->count) { |
| 1516 | goto end; |
| 1517 | } |
| 1518 | |
| 1519 | ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC); |
| 1520 | if (ret < 0) { |
| 1521 | goto error_create; |
| 1522 | } |
| 1523 | |
| 1524 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, |
| 1525 | &wait_queue->head, head) { |
| 1526 | assert(wait_node->app); |
| 1527 | ret = lttng_poll_add(&events, wait_node->app->sock, |
| 1528 | LPOLLHUP | LPOLLERR); |
| 1529 | if (ret < 0) { |
| 1530 | goto error; |
| 1531 | } |
| 1532 | |
| 1533 | fd_added = 1; |
| 1534 | } |
| 1535 | |
| 1536 | if (!fd_added) { |
| 1537 | goto end; |
| 1538 | } |
| 1539 | |
| 1540 | /* |
| 1541 | * Poll but don't block so we can quickly identify the faulty events and |
| 1542 | * clean them afterwards from the wait queue. |
| 1543 | */ |
| 1544 | ret = lttng_poll_wait(&events, 0); |
| 1545 | if (ret < 0) { |
| 1546 | goto error; |
| 1547 | } |
| 1548 | nb_fd = ret; |
| 1549 | |
| 1550 | for (i = 0; i < nb_fd; i++) { |
| 1551 | /* Get faulty FD. */ |
| 1552 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); |
| 1553 | int pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1554 | |
| 1555 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, |
| 1556 | &wait_queue->head, head) { |
| 1557 | if (pollfd == wait_node->app->sock && |
| 1558 | (revents & (LPOLLHUP | LPOLLERR))) { |
| 1559 | cds_list_del(&wait_node->head); |
| 1560 | wait_queue->count--; |
| 1561 | ust_app_destroy(wait_node->app); |
| 1562 | free(wait_node); |
| 1563 | break; |
| 1564 | } |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | if (nb_fd > 0) { |
| 1569 | DBG("Wait queue sanitized, %d node were cleaned up", nb_fd); |
| 1570 | } |
| 1571 | |
| 1572 | end: |
| 1573 | lttng_poll_clean(&events); |
| 1574 | return; |
| 1575 | |
| 1576 | error: |
| 1577 | lttng_poll_clean(&events); |
| 1578 | error_create: |
| 1579 | ERR("Unable to sanitize wait queue"); |
| 1580 | return; |
| 1581 | } |
| 1582 | |
| 1583 | /* |
| 1584 | * Dispatch request from the registration threads to the application |
| 1585 | * communication thread. |
| 1586 | */ |
| 1587 | static void *thread_dispatch_ust_registration(void *data) |
| 1588 | { |
| 1589 | int ret, err = -1; |
| 1590 | struct cds_wfq_node *node; |
| 1591 | struct ust_command *ust_cmd = NULL; |
| 1592 | struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node; |
| 1593 | struct ust_reg_wait_queue wait_queue = { |
| 1594 | .count = 0, |
| 1595 | }; |
| 1596 | |
| 1597 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH); |
| 1598 | |
| 1599 | health_code_update(); |
| 1600 | |
| 1601 | CDS_INIT_LIST_HEAD(&wait_queue.head); |
| 1602 | |
| 1603 | DBG("[thread] Dispatch UST command started"); |
| 1604 | |
| 1605 | while (!CMM_LOAD_SHARED(dispatch_thread_exit)) { |
| 1606 | health_code_update(); |
| 1607 | |
| 1608 | /* Atomically prepare the queue futex */ |
| 1609 | futex_nto1_prepare(&ust_cmd_queue.futex); |
| 1610 | |
| 1611 | do { |
| 1612 | struct ust_app *app = NULL; |
| 1613 | ust_cmd = NULL; |
| 1614 | |
| 1615 | /* |
| 1616 | * Make sure we don't have node(s) that have hung up before receiving |
| 1617 | * the notify socket. This is to clean the list in order to avoid |
| 1618 | * memory leaks from notify socket that are never seen. |
| 1619 | */ |
| 1620 | sanitize_wait_queue(&wait_queue); |
| 1621 | |
| 1622 | health_code_update(); |
| 1623 | /* Dequeue command for registration */ |
| 1624 | node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue); |
| 1625 | if (node == NULL) { |
| 1626 | DBG("Woken up but nothing in the UST command queue"); |
| 1627 | /* Continue thread execution */ |
| 1628 | break; |
| 1629 | } |
| 1630 | |
| 1631 | ust_cmd = caa_container_of(node, struct ust_command, node); |
| 1632 | |
| 1633 | DBG("Dispatching UST registration pid:%d ppid:%d uid:%d" |
| 1634 | " gid:%d sock:%d name:%s (version %d.%d)", |
| 1635 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, |
| 1636 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, |
| 1637 | ust_cmd->sock, ust_cmd->reg_msg.name, |
| 1638 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); |
| 1639 | |
| 1640 | if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) { |
| 1641 | wait_node = zmalloc(sizeof(*wait_node)); |
| 1642 | if (!wait_node) { |
| 1643 | PERROR("zmalloc wait_node dispatch"); |
| 1644 | ret = close(ust_cmd->sock); |
| 1645 | if (ret < 0) { |
| 1646 | PERROR("close ust sock dispatch %d", ust_cmd->sock); |
| 1647 | } |
| 1648 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 1649 | free(ust_cmd); |
| 1650 | goto error; |
| 1651 | } |
| 1652 | CDS_INIT_LIST_HEAD(&wait_node->head); |
| 1653 | |
| 1654 | /* Create application object if socket is CMD. */ |
| 1655 | wait_node->app = ust_app_create(&ust_cmd->reg_msg, |
| 1656 | ust_cmd->sock); |
| 1657 | if (!wait_node->app) { |
| 1658 | ret = close(ust_cmd->sock); |
| 1659 | if (ret < 0) { |
| 1660 | PERROR("close ust sock dispatch %d", ust_cmd->sock); |
| 1661 | } |
| 1662 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 1663 | free(wait_node); |
| 1664 | free(ust_cmd); |
| 1665 | continue; |
| 1666 | } |
| 1667 | /* |
| 1668 | * Add application to the wait queue so we can set the notify |
| 1669 | * socket before putting this object in the global ht. |
| 1670 | */ |
| 1671 | cds_list_add(&wait_node->head, &wait_queue.head); |
| 1672 | wait_queue.count++; |
| 1673 | |
| 1674 | free(ust_cmd); |
| 1675 | /* |
| 1676 | * We have to continue here since we don't have the notify |
| 1677 | * socket and the application MUST be added to the hash table |
| 1678 | * only at that moment. |
| 1679 | */ |
| 1680 | continue; |
| 1681 | } else { |
| 1682 | /* |
| 1683 | * Look for the application in the local wait queue and set the |
| 1684 | * notify socket if found. |
| 1685 | */ |
| 1686 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, |
| 1687 | &wait_queue.head, head) { |
| 1688 | health_code_update(); |
| 1689 | if (wait_node->app->pid == ust_cmd->reg_msg.pid) { |
| 1690 | wait_node->app->notify_sock = ust_cmd->sock; |
| 1691 | cds_list_del(&wait_node->head); |
| 1692 | wait_queue.count--; |
| 1693 | app = wait_node->app; |
| 1694 | free(wait_node); |
| 1695 | DBG3("UST app notify socket %d is set", ust_cmd->sock); |
| 1696 | break; |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | /* |
| 1701 | * With no application at this stage the received socket is |
| 1702 | * basically useless so close it before we free the cmd data |
| 1703 | * structure for good. |
| 1704 | */ |
| 1705 | if (!app) { |
| 1706 | ret = close(ust_cmd->sock); |
| 1707 | if (ret < 0) { |
| 1708 | PERROR("close ust sock dispatch %d", ust_cmd->sock); |
| 1709 | } |
| 1710 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 1711 | } |
| 1712 | free(ust_cmd); |
| 1713 | } |
| 1714 | |
| 1715 | if (app) { |
| 1716 | /* |
| 1717 | * @session_lock_list |
| 1718 | * |
| 1719 | * Lock the global session list so from the register up to the |
| 1720 | * registration done message, no thread can see the application |
| 1721 | * and change its state. |
| 1722 | */ |
| 1723 | session_lock_list(); |
| 1724 | rcu_read_lock(); |
| 1725 | |
| 1726 | /* |
| 1727 | * Add application to the global hash table. This needs to be |
| 1728 | * done before the update to the UST registry can locate the |
| 1729 | * application. |
| 1730 | */ |
| 1731 | ust_app_add(app); |
| 1732 | |
| 1733 | /* Set app version. This call will print an error if needed. */ |
| 1734 | (void) ust_app_version(app); |
| 1735 | |
| 1736 | /* Send notify socket through the notify pipe. */ |
| 1737 | ret = send_socket_to_thread(apps_cmd_notify_pipe[1], |
| 1738 | app->notify_sock); |
| 1739 | if (ret < 0) { |
| 1740 | rcu_read_unlock(); |
| 1741 | session_unlock_list(); |
| 1742 | /* |
| 1743 | * No notify thread, stop the UST tracing. However, this is |
| 1744 | * not an internal error of the this thread thus setting |
| 1745 | * the health error code to a normal exit. |
| 1746 | */ |
| 1747 | err = 0; |
| 1748 | goto error; |
| 1749 | } |
| 1750 | |
| 1751 | /* |
| 1752 | * Update newly registered application with the tracing |
| 1753 | * registry info already enabled information. |
| 1754 | */ |
| 1755 | update_ust_app(app->sock); |
| 1756 | |
| 1757 | /* |
| 1758 | * Don't care about return value. Let the manage apps threads |
| 1759 | * handle app unregistration upon socket close. |
| 1760 | */ |
| 1761 | (void) ust_app_register_done(app->sock); |
| 1762 | |
| 1763 | /* |
| 1764 | * Even if the application socket has been closed, send the app |
| 1765 | * to the thread and unregistration will take place at that |
| 1766 | * place. |
| 1767 | */ |
| 1768 | ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock); |
| 1769 | if (ret < 0) { |
| 1770 | rcu_read_unlock(); |
| 1771 | session_unlock_list(); |
| 1772 | /* |
| 1773 | * No apps. thread, stop the UST tracing. However, this is |
| 1774 | * not an internal error of the this thread thus setting |
| 1775 | * the health error code to a normal exit. |
| 1776 | */ |
| 1777 | err = 0; |
| 1778 | goto error; |
| 1779 | } |
| 1780 | |
| 1781 | rcu_read_unlock(); |
| 1782 | session_unlock_list(); |
| 1783 | } |
| 1784 | } while (node != NULL); |
| 1785 | |
| 1786 | health_poll_entry(); |
| 1787 | /* Futex wait on queue. Blocking call on futex() */ |
| 1788 | futex_nto1_wait(&ust_cmd_queue.futex); |
| 1789 | health_poll_exit(); |
| 1790 | } |
| 1791 | /* Normal exit, no error */ |
| 1792 | err = 0; |
| 1793 | |
| 1794 | error: |
| 1795 | /* Clean up wait queue. */ |
| 1796 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, |
| 1797 | &wait_queue.head, head) { |
| 1798 | cds_list_del(&wait_node->head); |
| 1799 | wait_queue.count--; |
| 1800 | free(wait_node); |
| 1801 | } |
| 1802 | |
| 1803 | DBG("Dispatch thread dying"); |
| 1804 | if (err) { |
| 1805 | health_error(); |
| 1806 | ERR("Health error occurred in %s", __func__); |
| 1807 | } |
| 1808 | health_unregister(health_sessiond); |
| 1809 | return NULL; |
| 1810 | } |
| 1811 | |
| 1812 | /* |
| 1813 | * This thread manage application registration. |
| 1814 | */ |
| 1815 | static void *thread_registration_apps(void *data) |
| 1816 | { |
| 1817 | int sock = -1, i, ret, pollfd, err = -1; |
| 1818 | uint32_t revents, nb_fd; |
| 1819 | struct lttng_poll_event events; |
| 1820 | /* |
| 1821 | * Get allocated in this thread, enqueued to a global queue, dequeued and |
| 1822 | * freed in the manage apps thread. |
| 1823 | */ |
| 1824 | struct ust_command *ust_cmd = NULL; |
| 1825 | |
| 1826 | DBG("[thread] Manage application registration started"); |
| 1827 | |
| 1828 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG); |
| 1829 | |
| 1830 | if (testpoint(sessiond_thread_registration_apps)) { |
| 1831 | goto error_testpoint; |
| 1832 | } |
| 1833 | |
| 1834 | ret = lttcomm_listen_unix_sock(apps_sock); |
| 1835 | if (ret < 0) { |
| 1836 | goto error_listen; |
| 1837 | } |
| 1838 | |
| 1839 | /* |
| 1840 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing |
| 1841 | * more will be added to this poll set. |
| 1842 | */ |
| 1843 | ret = sessiond_set_thread_pollset(&events, 2); |
| 1844 | if (ret < 0) { |
| 1845 | goto error_create_poll; |
| 1846 | } |
| 1847 | |
| 1848 | /* Add the application registration socket */ |
| 1849 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); |
| 1850 | if (ret < 0) { |
| 1851 | goto error_poll_add; |
| 1852 | } |
| 1853 | |
| 1854 | /* Notify all applications to register */ |
| 1855 | ret = notify_ust_apps(1); |
| 1856 | if (ret < 0) { |
| 1857 | ERR("Failed to notify applications or create the wait shared memory.\n" |
| 1858 | "Execution continues but there might be problem for already\n" |
| 1859 | "running applications that wishes to register."); |
| 1860 | } |
| 1861 | |
| 1862 | while (1) { |
| 1863 | DBG("Accepting application registration"); |
| 1864 | |
| 1865 | /* Inifinite blocking call, waiting for transmission */ |
| 1866 | restart: |
| 1867 | health_poll_entry(); |
| 1868 | ret = lttng_poll_wait(&events, -1); |
| 1869 | health_poll_exit(); |
| 1870 | if (ret < 0) { |
| 1871 | /* |
| 1872 | * Restart interrupted system call. |
| 1873 | */ |
| 1874 | if (errno == EINTR) { |
| 1875 | goto restart; |
| 1876 | } |
| 1877 | goto error; |
| 1878 | } |
| 1879 | |
| 1880 | nb_fd = ret; |
| 1881 | |
| 1882 | for (i = 0; i < nb_fd; i++) { |
| 1883 | health_code_update(); |
| 1884 | |
| 1885 | /* Fetch once the poll data */ |
| 1886 | revents = LTTNG_POLL_GETEV(&events, i); |
| 1887 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1888 | |
| 1889 | /* Thread quit pipe has been closed. Killing thread. */ |
| 1890 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
| 1891 | if (ret) { |
| 1892 | err = 0; |
| 1893 | goto exit; |
| 1894 | } |
| 1895 | |
| 1896 | /* Event on the registration socket */ |
| 1897 | if (pollfd == apps_sock) { |
| 1898 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1899 | ERR("Register apps socket poll error"); |
| 1900 | goto error; |
| 1901 | } else if (revents & LPOLLIN) { |
| 1902 | sock = lttcomm_accept_unix_sock(apps_sock); |
| 1903 | if (sock < 0) { |
| 1904 | goto error; |
| 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * Set socket timeout for both receiving and ending. |
| 1909 | * app_socket_timeout is in seconds, whereas |
| 1910 | * lttcomm_setsockopt_rcv_timeout and |
| 1911 | * lttcomm_setsockopt_snd_timeout expect msec as |
| 1912 | * parameter. |
| 1913 | */ |
| 1914 | (void) lttcomm_setsockopt_rcv_timeout(sock, |
| 1915 | app_socket_timeout * 1000); |
| 1916 | (void) lttcomm_setsockopt_snd_timeout(sock, |
| 1917 | app_socket_timeout * 1000); |
| 1918 | |
| 1919 | /* |
| 1920 | * Set the CLOEXEC flag. Return code is useless because |
| 1921 | * either way, the show must go on. |
| 1922 | */ |
| 1923 | (void) utils_set_fd_cloexec(sock); |
| 1924 | |
| 1925 | /* Create UST registration command for enqueuing */ |
| 1926 | ust_cmd = zmalloc(sizeof(struct ust_command)); |
| 1927 | if (ust_cmd == NULL) { |
| 1928 | PERROR("ust command zmalloc"); |
| 1929 | goto error; |
| 1930 | } |
| 1931 | |
| 1932 | /* |
| 1933 | * Using message-based transmissions to ensure we don't |
| 1934 | * have to deal with partially received messages. |
| 1935 | */ |
| 1936 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
| 1937 | if (ret < 0) { |
| 1938 | ERR("Exhausted file descriptors allowed for applications."); |
| 1939 | free(ust_cmd); |
| 1940 | ret = close(sock); |
| 1941 | if (ret) { |
| 1942 | PERROR("close"); |
| 1943 | } |
| 1944 | sock = -1; |
| 1945 | continue; |
| 1946 | } |
| 1947 | |
| 1948 | health_code_update(); |
| 1949 | ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg); |
| 1950 | if (ret < 0) { |
| 1951 | free(ust_cmd); |
| 1952 | /* Close socket of the application. */ |
| 1953 | ret = close(sock); |
| 1954 | if (ret) { |
| 1955 | PERROR("close"); |
| 1956 | } |
| 1957 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 1958 | sock = -1; |
| 1959 | continue; |
| 1960 | } |
| 1961 | health_code_update(); |
| 1962 | |
| 1963 | ust_cmd->sock = sock; |
| 1964 | sock = -1; |
| 1965 | |
| 1966 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
| 1967 | " gid:%d sock:%d name:%s (version %d.%d)", |
| 1968 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, |
| 1969 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, |
| 1970 | ust_cmd->sock, ust_cmd->reg_msg.name, |
| 1971 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); |
| 1972 | |
| 1973 | /* |
| 1974 | * Lock free enqueue the registration request. The red pill |
| 1975 | * has been taken! This apps will be part of the *system*. |
| 1976 | */ |
| 1977 | cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node); |
| 1978 | |
| 1979 | /* |
| 1980 | * Wake the registration queue futex. Implicit memory |
| 1981 | * barrier with the exchange in cds_wfq_enqueue. |
| 1982 | */ |
| 1983 | futex_nto1_wake(&ust_cmd_queue.futex); |
| 1984 | } |
| 1985 | } |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | exit: |
| 1990 | error: |
| 1991 | if (err) { |
| 1992 | health_error(); |
| 1993 | ERR("Health error occurred in %s", __func__); |
| 1994 | } |
| 1995 | |
| 1996 | /* Notify that the registration thread is gone */ |
| 1997 | notify_ust_apps(0); |
| 1998 | |
| 1999 | if (apps_sock >= 0) { |
| 2000 | ret = close(apps_sock); |
| 2001 | if (ret) { |
| 2002 | PERROR("close"); |
| 2003 | } |
| 2004 | } |
| 2005 | if (sock >= 0) { |
| 2006 | ret = close(sock); |
| 2007 | if (ret) { |
| 2008 | PERROR("close"); |
| 2009 | } |
| 2010 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 2011 | } |
| 2012 | unlink(apps_unix_sock_path); |
| 2013 | |
| 2014 | error_poll_add: |
| 2015 | lttng_poll_clean(&events); |
| 2016 | error_listen: |
| 2017 | error_create_poll: |
| 2018 | error_testpoint: |
| 2019 | DBG("UST Registration thread cleanup complete"); |
| 2020 | health_unregister(health_sessiond); |
| 2021 | |
| 2022 | return NULL; |
| 2023 | } |
| 2024 | |
| 2025 | /* |
| 2026 | * Start the thread_manage_consumer. This must be done after a lttng-consumerd |
| 2027 | * exec or it will fails. |
| 2028 | */ |
| 2029 | static int spawn_consumer_thread(struct consumer_data *consumer_data) |
| 2030 | { |
| 2031 | int ret, clock_ret; |
| 2032 | struct timespec timeout; |
| 2033 | |
| 2034 | /* Make sure we set the readiness flag to 0 because we are NOT ready */ |
| 2035 | consumer_data->consumer_thread_is_ready = 0; |
| 2036 | |
| 2037 | /* Setup pthread condition */ |
| 2038 | ret = pthread_condattr_init(&consumer_data->condattr); |
| 2039 | if (ret != 0) { |
| 2040 | errno = ret; |
| 2041 | PERROR("pthread_condattr_init consumer data"); |
| 2042 | goto error; |
| 2043 | } |
| 2044 | |
| 2045 | /* |
| 2046 | * Set the monotonic clock in order to make sure we DO NOT jump in time |
| 2047 | * between the clock_gettime() call and the timedwait call. See bug #324 |
| 2048 | * for a more details and how we noticed it. |
| 2049 | */ |
| 2050 | ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC); |
| 2051 | if (ret != 0) { |
| 2052 | errno = ret; |
| 2053 | PERROR("pthread_condattr_setclock consumer data"); |
| 2054 | goto error; |
| 2055 | } |
| 2056 | |
| 2057 | ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr); |
| 2058 | if (ret != 0) { |
| 2059 | errno = ret; |
| 2060 | PERROR("pthread_cond_init consumer data"); |
| 2061 | goto error; |
| 2062 | } |
| 2063 | |
| 2064 | ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer, |
| 2065 | consumer_data); |
| 2066 | if (ret != 0) { |
| 2067 | PERROR("pthread_create consumer"); |
| 2068 | ret = -1; |
| 2069 | goto error; |
| 2070 | } |
| 2071 | |
| 2072 | /* We are about to wait on a pthread condition */ |
| 2073 | pthread_mutex_lock(&consumer_data->cond_mutex); |
| 2074 | |
| 2075 | /* Get time for sem_timedwait absolute timeout */ |
| 2076 | clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout); |
| 2077 | /* |
| 2078 | * Set the timeout for the condition timed wait even if the clock gettime |
| 2079 | * call fails since we might loop on that call and we want to avoid to |
| 2080 | * increment the timeout too many times. |
| 2081 | */ |
| 2082 | timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; |
| 2083 | |
| 2084 | /* |
| 2085 | * The following loop COULD be skipped in some conditions so this is why we |
| 2086 | * set ret to 0 in order to make sure at least one round of the loop is |
| 2087 | * done. |
| 2088 | */ |
| 2089 | ret = 0; |
| 2090 | |
| 2091 | /* |
| 2092 | * Loop until the condition is reached or when a timeout is reached. Note |
| 2093 | * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT |
| 2094 | * be returned but the pthread_cond(3), from the glibc-doc, says that it is |
| 2095 | * possible. This loop does not take any chances and works with both of |
| 2096 | * them. |
| 2097 | */ |
| 2098 | while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) { |
| 2099 | if (clock_ret < 0) { |
| 2100 | PERROR("clock_gettime spawn consumer"); |
| 2101 | /* Infinite wait for the consumerd thread to be ready */ |
| 2102 | ret = pthread_cond_wait(&consumer_data->cond, |
| 2103 | &consumer_data->cond_mutex); |
| 2104 | } else { |
| 2105 | ret = pthread_cond_timedwait(&consumer_data->cond, |
| 2106 | &consumer_data->cond_mutex, &timeout); |
| 2107 | } |
| 2108 | } |
| 2109 | |
| 2110 | /* Release the pthread condition */ |
| 2111 | pthread_mutex_unlock(&consumer_data->cond_mutex); |
| 2112 | |
| 2113 | if (ret != 0) { |
| 2114 | errno = ret; |
| 2115 | if (ret == ETIMEDOUT) { |
| 2116 | /* |
| 2117 | * Call has timed out so we kill the kconsumerd_thread and return |
| 2118 | * an error. |
| 2119 | */ |
| 2120 | ERR("Condition timed out. The consumer thread was never ready." |
| 2121 | " Killing it"); |
| 2122 | ret = pthread_cancel(consumer_data->thread); |
| 2123 | if (ret < 0) { |
| 2124 | PERROR("pthread_cancel consumer thread"); |
| 2125 | } |
| 2126 | } else { |
| 2127 | PERROR("pthread_cond_wait failed consumer thread"); |
| 2128 | } |
| 2129 | goto error; |
| 2130 | } |
| 2131 | |
| 2132 | pthread_mutex_lock(&consumer_data->pid_mutex); |
| 2133 | if (consumer_data->pid == 0) { |
| 2134 | ERR("Consumerd did not start"); |
| 2135 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 2136 | goto error; |
| 2137 | } |
| 2138 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 2139 | |
| 2140 | return 0; |
| 2141 | |
| 2142 | error: |
| 2143 | return ret; |
| 2144 | } |
| 2145 | |
| 2146 | /* |
| 2147 | * Join consumer thread |
| 2148 | */ |
| 2149 | static int join_consumer_thread(struct consumer_data *consumer_data) |
| 2150 | { |
| 2151 | void *status; |
| 2152 | |
| 2153 | /* Consumer pid must be a real one. */ |
| 2154 | if (consumer_data->pid > 0) { |
| 2155 | int ret; |
| 2156 | ret = kill(consumer_data->pid, SIGTERM); |
| 2157 | if (ret) { |
| 2158 | ERR("Error killing consumer daemon"); |
| 2159 | return ret; |
| 2160 | } |
| 2161 | return pthread_join(consumer_data->thread, &status); |
| 2162 | } else { |
| 2163 | return 0; |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | /* |
| 2168 | * Fork and exec a consumer daemon (consumerd). |
| 2169 | * |
| 2170 | * Return pid if successful else -1. |
| 2171 | */ |
| 2172 | static pid_t spawn_consumerd(struct consumer_data *consumer_data) |
| 2173 | { |
| 2174 | int ret; |
| 2175 | pid_t pid; |
| 2176 | const char *consumer_to_use; |
| 2177 | const char *verbosity; |
| 2178 | struct stat st; |
| 2179 | |
| 2180 | DBG("Spawning consumerd"); |
| 2181 | |
| 2182 | pid = fork(); |
| 2183 | if (pid == 0) { |
| 2184 | /* |
| 2185 | * Exec consumerd. |
| 2186 | */ |
| 2187 | if (opt_verbose_consumer) { |
| 2188 | verbosity = "--verbose"; |
| 2189 | } else { |
| 2190 | verbosity = "--quiet"; |
| 2191 | } |
| 2192 | switch (consumer_data->type) { |
| 2193 | case LTTNG_CONSUMER_KERNEL: |
| 2194 | /* |
| 2195 | * Find out which consumerd to execute. We will first try the |
| 2196 | * 64-bit path, then the sessiond's installation directory, and |
| 2197 | * fallback on the 32-bit one, |
| 2198 | */ |
| 2199 | DBG3("Looking for a kernel consumer at these locations:"); |
| 2200 | DBG3(" 1) %s", consumerd64_bin); |
| 2201 | DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE); |
| 2202 | DBG3(" 3) %s", consumerd32_bin); |
| 2203 | if (stat(consumerd64_bin, &st) == 0) { |
| 2204 | DBG3("Found location #1"); |
| 2205 | consumer_to_use = consumerd64_bin; |
| 2206 | } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) { |
| 2207 | DBG3("Found location #2"); |
| 2208 | consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE; |
| 2209 | } else if (stat(consumerd32_bin, &st) == 0) { |
| 2210 | DBG3("Found location #3"); |
| 2211 | consumer_to_use = consumerd32_bin; |
| 2212 | } else { |
| 2213 | DBG("Could not find any valid consumerd executable"); |
| 2214 | break; |
| 2215 | } |
| 2216 | DBG("Using kernel consumer at: %s", consumer_to_use); |
| 2217 | execl(consumer_to_use, |
| 2218 | "lttng-consumerd", verbosity, "-k", |
| 2219 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
| 2220 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, |
| 2221 | "--group", tracing_group_name, |
| 2222 | NULL); |
| 2223 | break; |
| 2224 | case LTTNG_CONSUMER64_UST: |
| 2225 | { |
| 2226 | char *tmpnew = NULL; |
| 2227 | |
| 2228 | if (consumerd64_libdir[0] != '\0') { |
| 2229 | char *tmp; |
| 2230 | size_t tmplen; |
| 2231 | |
| 2232 | tmp = getenv("LD_LIBRARY_PATH"); |
| 2233 | if (!tmp) { |
| 2234 | tmp = ""; |
| 2235 | } |
| 2236 | tmplen = strlen("LD_LIBRARY_PATH=") |
| 2237 | + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp); |
| 2238 | tmpnew = zmalloc(tmplen + 1 /* \0 */); |
| 2239 | if (!tmpnew) { |
| 2240 | ret = -ENOMEM; |
| 2241 | goto error; |
| 2242 | } |
| 2243 | strcpy(tmpnew, "LD_LIBRARY_PATH="); |
| 2244 | strcat(tmpnew, consumerd64_libdir); |
| 2245 | if (tmp[0] != '\0') { |
| 2246 | strcat(tmpnew, ":"); |
| 2247 | strcat(tmpnew, tmp); |
| 2248 | } |
| 2249 | ret = putenv(tmpnew); |
| 2250 | if (ret) { |
| 2251 | ret = -errno; |
| 2252 | free(tmpnew); |
| 2253 | goto error; |
| 2254 | } |
| 2255 | } |
| 2256 | DBG("Using 64-bit UST consumer at: %s", consumerd64_bin); |
| 2257 | ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u", |
| 2258 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
| 2259 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, |
| 2260 | "--group", tracing_group_name, |
| 2261 | NULL); |
| 2262 | if (consumerd64_libdir[0] != '\0') { |
| 2263 | free(tmpnew); |
| 2264 | } |
| 2265 | if (ret) { |
| 2266 | goto error; |
| 2267 | } |
| 2268 | break; |
| 2269 | } |
| 2270 | case LTTNG_CONSUMER32_UST: |
| 2271 | { |
| 2272 | char *tmpnew = NULL; |
| 2273 | |
| 2274 | if (consumerd32_libdir[0] != '\0') { |
| 2275 | char *tmp; |
| 2276 | size_t tmplen; |
| 2277 | |
| 2278 | tmp = getenv("LD_LIBRARY_PATH"); |
| 2279 | if (!tmp) { |
| 2280 | tmp = ""; |
| 2281 | } |
| 2282 | tmplen = strlen("LD_LIBRARY_PATH=") |
| 2283 | + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp); |
| 2284 | tmpnew = zmalloc(tmplen + 1 /* \0 */); |
| 2285 | if (!tmpnew) { |
| 2286 | ret = -ENOMEM; |
| 2287 | goto error; |
| 2288 | } |
| 2289 | strcpy(tmpnew, "LD_LIBRARY_PATH="); |
| 2290 | strcat(tmpnew, consumerd32_libdir); |
| 2291 | if (tmp[0] != '\0') { |
| 2292 | strcat(tmpnew, ":"); |
| 2293 | strcat(tmpnew, tmp); |
| 2294 | } |
| 2295 | ret = putenv(tmpnew); |
| 2296 | if (ret) { |
| 2297 | ret = -errno; |
| 2298 | free(tmpnew); |
| 2299 | goto error; |
| 2300 | } |
| 2301 | } |
| 2302 | DBG("Using 32-bit UST consumer at: %s", consumerd32_bin); |
| 2303 | ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u", |
| 2304 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
| 2305 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, |
| 2306 | "--group", tracing_group_name, |
| 2307 | NULL); |
| 2308 | if (consumerd32_libdir[0] != '\0') { |
| 2309 | free(tmpnew); |
| 2310 | } |
| 2311 | if (ret) { |
| 2312 | goto error; |
| 2313 | } |
| 2314 | break; |
| 2315 | } |
| 2316 | default: |
| 2317 | PERROR("unknown consumer type"); |
| 2318 | exit(EXIT_FAILURE); |
| 2319 | } |
| 2320 | if (errno != 0) { |
| 2321 | PERROR("kernel start consumer exec"); |
| 2322 | } |
| 2323 | exit(EXIT_FAILURE); |
| 2324 | } else if (pid > 0) { |
| 2325 | ret = pid; |
| 2326 | } else { |
| 2327 | PERROR("start consumer fork"); |
| 2328 | ret = -errno; |
| 2329 | } |
| 2330 | error: |
| 2331 | return ret; |
| 2332 | } |
| 2333 | |
| 2334 | /* |
| 2335 | * Spawn the consumerd daemon and session daemon thread. |
| 2336 | */ |
| 2337 | static int start_consumerd(struct consumer_data *consumer_data) |
| 2338 | { |
| 2339 | int ret; |
| 2340 | |
| 2341 | /* |
| 2342 | * Set the listen() state on the socket since there is a possible race |
| 2343 | * between the exec() of the consumer daemon and this call if place in the |
| 2344 | * consumer thread. See bug #366 for more details. |
| 2345 | */ |
| 2346 | ret = lttcomm_listen_unix_sock(consumer_data->err_sock); |
| 2347 | if (ret < 0) { |
| 2348 | goto error; |
| 2349 | } |
| 2350 | |
| 2351 | pthread_mutex_lock(&consumer_data->pid_mutex); |
| 2352 | if (consumer_data->pid != 0) { |
| 2353 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 2354 | goto end; |
| 2355 | } |
| 2356 | |
| 2357 | ret = spawn_consumerd(consumer_data); |
| 2358 | if (ret < 0) { |
| 2359 | ERR("Spawning consumerd failed"); |
| 2360 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 2361 | goto error; |
| 2362 | } |
| 2363 | |
| 2364 | /* Setting up the consumer_data pid */ |
| 2365 | consumer_data->pid = ret; |
| 2366 | DBG2("Consumer pid %d", consumer_data->pid); |
| 2367 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 2368 | |
| 2369 | DBG2("Spawning consumer control thread"); |
| 2370 | ret = spawn_consumer_thread(consumer_data); |
| 2371 | if (ret < 0) { |
| 2372 | ERR("Fatal error spawning consumer control thread"); |
| 2373 | goto error; |
| 2374 | } |
| 2375 | |
| 2376 | end: |
| 2377 | return 0; |
| 2378 | |
| 2379 | error: |
| 2380 | /* Cleanup already created sockets on error. */ |
| 2381 | if (consumer_data->err_sock >= 0) { |
| 2382 | int err; |
| 2383 | |
| 2384 | err = close(consumer_data->err_sock); |
| 2385 | if (err < 0) { |
| 2386 | PERROR("close consumer data error socket"); |
| 2387 | } |
| 2388 | } |
| 2389 | return ret; |
| 2390 | } |
| 2391 | |
| 2392 | /* |
| 2393 | * Setup necessary data for kernel tracer action. |
| 2394 | */ |
| 2395 | static int init_kernel_tracer(void) |
| 2396 | { |
| 2397 | int ret; |
| 2398 | |
| 2399 | /* Modprobe lttng kernel modules */ |
| 2400 | ret = modprobe_lttng_control(); |
| 2401 | if (ret < 0) { |
| 2402 | goto error; |
| 2403 | } |
| 2404 | |
| 2405 | /* Open debugfs lttng */ |
| 2406 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); |
| 2407 | if (kernel_tracer_fd < 0) { |
| 2408 | DBG("Failed to open %s", module_proc_lttng); |
| 2409 | ret = -1; |
| 2410 | goto error_open; |
| 2411 | } |
| 2412 | |
| 2413 | /* Validate kernel version */ |
| 2414 | ret = kernel_validate_version(kernel_tracer_fd); |
| 2415 | if (ret < 0) { |
| 2416 | goto error_version; |
| 2417 | } |
| 2418 | |
| 2419 | ret = modprobe_lttng_data(); |
| 2420 | if (ret < 0) { |
| 2421 | goto error_modules; |
| 2422 | } |
| 2423 | |
| 2424 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
| 2425 | return 0; |
| 2426 | |
| 2427 | error_version: |
| 2428 | modprobe_remove_lttng_control(); |
| 2429 | ret = close(kernel_tracer_fd); |
| 2430 | if (ret) { |
| 2431 | PERROR("close"); |
| 2432 | } |
| 2433 | kernel_tracer_fd = -1; |
| 2434 | return LTTNG_ERR_KERN_VERSION; |
| 2435 | |
| 2436 | error_modules: |
| 2437 | ret = close(kernel_tracer_fd); |
| 2438 | if (ret) { |
| 2439 | PERROR("close"); |
| 2440 | } |
| 2441 | |
| 2442 | error_open: |
| 2443 | modprobe_remove_lttng_control(); |
| 2444 | |
| 2445 | error: |
| 2446 | WARN("No kernel tracer available"); |
| 2447 | kernel_tracer_fd = -1; |
| 2448 | if (!is_root) { |
| 2449 | return LTTNG_ERR_NEED_ROOT_SESSIOND; |
| 2450 | } else { |
| 2451 | return LTTNG_ERR_KERN_NA; |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | |
| 2456 | /* |
| 2457 | * Copy consumer output from the tracing session to the domain session. The |
| 2458 | * function also applies the right modification on a per domain basis for the |
| 2459 | * trace files destination directory. |
| 2460 | * |
| 2461 | * Should *NOT* be called with RCU read-side lock held. |
| 2462 | */ |
| 2463 | static int copy_session_consumer(int domain, struct ltt_session *session) |
| 2464 | { |
| 2465 | int ret; |
| 2466 | const char *dir_name; |
| 2467 | struct consumer_output *consumer; |
| 2468 | |
| 2469 | assert(session); |
| 2470 | assert(session->consumer); |
| 2471 | |
| 2472 | switch (domain) { |
| 2473 | case LTTNG_DOMAIN_KERNEL: |
| 2474 | DBG3("Copying tracing session consumer output in kernel session"); |
| 2475 | /* |
| 2476 | * XXX: We should audit the session creation and what this function |
| 2477 | * does "extra" in order to avoid a destroy since this function is used |
| 2478 | * in the domain session creation (kernel and ust) only. Same for UST |
| 2479 | * domain. |
| 2480 | */ |
| 2481 | if (session->kernel_session->consumer) { |
| 2482 | consumer_destroy_output(session->kernel_session->consumer); |
| 2483 | } |
| 2484 | session->kernel_session->consumer = |
| 2485 | consumer_copy_output(session->consumer); |
| 2486 | /* Ease our life a bit for the next part */ |
| 2487 | consumer = session->kernel_session->consumer; |
| 2488 | dir_name = DEFAULT_KERNEL_TRACE_DIR; |
| 2489 | break; |
| 2490 | case LTTNG_DOMAIN_JUL: |
| 2491 | case LTTNG_DOMAIN_UST: |
| 2492 | DBG3("Copying tracing session consumer output in UST session"); |
| 2493 | if (session->ust_session->consumer) { |
| 2494 | consumer_destroy_output(session->ust_session->consumer); |
| 2495 | } |
| 2496 | session->ust_session->consumer = |
| 2497 | consumer_copy_output(session->consumer); |
| 2498 | /* Ease our life a bit for the next part */ |
| 2499 | consumer = session->ust_session->consumer; |
| 2500 | dir_name = DEFAULT_UST_TRACE_DIR; |
| 2501 | break; |
| 2502 | default: |
| 2503 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
| 2504 | goto error; |
| 2505 | } |
| 2506 | |
| 2507 | /* Append correct directory to subdir */ |
| 2508 | strncat(consumer->subdir, dir_name, |
| 2509 | sizeof(consumer->subdir) - strlen(consumer->subdir) - 1); |
| 2510 | DBG3("Copy session consumer subdir %s", consumer->subdir); |
| 2511 | |
| 2512 | ret = LTTNG_OK; |
| 2513 | |
| 2514 | error: |
| 2515 | return ret; |
| 2516 | } |
| 2517 | |
| 2518 | /* |
| 2519 | * Create an UST session and add it to the session ust list. |
| 2520 | * |
| 2521 | * Should *NOT* be called with RCU read-side lock held. |
| 2522 | */ |
| 2523 | static int create_ust_session(struct ltt_session *session, |
| 2524 | struct lttng_domain *domain) |
| 2525 | { |
| 2526 | int ret; |
| 2527 | struct ltt_ust_session *lus = NULL; |
| 2528 | |
| 2529 | assert(session); |
| 2530 | assert(domain); |
| 2531 | assert(session->consumer); |
| 2532 | |
| 2533 | switch (domain->type) { |
| 2534 | case LTTNG_DOMAIN_JUL: |
| 2535 | case LTTNG_DOMAIN_UST: |
| 2536 | break; |
| 2537 | default: |
| 2538 | ERR("Unknown UST domain on create session %d", domain->type); |
| 2539 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
| 2540 | goto error; |
| 2541 | } |
| 2542 | |
| 2543 | DBG("Creating UST session"); |
| 2544 | |
| 2545 | lus = trace_ust_create_session(session->id); |
| 2546 | if (lus == NULL) { |
| 2547 | ret = LTTNG_ERR_UST_SESS_FAIL; |
| 2548 | goto error; |
| 2549 | } |
| 2550 | |
| 2551 | lus->uid = session->uid; |
| 2552 | lus->gid = session->gid; |
| 2553 | lus->output_traces = session->output_traces; |
| 2554 | lus->snapshot_mode = session->snapshot_mode; |
| 2555 | lus->live_timer_interval = session->live_timer; |
| 2556 | session->ust_session = lus; |
| 2557 | |
| 2558 | /* Copy session output to the newly created UST session */ |
| 2559 | ret = copy_session_consumer(domain->type, session); |
| 2560 | if (ret != LTTNG_OK) { |
| 2561 | goto error; |
| 2562 | } |
| 2563 | |
| 2564 | return LTTNG_OK; |
| 2565 | |
| 2566 | error: |
| 2567 | free(lus); |
| 2568 | session->ust_session = NULL; |
| 2569 | return ret; |
| 2570 | } |
| 2571 | |
| 2572 | /* |
| 2573 | * Create a kernel tracer session then create the default channel. |
| 2574 | */ |
| 2575 | static int create_kernel_session(struct ltt_session *session) |
| 2576 | { |
| 2577 | int ret; |
| 2578 | |
| 2579 | DBG("Creating kernel session"); |
| 2580 | |
| 2581 | ret = kernel_create_session(session, kernel_tracer_fd); |
| 2582 | if (ret < 0) { |
| 2583 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
| 2584 | goto error; |
| 2585 | } |
| 2586 | |
| 2587 | /* Code flow safety */ |
| 2588 | assert(session->kernel_session); |
| 2589 | |
| 2590 | /* Copy session output to the newly created Kernel session */ |
| 2591 | ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session); |
| 2592 | if (ret != LTTNG_OK) { |
| 2593 | goto error; |
| 2594 | } |
| 2595 | |
| 2596 | /* Create directory(ies) on local filesystem. */ |
| 2597 | if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL && |
| 2598 | strlen(session->kernel_session->consumer->dst.trace_path) > 0) { |
| 2599 | ret = run_as_mkdir_recursive( |
| 2600 | session->kernel_session->consumer->dst.trace_path, |
| 2601 | S_IRWXU | S_IRWXG, session->uid, session->gid); |
| 2602 | if (ret < 0) { |
| 2603 | if (ret != -EEXIST) { |
| 2604 | ERR("Trace directory creation error"); |
| 2605 | goto error; |
| 2606 | } |
| 2607 | } |
| 2608 | } |
| 2609 | |
| 2610 | session->kernel_session->uid = session->uid; |
| 2611 | session->kernel_session->gid = session->gid; |
| 2612 | session->kernel_session->output_traces = session->output_traces; |
| 2613 | session->kernel_session->snapshot_mode = session->snapshot_mode; |
| 2614 | |
| 2615 | return LTTNG_OK; |
| 2616 | |
| 2617 | error: |
| 2618 | trace_kernel_destroy_session(session->kernel_session); |
| 2619 | session->kernel_session = NULL; |
| 2620 | return ret; |
| 2621 | } |
| 2622 | |
| 2623 | /* |
| 2624 | * Count number of session permitted by uid/gid. |
| 2625 | */ |
| 2626 | static unsigned int lttng_sessions_count(uid_t uid, gid_t gid) |
| 2627 | { |
| 2628 | unsigned int i = 0; |
| 2629 | struct ltt_session *session; |
| 2630 | |
| 2631 | DBG("Counting number of available session for UID %d GID %d", |
| 2632 | uid, gid); |
| 2633 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
| 2634 | /* |
| 2635 | * Only list the sessions the user can control. |
| 2636 | */ |
| 2637 | if (!session_access_ok(session, uid, gid)) { |
| 2638 | continue; |
| 2639 | } |
| 2640 | i++; |
| 2641 | } |
| 2642 | return i; |
| 2643 | } |
| 2644 | |
| 2645 | /* |
| 2646 | * Process the command requested by the lttng client within the command |
| 2647 | * context structure. This function make sure that the return structure (llm) |
| 2648 | * is set and ready for transmission before returning. |
| 2649 | * |
| 2650 | * Return any error encountered or 0 for success. |
| 2651 | * |
| 2652 | * "sock" is only used for special-case var. len data. |
| 2653 | * |
| 2654 | * Should *NOT* be called with RCU read-side lock held. |
| 2655 | */ |
| 2656 | static int process_client_msg(struct command_ctx *cmd_ctx, int sock, |
| 2657 | int *sock_error) |
| 2658 | { |
| 2659 | int ret = LTTNG_OK; |
| 2660 | int need_tracing_session = 1; |
| 2661 | int need_domain; |
| 2662 | |
| 2663 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); |
| 2664 | |
| 2665 | *sock_error = 0; |
| 2666 | |
| 2667 | switch (cmd_ctx->lsm->cmd_type) { |
| 2668 | case LTTNG_CREATE_SESSION: |
| 2669 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
| 2670 | case LTTNG_CREATE_SESSION_LIVE: |
| 2671 | case LTTNG_DESTROY_SESSION: |
| 2672 | case LTTNG_LIST_SESSIONS: |
| 2673 | case LTTNG_LIST_DOMAINS: |
| 2674 | case LTTNG_START_TRACE: |
| 2675 | case LTTNG_STOP_TRACE: |
| 2676 | case LTTNG_DATA_PENDING: |
| 2677 | case LTTNG_SNAPSHOT_ADD_OUTPUT: |
| 2678 | case LTTNG_SNAPSHOT_DEL_OUTPUT: |
| 2679 | case LTTNG_SNAPSHOT_LIST_OUTPUT: |
| 2680 | case LTTNG_SNAPSHOT_RECORD: |
| 2681 | need_domain = 0; |
| 2682 | break; |
| 2683 | default: |
| 2684 | need_domain = 1; |
| 2685 | } |
| 2686 | |
| 2687 | if (opt_no_kernel && need_domain |
| 2688 | && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) { |
| 2689 | if (!is_root) { |
| 2690 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
| 2691 | } else { |
| 2692 | ret = LTTNG_ERR_KERN_NA; |
| 2693 | } |
| 2694 | goto error; |
| 2695 | } |
| 2696 | |
| 2697 | /* Deny register consumer if we already have a spawned consumer. */ |
| 2698 | if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) { |
| 2699 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
| 2700 | if (kconsumer_data.pid > 0) { |
| 2701 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
| 2702 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
| 2703 | goto error; |
| 2704 | } |
| 2705 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
| 2706 | } |
| 2707 | |
| 2708 | /* |
| 2709 | * Check for command that don't needs to allocate a returned payload. We do |
| 2710 | * this here so we don't have to make the call for no payload at each |
| 2711 | * command. |
| 2712 | */ |
| 2713 | switch(cmd_ctx->lsm->cmd_type) { |
| 2714 | case LTTNG_LIST_SESSIONS: |
| 2715 | case LTTNG_LIST_TRACEPOINTS: |
| 2716 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
| 2717 | case LTTNG_LIST_DOMAINS: |
| 2718 | case LTTNG_LIST_CHANNELS: |
| 2719 | case LTTNG_LIST_EVENTS: |
| 2720 | break; |
| 2721 | default: |
| 2722 | /* Setup lttng message with no payload */ |
| 2723 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 2724 | if (ret < 0) { |
| 2725 | /* This label does not try to unlock the session */ |
| 2726 | goto init_setup_error; |
| 2727 | } |
| 2728 | } |
| 2729 | |
| 2730 | /* Commands that DO NOT need a session. */ |
| 2731 | switch (cmd_ctx->lsm->cmd_type) { |
| 2732 | case LTTNG_CREATE_SESSION: |
| 2733 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
| 2734 | case LTTNG_CREATE_SESSION_LIVE: |
| 2735 | case LTTNG_CALIBRATE: |
| 2736 | case LTTNG_LIST_SESSIONS: |
| 2737 | case LTTNG_LIST_TRACEPOINTS: |
| 2738 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
| 2739 | need_tracing_session = 0; |
| 2740 | break; |
| 2741 | default: |
| 2742 | DBG("Getting session %s by name", cmd_ctx->lsm->session.name); |
| 2743 | /* |
| 2744 | * We keep the session list lock across _all_ commands |
| 2745 | * for now, because the per-session lock does not |
| 2746 | * handle teardown properly. |
| 2747 | */ |
| 2748 | session_lock_list(); |
| 2749 | cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name); |
| 2750 | if (cmd_ctx->session == NULL) { |
| 2751 | ret = LTTNG_ERR_SESS_NOT_FOUND; |
| 2752 | goto error; |
| 2753 | } else { |
| 2754 | /* Acquire lock for the session */ |
| 2755 | session_lock(cmd_ctx->session); |
| 2756 | } |
| 2757 | break; |
| 2758 | } |
| 2759 | |
| 2760 | if (!need_domain) { |
| 2761 | goto skip_domain; |
| 2762 | } |
| 2763 | |
| 2764 | /* |
| 2765 | * Check domain type for specific "pre-action". |
| 2766 | */ |
| 2767 | switch (cmd_ctx->lsm->domain.type) { |
| 2768 | case LTTNG_DOMAIN_KERNEL: |
| 2769 | if (!is_root) { |
| 2770 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
| 2771 | goto error; |
| 2772 | } |
| 2773 | |
| 2774 | /* Kernel tracer check */ |
| 2775 | if (kernel_tracer_fd == -1) { |
| 2776 | /* Basically, load kernel tracer modules */ |
| 2777 | ret = init_kernel_tracer(); |
| 2778 | if (ret != 0) { |
| 2779 | goto error; |
| 2780 | } |
| 2781 | } |
| 2782 | |
| 2783 | /* Consumer is in an ERROR state. Report back to client */ |
| 2784 | if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) { |
| 2785 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
| 2786 | goto error; |
| 2787 | } |
| 2788 | |
| 2789 | /* Need a session for kernel command */ |
| 2790 | if (need_tracing_session) { |
| 2791 | if (cmd_ctx->session->kernel_session == NULL) { |
| 2792 | ret = create_kernel_session(cmd_ctx->session); |
| 2793 | if (ret < 0) { |
| 2794 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
| 2795 | goto error; |
| 2796 | } |
| 2797 | } |
| 2798 | |
| 2799 | /* Start the kernel consumer daemon */ |
| 2800 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
| 2801 | if (kconsumer_data.pid == 0 && |
| 2802 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
| 2803 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
| 2804 | ret = start_consumerd(&kconsumer_data); |
| 2805 | if (ret < 0) { |
| 2806 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
| 2807 | goto error; |
| 2808 | } |
| 2809 | uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED); |
| 2810 | } else { |
| 2811 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
| 2812 | } |
| 2813 | |
| 2814 | /* |
| 2815 | * The consumer was just spawned so we need to add the socket to |
| 2816 | * the consumer output of the session if exist. |
| 2817 | */ |
| 2818 | ret = consumer_create_socket(&kconsumer_data, |
| 2819 | cmd_ctx->session->kernel_session->consumer); |
| 2820 | if (ret < 0) { |
| 2821 | goto error; |
| 2822 | } |
| 2823 | } |
| 2824 | |
| 2825 | break; |
| 2826 | case LTTNG_DOMAIN_JUL: |
| 2827 | case LTTNG_DOMAIN_UST: |
| 2828 | { |
| 2829 | if (!ust_app_supported()) { |
| 2830 | ret = LTTNG_ERR_NO_UST; |
| 2831 | goto error; |
| 2832 | } |
| 2833 | /* Consumer is in an ERROR state. Report back to client */ |
| 2834 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { |
| 2835 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
| 2836 | goto error; |
| 2837 | } |
| 2838 | |
| 2839 | if (need_tracing_session) { |
| 2840 | /* Create UST session if none exist. */ |
| 2841 | if (cmd_ctx->session->ust_session == NULL) { |
| 2842 | ret = create_ust_session(cmd_ctx->session, |
| 2843 | &cmd_ctx->lsm->domain); |
| 2844 | if (ret != LTTNG_OK) { |
| 2845 | goto error; |
| 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | /* Start the UST consumer daemons */ |
| 2850 | /* 64-bit */ |
| 2851 | pthread_mutex_lock(&ustconsumer64_data.pid_mutex); |
| 2852 | if (consumerd64_bin[0] != '\0' && |
| 2853 | ustconsumer64_data.pid == 0 && |
| 2854 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
| 2855 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
| 2856 | ret = start_consumerd(&ustconsumer64_data); |
| 2857 | if (ret < 0) { |
| 2858 | ret = LTTNG_ERR_UST_CONSUMER64_FAIL; |
| 2859 | uatomic_set(&ust_consumerd64_fd, -EINVAL); |
| 2860 | goto error; |
| 2861 | } |
| 2862 | |
| 2863 | uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock); |
| 2864 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
| 2865 | } else { |
| 2866 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
| 2867 | } |
| 2868 | |
| 2869 | /* |
| 2870 | * Setup socket for consumer 64 bit. No need for atomic access |
| 2871 | * since it was set above and can ONLY be set in this thread. |
| 2872 | */ |
| 2873 | ret = consumer_create_socket(&ustconsumer64_data, |
| 2874 | cmd_ctx->session->ust_session->consumer); |
| 2875 | if (ret < 0) { |
| 2876 | goto error; |
| 2877 | } |
| 2878 | |
| 2879 | /* 32-bit */ |
| 2880 | if (consumerd32_bin[0] != '\0' && |
| 2881 | ustconsumer32_data.pid == 0 && |
| 2882 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
| 2883 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); |
| 2884 | ret = start_consumerd(&ustconsumer32_data); |
| 2885 | if (ret < 0) { |
| 2886 | ret = LTTNG_ERR_UST_CONSUMER32_FAIL; |
| 2887 | uatomic_set(&ust_consumerd32_fd, -EINVAL); |
| 2888 | goto error; |
| 2889 | } |
| 2890 | |
| 2891 | uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock); |
| 2892 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
| 2893 | } else { |
| 2894 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); |
| 2895 | } |
| 2896 | |
| 2897 | /* |
| 2898 | * Setup socket for consumer 64 bit. No need for atomic access |
| 2899 | * since it was set above and can ONLY be set in this thread. |
| 2900 | */ |
| 2901 | ret = consumer_create_socket(&ustconsumer32_data, |
| 2902 | cmd_ctx->session->ust_session->consumer); |
| 2903 | if (ret < 0) { |
| 2904 | goto error; |
| 2905 | } |
| 2906 | } |
| 2907 | break; |
| 2908 | } |
| 2909 | default: |
| 2910 | break; |
| 2911 | } |
| 2912 | skip_domain: |
| 2913 | |
| 2914 | /* Validate consumer daemon state when start/stop trace command */ |
| 2915 | if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE || |
| 2916 | cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) { |
| 2917 | switch (cmd_ctx->lsm->domain.type) { |
| 2918 | case LTTNG_DOMAIN_JUL: |
| 2919 | case LTTNG_DOMAIN_UST: |
| 2920 | if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) { |
| 2921 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
| 2922 | goto error; |
| 2923 | } |
| 2924 | break; |
| 2925 | case LTTNG_DOMAIN_KERNEL: |
| 2926 | if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) { |
| 2927 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
| 2928 | goto error; |
| 2929 | } |
| 2930 | break; |
| 2931 | } |
| 2932 | } |
| 2933 | |
| 2934 | /* |
| 2935 | * Check that the UID or GID match that of the tracing session. |
| 2936 | * The root user can interact with all sessions. |
| 2937 | */ |
| 2938 | if (need_tracing_session) { |
| 2939 | if (!session_access_ok(cmd_ctx->session, |
| 2940 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
| 2941 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) { |
| 2942 | ret = LTTNG_ERR_EPERM; |
| 2943 | goto error; |
| 2944 | } |
| 2945 | } |
| 2946 | |
| 2947 | /* |
| 2948 | * Send relayd information to consumer as soon as we have a domain and a |
| 2949 | * session defined. |
| 2950 | */ |
| 2951 | if (cmd_ctx->session && need_domain) { |
| 2952 | /* |
| 2953 | * Setup relayd if not done yet. If the relayd information was already |
| 2954 | * sent to the consumer, this call will gracefully return. |
| 2955 | */ |
| 2956 | ret = cmd_setup_relayd(cmd_ctx->session); |
| 2957 | if (ret != LTTNG_OK) { |
| 2958 | goto error; |
| 2959 | } |
| 2960 | } |
| 2961 | |
| 2962 | /* Process by command type */ |
| 2963 | switch (cmd_ctx->lsm->cmd_type) { |
| 2964 | case LTTNG_ADD_CONTEXT: |
| 2965 | { |
| 2966 | ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 2967 | cmd_ctx->lsm->u.context.channel_name, |
| 2968 | &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]); |
| 2969 | break; |
| 2970 | } |
| 2971 | case LTTNG_DISABLE_CHANNEL: |
| 2972 | { |
| 2973 | ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 2974 | cmd_ctx->lsm->u.disable.channel_name); |
| 2975 | break; |
| 2976 | } |
| 2977 | case LTTNG_DISABLE_EVENT: |
| 2978 | { |
| 2979 | ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 2980 | cmd_ctx->lsm->u.disable.channel_name, |
| 2981 | cmd_ctx->lsm->u.disable.name); |
| 2982 | break; |
| 2983 | } |
| 2984 | case LTTNG_DISABLE_ALL_EVENT: |
| 2985 | { |
| 2986 | DBG("Disabling all events"); |
| 2987 | |
| 2988 | ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 2989 | cmd_ctx->lsm->u.disable.channel_name); |
| 2990 | break; |
| 2991 | } |
| 2992 | case LTTNG_ENABLE_CHANNEL: |
| 2993 | { |
| 2994 | ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain, |
| 2995 | &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]); |
| 2996 | break; |
| 2997 | } |
| 2998 | case LTTNG_ENABLE_EVENT: |
| 2999 | { |
| 3000 | struct lttng_event_exclusion *exclusion = NULL; |
| 3001 | struct lttng_filter_bytecode *bytecode = NULL; |
| 3002 | |
| 3003 | /* Handle exclusion events and receive it from the client. */ |
| 3004 | if (cmd_ctx->lsm->u.enable.exclusion_count > 0) { |
| 3005 | size_t count = cmd_ctx->lsm->u.enable.exclusion_count; |
| 3006 | |
| 3007 | exclusion = zmalloc(sizeof(struct lttng_event_exclusion) + |
| 3008 | (count * LTTNG_SYMBOL_NAME_LEN)); |
| 3009 | if (!exclusion) { |
| 3010 | ret = LTTNG_ERR_EXCLUSION_NOMEM; |
| 3011 | goto error; |
| 3012 | } |
| 3013 | |
| 3014 | DBG("Receiving var len exclusion event list from client ..."); |
| 3015 | exclusion->count = count; |
| 3016 | ret = lttcomm_recv_unix_sock(sock, exclusion->names, |
| 3017 | count * LTTNG_SYMBOL_NAME_LEN); |
| 3018 | if (ret <= 0) { |
| 3019 | DBG("Nothing recv() from client var len data... continuing"); |
| 3020 | *sock_error = 1; |
| 3021 | free(exclusion); |
| 3022 | ret = LTTNG_ERR_EXCLUSION_INVAL; |
| 3023 | goto error; |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | /* Handle filter and get bytecode from client. */ |
| 3028 | if (cmd_ctx->lsm->u.enable.bytecode_len > 0) { |
| 3029 | size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len; |
| 3030 | |
| 3031 | if (bytecode_len > LTTNG_FILTER_MAX_LEN) { |
| 3032 | ret = LTTNG_ERR_FILTER_INVAL; |
| 3033 | free(exclusion); |
| 3034 | goto error; |
| 3035 | } |
| 3036 | |
| 3037 | bytecode = zmalloc(bytecode_len); |
| 3038 | if (!bytecode) { |
| 3039 | free(exclusion); |
| 3040 | ret = LTTNG_ERR_FILTER_NOMEM; |
| 3041 | goto error; |
| 3042 | } |
| 3043 | |
| 3044 | /* Receive var. len. data */ |
| 3045 | DBG("Receiving var len filter's bytecode from client ..."); |
| 3046 | ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len); |
| 3047 | if (ret <= 0) { |
| 3048 | DBG("Nothing recv() from client car len data... continuing"); |
| 3049 | *sock_error = 1; |
| 3050 | free(bytecode); |
| 3051 | free(exclusion); |
| 3052 | ret = LTTNG_ERR_FILTER_INVAL; |
| 3053 | goto error; |
| 3054 | } |
| 3055 | |
| 3056 | if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) { |
| 3057 | free(bytecode); |
| 3058 | free(exclusion); |
| 3059 | ret = LTTNG_ERR_FILTER_INVAL; |
| 3060 | goto error; |
| 3061 | } |
| 3062 | } |
| 3063 | |
| 3064 | ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain, |
| 3065 | cmd_ctx->lsm->u.enable.channel_name, |
| 3066 | &cmd_ctx->lsm->u.enable.event, bytecode, exclusion, |
| 3067 | kernel_poll_pipe[1]); |
| 3068 | break; |
| 3069 | } |
| 3070 | case LTTNG_ENABLE_ALL_EVENT: |
| 3071 | { |
| 3072 | DBG("Enabling all events"); |
| 3073 | |
| 3074 | ret = cmd_enable_event_all(cmd_ctx->session, &cmd_ctx->lsm->domain, |
| 3075 | cmd_ctx->lsm->u.enable.channel_name, |
| 3076 | cmd_ctx->lsm->u.enable.event.type, NULL, kernel_poll_pipe[1]); |
| 3077 | break; |
| 3078 | } |
| 3079 | case LTTNG_LIST_TRACEPOINTS: |
| 3080 | { |
| 3081 | struct lttng_event *events; |
| 3082 | ssize_t nb_events; |
| 3083 | |
| 3084 | nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events); |
| 3085 | if (nb_events < 0) { |
| 3086 | /* Return value is a negative lttng_error_code. */ |
| 3087 | ret = -nb_events; |
| 3088 | goto error; |
| 3089 | } |
| 3090 | |
| 3091 | /* |
| 3092 | * Setup lttng message with payload size set to the event list size in |
| 3093 | * bytes and then copy list into the llm payload. |
| 3094 | */ |
| 3095 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events); |
| 3096 | if (ret < 0) { |
| 3097 | free(events); |
| 3098 | goto setup_error; |
| 3099 | } |
| 3100 | |
| 3101 | /* Copy event list into message payload */ |
| 3102 | memcpy(cmd_ctx->llm->payload, events, |
| 3103 | sizeof(struct lttng_event) * nb_events); |
| 3104 | |
| 3105 | free(events); |
| 3106 | |
| 3107 | ret = LTTNG_OK; |
| 3108 | break; |
| 3109 | } |
| 3110 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
| 3111 | { |
| 3112 | struct lttng_event_field *fields; |
| 3113 | ssize_t nb_fields; |
| 3114 | |
| 3115 | nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type, |
| 3116 | &fields); |
| 3117 | if (nb_fields < 0) { |
| 3118 | /* Return value is a negative lttng_error_code. */ |
| 3119 | ret = -nb_fields; |
| 3120 | goto error; |
| 3121 | } |
| 3122 | |
| 3123 | /* |
| 3124 | * Setup lttng message with payload size set to the event list size in |
| 3125 | * bytes and then copy list into the llm payload. |
| 3126 | */ |
| 3127 | ret = setup_lttng_msg(cmd_ctx, |
| 3128 | sizeof(struct lttng_event_field) * nb_fields); |
| 3129 | if (ret < 0) { |
| 3130 | free(fields); |
| 3131 | goto setup_error; |
| 3132 | } |
| 3133 | |
| 3134 | /* Copy event list into message payload */ |
| 3135 | memcpy(cmd_ctx->llm->payload, fields, |
| 3136 | sizeof(struct lttng_event_field) * nb_fields); |
| 3137 | |
| 3138 | free(fields); |
| 3139 | |
| 3140 | ret = LTTNG_OK; |
| 3141 | break; |
| 3142 | } |
| 3143 | case LTTNG_SET_CONSUMER_URI: |
| 3144 | { |
| 3145 | size_t nb_uri, len; |
| 3146 | struct lttng_uri *uris; |
| 3147 | |
| 3148 | nb_uri = cmd_ctx->lsm->u.uri.size; |
| 3149 | len = nb_uri * sizeof(struct lttng_uri); |
| 3150 | |
| 3151 | if (nb_uri == 0) { |
| 3152 | ret = LTTNG_ERR_INVALID; |
| 3153 | goto error; |
| 3154 | } |
| 3155 | |
| 3156 | uris = zmalloc(len); |
| 3157 | if (uris == NULL) { |
| 3158 | ret = LTTNG_ERR_FATAL; |
| 3159 | goto error; |
| 3160 | } |
| 3161 | |
| 3162 | /* Receive variable len data */ |
| 3163 | DBG("Receiving %zu URI(s) from client ...", nb_uri); |
| 3164 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
| 3165 | if (ret <= 0) { |
| 3166 | DBG("No URIs received from client... continuing"); |
| 3167 | *sock_error = 1; |
| 3168 | ret = LTTNG_ERR_SESSION_FAIL; |
| 3169 | free(uris); |
| 3170 | goto error; |
| 3171 | } |
| 3172 | |
| 3173 | ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session, |
| 3174 | nb_uri, uris); |
| 3175 | if (ret != LTTNG_OK) { |
| 3176 | free(uris); |
| 3177 | goto error; |
| 3178 | } |
| 3179 | |
| 3180 | /* |
| 3181 | * XXX: 0 means that this URI should be applied on the session. Should |
| 3182 | * be a DOMAIN enuam. |
| 3183 | */ |
| 3184 | if (cmd_ctx->lsm->domain.type == 0) { |
| 3185 | /* Add the URI for the UST session if a consumer is present. */ |
| 3186 | if (cmd_ctx->session->ust_session && |
| 3187 | cmd_ctx->session->ust_session->consumer) { |
| 3188 | ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session, |
| 3189 | nb_uri, uris); |
| 3190 | } else if (cmd_ctx->session->kernel_session && |
| 3191 | cmd_ctx->session->kernel_session->consumer) { |
| 3192 | ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL, |
| 3193 | cmd_ctx->session, nb_uri, uris); |
| 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | free(uris); |
| 3198 | |
| 3199 | break; |
| 3200 | } |
| 3201 | case LTTNG_START_TRACE: |
| 3202 | { |
| 3203 | ret = cmd_start_trace(cmd_ctx->session); |
| 3204 | break; |
| 3205 | } |
| 3206 | case LTTNG_STOP_TRACE: |
| 3207 | { |
| 3208 | ret = cmd_stop_trace(cmd_ctx->session); |
| 3209 | break; |
| 3210 | } |
| 3211 | case LTTNG_CREATE_SESSION: |
| 3212 | { |
| 3213 | size_t nb_uri, len; |
| 3214 | struct lttng_uri *uris = NULL; |
| 3215 | |
| 3216 | nb_uri = cmd_ctx->lsm->u.uri.size; |
| 3217 | len = nb_uri * sizeof(struct lttng_uri); |
| 3218 | |
| 3219 | if (nb_uri > 0) { |
| 3220 | uris = zmalloc(len); |
| 3221 | if (uris == NULL) { |
| 3222 | ret = LTTNG_ERR_FATAL; |
| 3223 | goto error; |
| 3224 | } |
| 3225 | |
| 3226 | /* Receive variable len data */ |
| 3227 | DBG("Waiting for %zu URIs from client ...", nb_uri); |
| 3228 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
| 3229 | if (ret <= 0) { |
| 3230 | DBG("No URIs received from client... continuing"); |
| 3231 | *sock_error = 1; |
| 3232 | ret = LTTNG_ERR_SESSION_FAIL; |
| 3233 | free(uris); |
| 3234 | goto error; |
| 3235 | } |
| 3236 | |
| 3237 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { |
| 3238 | DBG("Creating session with ONE network URI is a bad call"); |
| 3239 | ret = LTTNG_ERR_SESSION_FAIL; |
| 3240 | free(uris); |
| 3241 | goto error; |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri, |
| 3246 | &cmd_ctx->creds, 0); |
| 3247 | |
| 3248 | free(uris); |
| 3249 | |
| 3250 | break; |
| 3251 | } |
| 3252 | case LTTNG_DESTROY_SESSION: |
| 3253 | { |
| 3254 | ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]); |
| 3255 | |
| 3256 | /* Set session to NULL so we do not unlock it after free. */ |
| 3257 | cmd_ctx->session = NULL; |
| 3258 | break; |
| 3259 | } |
| 3260 | case LTTNG_LIST_DOMAINS: |
| 3261 | { |
| 3262 | ssize_t nb_dom; |
| 3263 | struct lttng_domain *domains; |
| 3264 | |
| 3265 | nb_dom = cmd_list_domains(cmd_ctx->session, &domains); |
| 3266 | if (nb_dom < 0) { |
| 3267 | /* Return value is a negative lttng_error_code. */ |
| 3268 | ret = -nb_dom; |
| 3269 | goto error; |
| 3270 | } |
| 3271 | |
| 3272 | ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain)); |
| 3273 | if (ret < 0) { |
| 3274 | free(domains); |
| 3275 | goto setup_error; |
| 3276 | } |
| 3277 | |
| 3278 | /* Copy event list into message payload */ |
| 3279 | memcpy(cmd_ctx->llm->payload, domains, |
| 3280 | nb_dom * sizeof(struct lttng_domain)); |
| 3281 | |
| 3282 | free(domains); |
| 3283 | |
| 3284 | ret = LTTNG_OK; |
| 3285 | break; |
| 3286 | } |
| 3287 | case LTTNG_LIST_CHANNELS: |
| 3288 | { |
| 3289 | int nb_chan; |
| 3290 | struct lttng_channel *channels; |
| 3291 | |
| 3292 | nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type, |
| 3293 | cmd_ctx->session, &channels); |
| 3294 | if (nb_chan < 0) { |
| 3295 | /* Return value is a negative lttng_error_code. */ |
| 3296 | ret = -nb_chan; |
| 3297 | goto error; |
| 3298 | } |
| 3299 | |
| 3300 | ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel)); |
| 3301 | if (ret < 0) { |
| 3302 | free(channels); |
| 3303 | goto setup_error; |
| 3304 | } |
| 3305 | |
| 3306 | /* Copy event list into message payload */ |
| 3307 | memcpy(cmd_ctx->llm->payload, channels, |
| 3308 | nb_chan * sizeof(struct lttng_channel)); |
| 3309 | |
| 3310 | free(channels); |
| 3311 | |
| 3312 | ret = LTTNG_OK; |
| 3313 | break; |
| 3314 | } |
| 3315 | case LTTNG_LIST_EVENTS: |
| 3316 | { |
| 3317 | ssize_t nb_event; |
| 3318 | struct lttng_event *events = NULL; |
| 3319 | |
| 3320 | nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session, |
| 3321 | cmd_ctx->lsm->u.list.channel_name, &events); |
| 3322 | if (nb_event < 0) { |
| 3323 | /* Return value is a negative lttng_error_code. */ |
| 3324 | ret = -nb_event; |
| 3325 | goto error; |
| 3326 | } |
| 3327 | |
| 3328 | ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event)); |
| 3329 | if (ret < 0) { |
| 3330 | free(events); |
| 3331 | goto setup_error; |
| 3332 | } |
| 3333 | |
| 3334 | /* Copy event list into message payload */ |
| 3335 | memcpy(cmd_ctx->llm->payload, events, |
| 3336 | nb_event * sizeof(struct lttng_event)); |
| 3337 | |
| 3338 | free(events); |
| 3339 | |
| 3340 | ret = LTTNG_OK; |
| 3341 | break; |
| 3342 | } |
| 3343 | case LTTNG_LIST_SESSIONS: |
| 3344 | { |
| 3345 | unsigned int nr_sessions; |
| 3346 | |
| 3347 | session_lock_list(); |
| 3348 | nr_sessions = lttng_sessions_count( |
| 3349 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
| 3350 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); |
| 3351 | |
| 3352 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions); |
| 3353 | if (ret < 0) { |
| 3354 | session_unlock_list(); |
| 3355 | goto setup_error; |
| 3356 | } |
| 3357 | |
| 3358 | /* Filled the session array */ |
| 3359 | cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload), |
| 3360 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
| 3361 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); |
| 3362 | |
| 3363 | session_unlock_list(); |
| 3364 | |
| 3365 | ret = LTTNG_OK; |
| 3366 | break; |
| 3367 | } |
| 3368 | case LTTNG_CALIBRATE: |
| 3369 | { |
| 3370 | ret = cmd_calibrate(cmd_ctx->lsm->domain.type, |
| 3371 | &cmd_ctx->lsm->u.calibrate); |
| 3372 | break; |
| 3373 | } |
| 3374 | case LTTNG_REGISTER_CONSUMER: |
| 3375 | { |
| 3376 | struct consumer_data *cdata; |
| 3377 | |
| 3378 | switch (cmd_ctx->lsm->domain.type) { |
| 3379 | case LTTNG_DOMAIN_KERNEL: |
| 3380 | cdata = &kconsumer_data; |
| 3381 | break; |
| 3382 | default: |
| 3383 | ret = LTTNG_ERR_UND; |
| 3384 | goto error; |
| 3385 | } |
| 3386 | |
| 3387 | ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 3388 | cmd_ctx->lsm->u.reg.path, cdata); |
| 3389 | break; |
| 3390 | } |
| 3391 | case LTTNG_DATA_PENDING: |
| 3392 | { |
| 3393 | ret = cmd_data_pending(cmd_ctx->session); |
| 3394 | break; |
| 3395 | } |
| 3396 | case LTTNG_SNAPSHOT_ADD_OUTPUT: |
| 3397 | { |
| 3398 | struct lttcomm_lttng_output_id reply; |
| 3399 | |
| 3400 | ret = cmd_snapshot_add_output(cmd_ctx->session, |
| 3401 | &cmd_ctx->lsm->u.snapshot_output.output, &reply.id); |
| 3402 | if (ret != LTTNG_OK) { |
| 3403 | goto error; |
| 3404 | } |
| 3405 | |
| 3406 | ret = setup_lttng_msg(cmd_ctx, sizeof(reply)); |
| 3407 | if (ret < 0) { |
| 3408 | goto setup_error; |
| 3409 | } |
| 3410 | |
| 3411 | /* Copy output list into message payload */ |
| 3412 | memcpy(cmd_ctx->llm->payload, &reply, sizeof(reply)); |
| 3413 | ret = LTTNG_OK; |
| 3414 | break; |
| 3415 | } |
| 3416 | case LTTNG_SNAPSHOT_DEL_OUTPUT: |
| 3417 | { |
| 3418 | ret = cmd_snapshot_del_output(cmd_ctx->session, |
| 3419 | &cmd_ctx->lsm->u.snapshot_output.output); |
| 3420 | break; |
| 3421 | } |
| 3422 | case LTTNG_SNAPSHOT_LIST_OUTPUT: |
| 3423 | { |
| 3424 | ssize_t nb_output; |
| 3425 | struct lttng_snapshot_output *outputs = NULL; |
| 3426 | |
| 3427 | nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs); |
| 3428 | if (nb_output < 0) { |
| 3429 | ret = -nb_output; |
| 3430 | goto error; |
| 3431 | } |
| 3432 | |
| 3433 | ret = setup_lttng_msg(cmd_ctx, |
| 3434 | nb_output * sizeof(struct lttng_snapshot_output)); |
| 3435 | if (ret < 0) { |
| 3436 | free(outputs); |
| 3437 | goto setup_error; |
| 3438 | } |
| 3439 | |
| 3440 | if (outputs) { |
| 3441 | /* Copy output list into message payload */ |
| 3442 | memcpy(cmd_ctx->llm->payload, outputs, |
| 3443 | nb_output * sizeof(struct lttng_snapshot_output)); |
| 3444 | free(outputs); |
| 3445 | } |
| 3446 | |
| 3447 | ret = LTTNG_OK; |
| 3448 | break; |
| 3449 | } |
| 3450 | case LTTNG_SNAPSHOT_RECORD: |
| 3451 | { |
| 3452 | ret = cmd_snapshot_record(cmd_ctx->session, |
| 3453 | &cmd_ctx->lsm->u.snapshot_record.output, |
| 3454 | cmd_ctx->lsm->u.snapshot_record.wait); |
| 3455 | break; |
| 3456 | } |
| 3457 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
| 3458 | { |
| 3459 | size_t nb_uri, len; |
| 3460 | struct lttng_uri *uris = NULL; |
| 3461 | |
| 3462 | nb_uri = cmd_ctx->lsm->u.uri.size; |
| 3463 | len = nb_uri * sizeof(struct lttng_uri); |
| 3464 | |
| 3465 | if (nb_uri > 0) { |
| 3466 | uris = zmalloc(len); |
| 3467 | if (uris == NULL) { |
| 3468 | ret = LTTNG_ERR_FATAL; |
| 3469 | goto error; |
| 3470 | } |
| 3471 | |
| 3472 | /* Receive variable len data */ |
| 3473 | DBG("Waiting for %zu URIs from client ...", nb_uri); |
| 3474 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
| 3475 | if (ret <= 0) { |
| 3476 | DBG("No URIs received from client... continuing"); |
| 3477 | *sock_error = 1; |
| 3478 | ret = LTTNG_ERR_SESSION_FAIL; |
| 3479 | free(uris); |
| 3480 | goto error; |
| 3481 | } |
| 3482 | |
| 3483 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { |
| 3484 | DBG("Creating session with ONE network URI is a bad call"); |
| 3485 | ret = LTTNG_ERR_SESSION_FAIL; |
| 3486 | free(uris); |
| 3487 | goto error; |
| 3488 | } |
| 3489 | } |
| 3490 | |
| 3491 | ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris, |
| 3492 | nb_uri, &cmd_ctx->creds); |
| 3493 | free(uris); |
| 3494 | break; |
| 3495 | } |
| 3496 | case LTTNG_CREATE_SESSION_LIVE: |
| 3497 | { |
| 3498 | size_t nb_uri, len; |
| 3499 | struct lttng_uri *uris = NULL; |
| 3500 | |
| 3501 | nb_uri = cmd_ctx->lsm->u.uri.size; |
| 3502 | len = nb_uri * sizeof(struct lttng_uri); |
| 3503 | |
| 3504 | if (nb_uri > 0) { |
| 3505 | uris = zmalloc(len); |
| 3506 | if (uris == NULL) { |
| 3507 | ret = LTTNG_ERR_FATAL; |
| 3508 | goto error; |
| 3509 | } |
| 3510 | |
| 3511 | /* Receive variable len data */ |
| 3512 | DBG("Waiting for %zu URIs from client ...", nb_uri); |
| 3513 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
| 3514 | if (ret <= 0) { |
| 3515 | DBG("No URIs received from client... continuing"); |
| 3516 | *sock_error = 1; |
| 3517 | ret = LTTNG_ERR_SESSION_FAIL; |
| 3518 | free(uris); |
| 3519 | goto error; |
| 3520 | } |
| 3521 | |
| 3522 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { |
| 3523 | DBG("Creating session with ONE network URI is a bad call"); |
| 3524 | ret = LTTNG_ERR_SESSION_FAIL; |
| 3525 | free(uris); |
| 3526 | goto error; |
| 3527 | } |
| 3528 | } |
| 3529 | |
| 3530 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, |
| 3531 | nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval); |
| 3532 | free(uris); |
| 3533 | break; |
| 3534 | } |
| 3535 | default: |
| 3536 | ret = LTTNG_ERR_UND; |
| 3537 | break; |
| 3538 | } |
| 3539 | |
| 3540 | error: |
| 3541 | if (cmd_ctx->llm == NULL) { |
| 3542 | DBG("Missing llm structure. Allocating one."); |
| 3543 | if (setup_lttng_msg(cmd_ctx, 0) < 0) { |
| 3544 | goto setup_error; |
| 3545 | } |
| 3546 | } |
| 3547 | /* Set return code */ |
| 3548 | cmd_ctx->llm->ret_code = ret; |
| 3549 | setup_error: |
| 3550 | if (cmd_ctx->session) { |
| 3551 | session_unlock(cmd_ctx->session); |
| 3552 | } |
| 3553 | if (need_tracing_session) { |
| 3554 | session_unlock_list(); |
| 3555 | } |
| 3556 | init_setup_error: |
| 3557 | return ret; |
| 3558 | } |
| 3559 | |
| 3560 | /* |
| 3561 | * Thread managing health check socket. |
| 3562 | */ |
| 3563 | static void *thread_manage_health(void *data) |
| 3564 | { |
| 3565 | int sock = -1, new_sock = -1, ret, i, pollfd, err = -1; |
| 3566 | uint32_t revents, nb_fd; |
| 3567 | struct lttng_poll_event events; |
| 3568 | struct health_comm_msg msg; |
| 3569 | struct health_comm_reply reply; |
| 3570 | |
| 3571 | DBG("[thread] Manage health check started"); |
| 3572 | |
| 3573 | rcu_register_thread(); |
| 3574 | |
| 3575 | /* We might hit an error path before this is created. */ |
| 3576 | lttng_poll_init(&events); |
| 3577 | |
| 3578 | /* Create unix socket */ |
| 3579 | sock = lttcomm_create_unix_sock(health_unix_sock_path); |
| 3580 | if (sock < 0) { |
| 3581 | ERR("Unable to create health check Unix socket"); |
| 3582 | ret = -1; |
| 3583 | goto error; |
| 3584 | } |
| 3585 | |
| 3586 | if (is_root) { |
| 3587 | /* lttng health client socket path permissions */ |
| 3588 | ret = chown(health_unix_sock_path, 0, |
| 3589 | utils_get_group_id(tracing_group_name)); |
| 3590 | if (ret < 0) { |
| 3591 | ERR("Unable to set group on %s", health_unix_sock_path); |
| 3592 | PERROR("chown"); |
| 3593 | ret = -1; |
| 3594 | goto error; |
| 3595 | } |
| 3596 | |
| 3597 | ret = chmod(health_unix_sock_path, |
| 3598 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 3599 | if (ret < 0) { |
| 3600 | ERR("Unable to set permissions on %s", health_unix_sock_path); |
| 3601 | PERROR("chmod"); |
| 3602 | ret = -1; |
| 3603 | goto error; |
| 3604 | } |
| 3605 | } |
| 3606 | |
| 3607 | /* |
| 3608 | * Set the CLOEXEC flag. Return code is useless because either way, the |
| 3609 | * show must go on. |
| 3610 | */ |
| 3611 | (void) utils_set_fd_cloexec(sock); |
| 3612 | |
| 3613 | ret = lttcomm_listen_unix_sock(sock); |
| 3614 | if (ret < 0) { |
| 3615 | goto error; |
| 3616 | } |
| 3617 | |
| 3618 | /* |
| 3619 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing |
| 3620 | * more will be added to this poll set. |
| 3621 | */ |
| 3622 | ret = sessiond_set_thread_pollset(&events, 2); |
| 3623 | if (ret < 0) { |
| 3624 | goto error; |
| 3625 | } |
| 3626 | |
| 3627 | /* Add the application registration socket */ |
| 3628 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI); |
| 3629 | if (ret < 0) { |
| 3630 | goto error; |
| 3631 | } |
| 3632 | |
| 3633 | while (1) { |
| 3634 | DBG("Health check ready"); |
| 3635 | |
| 3636 | /* Inifinite blocking call, waiting for transmission */ |
| 3637 | restart: |
| 3638 | ret = lttng_poll_wait(&events, -1); |
| 3639 | if (ret < 0) { |
| 3640 | /* |
| 3641 | * Restart interrupted system call. |
| 3642 | */ |
| 3643 | if (errno == EINTR) { |
| 3644 | goto restart; |
| 3645 | } |
| 3646 | goto error; |
| 3647 | } |
| 3648 | |
| 3649 | nb_fd = ret; |
| 3650 | |
| 3651 | for (i = 0; i < nb_fd; i++) { |
| 3652 | /* Fetch once the poll data */ |
| 3653 | revents = LTTNG_POLL_GETEV(&events, i); |
| 3654 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 3655 | |
| 3656 | /* Thread quit pipe has been closed. Killing thread. */ |
| 3657 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
| 3658 | if (ret) { |
| 3659 | err = 0; |
| 3660 | goto exit; |
| 3661 | } |
| 3662 | |
| 3663 | /* Event on the registration socket */ |
| 3664 | if (pollfd == sock) { |
| 3665 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 3666 | ERR("Health socket poll error"); |
| 3667 | goto error; |
| 3668 | } |
| 3669 | } |
| 3670 | } |
| 3671 | |
| 3672 | new_sock = lttcomm_accept_unix_sock(sock); |
| 3673 | if (new_sock < 0) { |
| 3674 | goto error; |
| 3675 | } |
| 3676 | |
| 3677 | /* |
| 3678 | * Set the CLOEXEC flag. Return code is useless because either way, the |
| 3679 | * show must go on. |
| 3680 | */ |
| 3681 | (void) utils_set_fd_cloexec(new_sock); |
| 3682 | |
| 3683 | DBG("Receiving data from client for health..."); |
| 3684 | ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg)); |
| 3685 | if (ret <= 0) { |
| 3686 | DBG("Nothing recv() from client... continuing"); |
| 3687 | ret = close(new_sock); |
| 3688 | if (ret) { |
| 3689 | PERROR("close"); |
| 3690 | } |
| 3691 | new_sock = -1; |
| 3692 | continue; |
| 3693 | } |
| 3694 | |
| 3695 | rcu_thread_online(); |
| 3696 | |
| 3697 | reply.ret_code = 0; |
| 3698 | for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) { |
| 3699 | /* |
| 3700 | * health_check_state returns 0 if health is |
| 3701 | * bad. |
| 3702 | */ |
| 3703 | if (!health_check_state(health_sessiond, i)) { |
| 3704 | reply.ret_code |= 1ULL << i; |
| 3705 | } |
| 3706 | } |
| 3707 | |
| 3708 | DBG2("Health check return value %" PRIx64, reply.ret_code); |
| 3709 | |
| 3710 | ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply)); |
| 3711 | if (ret < 0) { |
| 3712 | ERR("Failed to send health data back to client"); |
| 3713 | } |
| 3714 | |
| 3715 | /* End of transmission */ |
| 3716 | ret = close(new_sock); |
| 3717 | if (ret) { |
| 3718 | PERROR("close"); |
| 3719 | } |
| 3720 | new_sock = -1; |
| 3721 | } |
| 3722 | |
| 3723 | exit: |
| 3724 | error: |
| 3725 | if (err) { |
| 3726 | ERR("Health error occurred in %s", __func__); |
| 3727 | } |
| 3728 | DBG("Health check thread dying"); |
| 3729 | unlink(health_unix_sock_path); |
| 3730 | if (sock >= 0) { |
| 3731 | ret = close(sock); |
| 3732 | if (ret) { |
| 3733 | PERROR("close"); |
| 3734 | } |
| 3735 | } |
| 3736 | |
| 3737 | lttng_poll_clean(&events); |
| 3738 | |
| 3739 | rcu_unregister_thread(); |
| 3740 | return NULL; |
| 3741 | } |
| 3742 | |
| 3743 | /* |
| 3744 | * This thread manage all clients request using the unix client socket for |
| 3745 | * communication. |
| 3746 | */ |
| 3747 | static void *thread_manage_clients(void *data) |
| 3748 | { |
| 3749 | int sock = -1, ret, i, pollfd, err = -1; |
| 3750 | int sock_error; |
| 3751 | uint32_t revents, nb_fd; |
| 3752 | struct command_ctx *cmd_ctx = NULL; |
| 3753 | struct lttng_poll_event events; |
| 3754 | |
| 3755 | DBG("[thread] Manage client started"); |
| 3756 | |
| 3757 | rcu_register_thread(); |
| 3758 | |
| 3759 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD); |
| 3760 | |
| 3761 | health_code_update(); |
| 3762 | |
| 3763 | ret = lttcomm_listen_unix_sock(client_sock); |
| 3764 | if (ret < 0) { |
| 3765 | goto error_listen; |
| 3766 | } |
| 3767 | |
| 3768 | /* |
| 3769 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing |
| 3770 | * more will be added to this poll set. |
| 3771 | */ |
| 3772 | ret = sessiond_set_thread_pollset(&events, 2); |
| 3773 | if (ret < 0) { |
| 3774 | goto error_create_poll; |
| 3775 | } |
| 3776 | |
| 3777 | /* Add the application registration socket */ |
| 3778 | ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI); |
| 3779 | if (ret < 0) { |
| 3780 | goto error; |
| 3781 | } |
| 3782 | |
| 3783 | /* |
| 3784 | * Notify parent pid that we are ready to accept command for client side. |
| 3785 | * This ppid is the one from the external process that spawned us. |
| 3786 | */ |
| 3787 | if (opt_sig_parent) { |
| 3788 | kill(ppid, SIGUSR1); |
| 3789 | } |
| 3790 | |
| 3791 | /* Notify the parent of the fork() process that we are ready. */ |
| 3792 | if (opt_daemon) { |
| 3793 | kill(child_ppid, SIGUSR1); |
| 3794 | } |
| 3795 | |
| 3796 | /* This testpoint is after we signal readiness to the parent. */ |
| 3797 | if (testpoint(sessiond_thread_manage_clients)) { |
| 3798 | goto error; |
| 3799 | } |
| 3800 | |
| 3801 | if (testpoint(sessiond_thread_manage_clients_before_loop)) { |
| 3802 | goto error; |
| 3803 | } |
| 3804 | |
| 3805 | health_code_update(); |
| 3806 | |
| 3807 | while (1) { |
| 3808 | DBG("Accepting client command ..."); |
| 3809 | |
| 3810 | /* Inifinite blocking call, waiting for transmission */ |
| 3811 | restart: |
| 3812 | health_poll_entry(); |
| 3813 | ret = lttng_poll_wait(&events, -1); |
| 3814 | health_poll_exit(); |
| 3815 | if (ret < 0) { |
| 3816 | /* |
| 3817 | * Restart interrupted system call. |
| 3818 | */ |
| 3819 | if (errno == EINTR) { |
| 3820 | goto restart; |
| 3821 | } |
| 3822 | goto error; |
| 3823 | } |
| 3824 | |
| 3825 | nb_fd = ret; |
| 3826 | |
| 3827 | for (i = 0; i < nb_fd; i++) { |
| 3828 | /* Fetch once the poll data */ |
| 3829 | revents = LTTNG_POLL_GETEV(&events, i); |
| 3830 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 3831 | |
| 3832 | health_code_update(); |
| 3833 | |
| 3834 | /* Thread quit pipe has been closed. Killing thread. */ |
| 3835 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
| 3836 | if (ret) { |
| 3837 | err = 0; |
| 3838 | goto exit; |
| 3839 | } |
| 3840 | |
| 3841 | /* Event on the registration socket */ |
| 3842 | if (pollfd == client_sock) { |
| 3843 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 3844 | ERR("Client socket poll error"); |
| 3845 | goto error; |
| 3846 | } |
| 3847 | } |
| 3848 | } |
| 3849 | |
| 3850 | DBG("Wait for client response"); |
| 3851 | |
| 3852 | health_code_update(); |
| 3853 | |
| 3854 | sock = lttcomm_accept_unix_sock(client_sock); |
| 3855 | if (sock < 0) { |
| 3856 | goto error; |
| 3857 | } |
| 3858 | |
| 3859 | /* |
| 3860 | * Set the CLOEXEC flag. Return code is useless because either way, the |
| 3861 | * show must go on. |
| 3862 | */ |
| 3863 | (void) utils_set_fd_cloexec(sock); |
| 3864 | |
| 3865 | /* Set socket option for credentials retrieval */ |
| 3866 | ret = lttcomm_setsockopt_creds_unix_sock(sock); |
| 3867 | if (ret < 0) { |
| 3868 | goto error; |
| 3869 | } |
| 3870 | |
| 3871 | /* Allocate context command to process the client request */ |
| 3872 | cmd_ctx = zmalloc(sizeof(struct command_ctx)); |
| 3873 | if (cmd_ctx == NULL) { |
| 3874 | PERROR("zmalloc cmd_ctx"); |
| 3875 | goto error; |
| 3876 | } |
| 3877 | |
| 3878 | /* Allocate data buffer for reception */ |
| 3879 | cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg)); |
| 3880 | if (cmd_ctx->lsm == NULL) { |
| 3881 | PERROR("zmalloc cmd_ctx->lsm"); |
| 3882 | goto error; |
| 3883 | } |
| 3884 | |
| 3885 | cmd_ctx->llm = NULL; |
| 3886 | cmd_ctx->session = NULL; |
| 3887 | |
| 3888 | health_code_update(); |
| 3889 | |
| 3890 | /* |
| 3891 | * Data is received from the lttng client. The struct |
| 3892 | * lttcomm_session_msg (lsm) contains the command and data request of |
| 3893 | * the client. |
| 3894 | */ |
| 3895 | DBG("Receiving data from client ..."); |
| 3896 | ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm, |
| 3897 | sizeof(struct lttcomm_session_msg), &cmd_ctx->creds); |
| 3898 | if (ret <= 0) { |
| 3899 | DBG("Nothing recv() from client... continuing"); |
| 3900 | ret = close(sock); |
| 3901 | if (ret) { |
| 3902 | PERROR("close"); |
| 3903 | } |
| 3904 | sock = -1; |
| 3905 | clean_command_ctx(&cmd_ctx); |
| 3906 | continue; |
| 3907 | } |
| 3908 | |
| 3909 | health_code_update(); |
| 3910 | |
| 3911 | // TODO: Validate cmd_ctx including sanity check for |
| 3912 | // security purpose. |
| 3913 | |
| 3914 | rcu_thread_online(); |
| 3915 | /* |
| 3916 | * This function dispatch the work to the kernel or userspace tracer |
| 3917 | * libs and fill the lttcomm_lttng_msg data structure of all the needed |
| 3918 | * informations for the client. The command context struct contains |
| 3919 | * everything this function may needs. |
| 3920 | */ |
| 3921 | ret = process_client_msg(cmd_ctx, sock, &sock_error); |
| 3922 | rcu_thread_offline(); |
| 3923 | if (ret < 0) { |
| 3924 | ret = close(sock); |
| 3925 | if (ret) { |
| 3926 | PERROR("close"); |
| 3927 | } |
| 3928 | sock = -1; |
| 3929 | /* |
| 3930 | * TODO: Inform client somehow of the fatal error. At |
| 3931 | * this point, ret < 0 means that a zmalloc failed |
| 3932 | * (ENOMEM). Error detected but still accept |
| 3933 | * command, unless a socket error has been |
| 3934 | * detected. |
| 3935 | */ |
| 3936 | clean_command_ctx(&cmd_ctx); |
| 3937 | continue; |
| 3938 | } |
| 3939 | |
| 3940 | health_code_update(); |
| 3941 | |
| 3942 | DBG("Sending response (size: %d, retcode: %s)", |
| 3943 | cmd_ctx->lttng_msg_size, |
| 3944 | lttng_strerror(-cmd_ctx->llm->ret_code)); |
| 3945 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); |
| 3946 | if (ret < 0) { |
| 3947 | ERR("Failed to send data back to client"); |
| 3948 | } |
| 3949 | |
| 3950 | /* End of transmission */ |
| 3951 | ret = close(sock); |
| 3952 | if (ret) { |
| 3953 | PERROR("close"); |
| 3954 | } |
| 3955 | sock = -1; |
| 3956 | |
| 3957 | clean_command_ctx(&cmd_ctx); |
| 3958 | |
| 3959 | health_code_update(); |
| 3960 | } |
| 3961 | |
| 3962 | exit: |
| 3963 | error: |
| 3964 | if (sock >= 0) { |
| 3965 | ret = close(sock); |
| 3966 | if (ret) { |
| 3967 | PERROR("close"); |
| 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | lttng_poll_clean(&events); |
| 3972 | clean_command_ctx(&cmd_ctx); |
| 3973 | |
| 3974 | error_listen: |
| 3975 | error_create_poll: |
| 3976 | unlink(client_unix_sock_path); |
| 3977 | if (client_sock >= 0) { |
| 3978 | ret = close(client_sock); |
| 3979 | if (ret) { |
| 3980 | PERROR("close"); |
| 3981 | } |
| 3982 | } |
| 3983 | |
| 3984 | if (err) { |
| 3985 | health_error(); |
| 3986 | ERR("Health error occurred in %s", __func__); |
| 3987 | } |
| 3988 | |
| 3989 | health_unregister(health_sessiond); |
| 3990 | |
| 3991 | DBG("Client thread dying"); |
| 3992 | |
| 3993 | rcu_unregister_thread(); |
| 3994 | return NULL; |
| 3995 | } |
| 3996 | |
| 3997 | |
| 3998 | /* |
| 3999 | * usage function on stderr |
| 4000 | */ |
| 4001 | static void usage(void) |
| 4002 | { |
| 4003 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
| 4004 | fprintf(stderr, " -h, --help Display this usage.\n"); |
| 4005 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); |
| 4006 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n"); |
| 4007 | fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n"); |
| 4008 | fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n"); |
| 4009 | fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n"); |
| 4010 | fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n"); |
| 4011 | fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n"); |
| 4012 | fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n"); |
| 4013 | fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n"); |
| 4014 | fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n"); |
| 4015 | fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n"); |
| 4016 | fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n"); |
| 4017 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); |
| 4018 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); |
| 4019 | fprintf(stderr, " -V, --version Show version number.\n"); |
| 4020 | fprintf(stderr, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n"); |
| 4021 | fprintf(stderr, " -q, --quiet No output at all.\n"); |
| 4022 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); |
| 4023 | fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n"); |
| 4024 | fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n"); |
| 4025 | fprintf(stderr, " --no-kernel Disable kernel tracer\n"); |
| 4026 | fprintf(stderr, " --jul-tcp-port JUL application registration TCP port\n"); |
| 4027 | fprintf(stderr, " -f --config Load daemon configuration file\n"); |
| 4028 | } |
| 4029 | |
| 4030 | /* |
| 4031 | * Take an option from the getopt output and set it in the right variable to be |
| 4032 | * used later. |
| 4033 | * |
| 4034 | * Return 0 on success else a negative value. |
| 4035 | */ |
| 4036 | static int set_option(int opt, const char *arg, const char *optname) |
| 4037 | { |
| 4038 | int ret = 0; |
| 4039 | |
| 4040 | switch (opt) { |
| 4041 | case 0: |
| 4042 | fprintf(stderr, "option %s", optname); |
| 4043 | if (arg) { |
| 4044 | fprintf(stderr, " with arg %s\n", arg); |
| 4045 | } |
| 4046 | break; |
| 4047 | case 'c': |
| 4048 | snprintf(client_unix_sock_path, PATH_MAX, "%s", arg); |
| 4049 | break; |
| 4050 | case 'a': |
| 4051 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", arg); |
| 4052 | break; |
| 4053 | case 'd': |
| 4054 | opt_daemon = 1; |
| 4055 | break; |
| 4056 | case 'g': |
| 4057 | tracing_group_name = strdup(arg); |
| 4058 | break; |
| 4059 | case 'h': |
| 4060 | usage(); |
| 4061 | exit(EXIT_FAILURE); |
| 4062 | case 'V': |
| 4063 | fprintf(stdout, "%s\n", VERSION); |
| 4064 | exit(EXIT_SUCCESS); |
| 4065 | case 'S': |
| 4066 | opt_sig_parent = 1; |
| 4067 | break; |
| 4068 | case 'E': |
| 4069 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", arg); |
| 4070 | break; |
| 4071 | case 'C': |
| 4072 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", arg); |
| 4073 | break; |
| 4074 | case 'F': |
| 4075 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", arg); |
| 4076 | break; |
| 4077 | case 'D': |
| 4078 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", arg); |
| 4079 | break; |
| 4080 | case 'H': |
| 4081 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", arg); |
| 4082 | break; |
| 4083 | case 'G': |
| 4084 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", arg); |
| 4085 | break; |
| 4086 | case 'N': |
| 4087 | opt_no_kernel = 1; |
| 4088 | break; |
| 4089 | case 'q': |
| 4090 | lttng_opt_quiet = 1; |
| 4091 | break; |
| 4092 | case 'v': |
| 4093 | /* Verbose level can increase using multiple -v */ |
| 4094 | if (arg) { |
| 4095 | lttng_opt_verbose = config_parse_value(arg); |
| 4096 | } else { |
| 4097 | lttng_opt_verbose += 1; |
| 4098 | } |
| 4099 | break; |
| 4100 | case 'Z': |
| 4101 | if (arg) { |
| 4102 | opt_verbose_consumer = config_parse_value(arg); |
| 4103 | } else { |
| 4104 | opt_verbose_consumer += 1; |
| 4105 | } |
| 4106 | break; |
| 4107 | case 'u': |
| 4108 | consumerd32_bin = strdup(arg); |
| 4109 | consumerd32_bin_override = 1; |
| 4110 | break; |
| 4111 | case 'U': |
| 4112 | consumerd32_libdir = strdup(arg); |
| 4113 | consumerd32_libdir_override = 1; |
| 4114 | break; |
| 4115 | case 't': |
| 4116 | consumerd64_bin = strdup(arg); |
| 4117 | consumerd64_bin_override = 1; |
| 4118 | break; |
| 4119 | case 'T': |
| 4120 | consumerd64_libdir = strdup(arg); |
| 4121 | consumerd64_libdir_override = 1; |
| 4122 | break; |
| 4123 | case 'p': |
| 4124 | opt_pidfile = strdup(arg); |
| 4125 | break; |
| 4126 | case 'J': /* JUL TCP port. */ |
| 4127 | { |
| 4128 | unsigned long v; |
| 4129 | |
| 4130 | errno = 0; |
| 4131 | v = strtoul(arg, NULL, 0); |
| 4132 | if (errno != 0 || !isdigit(arg[0])) { |
| 4133 | ERR("Wrong value in --jul-tcp-port parameter: %s", arg); |
| 4134 | return -1; |
| 4135 | } |
| 4136 | if (v == 0 || v >= 65535) { |
| 4137 | ERR("Port overflow in --jul-tcp-port parameter: %s", arg); |
| 4138 | return -1; |
| 4139 | } |
| 4140 | jul_tcp_port = (uint32_t) v; |
| 4141 | DBG3("JUL TCP port set to non default: %u", jul_tcp_port); |
| 4142 | break; |
| 4143 | } |
| 4144 | default: |
| 4145 | /* Unknown option or other error. |
| 4146 | * Error is printed by getopt, just return */ |
| 4147 | ret = -1; |
| 4148 | } |
| 4149 | |
| 4150 | return ret; |
| 4151 | } |
| 4152 | |
| 4153 | /* |
| 4154 | * config_entry_handler_cb used to handle options read from a config file. |
| 4155 | * See config_entry_handler_cb comment in common/config/config.h for the |
| 4156 | * return value conventions. |
| 4157 | */ |
| 4158 | static int config_entry_handler(const struct config_entry *entry, void *unused) |
| 4159 | { |
| 4160 | int ret = 0, i; |
| 4161 | |
| 4162 | if (!entry || !entry->name || !entry->value) { |
| 4163 | ret = -EINVAL; |
| 4164 | goto end; |
| 4165 | } |
| 4166 | |
| 4167 | /* Check if the option is to be ignored */ |
| 4168 | for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) { |
| 4169 | if (!strcmp(entry->name, config_ignore_options[i])) { |
| 4170 | goto end; |
| 4171 | } |
| 4172 | } |
| 4173 | |
| 4174 | for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; |
| 4175 | i++) { |
| 4176 | |
| 4177 | /* Ignore if not fully matched. */ |
| 4178 | if (strcmp(entry->name, long_options[i].name)) { |
| 4179 | continue; |
| 4180 | } |
| 4181 | |
| 4182 | /* |
| 4183 | * If the option takes no argument on the command line, we have to |
| 4184 | * check if the value is "true". We support non-zero numeric values, |
| 4185 | * true, on and yes. |
| 4186 | */ |
| 4187 | if (!long_options[i].has_arg) { |
| 4188 | ret = config_parse_value(entry->value); |
| 4189 | if (ret <= 0) { |
| 4190 | if (ret) { |
| 4191 | WARN("Invalid configuration value \"%s\" for option %s", |
| 4192 | entry->value, entry->name); |
| 4193 | } |
| 4194 | /* False, skip boolean config option. */ |
| 4195 | goto end; |
| 4196 | } |
| 4197 | } |
| 4198 | |
| 4199 | ret = set_option(long_options[i].val, entry->value, entry->name); |
| 4200 | goto end; |
| 4201 | } |
| 4202 | |
| 4203 | WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name); |
| 4204 | |
| 4205 | end: |
| 4206 | return ret; |
| 4207 | } |
| 4208 | |
| 4209 | /* |
| 4210 | * daemon configuration loading and argument parsing |
| 4211 | */ |
| 4212 | static int set_options(int argc, char **argv) |
| 4213 | { |
| 4214 | int ret = 0, c = 0, option_index = 0; |
| 4215 | int orig_optopt = optopt, orig_optind = optind; |
| 4216 | char *optstring; |
| 4217 | const char *config_path = NULL; |
| 4218 | |
| 4219 | optstring = utils_generate_optstring(long_options, |
| 4220 | sizeof(long_options) / sizeof(struct option)); |
| 4221 | if (!optstring) { |
| 4222 | ret = -ENOMEM; |
| 4223 | goto end; |
| 4224 | } |
| 4225 | |
| 4226 | /* Check for the --config option */ |
| 4227 | while ((c = getopt_long(argc, argv, optstring, long_options, |
| 4228 | &option_index)) != -1) { |
| 4229 | if (c == '?') { |
| 4230 | ret = -EINVAL; |
| 4231 | goto end; |
| 4232 | } else if (c != 'f') { |
| 4233 | /* if not equal to --config option. */ |
| 4234 | continue; |
| 4235 | } |
| 4236 | |
| 4237 | config_path = utils_expand_path(optarg); |
| 4238 | if (!config_path) { |
| 4239 | ERR("Failed to resolve path: %s", optarg); |
| 4240 | } |
| 4241 | } |
| 4242 | |
| 4243 | ret = config_get_section_entries(config_path, config_section_name, |
| 4244 | config_entry_handler, NULL); |
| 4245 | if (ret) { |
| 4246 | if (ret > 0) { |
| 4247 | ERR("Invalid configuration option at line %i", ret); |
| 4248 | ret = -1; |
| 4249 | } |
| 4250 | goto end; |
| 4251 | } |
| 4252 | |
| 4253 | /* Reset getopt's global state */ |
| 4254 | optopt = orig_optopt; |
| 4255 | optind = orig_optind; |
| 4256 | while (1) { |
| 4257 | c = getopt_long(argc, argv, optstring, long_options, &option_index); |
| 4258 | if (c == -1) { |
| 4259 | break; |
| 4260 | } |
| 4261 | |
| 4262 | ret = set_option(c, optarg, long_options[option_index].name); |
| 4263 | if (ret < 0) { |
| 4264 | break; |
| 4265 | } |
| 4266 | } |
| 4267 | |
| 4268 | end: |
| 4269 | free(optstring); |
| 4270 | return ret; |
| 4271 | } |
| 4272 | |
| 4273 | /* |
| 4274 | * Creates the two needed socket by the daemon. |
| 4275 | * apps_sock - The communication socket for all UST apps. |
| 4276 | * client_sock - The communication of the cli tool (lttng). |
| 4277 | */ |
| 4278 | static int init_daemon_socket(void) |
| 4279 | { |
| 4280 | int ret = 0; |
| 4281 | mode_t old_umask; |
| 4282 | |
| 4283 | old_umask = umask(0); |
| 4284 | |
| 4285 | /* Create client tool unix socket */ |
| 4286 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
| 4287 | if (client_sock < 0) { |
| 4288 | ERR("Create unix sock failed: %s", client_unix_sock_path); |
| 4289 | ret = -1; |
| 4290 | goto end; |
| 4291 | } |
| 4292 | |
| 4293 | /* Set the cloexec flag */ |
| 4294 | ret = utils_set_fd_cloexec(client_sock); |
| 4295 | if (ret < 0) { |
| 4296 | ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). " |
| 4297 | "Continuing but note that the consumer daemon will have a " |
| 4298 | "reference to this socket on exec()", client_sock); |
| 4299 | } |
| 4300 | |
| 4301 | /* File permission MUST be 660 */ |
| 4302 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 4303 | if (ret < 0) { |
| 4304 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
| 4305 | PERROR("chmod"); |
| 4306 | goto end; |
| 4307 | } |
| 4308 | |
| 4309 | /* Create the application unix socket */ |
| 4310 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
| 4311 | if (apps_sock < 0) { |
| 4312 | ERR("Create unix sock failed: %s", apps_unix_sock_path); |
| 4313 | ret = -1; |
| 4314 | goto end; |
| 4315 | } |
| 4316 | |
| 4317 | /* Set the cloexec flag */ |
| 4318 | ret = utils_set_fd_cloexec(apps_sock); |
| 4319 | if (ret < 0) { |
| 4320 | ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). " |
| 4321 | "Continuing but note that the consumer daemon will have a " |
| 4322 | "reference to this socket on exec()", apps_sock); |
| 4323 | } |
| 4324 | |
| 4325 | /* File permission MUST be 666 */ |
| 4326 | ret = chmod(apps_unix_sock_path, |
| 4327 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 4328 | if (ret < 0) { |
| 4329 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
| 4330 | PERROR("chmod"); |
| 4331 | goto end; |
| 4332 | } |
| 4333 | |
| 4334 | DBG3("Session daemon client socket %d and application socket %d created", |
| 4335 | client_sock, apps_sock); |
| 4336 | |
| 4337 | end: |
| 4338 | umask(old_umask); |
| 4339 | return ret; |
| 4340 | } |
| 4341 | |
| 4342 | /* |
| 4343 | * Check if the global socket is available, and if a daemon is answering at the |
| 4344 | * other side. If yes, error is returned. |
| 4345 | */ |
| 4346 | static int check_existing_daemon(void) |
| 4347 | { |
| 4348 | /* Is there anybody out there ? */ |
| 4349 | if (lttng_session_daemon_alive()) { |
| 4350 | return -EEXIST; |
| 4351 | } |
| 4352 | |
| 4353 | return 0; |
| 4354 | } |
| 4355 | |
| 4356 | /* |
| 4357 | * Set the tracing group gid onto the client socket. |
| 4358 | * |
| 4359 | * Race window between mkdir and chown is OK because we are going from more |
| 4360 | * permissive (root.root) to less permissive (root.tracing). |
| 4361 | */ |
| 4362 | static int set_permissions(char *rundir) |
| 4363 | { |
| 4364 | int ret; |
| 4365 | gid_t gid; |
| 4366 | |
| 4367 | gid = utils_get_group_id(tracing_group_name); |
| 4368 | |
| 4369 | /* Set lttng run dir */ |
| 4370 | ret = chown(rundir, 0, gid); |
| 4371 | if (ret < 0) { |
| 4372 | ERR("Unable to set group on %s", rundir); |
| 4373 | PERROR("chown"); |
| 4374 | } |
| 4375 | |
| 4376 | /* |
| 4377 | * Ensure all applications and tracing group can search the run |
| 4378 | * dir. Allow everyone to read the directory, since it does not |
| 4379 | * buy us anything to hide its content. |
| 4380 | */ |
| 4381 | ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
| 4382 | if (ret < 0) { |
| 4383 | ERR("Unable to set permissions on %s", rundir); |
| 4384 | PERROR("chmod"); |
| 4385 | } |
| 4386 | |
| 4387 | /* lttng client socket path */ |
| 4388 | ret = chown(client_unix_sock_path, 0, gid); |
| 4389 | if (ret < 0) { |
| 4390 | ERR("Unable to set group on %s", client_unix_sock_path); |
| 4391 | PERROR("chown"); |
| 4392 | } |
| 4393 | |
| 4394 | /* kconsumer error socket path */ |
| 4395 | ret = chown(kconsumer_data.err_unix_sock_path, 0, 0); |
| 4396 | if (ret < 0) { |
| 4397 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
| 4398 | PERROR("chown"); |
| 4399 | } |
| 4400 | |
| 4401 | /* 64-bit ustconsumer error socket path */ |
| 4402 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0); |
| 4403 | if (ret < 0) { |
| 4404 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); |
| 4405 | PERROR("chown"); |
| 4406 | } |
| 4407 | |
| 4408 | /* 32-bit ustconsumer compat32 error socket path */ |
| 4409 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0); |
| 4410 | if (ret < 0) { |
| 4411 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
| 4412 | PERROR("chown"); |
| 4413 | } |
| 4414 | |
| 4415 | DBG("All permissions are set"); |
| 4416 | |
| 4417 | return ret; |
| 4418 | } |
| 4419 | |
| 4420 | /* |
| 4421 | * Create the lttng run directory needed for all global sockets and pipe. |
| 4422 | */ |
| 4423 | static int create_lttng_rundir(const char *rundir) |
| 4424 | { |
| 4425 | int ret; |
| 4426 | |
| 4427 | DBG3("Creating LTTng run directory: %s", rundir); |
| 4428 | |
| 4429 | ret = mkdir(rundir, S_IRWXU); |
| 4430 | if (ret < 0) { |
| 4431 | if (errno != EEXIST) { |
| 4432 | ERR("Unable to create %s", rundir); |
| 4433 | goto error; |
| 4434 | } else { |
| 4435 | ret = 0; |
| 4436 | } |
| 4437 | } |
| 4438 | |
| 4439 | error: |
| 4440 | return ret; |
| 4441 | } |
| 4442 | |
| 4443 | /* |
| 4444 | * Setup sockets and directory needed by the kconsumerd communication with the |
| 4445 | * session daemon. |
| 4446 | */ |
| 4447 | static int set_consumer_sockets(struct consumer_data *consumer_data, |
| 4448 | const char *rundir) |
| 4449 | { |
| 4450 | int ret; |
| 4451 | char path[PATH_MAX]; |
| 4452 | |
| 4453 | switch (consumer_data->type) { |
| 4454 | case LTTNG_CONSUMER_KERNEL: |
| 4455 | snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir); |
| 4456 | break; |
| 4457 | case LTTNG_CONSUMER64_UST: |
| 4458 | snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir); |
| 4459 | break; |
| 4460 | case LTTNG_CONSUMER32_UST: |
| 4461 | snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir); |
| 4462 | break; |
| 4463 | default: |
| 4464 | ERR("Consumer type unknown"); |
| 4465 | ret = -EINVAL; |
| 4466 | goto error; |
| 4467 | } |
| 4468 | |
| 4469 | DBG2("Creating consumer directory: %s", path); |
| 4470 | |
| 4471 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP); |
| 4472 | if (ret < 0) { |
| 4473 | if (errno != EEXIST) { |
| 4474 | PERROR("mkdir"); |
| 4475 | ERR("Failed to create %s", path); |
| 4476 | goto error; |
| 4477 | } |
| 4478 | ret = -1; |
| 4479 | } |
| 4480 | if (is_root) { |
| 4481 | ret = chown(path, 0, utils_get_group_id(tracing_group_name)); |
| 4482 | if (ret < 0) { |
| 4483 | ERR("Unable to set group on %s", path); |
| 4484 | PERROR("chown"); |
| 4485 | goto error; |
| 4486 | } |
| 4487 | } |
| 4488 | |
| 4489 | /* Create the kconsumerd error unix socket */ |
| 4490 | consumer_data->err_sock = |
| 4491 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); |
| 4492 | if (consumer_data->err_sock < 0) { |
| 4493 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); |
| 4494 | ret = -1; |
| 4495 | goto error; |
| 4496 | } |
| 4497 | |
| 4498 | /* |
| 4499 | * Set the CLOEXEC flag. Return code is useless because either way, the |
| 4500 | * show must go on. |
| 4501 | */ |
| 4502 | ret = utils_set_fd_cloexec(consumer_data->err_sock); |
| 4503 | if (ret < 0) { |
| 4504 | PERROR("utils_set_fd_cloexec"); |
| 4505 | /* continue anyway */ |
| 4506 | } |
| 4507 | |
| 4508 | /* File permission MUST be 660 */ |
| 4509 | ret = chmod(consumer_data->err_unix_sock_path, |
| 4510 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 4511 | if (ret < 0) { |
| 4512 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
| 4513 | PERROR("chmod"); |
| 4514 | goto error; |
| 4515 | } |
| 4516 | |
| 4517 | error: |
| 4518 | return ret; |
| 4519 | } |
| 4520 | |
| 4521 | /* |
| 4522 | * Signal handler for the daemon |
| 4523 | * |
| 4524 | * Simply stop all worker threads, leaving main() return gracefully after |
| 4525 | * joining all threads and calling cleanup(). |
| 4526 | */ |
| 4527 | static void sighandler(int sig) |
| 4528 | { |
| 4529 | switch (sig) { |
| 4530 | case SIGPIPE: |
| 4531 | DBG("SIGPIPE caught"); |
| 4532 | return; |
| 4533 | case SIGINT: |
| 4534 | DBG("SIGINT caught"); |
| 4535 | stop_threads(); |
| 4536 | break; |
| 4537 | case SIGTERM: |
| 4538 | DBG("SIGTERM caught"); |
| 4539 | stop_threads(); |
| 4540 | break; |
| 4541 | case SIGUSR1: |
| 4542 | CMM_STORE_SHARED(recv_child_signal, 1); |
| 4543 | break; |
| 4544 | default: |
| 4545 | break; |
| 4546 | } |
| 4547 | } |
| 4548 | |
| 4549 | /* |
| 4550 | * Setup signal handler for : |
| 4551 | * SIGINT, SIGTERM, SIGPIPE |
| 4552 | */ |
| 4553 | static int set_signal_handler(void) |
| 4554 | { |
| 4555 | int ret = 0; |
| 4556 | struct sigaction sa; |
| 4557 | sigset_t sigset; |
| 4558 | |
| 4559 | if ((ret = sigemptyset(&sigset)) < 0) { |
| 4560 | PERROR("sigemptyset"); |
| 4561 | return ret; |
| 4562 | } |
| 4563 | |
| 4564 | sa.sa_handler = sighandler; |
| 4565 | sa.sa_mask = sigset; |
| 4566 | sa.sa_flags = 0; |
| 4567 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
| 4568 | PERROR("sigaction"); |
| 4569 | return ret; |
| 4570 | } |
| 4571 | |
| 4572 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
| 4573 | PERROR("sigaction"); |
| 4574 | return ret; |
| 4575 | } |
| 4576 | |
| 4577 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
| 4578 | PERROR("sigaction"); |
| 4579 | return ret; |
| 4580 | } |
| 4581 | |
| 4582 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
| 4583 | PERROR("sigaction"); |
| 4584 | return ret; |
| 4585 | } |
| 4586 | |
| 4587 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); |
| 4588 | |
| 4589 | return ret; |
| 4590 | } |
| 4591 | |
| 4592 | /* |
| 4593 | * Set open files limit to unlimited. This daemon can open a large number of |
| 4594 | * file descriptors in order to consumer multiple kernel traces. |
| 4595 | */ |
| 4596 | static void set_ulimit(void) |
| 4597 | { |
| 4598 | int ret; |
| 4599 | struct rlimit lim; |
| 4600 | |
| 4601 | /* The kernel does not allowed an infinite limit for open files */ |
| 4602 | lim.rlim_cur = 65535; |
| 4603 | lim.rlim_max = 65535; |
| 4604 | |
| 4605 | ret = setrlimit(RLIMIT_NOFILE, &lim); |
| 4606 | if (ret < 0) { |
| 4607 | PERROR("failed to set open files limit"); |
| 4608 | } |
| 4609 | } |
| 4610 | |
| 4611 | /* |
| 4612 | * Write pidfile using the rundir and opt_pidfile. |
| 4613 | */ |
| 4614 | static void write_pidfile(void) |
| 4615 | { |
| 4616 | int ret; |
| 4617 | char pidfile_path[PATH_MAX]; |
| 4618 | |
| 4619 | assert(rundir); |
| 4620 | |
| 4621 | if (opt_pidfile) { |
| 4622 | strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path)); |
| 4623 | } else { |
| 4624 | /* Build pidfile path from rundir and opt_pidfile. */ |
| 4625 | ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/" |
| 4626 | DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir); |
| 4627 | if (ret < 0) { |
| 4628 | PERROR("snprintf pidfile path"); |
| 4629 | goto error; |
| 4630 | } |
| 4631 | } |
| 4632 | |
| 4633 | /* |
| 4634 | * Create pid file in rundir. Return value is of no importance. The |
| 4635 | * execution will continue even though we are not able to write the file. |
| 4636 | */ |
| 4637 | (void) utils_create_pid_file(getpid(), pidfile_path); |
| 4638 | |
| 4639 | error: |
| 4640 | return; |
| 4641 | } |
| 4642 | |
| 4643 | /* |
| 4644 | * Write JUL TCP port using the rundir. |
| 4645 | */ |
| 4646 | static void write_julport(void) |
| 4647 | { |
| 4648 | int ret; |
| 4649 | char path[PATH_MAX]; |
| 4650 | |
| 4651 | assert(rundir); |
| 4652 | |
| 4653 | ret = snprintf(path, sizeof(path), "%s/" |
| 4654 | DEFAULT_LTTNG_SESSIOND_JULPORT_FILE, rundir); |
| 4655 | if (ret < 0) { |
| 4656 | PERROR("snprintf julport path"); |
| 4657 | goto error; |
| 4658 | } |
| 4659 | |
| 4660 | /* |
| 4661 | * Create TCP JUL port file in rundir. Return value is of no importance. |
| 4662 | * The execution will continue even though we are not able to write the |
| 4663 | * file. |
| 4664 | */ |
| 4665 | (void) utils_create_pid_file(jul_tcp_port, path); |
| 4666 | |
| 4667 | error: |
| 4668 | return; |
| 4669 | } |
| 4670 | |
| 4671 | /* |
| 4672 | * Daemonize this process by forking and making the parent wait for the child |
| 4673 | * to signal it indicating readiness. Once received, the parent successfully |
| 4674 | * quits. |
| 4675 | * |
| 4676 | * The child process undergoes the same action that daemon(3) does meaning |
| 4677 | * setsid, chdir, and dup /dev/null into 0, 1 and 2. |
| 4678 | * |
| 4679 | * Return 0 on success else -1 on error. |
| 4680 | */ |
| 4681 | static int daemonize(void) |
| 4682 | { |
| 4683 | int ret; |
| 4684 | pid_t pid; |
| 4685 | |
| 4686 | /* Get parent pid of this process. */ |
| 4687 | child_ppid = getppid(); |
| 4688 | |
| 4689 | pid = fork(); |
| 4690 | if (pid < 0) { |
| 4691 | PERROR("fork"); |
| 4692 | goto error; |
| 4693 | } else if (pid == 0) { |
| 4694 | int fd; |
| 4695 | pid_t sid; |
| 4696 | |
| 4697 | /* Child */ |
| 4698 | |
| 4699 | /* |
| 4700 | * Get the newly created parent pid so we can signal that process when |
| 4701 | * we are ready to operate. |
| 4702 | */ |
| 4703 | child_ppid = getppid(); |
| 4704 | |
| 4705 | sid = setsid(); |
| 4706 | if (sid < 0) { |
| 4707 | PERROR("setsid"); |
| 4708 | goto error; |
| 4709 | } |
| 4710 | |
| 4711 | /* Try to change directory to /. If we can't well at least notify. */ |
| 4712 | ret = chdir("/"); |
| 4713 | if (ret < 0) { |
| 4714 | PERROR("chdir"); |
| 4715 | } |
| 4716 | |
| 4717 | fd = open(_PATH_DEVNULL, O_RDWR, 0); |
| 4718 | if (fd < 0) { |
| 4719 | PERROR("open %s", _PATH_DEVNULL); |
| 4720 | /* Let 0, 1 and 2 open since we can't bind them to /dev/null. */ |
| 4721 | } else { |
| 4722 | (void) dup2(fd, STDIN_FILENO); |
| 4723 | (void) dup2(fd, STDOUT_FILENO); |
| 4724 | (void) dup2(fd, STDERR_FILENO); |
| 4725 | if (fd > 2) { |
| 4726 | ret = close(fd); |
| 4727 | if (ret < 0) { |
| 4728 | PERROR("close"); |
| 4729 | } |
| 4730 | } |
| 4731 | } |
| 4732 | goto end; |
| 4733 | } else { |
| 4734 | /* Parent */ |
| 4735 | |
| 4736 | /* |
| 4737 | * Waiting for child to notify this parent that it can exit. Note that |
| 4738 | * sleep() is interrupted before the 1 second delay as soon as the |
| 4739 | * signal is received, so it will not cause visible delay for the |
| 4740 | * user. |
| 4741 | */ |
| 4742 | while (!CMM_LOAD_SHARED(recv_child_signal)) { |
| 4743 | int status; |
| 4744 | pid_t ret; |
| 4745 | |
| 4746 | /* |
| 4747 | * Check if child exists without blocking. If so, we have to stop |
| 4748 | * this parent process and return an error. |
| 4749 | */ |
| 4750 | ret = waitpid(pid, &status, WNOHANG); |
| 4751 | if (ret < 0 || (ret != 0 && WIFEXITED(status))) { |
| 4752 | /* The child exited somehow or was not valid. */ |
| 4753 | goto error; |
| 4754 | } |
| 4755 | sleep(1); |
| 4756 | } |
| 4757 | |
| 4758 | /* |
| 4759 | * From this point on, the parent can exit and the child is now an |
| 4760 | * operationnal session daemon ready to serve clients and applications. |
| 4761 | */ |
| 4762 | exit(EXIT_SUCCESS); |
| 4763 | } |
| 4764 | |
| 4765 | end: |
| 4766 | return 0; |
| 4767 | |
| 4768 | error: |
| 4769 | return -1; |
| 4770 | } |
| 4771 | |
| 4772 | /* |
| 4773 | * main |
| 4774 | */ |
| 4775 | int main(int argc, char **argv) |
| 4776 | { |
| 4777 | int ret = 0; |
| 4778 | void *status; |
| 4779 | const char *home_path, *env_app_timeout; |
| 4780 | |
| 4781 | init_kernel_workarounds(); |
| 4782 | |
| 4783 | rcu_register_thread(); |
| 4784 | |
| 4785 | if ((ret = set_signal_handler()) < 0) { |
| 4786 | goto error; |
| 4787 | } |
| 4788 | |
| 4789 | setup_consumerd_path(); |
| 4790 | |
| 4791 | page_size = sysconf(_SC_PAGESIZE); |
| 4792 | if (page_size < 0) { |
| 4793 | PERROR("sysconf _SC_PAGESIZE"); |
| 4794 | page_size = LONG_MAX; |
| 4795 | WARN("Fallback page size to %ld", page_size); |
| 4796 | } |
| 4797 | |
| 4798 | /* Parse arguments and load the daemon configuration file */ |
| 4799 | progname = argv[0]; |
| 4800 | if ((ret = set_options(argc, argv)) < 0) { |
| 4801 | goto error; |
| 4802 | } |
| 4803 | |
| 4804 | /* Daemonize */ |
| 4805 | if (opt_daemon) { |
| 4806 | int i; |
| 4807 | |
| 4808 | ret = daemonize(); |
| 4809 | if (ret < 0) { |
| 4810 | goto error; |
| 4811 | } |
| 4812 | |
| 4813 | /* |
| 4814 | * We are in the child. Make sure all other file descriptors are |
| 4815 | * closed, in case we are called with more opened file descriptors than |
| 4816 | * the standard ones. |
| 4817 | */ |
| 4818 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { |
| 4819 | (void) close(i); |
| 4820 | } |
| 4821 | } |
| 4822 | |
| 4823 | /* Create thread quit pipe */ |
| 4824 | if ((ret = init_thread_quit_pipe()) < 0) { |
| 4825 | goto error; |
| 4826 | } |
| 4827 | |
| 4828 | /* Check if daemon is UID = 0 */ |
| 4829 | is_root = !getuid(); |
| 4830 | |
| 4831 | if (is_root) { |
| 4832 | rundir = strdup(DEFAULT_LTTNG_RUNDIR); |
| 4833 | |
| 4834 | /* Create global run dir with root access */ |
| 4835 | ret = create_lttng_rundir(rundir); |
| 4836 | if (ret < 0) { |
| 4837 | goto error; |
| 4838 | } |
| 4839 | |
| 4840 | if (strlen(apps_unix_sock_path) == 0) { |
| 4841 | snprintf(apps_unix_sock_path, PATH_MAX, |
| 4842 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); |
| 4843 | } |
| 4844 | |
| 4845 | if (strlen(client_unix_sock_path) == 0) { |
| 4846 | snprintf(client_unix_sock_path, PATH_MAX, |
| 4847 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); |
| 4848 | } |
| 4849 | |
| 4850 | /* Set global SHM for ust */ |
| 4851 | if (strlen(wait_shm_path) == 0) { |
| 4852 | snprintf(wait_shm_path, PATH_MAX, |
| 4853 | DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH); |
| 4854 | } |
| 4855 | |
| 4856 | if (strlen(health_unix_sock_path) == 0) { |
| 4857 | snprintf(health_unix_sock_path, sizeof(health_unix_sock_path), |
| 4858 | DEFAULT_GLOBAL_HEALTH_UNIX_SOCK); |
| 4859 | } |
| 4860 | |
| 4861 | /* Setup kernel consumerd path */ |
| 4862 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, |
| 4863 | DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir); |
| 4864 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, |
| 4865 | DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir); |
| 4866 | |
| 4867 | DBG2("Kernel consumer err path: %s", |
| 4868 | kconsumer_data.err_unix_sock_path); |
| 4869 | DBG2("Kernel consumer cmd path: %s", |
| 4870 | kconsumer_data.cmd_unix_sock_path); |
| 4871 | } else { |
| 4872 | home_path = utils_get_home_dir(); |
| 4873 | if (home_path == NULL) { |
| 4874 | /* TODO: Add --socket PATH option */ |
| 4875 | ERR("Can't get HOME directory for sockets creation."); |
| 4876 | ret = -EPERM; |
| 4877 | goto error; |
| 4878 | } |
| 4879 | |
| 4880 | /* |
| 4881 | * Create rundir from home path. This will create something like |
| 4882 | * $HOME/.lttng |
| 4883 | */ |
| 4884 | ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path); |
| 4885 | if (ret < 0) { |
| 4886 | ret = -ENOMEM; |
| 4887 | goto error; |
| 4888 | } |
| 4889 | |
| 4890 | ret = create_lttng_rundir(rundir); |
| 4891 | if (ret < 0) { |
| 4892 | goto error; |
| 4893 | } |
| 4894 | |
| 4895 | if (strlen(apps_unix_sock_path) == 0) { |
| 4896 | snprintf(apps_unix_sock_path, PATH_MAX, |
| 4897 | DEFAULT_HOME_APPS_UNIX_SOCK, home_path); |
| 4898 | } |
| 4899 | |
| 4900 | /* Set the cli tool unix socket path */ |
| 4901 | if (strlen(client_unix_sock_path) == 0) { |
| 4902 | snprintf(client_unix_sock_path, PATH_MAX, |
| 4903 | DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path); |
| 4904 | } |
| 4905 | |
| 4906 | /* Set global SHM for ust */ |
| 4907 | if (strlen(wait_shm_path) == 0) { |
| 4908 | snprintf(wait_shm_path, PATH_MAX, |
| 4909 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid()); |
| 4910 | } |
| 4911 | |
| 4912 | /* Set health check Unix path */ |
| 4913 | if (strlen(health_unix_sock_path) == 0) { |
| 4914 | snprintf(health_unix_sock_path, sizeof(health_unix_sock_path), |
| 4915 | DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path); |
| 4916 | } |
| 4917 | } |
| 4918 | |
| 4919 | /* Set consumer initial state */ |
| 4920 | kernel_consumerd_state = CONSUMER_STOPPED; |
| 4921 | ust_consumerd_state = CONSUMER_STOPPED; |
| 4922 | |
| 4923 | DBG("Client socket path %s", client_unix_sock_path); |
| 4924 | DBG("Application socket path %s", apps_unix_sock_path); |
| 4925 | DBG("Application wait path %s", wait_shm_path); |
| 4926 | DBG("LTTng run directory path: %s", rundir); |
| 4927 | |
| 4928 | /* 32 bits consumerd path setup */ |
| 4929 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, |
| 4930 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir); |
| 4931 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, |
| 4932 | DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir); |
| 4933 | |
| 4934 | DBG2("UST consumer 32 bits err path: %s", |
| 4935 | ustconsumer32_data.err_unix_sock_path); |
| 4936 | DBG2("UST consumer 32 bits cmd path: %s", |
| 4937 | ustconsumer32_data.cmd_unix_sock_path); |
| 4938 | |
| 4939 | /* 64 bits consumerd path setup */ |
| 4940 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, |
| 4941 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir); |
| 4942 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, |
| 4943 | DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir); |
| 4944 | |
| 4945 | DBG2("UST consumer 64 bits err path: %s", |
| 4946 | ustconsumer64_data.err_unix_sock_path); |
| 4947 | DBG2("UST consumer 64 bits cmd path: %s", |
| 4948 | ustconsumer64_data.cmd_unix_sock_path); |
| 4949 | |
| 4950 | /* |
| 4951 | * See if daemon already exist. |
| 4952 | */ |
| 4953 | if ((ret = check_existing_daemon()) < 0) { |
| 4954 | ERR("Already running daemon.\n"); |
| 4955 | /* |
| 4956 | * We do not goto exit because we must not cleanup() |
| 4957 | * because a daemon is already running. |
| 4958 | */ |
| 4959 | goto error; |
| 4960 | } |
| 4961 | |
| 4962 | /* |
| 4963 | * Init UST app hash table. Alloc hash table before this point since |
| 4964 | * cleanup() can get called after that point. |
| 4965 | */ |
| 4966 | ust_app_ht_alloc(); |
| 4967 | |
| 4968 | /* Initialize JUL domain subsystem. */ |
| 4969 | if ((ret = jul_init()) < 0) { |
| 4970 | /* ENOMEM at this point. */ |
| 4971 | goto error; |
| 4972 | } |
| 4973 | |
| 4974 | /* After this point, we can safely call cleanup() with "goto exit" */ |
| 4975 | |
| 4976 | /* |
| 4977 | * These actions must be executed as root. We do that *after* setting up |
| 4978 | * the sockets path because we MUST make the check for another daemon using |
| 4979 | * those paths *before* trying to set the kernel consumer sockets and init |
| 4980 | * kernel tracer. |
| 4981 | */ |
| 4982 | if (is_root) { |
| 4983 | ret = set_consumer_sockets(&kconsumer_data, rundir); |
| 4984 | if (ret < 0) { |
| 4985 | goto exit; |
| 4986 | } |
| 4987 | |
| 4988 | /* Setup kernel tracer */ |
| 4989 | if (!opt_no_kernel) { |
| 4990 | init_kernel_tracer(); |
| 4991 | } |
| 4992 | |
| 4993 | /* Set ulimit for open files */ |
| 4994 | set_ulimit(); |
| 4995 | } |
| 4996 | /* init lttng_fd tracking must be done after set_ulimit. */ |
| 4997 | lttng_fd_init(); |
| 4998 | |
| 4999 | ret = set_consumer_sockets(&ustconsumer64_data, rundir); |
| 5000 | if (ret < 0) { |
| 5001 | goto exit; |
| 5002 | } |
| 5003 | |
| 5004 | ret = set_consumer_sockets(&ustconsumer32_data, rundir); |
| 5005 | if (ret < 0) { |
| 5006 | goto exit; |
| 5007 | } |
| 5008 | |
| 5009 | /* Setup the needed unix socket */ |
| 5010 | if ((ret = init_daemon_socket()) < 0) { |
| 5011 | goto exit; |
| 5012 | } |
| 5013 | |
| 5014 | /* Set credentials to socket */ |
| 5015 | if (is_root && ((ret = set_permissions(rundir)) < 0)) { |
| 5016 | goto exit; |
| 5017 | } |
| 5018 | |
| 5019 | /* Get parent pid if -S, --sig-parent is specified. */ |
| 5020 | if (opt_sig_parent) { |
| 5021 | ppid = getppid(); |
| 5022 | } |
| 5023 | |
| 5024 | /* Setup the kernel pipe for waking up the kernel thread */ |
| 5025 | if (is_root && !opt_no_kernel) { |
| 5026 | if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) { |
| 5027 | goto exit; |
| 5028 | } |
| 5029 | } |
| 5030 | |
| 5031 | /* Setup the thread ht_cleanup communication pipe. */ |
| 5032 | if (utils_create_pipe_cloexec(ht_cleanup_pipe) < 0) { |
| 5033 | goto exit; |
| 5034 | } |
| 5035 | |
| 5036 | /* Setup the thread apps communication pipe. */ |
| 5037 | if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) { |
| 5038 | goto exit; |
| 5039 | } |
| 5040 | |
| 5041 | /* Setup the thread apps notify communication pipe. */ |
| 5042 | if (utils_create_pipe_cloexec(apps_cmd_notify_pipe) < 0) { |
| 5043 | goto exit; |
| 5044 | } |
| 5045 | |
| 5046 | /* Initialize global buffer per UID and PID registry. */ |
| 5047 | buffer_reg_init_uid_registry(); |
| 5048 | buffer_reg_init_pid_registry(); |
| 5049 | |
| 5050 | /* Init UST command queue. */ |
| 5051 | cds_wfq_init(&ust_cmd_queue.queue); |
| 5052 | |
| 5053 | /* |
| 5054 | * Get session list pointer. This pointer MUST NOT be free(). This list is |
| 5055 | * statically declared in session.c |
| 5056 | */ |
| 5057 | session_list_ptr = session_get_list(); |
| 5058 | |
| 5059 | /* Set up max poll set size */ |
| 5060 | lttng_poll_set_max_size(); |
| 5061 | |
| 5062 | cmd_init(); |
| 5063 | |
| 5064 | /* Check for the application socket timeout env variable. */ |
| 5065 | env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV); |
| 5066 | if (env_app_timeout) { |
| 5067 | app_socket_timeout = atoi(env_app_timeout); |
| 5068 | } else { |
| 5069 | app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT; |
| 5070 | } |
| 5071 | |
| 5072 | write_pidfile(); |
| 5073 | write_julport(); |
| 5074 | |
| 5075 | /* Initialize communication library */ |
| 5076 | lttcomm_init(); |
| 5077 | /* This is to get the TCP timeout value. */ |
| 5078 | lttcomm_inet_init(); |
| 5079 | |
| 5080 | /* |
| 5081 | * Initialize the health check subsystem. This call should set the |
| 5082 | * appropriate time values. |
| 5083 | */ |
| 5084 | health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); |
| 5085 | if (!health_sessiond) { |
| 5086 | PERROR("health_app_create error"); |
| 5087 | goto exit_health_sessiond_cleanup; |
| 5088 | } |
| 5089 | |
| 5090 | /* Create thread to clean up RCU hash tables */ |
| 5091 | ret = pthread_create(&ht_cleanup_thread, NULL, |
| 5092 | thread_ht_cleanup, (void *) NULL); |
| 5093 | if (ret != 0) { |
| 5094 | PERROR("pthread_create ht_cleanup"); |
| 5095 | goto exit_ht_cleanup; |
| 5096 | } |
| 5097 | |
| 5098 | /* Create health-check thread */ |
| 5099 | ret = pthread_create(&health_thread, NULL, |
| 5100 | thread_manage_health, (void *) NULL); |
| 5101 | if (ret != 0) { |
| 5102 | PERROR("pthread_create health"); |
| 5103 | goto exit_health; |
| 5104 | } |
| 5105 | |
| 5106 | /* Create thread to manage the client socket */ |
| 5107 | ret = pthread_create(&client_thread, NULL, |
| 5108 | thread_manage_clients, (void *) NULL); |
| 5109 | if (ret != 0) { |
| 5110 | PERROR("pthread_create clients"); |
| 5111 | goto exit_client; |
| 5112 | } |
| 5113 | |
| 5114 | /* Create thread to dispatch registration */ |
| 5115 | ret = pthread_create(&dispatch_thread, NULL, |
| 5116 | thread_dispatch_ust_registration, (void *) NULL); |
| 5117 | if (ret != 0) { |
| 5118 | PERROR("pthread_create dispatch"); |
| 5119 | goto exit_dispatch; |
| 5120 | } |
| 5121 | |
| 5122 | /* Create thread to manage application registration. */ |
| 5123 | ret = pthread_create(®_apps_thread, NULL, |
| 5124 | thread_registration_apps, (void *) NULL); |
| 5125 | if (ret != 0) { |
| 5126 | PERROR("pthread_create registration"); |
| 5127 | goto exit_reg_apps; |
| 5128 | } |
| 5129 | |
| 5130 | /* Create thread to manage application socket */ |
| 5131 | ret = pthread_create(&apps_thread, NULL, |
| 5132 | thread_manage_apps, (void *) NULL); |
| 5133 | if (ret != 0) { |
| 5134 | PERROR("pthread_create apps"); |
| 5135 | goto exit_apps; |
| 5136 | } |
| 5137 | |
| 5138 | /* Create thread to manage application notify socket */ |
| 5139 | ret = pthread_create(&apps_notify_thread, NULL, |
| 5140 | ust_thread_manage_notify, (void *) NULL); |
| 5141 | if (ret != 0) { |
| 5142 | PERROR("pthread_create apps"); |
| 5143 | goto exit_apps_notify; |
| 5144 | } |
| 5145 | |
| 5146 | /* Create JUL registration thread. */ |
| 5147 | ret = pthread_create(&jul_reg_thread, NULL, |
| 5148 | jul_thread_manage_registration, (void *) NULL); |
| 5149 | if (ret != 0) { |
| 5150 | PERROR("pthread_create apps"); |
| 5151 | goto exit_jul_reg; |
| 5152 | } |
| 5153 | |
| 5154 | /* Don't start this thread if kernel tracing is not requested nor root */ |
| 5155 | if (is_root && !opt_no_kernel) { |
| 5156 | /* Create kernel thread to manage kernel event */ |
| 5157 | ret = pthread_create(&kernel_thread, NULL, |
| 5158 | thread_manage_kernel, (void *) NULL); |
| 5159 | if (ret != 0) { |
| 5160 | PERROR("pthread_create kernel"); |
| 5161 | goto exit_kernel; |
| 5162 | } |
| 5163 | |
| 5164 | ret = pthread_join(kernel_thread, &status); |
| 5165 | if (ret != 0) { |
| 5166 | PERROR("pthread_join"); |
| 5167 | goto error; /* join error, exit without cleanup */ |
| 5168 | } |
| 5169 | } |
| 5170 | |
| 5171 | exit_kernel: |
| 5172 | ret = pthread_join(jul_reg_thread, &status); |
| 5173 | if (ret != 0) { |
| 5174 | PERROR("pthread_join JUL"); |
| 5175 | goto error; /* join error, exit without cleanup */ |
| 5176 | } |
| 5177 | |
| 5178 | exit_jul_reg: |
| 5179 | ret = pthread_join(apps_notify_thread, &status); |
| 5180 | if (ret != 0) { |
| 5181 | PERROR("pthread_join apps notify"); |
| 5182 | goto error; /* join error, exit without cleanup */ |
| 5183 | } |
| 5184 | |
| 5185 | exit_apps_notify: |
| 5186 | ret = pthread_join(apps_thread, &status); |
| 5187 | if (ret != 0) { |
| 5188 | PERROR("pthread_join apps"); |
| 5189 | goto error; /* join error, exit without cleanup */ |
| 5190 | } |
| 5191 | |
| 5192 | |
| 5193 | exit_apps: |
| 5194 | ret = pthread_join(reg_apps_thread, &status); |
| 5195 | if (ret != 0) { |
| 5196 | PERROR("pthread_join"); |
| 5197 | goto error; /* join error, exit without cleanup */ |
| 5198 | } |
| 5199 | |
| 5200 | exit_reg_apps: |
| 5201 | ret = pthread_join(dispatch_thread, &status); |
| 5202 | if (ret != 0) { |
| 5203 | PERROR("pthread_join"); |
| 5204 | goto error; /* join error, exit without cleanup */ |
| 5205 | } |
| 5206 | |
| 5207 | exit_dispatch: |
| 5208 | ret = pthread_join(client_thread, &status); |
| 5209 | if (ret != 0) { |
| 5210 | PERROR("pthread_join"); |
| 5211 | goto error; /* join error, exit without cleanup */ |
| 5212 | } |
| 5213 | |
| 5214 | ret = join_consumer_thread(&kconsumer_data); |
| 5215 | if (ret != 0) { |
| 5216 | PERROR("join_consumer"); |
| 5217 | goto error; /* join error, exit without cleanup */ |
| 5218 | } |
| 5219 | |
| 5220 | ret = join_consumer_thread(&ustconsumer32_data); |
| 5221 | if (ret != 0) { |
| 5222 | PERROR("join_consumer ust32"); |
| 5223 | goto error; /* join error, exit without cleanup */ |
| 5224 | } |
| 5225 | |
| 5226 | ret = join_consumer_thread(&ustconsumer64_data); |
| 5227 | if (ret != 0) { |
| 5228 | PERROR("join_consumer ust64"); |
| 5229 | goto error; /* join error, exit without cleanup */ |
| 5230 | } |
| 5231 | |
| 5232 | exit_client: |
| 5233 | ret = pthread_join(health_thread, &status); |
| 5234 | if (ret != 0) { |
| 5235 | PERROR("pthread_join health thread"); |
| 5236 | goto error; /* join error, exit without cleanup */ |
| 5237 | } |
| 5238 | |
| 5239 | exit_health: |
| 5240 | ret = pthread_join(ht_cleanup_thread, &status); |
| 5241 | if (ret != 0) { |
| 5242 | PERROR("pthread_join ht cleanup thread"); |
| 5243 | goto error; /* join error, exit without cleanup */ |
| 5244 | } |
| 5245 | exit_ht_cleanup: |
| 5246 | health_app_destroy(health_sessiond); |
| 5247 | exit_health_sessiond_cleanup: |
| 5248 | exit: |
| 5249 | /* |
| 5250 | * cleanup() is called when no other thread is running. |
| 5251 | */ |
| 5252 | rcu_thread_online(); |
| 5253 | cleanup(); |
| 5254 | rcu_thread_offline(); |
| 5255 | rcu_unregister_thread(); |
| 5256 | if (!ret) { |
| 5257 | exit(EXIT_SUCCESS); |
| 5258 | } |
| 5259 | error: |
| 5260 | exit(EXIT_FAILURE); |
| 5261 | } |