| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License as published by the Free |
| 7 | * Software Foundation; only version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 16 | * Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <fcntl.h> |
| 21 | #include <getopt.h> |
| 22 | #include <grp.h> |
| 23 | #include <limits.h> |
| 24 | #include <pthread.h> |
| 25 | #include <semaphore.h> |
| 26 | #include <signal.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/mman.h> |
| 31 | #include <sys/mount.h> |
| 32 | #include <sys/resource.h> |
| 33 | #include <sys/socket.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/wait.h> |
| 37 | #include <urcu/futex.h> |
| 38 | #include <unistd.h> |
| 39 | #include <config.h> |
| 40 | |
| 41 | #include <lttng-consumerd.h> |
| 42 | #include <lttng-sessiond-comm.h> |
| 43 | #include <lttng/lttng-consumer.h> |
| 44 | |
| 45 | #ifdef CONFIG_LTTNG_TOOLS_HAVE_UST |
| 46 | #include <ust/lttng-ust-ctl.h> |
| 47 | #else |
| 48 | #include "lttng-ust-ctl.h" |
| 49 | #endif |
| 50 | |
| 51 | |
| 52 | #include <lttngerr.h> |
| 53 | |
| 54 | #include "channel.h" |
| 55 | #include "compat/poll.h" |
| 56 | #include "context.h" |
| 57 | #include "event.h" |
| 58 | #include "futex.h" |
| 59 | #include "kernel-ctl.h" |
| 60 | #include "ltt-sessiond.h" |
| 61 | #include "shm.h" |
| 62 | #include "ust-app.h" |
| 63 | #include "ust-ctl.h" |
| 64 | #include "utils.h" |
| 65 | #include "ust-ctl.h" |
| 66 | |
| 67 | struct consumer_data { |
| 68 | enum lttng_consumer_type type; |
| 69 | |
| 70 | pthread_t thread; /* Worker thread interacting with the consumer */ |
| 71 | sem_t sem; |
| 72 | |
| 73 | /* Mutex to control consumerd pid assignation */ |
| 74 | pthread_mutex_t pid_mutex; |
| 75 | pid_t pid; |
| 76 | |
| 77 | int err_sock; |
| 78 | int cmd_sock; |
| 79 | |
| 80 | /* consumer error and command Unix socket path */ |
| 81 | char err_unix_sock_path[PATH_MAX]; |
| 82 | char cmd_unix_sock_path[PATH_MAX]; |
| 83 | }; |
| 84 | |
| 85 | /* Const values */ |
| 86 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
| 87 | const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP; |
| 88 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; |
| 89 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; |
| 90 | |
| 91 | /* Variables */ |
| 92 | int opt_verbose; /* Not static for lttngerr.h */ |
| 93 | int opt_verbose_consumer; /* Not static for lttngerr.h */ |
| 94 | int opt_quiet; /* Not static for lttngerr.h */ |
| 95 | |
| 96 | const char *progname; |
| 97 | const char *opt_tracing_group; |
| 98 | static int opt_sig_parent; |
| 99 | static int opt_daemon; |
| 100 | static int is_root; /* Set to 1 if the daemon is running as root */ |
| 101 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
| 102 | |
| 103 | /* Consumer daemon specific control data */ |
| 104 | static struct consumer_data kconsumer_data = { |
| 105 | .type = LTTNG_CONSUMER_KERNEL, |
| 106 | }; |
| 107 | static struct consumer_data ustconsumer_data = { |
| 108 | .type = LTTNG_CONSUMER_UST, |
| 109 | }; |
| 110 | |
| 111 | static int dispatch_thread_exit; |
| 112 | |
| 113 | /* Global application Unix socket path */ |
| 114 | static char apps_unix_sock_path[PATH_MAX]; |
| 115 | /* Global client Unix socket path */ |
| 116 | static char client_unix_sock_path[PATH_MAX]; |
| 117 | /* global wait shm path for UST */ |
| 118 | static char wait_shm_path[PATH_MAX]; |
| 119 | |
| 120 | /* Sockets and FDs */ |
| 121 | static int client_sock; |
| 122 | static int apps_sock; |
| 123 | static int kernel_tracer_fd; |
| 124 | static int kernel_poll_pipe[2]; |
| 125 | |
| 126 | /* |
| 127 | * Quit pipe for all threads. This permits a single cancellation point |
| 128 | * for all threads when receiving an event on the pipe. |
| 129 | */ |
| 130 | static int thread_quit_pipe[2]; |
| 131 | |
| 132 | /* |
| 133 | * This pipe is used to inform the thread managing application communication |
| 134 | * that a command is queued and ready to be processed. |
| 135 | */ |
| 136 | static int apps_cmd_pipe[2]; |
| 137 | |
| 138 | /* Pthread, Mutexes and Semaphores */ |
| 139 | static pthread_t apps_thread; |
| 140 | static pthread_t reg_apps_thread; |
| 141 | static pthread_t client_thread; |
| 142 | static pthread_t kernel_thread; |
| 143 | static pthread_t dispatch_thread; |
| 144 | |
| 145 | |
| 146 | /* |
| 147 | * UST registration command queue. This queue is tied with a futex and uses a N |
| 148 | * wakers / 1 waiter implemented and detailed in futex.c/.h |
| 149 | * |
| 150 | * The thread_manage_apps and thread_dispatch_ust_registration interact with |
| 151 | * this queue and the wait/wake scheme. |
| 152 | */ |
| 153 | static struct ust_cmd_queue ust_cmd_queue; |
| 154 | |
| 155 | /* |
| 156 | * Pointer initialized before thread creation. |
| 157 | * |
| 158 | * This points to the tracing session list containing the session count and a |
| 159 | * mutex lock. The lock MUST be taken if you iterate over the list. The lock |
| 160 | * MUST NOT be taken if you call a public function in session.c. |
| 161 | * |
| 162 | * The lock is nested inside the structure: session_list_ptr->lock. Please use |
| 163 | * session_lock_list and session_unlock_list for lock acquisition. |
| 164 | */ |
| 165 | static struct ltt_session_list *session_list_ptr; |
| 166 | |
| 167 | /* |
| 168 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. |
| 169 | */ |
| 170 | static int create_thread_poll_set(struct lttng_poll_event *events, |
| 171 | unsigned int size) |
| 172 | { |
| 173 | int ret; |
| 174 | |
| 175 | if (events == NULL || size == 0) { |
| 176 | ret = -1; |
| 177 | goto error; |
| 178 | } |
| 179 | |
| 180 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); |
| 181 | if (ret < 0) { |
| 182 | goto error; |
| 183 | } |
| 184 | |
| 185 | /* Add quit pipe */ |
| 186 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN); |
| 187 | if (ret < 0) { |
| 188 | goto error; |
| 189 | } |
| 190 | |
| 191 | return 0; |
| 192 | |
| 193 | error: |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Check if the thread quit pipe was triggered. |
| 199 | * |
| 200 | * Return 1 if it was triggered else 0; |
| 201 | */ |
| 202 | static int check_thread_quit_pipe(int fd, uint32_t events) |
| 203 | { |
| 204 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { |
| 205 | return 1; |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Remove modules in reverse load order. |
| 213 | */ |
| 214 | static int modprobe_remove_kernel_modules(void) |
| 215 | { |
| 216 | int ret = 0, i; |
| 217 | char modprobe[256]; |
| 218 | |
| 219 | for (i = ARRAY_SIZE(kernel_modules_list) - 1; i >= 0; i--) { |
| 220 | ret = snprintf(modprobe, sizeof(modprobe), |
| 221 | "/sbin/modprobe --remove --quiet %s", |
| 222 | kernel_modules_list[i].name); |
| 223 | if (ret < 0) { |
| 224 | perror("snprintf modprobe --remove"); |
| 225 | goto error; |
| 226 | } |
| 227 | modprobe[sizeof(modprobe) - 1] = '\0'; |
| 228 | ret = system(modprobe); |
| 229 | if (ret == -1) { |
| 230 | ERR("Unable to launch modprobe --remove for module %s", |
| 231 | kernel_modules_list[i].name); |
| 232 | } else if (kernel_modules_list[i].required |
| 233 | && WEXITSTATUS(ret) != 0) { |
| 234 | ERR("Unable to remove module %s", |
| 235 | kernel_modules_list[i].name); |
| 236 | } else { |
| 237 | DBG("Modprobe removal successful %s", |
| 238 | kernel_modules_list[i].name); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | error: |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Return group ID of the tracing group or -1 if not found. |
| 248 | */ |
| 249 | static gid_t allowed_group(void) |
| 250 | { |
| 251 | struct group *grp; |
| 252 | |
| 253 | if (opt_tracing_group) { |
| 254 | grp = getgrnam(opt_tracing_group); |
| 255 | } else { |
| 256 | grp = getgrnam(default_tracing_group); |
| 257 | } |
| 258 | if (!grp) { |
| 259 | return -1; |
| 260 | } else { |
| 261 | return grp->gr_gid; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Init thread quit pipe. |
| 267 | * |
| 268 | * Return -1 on error or 0 if all pipes are created. |
| 269 | */ |
| 270 | static int init_thread_quit_pipe(void) |
| 271 | { |
| 272 | int ret; |
| 273 | |
| 274 | ret = pipe2(thread_quit_pipe, O_CLOEXEC); |
| 275 | if (ret < 0) { |
| 276 | perror("thread quit pipe"); |
| 277 | goto error; |
| 278 | } |
| 279 | |
| 280 | error: |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Complete teardown of a kernel session. This free all data structure related |
| 286 | * to a kernel session and update counter. |
| 287 | */ |
| 288 | static void teardown_kernel_session(struct ltt_session *session) |
| 289 | { |
| 290 | if (session->kernel_session != NULL) { |
| 291 | DBG("Tearing down kernel session"); |
| 292 | |
| 293 | /* |
| 294 | * If a custom kernel consumer was registered, close the socket before |
| 295 | * tearing down the complete kernel session structure |
| 296 | */ |
| 297 | if (session->kernel_session->consumer_fd != kconsumer_data.cmd_sock) { |
| 298 | lttcomm_close_unix_sock(session->kernel_session->consumer_fd); |
| 299 | } |
| 300 | |
| 301 | trace_kernel_destroy_session(session->kernel_session); |
| 302 | /* Extra precaution */ |
| 303 | session->kernel_session = NULL; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Stop all threads by closing the thread quit pipe. |
| 309 | */ |
| 310 | static void stop_threads(void) |
| 311 | { |
| 312 | int ret; |
| 313 | |
| 314 | /* Stopping all threads */ |
| 315 | DBG("Terminating all threads"); |
| 316 | ret = notify_thread_pipe(thread_quit_pipe[1]); |
| 317 | if (ret < 0) { |
| 318 | ERR("write error on thread quit pipe"); |
| 319 | } |
| 320 | |
| 321 | /* Dispatch thread */ |
| 322 | dispatch_thread_exit = 1; |
| 323 | futex_nto1_wake(&ust_cmd_queue.futex); |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Cleanup the daemon |
| 328 | */ |
| 329 | static void cleanup(void) |
| 330 | { |
| 331 | int ret; |
| 332 | char *cmd; |
| 333 | struct ltt_session *sess, *stmp; |
| 334 | |
| 335 | DBG("Cleaning up"); |
| 336 | |
| 337 | /* <fun> */ |
| 338 | MSG("%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm" |
| 339 | "Matthew, BEET driven development works!%c[%dm", |
| 340 | 27, 1, 31, 27, 0, 27, 1, 33, 27, 0); |
| 341 | /* </fun> */ |
| 342 | |
| 343 | if (is_root) { |
| 344 | DBG("Removing %s directory", LTTNG_RUNDIR); |
| 345 | ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR); |
| 346 | if (ret < 0) { |
| 347 | ERR("asprintf failed. Something is really wrong!"); |
| 348 | } |
| 349 | |
| 350 | /* Remove lttng run directory */ |
| 351 | ret = system(cmd); |
| 352 | if (ret < 0) { |
| 353 | ERR("Unable to clean " LTTNG_RUNDIR); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | DBG("Cleaning up all session"); |
| 358 | |
| 359 | /* Destroy session list mutex */ |
| 360 | if (session_list_ptr != NULL) { |
| 361 | pthread_mutex_destroy(&session_list_ptr->lock); |
| 362 | |
| 363 | /* Cleanup ALL session */ |
| 364 | cds_list_for_each_entry_safe(sess, stmp, |
| 365 | &session_list_ptr->head, list) { |
| 366 | teardown_kernel_session(sess); |
| 367 | // TODO complete session cleanup (including UST) |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | DBG("Closing all UST sockets"); |
| 372 | ust_app_clean_list(); |
| 373 | |
| 374 | pthread_mutex_destroy(&kconsumer_data.pid_mutex); |
| 375 | |
| 376 | DBG("Closing kernel fd"); |
| 377 | close(kernel_tracer_fd); |
| 378 | |
| 379 | if (is_root) { |
| 380 | DBG("Unloading kernel modules"); |
| 381 | modprobe_remove_kernel_modules(); |
| 382 | } |
| 383 | |
| 384 | close(thread_quit_pipe[0]); |
| 385 | close(thread_quit_pipe[1]); |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * Send data on a unix socket using the liblttsessiondcomm API. |
| 390 | * |
| 391 | * Return lttcomm error code. |
| 392 | */ |
| 393 | static int send_unix_sock(int sock, void *buf, size_t len) |
| 394 | { |
| 395 | /* Check valid length */ |
| 396 | if (len <= 0) { |
| 397 | return -1; |
| 398 | } |
| 399 | |
| 400 | return lttcomm_send_unix_sock(sock, buf, len); |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Free memory of a command context structure. |
| 405 | */ |
| 406 | static void clean_command_ctx(struct command_ctx **cmd_ctx) |
| 407 | { |
| 408 | DBG("Clean command context structure"); |
| 409 | if (*cmd_ctx) { |
| 410 | if ((*cmd_ctx)->llm) { |
| 411 | free((*cmd_ctx)->llm); |
| 412 | } |
| 413 | if ((*cmd_ctx)->lsm) { |
| 414 | free((*cmd_ctx)->lsm); |
| 415 | } |
| 416 | free(*cmd_ctx); |
| 417 | *cmd_ctx = NULL; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Send all stream fds of kernel channel to the consumer. |
| 423 | */ |
| 424 | static int send_kconsumer_channel_streams(struct consumer_data *consumer_data, |
| 425 | int sock, struct ltt_kernel_channel *channel) |
| 426 | { |
| 427 | int ret; |
| 428 | struct ltt_kernel_stream *stream; |
| 429 | struct lttcomm_consumer_msg lkm; |
| 430 | |
| 431 | DBG("Sending streams of channel %s to kernel consumer", |
| 432 | channel->channel->name); |
| 433 | |
| 434 | /* Send channel */ |
| 435 | lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL; |
| 436 | lkm.u.channel.channel_key = channel->fd; |
| 437 | lkm.u.channel.max_sb_size = channel->channel->attr.subbuf_size; |
| 438 | lkm.u.channel.mmap_len = 0; /* for kernel */ |
| 439 | DBG("Sending channel %d to consumer", lkm.u.channel.channel_key); |
| 440 | ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); |
| 441 | if (ret < 0) { |
| 442 | perror("send consumer channel"); |
| 443 | goto error; |
| 444 | } |
| 445 | |
| 446 | /* Send streams */ |
| 447 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { |
| 448 | if (!stream->fd) { |
| 449 | continue; |
| 450 | } |
| 451 | lkm.cmd_type = LTTNG_CONSUMER_ADD_STREAM; |
| 452 | lkm.u.stream.channel_key = channel->fd; |
| 453 | lkm.u.stream.stream_key = stream->fd; |
| 454 | lkm.u.stream.state = stream->state; |
| 455 | lkm.u.stream.output = channel->channel->attr.output; |
| 456 | lkm.u.stream.mmap_len = 0; /* for kernel */ |
| 457 | strncpy(lkm.u.stream.path_name, stream->pathname, PATH_MAX - 1); |
| 458 | lkm.u.stream.path_name[PATH_MAX - 1] = '\0'; |
| 459 | DBG("Sending stream %d to consumer", lkm.u.stream.stream_key); |
| 460 | ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); |
| 461 | if (ret < 0) { |
| 462 | perror("send consumer stream"); |
| 463 | goto error; |
| 464 | } |
| 465 | ret = lttcomm_send_fds_unix_sock(sock, &stream->fd, 1); |
| 466 | if (ret < 0) { |
| 467 | perror("send consumer stream ancillary data"); |
| 468 | goto error; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | DBG("consumer channel streams sent"); |
| 473 | |
| 474 | return 0; |
| 475 | |
| 476 | error: |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Send all stream fds of UST channel to the consumer. |
| 482 | */ |
| 483 | static int send_ustconsumer_channel_streams(struct consumer_data *consumer_data, |
| 484 | int sock, struct ltt_ust_channel *channel) |
| 485 | { |
| 486 | int ret, fds[2]; |
| 487 | struct ltt_ust_stream *stream; |
| 488 | struct lttcomm_consumer_msg lum; |
| 489 | |
| 490 | DBG("Sending streams of channel %s to UST consumer", |
| 491 | channel->name); |
| 492 | |
| 493 | /* Send channel */ |
| 494 | lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL; |
| 495 | /* |
| 496 | * We need to keep shm_fd open to make sure this key stays |
| 497 | * unique within the session daemon. |
| 498 | */ |
| 499 | lum.u.channel.channel_key = channel->obj->shm_fd; |
| 500 | lum.u.channel.max_sb_size = channel->attr.subbuf_size; |
| 501 | lum.u.channel.mmap_len = channel->obj->memory_map_size; |
| 502 | DBG("Sending channel %d to consumer", lum.u.channel.channel_key); |
| 503 | ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); |
| 504 | if (ret < 0) { |
| 505 | perror("send consumer channel"); |
| 506 | goto error; |
| 507 | } |
| 508 | fds[0] = channel->obj->shm_fd; |
| 509 | fds[1] = channel->obj->wait_fd; |
| 510 | ret = lttcomm_send_fds_unix_sock(sock, fds, 2); |
| 511 | if (ret < 0) { |
| 512 | perror("send consumer channel ancillary data"); |
| 513 | goto error; |
| 514 | } |
| 515 | |
| 516 | /* Send streams */ |
| 517 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { |
| 518 | int fds[2]; |
| 519 | |
| 520 | if (!stream->obj->shm_fd) { |
| 521 | continue; |
| 522 | } |
| 523 | lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM; |
| 524 | lum.u.stream.channel_key = channel->obj->shm_fd; |
| 525 | lum.u.stream.stream_key = stream->obj->shm_fd; |
| 526 | lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM; |
| 527 | lum.u.stream.output = channel->attr.output; |
| 528 | lum.u.stream.mmap_len = stream->obj->memory_map_size; |
| 529 | strncpy(lum.u.stream.path_name, stream->pathname, PATH_MAX - 1); |
| 530 | lum.u.stream.path_name[PATH_MAX - 1] = '\0'; |
| 531 | DBG("Sending stream %d to consumer", lum.u.stream.stream_key); |
| 532 | ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); |
| 533 | if (ret < 0) { |
| 534 | perror("send consumer stream"); |
| 535 | goto error; |
| 536 | } |
| 537 | fds[0] = stream->obj->shm_fd; |
| 538 | fds[1] = stream->obj->wait_fd; |
| 539 | ret = lttcomm_send_fds_unix_sock(sock, fds, 2); |
| 540 | if (ret < 0) { |
| 541 | perror("send consumer stream ancillary data"); |
| 542 | goto error; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | DBG("consumer channel streams sent"); |
| 547 | |
| 548 | return 0; |
| 549 | |
| 550 | error: |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * Send all stream fds of the kernel session to the consumer. |
| 556 | */ |
| 557 | static int send_kconsumer_session_streams(struct consumer_data *consumer_data, |
| 558 | struct ltt_kernel_session *session) |
| 559 | { |
| 560 | int ret; |
| 561 | struct ltt_kernel_channel *chan; |
| 562 | struct lttcomm_consumer_msg lkm; |
| 563 | int sock = session->consumer_fd; |
| 564 | |
| 565 | DBG("Sending metadata stream fd"); |
| 566 | |
| 567 | /* Extra protection. It's NOT supposed to be set to 0 at this point */ |
| 568 | if (session->consumer_fd == 0) { |
| 569 | session->consumer_fd = consumer_data->cmd_sock; |
| 570 | } |
| 571 | |
| 572 | if (session->metadata_stream_fd != 0) { |
| 573 | /* Send metadata channel fd */ |
| 574 | lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL; |
| 575 | lkm.u.channel.channel_key = session->metadata->fd; |
| 576 | lkm.u.channel.max_sb_size = session->metadata->conf->attr.subbuf_size; |
| 577 | lkm.u.channel.mmap_len = 0; /* for kernel */ |
| 578 | DBG("Sending metadata channel %d to consumer", lkm.u.stream.stream_key); |
| 579 | ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); |
| 580 | if (ret < 0) { |
| 581 | perror("send consumer channel"); |
| 582 | goto error; |
| 583 | } |
| 584 | |
| 585 | /* Send metadata stream fd */ |
| 586 | lkm.cmd_type = LTTNG_CONSUMER_ADD_STREAM; |
| 587 | lkm.u.stream.channel_key = session->metadata->fd; |
| 588 | lkm.u.stream.stream_key = session->metadata_stream_fd; |
| 589 | lkm.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM; |
| 590 | lkm.u.stream.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
| 591 | lkm.u.stream.mmap_len = 0; /* for kernel */ |
| 592 | strncpy(lkm.u.stream.path_name, session->metadata->pathname, PATH_MAX - 1); |
| 593 | lkm.u.stream.path_name[PATH_MAX - 1] = '\0'; |
| 594 | DBG("Sending metadata stream %d to consumer", lkm.u.stream.stream_key); |
| 595 | ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); |
| 596 | if (ret < 0) { |
| 597 | perror("send consumer stream"); |
| 598 | goto error; |
| 599 | } |
| 600 | ret = lttcomm_send_fds_unix_sock(sock, &session->metadata_stream_fd, 1); |
| 601 | if (ret < 0) { |
| 602 | perror("send consumer stream"); |
| 603 | goto error; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
| 608 | ret = send_kconsumer_channel_streams(consumer_data, sock, chan); |
| 609 | if (ret < 0) { |
| 610 | goto error; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | DBG("consumer fds (metadata and channel streams) sent"); |
| 615 | |
| 616 | return 0; |
| 617 | |
| 618 | error: |
| 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * Send all stream fds of the UST session to the consumer. |
| 624 | */ |
| 625 | static int send_ustconsumer_session_streams(struct consumer_data *consumer_data, |
| 626 | struct ltt_ust_session *session) |
| 627 | { |
| 628 | int ret; |
| 629 | struct ltt_ust_channel *chan; |
| 630 | struct lttcomm_consumer_msg lum; |
| 631 | int sock = session->consumer_fd; |
| 632 | |
| 633 | DBG("Sending metadata stream fd"); |
| 634 | |
| 635 | /* Extra protection. It's NOT supposed to be set to 0 at this point */ |
| 636 | if (session->consumer_fd == 0) { |
| 637 | session->consumer_fd = consumer_data->cmd_sock; |
| 638 | } |
| 639 | |
| 640 | if (session->metadata->obj->shm_fd != 0) { |
| 641 | int fds[2]; |
| 642 | |
| 643 | /* Send metadata channel fd */ |
| 644 | lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL; |
| 645 | lum.u.channel.channel_key = session->metadata->obj->shm_fd; |
| 646 | lum.u.channel.max_sb_size = session->metadata->attr.subbuf_size; |
| 647 | lum.u.channel.mmap_len = 0; /* for kernel */ |
| 648 | DBG("Sending metadata channel %d to consumer", lum.u.stream.stream_key); |
| 649 | ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); |
| 650 | if (ret < 0) { |
| 651 | perror("send consumer channel"); |
| 652 | goto error; |
| 653 | } |
| 654 | fds[0] = session->metadata->obj->shm_fd; |
| 655 | fds[1] = session->metadata->obj->wait_fd; |
| 656 | ret = lttcomm_send_fds_unix_sock(sock, fds, 2); |
| 657 | if (ret < 0) { |
| 658 | perror("send consumer metadata channel"); |
| 659 | goto error; |
| 660 | } |
| 661 | |
| 662 | /* Send metadata stream fd */ |
| 663 | lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM; |
| 664 | lum.u.stream.channel_key = session->metadata->obj->shm_fd; |
| 665 | lum.u.stream.stream_key = session->metadata->stream_obj->shm_fd; |
| 666 | lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM; |
| 667 | lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT; |
| 668 | lum.u.stream.mmap_len = session->metadata->stream_obj->memory_map_size; |
| 669 | strncpy(lum.u.stream.path_name, session->metadata->pathname, PATH_MAX - 1); |
| 670 | lum.u.stream.path_name[PATH_MAX - 1] = '\0'; |
| 671 | DBG("Sending metadata stream %d to consumer", lum.u.stream.stream_key); |
| 672 | ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); |
| 673 | if (ret < 0) { |
| 674 | perror("send consumer metadata stream"); |
| 675 | goto error; |
| 676 | } |
| 677 | fds[0] = session->metadata->stream_obj->shm_fd; |
| 678 | fds[1] = session->metadata->stream_obj->wait_fd; |
| 679 | ret = lttcomm_send_fds_unix_sock(sock, fds, 2); |
| 680 | if (ret < 0) { |
| 681 | perror("send consumer stream"); |
| 682 | goto error; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | cds_list_for_each_entry(chan, &session->channels.head, list) { |
| 687 | ret = send_ustconsumer_channel_streams(consumer_data, sock, chan); |
| 688 | if (ret < 0) { |
| 689 | goto error; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | DBG("consumer fds (metadata and channel streams) sent"); |
| 694 | |
| 695 | return 0; |
| 696 | |
| 697 | error: |
| 698 | return ret; |
| 699 | } |
| 700 | |
| 701 | /* |
| 702 | * Notify UST applications using the shm mmap futex. |
| 703 | */ |
| 704 | static int notify_ust_apps(int active) |
| 705 | { |
| 706 | char *wait_shm_mmap; |
| 707 | |
| 708 | DBG("Notifying applications of session daemon state: %d", active); |
| 709 | |
| 710 | /* See shm.c for this call implying mmap, shm and futex calls */ |
| 711 | wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root); |
| 712 | if (wait_shm_mmap == NULL) { |
| 713 | goto error; |
| 714 | } |
| 715 | |
| 716 | /* Wake waiting process */ |
| 717 | futex_wait_update((int32_t *) wait_shm_mmap, active); |
| 718 | |
| 719 | /* Apps notified successfully */ |
| 720 | return 0; |
| 721 | |
| 722 | error: |
| 723 | return -1; |
| 724 | } |
| 725 | |
| 726 | /* |
| 727 | * Setup the outgoing data buffer for the response (llm) by allocating the |
| 728 | * right amount of memory and copying the original information from the lsm |
| 729 | * structure. |
| 730 | * |
| 731 | * Return total size of the buffer pointed by buf. |
| 732 | */ |
| 733 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
| 734 | { |
| 735 | int ret, buf_size; |
| 736 | |
| 737 | buf_size = size; |
| 738 | |
| 739 | cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size); |
| 740 | if (cmd_ctx->llm == NULL) { |
| 741 | perror("malloc"); |
| 742 | ret = -ENOMEM; |
| 743 | goto error; |
| 744 | } |
| 745 | |
| 746 | /* Copy common data */ |
| 747 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; |
| 748 | cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid; |
| 749 | |
| 750 | cmd_ctx->llm->data_size = size; |
| 751 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; |
| 752 | |
| 753 | return buf_size; |
| 754 | |
| 755 | error: |
| 756 | return ret; |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | * Update the kernel poll set of all channel fd available over all tracing |
| 761 | * session. Add the wakeup pipe at the end of the set. |
| 762 | */ |
| 763 | static int update_kernel_poll(struct lttng_poll_event *events) |
| 764 | { |
| 765 | int ret; |
| 766 | struct ltt_session *session; |
| 767 | struct ltt_kernel_channel *channel; |
| 768 | |
| 769 | DBG("Updating kernel poll set"); |
| 770 | |
| 771 | session_lock_list(); |
| 772 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
| 773 | session_lock(session); |
| 774 | if (session->kernel_session == NULL) { |
| 775 | session_unlock(session); |
| 776 | continue; |
| 777 | } |
| 778 | |
| 779 | cds_list_for_each_entry(channel, |
| 780 | &session->kernel_session->channel_list.head, list) { |
| 781 | /* Add channel fd to the kernel poll set */ |
| 782 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); |
| 783 | if (ret < 0) { |
| 784 | session_unlock(session); |
| 785 | goto error; |
| 786 | } |
| 787 | DBG("Channel fd %d added to kernel set", channel->fd); |
| 788 | } |
| 789 | session_unlock(session); |
| 790 | } |
| 791 | session_unlock_list(); |
| 792 | |
| 793 | return 0; |
| 794 | |
| 795 | error: |
| 796 | session_unlock_list(); |
| 797 | return -1; |
| 798 | } |
| 799 | |
| 800 | /* |
| 801 | * Find the channel fd from 'fd' over all tracing session. When found, check |
| 802 | * for new channel stream and send those stream fds to the kernel consumer. |
| 803 | * |
| 804 | * Useful for CPU hotplug feature. |
| 805 | */ |
| 806 | static int update_kernel_stream(struct consumer_data *consumer_data, int fd) |
| 807 | { |
| 808 | int ret = 0; |
| 809 | struct ltt_session *session; |
| 810 | struct ltt_kernel_channel *channel; |
| 811 | |
| 812 | DBG("Updating kernel streams for channel fd %d", fd); |
| 813 | |
| 814 | session_lock_list(); |
| 815 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
| 816 | session_lock(session); |
| 817 | if (session->kernel_session == NULL) { |
| 818 | session_unlock(session); |
| 819 | continue; |
| 820 | } |
| 821 | |
| 822 | /* This is not suppose to be 0 but this is an extra security check */ |
| 823 | if (session->kernel_session->consumer_fd == 0) { |
| 824 | session->kernel_session->consumer_fd = consumer_data->cmd_sock; |
| 825 | } |
| 826 | |
| 827 | cds_list_for_each_entry(channel, |
| 828 | &session->kernel_session->channel_list.head, list) { |
| 829 | if (channel->fd == fd) { |
| 830 | DBG("Channel found, updating kernel streams"); |
| 831 | ret = kernel_open_channel_stream(channel); |
| 832 | if (ret < 0) { |
| 833 | goto error; |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * Have we already sent fds to the consumer? If yes, it means |
| 838 | * that tracing is started so it is safe to send our updated |
| 839 | * stream fds. |
| 840 | */ |
| 841 | if (session->kernel_session->consumer_fds_sent == 1) { |
| 842 | ret = send_kconsumer_channel_streams(consumer_data, |
| 843 | session->kernel_session->consumer_fd, channel); |
| 844 | if (ret < 0) { |
| 845 | goto error; |
| 846 | } |
| 847 | } |
| 848 | goto error; |
| 849 | } |
| 850 | } |
| 851 | session_unlock(session); |
| 852 | } |
| 853 | session_unlock_list(); |
| 854 | return ret; |
| 855 | |
| 856 | error: |
| 857 | session_unlock(session); |
| 858 | session_unlock_list(); |
| 859 | return ret; |
| 860 | } |
| 861 | |
| 862 | /* |
| 863 | * This thread manage event coming from the kernel. |
| 864 | * |
| 865 | * Features supported in this thread: |
| 866 | * -) CPU Hotplug |
| 867 | */ |
| 868 | static void *thread_manage_kernel(void *data) |
| 869 | { |
| 870 | int ret, i, pollfd, update_poll_flag = 1; |
| 871 | uint32_t revents, nb_fd; |
| 872 | char tmp; |
| 873 | struct lttng_poll_event events; |
| 874 | |
| 875 | DBG("Thread manage kernel started"); |
| 876 | |
| 877 | ret = create_thread_poll_set(&events, 2); |
| 878 | if (ret < 0) { |
| 879 | goto error; |
| 880 | } |
| 881 | |
| 882 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); |
| 883 | if (ret < 0) { |
| 884 | goto error; |
| 885 | } |
| 886 | |
| 887 | while (1) { |
| 888 | if (update_poll_flag == 1) { |
| 889 | /* |
| 890 | * Reset number of fd in the poll set. Always 2 since there is the thread |
| 891 | * quit pipe and the kernel pipe. |
| 892 | */ |
| 893 | events.nb_fd = 2; |
| 894 | |
| 895 | ret = update_kernel_poll(&events); |
| 896 | if (ret < 0) { |
| 897 | goto error; |
| 898 | } |
| 899 | update_poll_flag = 0; |
| 900 | } |
| 901 | |
| 902 | nb_fd = LTTNG_POLL_GETNB(&events); |
| 903 | |
| 904 | DBG("Thread kernel polling on %d fds", nb_fd); |
| 905 | |
| 906 | /* Zeroed the poll events */ |
| 907 | lttng_poll_reset(&events); |
| 908 | |
| 909 | /* Poll infinite value of time */ |
| 910 | ret = lttng_poll_wait(&events, -1); |
| 911 | if (ret < 0) { |
| 912 | goto error; |
| 913 | } else if (ret == 0) { |
| 914 | /* Should not happen since timeout is infinite */ |
| 915 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
| 916 | "This should not have happened! Continuing..."); |
| 917 | continue; |
| 918 | } |
| 919 | |
| 920 | for (i = 0; i < nb_fd; i++) { |
| 921 | /* Fetch once the poll data */ |
| 922 | revents = LTTNG_POLL_GETEV(&events, i); |
| 923 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 924 | |
| 925 | /* Thread quit pipe has been closed. Killing thread. */ |
| 926 | ret = check_thread_quit_pipe(pollfd, revents); |
| 927 | if (ret) { |
| 928 | goto error; |
| 929 | } |
| 930 | |
| 931 | /* Check for data on kernel pipe */ |
| 932 | if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) { |
| 933 | ret = read(kernel_poll_pipe[0], &tmp, 1); |
| 934 | update_poll_flag = 1; |
| 935 | continue; |
| 936 | } else { |
| 937 | /* |
| 938 | * New CPU detected by the kernel. Adding kernel stream to |
| 939 | * kernel session and updating the kernel consumer |
| 940 | */ |
| 941 | if (revents & LPOLLIN) { |
| 942 | ret = update_kernel_stream(&kconsumer_data, pollfd); |
| 943 | if (ret < 0) { |
| 944 | continue; |
| 945 | } |
| 946 | break; |
| 947 | /* |
| 948 | * TODO: We might want to handle the LPOLLERR | LPOLLHUP |
| 949 | * and unregister kernel stream at this point. |
| 950 | */ |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | error: |
| 957 | DBG("Kernel thread dying"); |
| 958 | close(kernel_poll_pipe[0]); |
| 959 | close(kernel_poll_pipe[1]); |
| 960 | |
| 961 | lttng_poll_clean(&events); |
| 962 | |
| 963 | return NULL; |
| 964 | } |
| 965 | |
| 966 | /* |
| 967 | * This thread manage the consumer error sent back to the session daemon. |
| 968 | */ |
| 969 | static void *thread_manage_consumer(void *data) |
| 970 | { |
| 971 | int sock = 0, i, ret, pollfd; |
| 972 | uint32_t revents, nb_fd; |
| 973 | enum lttcomm_return_code code; |
| 974 | struct lttng_poll_event events; |
| 975 | struct consumer_data *consumer_data = data; |
| 976 | |
| 977 | DBG("[thread] Manage consumer started"); |
| 978 | |
| 979 | ret = lttcomm_listen_unix_sock(consumer_data->err_sock); |
| 980 | if (ret < 0) { |
| 981 | goto error; |
| 982 | } |
| 983 | |
| 984 | /* |
| 985 | * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock. |
| 986 | * Nothing more will be added to this poll set. |
| 987 | */ |
| 988 | ret = create_thread_poll_set(&events, 2); |
| 989 | if (ret < 0) { |
| 990 | goto error; |
| 991 | } |
| 992 | |
| 993 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
| 994 | if (ret < 0) { |
| 995 | goto error; |
| 996 | } |
| 997 | |
| 998 | nb_fd = LTTNG_POLL_GETNB(&events); |
| 999 | |
| 1000 | /* Inifinite blocking call, waiting for transmission */ |
| 1001 | ret = lttng_poll_wait(&events, -1); |
| 1002 | if (ret < 0) { |
| 1003 | goto error; |
| 1004 | } |
| 1005 | |
| 1006 | for (i = 0; i < nb_fd; i++) { |
| 1007 | /* Fetch once the poll data */ |
| 1008 | revents = LTTNG_POLL_GETEV(&events, i); |
| 1009 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1010 | |
| 1011 | /* Thread quit pipe has been closed. Killing thread. */ |
| 1012 | ret = check_thread_quit_pipe(pollfd, revents); |
| 1013 | if (ret) { |
| 1014 | goto error; |
| 1015 | } |
| 1016 | |
| 1017 | /* Event on the registration socket */ |
| 1018 | if (pollfd == consumer_data->err_sock) { |
| 1019 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1020 | ERR("consumer err socket poll error"); |
| 1021 | goto error; |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
| 1027 | if (sock < 0) { |
| 1028 | goto error; |
| 1029 | } |
| 1030 | |
| 1031 | DBG2("Receiving code from consumer err_sock"); |
| 1032 | |
| 1033 | /* Getting status code from kconsumerd */ |
| 1034 | ret = lttcomm_recv_unix_sock(sock, &code, |
| 1035 | sizeof(enum lttcomm_return_code)); |
| 1036 | if (ret <= 0) { |
| 1037 | goto error; |
| 1038 | } |
| 1039 | |
| 1040 | if (code == CONSUMERD_COMMAND_SOCK_READY) { |
| 1041 | consumer_data->cmd_sock = |
| 1042 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); |
| 1043 | if (consumer_data->cmd_sock < 0) { |
| 1044 | sem_post(&consumer_data->sem); |
| 1045 | perror("consumer connect"); |
| 1046 | goto error; |
| 1047 | } |
| 1048 | /* Signal condition to tell that the kconsumerd is ready */ |
| 1049 | sem_post(&consumer_data->sem); |
| 1050 | DBG("consumer command socket ready"); |
| 1051 | } else { |
| 1052 | ERR("consumer error when waiting for SOCK_READY : %s", |
| 1053 | lttcomm_get_readable_code(-code)); |
| 1054 | goto error; |
| 1055 | } |
| 1056 | |
| 1057 | /* Remove the kconsumerd error sock since we've established a connexion */ |
| 1058 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
| 1059 | if (ret < 0) { |
| 1060 | goto error; |
| 1061 | } |
| 1062 | |
| 1063 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
| 1064 | if (ret < 0) { |
| 1065 | goto error; |
| 1066 | } |
| 1067 | |
| 1068 | /* Update number of fd */ |
| 1069 | nb_fd = LTTNG_POLL_GETNB(&events); |
| 1070 | |
| 1071 | /* Inifinite blocking call, waiting for transmission */ |
| 1072 | ret = lttng_poll_wait(&events, -1); |
| 1073 | if (ret < 0) { |
| 1074 | goto error; |
| 1075 | } |
| 1076 | |
| 1077 | for (i = 0; i < nb_fd; i++) { |
| 1078 | /* Fetch once the poll data */ |
| 1079 | revents = LTTNG_POLL_GETEV(&events, i); |
| 1080 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1081 | |
| 1082 | /* Thread quit pipe has been closed. Killing thread. */ |
| 1083 | ret = check_thread_quit_pipe(pollfd, revents); |
| 1084 | if (ret) { |
| 1085 | goto error; |
| 1086 | } |
| 1087 | |
| 1088 | /* Event on the kconsumerd socket */ |
| 1089 | if (pollfd == sock) { |
| 1090 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1091 | ERR("consumer err socket second poll error"); |
| 1092 | goto error; |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | /* Wait for any kconsumerd error */ |
| 1098 | ret = lttcomm_recv_unix_sock(sock, &code, |
| 1099 | sizeof(enum lttcomm_return_code)); |
| 1100 | if (ret <= 0) { |
| 1101 | ERR("consumer closed the command socket"); |
| 1102 | goto error; |
| 1103 | } |
| 1104 | |
| 1105 | ERR("consumer return code : %s", lttcomm_get_readable_code(-code)); |
| 1106 | |
| 1107 | error: |
| 1108 | DBG("consumer thread dying"); |
| 1109 | close(consumer_data->err_sock); |
| 1110 | close(consumer_data->cmd_sock); |
| 1111 | close(sock); |
| 1112 | |
| 1113 | unlink(consumer_data->err_unix_sock_path); |
| 1114 | unlink(consumer_data->cmd_unix_sock_path); |
| 1115 | consumer_data->pid = 0; |
| 1116 | |
| 1117 | lttng_poll_clean(&events); |
| 1118 | |
| 1119 | return NULL; |
| 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | * This thread manage application communication. |
| 1124 | */ |
| 1125 | static void *thread_manage_apps(void *data) |
| 1126 | { |
| 1127 | int i, ret, pollfd; |
| 1128 | uint32_t revents, nb_fd; |
| 1129 | struct ust_command ust_cmd; |
| 1130 | struct lttng_poll_event events; |
| 1131 | |
| 1132 | DBG("[thread] Manage application started"); |
| 1133 | |
| 1134 | ret = create_thread_poll_set(&events, 2); |
| 1135 | if (ret < 0) { |
| 1136 | goto error; |
| 1137 | } |
| 1138 | |
| 1139 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
| 1140 | if (ret < 0) { |
| 1141 | goto error; |
| 1142 | } |
| 1143 | |
| 1144 | while (1) { |
| 1145 | /* Zeroed the events structure */ |
| 1146 | lttng_poll_reset(&events); |
| 1147 | |
| 1148 | nb_fd = LTTNG_POLL_GETNB(&events); |
| 1149 | |
| 1150 | DBG("Apps thread polling on %d fds", nb_fd); |
| 1151 | |
| 1152 | /* Inifinite blocking call, waiting for transmission */ |
| 1153 | ret = lttng_poll_wait(&events, -1); |
| 1154 | if (ret < 0) { |
| 1155 | goto error; |
| 1156 | } |
| 1157 | |
| 1158 | for (i = 0; i < nb_fd; i++) { |
| 1159 | /* Fetch once the poll data */ |
| 1160 | revents = LTTNG_POLL_GETEV(&events, i); |
| 1161 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1162 | |
| 1163 | /* Thread quit pipe has been closed. Killing thread. */ |
| 1164 | ret = check_thread_quit_pipe(pollfd, revents); |
| 1165 | if (ret) { |
| 1166 | goto error; |
| 1167 | } |
| 1168 | |
| 1169 | /* Inspect the apps cmd pipe */ |
| 1170 | if (pollfd == apps_cmd_pipe[0]) { |
| 1171 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1172 | ERR("Apps command pipe error"); |
| 1173 | goto error; |
| 1174 | } else if (revents & LPOLLIN) { |
| 1175 | /* Empty pipe */ |
| 1176 | ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd)); |
| 1177 | if (ret < 0 || ret < sizeof(ust_cmd)) { |
| 1178 | perror("read apps cmd pipe"); |
| 1179 | goto error; |
| 1180 | } |
| 1181 | |
| 1182 | /* Register applicaton to the session daemon */ |
| 1183 | ret = ust_app_register(&ust_cmd.reg_msg, |
| 1184 | ust_cmd.sock); |
| 1185 | if (ret < 0) { |
| 1186 | /* Only critical ENOMEM error can be returned here */ |
| 1187 | goto error; |
| 1188 | } |
| 1189 | |
| 1190 | ret = ustctl_register_done(ust_cmd.sock); |
| 1191 | if (ret < 0) { |
| 1192 | /* |
| 1193 | * If the registration is not possible, we simply |
| 1194 | * unregister the apps and continue |
| 1195 | */ |
| 1196 | ust_app_unregister(ust_cmd.sock); |
| 1197 | } else { |
| 1198 | /* |
| 1199 | * We just need here to monitor the close of the UST |
| 1200 | * socket and poll set monitor those by default. |
| 1201 | */ |
| 1202 | ret = lttng_poll_add(&events, ust_cmd.sock, 0); |
| 1203 | if (ret < 0) { |
| 1204 | goto error; |
| 1205 | } |
| 1206 | |
| 1207 | DBG("Apps with sock %d added to poll set", |
| 1208 | ust_cmd.sock); |
| 1209 | } |
| 1210 | break; |
| 1211 | } |
| 1212 | } else { |
| 1213 | /* |
| 1214 | * At this point, we know that a registered application made |
| 1215 | * the event at poll_wait. |
| 1216 | */ |
| 1217 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1218 | /* Removing from the poll set */ |
| 1219 | ret = lttng_poll_del(&events, pollfd); |
| 1220 | if (ret < 0) { |
| 1221 | goto error; |
| 1222 | } |
| 1223 | |
| 1224 | /* Socket closed */ |
| 1225 | ust_app_unregister(pollfd); |
| 1226 | break; |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | error: |
| 1233 | DBG("Application communication apps dying"); |
| 1234 | close(apps_cmd_pipe[0]); |
| 1235 | close(apps_cmd_pipe[1]); |
| 1236 | |
| 1237 | lttng_poll_clean(&events); |
| 1238 | |
| 1239 | return NULL; |
| 1240 | } |
| 1241 | |
| 1242 | /* |
| 1243 | * Dispatch request from the registration threads to the application |
| 1244 | * communication thread. |
| 1245 | */ |
| 1246 | static void *thread_dispatch_ust_registration(void *data) |
| 1247 | { |
| 1248 | int ret; |
| 1249 | struct cds_wfq_node *node; |
| 1250 | struct ust_command *ust_cmd = NULL; |
| 1251 | |
| 1252 | DBG("[thread] Dispatch UST command started"); |
| 1253 | |
| 1254 | while (!dispatch_thread_exit) { |
| 1255 | /* Atomically prepare the queue futex */ |
| 1256 | futex_nto1_prepare(&ust_cmd_queue.futex); |
| 1257 | |
| 1258 | do { |
| 1259 | /* Dequeue command for registration */ |
| 1260 | node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue); |
| 1261 | if (node == NULL) { |
| 1262 | DBG("Woken up but nothing in the UST command queue"); |
| 1263 | /* Continue thread execution */ |
| 1264 | break; |
| 1265 | } |
| 1266 | |
| 1267 | ust_cmd = caa_container_of(node, struct ust_command, node); |
| 1268 | |
| 1269 | DBG("Dispatching UST registration pid:%d ppid:%d uid:%d" |
| 1270 | " gid:%d sock:%d name:%s (version %d.%d)", |
| 1271 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, |
| 1272 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, |
| 1273 | ust_cmd->sock, ust_cmd->reg_msg.name, |
| 1274 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); |
| 1275 | /* |
| 1276 | * Inform apps thread of the new application registration. This |
| 1277 | * call is blocking so we can be assured that the data will be read |
| 1278 | * at some point in time or wait to the end of the world :) |
| 1279 | */ |
| 1280 | ret = write(apps_cmd_pipe[1], ust_cmd, |
| 1281 | sizeof(struct ust_command)); |
| 1282 | if (ret < 0) { |
| 1283 | perror("write apps cmd pipe"); |
| 1284 | if (errno == EBADF) { |
| 1285 | /* |
| 1286 | * We can't inform the application thread to process |
| 1287 | * registration. We will exit or else application |
| 1288 | * registration will not occur and tracing will never |
| 1289 | * start. |
| 1290 | */ |
| 1291 | goto error; |
| 1292 | } |
| 1293 | } |
| 1294 | free(ust_cmd); |
| 1295 | } while (node != NULL); |
| 1296 | |
| 1297 | /* Futex wait on queue. Blocking call on futex() */ |
| 1298 | futex_nto1_wait(&ust_cmd_queue.futex); |
| 1299 | } |
| 1300 | |
| 1301 | error: |
| 1302 | DBG("Dispatch thread dying"); |
| 1303 | return NULL; |
| 1304 | } |
| 1305 | |
| 1306 | /* |
| 1307 | * This thread manage application registration. |
| 1308 | */ |
| 1309 | static void *thread_registration_apps(void *data) |
| 1310 | { |
| 1311 | int sock = 0, i, ret, pollfd; |
| 1312 | uint32_t revents, nb_fd; |
| 1313 | struct lttng_poll_event events; |
| 1314 | /* |
| 1315 | * Get allocated in this thread, enqueued to a global queue, dequeued and |
| 1316 | * freed in the manage apps thread. |
| 1317 | */ |
| 1318 | struct ust_command *ust_cmd = NULL; |
| 1319 | |
| 1320 | DBG("[thread] Manage application registration started"); |
| 1321 | |
| 1322 | ret = lttcomm_listen_unix_sock(apps_sock); |
| 1323 | if (ret < 0) { |
| 1324 | goto error; |
| 1325 | } |
| 1326 | |
| 1327 | /* |
| 1328 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing |
| 1329 | * more will be added to this poll set. |
| 1330 | */ |
| 1331 | ret = create_thread_poll_set(&events, 2); |
| 1332 | if (ret < 0) { |
| 1333 | goto error; |
| 1334 | } |
| 1335 | |
| 1336 | /* Add the application registration socket */ |
| 1337 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); |
| 1338 | if (ret < 0) { |
| 1339 | goto error; |
| 1340 | } |
| 1341 | |
| 1342 | /* Notify all applications to register */ |
| 1343 | ret = notify_ust_apps(1); |
| 1344 | if (ret < 0) { |
| 1345 | ERR("Failed to notify applications or create the wait shared memory.\n" |
| 1346 | "Execution continues but there might be problem for already\n" |
| 1347 | "running applications that wishes to register."); |
| 1348 | } |
| 1349 | |
| 1350 | while (1) { |
| 1351 | DBG("Accepting application registration"); |
| 1352 | |
| 1353 | nb_fd = LTTNG_POLL_GETNB(&events); |
| 1354 | |
| 1355 | /* Inifinite blocking call, waiting for transmission */ |
| 1356 | ret = lttng_poll_wait(&events, -1); |
| 1357 | if (ret < 0) { |
| 1358 | goto error; |
| 1359 | } |
| 1360 | |
| 1361 | for (i = 0; i < nb_fd; i++) { |
| 1362 | /* Fetch once the poll data */ |
| 1363 | revents = LTTNG_POLL_GETEV(&events, i); |
| 1364 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1365 | |
| 1366 | /* Thread quit pipe has been closed. Killing thread. */ |
| 1367 | ret = check_thread_quit_pipe(pollfd, revents); |
| 1368 | if (ret) { |
| 1369 | goto error; |
| 1370 | } |
| 1371 | |
| 1372 | /* Event on the registration socket */ |
| 1373 | if (pollfd == apps_sock) { |
| 1374 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1375 | ERR("Register apps socket poll error"); |
| 1376 | goto error; |
| 1377 | } else if (revents & LPOLLIN) { |
| 1378 | sock = lttcomm_accept_unix_sock(apps_sock); |
| 1379 | if (sock < 0) { |
| 1380 | goto error; |
| 1381 | } |
| 1382 | |
| 1383 | /* Create UST registration command for enqueuing */ |
| 1384 | ust_cmd = malloc(sizeof(struct ust_command)); |
| 1385 | if (ust_cmd == NULL) { |
| 1386 | perror("ust command malloc"); |
| 1387 | goto error; |
| 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | * Using message-based transmissions to ensure we don't |
| 1392 | * have to deal with partially received messages. |
| 1393 | */ |
| 1394 | ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg, |
| 1395 | sizeof(struct ust_register_msg)); |
| 1396 | if (ret < 0 || ret < sizeof(struct ust_register_msg)) { |
| 1397 | if (ret < 0) { |
| 1398 | perror("lttcomm_recv_unix_sock register apps"); |
| 1399 | } else { |
| 1400 | ERR("Wrong size received on apps register"); |
| 1401 | } |
| 1402 | free(ust_cmd); |
| 1403 | close(sock); |
| 1404 | continue; |
| 1405 | } |
| 1406 | |
| 1407 | ust_cmd->sock = sock; |
| 1408 | |
| 1409 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
| 1410 | " gid:%d sock:%d name:%s (version %d.%d)", |
| 1411 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, |
| 1412 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, |
| 1413 | ust_cmd->sock, ust_cmd->reg_msg.name, |
| 1414 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); |
| 1415 | |
| 1416 | /* |
| 1417 | * Lock free enqueue the registration request. The red pill |
| 1418 | * has been taken! This apps will be part of the *system*. |
| 1419 | */ |
| 1420 | cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node); |
| 1421 | |
| 1422 | /* |
| 1423 | * Wake the registration queue futex. Implicit memory |
| 1424 | * barrier with the exchange in cds_wfq_enqueue. |
| 1425 | */ |
| 1426 | futex_nto1_wake(&ust_cmd_queue.futex); |
| 1427 | } |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | error: |
| 1433 | DBG("UST Registration thread dying"); |
| 1434 | |
| 1435 | /* Notify that the registration thread is gone */ |
| 1436 | notify_ust_apps(0); |
| 1437 | |
| 1438 | close(apps_sock); |
| 1439 | close(sock); |
| 1440 | unlink(apps_unix_sock_path); |
| 1441 | |
| 1442 | lttng_poll_clean(&events); |
| 1443 | |
| 1444 | return NULL; |
| 1445 | } |
| 1446 | |
| 1447 | /* |
| 1448 | * Start the thread_manage_consumer. This must be done after a lttng-consumerd |
| 1449 | * exec or it will fails. |
| 1450 | */ |
| 1451 | static int spawn_consumer_thread(struct consumer_data *consumer_data) |
| 1452 | { |
| 1453 | int ret; |
| 1454 | struct timespec timeout; |
| 1455 | |
| 1456 | timeout.tv_sec = DEFAULT_SEM_WAIT_TIMEOUT; |
| 1457 | timeout.tv_nsec = 0; |
| 1458 | |
| 1459 | /* Setup semaphore */ |
| 1460 | ret = sem_init(&consumer_data->sem, 0, 0); |
| 1461 | if (ret < 0) { |
| 1462 | PERROR("sem_init consumer semaphore"); |
| 1463 | goto error; |
| 1464 | } |
| 1465 | |
| 1466 | ret = pthread_create(&consumer_data->thread, NULL, |
| 1467 | thread_manage_consumer, consumer_data); |
| 1468 | if (ret != 0) { |
| 1469 | PERROR("pthread_create consumer"); |
| 1470 | ret = -1; |
| 1471 | goto error; |
| 1472 | } |
| 1473 | |
| 1474 | /* Get time for sem_timedwait absolute timeout */ |
| 1475 | ret = clock_gettime(CLOCK_REALTIME, &timeout); |
| 1476 | if (ret < 0) { |
| 1477 | PERROR("clock_gettime spawn consumer"); |
| 1478 | /* Infinite wait for the kconsumerd thread to be ready */ |
| 1479 | ret = sem_wait(&consumer_data->sem); |
| 1480 | } else { |
| 1481 | /* Normal timeout if the gettime was successful */ |
| 1482 | timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; |
| 1483 | ret = sem_timedwait(&consumer_data->sem, &timeout); |
| 1484 | } |
| 1485 | |
| 1486 | if (ret < 0) { |
| 1487 | if (errno == ETIMEDOUT) { |
| 1488 | /* |
| 1489 | * Call has timed out so we kill the kconsumerd_thread and return |
| 1490 | * an error. |
| 1491 | */ |
| 1492 | ERR("The consumer thread was never ready. Killing it"); |
| 1493 | ret = pthread_cancel(consumer_data->thread); |
| 1494 | if (ret < 0) { |
| 1495 | PERROR("pthread_cancel consumer thread"); |
| 1496 | } |
| 1497 | } else { |
| 1498 | PERROR("semaphore wait failed consumer thread"); |
| 1499 | } |
| 1500 | goto error; |
| 1501 | } |
| 1502 | |
| 1503 | pthread_mutex_lock(&consumer_data->pid_mutex); |
| 1504 | if (consumer_data->pid == 0) { |
| 1505 | ERR("Kconsumerd did not start"); |
| 1506 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 1507 | goto error; |
| 1508 | } |
| 1509 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 1510 | |
| 1511 | return 0; |
| 1512 | |
| 1513 | error: |
| 1514 | return ret; |
| 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | * Join consumer thread |
| 1519 | */ |
| 1520 | static int join_consumer_thread(struct consumer_data *consumer_data) |
| 1521 | { |
| 1522 | void *status; |
| 1523 | int ret; |
| 1524 | |
| 1525 | if (consumer_data->pid != 0) { |
| 1526 | ret = kill(consumer_data->pid, SIGTERM); |
| 1527 | if (ret) { |
| 1528 | ERR("Error killing consumer daemon"); |
| 1529 | return ret; |
| 1530 | } |
| 1531 | return pthread_join(consumer_data->thread, &status); |
| 1532 | } else { |
| 1533 | return 0; |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | /* |
| 1538 | * Fork and exec a consumer daemon (consumerd). |
| 1539 | * |
| 1540 | * Return pid if successful else -1. |
| 1541 | */ |
| 1542 | static pid_t spawn_consumerd(struct consumer_data *consumer_data) |
| 1543 | { |
| 1544 | int ret; |
| 1545 | pid_t pid; |
| 1546 | const char *verbosity; |
| 1547 | |
| 1548 | DBG("Spawning consumerd"); |
| 1549 | |
| 1550 | pid = fork(); |
| 1551 | if (pid == 0) { |
| 1552 | /* |
| 1553 | * Exec consumerd. |
| 1554 | */ |
| 1555 | if (opt_verbose > 1 || opt_verbose_consumer) { |
| 1556 | verbosity = "--verbose"; |
| 1557 | } else { |
| 1558 | verbosity = "--quiet"; |
| 1559 | } |
| 1560 | switch (consumer_data->type) { |
| 1561 | case LTTNG_CONSUMER_KERNEL: |
| 1562 | execl(INSTALL_BIN_PATH "/lttng-consumerd", |
| 1563 | "lttng-consumerd", verbosity, "-k", NULL); |
| 1564 | break; |
| 1565 | case LTTNG_CONSUMER_UST: |
| 1566 | execl(INSTALL_BIN_PATH "/lttng-consumerd", |
| 1567 | "lttng-consumerd", verbosity, "-u", NULL); |
| 1568 | break; |
| 1569 | default: |
| 1570 | perror("unknown consumer type"); |
| 1571 | exit(EXIT_FAILURE); |
| 1572 | } |
| 1573 | if (errno != 0) { |
| 1574 | perror("kernel start consumer exec"); |
| 1575 | } |
| 1576 | exit(EXIT_FAILURE); |
| 1577 | } else if (pid > 0) { |
| 1578 | ret = pid; |
| 1579 | } else { |
| 1580 | perror("start consumer fork"); |
| 1581 | ret = -errno; |
| 1582 | } |
| 1583 | return ret; |
| 1584 | } |
| 1585 | |
| 1586 | /* |
| 1587 | * Spawn the consumerd daemon and session daemon thread. |
| 1588 | */ |
| 1589 | static int start_consumerd(struct consumer_data *consumer_data) |
| 1590 | { |
| 1591 | int ret; |
| 1592 | |
| 1593 | pthread_mutex_lock(&consumer_data->pid_mutex); |
| 1594 | if (consumer_data->pid != 0) { |
| 1595 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 1596 | goto end; |
| 1597 | } |
| 1598 | |
| 1599 | ret = spawn_consumerd(consumer_data); |
| 1600 | if (ret < 0) { |
| 1601 | ERR("Spawning consumerd failed"); |
| 1602 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 1603 | goto error; |
| 1604 | } |
| 1605 | |
| 1606 | /* Setting up the consumer_data pid */ |
| 1607 | consumer_data->pid = ret; |
| 1608 | DBG2("consumer pid %d", consumer_data->pid); |
| 1609 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
| 1610 | |
| 1611 | DBG2("Spawning consumer control thread"); |
| 1612 | ret = spawn_consumer_thread(consumer_data); |
| 1613 | if (ret < 0) { |
| 1614 | ERR("Fatal error spawning consumer control thread"); |
| 1615 | goto error; |
| 1616 | } |
| 1617 | |
| 1618 | end: |
| 1619 | return 0; |
| 1620 | |
| 1621 | error: |
| 1622 | return ret; |
| 1623 | } |
| 1624 | |
| 1625 | /* |
| 1626 | * modprobe_kernel_modules |
| 1627 | */ |
| 1628 | static int modprobe_kernel_modules(void) |
| 1629 | { |
| 1630 | int ret = 0, i; |
| 1631 | char modprobe[256]; |
| 1632 | |
| 1633 | for (i = 0; i < ARRAY_SIZE(kernel_modules_list); i++) { |
| 1634 | ret = snprintf(modprobe, sizeof(modprobe), |
| 1635 | "/sbin/modprobe %s%s", |
| 1636 | kernel_modules_list[i].required ? "" : "--quiet ", |
| 1637 | kernel_modules_list[i].name); |
| 1638 | if (ret < 0) { |
| 1639 | perror("snprintf modprobe"); |
| 1640 | goto error; |
| 1641 | } |
| 1642 | modprobe[sizeof(modprobe) - 1] = '\0'; |
| 1643 | ret = system(modprobe); |
| 1644 | if (ret == -1) { |
| 1645 | ERR("Unable to launch modprobe for module %s", |
| 1646 | kernel_modules_list[i].name); |
| 1647 | } else if (kernel_modules_list[i].required |
| 1648 | && WEXITSTATUS(ret) != 0) { |
| 1649 | ERR("Unable to load module %s", |
| 1650 | kernel_modules_list[i].name); |
| 1651 | } else { |
| 1652 | DBG("Modprobe successfully %s", |
| 1653 | kernel_modules_list[i].name); |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | error: |
| 1658 | return ret; |
| 1659 | } |
| 1660 | |
| 1661 | /* |
| 1662 | * mount_debugfs |
| 1663 | */ |
| 1664 | static int mount_debugfs(char *path) |
| 1665 | { |
| 1666 | int ret; |
| 1667 | char *type = "debugfs"; |
| 1668 | |
| 1669 | ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid()); |
| 1670 | if (ret < 0) { |
| 1671 | PERROR("Cannot create debugfs path"); |
| 1672 | goto error; |
| 1673 | } |
| 1674 | |
| 1675 | ret = mount(type, path, type, 0, NULL); |
| 1676 | if (ret < 0) { |
| 1677 | PERROR("Cannot mount debugfs"); |
| 1678 | goto error; |
| 1679 | } |
| 1680 | |
| 1681 | DBG("Mounted debugfs successfully at %s", path); |
| 1682 | |
| 1683 | error: |
| 1684 | return ret; |
| 1685 | } |
| 1686 | |
| 1687 | /* |
| 1688 | * Setup necessary data for kernel tracer action. |
| 1689 | */ |
| 1690 | static void init_kernel_tracer(void) |
| 1691 | { |
| 1692 | int ret; |
| 1693 | char *proc_mounts = "/proc/mounts"; |
| 1694 | char line[256]; |
| 1695 | char *debugfs_path = NULL, *lttng_path = NULL; |
| 1696 | FILE *fp; |
| 1697 | |
| 1698 | /* Detect debugfs */ |
| 1699 | fp = fopen(proc_mounts, "r"); |
| 1700 | if (fp == NULL) { |
| 1701 | ERR("Unable to probe %s", proc_mounts); |
| 1702 | goto error; |
| 1703 | } |
| 1704 | |
| 1705 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 1706 | if (strstr(line, "debugfs") != NULL) { |
| 1707 | /* Remove first string */ |
| 1708 | strtok(line, " "); |
| 1709 | /* Dup string here so we can reuse line later on */ |
| 1710 | debugfs_path = strdup(strtok(NULL, " ")); |
| 1711 | DBG("Got debugfs path : %s", debugfs_path); |
| 1712 | break; |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | fclose(fp); |
| 1717 | |
| 1718 | /* Mount debugfs if needded */ |
| 1719 | if (debugfs_path == NULL) { |
| 1720 | ret = asprintf(&debugfs_path, "/mnt/debugfs"); |
| 1721 | if (ret < 0) { |
| 1722 | perror("asprintf debugfs path"); |
| 1723 | goto error; |
| 1724 | } |
| 1725 | ret = mount_debugfs(debugfs_path); |
| 1726 | if (ret < 0) { |
| 1727 | perror("Cannot mount debugfs"); |
| 1728 | goto error; |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | /* Modprobe lttng kernel modules */ |
| 1733 | ret = modprobe_kernel_modules(); |
| 1734 | if (ret < 0) { |
| 1735 | goto error; |
| 1736 | } |
| 1737 | |
| 1738 | /* Setup lttng kernel path */ |
| 1739 | ret = asprintf(<tng_path, "%s/lttng", debugfs_path); |
| 1740 | if (ret < 0) { |
| 1741 | perror("asprintf lttng path"); |
| 1742 | goto error; |
| 1743 | } |
| 1744 | |
| 1745 | /* Open debugfs lttng */ |
| 1746 | kernel_tracer_fd = open(lttng_path, O_RDWR); |
| 1747 | if (kernel_tracer_fd < 0) { |
| 1748 | DBG("Failed to open %s", lttng_path); |
| 1749 | goto error; |
| 1750 | } |
| 1751 | |
| 1752 | free(lttng_path); |
| 1753 | free(debugfs_path); |
| 1754 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
| 1755 | return; |
| 1756 | |
| 1757 | error: |
| 1758 | if (lttng_path) { |
| 1759 | free(lttng_path); |
| 1760 | } |
| 1761 | if (debugfs_path) { |
| 1762 | free(debugfs_path); |
| 1763 | } |
| 1764 | WARN("No kernel tracer available"); |
| 1765 | kernel_tracer_fd = 0; |
| 1766 | return; |
| 1767 | } |
| 1768 | |
| 1769 | /* |
| 1770 | * Init tracing by creating trace directory and sending fds kernel consumer. |
| 1771 | */ |
| 1772 | static int init_kernel_tracing(struct ltt_kernel_session *session) |
| 1773 | { |
| 1774 | int ret = 0; |
| 1775 | |
| 1776 | if (session->consumer_fds_sent == 0) { |
| 1777 | /* |
| 1778 | * Assign default kernel consumer socket if no consumer assigned to the |
| 1779 | * kernel session. At this point, it's NOT suppose to be 0 but this is |
| 1780 | * an extra security check. |
| 1781 | */ |
| 1782 | if (session->consumer_fd == 0) { |
| 1783 | session->consumer_fd = kconsumer_data.cmd_sock; |
| 1784 | } |
| 1785 | |
| 1786 | ret = send_kconsumer_session_streams(&kconsumer_data, session); |
| 1787 | if (ret < 0) { |
| 1788 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
| 1789 | goto error; |
| 1790 | } |
| 1791 | |
| 1792 | session->consumer_fds_sent = 1; |
| 1793 | } |
| 1794 | |
| 1795 | error: |
| 1796 | return ret; |
| 1797 | } |
| 1798 | |
| 1799 | /* |
| 1800 | * Init tracing by creating trace directory and sending fds ust consumer. |
| 1801 | */ |
| 1802 | static int init_ust_tracing(struct ltt_ust_session *session) |
| 1803 | { |
| 1804 | int ret = 0; |
| 1805 | |
| 1806 | if (session->consumer_fds_sent == 0) { |
| 1807 | /* |
| 1808 | * Assign default ust consumer socket if no consumer assigned to the |
| 1809 | * ust session. At this point, it's NOT suppose to be 0 but this is |
| 1810 | * an extra security check. |
| 1811 | */ |
| 1812 | if (session->consumer_fd == 0) { |
| 1813 | session->consumer_fd = ustconsumer_data.cmd_sock; |
| 1814 | } |
| 1815 | |
| 1816 | ret = send_ustconsumer_session_streams(&ustconsumer_data, session); |
| 1817 | if (ret < 0) { |
| 1818 | ret = LTTCOMM_UST_CONSUMER_FAIL; |
| 1819 | goto error; |
| 1820 | } |
| 1821 | |
| 1822 | session->consumer_fds_sent = 1; |
| 1823 | } |
| 1824 | |
| 1825 | error: |
| 1826 | return ret; |
| 1827 | } |
| 1828 | |
| 1829 | /* |
| 1830 | * Create an UST session and add it to the session ust list. |
| 1831 | */ |
| 1832 | static int create_ust_session(struct ltt_session *session, |
| 1833 | struct lttng_domain *domain) |
| 1834 | { |
| 1835 | int ret; |
| 1836 | struct ltt_ust_session *lus = NULL; |
| 1837 | struct ust_app *app; |
| 1838 | |
| 1839 | switch (domain->type) { |
| 1840 | case LTTNG_DOMAIN_UST_PID: |
| 1841 | app = ust_app_get_by_pid(domain->attr.pid); |
| 1842 | if (app == NULL) { |
| 1843 | ret = LTTCOMM_APP_NOT_FOUND; |
| 1844 | goto error; |
| 1845 | } |
| 1846 | break; |
| 1847 | default: |
| 1848 | ret = LTTCOMM_UNKNOWN_DOMAIN; |
| 1849 | goto error; |
| 1850 | } |
| 1851 | |
| 1852 | DBG("Creating UST session"); |
| 1853 | |
| 1854 | lus = trace_ust_create_session(session->path, domain->attr.pid, domain); |
| 1855 | if (lus == NULL) { |
| 1856 | ret = LTTCOMM_UST_SESS_FAIL; |
| 1857 | goto error; |
| 1858 | } |
| 1859 | |
| 1860 | ret = mkdir_recursive(lus->path, S_IRWXU | S_IRWXG, |
| 1861 | geteuid(), allowed_group()); |
| 1862 | if (ret < 0) { |
| 1863 | if (ret != -EEXIST) { |
| 1864 | ERR("Trace directory creation error"); |
| 1865 | ret = LTTCOMM_UST_SESS_FAIL; |
| 1866 | goto error; |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | /* Create session on the UST tracer */ |
| 1871 | ret = ustctl_create_session(app->sock); |
| 1872 | if (ret < 0) { |
| 1873 | ret = LTTCOMM_UST_SESS_FAIL; |
| 1874 | goto error; |
| 1875 | } |
| 1876 | lus->handle = ret; |
| 1877 | lus->sock = app->sock; |
| 1878 | |
| 1879 | cds_list_add(&lus->list, &session->ust_session_list.head); |
| 1880 | session->ust_session_list.count++; |
| 1881 | |
| 1882 | return LTTCOMM_OK; |
| 1883 | |
| 1884 | error: |
| 1885 | free(lus); |
| 1886 | return ret; |
| 1887 | } |
| 1888 | |
| 1889 | /* |
| 1890 | * Create a kernel tracer session then create the default channel. |
| 1891 | */ |
| 1892 | static int create_kernel_session(struct ltt_session *session) |
| 1893 | { |
| 1894 | int ret; |
| 1895 | |
| 1896 | DBG("Creating kernel session"); |
| 1897 | |
| 1898 | ret = kernel_create_session(session, kernel_tracer_fd); |
| 1899 | if (ret < 0) { |
| 1900 | ret = LTTCOMM_KERN_SESS_FAIL; |
| 1901 | goto error; |
| 1902 | } |
| 1903 | |
| 1904 | /* Set kernel consumer socket fd */ |
| 1905 | if (kconsumer_data.cmd_sock) { |
| 1906 | session->kernel_session->consumer_fd = kconsumer_data.cmd_sock; |
| 1907 | } |
| 1908 | |
| 1909 | ret = mkdir_recursive(session->kernel_session->trace_path, |
| 1910 | S_IRWXU | S_IRWXG, geteuid(), allowed_group()); |
| 1911 | if (ret < 0) { |
| 1912 | if (ret != -EEXIST) { |
| 1913 | ERR("Trace directory creation error"); |
| 1914 | goto error; |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | error: |
| 1919 | return ret; |
| 1920 | } |
| 1921 | |
| 1922 | /* |
| 1923 | * Using the session list, filled a lttng_session array to send back to the |
| 1924 | * client for session listing. |
| 1925 | * |
| 1926 | * The session list lock MUST be acquired before calling this function. Use |
| 1927 | * session_lock_list() and session_unlock_list(). |
| 1928 | */ |
| 1929 | static void list_lttng_sessions(struct lttng_session *sessions) |
| 1930 | { |
| 1931 | int i = 0; |
| 1932 | struct ltt_session *session; |
| 1933 | |
| 1934 | DBG("Getting all available session"); |
| 1935 | /* |
| 1936 | * Iterate over session list and append data after the control struct in |
| 1937 | * the buffer. |
| 1938 | */ |
| 1939 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
| 1940 | strncpy(sessions[i].path, session->path, PATH_MAX); |
| 1941 | sessions[i].path[PATH_MAX - 1] = '\0'; |
| 1942 | strncpy(sessions[i].name, session->name, NAME_MAX); |
| 1943 | sessions[i].name[NAME_MAX - 1] = '\0'; |
| 1944 | i++; |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | /* |
| 1949 | * Fill lttng_channel array of all channels. |
| 1950 | */ |
| 1951 | static void list_lttng_channels(struct ltt_session *session, |
| 1952 | struct lttng_channel *channels) |
| 1953 | { |
| 1954 | int i = 0; |
| 1955 | struct ltt_kernel_channel *kchan; |
| 1956 | |
| 1957 | DBG("Listing channels for session %s", session->name); |
| 1958 | |
| 1959 | /* Kernel channels */ |
| 1960 | if (session->kernel_session != NULL) { |
| 1961 | cds_list_for_each_entry(kchan, |
| 1962 | &session->kernel_session->channel_list.head, list) { |
| 1963 | /* Copy lttng_channel struct to array */ |
| 1964 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); |
| 1965 | channels[i].enabled = kchan->enabled; |
| 1966 | i++; |
| 1967 | } |
| 1968 | } |
| 1969 | |
| 1970 | /* TODO: Missing UST listing */ |
| 1971 | } |
| 1972 | |
| 1973 | /* |
| 1974 | * Fill lttng_event array of all events in the channel. |
| 1975 | */ |
| 1976 | static void list_lttng_events(struct ltt_kernel_channel *kchan, |
| 1977 | struct lttng_event *events) |
| 1978 | { |
| 1979 | /* |
| 1980 | * TODO: This is ONLY kernel. Need UST support. |
| 1981 | */ |
| 1982 | int i = 0; |
| 1983 | struct ltt_kernel_event *event; |
| 1984 | |
| 1985 | DBG("Listing events for channel %s", kchan->channel->name); |
| 1986 | |
| 1987 | /* Kernel channels */ |
| 1988 | cds_list_for_each_entry(event, &kchan->events_list.head , list) { |
| 1989 | strncpy(events[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); |
| 1990 | events[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 1991 | events[i].enabled = event->enabled; |
| 1992 | switch (event->event->instrumentation) { |
| 1993 | case LTTNG_KERNEL_TRACEPOINT: |
| 1994 | events[i].type = LTTNG_EVENT_TRACEPOINT; |
| 1995 | break; |
| 1996 | case LTTNG_KERNEL_KPROBE: |
| 1997 | case LTTNG_KERNEL_KRETPROBE: |
| 1998 | events[i].type = LTTNG_EVENT_PROBE; |
| 1999 | memcpy(&events[i].attr.probe, &event->event->u.kprobe, |
| 2000 | sizeof(struct lttng_kernel_kprobe)); |
| 2001 | break; |
| 2002 | case LTTNG_KERNEL_FUNCTION: |
| 2003 | events[i].type = LTTNG_EVENT_FUNCTION; |
| 2004 | memcpy(&events[i].attr.ftrace, &event->event->u.ftrace, |
| 2005 | sizeof(struct lttng_kernel_function)); |
| 2006 | break; |
| 2007 | case LTTNG_KERNEL_NOOP: |
| 2008 | events[i].type = LTTNG_EVENT_NOOP; |
| 2009 | break; |
| 2010 | case LTTNG_KERNEL_SYSCALL: |
| 2011 | events[i].type = LTTNG_EVENT_SYSCALL; |
| 2012 | break; |
| 2013 | case LTTNG_KERNEL_ALL: |
| 2014 | assert(0); |
| 2015 | break; |
| 2016 | } |
| 2017 | i++; |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | /* |
| 2022 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. |
| 2023 | */ |
| 2024 | static int cmd_disable_channel(struct ltt_session *session, |
| 2025 | int domain, char *channel_name) |
| 2026 | { |
| 2027 | int ret; |
| 2028 | |
| 2029 | switch (domain) { |
| 2030 | case LTTNG_DOMAIN_KERNEL: |
| 2031 | ret = channel_kernel_disable(session->kernel_session, |
| 2032 | channel_name); |
| 2033 | if (ret != LTTCOMM_OK) { |
| 2034 | goto error; |
| 2035 | } |
| 2036 | |
| 2037 | kernel_wait_quiescent(kernel_tracer_fd); |
| 2038 | break; |
| 2039 | case LTTNG_DOMAIN_UST_PID: |
| 2040 | break; |
| 2041 | default: |
| 2042 | ret = LTTCOMM_UNKNOWN_DOMAIN; |
| 2043 | goto error; |
| 2044 | } |
| 2045 | |
| 2046 | ret = LTTCOMM_OK; |
| 2047 | |
| 2048 | error: |
| 2049 | return ret; |
| 2050 | } |
| 2051 | |
| 2052 | /* |
| 2053 | * Copy channel from attributes and set it in the application channel list. |
| 2054 | */ |
| 2055 | static int copy_ust_channel_to_app(struct ltt_ust_session *usess, |
| 2056 | struct lttng_channel *attr, struct ust_app *app) |
| 2057 | { |
| 2058 | int ret; |
| 2059 | struct ltt_ust_channel *uchan, *new_chan; |
| 2060 | |
| 2061 | uchan = trace_ust_get_channel_by_name(attr->name, usess); |
| 2062 | if (uchan == NULL) { |
| 2063 | ret = LTTCOMM_FATAL; |
| 2064 | goto error; |
| 2065 | } |
| 2066 | |
| 2067 | new_chan = trace_ust_create_channel(attr, usess->path); |
| 2068 | if (new_chan == NULL) { |
| 2069 | PERROR("malloc ltt_ust_channel"); |
| 2070 | ret = LTTCOMM_FATAL; |
| 2071 | goto error; |
| 2072 | } |
| 2073 | |
| 2074 | ret = channel_ust_copy(new_chan, uchan); |
| 2075 | if (ret < 0) { |
| 2076 | ret = LTTCOMM_FATAL; |
| 2077 | goto error; |
| 2078 | } |
| 2079 | |
| 2080 | /* Add channel to the ust app channel list */ |
| 2081 | cds_list_add(&new_chan->list, &app->channels.head); |
| 2082 | app->channels.count++; |
| 2083 | |
| 2084 | error: |
| 2085 | return ret; |
| 2086 | } |
| 2087 | |
| 2088 | /* |
| 2089 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. |
| 2090 | */ |
| 2091 | static int cmd_enable_channel(struct ltt_session *session, |
| 2092 | struct lttng_domain *domain, struct lttng_channel *attr) |
| 2093 | { |
| 2094 | int ret; |
| 2095 | |
| 2096 | switch (domain->type) { |
| 2097 | case LTTNG_DOMAIN_KERNEL: |
| 2098 | { |
| 2099 | struct ltt_kernel_channel *kchan; |
| 2100 | |
| 2101 | kchan = trace_kernel_get_channel_by_name(attr->name, |
| 2102 | session->kernel_session); |
| 2103 | if (kchan == NULL) { |
| 2104 | ret = channel_kernel_create(session->kernel_session, |
| 2105 | attr, kernel_poll_pipe[1]); |
| 2106 | } else { |
| 2107 | ret = channel_kernel_enable(session->kernel_session, kchan); |
| 2108 | } |
| 2109 | |
| 2110 | if (ret != LTTCOMM_OK) { |
| 2111 | goto error; |
| 2112 | } |
| 2113 | |
| 2114 | kernel_wait_quiescent(kernel_tracer_fd); |
| 2115 | break; |
| 2116 | } |
| 2117 | case LTTNG_DOMAIN_UST_PID: |
| 2118 | { |
| 2119 | int sock; |
| 2120 | struct ltt_ust_channel *uchan; |
| 2121 | struct ltt_ust_session *usess; |
| 2122 | struct ust_app *app; |
| 2123 | |
| 2124 | usess = trace_ust_get_session_by_pid(&session->ust_session_list, |
| 2125 | domain->attr.pid); |
| 2126 | if (usess == NULL) { |
| 2127 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; |
| 2128 | goto error; |
| 2129 | } |
| 2130 | |
| 2131 | app = ust_app_get_by_pid(domain->attr.pid); |
| 2132 | if (app == NULL) { |
| 2133 | ret = LTTCOMM_APP_NOT_FOUND; |
| 2134 | goto error; |
| 2135 | } |
| 2136 | sock = app->sock; |
| 2137 | |
| 2138 | uchan = trace_ust_get_channel_by_name(attr->name, usess); |
| 2139 | if (uchan == NULL) { |
| 2140 | ret = channel_ust_create(usess, attr, sock); |
| 2141 | } else { |
| 2142 | ret = channel_ust_enable(usess, uchan, sock); |
| 2143 | } |
| 2144 | |
| 2145 | if (ret != LTTCOMM_OK) { |
| 2146 | goto error; |
| 2147 | } |
| 2148 | |
| 2149 | ret = copy_ust_channel_to_app(usess, attr, app); |
| 2150 | if (ret != LTTCOMM_OK) { |
| 2151 | goto error; |
| 2152 | } |
| 2153 | |
| 2154 | DBG("UST channel %s created for app sock %d with pid %d", |
| 2155 | attr->name, app->sock, domain->attr.pid); |
| 2156 | break; |
| 2157 | } |
| 2158 | default: |
| 2159 | ret = LTTCOMM_UNKNOWN_DOMAIN; |
| 2160 | goto error; |
| 2161 | } |
| 2162 | |
| 2163 | ret = LTTCOMM_OK; |
| 2164 | |
| 2165 | error: |
| 2166 | return ret; |
| 2167 | } |
| 2168 | |
| 2169 | /* |
| 2170 | * Command LTTNG_DISABLE_EVENT processed by the client thread. |
| 2171 | */ |
| 2172 | static int cmd_disable_event(struct ltt_session *session, int domain, |
| 2173 | char *channel_name, char *event_name) |
| 2174 | { |
| 2175 | int ret; |
| 2176 | |
| 2177 | switch (domain) { |
| 2178 | case LTTNG_DOMAIN_KERNEL: |
| 2179 | { |
| 2180 | struct ltt_kernel_channel *kchan; |
| 2181 | |
| 2182 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 2183 | session->kernel_session); |
| 2184 | if (kchan == NULL) { |
| 2185 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 2186 | goto error; |
| 2187 | } |
| 2188 | |
| 2189 | ret = event_kernel_disable_tracepoint(session->kernel_session, kchan, event_name); |
| 2190 | if (ret != LTTCOMM_OK) { |
| 2191 | goto error; |
| 2192 | } |
| 2193 | |
| 2194 | kernel_wait_quiescent(kernel_tracer_fd); |
| 2195 | break; |
| 2196 | } |
| 2197 | case LTTNG_DOMAIN_UST: |
| 2198 | { |
| 2199 | struct ltt_ust_session *ustsession; |
| 2200 | |
| 2201 | cds_list_for_each_entry(ustsession, &session->ust_session_list.head, list) { |
| 2202 | struct ltt_ust_channel *ustchan; |
| 2203 | |
| 2204 | ustchan = trace_ust_get_channel_by_name(channel_name, |
| 2205 | ustsession); |
| 2206 | if (ustchan == NULL) { |
| 2207 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 2208 | goto error; |
| 2209 | } |
| 2210 | ret = event_ust_disable_tracepoint(ustsession, ustchan, event_name); |
| 2211 | if (ret != LTTCOMM_OK) { |
| 2212 | goto error; |
| 2213 | } |
| 2214 | |
| 2215 | ustctl_wait_quiescent(ustsession->sock); |
| 2216 | } |
| 2217 | break; |
| 2218 | } |
| 2219 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 2220 | case LTTNG_DOMAIN_UST_PID: |
| 2221 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 2222 | default: |
| 2223 | /* TODO: Other UST domains */ |
| 2224 | ret = LTTCOMM_NOT_IMPLEMENTED; |
| 2225 | goto error; |
| 2226 | } |
| 2227 | |
| 2228 | ret = LTTCOMM_OK; |
| 2229 | |
| 2230 | error: |
| 2231 | return ret; |
| 2232 | } |
| 2233 | |
| 2234 | /* |
| 2235 | * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread. |
| 2236 | */ |
| 2237 | static int cmd_disable_event_all(struct ltt_session *session, int domain, |
| 2238 | char *channel_name) |
| 2239 | { |
| 2240 | int ret; |
| 2241 | struct ltt_kernel_channel *kchan; |
| 2242 | |
| 2243 | switch (domain) { |
| 2244 | case LTTNG_DOMAIN_KERNEL: |
| 2245 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 2246 | session->kernel_session); |
| 2247 | if (kchan == NULL) { |
| 2248 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 2249 | goto error; |
| 2250 | } |
| 2251 | |
| 2252 | ret = event_kernel_disable_all(session->kernel_session, kchan); |
| 2253 | if (ret != LTTCOMM_OK) { |
| 2254 | goto error; |
| 2255 | } |
| 2256 | |
| 2257 | kernel_wait_quiescent(kernel_tracer_fd); |
| 2258 | break; |
| 2259 | default: |
| 2260 | /* TODO: Userspace tracing */ |
| 2261 | ret = LTTCOMM_NOT_IMPLEMENTED; |
| 2262 | goto error; |
| 2263 | } |
| 2264 | |
| 2265 | ret = LTTCOMM_OK; |
| 2266 | |
| 2267 | error: |
| 2268 | return ret; |
| 2269 | } |
| 2270 | |
| 2271 | /* |
| 2272 | * Command LTTNG_ADD_CONTEXT processed by the client thread. |
| 2273 | */ |
| 2274 | static int cmd_add_context(struct ltt_session *session, int domain, |
| 2275 | char *channel_name, char *event_name, struct lttng_event_context *ctx) |
| 2276 | { |
| 2277 | int ret; |
| 2278 | |
| 2279 | switch (domain) { |
| 2280 | case LTTNG_DOMAIN_KERNEL: |
| 2281 | /* Add kernel context to kernel tracer */ |
| 2282 | ret = context_kernel_add(session->kernel_session, ctx, |
| 2283 | event_name, channel_name); |
| 2284 | if (ret != LTTCOMM_OK) { |
| 2285 | goto error; |
| 2286 | } |
| 2287 | break; |
| 2288 | case LTTNG_DOMAIN_UST: |
| 2289 | { |
| 2290 | struct ltt_ust_session *ustsession; |
| 2291 | |
| 2292 | cds_list_for_each_entry(ustsession, &session->ust_session_list.head, list) { |
| 2293 | /* Add UST context to UST tracer */ |
| 2294 | ret = context_ust_add(ustsession, ctx, |
| 2295 | event_name, channel_name); |
| 2296 | if (ret != LTTCOMM_OK) { |
| 2297 | goto error; |
| 2298 | } |
| 2299 | } |
| 2300 | break; |
| 2301 | } |
| 2302 | default: |
| 2303 | /* TODO: UST other domains */ |
| 2304 | ret = LTTCOMM_NOT_IMPLEMENTED; |
| 2305 | goto error; |
| 2306 | } |
| 2307 | |
| 2308 | ret = LTTCOMM_OK; |
| 2309 | |
| 2310 | error: |
| 2311 | return ret; |
| 2312 | } |
| 2313 | |
| 2314 | /* |
| 2315 | * Command LTTNG_ENABLE_EVENT processed by the client thread. |
| 2316 | */ |
| 2317 | static int cmd_enable_event(struct ltt_session *session, int domain, |
| 2318 | char *channel_name, struct lttng_event *event) |
| 2319 | { |
| 2320 | int ret; |
| 2321 | struct lttng_channel *attr; |
| 2322 | |
| 2323 | switch (domain) { |
| 2324 | case LTTNG_DOMAIN_KERNEL: |
| 2325 | { |
| 2326 | struct ltt_kernel_channel *kchan; |
| 2327 | |
| 2328 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 2329 | session->kernel_session); |
| 2330 | if (kchan == NULL) { |
| 2331 | attr = channel_new_default_attr(domain); |
| 2332 | if (attr == NULL) { |
| 2333 | ret = LTTCOMM_FATAL; |
| 2334 | goto error; |
| 2335 | } |
| 2336 | snprintf(attr->name, NAME_MAX, "%s", channel_name); |
| 2337 | |
| 2338 | /* This call will notify the kernel thread */ |
| 2339 | ret = channel_kernel_create(session->kernel_session, |
| 2340 | attr, kernel_poll_pipe[1]); |
| 2341 | if (ret != LTTCOMM_OK) { |
| 2342 | goto error; |
| 2343 | } |
| 2344 | } |
| 2345 | |
| 2346 | /* Get the newly created kernel channel pointer */ |
| 2347 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 2348 | session->kernel_session); |
| 2349 | if (kchan == NULL) { |
| 2350 | /* This sould not happen... */ |
| 2351 | ret = LTTCOMM_FATAL; |
| 2352 | goto error; |
| 2353 | } |
| 2354 | |
| 2355 | ret = event_kernel_enable_tracepoint(session->kernel_session, kchan, event); |
| 2356 | if (ret != LTTCOMM_OK) { |
| 2357 | goto error; |
| 2358 | } |
| 2359 | |
| 2360 | kernel_wait_quiescent(kernel_tracer_fd); |
| 2361 | break; |
| 2362 | } |
| 2363 | case LTTNG_DOMAIN_UST: |
| 2364 | { |
| 2365 | struct ltt_ust_session *ustsession; |
| 2366 | |
| 2367 | cds_list_for_each_entry(ustsession, &session->ust_session_list.head, list) { |
| 2368 | struct ltt_ust_channel *ustchan; |
| 2369 | |
| 2370 | ustchan = trace_ust_get_channel_by_name(channel_name, |
| 2371 | ustsession); |
| 2372 | if (ustchan == NULL) { |
| 2373 | attr = channel_new_default_attr(domain); |
| 2374 | if (attr == NULL) { |
| 2375 | ret = LTTCOMM_FATAL; |
| 2376 | goto error; |
| 2377 | } |
| 2378 | snprintf(attr->name, NAME_MAX, "%s", channel_name); |
| 2379 | |
| 2380 | ret = channel_ust_create(ustsession, |
| 2381 | attr, ustsession->sock); |
| 2382 | if (ret != LTTCOMM_OK) { |
| 2383 | goto error; |
| 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | /* Get the newly created ust channel pointer */ |
| 2388 | ustchan = trace_ust_get_channel_by_name(channel_name, |
| 2389 | ustsession); |
| 2390 | if (ustchan == NULL) { |
| 2391 | /* This sould not happen... */ |
| 2392 | ret = LTTCOMM_FATAL; |
| 2393 | goto error; |
| 2394 | } |
| 2395 | |
| 2396 | ret = event_ust_enable_tracepoint(ustsession, ustchan, event); |
| 2397 | if (ret != LTTCOMM_OK) { |
| 2398 | goto error; |
| 2399 | } |
| 2400 | |
| 2401 | ustctl_wait_quiescent(ustsession->sock); |
| 2402 | } |
| 2403 | break; |
| 2404 | } |
| 2405 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 2406 | case LTTNG_DOMAIN_UST_PID: |
| 2407 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 2408 | default: |
| 2409 | /* TODO: UST other domains */ |
| 2410 | ret = LTTCOMM_NOT_IMPLEMENTED; |
| 2411 | goto error; |
| 2412 | } |
| 2413 | |
| 2414 | ret = LTTCOMM_OK; |
| 2415 | |
| 2416 | error: |
| 2417 | return ret; |
| 2418 | } |
| 2419 | |
| 2420 | /* |
| 2421 | * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread. |
| 2422 | */ |
| 2423 | static int cmd_enable_event_all(struct ltt_session *session, int domain, |
| 2424 | char *channel_name, int event_type) |
| 2425 | { |
| 2426 | int ret; |
| 2427 | struct ltt_kernel_channel *kchan; |
| 2428 | |
| 2429 | switch (domain) { |
| 2430 | case LTTNG_DOMAIN_KERNEL: |
| 2431 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 2432 | session->kernel_session); |
| 2433 | if (kchan == NULL) { |
| 2434 | /* This call will notify the kernel thread */ |
| 2435 | ret = channel_kernel_create(session->kernel_session, NULL, |
| 2436 | kernel_poll_pipe[1]); |
| 2437 | if (ret != LTTCOMM_OK) { |
| 2438 | goto error; |
| 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | /* Get the newly created kernel channel pointer */ |
| 2443 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 2444 | session->kernel_session); |
| 2445 | if (kchan == NULL) { |
| 2446 | /* This sould not happen... */ |
| 2447 | ret = LTTCOMM_FATAL; |
| 2448 | goto error; |
| 2449 | } |
| 2450 | |
| 2451 | switch (event_type) { |
| 2452 | case LTTNG_KERNEL_SYSCALL: |
| 2453 | ret = event_kernel_enable_all_syscalls(session->kernel_session, |
| 2454 | kchan, kernel_tracer_fd); |
| 2455 | break; |
| 2456 | case LTTNG_KERNEL_TRACEPOINT: |
| 2457 | /* |
| 2458 | * This call enables all LTTNG_KERNEL_TRACEPOINTS and |
| 2459 | * events already registered to the channel. |
| 2460 | */ |
| 2461 | ret = event_kernel_enable_all_tracepoints(session->kernel_session, |
| 2462 | kchan, kernel_tracer_fd); |
| 2463 | break; |
| 2464 | case LTTNG_KERNEL_ALL: |
| 2465 | /* Enable syscalls and tracepoints */ |
| 2466 | ret = event_kernel_enable_all(session->kernel_session, |
| 2467 | kchan, kernel_tracer_fd); |
| 2468 | break; |
| 2469 | default: |
| 2470 | ret = LTTCOMM_KERN_ENABLE_FAIL; |
| 2471 | goto error; |
| 2472 | } |
| 2473 | if (ret != LTTCOMM_OK) { |
| 2474 | goto error; |
| 2475 | } |
| 2476 | |
| 2477 | kernel_wait_quiescent(kernel_tracer_fd); |
| 2478 | break; |
| 2479 | default: |
| 2480 | /* TODO: Userspace tracing */ |
| 2481 | ret = LTTCOMM_NOT_IMPLEMENTED; |
| 2482 | goto error; |
| 2483 | } |
| 2484 | |
| 2485 | ret = LTTCOMM_OK; |
| 2486 | |
| 2487 | error: |
| 2488 | return ret; |
| 2489 | } |
| 2490 | |
| 2491 | /* |
| 2492 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. |
| 2493 | */ |
| 2494 | static ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events) |
| 2495 | { |
| 2496 | int ret; |
| 2497 | ssize_t nb_events = 0; |
| 2498 | |
| 2499 | switch (domain) { |
| 2500 | case LTTNG_DOMAIN_KERNEL: |
| 2501 | nb_events = kernel_list_events(kernel_tracer_fd, events); |
| 2502 | if (nb_events < 0) { |
| 2503 | ret = LTTCOMM_KERN_LIST_FAIL; |
| 2504 | goto error; |
| 2505 | } |
| 2506 | break; |
| 2507 | default: |
| 2508 | /* TODO: Userspace listing */ |
| 2509 | ret = LTTCOMM_NOT_IMPLEMENTED; |
| 2510 | goto error; |
| 2511 | } |
| 2512 | |
| 2513 | return nb_events; |
| 2514 | |
| 2515 | error: |
| 2516 | /* Return negative value to differentiate return code */ |
| 2517 | return -ret; |
| 2518 | } |
| 2519 | |
| 2520 | /* |
| 2521 | * Command LTTNG_START_TRACE processed by the client thread. |
| 2522 | */ |
| 2523 | static int cmd_start_trace(struct ltt_session *session) |
| 2524 | { |
| 2525 | int ret; |
| 2526 | struct ltt_kernel_session *ksession; |
| 2527 | struct ltt_ust_session *ustsession; |
| 2528 | |
| 2529 | /* Short cut */ |
| 2530 | ksession = session->kernel_session; |
| 2531 | |
| 2532 | /* Kernel tracing */ |
| 2533 | if (ksession != NULL) { |
| 2534 | struct ltt_kernel_channel *kchan; |
| 2535 | |
| 2536 | /* Open kernel metadata */ |
| 2537 | if (ksession->metadata == NULL) { |
| 2538 | ret = kernel_open_metadata(ksession, ksession->trace_path); |
| 2539 | if (ret < 0) { |
| 2540 | ret = LTTCOMM_KERN_META_FAIL; |
| 2541 | goto error; |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | /* Open kernel metadata stream */ |
| 2546 | if (ksession->metadata_stream_fd == 0) { |
| 2547 | ret = kernel_open_metadata_stream(ksession); |
| 2548 | if (ret < 0) { |
| 2549 | ERR("Kernel create metadata stream failed"); |
| 2550 | ret = LTTCOMM_KERN_STREAM_FAIL; |
| 2551 | goto error; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | /* For each channel */ |
| 2556 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
| 2557 | if (kchan->stream_count == 0) { |
| 2558 | ret = kernel_open_channel_stream(kchan); |
| 2559 | if (ret < 0) { |
| 2560 | ret = LTTCOMM_KERN_STREAM_FAIL; |
| 2561 | goto error; |
| 2562 | } |
| 2563 | /* Update the stream global counter */ |
| 2564 | ksession->stream_count_global += ret; |
| 2565 | } |
| 2566 | } |
| 2567 | |
| 2568 | /* Setup kernel consumer socket and send fds to it */ |
| 2569 | ret = init_kernel_tracing(ksession); |
| 2570 | if (ret < 0) { |
| 2571 | ret = LTTCOMM_KERN_START_FAIL; |
| 2572 | goto error; |
| 2573 | } |
| 2574 | |
| 2575 | /* This start the kernel tracing */ |
| 2576 | ret = kernel_start_session(ksession); |
| 2577 | if (ret < 0) { |
| 2578 | ret = LTTCOMM_KERN_START_FAIL; |
| 2579 | goto error; |
| 2580 | } |
| 2581 | |
| 2582 | /* Quiescent wait after starting trace */ |
| 2583 | kernel_wait_quiescent(kernel_tracer_fd); |
| 2584 | } |
| 2585 | |
| 2586 | /* Start all UST traces */ |
| 2587 | cds_list_for_each_entry(ustsession, &session->ust_session_list.head, list) { |
| 2588 | struct ltt_ust_channel *ustchan; |
| 2589 | |
| 2590 | /* Open kernel metadata */ |
| 2591 | if (ustsession->metadata == NULL) { |
| 2592 | struct lttng_ust_channel_attr ustattr; |
| 2593 | |
| 2594 | /* Allocate UST metadata */ |
| 2595 | ustsession->metadata = trace_ust_create_metadata(ustsession->path); |
| 2596 | if (ustsession->metadata == NULL) { |
| 2597 | ret = LTTCOMM_UST_META_FAIL; |
| 2598 | goto error; |
| 2599 | } |
| 2600 | |
| 2601 | ustattr.overwrite = ustsession->metadata->attr.overwrite; |
| 2602 | ustattr.subbuf_size = ustsession->metadata->attr.subbuf_size; |
| 2603 | ustattr.num_subbuf = ustsession->metadata->attr.num_subbuf; |
| 2604 | ustattr.switch_timer_interval = ustsession->metadata->attr.switch_timer_interval; |
| 2605 | ustattr.read_timer_interval = ustsession->metadata->attr.read_timer_interval; |
| 2606 | ustattr.output = ustsession->metadata->attr.output; |
| 2607 | |
| 2608 | /* UST tracer metadata creation */ |
| 2609 | ret = ustctl_open_metadata(ustsession->sock, |
| 2610 | ustsession->handle, &ustattr, |
| 2611 | &ustsession->metadata->obj); |
| 2612 | if (ret < 0) { |
| 2613 | ret = LTTCOMM_UST_META_FAIL; |
| 2614 | goto error; |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | /* Open UST metadata stream */ |
| 2619 | if (ustsession->metadata->stream_obj == NULL) { |
| 2620 | ret = ustctl_create_stream(ustsession->sock, |
| 2621 | ustsession->metadata->obj, |
| 2622 | &ustsession->metadata->stream_obj); |
| 2623 | if (ret < 0) { |
| 2624 | ERR("UST create metadata stream failed"); |
| 2625 | ret = LTTCOMM_UST_STREAM_FAIL; |
| 2626 | goto error; |
| 2627 | } |
| 2628 | ret = asprintf(&ustsession->metadata->pathname, "%s/%s", |
| 2629 | ustsession->path, "metadata"); |
| 2630 | if (ret < 0) { |
| 2631 | perror("asprintf UST create stream"); |
| 2632 | goto error; |
| 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | /* For each channel */ |
| 2637 | cds_list_for_each_entry(ustchan, &ustsession->channels.head, list) { |
| 2638 | if (ustchan->stream_count == 0) { |
| 2639 | struct ltt_ust_stream *ustream; |
| 2640 | |
| 2641 | ustream = zmalloc(sizeof(*ustream)); |
| 2642 | if (!ustream) { |
| 2643 | ret = LTTCOMM_UST_STREAM_FAIL; |
| 2644 | goto error; |
| 2645 | } |
| 2646 | ret = ustctl_create_stream(ustsession->sock, |
| 2647 | ustchan->obj, &ustream->obj); |
| 2648 | if (ret < 0) { |
| 2649 | ret = LTTCOMM_UST_STREAM_FAIL; |
| 2650 | goto error; |
| 2651 | } |
| 2652 | ret = asprintf(&ustream->pathname, "%s/%s_%d", |
| 2653 | ustchan->trace_path, ustchan->name, |
| 2654 | ustchan->stream_count); |
| 2655 | if (ret < 0) { |
| 2656 | perror("asprintf UST create stream"); |
| 2657 | goto error; |
| 2658 | } |
| 2659 | cds_list_add(&ustream->list, &ustchan->stream_list.head); |
| 2660 | ustchan->stream_count++; |
| 2661 | } |
| 2662 | } |
| 2663 | |
| 2664 | /* Setup UST consumer socket and send fds to it */ |
| 2665 | ret = init_ust_tracing(ustsession); |
| 2666 | if (ret < 0) { |
| 2667 | ret = LTTCOMM_UST_START_FAIL; |
| 2668 | goto error; |
| 2669 | } |
| 2670 | |
| 2671 | /* This start the UST tracing */ |
| 2672 | ret = ustctl_start_session(ustsession->sock, ustsession->handle); |
| 2673 | if (ret < 0) { |
| 2674 | ret = LTTCOMM_UST_START_FAIL; |
| 2675 | goto error; |
| 2676 | } |
| 2677 | |
| 2678 | /* Quiescent wait after starting trace */ |
| 2679 | ustctl_wait_quiescent(ustsession->sock); |
| 2680 | } |
| 2681 | |
| 2682 | ret = LTTCOMM_OK; |
| 2683 | |
| 2684 | error: |
| 2685 | return ret; |
| 2686 | } |
| 2687 | |
| 2688 | /* |
| 2689 | * Command LTTNG_STOP_TRACE processed by the client thread. |
| 2690 | */ |
| 2691 | static int cmd_stop_trace(struct ltt_session *session) |
| 2692 | { |
| 2693 | int ret; |
| 2694 | struct ltt_kernel_channel *kchan; |
| 2695 | struct ltt_kernel_session *ksession; |
| 2696 | struct ltt_ust_session *ustsession; |
| 2697 | struct ltt_ust_channel *ustchan; |
| 2698 | |
| 2699 | /* Short cut */ |
| 2700 | ksession = session->kernel_session; |
| 2701 | |
| 2702 | /* Kernel tracer */ |
| 2703 | if (ksession != NULL) { |
| 2704 | DBG("Stop kernel tracing"); |
| 2705 | |
| 2706 | /* Flush all buffers before stopping */ |
| 2707 | ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd); |
| 2708 | if (ret < 0) { |
| 2709 | ERR("Kernel metadata flush failed"); |
| 2710 | } |
| 2711 | |
| 2712 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
| 2713 | ret = kernel_flush_buffer(kchan); |
| 2714 | if (ret < 0) { |
| 2715 | ERR("Kernel flush buffer error"); |
| 2716 | } |
| 2717 | } |
| 2718 | |
| 2719 | ret = kernel_stop_session(ksession); |
| 2720 | if (ret < 0) { |
| 2721 | ret = LTTCOMM_KERN_STOP_FAIL; |
| 2722 | goto error; |
| 2723 | } |
| 2724 | |
| 2725 | kernel_wait_quiescent(kernel_tracer_fd); |
| 2726 | } |
| 2727 | |
| 2728 | /* Stop each UST session */ |
| 2729 | DBG("Stop UST tracing"); |
| 2730 | cds_list_for_each_entry(ustsession, &session->ust_session_list.head, list) { |
| 2731 | /* Flush all buffers before stopping */ |
| 2732 | ret = ustctl_flush_buffer(ustsession->sock, ustsession->metadata->obj); |
| 2733 | if (ret < 0) { |
| 2734 | ERR("UST metadata flush failed"); |
| 2735 | } |
| 2736 | |
| 2737 | cds_list_for_each_entry(ustchan, &ustsession->channels.head, list) { |
| 2738 | ret = ustctl_flush_buffer(ustsession->sock, ustchan->obj); |
| 2739 | if (ret < 0) { |
| 2740 | ERR("UST flush buffer error"); |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | ret = ustctl_stop_session(ustsession->sock, ustsession->handle); |
| 2745 | if (ret < 0) { |
| 2746 | ret = LTTCOMM_KERN_STOP_FAIL; |
| 2747 | goto error; |
| 2748 | } |
| 2749 | |
| 2750 | ustctl_wait_quiescent(ustsession->sock); |
| 2751 | } |
| 2752 | |
| 2753 | ret = LTTCOMM_OK; |
| 2754 | |
| 2755 | error: |
| 2756 | return ret; |
| 2757 | } |
| 2758 | |
| 2759 | /* |
| 2760 | * Command LTTNG_CREATE_SESSION processed by the client thread. |
| 2761 | */ |
| 2762 | static int cmd_create_session(char *name, char *path) |
| 2763 | { |
| 2764 | int ret; |
| 2765 | |
| 2766 | ret = session_create(name, path); |
| 2767 | if (ret != LTTCOMM_OK) { |
| 2768 | goto error; |
| 2769 | } |
| 2770 | |
| 2771 | ret = LTTCOMM_OK; |
| 2772 | |
| 2773 | error: |
| 2774 | return ret; |
| 2775 | } |
| 2776 | |
| 2777 | /* |
| 2778 | * Command LTTNG_DESTROY_SESSION processed by the client thread. |
| 2779 | */ |
| 2780 | static int cmd_destroy_session(struct ltt_session *session, char *name) |
| 2781 | { |
| 2782 | int ret; |
| 2783 | |
| 2784 | /* Clean kernel session teardown */ |
| 2785 | teardown_kernel_session(session); |
| 2786 | |
| 2787 | /* |
| 2788 | * Must notify the kernel thread here to update it's poll setin order |
| 2789 | * to remove the channel(s)' fd just destroyed. |
| 2790 | */ |
| 2791 | ret = notify_thread_pipe(kernel_poll_pipe[1]); |
| 2792 | if (ret < 0) { |
| 2793 | perror("write kernel poll pipe"); |
| 2794 | } |
| 2795 | |
| 2796 | ret = session_destroy(session); |
| 2797 | |
| 2798 | return ret; |
| 2799 | } |
| 2800 | |
| 2801 | /* |
| 2802 | * Command LTTNG_CALIBRATE processed by the client thread. |
| 2803 | */ |
| 2804 | static int cmd_calibrate(int domain, struct lttng_calibrate *calibrate) |
| 2805 | { |
| 2806 | int ret; |
| 2807 | |
| 2808 | switch (domain) { |
| 2809 | case LTTNG_DOMAIN_KERNEL: |
| 2810 | { |
| 2811 | struct lttng_kernel_calibrate kcalibrate; |
| 2812 | |
| 2813 | kcalibrate.type = calibrate->type; |
| 2814 | ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate); |
| 2815 | if (ret < 0) { |
| 2816 | ret = LTTCOMM_KERN_ENABLE_FAIL; |
| 2817 | goto error; |
| 2818 | } |
| 2819 | break; |
| 2820 | } |
| 2821 | default: |
| 2822 | /* TODO: Userspace tracing */ |
| 2823 | ret = LTTCOMM_NOT_IMPLEMENTED; |
| 2824 | goto error; |
| 2825 | } |
| 2826 | |
| 2827 | ret = LTTCOMM_OK; |
| 2828 | |
| 2829 | error: |
| 2830 | return ret; |
| 2831 | } |
| 2832 | |
| 2833 | /* |
| 2834 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. |
| 2835 | */ |
| 2836 | static int cmd_register_consumer(struct ltt_session *session, int domain, |
| 2837 | char *sock_path) |
| 2838 | { |
| 2839 | int ret, sock; |
| 2840 | |
| 2841 | switch (domain) { |
| 2842 | case LTTNG_DOMAIN_KERNEL: |
| 2843 | /* Can't register a consumer if there is already one */ |
| 2844 | if (session->kernel_session->consumer_fd != 0) { |
| 2845 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
| 2846 | goto error; |
| 2847 | } |
| 2848 | |
| 2849 | sock = lttcomm_connect_unix_sock(sock_path); |
| 2850 | if (sock < 0) { |
| 2851 | ret = LTTCOMM_CONNECT_FAIL; |
| 2852 | goto error; |
| 2853 | } |
| 2854 | |
| 2855 | session->kernel_session->consumer_fd = sock; |
| 2856 | break; |
| 2857 | default: |
| 2858 | /* TODO: Userspace tracing */ |
| 2859 | ret = LTTCOMM_NOT_IMPLEMENTED; |
| 2860 | goto error; |
| 2861 | } |
| 2862 | |
| 2863 | ret = LTTCOMM_OK; |
| 2864 | |
| 2865 | error: |
| 2866 | return ret; |
| 2867 | } |
| 2868 | |
| 2869 | /* |
| 2870 | * Command LTTNG_LIST_DOMAINS processed by the client thread. |
| 2871 | */ |
| 2872 | static ssize_t cmd_list_domains(struct ltt_session *session, |
| 2873 | struct lttng_domain **domains) |
| 2874 | { |
| 2875 | int ret; |
| 2876 | ssize_t nb_dom = 0; |
| 2877 | |
| 2878 | if (session->kernel_session != NULL) { |
| 2879 | nb_dom++; |
| 2880 | } |
| 2881 | |
| 2882 | nb_dom += session->ust_session_list.count; |
| 2883 | |
| 2884 | *domains = malloc(nb_dom * sizeof(struct lttng_domain)); |
| 2885 | if (*domains == NULL) { |
| 2886 | ret = -LTTCOMM_FATAL; |
| 2887 | goto error; |
| 2888 | } |
| 2889 | |
| 2890 | (*domains)[0].type = LTTNG_DOMAIN_KERNEL; |
| 2891 | |
| 2892 | /* TODO: User-space tracer domain support */ |
| 2893 | |
| 2894 | return nb_dom; |
| 2895 | |
| 2896 | error: |
| 2897 | return ret; |
| 2898 | } |
| 2899 | |
| 2900 | /* |
| 2901 | * Command LTTNG_LIST_CHANNELS processed by the client thread. |
| 2902 | */ |
| 2903 | static ssize_t cmd_list_channels(struct ltt_session *session, |
| 2904 | struct lttng_channel **channels) |
| 2905 | { |
| 2906 | int ret; |
| 2907 | ssize_t nb_chan = 0; |
| 2908 | |
| 2909 | if (session->kernel_session != NULL) { |
| 2910 | nb_chan += session->kernel_session->channel_count; |
| 2911 | } |
| 2912 | |
| 2913 | *channels = malloc(nb_chan * sizeof(struct lttng_channel)); |
| 2914 | if (*channels == NULL) { |
| 2915 | ret = -LTTCOMM_FATAL; |
| 2916 | goto error; |
| 2917 | } |
| 2918 | |
| 2919 | list_lttng_channels(session, *channels); |
| 2920 | |
| 2921 | /* TODO UST support */ |
| 2922 | |
| 2923 | return nb_chan; |
| 2924 | |
| 2925 | error: |
| 2926 | return ret; |
| 2927 | } |
| 2928 | |
| 2929 | /* |
| 2930 | * Command LTTNG_LIST_EVENTS processed by the client thread. |
| 2931 | */ |
| 2932 | static ssize_t cmd_list_events(struct ltt_session *session, |
| 2933 | char *channel_name, struct lttng_event **events) |
| 2934 | { |
| 2935 | int ret; |
| 2936 | ssize_t nb_event = 0; |
| 2937 | struct ltt_kernel_channel *kchan = NULL; |
| 2938 | |
| 2939 | if (session->kernel_session != NULL) { |
| 2940 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 2941 | session->kernel_session); |
| 2942 | if (kchan == NULL) { |
| 2943 | ret = -LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 2944 | goto error; |
| 2945 | } |
| 2946 | nb_event += kchan->event_count; |
| 2947 | } |
| 2948 | |
| 2949 | *events = malloc(nb_event * sizeof(struct lttng_event)); |
| 2950 | if (*events == NULL) { |
| 2951 | ret = -LTTCOMM_FATAL; |
| 2952 | goto error; |
| 2953 | } |
| 2954 | |
| 2955 | list_lttng_events(kchan, *events); |
| 2956 | |
| 2957 | /* TODO: User-space tracer support */ |
| 2958 | |
| 2959 | return nb_event; |
| 2960 | |
| 2961 | error: |
| 2962 | return ret; |
| 2963 | } |
| 2964 | |
| 2965 | /* |
| 2966 | * Process the command requested by the lttng client within the command |
| 2967 | * context structure. This function make sure that the return structure (llm) |
| 2968 | * is set and ready for transmission before returning. |
| 2969 | * |
| 2970 | * Return any error encountered or 0 for success. |
| 2971 | */ |
| 2972 | static int process_client_msg(struct command_ctx *cmd_ctx) |
| 2973 | { |
| 2974 | int ret = LTTCOMM_OK; |
| 2975 | int need_tracing_session = 1; |
| 2976 | |
| 2977 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); |
| 2978 | |
| 2979 | /* |
| 2980 | * Check for command that don't needs to allocate a returned payload. We do |
| 2981 | * this here so we don't have to make the call for no payload at each |
| 2982 | * command. |
| 2983 | */ |
| 2984 | switch(cmd_ctx->lsm->cmd_type) { |
| 2985 | case LTTNG_LIST_SESSIONS: |
| 2986 | case LTTNG_LIST_TRACEPOINTS: |
| 2987 | case LTTNG_LIST_DOMAINS: |
| 2988 | case LTTNG_LIST_CHANNELS: |
| 2989 | case LTTNG_LIST_EVENTS: |
| 2990 | break; |
| 2991 | default: |
| 2992 | /* Setup lttng message with no payload */ |
| 2993 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 2994 | if (ret < 0) { |
| 2995 | /* This label does not try to unlock the session */ |
| 2996 | goto init_setup_error; |
| 2997 | } |
| 2998 | } |
| 2999 | |
| 3000 | /* Commands that DO NOT need a session. */ |
| 3001 | switch (cmd_ctx->lsm->cmd_type) { |
| 3002 | case LTTNG_CALIBRATE: |
| 3003 | case LTTNG_CREATE_SESSION: |
| 3004 | case LTTNG_LIST_SESSIONS: |
| 3005 | case LTTNG_LIST_TRACEPOINTS: |
| 3006 | need_tracing_session = 0; |
| 3007 | break; |
| 3008 | default: |
| 3009 | DBG("Getting session %s by name", cmd_ctx->lsm->session.name); |
| 3010 | session_lock_list(); |
| 3011 | cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name); |
| 3012 | session_unlock_list(); |
| 3013 | if (cmd_ctx->session == NULL) { |
| 3014 | if (cmd_ctx->lsm->session.name != NULL) { |
| 3015 | ret = LTTCOMM_SESS_NOT_FOUND; |
| 3016 | } else { |
| 3017 | /* If no session name specified */ |
| 3018 | ret = LTTCOMM_SELECT_SESS; |
| 3019 | } |
| 3020 | goto error; |
| 3021 | } else { |
| 3022 | /* Acquire lock for the session */ |
| 3023 | session_lock(cmd_ctx->session); |
| 3024 | } |
| 3025 | break; |
| 3026 | } |
| 3027 | |
| 3028 | /* |
| 3029 | * Check domain type for specific "pre-action". |
| 3030 | */ |
| 3031 | switch (cmd_ctx->lsm->domain.type) { |
| 3032 | case LTTNG_DOMAIN_KERNEL: |
| 3033 | /* Kernel tracer check */ |
| 3034 | if (kernel_tracer_fd == 0) { |
| 3035 | /* Basically, load kernel tracer modules */ |
| 3036 | init_kernel_tracer(); |
| 3037 | if (kernel_tracer_fd == 0) { |
| 3038 | ret = LTTCOMM_KERN_NA; |
| 3039 | goto error; |
| 3040 | } |
| 3041 | } |
| 3042 | |
| 3043 | /* Need a session for kernel command */ |
| 3044 | if (need_tracing_session) { |
| 3045 | if (cmd_ctx->session->kernel_session == NULL) { |
| 3046 | ret = create_kernel_session(cmd_ctx->session); |
| 3047 | if (ret < 0) { |
| 3048 | ret = LTTCOMM_KERN_SESS_FAIL; |
| 3049 | goto error; |
| 3050 | } |
| 3051 | } |
| 3052 | |
| 3053 | /* Start the kernel consumer daemon */ |
| 3054 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
| 3055 | if (kconsumer_data.pid == 0 && |
| 3056 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
| 3057 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
| 3058 | ret = start_consumerd(&kconsumer_data); |
| 3059 | if (ret < 0) { |
| 3060 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
| 3061 | goto error; |
| 3062 | } |
| 3063 | } |
| 3064 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
| 3065 | } |
| 3066 | break; |
| 3067 | case LTTNG_DOMAIN_UST: |
| 3068 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 3069 | case LTTNG_DOMAIN_UST_PID: |
| 3070 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 3071 | { |
| 3072 | struct ltt_ust_session *usess; |
| 3073 | |
| 3074 | if (need_tracing_session) { |
| 3075 | usess = trace_ust_get_session_by_pid( |
| 3076 | &cmd_ctx->session->ust_session_list, |
| 3077 | cmd_ctx->lsm->domain.attr.pid); |
| 3078 | if (usess == NULL) { |
| 3079 | ret = create_ust_session(cmd_ctx->session, |
| 3080 | &cmd_ctx->lsm->domain); |
| 3081 | if (ret != LTTCOMM_OK) { |
| 3082 | goto error; |
| 3083 | } |
| 3084 | } |
| 3085 | /* Start the kernel consumer daemon */ |
| 3086 | pthread_mutex_lock(&ustconsumer_data.pid_mutex); |
| 3087 | if (ustconsumer_data.pid == 0 && |
| 3088 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
| 3089 | pthread_mutex_unlock(&ustconsumer_data.pid_mutex); |
| 3090 | ret = start_consumerd(&ustconsumer_data); |
| 3091 | if (ret < 0) { |
| 3092 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
| 3093 | goto error; |
| 3094 | } |
| 3095 | } |
| 3096 | pthread_mutex_unlock(&ustconsumer_data.pid_mutex); |
| 3097 | } |
| 3098 | break; |
| 3099 | } |
| 3100 | default: |
| 3101 | break; |
| 3102 | } |
| 3103 | |
| 3104 | /* Process by command type */ |
| 3105 | switch (cmd_ctx->lsm->cmd_type) { |
| 3106 | case LTTNG_ADD_CONTEXT: |
| 3107 | { |
| 3108 | ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 3109 | cmd_ctx->lsm->u.context.channel_name, |
| 3110 | cmd_ctx->lsm->u.context.event_name, |
| 3111 | &cmd_ctx->lsm->u.context.ctx); |
| 3112 | break; |
| 3113 | } |
| 3114 | case LTTNG_DISABLE_CHANNEL: |
| 3115 | { |
| 3116 | ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 3117 | cmd_ctx->lsm->u.disable.channel_name); |
| 3118 | break; |
| 3119 | } |
| 3120 | case LTTNG_DISABLE_EVENT: |
| 3121 | { |
| 3122 | ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 3123 | cmd_ctx->lsm->u.disable.channel_name, |
| 3124 | cmd_ctx->lsm->u.disable.name); |
| 3125 | ret = LTTCOMM_OK; |
| 3126 | break; |
| 3127 | } |
| 3128 | case LTTNG_DISABLE_ALL_EVENT: |
| 3129 | { |
| 3130 | DBG("Disabling all events"); |
| 3131 | |
| 3132 | ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 3133 | cmd_ctx->lsm->u.disable.channel_name); |
| 3134 | break; |
| 3135 | } |
| 3136 | case LTTNG_ENABLE_CHANNEL: |
| 3137 | { |
| 3138 | ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain, |
| 3139 | &cmd_ctx->lsm->u.channel.chan); |
| 3140 | break; |
| 3141 | } |
| 3142 | case LTTNG_ENABLE_EVENT: |
| 3143 | { |
| 3144 | ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 3145 | cmd_ctx->lsm->u.enable.channel_name, |
| 3146 | &cmd_ctx->lsm->u.enable.event); |
| 3147 | break; |
| 3148 | } |
| 3149 | case LTTNG_ENABLE_ALL_EVENT: |
| 3150 | { |
| 3151 | DBG("Enabling all events"); |
| 3152 | |
| 3153 | ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 3154 | cmd_ctx->lsm->u.enable.channel_name, |
| 3155 | cmd_ctx->lsm->u.enable.event.type); |
| 3156 | break; |
| 3157 | } |
| 3158 | case LTTNG_LIST_TRACEPOINTS: |
| 3159 | { |
| 3160 | struct lttng_event *events; |
| 3161 | ssize_t nb_events; |
| 3162 | |
| 3163 | nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events); |
| 3164 | if (nb_events < 0) { |
| 3165 | ret = -nb_events; |
| 3166 | goto error; |
| 3167 | } |
| 3168 | |
| 3169 | /* |
| 3170 | * Setup lttng message with payload size set to the event list size in |
| 3171 | * bytes and then copy list into the llm payload. |
| 3172 | */ |
| 3173 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events); |
| 3174 | if (ret < 0) { |
| 3175 | free(events); |
| 3176 | goto setup_error; |
| 3177 | } |
| 3178 | |
| 3179 | /* Copy event list into message payload */ |
| 3180 | memcpy(cmd_ctx->llm->payload, events, |
| 3181 | sizeof(struct lttng_event) * nb_events); |
| 3182 | |
| 3183 | free(events); |
| 3184 | |
| 3185 | ret = LTTCOMM_OK; |
| 3186 | break; |
| 3187 | } |
| 3188 | case LTTNG_START_TRACE: |
| 3189 | { |
| 3190 | ret = cmd_start_trace(cmd_ctx->session); |
| 3191 | break; |
| 3192 | } |
| 3193 | case LTTNG_STOP_TRACE: |
| 3194 | { |
| 3195 | ret = cmd_stop_trace(cmd_ctx->session); |
| 3196 | break; |
| 3197 | } |
| 3198 | case LTTNG_CREATE_SESSION: |
| 3199 | { |
| 3200 | ret = cmd_create_session(cmd_ctx->lsm->session.name, |
| 3201 | cmd_ctx->lsm->session.path); |
| 3202 | break; |
| 3203 | } |
| 3204 | case LTTNG_DESTROY_SESSION: |
| 3205 | { |
| 3206 | ret = cmd_destroy_session(cmd_ctx->session, |
| 3207 | cmd_ctx->lsm->session.name); |
| 3208 | break; |
| 3209 | } |
| 3210 | case LTTNG_LIST_DOMAINS: |
| 3211 | { |
| 3212 | ssize_t nb_dom; |
| 3213 | struct lttng_domain *domains; |
| 3214 | |
| 3215 | nb_dom = cmd_list_domains(cmd_ctx->session, &domains); |
| 3216 | if (nb_dom < 0) { |
| 3217 | ret = -nb_dom; |
| 3218 | goto error; |
| 3219 | } |
| 3220 | |
| 3221 | ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain)); |
| 3222 | if (ret < 0) { |
| 3223 | goto setup_error; |
| 3224 | } |
| 3225 | |
| 3226 | /* Copy event list into message payload */ |
| 3227 | memcpy(cmd_ctx->llm->payload, domains, |
| 3228 | nb_dom * sizeof(struct lttng_domain)); |
| 3229 | |
| 3230 | free(domains); |
| 3231 | |
| 3232 | ret = LTTCOMM_OK; |
| 3233 | break; |
| 3234 | } |
| 3235 | case LTTNG_LIST_CHANNELS: |
| 3236 | { |
| 3237 | size_t nb_chan; |
| 3238 | struct lttng_channel *channels; |
| 3239 | |
| 3240 | nb_chan = cmd_list_channels(cmd_ctx->session, &channels); |
| 3241 | if (nb_chan < 0) { |
| 3242 | ret = -nb_chan; |
| 3243 | goto error; |
| 3244 | } |
| 3245 | |
| 3246 | ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel)); |
| 3247 | if (ret < 0) { |
| 3248 | goto setup_error; |
| 3249 | } |
| 3250 | |
| 3251 | /* Copy event list into message payload */ |
| 3252 | memcpy(cmd_ctx->llm->payload, channels, |
| 3253 | nb_chan * sizeof(struct lttng_channel)); |
| 3254 | |
| 3255 | free(channels); |
| 3256 | |
| 3257 | ret = LTTCOMM_OK; |
| 3258 | break; |
| 3259 | } |
| 3260 | case LTTNG_LIST_EVENTS: |
| 3261 | { |
| 3262 | size_t nb_event; |
| 3263 | struct lttng_event *events = NULL; |
| 3264 | |
| 3265 | nb_event = cmd_list_events(cmd_ctx->session, |
| 3266 | cmd_ctx->lsm->u.list.channel_name, &events); |
| 3267 | if (nb_event < 0) { |
| 3268 | ret = -nb_event; |
| 3269 | goto error; |
| 3270 | } |
| 3271 | |
| 3272 | ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event)); |
| 3273 | if (ret < 0) { |
| 3274 | goto setup_error; |
| 3275 | } |
| 3276 | |
| 3277 | /* Copy event list into message payload */ |
| 3278 | memcpy(cmd_ctx->llm->payload, events, |
| 3279 | nb_event * sizeof(struct lttng_event)); |
| 3280 | |
| 3281 | free(events); |
| 3282 | |
| 3283 | ret = LTTCOMM_OK; |
| 3284 | break; |
| 3285 | } |
| 3286 | case LTTNG_LIST_SESSIONS: |
| 3287 | { |
| 3288 | session_lock_list(); |
| 3289 | |
| 3290 | if (session_list_ptr->count == 0) { |
| 3291 | ret = LTTCOMM_NO_SESSION; |
| 3292 | session_unlock_list(); |
| 3293 | goto error; |
| 3294 | } |
| 3295 | |
| 3296 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * |
| 3297 | session_list_ptr->count); |
| 3298 | if (ret < 0) { |
| 3299 | session_unlock_list(); |
| 3300 | goto setup_error; |
| 3301 | } |
| 3302 | |
| 3303 | /* Filled the session array */ |
| 3304 | list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload)); |
| 3305 | |
| 3306 | session_unlock_list(); |
| 3307 | |
| 3308 | ret = LTTCOMM_OK; |
| 3309 | break; |
| 3310 | } |
| 3311 | case LTTNG_CALIBRATE: |
| 3312 | { |
| 3313 | ret = cmd_calibrate(cmd_ctx->lsm->domain.type, |
| 3314 | &cmd_ctx->lsm->u.calibrate); |
| 3315 | break; |
| 3316 | } |
| 3317 | case LTTNG_REGISTER_CONSUMER: |
| 3318 | { |
| 3319 | ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
| 3320 | cmd_ctx->lsm->u.reg.path); |
| 3321 | break; |
| 3322 | } |
| 3323 | default: |
| 3324 | ret = LTTCOMM_UND; |
| 3325 | break; |
| 3326 | } |
| 3327 | |
| 3328 | error: |
| 3329 | if (cmd_ctx->llm == NULL) { |
| 3330 | DBG("Missing llm structure. Allocating one."); |
| 3331 | if (setup_lttng_msg(cmd_ctx, 0) < 0) { |
| 3332 | goto setup_error; |
| 3333 | } |
| 3334 | } |
| 3335 | /* Set return code */ |
| 3336 | cmd_ctx->llm->ret_code = ret; |
| 3337 | setup_error: |
| 3338 | if (cmd_ctx->session) { |
| 3339 | session_unlock(cmd_ctx->session); |
| 3340 | } |
| 3341 | init_setup_error: |
| 3342 | return ret; |
| 3343 | } |
| 3344 | |
| 3345 | /* |
| 3346 | * This thread manage all clients request using the unix client socket for |
| 3347 | * communication. |
| 3348 | */ |
| 3349 | static void *thread_manage_clients(void *data) |
| 3350 | { |
| 3351 | int sock = 0, ret, i, pollfd; |
| 3352 | uint32_t revents, nb_fd; |
| 3353 | struct command_ctx *cmd_ctx = NULL; |
| 3354 | struct lttng_poll_event events; |
| 3355 | |
| 3356 | DBG("[thread] Manage client started"); |
| 3357 | |
| 3358 | ret = lttcomm_listen_unix_sock(client_sock); |
| 3359 | if (ret < 0) { |
| 3360 | goto error; |
| 3361 | } |
| 3362 | |
| 3363 | /* |
| 3364 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing |
| 3365 | * more will be added to this poll set. |
| 3366 | */ |
| 3367 | ret = create_thread_poll_set(&events, 2); |
| 3368 | if (ret < 0) { |
| 3369 | goto error; |
| 3370 | } |
| 3371 | |
| 3372 | /* Add the application registration socket */ |
| 3373 | ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI); |
| 3374 | if (ret < 0) { |
| 3375 | goto error; |
| 3376 | } |
| 3377 | |
| 3378 | /* |
| 3379 | * Notify parent pid that we are ready to accept command for client side. |
| 3380 | */ |
| 3381 | if (opt_sig_parent) { |
| 3382 | kill(ppid, SIGCHLD); |
| 3383 | } |
| 3384 | |
| 3385 | while (1) { |
| 3386 | DBG("Accepting client command ..."); |
| 3387 | |
| 3388 | nb_fd = LTTNG_POLL_GETNB(&events); |
| 3389 | |
| 3390 | /* Inifinite blocking call, waiting for transmission */ |
| 3391 | ret = lttng_poll_wait(&events, -1); |
| 3392 | if (ret < 0) { |
| 3393 | goto error; |
| 3394 | } |
| 3395 | |
| 3396 | for (i = 0; i < nb_fd; i++) { |
| 3397 | /* Fetch once the poll data */ |
| 3398 | revents = LTTNG_POLL_GETEV(&events, i); |
| 3399 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 3400 | |
| 3401 | /* Thread quit pipe has been closed. Killing thread. */ |
| 3402 | ret = check_thread_quit_pipe(pollfd, revents); |
| 3403 | if (ret) { |
| 3404 | goto error; |
| 3405 | } |
| 3406 | |
| 3407 | /* Event on the registration socket */ |
| 3408 | if (pollfd == client_sock) { |
| 3409 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 3410 | ERR("Client socket poll error"); |
| 3411 | goto error; |
| 3412 | } |
| 3413 | } |
| 3414 | } |
| 3415 | |
| 3416 | DBG("Wait for client response"); |
| 3417 | |
| 3418 | sock = lttcomm_accept_unix_sock(client_sock); |
| 3419 | if (sock < 0) { |
| 3420 | goto error; |
| 3421 | } |
| 3422 | |
| 3423 | /* Allocate context command to process the client request */ |
| 3424 | cmd_ctx = malloc(sizeof(struct command_ctx)); |
| 3425 | if (cmd_ctx == NULL) { |
| 3426 | perror("malloc cmd_ctx"); |
| 3427 | goto error; |
| 3428 | } |
| 3429 | |
| 3430 | /* Allocate data buffer for reception */ |
| 3431 | cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg)); |
| 3432 | if (cmd_ctx->lsm == NULL) { |
| 3433 | perror("malloc cmd_ctx->lsm"); |
| 3434 | goto error; |
| 3435 | } |
| 3436 | |
| 3437 | cmd_ctx->llm = NULL; |
| 3438 | cmd_ctx->session = NULL; |
| 3439 | |
| 3440 | /* |
| 3441 | * Data is received from the lttng client. The struct |
| 3442 | * lttcomm_session_msg (lsm) contains the command and data request of |
| 3443 | * the client. |
| 3444 | */ |
| 3445 | DBG("Receiving data from client ..."); |
| 3446 | ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, |
| 3447 | sizeof(struct lttcomm_session_msg)); |
| 3448 | if (ret <= 0) { |
| 3449 | DBG("Nothing recv() from client... continuing"); |
| 3450 | close(sock); |
| 3451 | free(cmd_ctx); |
| 3452 | continue; |
| 3453 | } |
| 3454 | |
| 3455 | // TODO: Validate cmd_ctx including sanity check for |
| 3456 | // security purpose. |
| 3457 | |
| 3458 | /* |
| 3459 | * This function dispatch the work to the kernel or userspace tracer |
| 3460 | * libs and fill the lttcomm_lttng_msg data structure of all the needed |
| 3461 | * informations for the client. The command context struct contains |
| 3462 | * everything this function may needs. |
| 3463 | */ |
| 3464 | ret = process_client_msg(cmd_ctx); |
| 3465 | if (ret < 0) { |
| 3466 | /* |
| 3467 | * TODO: Inform client somehow of the fatal error. At |
| 3468 | * this point, ret < 0 means that a malloc failed |
| 3469 | * (ENOMEM). Error detected but still accept command. |
| 3470 | */ |
| 3471 | clean_command_ctx(&cmd_ctx); |
| 3472 | continue; |
| 3473 | } |
| 3474 | |
| 3475 | DBG("Sending response (size: %d, retcode: %s)", |
| 3476 | cmd_ctx->lttng_msg_size, |
| 3477 | lttng_strerror(-cmd_ctx->llm->ret_code)); |
| 3478 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); |
| 3479 | if (ret < 0) { |
| 3480 | ERR("Failed to send data back to client"); |
| 3481 | } |
| 3482 | |
| 3483 | clean_command_ctx(&cmd_ctx); |
| 3484 | |
| 3485 | /* End of transmission */ |
| 3486 | close(sock); |
| 3487 | } |
| 3488 | |
| 3489 | error: |
| 3490 | DBG("Client thread dying"); |
| 3491 | unlink(client_unix_sock_path); |
| 3492 | close(client_sock); |
| 3493 | close(sock); |
| 3494 | |
| 3495 | lttng_poll_clean(&events); |
| 3496 | clean_command_ctx(&cmd_ctx); |
| 3497 | return NULL; |
| 3498 | } |
| 3499 | |
| 3500 | |
| 3501 | /* |
| 3502 | * usage function on stderr |
| 3503 | */ |
| 3504 | static void usage(void) |
| 3505 | { |
| 3506 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
| 3507 | fprintf(stderr, " -h, --help Display this usage.\n"); |
| 3508 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); |
| 3509 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n"); |
| 3510 | fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n"); |
| 3511 | fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n"); |
| 3512 | fprintf(stderr, " --ustconsumerd-err-sock PATH Specify path for the UST consumer error socket\n"); |
| 3513 | fprintf(stderr, " --ustconsumerd-cmd-sock PATH Specify path for the UST consumer command socket\n"); |
| 3514 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); |
| 3515 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); |
| 3516 | fprintf(stderr, " -V, --version Show version number.\n"); |
| 3517 | fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n"); |
| 3518 | fprintf(stderr, " -q, --quiet No output at all.\n"); |
| 3519 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); |
| 3520 | fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n"); |
| 3521 | } |
| 3522 | |
| 3523 | /* |
| 3524 | * daemon argument parsing |
| 3525 | */ |
| 3526 | static int parse_args(int argc, char **argv) |
| 3527 | { |
| 3528 | int c; |
| 3529 | |
| 3530 | static struct option long_options[] = { |
| 3531 | { "client-sock", 1, 0, 'c' }, |
| 3532 | { "apps-sock", 1, 0, 'a' }, |
| 3533 | { "kconsumerd-cmd-sock", 1, 0, 'C' }, |
| 3534 | { "kconsumerd-err-sock", 1, 0, 'E' }, |
| 3535 | { "ustconsumerd-cmd-sock", 1, 0, 'D' }, |
| 3536 | { "ustconsumerd-err-sock", 1, 0, 'F' }, |
| 3537 | { "daemonize", 0, 0, 'd' }, |
| 3538 | { "sig-parent", 0, 0, 'S' }, |
| 3539 | { "help", 0, 0, 'h' }, |
| 3540 | { "group", 1, 0, 'g' }, |
| 3541 | { "version", 0, 0, 'V' }, |
| 3542 | { "quiet", 0, 0, 'q' }, |
| 3543 | { "verbose", 0, 0, 'v' }, |
| 3544 | { "verbose-consumer", 0, 0, 'Z' }, |
| 3545 | { NULL, 0, 0, 0 } |
| 3546 | }; |
| 3547 | |
| 3548 | while (1) { |
| 3549 | int option_index = 0; |
| 3550 | c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:C:E:D:F:Z", |
| 3551 | long_options, &option_index); |
| 3552 | if (c == -1) { |
| 3553 | break; |
| 3554 | } |
| 3555 | |
| 3556 | switch (c) { |
| 3557 | case 0: |
| 3558 | fprintf(stderr, "option %s", long_options[option_index].name); |
| 3559 | if (optarg) { |
| 3560 | fprintf(stderr, " with arg %s\n", optarg); |
| 3561 | } |
| 3562 | break; |
| 3563 | case 'c': |
| 3564 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
| 3565 | break; |
| 3566 | case 'a': |
| 3567 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); |
| 3568 | break; |
| 3569 | case 'd': |
| 3570 | opt_daemon = 1; |
| 3571 | break; |
| 3572 | case 'g': |
| 3573 | opt_tracing_group = strdup(optarg); |
| 3574 | break; |
| 3575 | case 'h': |
| 3576 | usage(); |
| 3577 | exit(EXIT_FAILURE); |
| 3578 | case 'V': |
| 3579 | fprintf(stdout, "%s\n", VERSION); |
| 3580 | exit(EXIT_SUCCESS); |
| 3581 | case 'S': |
| 3582 | opt_sig_parent = 1; |
| 3583 | break; |
| 3584 | case 'E': |
| 3585 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
| 3586 | break; |
| 3587 | case 'C': |
| 3588 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
| 3589 | break; |
| 3590 | case 'F': |
| 3591 | snprintf(ustconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
| 3592 | break; |
| 3593 | case 'D': |
| 3594 | snprintf(ustconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
| 3595 | break; |
| 3596 | case 'q': |
| 3597 | opt_quiet = 1; |
| 3598 | break; |
| 3599 | case 'v': |
| 3600 | /* Verbose level can increase using multiple -v */ |
| 3601 | opt_verbose += 1; |
| 3602 | break; |
| 3603 | case 'Z': |
| 3604 | opt_verbose_consumer += 1; |
| 3605 | break; |
| 3606 | default: |
| 3607 | /* Unknown option or other error. |
| 3608 | * Error is printed by getopt, just return */ |
| 3609 | return -1; |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | return 0; |
| 3614 | } |
| 3615 | |
| 3616 | /* |
| 3617 | * Creates the two needed socket by the daemon. |
| 3618 | * apps_sock - The communication socket for all UST apps. |
| 3619 | * client_sock - The communication of the cli tool (lttng). |
| 3620 | */ |
| 3621 | static int init_daemon_socket(void) |
| 3622 | { |
| 3623 | int ret = 0; |
| 3624 | mode_t old_umask; |
| 3625 | |
| 3626 | old_umask = umask(0); |
| 3627 | |
| 3628 | /* Create client tool unix socket */ |
| 3629 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
| 3630 | if (client_sock < 0) { |
| 3631 | ERR("Create unix sock failed: %s", client_unix_sock_path); |
| 3632 | ret = -1; |
| 3633 | goto end; |
| 3634 | } |
| 3635 | |
| 3636 | /* File permission MUST be 660 */ |
| 3637 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 3638 | if (ret < 0) { |
| 3639 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
| 3640 | perror("chmod"); |
| 3641 | goto end; |
| 3642 | } |
| 3643 | |
| 3644 | /* Create the application unix socket */ |
| 3645 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
| 3646 | if (apps_sock < 0) { |
| 3647 | ERR("Create unix sock failed: %s", apps_unix_sock_path); |
| 3648 | ret = -1; |
| 3649 | goto end; |
| 3650 | } |
| 3651 | |
| 3652 | /* File permission MUST be 666 */ |
| 3653 | ret = chmod(apps_unix_sock_path, |
| 3654 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 3655 | if (ret < 0) { |
| 3656 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
| 3657 | perror("chmod"); |
| 3658 | goto end; |
| 3659 | } |
| 3660 | |
| 3661 | end: |
| 3662 | umask(old_umask); |
| 3663 | return ret; |
| 3664 | } |
| 3665 | |
| 3666 | /* |
| 3667 | * Check if the global socket is available, and if a daemon is answering at the |
| 3668 | * other side. If yes, error is returned. |
| 3669 | */ |
| 3670 | static int check_existing_daemon(void) |
| 3671 | { |
| 3672 | if (access(client_unix_sock_path, F_OK) < 0 && |
| 3673 | access(apps_unix_sock_path, F_OK) < 0) { |
| 3674 | return 0; |
| 3675 | } |
| 3676 | |
| 3677 | /* Is there anybody out there ? */ |
| 3678 | if (lttng_session_daemon_alive()) { |
| 3679 | return -EEXIST; |
| 3680 | } else { |
| 3681 | return 0; |
| 3682 | } |
| 3683 | } |
| 3684 | |
| 3685 | /* |
| 3686 | * Set the tracing group gid onto the client socket. |
| 3687 | * |
| 3688 | * Race window between mkdir and chown is OK because we are going from more |
| 3689 | * permissive (root.root) to les permissive (root.tracing). |
| 3690 | */ |
| 3691 | static int set_permissions(void) |
| 3692 | { |
| 3693 | int ret; |
| 3694 | gid_t gid; |
| 3695 | |
| 3696 | gid = allowed_group(); |
| 3697 | if (gid < 0) { |
| 3698 | if (is_root) { |
| 3699 | WARN("No tracing group detected"); |
| 3700 | ret = 0; |
| 3701 | } else { |
| 3702 | ERR("Missing tracing group. Aborting execution."); |
| 3703 | ret = -1; |
| 3704 | } |
| 3705 | goto end; |
| 3706 | } |
| 3707 | |
| 3708 | /* Set lttng run dir */ |
| 3709 | ret = chown(LTTNG_RUNDIR, 0, gid); |
| 3710 | if (ret < 0) { |
| 3711 | ERR("Unable to set group on " LTTNG_RUNDIR); |
| 3712 | perror("chown"); |
| 3713 | } |
| 3714 | |
| 3715 | /* lttng client socket path */ |
| 3716 | ret = chown(client_unix_sock_path, 0, gid); |
| 3717 | if (ret < 0) { |
| 3718 | ERR("Unable to set group on %s", client_unix_sock_path); |
| 3719 | perror("chown"); |
| 3720 | } |
| 3721 | |
| 3722 | /* kconsumer error socket path */ |
| 3723 | ret = chown(kconsumer_data.err_unix_sock_path, 0, gid); |
| 3724 | if (ret < 0) { |
| 3725 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
| 3726 | perror("chown"); |
| 3727 | } |
| 3728 | |
| 3729 | /* ustconsumer error socket path */ |
| 3730 | ret = chown(ustconsumer_data.err_unix_sock_path, 0, gid); |
| 3731 | if (ret < 0) { |
| 3732 | ERR("Unable to set group on %s", ustconsumer_data.err_unix_sock_path); |
| 3733 | perror("chown"); |
| 3734 | } |
| 3735 | |
| 3736 | DBG("All permissions are set"); |
| 3737 | |
| 3738 | end: |
| 3739 | return ret; |
| 3740 | } |
| 3741 | |
| 3742 | /* |
| 3743 | * Create the pipe used to wake up the kernel thread. |
| 3744 | */ |
| 3745 | static int create_kernel_poll_pipe(void) |
| 3746 | { |
| 3747 | return pipe2(kernel_poll_pipe, O_CLOEXEC); |
| 3748 | } |
| 3749 | |
| 3750 | /* |
| 3751 | * Create the application command pipe to wake thread_manage_apps. |
| 3752 | */ |
| 3753 | static int create_apps_cmd_pipe(void) |
| 3754 | { |
| 3755 | return pipe2(apps_cmd_pipe, O_CLOEXEC); |
| 3756 | } |
| 3757 | |
| 3758 | /* |
| 3759 | * Create the lttng run directory needed for all global sockets and pipe. |
| 3760 | */ |
| 3761 | static int create_lttng_rundir(void) |
| 3762 | { |
| 3763 | int ret; |
| 3764 | |
| 3765 | ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG ); |
| 3766 | if (ret < 0) { |
| 3767 | if (errno != EEXIST) { |
| 3768 | ERR("Unable to create " LTTNG_RUNDIR); |
| 3769 | goto error; |
| 3770 | } else { |
| 3771 | ret = 0; |
| 3772 | } |
| 3773 | } |
| 3774 | |
| 3775 | error: |
| 3776 | return ret; |
| 3777 | } |
| 3778 | |
| 3779 | /* |
| 3780 | * Setup sockets and directory needed by the kconsumerd communication with the |
| 3781 | * session daemon. |
| 3782 | */ |
| 3783 | static int set_consumer_sockets(struct consumer_data *consumer_data) |
| 3784 | { |
| 3785 | int ret; |
| 3786 | const char *path = consumer_data->type == LTTNG_CONSUMER_KERNEL ? |
| 3787 | KCONSUMERD_PATH : USTCONSUMERD_PATH; |
| 3788 | |
| 3789 | if (strlen(consumer_data->err_unix_sock_path) == 0) { |
| 3790 | snprintf(consumer_data->err_unix_sock_path, PATH_MAX, |
| 3791 | consumer_data->type == LTTNG_CONSUMER_KERNEL ? |
| 3792 | KCONSUMERD_ERR_SOCK_PATH : |
| 3793 | USTCONSUMERD_ERR_SOCK_PATH); |
| 3794 | } |
| 3795 | |
| 3796 | if (strlen(consumer_data->cmd_unix_sock_path) == 0) { |
| 3797 | snprintf(consumer_data->cmd_unix_sock_path, PATH_MAX, |
| 3798 | consumer_data->type == LTTNG_CONSUMER_KERNEL ? |
| 3799 | KCONSUMERD_CMD_SOCK_PATH : |
| 3800 | USTCONSUMERD_CMD_SOCK_PATH); |
| 3801 | } |
| 3802 | |
| 3803 | ret = mkdir(path, S_IRWXU | S_IRWXG); |
| 3804 | if (ret < 0) { |
| 3805 | if (errno != EEXIST) { |
| 3806 | ERR("Failed to create %s", path); |
| 3807 | goto error; |
| 3808 | } |
| 3809 | ret = 0; |
| 3810 | } |
| 3811 | |
| 3812 | /* Create the kconsumerd error unix socket */ |
| 3813 | consumer_data->err_sock = |
| 3814 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); |
| 3815 | if (consumer_data->err_sock < 0) { |
| 3816 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); |
| 3817 | ret = -1; |
| 3818 | goto error; |
| 3819 | } |
| 3820 | |
| 3821 | /* File permission MUST be 660 */ |
| 3822 | ret = chmod(consumer_data->err_unix_sock_path, |
| 3823 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 3824 | if (ret < 0) { |
| 3825 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
| 3826 | perror("chmod"); |
| 3827 | goto error; |
| 3828 | } |
| 3829 | |
| 3830 | error: |
| 3831 | return ret; |
| 3832 | } |
| 3833 | |
| 3834 | /* |
| 3835 | * Signal handler for the daemon |
| 3836 | * |
| 3837 | * Simply stop all worker threads, leaving main() return gracefully after |
| 3838 | * joining all threads and calling cleanup(). |
| 3839 | */ |
| 3840 | static void sighandler(int sig) |
| 3841 | { |
| 3842 | switch (sig) { |
| 3843 | case SIGPIPE: |
| 3844 | DBG("SIGPIPE catched"); |
| 3845 | return; |
| 3846 | case SIGINT: |
| 3847 | DBG("SIGINT catched"); |
| 3848 | stop_threads(); |
| 3849 | break; |
| 3850 | case SIGTERM: |
| 3851 | DBG("SIGTERM catched"); |
| 3852 | stop_threads(); |
| 3853 | break; |
| 3854 | default: |
| 3855 | break; |
| 3856 | } |
| 3857 | } |
| 3858 | |
| 3859 | /* |
| 3860 | * Setup signal handler for : |
| 3861 | * SIGINT, SIGTERM, SIGPIPE |
| 3862 | */ |
| 3863 | static int set_signal_handler(void) |
| 3864 | { |
| 3865 | int ret = 0; |
| 3866 | struct sigaction sa; |
| 3867 | sigset_t sigset; |
| 3868 | |
| 3869 | if ((ret = sigemptyset(&sigset)) < 0) { |
| 3870 | perror("sigemptyset"); |
| 3871 | return ret; |
| 3872 | } |
| 3873 | |
| 3874 | sa.sa_handler = sighandler; |
| 3875 | sa.sa_mask = sigset; |
| 3876 | sa.sa_flags = 0; |
| 3877 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
| 3878 | perror("sigaction"); |
| 3879 | return ret; |
| 3880 | } |
| 3881 | |
| 3882 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
| 3883 | perror("sigaction"); |
| 3884 | return ret; |
| 3885 | } |
| 3886 | |
| 3887 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
| 3888 | perror("sigaction"); |
| 3889 | return ret; |
| 3890 | } |
| 3891 | |
| 3892 | DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT"); |
| 3893 | |
| 3894 | return ret; |
| 3895 | } |
| 3896 | |
| 3897 | /* |
| 3898 | * Set open files limit to unlimited. This daemon can open a large number of |
| 3899 | * file descriptors in order to consumer multiple kernel traces. |
| 3900 | */ |
| 3901 | static void set_ulimit(void) |
| 3902 | { |
| 3903 | int ret; |
| 3904 | struct rlimit lim; |
| 3905 | |
| 3906 | /* The kernel does not allowed an infinite limit for open files */ |
| 3907 | lim.rlim_cur = 65535; |
| 3908 | lim.rlim_max = 65535; |
| 3909 | |
| 3910 | ret = setrlimit(RLIMIT_NOFILE, &lim); |
| 3911 | if (ret < 0) { |
| 3912 | perror("failed to set open files limit"); |
| 3913 | } |
| 3914 | } |
| 3915 | |
| 3916 | /* |
| 3917 | * main |
| 3918 | */ |
| 3919 | int main(int argc, char **argv) |
| 3920 | { |
| 3921 | int ret = 0; |
| 3922 | void *status; |
| 3923 | const char *home_path; |
| 3924 | |
| 3925 | /* Create thread quit pipe */ |
| 3926 | if ((ret = init_thread_quit_pipe()) < 0) { |
| 3927 | goto error; |
| 3928 | } |
| 3929 | |
| 3930 | /* Parse arguments */ |
| 3931 | progname = argv[0]; |
| 3932 | if ((ret = parse_args(argc, argv) < 0)) { |
| 3933 | goto error; |
| 3934 | } |
| 3935 | |
| 3936 | /* Daemonize */ |
| 3937 | if (opt_daemon) { |
| 3938 | ret = daemon(0, 0); |
| 3939 | if (ret < 0) { |
| 3940 | perror("daemon"); |
| 3941 | goto error; |
| 3942 | } |
| 3943 | } |
| 3944 | |
| 3945 | /* Check if daemon is UID = 0 */ |
| 3946 | is_root = !getuid(); |
| 3947 | |
| 3948 | if (is_root) { |
| 3949 | ret = create_lttng_rundir(); |
| 3950 | if (ret < 0) { |
| 3951 | goto error; |
| 3952 | } |
| 3953 | |
| 3954 | if (strlen(apps_unix_sock_path) == 0) { |
| 3955 | snprintf(apps_unix_sock_path, PATH_MAX, |
| 3956 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); |
| 3957 | } |
| 3958 | |
| 3959 | if (strlen(client_unix_sock_path) == 0) { |
| 3960 | snprintf(client_unix_sock_path, PATH_MAX, |
| 3961 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); |
| 3962 | } |
| 3963 | |
| 3964 | /* Set global SHM for ust */ |
| 3965 | if (strlen(wait_shm_path) == 0) { |
| 3966 | snprintf(wait_shm_path, PATH_MAX, |
| 3967 | DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH); |
| 3968 | } |
| 3969 | } else { |
| 3970 | home_path = get_home_dir(); |
| 3971 | if (home_path == NULL) { |
| 3972 | /* TODO: Add --socket PATH option */ |
| 3973 | ERR("Can't get HOME directory for sockets creation."); |
| 3974 | ret = -EPERM; |
| 3975 | goto error; |
| 3976 | } |
| 3977 | |
| 3978 | if (strlen(apps_unix_sock_path) == 0) { |
| 3979 | snprintf(apps_unix_sock_path, PATH_MAX, |
| 3980 | DEFAULT_HOME_APPS_UNIX_SOCK, home_path); |
| 3981 | } |
| 3982 | |
| 3983 | /* Set the cli tool unix socket path */ |
| 3984 | if (strlen(client_unix_sock_path) == 0) { |
| 3985 | snprintf(client_unix_sock_path, PATH_MAX, |
| 3986 | DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path); |
| 3987 | } |
| 3988 | |
| 3989 | /* Set global SHM for ust */ |
| 3990 | if (strlen(wait_shm_path) == 0) { |
| 3991 | snprintf(wait_shm_path, PATH_MAX, |
| 3992 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, geteuid()); |
| 3993 | } |
| 3994 | } |
| 3995 | |
| 3996 | DBG("Client socket path %s", client_unix_sock_path); |
| 3997 | DBG("Application socket path %s", apps_unix_sock_path); |
| 3998 | |
| 3999 | /* |
| 4000 | * See if daemon already exist. |
| 4001 | */ |
| 4002 | if ((ret = check_existing_daemon()) < 0) { |
| 4003 | ERR("Already running daemon.\n"); |
| 4004 | /* |
| 4005 | * We do not goto exit because we must not cleanup() |
| 4006 | * because a daemon is already running. |
| 4007 | */ |
| 4008 | goto error; |
| 4009 | } |
| 4010 | |
| 4011 | /* After this point, we can safely call cleanup() with "goto exit" */ |
| 4012 | |
| 4013 | /* |
| 4014 | * These actions must be executed as root. We do that *after* setting up |
| 4015 | * the sockets path because we MUST make the check for another daemon using |
| 4016 | * those paths *before* trying to set the kernel consumer sockets and init |
| 4017 | * kernel tracer. |
| 4018 | */ |
| 4019 | if (is_root) { |
| 4020 | ret = set_consumer_sockets(&kconsumer_data); |
| 4021 | if (ret < 0) { |
| 4022 | goto exit; |
| 4023 | } |
| 4024 | ret = set_consumer_sockets(&ustconsumer_data); |
| 4025 | if (ret < 0) { |
| 4026 | goto exit; |
| 4027 | } |
| 4028 | /* Setup kernel tracer */ |
| 4029 | init_kernel_tracer(); |
| 4030 | |
| 4031 | /* Set ulimit for open files */ |
| 4032 | set_ulimit(); |
| 4033 | } |
| 4034 | |
| 4035 | if ((ret = set_signal_handler()) < 0) { |
| 4036 | goto exit; |
| 4037 | } |
| 4038 | |
| 4039 | /* Setup the needed unix socket */ |
| 4040 | if ((ret = init_daemon_socket()) < 0) { |
| 4041 | goto exit; |
| 4042 | } |
| 4043 | |
| 4044 | /* Set credentials to socket */ |
| 4045 | if (is_root && ((ret = set_permissions()) < 0)) { |
| 4046 | goto exit; |
| 4047 | } |
| 4048 | |
| 4049 | /* Get parent pid if -S, --sig-parent is specified. */ |
| 4050 | if (opt_sig_parent) { |
| 4051 | ppid = getppid(); |
| 4052 | } |
| 4053 | |
| 4054 | /* Setup the kernel pipe for waking up the kernel thread */ |
| 4055 | if ((ret = create_kernel_poll_pipe()) < 0) { |
| 4056 | goto exit; |
| 4057 | } |
| 4058 | |
| 4059 | /* Setup the thread apps communication pipe. */ |
| 4060 | if ((ret = create_apps_cmd_pipe()) < 0) { |
| 4061 | goto exit; |
| 4062 | } |
| 4063 | |
| 4064 | /* Init UST command queue. */ |
| 4065 | cds_wfq_init(&ust_cmd_queue.queue); |
| 4066 | |
| 4067 | /* |
| 4068 | * Get session list pointer. This pointer MUST NOT be free(). This list is |
| 4069 | * statically declared in session.c |
| 4070 | */ |
| 4071 | session_list_ptr = session_get_list(); |
| 4072 | |
| 4073 | /* Set up max poll set size */ |
| 4074 | lttng_poll_set_max_size(); |
| 4075 | |
| 4076 | /* Create thread to manage the client socket */ |
| 4077 | ret = pthread_create(&client_thread, NULL, |
| 4078 | thread_manage_clients, (void *) NULL); |
| 4079 | if (ret != 0) { |
| 4080 | perror("pthread_create clients"); |
| 4081 | goto exit_client; |
| 4082 | } |
| 4083 | |
| 4084 | /* Create thread to dispatch registration */ |
| 4085 | ret = pthread_create(&dispatch_thread, NULL, |
| 4086 | thread_dispatch_ust_registration, (void *) NULL); |
| 4087 | if (ret != 0) { |
| 4088 | perror("pthread_create dispatch"); |
| 4089 | goto exit_dispatch; |
| 4090 | } |
| 4091 | |
| 4092 | /* Create thread to manage application registration. */ |
| 4093 | ret = pthread_create(®_apps_thread, NULL, |
| 4094 | thread_registration_apps, (void *) NULL); |
| 4095 | if (ret != 0) { |
| 4096 | perror("pthread_create registration"); |
| 4097 | goto exit_reg_apps; |
| 4098 | } |
| 4099 | |
| 4100 | /* Create thread to manage application socket */ |
| 4101 | ret = pthread_create(&apps_thread, NULL, |
| 4102 | thread_manage_apps, (void *) NULL); |
| 4103 | if (ret != 0) { |
| 4104 | perror("pthread_create apps"); |
| 4105 | goto exit_apps; |
| 4106 | } |
| 4107 | |
| 4108 | /* Create kernel thread to manage kernel event */ |
| 4109 | ret = pthread_create(&kernel_thread, NULL, |
| 4110 | thread_manage_kernel, (void *) NULL); |
| 4111 | if (ret != 0) { |
| 4112 | perror("pthread_create kernel"); |
| 4113 | goto exit_kernel; |
| 4114 | } |
| 4115 | |
| 4116 | ret = pthread_join(kernel_thread, &status); |
| 4117 | if (ret != 0) { |
| 4118 | perror("pthread_join"); |
| 4119 | goto error; /* join error, exit without cleanup */ |
| 4120 | } |
| 4121 | |
| 4122 | exit_kernel: |
| 4123 | ret = pthread_join(apps_thread, &status); |
| 4124 | if (ret != 0) { |
| 4125 | perror("pthread_join"); |
| 4126 | goto error; /* join error, exit without cleanup */ |
| 4127 | } |
| 4128 | |
| 4129 | exit_apps: |
| 4130 | ret = pthread_join(reg_apps_thread, &status); |
| 4131 | if (ret != 0) { |
| 4132 | perror("pthread_join"); |
| 4133 | goto error; /* join error, exit without cleanup */ |
| 4134 | } |
| 4135 | |
| 4136 | exit_reg_apps: |
| 4137 | ret = pthread_join(dispatch_thread, &status); |
| 4138 | if (ret != 0) { |
| 4139 | perror("pthread_join"); |
| 4140 | goto error; /* join error, exit without cleanup */ |
| 4141 | } |
| 4142 | |
| 4143 | exit_dispatch: |
| 4144 | ret = pthread_join(client_thread, &status); |
| 4145 | if (ret != 0) { |
| 4146 | perror("pthread_join"); |
| 4147 | goto error; /* join error, exit without cleanup */ |
| 4148 | } |
| 4149 | |
| 4150 | ret = join_consumer_thread(&kconsumer_data); |
| 4151 | if (ret != 0) { |
| 4152 | perror("join_consumer"); |
| 4153 | goto error; /* join error, exit without cleanup */ |
| 4154 | } |
| 4155 | |
| 4156 | exit_client: |
| 4157 | exit: |
| 4158 | /* |
| 4159 | * cleanup() is called when no other thread is running. |
| 4160 | */ |
| 4161 | cleanup(); |
| 4162 | if (!ret) |
| 4163 | exit(EXIT_SUCCESS); |
| 4164 | error: |
| 4165 | exit(EXIT_FAILURE); |
| 4166 | } |