| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple 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/ipc.h> |
| 31 | #include <sys/mount.h> |
| 32 | #include <sys/shm.h> |
| 33 | #include <sys/socket.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/time.h> |
| 37 | #include <sys/resource.h> |
| 38 | #include <unistd.h> |
| 39 | |
| 40 | #include <urcu/list.h> /* URCU list library (-lurcu) */ |
| 41 | #include <lttng/lttng.h> |
| 42 | |
| 43 | #include "liblttsessiondcomm.h" |
| 44 | #include "ltt-sessiond.h" |
| 45 | #include "lttngerr.h" |
| 46 | #include "kernel-ctl.h" |
| 47 | #include "ust-ctl.h" |
| 48 | #include "session.h" |
| 49 | #include "traceable-app.h" |
| 50 | #include "lttng-kconsumerd.h" |
| 51 | #include "libustctl.h" |
| 52 | #include "utils.h" |
| 53 | |
| 54 | /* |
| 55 | * TODO: |
| 56 | * teardown: signal SIGTERM handler -> write into pipe. Threads waits |
| 57 | * with epoll on pipe and on other pipes/sockets for commands. Main |
| 58 | * simply waits on pthread join. |
| 59 | */ |
| 60 | |
| 61 | /* Const values */ |
| 62 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
| 63 | const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP; |
| 64 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; |
| 65 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; |
| 66 | |
| 67 | /* Variables */ |
| 68 | int opt_verbose; /* Not static for lttngerr.h */ |
| 69 | int opt_quiet; /* Not static for lttngerr.h */ |
| 70 | const char *progname; |
| 71 | const char *opt_tracing_group; |
| 72 | static int opt_sig_parent; |
| 73 | static int opt_daemon; |
| 74 | static int is_root; /* Set to 1 if the daemon is running as root */ |
| 75 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
| 76 | static pid_t kconsumerd_pid; |
| 77 | |
| 78 | static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */ |
| 79 | static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */ |
| 80 | static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */ |
| 81 | static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */ |
| 82 | |
| 83 | /* Sockets and FDs */ |
| 84 | static int client_sock; |
| 85 | static int apps_sock; |
| 86 | static int kconsumerd_err_sock; |
| 87 | static int kconsumerd_cmd_sock; |
| 88 | static int kernel_tracer_fd; |
| 89 | |
| 90 | /* Pthread, Mutexes and Semaphores */ |
| 91 | static pthread_t kconsumerd_thread; |
| 92 | static pthread_t apps_thread; |
| 93 | static pthread_t client_thread; |
| 94 | static sem_t kconsumerd_sem; |
| 95 | |
| 96 | static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */ |
| 97 | |
| 98 | /* |
| 99 | * teardown_kernel_session |
| 100 | * |
| 101 | * Complete teardown of a kernel session. This free all data structure |
| 102 | * related to a kernel session and update counter. |
| 103 | */ |
| 104 | static void teardown_kernel_session(struct ltt_session *session) |
| 105 | { |
| 106 | if (session->kernel_session != NULL) { |
| 107 | DBG("Tearing down kernel session"); |
| 108 | trace_destroy_kernel_session(session->kernel_session); |
| 109 | /* Extra precaution */ |
| 110 | session->kernel_session = NULL; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * cleanup |
| 116 | * |
| 117 | * Cleanup the daemon on exit |
| 118 | */ |
| 119 | static void cleanup() |
| 120 | { |
| 121 | int ret; |
| 122 | char *cmd; |
| 123 | struct ltt_session *sess; |
| 124 | |
| 125 | DBG("Cleaning up"); |
| 126 | |
| 127 | /* <fun> */ |
| 128 | MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0); |
| 129 | MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0); |
| 130 | /* </fun> */ |
| 131 | |
| 132 | /* Stopping all threads */ |
| 133 | DBG("Terminating all threads"); |
| 134 | pthread_cancel(client_thread); |
| 135 | pthread_cancel(apps_thread); |
| 136 | if (kconsumerd_pid != 0) { |
| 137 | pthread_cancel(kconsumerd_thread); |
| 138 | } |
| 139 | |
| 140 | DBG("Unlinking all unix socket"); |
| 141 | unlink(client_unix_sock_path); |
| 142 | unlink(apps_unix_sock_path); |
| 143 | unlink(kconsumerd_err_unix_sock_path); |
| 144 | |
| 145 | DBG("Removing %s directory", LTTNG_RUNDIR); |
| 146 | ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR); |
| 147 | if (ret < 0) { |
| 148 | ERR("asprintf failed. Something is really wrong!"); |
| 149 | } |
| 150 | |
| 151 | /* Remove lttng run directory */ |
| 152 | ret = system(cmd); |
| 153 | if (ret < 0) { |
| 154 | ERR("Unable to clean " LTTNG_RUNDIR); |
| 155 | } |
| 156 | |
| 157 | DBG("Cleaning up all session"); |
| 158 | /* Cleanup ALL session */ |
| 159 | cds_list_for_each_entry(sess, <t_session_list.head, list) { |
| 160 | teardown_kernel_session(sess); |
| 161 | // TODO complete session cleanup (including UST) |
| 162 | } |
| 163 | |
| 164 | DBG("Closing kernel fd"); |
| 165 | close(kernel_tracer_fd); |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * send_unix_sock |
| 170 | * |
| 171 | * Send data on a unix socket using the liblttsessiondcomm API. |
| 172 | * |
| 173 | * Return lttcomm error code. |
| 174 | */ |
| 175 | static int send_unix_sock(int sock, void *buf, size_t len) |
| 176 | { |
| 177 | /* Check valid length */ |
| 178 | if (len <= 0) { |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | return lttcomm_send_unix_sock(sock, buf, len); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * clean_command_ctx |
| 187 | * |
| 188 | * Free memory of a command context structure. |
| 189 | */ |
| 190 | static void clean_command_ctx(struct command_ctx *cmd_ctx) |
| 191 | { |
| 192 | DBG("Clean command context structure %p", cmd_ctx); |
| 193 | if (cmd_ctx) { |
| 194 | if (cmd_ctx->llm) { |
| 195 | free(cmd_ctx->llm); |
| 196 | } |
| 197 | if (cmd_ctx->lsm) { |
| 198 | free(cmd_ctx->lsm); |
| 199 | } |
| 200 | free(cmd_ctx); |
| 201 | cmd_ctx = NULL; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * send_kconsumerd_fds |
| 207 | * |
| 208 | * Send all stream fds of the kernel session to the consumer. |
| 209 | */ |
| 210 | static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session) |
| 211 | { |
| 212 | int ret; |
| 213 | size_t nb_fd; |
| 214 | struct ltt_kernel_stream *stream; |
| 215 | struct ltt_kernel_channel *chan; |
| 216 | struct lttcomm_kconsumerd_header lkh; |
| 217 | struct lttcomm_kconsumerd_msg lkm; |
| 218 | |
| 219 | nb_fd = session->stream_count_global; |
| 220 | |
| 221 | /* Setup header */ |
| 222 | lkh.payload_size = (nb_fd + 1) * sizeof(struct lttcomm_kconsumerd_msg); |
| 223 | lkh.cmd_type = ADD_STREAM; |
| 224 | |
| 225 | DBG("Sending kconsumerd header"); |
| 226 | |
| 227 | ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header)); |
| 228 | if (ret < 0) { |
| 229 | perror("send kconsumerd header"); |
| 230 | goto error; |
| 231 | } |
| 232 | |
| 233 | DBG("Sending metadata stream fd"); |
| 234 | |
| 235 | /* Send metadata stream fd first */ |
| 236 | lkm.fd = session->metadata_stream_fd; |
| 237 | lkm.state = ACTIVE_FD; |
| 238 | lkm.max_sb_size = session->metadata->conf->attr.subbuf_size; |
| 239 | strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX); |
| 240 | |
| 241 | ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm)); |
| 242 | if (ret < 0) { |
| 243 | perror("send kconsumerd fd"); |
| 244 | goto error; |
| 245 | } |
| 246 | |
| 247 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
| 248 | cds_list_for_each_entry(stream, &chan->stream_list.head, list) { |
| 249 | lkm.fd = stream->fd; |
| 250 | lkm.state = stream->state; |
| 251 | lkm.max_sb_size = chan->channel->attr.subbuf_size; |
| 252 | strncpy(lkm.path_name, stream->pathname, PATH_MAX); |
| 253 | |
| 254 | DBG("Sending fd %d to kconsumerd", lkm.fd); |
| 255 | |
| 256 | ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm)); |
| 257 | if (ret < 0) { |
| 258 | perror("send kconsumerd fd"); |
| 259 | goto error; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | DBG("Kconsumerd fds sent"); |
| 265 | |
| 266 | return 0; |
| 267 | |
| 268 | error: |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * create_trace_dir |
| 274 | * |
| 275 | * Create the trace output directory. |
| 276 | */ |
| 277 | static int create_trace_dir(struct ltt_kernel_session *session) |
| 278 | { |
| 279 | int ret; |
| 280 | struct ltt_kernel_channel *chan; |
| 281 | |
| 282 | /* Create all channel directories */ |
| 283 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
| 284 | DBG("Creating trace directory at %s", chan->pathname); |
| 285 | ret = mkdir_recursive(chan->pathname, S_IRWXU | S_IRWXG ); |
| 286 | if (ret < 0) { |
| 287 | if (ret != EEXIST) { |
| 288 | ERR("Trace directory creation error"); |
| 289 | goto error; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | |
| 296 | error: |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * ust_connect_app |
| 302 | * |
| 303 | * Return a socket connected to the libust communication socket |
| 304 | * of the application identified by the pid. |
| 305 | * |
| 306 | * If the pid is not found in the traceable list, |
| 307 | * return -1 to indicate error. |
| 308 | */ |
| 309 | static int ust_connect_app(pid_t pid) |
| 310 | { |
| 311 | int sock; |
| 312 | struct ltt_traceable_app *lta; |
| 313 | |
| 314 | DBG("Connect to application pid %d", pid); |
| 315 | |
| 316 | lta = find_app_by_pid(pid); |
| 317 | if (lta == NULL) { |
| 318 | /* App not found */ |
| 319 | DBG("Application pid %d not found", pid); |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | sock = ustctl_connect_pid(lta->pid); |
| 324 | if (sock < 0) { |
| 325 | ERR("Fail connecting to the PID %d", pid); |
| 326 | } |
| 327 | |
| 328 | return sock; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * notify_apps |
| 333 | * |
| 334 | * Notify apps by writing 42 to a named pipe using name. |
| 335 | * Every applications waiting for a ltt-sessiond will be notified |
| 336 | * and re-register automatically to the session daemon. |
| 337 | * |
| 338 | * Return open or write error value. |
| 339 | */ |
| 340 | static int notify_apps(const char *name) |
| 341 | { |
| 342 | int fd; |
| 343 | int ret = -1; |
| 344 | |
| 345 | DBG("Notify the global application pipe"); |
| 346 | |
| 347 | /* Try opening the global pipe */ |
| 348 | fd = open(name, O_WRONLY); |
| 349 | if (fd < 0) { |
| 350 | goto error; |
| 351 | } |
| 352 | |
| 353 | /* Notify by writing on the pipe */ |
| 354 | ret = write(fd, "42", 2); |
| 355 | if (ret < 0) { |
| 356 | perror("write"); |
| 357 | } |
| 358 | |
| 359 | error: |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * setup_lttng_msg |
| 365 | * |
| 366 | * Setup the outgoing data buffer for the response (llm) by allocating the |
| 367 | * right amount of memory and copying the original information from the lsm |
| 368 | * structure. |
| 369 | * |
| 370 | * Return total size of the buffer pointed by buf. |
| 371 | */ |
| 372 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
| 373 | { |
| 374 | int ret, buf_size; |
| 375 | |
| 376 | buf_size = size; |
| 377 | |
| 378 | cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size); |
| 379 | if (cmd_ctx->llm == NULL) { |
| 380 | perror("malloc"); |
| 381 | ret = -ENOMEM; |
| 382 | goto error; |
| 383 | } |
| 384 | |
| 385 | /* Copy common data */ |
| 386 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; |
| 387 | cmd_ctx->llm->pid = cmd_ctx->lsm->pid; |
| 388 | |
| 389 | cmd_ctx->llm->data_size = size; |
| 390 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; |
| 391 | |
| 392 | return buf_size; |
| 393 | |
| 394 | error: |
| 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * thread_manage_kconsumerd |
| 400 | * |
| 401 | * This thread manage the kconsumerd error sent |
| 402 | * back to the session daemon. |
| 403 | */ |
| 404 | static void *thread_manage_kconsumerd(void *data) |
| 405 | { |
| 406 | int sock, ret; |
| 407 | enum lttcomm_return_code code; |
| 408 | |
| 409 | DBG("[thread] Manage kconsumerd started"); |
| 410 | |
| 411 | ret = lttcomm_listen_unix_sock(kconsumerd_err_sock); |
| 412 | if (ret < 0) { |
| 413 | goto error; |
| 414 | } |
| 415 | |
| 416 | sock = lttcomm_accept_unix_sock(kconsumerd_err_sock); |
| 417 | if (sock < 0) { |
| 418 | goto error; |
| 419 | } |
| 420 | |
| 421 | /* Getting status code from kconsumerd */ |
| 422 | ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code)); |
| 423 | if (ret <= 0) { |
| 424 | goto error; |
| 425 | } |
| 426 | |
| 427 | if (code == KCONSUMERD_COMMAND_SOCK_READY) { |
| 428 | kconsumerd_cmd_sock = lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path); |
| 429 | if (kconsumerd_cmd_sock < 0) { |
| 430 | sem_post(&kconsumerd_sem); |
| 431 | perror("kconsumerd connect"); |
| 432 | goto error; |
| 433 | } |
| 434 | /* Signal condition to tell that the kconsumerd is ready */ |
| 435 | sem_post(&kconsumerd_sem); |
| 436 | DBG("Kconsumerd command socket ready"); |
| 437 | } else { |
| 438 | DBG("Kconsumerd error when waiting for SOCK_READY : %s", |
| 439 | lttcomm_get_readable_code(-code)); |
| 440 | goto error; |
| 441 | } |
| 442 | |
| 443 | /* Wait for any kconsumerd error */ |
| 444 | ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code)); |
| 445 | if (ret <= 0) { |
| 446 | ERR("Kconsumerd closed the command socket"); |
| 447 | goto error; |
| 448 | } |
| 449 | |
| 450 | ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code)); |
| 451 | |
| 452 | error: |
| 453 | kconsumerd_pid = 0; |
| 454 | DBG("Kconsumerd thread dying"); |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * thread_manage_apps |
| 460 | * |
| 461 | * This thread manage the application socket communication |
| 462 | */ |
| 463 | static void *thread_manage_apps(void *data) |
| 464 | { |
| 465 | int sock, ret; |
| 466 | |
| 467 | /* TODO: Something more elegant is needed but fine for now */ |
| 468 | /* FIXME: change all types to either uint8_t, uint32_t, uint64_t |
| 469 | * for 32-bit vs 64-bit compat processes. */ |
| 470 | /* replicate in ust with version number */ |
| 471 | struct { |
| 472 | int reg; /* 1:register, 0:unregister */ |
| 473 | pid_t pid; |
| 474 | uid_t uid; |
| 475 | } reg_msg; |
| 476 | |
| 477 | DBG("[thread] Manage apps started"); |
| 478 | |
| 479 | ret = lttcomm_listen_unix_sock(apps_sock); |
| 480 | if (ret < 0) { |
| 481 | goto error; |
| 482 | } |
| 483 | |
| 484 | /* Notify all applications to register */ |
| 485 | notify_apps(default_global_apps_pipe); |
| 486 | |
| 487 | while (1) { |
| 488 | DBG("Accepting application registration"); |
| 489 | /* Blocking call, waiting for transmission */ |
| 490 | sock = lttcomm_accept_unix_sock(apps_sock); |
| 491 | if (sock < 0) { |
| 492 | goto error; |
| 493 | } |
| 494 | |
| 495 | /* Basic recv here to handle the very simple data |
| 496 | * that the libust send to register (reg_msg). |
| 497 | */ |
| 498 | ret = recv(sock, ®_msg, sizeof(reg_msg), 0); |
| 499 | if (ret < 0) { |
| 500 | perror("recv"); |
| 501 | continue; |
| 502 | } |
| 503 | |
| 504 | /* Add application to the global traceable list */ |
| 505 | if (reg_msg.reg == 1) { |
| 506 | /* Registering */ |
| 507 | ret = register_traceable_app(reg_msg.pid, reg_msg.uid); |
| 508 | if (ret < 0) { |
| 509 | /* register_traceable_app only return an error with |
| 510 | * ENOMEM. At this point, we better stop everything. |
| 511 | */ |
| 512 | goto error; |
| 513 | } |
| 514 | } else { |
| 515 | /* Unregistering */ |
| 516 | unregister_traceable_app(reg_msg.pid); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | error: |
| 521 | |
| 522 | return NULL; |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * spawn_kconsumerd_thread |
| 527 | * |
| 528 | * Start the thread_manage_kconsumerd. This must be done after a kconsumerd |
| 529 | * exec or it will fails. |
| 530 | */ |
| 531 | static int spawn_kconsumerd_thread(void) |
| 532 | { |
| 533 | int ret; |
| 534 | |
| 535 | /* Setup semaphore */ |
| 536 | sem_init(&kconsumerd_sem, 0, 0); |
| 537 | |
| 538 | ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL); |
| 539 | if (ret != 0) { |
| 540 | perror("pthread_create kconsumerd"); |
| 541 | goto error; |
| 542 | } |
| 543 | |
| 544 | /* Wait for the kconsumerd thread to be ready */ |
| 545 | sem_wait(&kconsumerd_sem); |
| 546 | |
| 547 | if (kconsumerd_pid == 0) { |
| 548 | ERR("Kconsumerd did not start"); |
| 549 | goto error; |
| 550 | } |
| 551 | |
| 552 | return 0; |
| 553 | |
| 554 | error: |
| 555 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
| 556 | return ret; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * spawn_kconsumerd |
| 561 | * |
| 562 | * Fork and exec a kernel consumer daemon (kconsumerd). |
| 563 | * |
| 564 | * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel |
| 565 | * file descriptor using the libkernelctl or kernel-ctl functions. So, a |
| 566 | * kernel consumer MUST only be spawned before creating a kernel session. |
| 567 | * |
| 568 | * Return pid if successful else -1. |
| 569 | */ |
| 570 | static pid_t spawn_kconsumerd(void) |
| 571 | { |
| 572 | int ret; |
| 573 | pid_t pid; |
| 574 | |
| 575 | DBG("Spawning kconsumerd"); |
| 576 | |
| 577 | pid = fork(); |
| 578 | if (pid == 0) { |
| 579 | /* |
| 580 | * Exec kconsumerd. |
| 581 | */ |
| 582 | execlp("ltt-kconsumerd", "ltt-kconsumerd", "--verbose", NULL); |
| 583 | if (errno != 0) { |
| 584 | perror("kernel start consumer exec"); |
| 585 | } |
| 586 | exit(EXIT_FAILURE); |
| 587 | } else if (pid > 0) { |
| 588 | ret = pid; |
| 589 | goto error; |
| 590 | } else { |
| 591 | perror("kernel start consumer fork"); |
| 592 | ret = -errno; |
| 593 | goto error; |
| 594 | } |
| 595 | |
| 596 | error: |
| 597 | return ret; |
| 598 | } |
| 599 | |
| 600 | /* |
| 601 | * start_kconsumerd |
| 602 | * |
| 603 | * Spawn the kconsumerd daemon and session daemon thread. |
| 604 | */ |
| 605 | static int start_kconsumerd(void) |
| 606 | { |
| 607 | int ret; |
| 608 | |
| 609 | pthread_mutex_lock(&kconsumerd_pid_mutex); |
| 610 | if (kconsumerd_pid != 0) { |
| 611 | goto end; |
| 612 | } |
| 613 | |
| 614 | ret = spawn_kconsumerd(); |
| 615 | if (ret < 0) { |
| 616 | ERR("Spawning kconsumerd failed"); |
| 617 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
| 618 | pthread_mutex_unlock(&kconsumerd_pid_mutex); |
| 619 | goto error; |
| 620 | } |
| 621 | |
| 622 | /* Setting up the global kconsumerd_pid */ |
| 623 | kconsumerd_pid = ret; |
| 624 | pthread_mutex_unlock(&kconsumerd_pid_mutex); |
| 625 | |
| 626 | DBG("Kconsumerd pid %d", ret); |
| 627 | |
| 628 | DBG("Spawning kconsumerd thread"); |
| 629 | ret = spawn_kconsumerd_thread(); |
| 630 | if (ret < 0) { |
| 631 | ERR("Fatal error spawning kconsumerd thread"); |
| 632 | goto error; |
| 633 | } |
| 634 | |
| 635 | end: |
| 636 | pthread_mutex_unlock(&kconsumerd_pid_mutex); |
| 637 | return 0; |
| 638 | |
| 639 | error: |
| 640 | return ret; |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * modprobe_kernel_modules |
| 645 | */ |
| 646 | static int modprobe_kernel_modules(void) |
| 647 | { |
| 648 | int ret = 0, i = 0; |
| 649 | char modprobe[256]; |
| 650 | |
| 651 | while (kernel_modules_list[i] != NULL) { |
| 652 | ret = snprintf(modprobe, sizeof(modprobe), "/sbin/modprobe %s", |
| 653 | kernel_modules_list[i]); |
| 654 | if (ret < 0) { |
| 655 | perror("snprintf modprobe"); |
| 656 | goto error; |
| 657 | } |
| 658 | ret = system(modprobe); |
| 659 | if (ret < 0) { |
| 660 | ERR("Unable to load module %s", kernel_modules_list[i]); |
| 661 | } |
| 662 | DBG("Modprobe successfully %s", kernel_modules_list[i]); |
| 663 | i++; |
| 664 | } |
| 665 | |
| 666 | error: |
| 667 | return ret; |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | * mount_debugfs |
| 672 | */ |
| 673 | static int mount_debugfs(char *path) |
| 674 | { |
| 675 | int ret; |
| 676 | char *type = "debugfs"; |
| 677 | |
| 678 | ret = mkdir_recursive(path, S_IRWXU | S_IRWXG); |
| 679 | if (ret < 0) { |
| 680 | goto error; |
| 681 | } |
| 682 | |
| 683 | ret = mount(type, path, type, 0, NULL); |
| 684 | if (ret < 0) { |
| 685 | perror("mount debugfs"); |
| 686 | goto error; |
| 687 | } |
| 688 | |
| 689 | DBG("Mounted debugfs successfully at %s", path); |
| 690 | |
| 691 | error: |
| 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * init_kernel_tracer |
| 697 | * |
| 698 | * Setup necessary data for kernel tracer action. |
| 699 | */ |
| 700 | static void init_kernel_tracer(void) |
| 701 | { |
| 702 | int ret; |
| 703 | char *proc_mounts = "/proc/mounts"; |
| 704 | char line[256]; |
| 705 | char *debugfs_path = NULL, *lttng_path; |
| 706 | FILE *fp; |
| 707 | |
| 708 | /* Detect debugfs */ |
| 709 | fp = fopen(proc_mounts, "r"); |
| 710 | if (fp == NULL) { |
| 711 | ERR("Unable to probe %s", proc_mounts); |
| 712 | goto error; |
| 713 | } |
| 714 | |
| 715 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 716 | if (strstr(line, "debugfs") != NULL) { |
| 717 | /* Remove first string */ |
| 718 | strtok(line, " "); |
| 719 | /* Dup string here so we can reuse line later on */ |
| 720 | debugfs_path = strdup(strtok(NULL, " ")); |
| 721 | DBG("Got debugfs path : %s", debugfs_path); |
| 722 | break; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | fclose(fp); |
| 727 | |
| 728 | /* Mount debugfs if needded */ |
| 729 | if (debugfs_path == NULL) { |
| 730 | ret = asprintf(&debugfs_path, "/mnt/debugfs"); |
| 731 | if (ret < 0) { |
| 732 | perror("asprintf debugfs path"); |
| 733 | goto error; |
| 734 | } |
| 735 | ret = mount_debugfs(debugfs_path); |
| 736 | if (ret < 0) { |
| 737 | goto error; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | /* Modprobe lttng kernel modules */ |
| 742 | ret = modprobe_kernel_modules(); |
| 743 | if (ret < 0) { |
| 744 | goto error; |
| 745 | } |
| 746 | |
| 747 | /* Setup lttng kernel path */ |
| 748 | ret = asprintf(<tng_path, "%s/lttng", debugfs_path); |
| 749 | if (ret < 0) { |
| 750 | perror("asprintf lttng path"); |
| 751 | goto error; |
| 752 | } |
| 753 | |
| 754 | /* Open debugfs lttng */ |
| 755 | kernel_tracer_fd = open(lttng_path, O_RDWR); |
| 756 | if (kernel_tracer_fd < 0) { |
| 757 | DBG("Failed to open %s", lttng_path); |
| 758 | goto error; |
| 759 | } |
| 760 | |
| 761 | free(lttng_path); |
| 762 | free(debugfs_path); |
| 763 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
| 764 | return; |
| 765 | |
| 766 | error: |
| 767 | if (lttng_path) { |
| 768 | free(lttng_path); |
| 769 | } |
| 770 | if (debugfs_path) { |
| 771 | free(debugfs_path); |
| 772 | } |
| 773 | WARN("No kernel tracer available"); |
| 774 | kernel_tracer_fd = 0; |
| 775 | return; |
| 776 | } |
| 777 | |
| 778 | /* |
| 779 | * start_kernel_trace |
| 780 | * |
| 781 | * Start tracing by creating trace directory and sending FDs to the kernel |
| 782 | * consumer. |
| 783 | */ |
| 784 | static int start_kernel_trace(struct ltt_kernel_session *session) |
| 785 | { |
| 786 | int ret; |
| 787 | |
| 788 | /* Create trace directory */ |
| 789 | ret = create_trace_dir(session); |
| 790 | if (ret < 0) { |
| 791 | if (ret == -EEXIST) { |
| 792 | ret = LTTCOMM_KERN_DIR_EXIST; |
| 793 | } else { |
| 794 | ret = LTTCOMM_KERN_DIR_FAIL; |
| 795 | goto error; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | if (session->kconsumer_fds_sent == 0) { |
| 800 | ret = send_kconsumerd_fds(kconsumerd_cmd_sock, session); |
| 801 | if (ret < 0) { |
| 802 | ERR("Send kconsumerd fds failed"); |
| 803 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
| 804 | goto error; |
| 805 | } |
| 806 | |
| 807 | session->kconsumer_fds_sent = 1; |
| 808 | } |
| 809 | |
| 810 | error: |
| 811 | return ret; |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | * init_default_channel |
| 816 | * |
| 817 | * Allocate a channel structure and fill it. |
| 818 | */ |
| 819 | static struct lttng_channel *init_default_channel(void) |
| 820 | { |
| 821 | struct lttng_channel *chan; |
| 822 | |
| 823 | chan = malloc(sizeof(struct lttng_channel)); |
| 824 | if (chan == NULL) { |
| 825 | perror("init channel malloc"); |
| 826 | goto error; |
| 827 | } |
| 828 | |
| 829 | if (snprintf(chan->name, NAME_MAX, DEFAULT_CHANNEL_NAME) < 0) { |
| 830 | perror("snprintf defautl channel name"); |
| 831 | return NULL; |
| 832 | } |
| 833 | |
| 834 | chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; |
| 835 | chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE; |
| 836 | chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM; |
| 837 | chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; |
| 838 | chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; |
| 839 | chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
| 840 | |
| 841 | error: |
| 842 | return chan; |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * create_kernel_session |
| 847 | * |
| 848 | * Create a kernel tracer session then create the default channel. |
| 849 | */ |
| 850 | static int create_kernel_session(struct ltt_session *session) |
| 851 | { |
| 852 | int ret; |
| 853 | struct lttng_channel *chan; |
| 854 | |
| 855 | DBG("Creating kernel session"); |
| 856 | |
| 857 | ret = kernel_create_session(session, kernel_tracer_fd); |
| 858 | if (ret < 0) { |
| 859 | ret = LTTCOMM_KERN_SESS_FAIL; |
| 860 | goto error; |
| 861 | } |
| 862 | |
| 863 | chan = init_default_channel(); |
| 864 | if (chan == NULL) { |
| 865 | ret = LTTCOMM_FATAL; |
| 866 | goto error; |
| 867 | } |
| 868 | |
| 869 | DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME); |
| 870 | |
| 871 | ret = kernel_create_channel(session->kernel_session, chan, session->path); |
| 872 | if (ret < 0) { |
| 873 | ret = LTTCOMM_KERN_CHAN_FAIL; |
| 874 | goto error; |
| 875 | } |
| 876 | |
| 877 | error: |
| 878 | return ret; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * process_client_msg |
| 883 | * |
| 884 | * Process the command requested by the lttng client within the command |
| 885 | * context structure. This function make sure that the return structure (llm) |
| 886 | * is set and ready for transmission before returning. |
| 887 | * |
| 888 | * Return any error encountered or 0 for success. |
| 889 | */ |
| 890 | static int process_client_msg(struct command_ctx *cmd_ctx) |
| 891 | { |
| 892 | int ret; |
| 893 | |
| 894 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); |
| 895 | |
| 896 | /* Listing commands don't need a session */ |
| 897 | switch (cmd_ctx->lsm->cmd_type) { |
| 898 | case LTTNG_CREATE_SESSION: |
| 899 | case LTTNG_LIST_SESSIONS: |
| 900 | case LTTNG_LIST_EVENTS: |
| 901 | case LTTNG_KERNEL_LIST_EVENTS: |
| 902 | case LTTNG_LIST_TRACEABLE_APPS: |
| 903 | break; |
| 904 | default: |
| 905 | DBG("Getting session %s by name", cmd_ctx->lsm->session_name); |
| 906 | cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session_name); |
| 907 | if (cmd_ctx->session == NULL) { |
| 908 | /* If session name not found */ |
| 909 | if (cmd_ctx->lsm->session_name != NULL) { |
| 910 | ret = LTTCOMM_SESS_NOT_FOUND; |
| 911 | } else { /* If no session name specified */ |
| 912 | ret = LTTCOMM_SELECT_SESS; |
| 913 | } |
| 914 | goto error; |
| 915 | } |
| 916 | break; |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * Check kernel command for kernel session. |
| 921 | */ |
| 922 | switch (cmd_ctx->lsm->cmd_type) { |
| 923 | case LTTNG_KERNEL_ADD_CONTEXT: |
| 924 | case LTTNG_KERNEL_CREATE_CHANNEL: |
| 925 | case LTTNG_KERNEL_DISABLE_ALL_EVENT: |
| 926 | case LTTNG_KERNEL_DISABLE_CHANNEL: |
| 927 | case LTTNG_KERNEL_DISABLE_EVENT: |
| 928 | case LTTNG_KERNEL_ENABLE_ALL_EVENT: |
| 929 | case LTTNG_KERNEL_ENABLE_CHANNEL: |
| 930 | case LTTNG_KERNEL_ENABLE_EVENT: |
| 931 | case LTTNG_KERNEL_LIST_EVENTS: |
| 932 | /* Kernel tracer check */ |
| 933 | if (kernel_tracer_fd == 0) { |
| 934 | init_kernel_tracer(); |
| 935 | if (kernel_tracer_fd == 0) { |
| 936 | ret = LTTCOMM_KERN_NA; |
| 937 | goto error; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | /* Need a session for kernel command */ |
| 942 | if (cmd_ctx->lsm->cmd_type != LTTNG_KERNEL_LIST_EVENTS && |
| 943 | cmd_ctx->session->kernel_session == NULL) { |
| 944 | |
| 945 | ret = create_kernel_session(cmd_ctx->session); |
| 946 | if (ret < 0) { |
| 947 | ret = LTTCOMM_KERN_SESS_FAIL; |
| 948 | goto error; |
| 949 | } |
| 950 | |
| 951 | /* Start the kernel consumer daemon */ |
| 952 | if (kconsumerd_pid == 0) { |
| 953 | ret = start_kconsumerd(); |
| 954 | if (ret < 0) { |
| 955 | goto error; |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | /* Connect to ust apps if available pid */ |
| 962 | if (cmd_ctx->lsm->pid > 0) { |
| 963 | /* Connect to app using ustctl API */ |
| 964 | cmd_ctx->ust_sock = ust_connect_app(cmd_ctx->lsm->pid); |
| 965 | if (cmd_ctx->ust_sock < 0) { |
| 966 | ret = LTTCOMM_NO_TRACEABLE; |
| 967 | goto error; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | /* Process by command type */ |
| 972 | switch (cmd_ctx->lsm->cmd_type) { |
| 973 | case LTTNG_KERNEL_ADD_CONTEXT: |
| 974 | { |
| 975 | int found = 0, no_event = 0; |
| 976 | struct ltt_kernel_channel *chan; |
| 977 | struct ltt_kernel_event *event; |
| 978 | |
| 979 | /* Setup lttng message with no payload */ |
| 980 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 981 | if (ret < 0) { |
| 982 | goto setup_error; |
| 983 | } |
| 984 | |
| 985 | /* Check if event name is given */ |
| 986 | if (strlen(cmd_ctx->lsm->u.context.event_name) == 0) { |
| 987 | no_event = 1; |
| 988 | } |
| 989 | |
| 990 | if (strlen(cmd_ctx->lsm->u.context.channel_name) == 0) { |
| 991 | /* Go over all channels */ |
| 992 | DBG("Adding context to all channels"); |
| 993 | cds_list_for_each_entry(chan, |
| 994 | &cmd_ctx->session->kernel_session->channel_list.head, list) { |
| 995 | if (no_event) { |
| 996 | ret = kernel_add_channel_context(chan, |
| 997 | &cmd_ctx->lsm->u.context.ctx); |
| 998 | if (ret < 0) { |
| 999 | continue; |
| 1000 | } |
| 1001 | } else { |
| 1002 | event = get_kernel_event_by_name(cmd_ctx->lsm->u.context.event_name, chan); |
| 1003 | if (event != NULL) { |
| 1004 | ret = kernel_add_event_context(event, |
| 1005 | &cmd_ctx->lsm->u.context.ctx); |
| 1006 | if (ret < 0) { |
| 1007 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 1008 | goto error; |
| 1009 | } |
| 1010 | found = 1; |
| 1011 | break; |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | } else { |
| 1016 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.context.channel_name, |
| 1017 | cmd_ctx->session->kernel_session); |
| 1018 | if (chan == NULL) { |
| 1019 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 1020 | goto error; |
| 1021 | } |
| 1022 | |
| 1023 | if (no_event) { |
| 1024 | ret = kernel_add_channel_context(chan, |
| 1025 | &cmd_ctx->lsm->u.context.ctx); |
| 1026 | if (ret < 0) { |
| 1027 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 1028 | goto error; |
| 1029 | } |
| 1030 | } else { |
| 1031 | event = get_kernel_event_by_name(cmd_ctx->lsm->u.context.event_name, chan); |
| 1032 | if (event != NULL) { |
| 1033 | ret = kernel_add_event_context(event, |
| 1034 | &cmd_ctx->lsm->u.context.ctx); |
| 1035 | if (ret < 0) { |
| 1036 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 1037 | goto error; |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | if (!found && !no_event) { |
| 1044 | ret = LTTCOMM_NO_EVENT; |
| 1045 | goto error; |
| 1046 | } |
| 1047 | |
| 1048 | ret = LTTCOMM_OK; |
| 1049 | break; |
| 1050 | } |
| 1051 | case LTTNG_KERNEL_CREATE_CHANNEL: |
| 1052 | { |
| 1053 | /* Setup lttng message with no payload */ |
| 1054 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1055 | if (ret < 0) { |
| 1056 | goto setup_error; |
| 1057 | } |
| 1058 | |
| 1059 | /* Kernel tracer */ |
| 1060 | DBG("Creating kernel channel"); |
| 1061 | |
| 1062 | ret = kernel_create_channel(cmd_ctx->session->kernel_session, |
| 1063 | &cmd_ctx->lsm->u.channel.chan, cmd_ctx->session->path); |
| 1064 | if (ret < 0) { |
| 1065 | ret = LTTCOMM_KERN_CHAN_FAIL; |
| 1066 | goto error; |
| 1067 | } |
| 1068 | |
| 1069 | ret = LTTCOMM_OK; |
| 1070 | break; |
| 1071 | } |
| 1072 | case LTTNG_KERNEL_DISABLE_CHANNEL: |
| 1073 | { |
| 1074 | struct ltt_kernel_channel *chan; |
| 1075 | |
| 1076 | /* Setup lttng message with no payload */ |
| 1077 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1078 | if (ret < 0) { |
| 1079 | goto setup_error; |
| 1080 | } |
| 1081 | |
| 1082 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name, |
| 1083 | cmd_ctx->session->kernel_session); |
| 1084 | if (chan == NULL) { |
| 1085 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 1086 | goto error; |
| 1087 | } else if (chan->enabled == 1) { |
| 1088 | ret = kernel_disable_channel(chan); |
| 1089 | if (ret < 0) { |
| 1090 | if (ret != EEXIST) { |
| 1091 | ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL; |
| 1092 | } |
| 1093 | goto error; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | kernel_wait_quiescent(kernel_tracer_fd); |
| 1098 | ret = LTTCOMM_OK; |
| 1099 | break; |
| 1100 | } |
| 1101 | case LTTNG_KERNEL_DISABLE_EVENT: |
| 1102 | { |
| 1103 | struct ltt_kernel_channel *chan; |
| 1104 | struct ltt_kernel_event *ev; |
| 1105 | |
| 1106 | /* Setup lttng message with no payload */ |
| 1107 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1108 | if (ret < 0) { |
| 1109 | goto setup_error; |
| 1110 | } |
| 1111 | |
| 1112 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name, |
| 1113 | cmd_ctx->session->kernel_session); |
| 1114 | if (chan == NULL) { |
| 1115 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 1116 | goto error; |
| 1117 | } |
| 1118 | |
| 1119 | ev = get_kernel_event_by_name(cmd_ctx->lsm->u.disable.name, chan); |
| 1120 | if (ev != NULL) { |
| 1121 | DBG("Disabling kernel event %s for channel %s.", |
| 1122 | cmd_ctx->lsm->u.disable.name, cmd_ctx->lsm->u.disable.channel_name); |
| 1123 | ret = kernel_disable_event(ev); |
| 1124 | if (ret < 0) { |
| 1125 | ret = LTTCOMM_KERN_ENABLE_FAIL; |
| 1126 | goto error; |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | kernel_wait_quiescent(kernel_tracer_fd); |
| 1131 | ret = LTTCOMM_OK; |
| 1132 | break; |
| 1133 | } |
| 1134 | case LTTNG_KERNEL_DISABLE_ALL_EVENT: |
| 1135 | { |
| 1136 | struct ltt_kernel_channel *chan; |
| 1137 | struct ltt_kernel_event *ev; |
| 1138 | |
| 1139 | /* Setup lttng message with no payload */ |
| 1140 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1141 | if (ret < 0) { |
| 1142 | goto setup_error; |
| 1143 | } |
| 1144 | |
| 1145 | DBG("Disabling all enabled kernel events"); |
| 1146 | |
| 1147 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name, |
| 1148 | cmd_ctx->session->kernel_session); |
| 1149 | if (chan == NULL) { |
| 1150 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 1151 | goto error; |
| 1152 | } |
| 1153 | |
| 1154 | /* For each event in the kernel session */ |
| 1155 | cds_list_for_each_entry(ev, &chan->events_list.head, list) { |
| 1156 | DBG("Disabling kernel event %s for channel %s.", |
| 1157 | ev->event->name, cmd_ctx->lsm->u.disable.channel_name); |
| 1158 | ret = kernel_disable_event(ev); |
| 1159 | if (ret < 0) { |
| 1160 | continue; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | /* Quiescent wait after event disable */ |
| 1165 | kernel_wait_quiescent(kernel_tracer_fd); |
| 1166 | ret = LTTCOMM_OK; |
| 1167 | break; |
| 1168 | } |
| 1169 | case LTTNG_KERNEL_ENABLE_CHANNEL: |
| 1170 | { |
| 1171 | struct ltt_kernel_channel *chan; |
| 1172 | |
| 1173 | /* Setup lttng message with no payload */ |
| 1174 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1175 | if (ret < 0) { |
| 1176 | goto setup_error; |
| 1177 | } |
| 1178 | |
| 1179 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name, |
| 1180 | cmd_ctx->session->kernel_session); |
| 1181 | if (chan == NULL) { |
| 1182 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 1183 | goto error; |
| 1184 | } else if (chan->enabled == 0) { |
| 1185 | ret = kernel_enable_channel(chan); |
| 1186 | if (ret < 0) { |
| 1187 | if (ret != EEXIST) { |
| 1188 | ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL; |
| 1189 | } |
| 1190 | goto error; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | kernel_wait_quiescent(kernel_tracer_fd); |
| 1195 | ret = LTTCOMM_OK; |
| 1196 | break; |
| 1197 | } |
| 1198 | case LTTNG_KERNEL_ENABLE_EVENT: |
| 1199 | { |
| 1200 | struct ltt_kernel_channel *chan; |
| 1201 | struct ltt_kernel_event *ev; |
| 1202 | |
| 1203 | /* Setup lttng message with no payload */ |
| 1204 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1205 | if (ret < 0) { |
| 1206 | goto setup_error; |
| 1207 | } |
| 1208 | |
| 1209 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name, |
| 1210 | cmd_ctx->session->kernel_session); |
| 1211 | if (chan == NULL) { |
| 1212 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 1213 | goto error; |
| 1214 | } |
| 1215 | |
| 1216 | ev = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, chan); |
| 1217 | if (ev == NULL) { |
| 1218 | DBG("Creating kernel event %s for channel %s.", |
| 1219 | cmd_ctx->lsm->u.enable.event.name, chan->channel->name); |
| 1220 | ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, chan); |
| 1221 | } else { |
| 1222 | DBG("Enabling kernel event %s for channel %s.", |
| 1223 | cmd_ctx->lsm->u.enable.event.name, chan->channel->name); |
| 1224 | ret = kernel_enable_event(ev); |
| 1225 | } |
| 1226 | |
| 1227 | if (ret < 0) { |
| 1228 | ret = LTTCOMM_KERN_ENABLE_FAIL; |
| 1229 | goto error; |
| 1230 | } |
| 1231 | |
| 1232 | kernel_wait_quiescent(kernel_tracer_fd); |
| 1233 | ret = LTTCOMM_OK; |
| 1234 | break; |
| 1235 | } |
| 1236 | case LTTNG_KERNEL_ENABLE_ALL_EVENT: |
| 1237 | { |
| 1238 | int pos, size; |
| 1239 | char *event_list, *event, *ptr; |
| 1240 | struct ltt_kernel_channel *chan; |
| 1241 | struct ltt_kernel_event *ev; |
| 1242 | struct lttng_event ev_attr; |
| 1243 | |
| 1244 | /* Setup lttng message with no payload */ |
| 1245 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1246 | if (ret < 0) { |
| 1247 | goto setup_error; |
| 1248 | } |
| 1249 | |
| 1250 | DBG("Enabling all kernel event"); |
| 1251 | |
| 1252 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name, |
| 1253 | cmd_ctx->session->kernel_session); |
| 1254 | if (chan == NULL) { |
| 1255 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 1256 | goto error; |
| 1257 | } |
| 1258 | |
| 1259 | /* For each event in the kernel session */ |
| 1260 | cds_list_for_each_entry(ev, &chan->events_list.head, list) { |
| 1261 | DBG("Enabling kernel event %s for channel %s.", |
| 1262 | ev->event->name, chan->channel->name); |
| 1263 | ret = kernel_enable_event(ev); |
| 1264 | if (ret < 0) { |
| 1265 | continue; |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | size = kernel_list_events(kernel_tracer_fd, &event_list); |
| 1270 | if (size < 0) { |
| 1271 | ret = LTTCOMM_KERN_LIST_FAIL; |
| 1272 | goto error; |
| 1273 | } |
| 1274 | |
| 1275 | ptr = event_list; |
| 1276 | while ((size = sscanf(ptr, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) { |
| 1277 | ev = get_kernel_event_by_name(event, chan); |
| 1278 | if (ev == NULL) { |
| 1279 | strncpy(ev_attr.name, event, LTTNG_SYM_NAME_LEN); |
| 1280 | /* Default event type for enable all */ |
| 1281 | ev_attr.type = LTTNG_EVENT_TRACEPOINTS; |
| 1282 | /* Enable each single tracepoint event */ |
| 1283 | ret = kernel_create_event(&ev_attr, chan); |
| 1284 | if (ret < 0) { |
| 1285 | /* Ignore error here and continue */ |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | /* Move pointer to the next line */ |
| 1290 | ptr += pos + 1; |
| 1291 | free(event); |
| 1292 | } |
| 1293 | |
| 1294 | free(event_list); |
| 1295 | |
| 1296 | /* Quiescent wait after event enable */ |
| 1297 | kernel_wait_quiescent(kernel_tracer_fd); |
| 1298 | ret = LTTCOMM_OK; |
| 1299 | break; |
| 1300 | } |
| 1301 | case LTTNG_KERNEL_LIST_EVENTS: |
| 1302 | { |
| 1303 | char *event_list; |
| 1304 | ssize_t size = 0; |
| 1305 | |
| 1306 | DBG("Listing kernel events"); |
| 1307 | |
| 1308 | size = kernel_list_events(kernel_tracer_fd, &event_list); |
| 1309 | if (size < 0) { |
| 1310 | ret = LTTCOMM_KERN_LIST_FAIL; |
| 1311 | goto error; |
| 1312 | } |
| 1313 | |
| 1314 | /* |
| 1315 | * Setup lttng message with payload size set to the event list size in |
| 1316 | * bytes and then copy list into the llm payload. |
| 1317 | */ |
| 1318 | ret = setup_lttng_msg(cmd_ctx, size); |
| 1319 | if (ret < 0) { |
| 1320 | goto setup_error; |
| 1321 | } |
| 1322 | |
| 1323 | /* Copy event list into message payload */ |
| 1324 | memcpy(cmd_ctx->llm->payload, event_list, size); |
| 1325 | |
| 1326 | free(event_list); |
| 1327 | |
| 1328 | ret = LTTCOMM_OK; |
| 1329 | break; |
| 1330 | } |
| 1331 | case LTTNG_START_TRACE: |
| 1332 | { |
| 1333 | struct ltt_kernel_channel *chan; |
| 1334 | |
| 1335 | /* Setup lttng message with no payload */ |
| 1336 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1337 | if (ret < 0) { |
| 1338 | goto setup_error; |
| 1339 | } |
| 1340 | |
| 1341 | /* Kernel tracing */ |
| 1342 | if (cmd_ctx->session->kernel_session != NULL) { |
| 1343 | if (cmd_ctx->session->kernel_session->metadata == NULL) { |
| 1344 | DBG("Open kernel metadata"); |
| 1345 | ret = kernel_open_metadata(cmd_ctx->session->kernel_session, |
| 1346 | cmd_ctx->session->path); |
| 1347 | if (ret < 0) { |
| 1348 | ret = LTTCOMM_KERN_META_FAIL; |
| 1349 | goto error; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) { |
| 1354 | DBG("Opening kernel metadata stream"); |
| 1355 | if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) { |
| 1356 | ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session); |
| 1357 | if (ret < 0) { |
| 1358 | ERR("Kernel create metadata stream failed"); |
| 1359 | ret = LTTCOMM_KERN_STREAM_FAIL; |
| 1360 | goto error; |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | /* For each channel */ |
| 1366 | cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) { |
| 1367 | if (chan->stream_count == 0) { |
| 1368 | ret = kernel_open_channel_stream(chan); |
| 1369 | if (ret < 0) { |
| 1370 | ERR("Kernel create channel stream failed"); |
| 1371 | ret = LTTCOMM_KERN_STREAM_FAIL; |
| 1372 | goto error; |
| 1373 | } |
| 1374 | /* Update the stream global counter */ |
| 1375 | cmd_ctx->session->kernel_session->stream_count_global += ret; |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | DBG("Start kernel tracing"); |
| 1380 | ret = kernel_start_session(cmd_ctx->session->kernel_session); |
| 1381 | if (ret < 0) { |
| 1382 | ERR("Kernel start session failed"); |
| 1383 | ret = LTTCOMM_KERN_START_FAIL; |
| 1384 | goto error; |
| 1385 | } |
| 1386 | |
| 1387 | ret = start_kernel_trace(cmd_ctx->session->kernel_session); |
| 1388 | if (ret < 0) { |
| 1389 | ret = LTTCOMM_KERN_START_FAIL; |
| 1390 | goto error; |
| 1391 | } |
| 1392 | |
| 1393 | /* Quiescent wait after starting trace */ |
| 1394 | kernel_wait_quiescent(kernel_tracer_fd); |
| 1395 | } |
| 1396 | |
| 1397 | /* TODO: Start all UST traces */ |
| 1398 | |
| 1399 | ret = LTTCOMM_OK; |
| 1400 | break; |
| 1401 | } |
| 1402 | case LTTNG_STOP_TRACE: |
| 1403 | { |
| 1404 | struct ltt_kernel_channel *chan; |
| 1405 | /* Setup lttng message with no payload */ |
| 1406 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1407 | if (ret < 0) { |
| 1408 | goto setup_error; |
| 1409 | } |
| 1410 | |
| 1411 | /* Kernel tracer */ |
| 1412 | if (cmd_ctx->session->kernel_session != NULL) { |
| 1413 | DBG("Stop kernel tracing"); |
| 1414 | |
| 1415 | ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd); |
| 1416 | if (ret < 0) { |
| 1417 | ERR("Kernel metadata flush failed"); |
| 1418 | } |
| 1419 | |
| 1420 | cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) { |
| 1421 | ret = kernel_flush_buffer(chan); |
| 1422 | if (ret < 0) { |
| 1423 | ERR("Kernel flush buffer error"); |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | ret = kernel_stop_session(cmd_ctx->session->kernel_session); |
| 1428 | if (ret < 0) { |
| 1429 | ERR("Kernel stop session failed"); |
| 1430 | ret = LTTCOMM_KERN_STOP_FAIL; |
| 1431 | goto error; |
| 1432 | } |
| 1433 | |
| 1434 | /* Quiescent wait after stopping trace */ |
| 1435 | kernel_wait_quiescent(kernel_tracer_fd); |
| 1436 | } |
| 1437 | |
| 1438 | /* TODO : User-space tracer */ |
| 1439 | |
| 1440 | ret = LTTCOMM_OK; |
| 1441 | break; |
| 1442 | } |
| 1443 | case LTTNG_CREATE_SESSION: |
| 1444 | { |
| 1445 | /* Setup lttng message with no payload */ |
| 1446 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1447 | if (ret < 0) { |
| 1448 | goto setup_error; |
| 1449 | } |
| 1450 | |
| 1451 | ret = create_session(cmd_ctx->lsm->session_name, cmd_ctx->lsm->path); |
| 1452 | if (ret < 0) { |
| 1453 | if (ret == -EEXIST) { |
| 1454 | ret = LTTCOMM_EXIST_SESS; |
| 1455 | } else { |
| 1456 | ret = LTTCOMM_FATAL; |
| 1457 | } |
| 1458 | goto error; |
| 1459 | } |
| 1460 | |
| 1461 | ret = LTTCOMM_OK; |
| 1462 | break; |
| 1463 | } |
| 1464 | case LTTNG_DESTROY_SESSION: |
| 1465 | { |
| 1466 | /* Setup lttng message with no payload */ |
| 1467 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1468 | if (ret < 0) { |
| 1469 | goto setup_error; |
| 1470 | } |
| 1471 | |
| 1472 | /* Clean kernel session teardown */ |
| 1473 | teardown_kernel_session(cmd_ctx->session); |
| 1474 | |
| 1475 | ret = destroy_session(cmd_ctx->lsm->session_name); |
| 1476 | if (ret < 0) { |
| 1477 | ret = LTTCOMM_FATAL; |
| 1478 | goto error; |
| 1479 | } |
| 1480 | |
| 1481 | ret = LTTCOMM_OK; |
| 1482 | break; |
| 1483 | } |
| 1484 | /* |
| 1485 | case LTTNG_LIST_TRACES: |
| 1486 | { |
| 1487 | unsigned int trace_count; |
| 1488 | |
| 1489 | trace_count = get_trace_count_per_session(cmd_ctx->session); |
| 1490 | if (trace_count == 0) { |
| 1491 | ret = LTTCOMM_NO_TRACE; |
| 1492 | goto error; |
| 1493 | } |
| 1494 | |
| 1495 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count); |
| 1496 | if (ret < 0) { |
| 1497 | goto setup_error; |
| 1498 | } |
| 1499 | |
| 1500 | get_traces_per_session(cmd_ctx->session, |
| 1501 | (struct lttng_trace *)(cmd_ctx->llm->payload)); |
| 1502 | |
| 1503 | ret = LTTCOMM_OK; |
| 1504 | break; |
| 1505 | } |
| 1506 | */ |
| 1507 | /* |
| 1508 | case UST_CREATE_TRACE: |
| 1509 | { |
| 1510 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1511 | if (ret < 0) { |
| 1512 | goto setup_error; |
| 1513 | } |
| 1514 | |
| 1515 | ret = ust_create_trace(cmd_ctx); |
| 1516 | if (ret < 0) { |
| 1517 | goto error; |
| 1518 | } |
| 1519 | break; |
| 1520 | } |
| 1521 | */ |
| 1522 | case LTTNG_LIST_TRACEABLE_APPS: |
| 1523 | { |
| 1524 | unsigned int app_count; |
| 1525 | |
| 1526 | app_count = get_app_count(); |
| 1527 | DBG("Traceable application count : %d", app_count); |
| 1528 | if (app_count == 0) { |
| 1529 | ret = LTTCOMM_NO_APPS; |
| 1530 | goto error; |
| 1531 | } |
| 1532 | |
| 1533 | ret = setup_lttng_msg(cmd_ctx, sizeof(pid_t) * app_count); |
| 1534 | if (ret < 0) { |
| 1535 | goto setup_error; |
| 1536 | } |
| 1537 | |
| 1538 | get_app_list_pids((pid_t *)(cmd_ctx->llm->payload)); |
| 1539 | |
| 1540 | ret = LTTCOMM_OK; |
| 1541 | break; |
| 1542 | } |
| 1543 | /* |
| 1544 | case UST_START_TRACE: |
| 1545 | { |
| 1546 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1547 | if (ret < 0) { |
| 1548 | goto setup_error; |
| 1549 | } |
| 1550 | |
| 1551 | ret = ust_start_trace(cmd_ctx); |
| 1552 | if (ret < 0) { |
| 1553 | goto setup_error; |
| 1554 | } |
| 1555 | break; |
| 1556 | } |
| 1557 | case UST_STOP_TRACE: |
| 1558 | { |
| 1559 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1560 | if (ret < 0) { |
| 1561 | goto setup_error; |
| 1562 | } |
| 1563 | |
| 1564 | ret = ust_stop_trace(cmd_ctx); |
| 1565 | if (ret < 0) { |
| 1566 | goto setup_error; |
| 1567 | } |
| 1568 | break; |
| 1569 | } |
| 1570 | */ |
| 1571 | case LTTNG_LIST_SESSIONS: |
| 1572 | { |
| 1573 | unsigned int session_count; |
| 1574 | |
| 1575 | session_count = get_session_count(); |
| 1576 | if (session_count == 0) { |
| 1577 | ret = LTTCOMM_NO_SESSION; |
| 1578 | goto error; |
| 1579 | } |
| 1580 | |
| 1581 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * session_count); |
| 1582 | if (ret < 0) { |
| 1583 | goto setup_error; |
| 1584 | } |
| 1585 | |
| 1586 | get_lttng_session((struct lttng_session *)(cmd_ctx->llm->payload)); |
| 1587 | |
| 1588 | ret = LTTCOMM_OK; |
| 1589 | break; |
| 1590 | } |
| 1591 | default: |
| 1592 | /* Undefined command */ |
| 1593 | ret = setup_lttng_msg(cmd_ctx, 0); |
| 1594 | if (ret < 0) { |
| 1595 | goto setup_error; |
| 1596 | } |
| 1597 | |
| 1598 | ret = LTTCOMM_UND; |
| 1599 | break; |
| 1600 | } |
| 1601 | |
| 1602 | /* Set return code */ |
| 1603 | cmd_ctx->llm->ret_code = ret; |
| 1604 | |
| 1605 | return ret; |
| 1606 | |
| 1607 | error: |
| 1608 | if (cmd_ctx->llm == NULL) { |
| 1609 | DBG("Missing llm structure. Allocating one."); |
| 1610 | if (setup_lttng_msg(cmd_ctx, 0) < 0) { |
| 1611 | goto setup_error; |
| 1612 | } |
| 1613 | } |
| 1614 | /* Notify client of error */ |
| 1615 | cmd_ctx->llm->ret_code = ret; |
| 1616 | |
| 1617 | setup_error: |
| 1618 | return ret; |
| 1619 | } |
| 1620 | |
| 1621 | /* |
| 1622 | * thread_manage_clients |
| 1623 | * |
| 1624 | * This thread manage all clients request using the unix |
| 1625 | * client socket for communication. |
| 1626 | */ |
| 1627 | static void *thread_manage_clients(void *data) |
| 1628 | { |
| 1629 | int sock, ret; |
| 1630 | struct command_ctx *cmd_ctx; |
| 1631 | |
| 1632 | DBG("[thread] Manage client started"); |
| 1633 | |
| 1634 | ret = lttcomm_listen_unix_sock(client_sock); |
| 1635 | if (ret < 0) { |
| 1636 | goto error; |
| 1637 | } |
| 1638 | |
| 1639 | /* Notify parent pid that we are ready |
| 1640 | * to accept command for client side. |
| 1641 | */ |
| 1642 | if (opt_sig_parent) { |
| 1643 | kill(ppid, SIGCHLD); |
| 1644 | } |
| 1645 | |
| 1646 | while (1) { |
| 1647 | /* Blocking call, waiting for transmission */ |
| 1648 | DBG("Accepting client command ..."); |
| 1649 | sock = lttcomm_accept_unix_sock(client_sock); |
| 1650 | if (sock < 0) { |
| 1651 | goto error; |
| 1652 | } |
| 1653 | |
| 1654 | /* Allocate context command to process the client request */ |
| 1655 | cmd_ctx = malloc(sizeof(struct command_ctx)); |
| 1656 | |
| 1657 | /* Allocate data buffer for reception */ |
| 1658 | cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg)); |
| 1659 | cmd_ctx->llm = NULL; |
| 1660 | cmd_ctx->session = NULL; |
| 1661 | |
| 1662 | /* |
| 1663 | * Data is received from the lttng client. The struct |
| 1664 | * lttcomm_session_msg (lsm) contains the command and data request of |
| 1665 | * the client. |
| 1666 | */ |
| 1667 | DBG("Receiving data from client ..."); |
| 1668 | ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg)); |
| 1669 | if (ret <= 0) { |
| 1670 | continue; |
| 1671 | } |
| 1672 | |
| 1673 | // TODO: Validate cmd_ctx including sanity check for security purpose. |
| 1674 | |
| 1675 | /* |
| 1676 | * This function dispatch the work to the kernel or userspace tracer |
| 1677 | * libs and fill the lttcomm_lttng_msg data structure of all the needed |
| 1678 | * informations for the client. The command context struct contains |
| 1679 | * everything this function may needs. |
| 1680 | */ |
| 1681 | ret = process_client_msg(cmd_ctx); |
| 1682 | if (ret < 0) { |
| 1683 | /* TODO: Inform client somehow of the fatal error. At this point, |
| 1684 | * ret < 0 means that a malloc failed (ENOMEM). */ |
| 1685 | /* Error detected but still accept command */ |
| 1686 | clean_command_ctx(cmd_ctx); |
| 1687 | continue; |
| 1688 | } |
| 1689 | |
| 1690 | DBG("Sending response (size: %d, retcode: %d)", |
| 1691 | cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code); |
| 1692 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); |
| 1693 | if (ret < 0) { |
| 1694 | ERR("Failed to send data back to client"); |
| 1695 | } |
| 1696 | |
| 1697 | clean_command_ctx(cmd_ctx); |
| 1698 | |
| 1699 | /* End of transmission */ |
| 1700 | close(sock); |
| 1701 | } |
| 1702 | |
| 1703 | error: |
| 1704 | return NULL; |
| 1705 | } |
| 1706 | |
| 1707 | |
| 1708 | /* |
| 1709 | * usage function on stderr |
| 1710 | */ |
| 1711 | static void usage(void) |
| 1712 | { |
| 1713 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
| 1714 | fprintf(stderr, " -h, --help Display this usage.\n"); |
| 1715 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); |
| 1716 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n"); |
| 1717 | fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n"); |
| 1718 | fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n"); |
| 1719 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); |
| 1720 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); |
| 1721 | fprintf(stderr, " -V, --version Show version number.\n"); |
| 1722 | fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n"); |
| 1723 | fprintf(stderr, " -q, --quiet No output at all.\n"); |
| 1724 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); |
| 1725 | } |
| 1726 | |
| 1727 | /* |
| 1728 | * daemon argument parsing |
| 1729 | */ |
| 1730 | static int parse_args(int argc, char **argv) |
| 1731 | { |
| 1732 | int c; |
| 1733 | |
| 1734 | static struct option long_options[] = { |
| 1735 | { "client-sock", 1, 0, 'c' }, |
| 1736 | { "apps-sock", 1, 0, 'a' }, |
| 1737 | { "kconsumerd-cmd-sock", 1, 0, 0 }, |
| 1738 | { "kconsumerd-err-sock", 1, 0, 0 }, |
| 1739 | { "daemonize", 0, 0, 'd' }, |
| 1740 | { "sig-parent", 0, 0, 'S' }, |
| 1741 | { "help", 0, 0, 'h' }, |
| 1742 | { "group", 1, 0, 'g' }, |
| 1743 | { "version", 0, 0, 'V' }, |
| 1744 | { "quiet", 0, 0, 'q' }, |
| 1745 | { "verbose", 0, 0, 'v' }, |
| 1746 | { NULL, 0, 0, 0 } |
| 1747 | }; |
| 1748 | |
| 1749 | while (1) { |
| 1750 | int option_index = 0; |
| 1751 | c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:", long_options, &option_index); |
| 1752 | if (c == -1) { |
| 1753 | break; |
| 1754 | } |
| 1755 | |
| 1756 | switch (c) { |
| 1757 | case 0: |
| 1758 | fprintf(stderr, "option %s", long_options[option_index].name); |
| 1759 | if (optarg) { |
| 1760 | fprintf(stderr, " with arg %s\n", optarg); |
| 1761 | } |
| 1762 | break; |
| 1763 | case 'c': |
| 1764 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
| 1765 | break; |
| 1766 | case 'a': |
| 1767 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); |
| 1768 | break; |
| 1769 | case 'd': |
| 1770 | opt_daemon = 1; |
| 1771 | break; |
| 1772 | case 'g': |
| 1773 | opt_tracing_group = strdup(optarg); |
| 1774 | break; |
| 1775 | case 'h': |
| 1776 | usage(); |
| 1777 | exit(EXIT_FAILURE); |
| 1778 | case 'V': |
| 1779 | fprintf(stdout, "%s\n", VERSION); |
| 1780 | exit(EXIT_SUCCESS); |
| 1781 | case 'S': |
| 1782 | opt_sig_parent = 1; |
| 1783 | break; |
| 1784 | case 'E': |
| 1785 | snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg); |
| 1786 | break; |
| 1787 | case 'C': |
| 1788 | snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
| 1789 | break; |
| 1790 | case 'q': |
| 1791 | opt_quiet = 1; |
| 1792 | break; |
| 1793 | case 'v': |
| 1794 | opt_verbose = 1; |
| 1795 | break; |
| 1796 | default: |
| 1797 | /* Unknown option or other error. |
| 1798 | * Error is printed by getopt, just return */ |
| 1799 | return -1; |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | return 0; |
| 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | * init_daemon_socket |
| 1808 | * |
| 1809 | * Creates the two needed socket by the daemon. |
| 1810 | * apps_sock - The communication socket for all UST apps. |
| 1811 | * client_sock - The communication of the cli tool (lttng). |
| 1812 | */ |
| 1813 | static int init_daemon_socket() |
| 1814 | { |
| 1815 | int ret = 0; |
| 1816 | mode_t old_umask; |
| 1817 | |
| 1818 | old_umask = umask(0); |
| 1819 | |
| 1820 | /* Create client tool unix socket */ |
| 1821 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
| 1822 | if (client_sock < 0) { |
| 1823 | ERR("Create unix sock failed: %s", client_unix_sock_path); |
| 1824 | ret = -1; |
| 1825 | goto end; |
| 1826 | } |
| 1827 | |
| 1828 | /* File permission MUST be 660 */ |
| 1829 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 1830 | if (ret < 0) { |
| 1831 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
| 1832 | perror("chmod"); |
| 1833 | goto end; |
| 1834 | } |
| 1835 | |
| 1836 | /* Create the application unix socket */ |
| 1837 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
| 1838 | if (apps_sock < 0) { |
| 1839 | ERR("Create unix sock failed: %s", apps_unix_sock_path); |
| 1840 | ret = -1; |
| 1841 | goto end; |
| 1842 | } |
| 1843 | |
| 1844 | /* File permission MUST be 666 */ |
| 1845 | ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 1846 | if (ret < 0) { |
| 1847 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
| 1848 | perror("chmod"); |
| 1849 | goto end; |
| 1850 | } |
| 1851 | |
| 1852 | end: |
| 1853 | umask(old_umask); |
| 1854 | return ret; |
| 1855 | } |
| 1856 | |
| 1857 | /* |
| 1858 | * check_existing_daemon |
| 1859 | * |
| 1860 | * Check if the global socket is available. |
| 1861 | * If yes, error is returned. |
| 1862 | */ |
| 1863 | static int check_existing_daemon() |
| 1864 | { |
| 1865 | int ret; |
| 1866 | |
| 1867 | ret = access(client_unix_sock_path, F_OK); |
| 1868 | if (ret == 0) { |
| 1869 | ret = access(apps_unix_sock_path, F_OK); |
| 1870 | } |
| 1871 | |
| 1872 | return ret; |
| 1873 | } |
| 1874 | |
| 1875 | /* |
| 1876 | * set_permissions |
| 1877 | * |
| 1878 | * Set the tracing group gid onto the client socket. |
| 1879 | * |
| 1880 | * Race window between mkdir and chown is OK because we are going from |
| 1881 | * more permissive (root.root) to les permissive (root.tracing). |
| 1882 | */ |
| 1883 | static int set_permissions(void) |
| 1884 | { |
| 1885 | int ret; |
| 1886 | struct group *grp; |
| 1887 | |
| 1888 | /* Decide which group name to use */ |
| 1889 | (opt_tracing_group != NULL) ? |
| 1890 | (grp = getgrnam(opt_tracing_group)) : |
| 1891 | (grp = getgrnam(default_tracing_group)); |
| 1892 | |
| 1893 | if (grp == NULL) { |
| 1894 | if (is_root) { |
| 1895 | WARN("No tracing group detected"); |
| 1896 | ret = 0; |
| 1897 | } else { |
| 1898 | ERR("Missing tracing group. Aborting execution."); |
| 1899 | ret = -1; |
| 1900 | } |
| 1901 | goto end; |
| 1902 | } |
| 1903 | |
| 1904 | /* Set lttng run dir */ |
| 1905 | ret = chown(LTTNG_RUNDIR, 0, grp->gr_gid); |
| 1906 | if (ret < 0) { |
| 1907 | ERR("Unable to set group on " LTTNG_RUNDIR); |
| 1908 | perror("chown"); |
| 1909 | } |
| 1910 | |
| 1911 | /* lttng client socket path */ |
| 1912 | ret = chown(client_unix_sock_path, 0, grp->gr_gid); |
| 1913 | if (ret < 0) { |
| 1914 | ERR("Unable to set group on %s", client_unix_sock_path); |
| 1915 | perror("chown"); |
| 1916 | } |
| 1917 | |
| 1918 | /* kconsumerd error socket path */ |
| 1919 | ret = chown(kconsumerd_err_unix_sock_path, 0, grp->gr_gid); |
| 1920 | if (ret < 0) { |
| 1921 | ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path); |
| 1922 | perror("chown"); |
| 1923 | } |
| 1924 | |
| 1925 | DBG("All permissions are set"); |
| 1926 | |
| 1927 | end: |
| 1928 | return ret; |
| 1929 | } |
| 1930 | |
| 1931 | /* |
| 1932 | * create_lttng_rundir |
| 1933 | * |
| 1934 | * Create the lttng run directory needed for all |
| 1935 | * global sockets and pipe. |
| 1936 | */ |
| 1937 | static int create_lttng_rundir(void) |
| 1938 | { |
| 1939 | int ret; |
| 1940 | |
| 1941 | ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG ); |
| 1942 | if (ret < 0) { |
| 1943 | if (errno != EEXIST) { |
| 1944 | ERR("Unable to create " LTTNG_RUNDIR); |
| 1945 | goto error; |
| 1946 | } else { |
| 1947 | ret = 0; |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | error: |
| 1952 | return ret; |
| 1953 | } |
| 1954 | |
| 1955 | /* |
| 1956 | * set_kconsumerd_sockets |
| 1957 | * |
| 1958 | * Setup sockets and directory needed by the kconsumerd |
| 1959 | * communication with the session daemon. |
| 1960 | */ |
| 1961 | static int set_kconsumerd_sockets(void) |
| 1962 | { |
| 1963 | int ret; |
| 1964 | |
| 1965 | if (strlen(kconsumerd_err_unix_sock_path) == 0) { |
| 1966 | snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH); |
| 1967 | } |
| 1968 | |
| 1969 | if (strlen(kconsumerd_cmd_unix_sock_path) == 0) { |
| 1970 | snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH); |
| 1971 | } |
| 1972 | |
| 1973 | ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG); |
| 1974 | if (ret < 0) { |
| 1975 | if (errno != EEXIST) { |
| 1976 | ERR("Failed to create " KCONSUMERD_PATH); |
| 1977 | goto error; |
| 1978 | } |
| 1979 | ret = 0; |
| 1980 | } |
| 1981 | |
| 1982 | /* Create the kconsumerd error unix socket */ |
| 1983 | kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path); |
| 1984 | if (kconsumerd_err_sock < 0) { |
| 1985 | ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path); |
| 1986 | ret = -1; |
| 1987 | goto error; |
| 1988 | } |
| 1989 | |
| 1990 | /* File permission MUST be 660 */ |
| 1991 | ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 1992 | if (ret < 0) { |
| 1993 | ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path); |
| 1994 | perror("chmod"); |
| 1995 | goto error; |
| 1996 | } |
| 1997 | |
| 1998 | error: |
| 1999 | return ret; |
| 2000 | } |
| 2001 | |
| 2002 | /* |
| 2003 | * sighandler |
| 2004 | * |
| 2005 | * Signal handler for the daemon |
| 2006 | */ |
| 2007 | static void sighandler(int sig) |
| 2008 | { |
| 2009 | switch (sig) { |
| 2010 | case SIGPIPE: |
| 2011 | DBG("SIGPIPE catched"); |
| 2012 | return; |
| 2013 | case SIGINT: |
| 2014 | DBG("SIGINT catched"); |
| 2015 | cleanup(); |
| 2016 | break; |
| 2017 | case SIGTERM: |
| 2018 | DBG("SIGTERM catched"); |
| 2019 | cleanup(); |
| 2020 | break; |
| 2021 | default: |
| 2022 | break; |
| 2023 | } |
| 2024 | |
| 2025 | exit(EXIT_SUCCESS); |
| 2026 | } |
| 2027 | |
| 2028 | /* |
| 2029 | * set_signal_handler |
| 2030 | * |
| 2031 | * Setup signal handler for : |
| 2032 | * SIGINT, SIGTERM, SIGPIPE |
| 2033 | */ |
| 2034 | static int set_signal_handler(void) |
| 2035 | { |
| 2036 | int ret = 0; |
| 2037 | struct sigaction sa; |
| 2038 | sigset_t sigset; |
| 2039 | |
| 2040 | if ((ret = sigemptyset(&sigset)) < 0) { |
| 2041 | perror("sigemptyset"); |
| 2042 | return ret; |
| 2043 | } |
| 2044 | |
| 2045 | sa.sa_handler = sighandler; |
| 2046 | sa.sa_mask = sigset; |
| 2047 | sa.sa_flags = 0; |
| 2048 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
| 2049 | perror("sigaction"); |
| 2050 | return ret; |
| 2051 | } |
| 2052 | |
| 2053 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
| 2054 | perror("sigaction"); |
| 2055 | return ret; |
| 2056 | } |
| 2057 | |
| 2058 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
| 2059 | perror("sigaction"); |
| 2060 | return ret; |
| 2061 | } |
| 2062 | |
| 2063 | DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT"); |
| 2064 | |
| 2065 | return ret; |
| 2066 | } |
| 2067 | |
| 2068 | /* |
| 2069 | * set_ulimit |
| 2070 | * |
| 2071 | * Set open files limit to unlimited. This daemon can open a large number of |
| 2072 | * file descriptors in order to consumer multiple kernel traces. |
| 2073 | */ |
| 2074 | static void set_ulimit(void) |
| 2075 | { |
| 2076 | int ret; |
| 2077 | struct rlimit lim; |
| 2078 | |
| 2079 | lim.rlim_cur = 65535; |
| 2080 | lim.rlim_max = 65535; |
| 2081 | |
| 2082 | ret = setrlimit(RLIMIT_NOFILE, &lim); |
| 2083 | if (ret < 0) { |
| 2084 | perror("failed to set open files limit"); |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | /* |
| 2089 | * main |
| 2090 | */ |
| 2091 | int main(int argc, char **argv) |
| 2092 | { |
| 2093 | int ret = 0; |
| 2094 | void *status; |
| 2095 | const char *home_path; |
| 2096 | |
| 2097 | /* Parse arguments */ |
| 2098 | progname = argv[0]; |
| 2099 | if ((ret = parse_args(argc, argv) < 0)) { |
| 2100 | goto error; |
| 2101 | } |
| 2102 | |
| 2103 | /* Daemonize */ |
| 2104 | if (opt_daemon) { |
| 2105 | ret = daemon(0, 0); |
| 2106 | if (ret < 0) { |
| 2107 | perror("daemon"); |
| 2108 | goto error; |
| 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | /* Check if daemon is UID = 0 */ |
| 2113 | is_root = !getuid(); |
| 2114 | |
| 2115 | if (is_root) { |
| 2116 | ret = create_lttng_rundir(); |
| 2117 | if (ret < 0) { |
| 2118 | goto error; |
| 2119 | } |
| 2120 | |
| 2121 | if (strlen(apps_unix_sock_path) == 0) { |
| 2122 | snprintf(apps_unix_sock_path, PATH_MAX, |
| 2123 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); |
| 2124 | } |
| 2125 | |
| 2126 | if (strlen(client_unix_sock_path) == 0) { |
| 2127 | snprintf(client_unix_sock_path, PATH_MAX, |
| 2128 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); |
| 2129 | } |
| 2130 | |
| 2131 | ret = set_kconsumerd_sockets(); |
| 2132 | if (ret < 0) { |
| 2133 | goto error; |
| 2134 | } |
| 2135 | |
| 2136 | /* Setup kernel tracer */ |
| 2137 | init_kernel_tracer(); |
| 2138 | |
| 2139 | /* Set ulimit for open files */ |
| 2140 | set_ulimit(); |
| 2141 | } else { |
| 2142 | home_path = get_home_dir(); |
| 2143 | if (home_path == NULL) { |
| 2144 | ERR("Can't get HOME directory for sockets creation.\n \ |
| 2145 | Please specify --socket PATH."); |
| 2146 | goto error; |
| 2147 | } |
| 2148 | |
| 2149 | if (strlen(apps_unix_sock_path) == 0) { |
| 2150 | snprintf(apps_unix_sock_path, PATH_MAX, |
| 2151 | DEFAULT_HOME_APPS_UNIX_SOCK, home_path); |
| 2152 | } |
| 2153 | |
| 2154 | /* Set the cli tool unix socket path */ |
| 2155 | if (strlen(client_unix_sock_path) == 0) { |
| 2156 | snprintf(client_unix_sock_path, PATH_MAX, |
| 2157 | DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path); |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | DBG("Client socket path %s", client_unix_sock_path); |
| 2162 | DBG("Application socket path %s", apps_unix_sock_path); |
| 2163 | |
| 2164 | /* See if daemon already exist. If any of the two |
| 2165 | * socket needed by the daemon are present, this test fails |
| 2166 | */ |
| 2167 | if ((ret = check_existing_daemon()) == 0) { |
| 2168 | ERR("Already running daemon.\n"); |
| 2169 | /* We do not goto error because we must not |
| 2170 | * cleanup() because a daemon is already running. |
| 2171 | */ |
| 2172 | exit(EXIT_FAILURE); |
| 2173 | } |
| 2174 | |
| 2175 | if (set_signal_handler() < 0) { |
| 2176 | goto error; |
| 2177 | } |
| 2178 | |
| 2179 | /* Setup the needed unix socket */ |
| 2180 | if (init_daemon_socket() < 0) { |
| 2181 | goto error; |
| 2182 | } |
| 2183 | |
| 2184 | /* Set credentials to socket */ |
| 2185 | if (is_root && (set_permissions() < 0)) { |
| 2186 | goto error; |
| 2187 | } |
| 2188 | |
| 2189 | /* Get parent pid if -S, --sig-parent is specified. */ |
| 2190 | if (opt_sig_parent) { |
| 2191 | ppid = getppid(); |
| 2192 | } |
| 2193 | |
| 2194 | while (1) { |
| 2195 | /* Create thread to manage the client socket */ |
| 2196 | ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL); |
| 2197 | if (ret != 0) { |
| 2198 | perror("pthread_create"); |
| 2199 | goto error; |
| 2200 | } |
| 2201 | |
| 2202 | /* Create thread to manage application socket */ |
| 2203 | ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL); |
| 2204 | if (ret != 0) { |
| 2205 | perror("pthread_create"); |
| 2206 | goto error; |
| 2207 | } |
| 2208 | |
| 2209 | ret = pthread_join(client_thread, &status); |
| 2210 | if (ret != 0) { |
| 2211 | perror("pthread_join"); |
| 2212 | goto error; |
| 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | cleanup(); |
| 2217 | exit(EXIT_SUCCESS); |
| 2218 | |
| 2219 | error: |
| 2220 | cleanup(); |
| 2221 | exit(EXIT_FAILURE); |
| 2222 | } |