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