| 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 <signal.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/ipc.h> |
| 30 | #include <sys/shm.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | #include <urcu/list.h> /* URCU list library (-lurcu) */ |
| 37 | #include <ust/ustctl.h> /* UST control lib (-lust) */ |
| 38 | #include <lttng/liblttngctl.h> |
| 39 | |
| 40 | #include "liblttsessiondcomm.h" |
| 41 | #include "ltt-sessiond.h" |
| 42 | #include "lttngerr.h" |
| 43 | #include "session.h" |
| 44 | #include "traceable-app.h" |
| 45 | |
| 46 | /* Const values */ |
| 47 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
| 48 | const char default_tracing_group[] = DEFAULT_TRACING_GROUP; |
| 49 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; |
| 50 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; |
| 51 | |
| 52 | /* Static functions */ |
| 53 | static int set_signal_handler(void); |
| 54 | static int set_socket_perms(void); |
| 55 | static void sighandler(int sig); |
| 56 | static void cleanup(void); |
| 57 | static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm); |
| 58 | static int check_existing_daemon(void); |
| 59 | static int notify_apps(const char* name); |
| 60 | static int connect_app(pid_t pid); |
| 61 | static int init_daemon_socket(void); |
| 62 | static int process_client_msg(int sock, struct lttcomm_session_msg*); |
| 63 | static int send_unix_sock(int sock, void *buf, size_t len); |
| 64 | static int setup_data_buffer(char **buf, size_t size, struct lttcomm_lttng_msg *llm); |
| 65 | |
| 66 | /* Command function */ |
| 67 | |
| 68 | static void *thread_manage_clients(void *data); |
| 69 | static void *thread_manage_apps(void *data); |
| 70 | |
| 71 | /* Variables */ |
| 72 | const char *progname; |
| 73 | const char *opt_tracing_group; |
| 74 | static int opt_sig_parent; |
| 75 | static int opt_daemon; |
| 76 | int opt_verbose; |
| 77 | int opt_quiet; |
| 78 | static int is_root; /* Set to 1 if the daemon is running as root */ |
| 79 | static pid_t ppid; |
| 80 | |
| 81 | static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */ |
| 82 | static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */ |
| 83 | |
| 84 | static int client_socket; |
| 85 | static int apps_socket; |
| 86 | |
| 87 | static struct ltt_session *current_session; |
| 88 | |
| 89 | /* |
| 90 | * thread_manage_apps |
| 91 | * |
| 92 | * This thread manage the application socket communication |
| 93 | */ |
| 94 | static void *thread_manage_apps(void *data) |
| 95 | { |
| 96 | int sock, ret; |
| 97 | |
| 98 | /* TODO: Something more elegant is needed but fine for now */ |
| 99 | struct { |
| 100 | int reg; /* 1:register, 0:unregister */ |
| 101 | pid_t pid; |
| 102 | uid_t uid; |
| 103 | } reg_msg; |
| 104 | |
| 105 | /* Notify all applications to register */ |
| 106 | notify_apps(default_global_apps_pipe); |
| 107 | |
| 108 | ret = lttcomm_listen_unix_sock(apps_socket); |
| 109 | if (ret < 0) { |
| 110 | goto error; |
| 111 | } |
| 112 | |
| 113 | while (1) { |
| 114 | /* Blocking call, waiting for transmission */ |
| 115 | sock = lttcomm_accept_unix_sock(apps_socket); |
| 116 | if (sock < 0) { |
| 117 | goto error; |
| 118 | } |
| 119 | |
| 120 | /* Basic recv here to handle the very simple data |
| 121 | * that the libust send to register (reg_msg). |
| 122 | */ |
| 123 | ret = recv(sock, ®_msg, sizeof(reg_msg), 0); |
| 124 | if (ret < 0) { |
| 125 | perror("recv"); |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | /* Add application to the global traceable list */ |
| 130 | if (reg_msg.reg == 1) { |
| 131 | /* Registering */ |
| 132 | ret = register_traceable_app(reg_msg.pid, reg_msg.uid); |
| 133 | if (ret < 0) { |
| 134 | /* register_traceable_app only return an error with |
| 135 | * ENOMEM. At this point, we better stop everything. |
| 136 | */ |
| 137 | goto error; |
| 138 | } |
| 139 | } else { |
| 140 | /* Unregistering */ |
| 141 | unregister_traceable_app(reg_msg.pid); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | error: |
| 146 | |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * thread_manage_clients |
| 152 | * |
| 153 | * This thread manage all clients request using the unix |
| 154 | * client socket for communication. |
| 155 | */ |
| 156 | static void *thread_manage_clients(void *data) |
| 157 | { |
| 158 | int sock, ret; |
| 159 | struct lttcomm_session_msg lsm; |
| 160 | |
| 161 | ret = lttcomm_listen_unix_sock(client_socket); |
| 162 | if (ret < 0) { |
| 163 | goto error; |
| 164 | } |
| 165 | |
| 166 | /* Notify parent pid that we are ready |
| 167 | * to accept command for client side. |
| 168 | */ |
| 169 | if (opt_sig_parent) { |
| 170 | kill(ppid, SIGCHLD); |
| 171 | } |
| 172 | |
| 173 | while (1) { |
| 174 | /* Blocking call, waiting for transmission */ |
| 175 | sock = lttcomm_accept_unix_sock(client_socket); |
| 176 | if (sock < 0) { |
| 177 | goto error; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Data is received from the lttng client. The struct |
| 182 | * lttcomm_session_msg (lsm) contains the command and data |
| 183 | * request of the client. |
| 184 | */ |
| 185 | ret = lttcomm_recv_unix_sock(sock, &lsm, sizeof(lsm)); |
| 186 | if (ret <= 0) { |
| 187 | continue; |
| 188 | } |
| 189 | |
| 190 | /* This function dispatch the work to the LTTng or UST libs |
| 191 | * and then sends back the response to the client. This is needed |
| 192 | * because there might be more then one lttcomm_lttng_msg to |
| 193 | * send out so process_client_msg do both jobs. |
| 194 | */ |
| 195 | ret = process_client_msg(sock, &lsm); |
| 196 | if (ret < 0) { |
| 197 | /* Error detected but still accept command */ |
| 198 | continue; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | error: |
| 203 | return NULL; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * send_unix_sock |
| 208 | * |
| 209 | * Send data on a unix socket using the liblttsessiondcomm API. |
| 210 | * |
| 211 | * Return lttcomm error code. |
| 212 | */ |
| 213 | static int send_unix_sock(int sock, void *buf, size_t len) |
| 214 | { |
| 215 | /* Check valid length */ |
| 216 | if (len <= 0) { |
| 217 | return -1; |
| 218 | } |
| 219 | |
| 220 | return lttcomm_send_unix_sock(sock, buf, len); |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * connect_app |
| 225 | * |
| 226 | * Return a socket connected to the libust communication socket |
| 227 | * of the application identified by the pid. |
| 228 | * |
| 229 | * If the pid is not found in the traceable list, |
| 230 | * return -1 to indicate error. |
| 231 | */ |
| 232 | static int connect_app(pid_t pid) |
| 233 | { |
| 234 | int sock; |
| 235 | struct ltt_traceable_app *lta; |
| 236 | |
| 237 | lta = find_app_by_pid(pid); |
| 238 | if (lta == NULL) { |
| 239 | /* App not found */ |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | sock = ustctl_connect_pid(lta->pid); |
| 244 | if (sock < 0) { |
| 245 | ERR("Fail connecting to the PID %d\n", pid); |
| 246 | } |
| 247 | |
| 248 | return sock; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * notify_apps |
| 253 | * |
| 254 | * Notify apps by writing 42 to a named pipe using name. |
| 255 | * Every applications waiting for a ltt-sessiond will be notified |
| 256 | * and re-register automatically to the session daemon. |
| 257 | * |
| 258 | * Return open or write error value. |
| 259 | */ |
| 260 | static int notify_apps(const char *name) |
| 261 | { |
| 262 | int fd; |
| 263 | int ret = -1; |
| 264 | |
| 265 | /* Try opening the global pipe */ |
| 266 | fd = open(name, O_WRONLY); |
| 267 | if (fd < 0) { |
| 268 | goto error; |
| 269 | } |
| 270 | |
| 271 | /* Notify by writing on the pipe */ |
| 272 | ret = write(fd, "42", 2); |
| 273 | if (ret < 0) { |
| 274 | perror("write"); |
| 275 | } |
| 276 | |
| 277 | error: |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * ust_create_trace |
| 283 | * |
| 284 | * Create an userspace trace using pid. |
| 285 | * This trace is then appended to the current session |
| 286 | * ust trace list. |
| 287 | */ |
| 288 | static int ust_create_trace(pid_t pid) |
| 289 | { |
| 290 | int sock, ret; |
| 291 | struct ltt_ust_trace *trace; |
| 292 | |
| 293 | trace = malloc(sizeof(struct ltt_ust_trace)); |
| 294 | if (trace == NULL) { |
| 295 | perror("malloc"); |
| 296 | ret = -1; |
| 297 | goto error; |
| 298 | } |
| 299 | |
| 300 | /* Init */ |
| 301 | trace->pid = pid; |
| 302 | trace->shmid = 0; |
| 303 | |
| 304 | /* Connect to app using ustctl API */ |
| 305 | sock = connect_app(pid); |
| 306 | if (sock < 0) { |
| 307 | ret = LTTCOMM_NO_TRACEABLE; |
| 308 | goto error; |
| 309 | } |
| 310 | |
| 311 | ret = ustctl_create_trace(sock, "auto"); |
| 312 | if (ret < 0) { |
| 313 | ret = LTTCOMM_CREATE_FAIL; |
| 314 | goto error; |
| 315 | } |
| 316 | |
| 317 | /* Check if current session is valid */ |
| 318 | if (current_session) { |
| 319 | cds_list_add(&trace->list, ¤t_session->ust_traces); |
| 320 | } |
| 321 | |
| 322 | error: |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * copy_common_data |
| 328 | * |
| 329 | * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg |
| 330 | */ |
| 331 | static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm) |
| 332 | { |
| 333 | llm->cmd_type = lsm->cmd_type; |
| 334 | llm->pid = lsm->pid; |
| 335 | |
| 336 | /* Manage uuid */ |
| 337 | if (!uuid_is_null(lsm->session_id)) { |
| 338 | uuid_copy(llm->session_id, lsm->session_id); |
| 339 | } |
| 340 | |
| 341 | strncpy(llm->trace_name, lsm->trace_name, strlen(llm->trace_name)); |
| 342 | llm->trace_name[strlen(llm->trace_name) - 1] = '\0'; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * setup_data_buffer |
| 347 | * |
| 348 | * Setup the outgoing data buffer for the response |
| 349 | * data allocating the right amount of memory. |
| 350 | * |
| 351 | * Return total size of the buffer pointed by buf. |
| 352 | */ |
| 353 | static int setup_data_buffer(char **buf, size_t s_data, struct lttcomm_lttng_msg *llm) |
| 354 | { |
| 355 | int ret = 0; |
| 356 | size_t buf_size; |
| 357 | |
| 358 | buf_size = sizeof(struct lttcomm_lttng_msg) + s_data; |
| 359 | *buf = malloc(buf_size); |
| 360 | if (*buf == NULL) { |
| 361 | perror("malloc"); |
| 362 | ret = -1; |
| 363 | goto error; |
| 364 | } |
| 365 | |
| 366 | /* Setup lttcomm_lttng_msg data and copy |
| 367 | * it to the newly allocated buffer. |
| 368 | */ |
| 369 | llm->size_payload = s_data; |
| 370 | memcpy(*buf, llm, sizeof(struct lttcomm_lttng_msg)); |
| 371 | |
| 372 | return buf_size; |
| 373 | |
| 374 | error: |
| 375 | return ret; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * process_client_msg |
| 380 | * |
| 381 | * This takes the lttcomm_session_msg struct and process the command requested |
| 382 | * by the client. It then creates response(s) and send it back to the |
| 383 | * given socket (sock). |
| 384 | * |
| 385 | * Return any error encountered or 0 for success. |
| 386 | */ |
| 387 | static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) |
| 388 | { |
| 389 | int ret; |
| 390 | int buf_size; |
| 391 | size_t header_size; |
| 392 | char *send_buf = NULL; |
| 393 | struct lttcomm_lttng_msg llm; |
| 394 | |
| 395 | /* Copy common data to identify the response |
| 396 | * on the lttng client side. |
| 397 | */ |
| 398 | copy_common_data(&llm, lsm); |
| 399 | |
| 400 | /* Check command that needs a session */ |
| 401 | if (lsm->cmd_type != LTTNG_CREATE_SESSION && |
| 402 | lsm->cmd_type != LTTNG_LIST_SESSIONS && |
| 403 | lsm->cmd_type != UST_LIST_APPS) |
| 404 | { |
| 405 | current_session = find_session_by_uuid(lsm->session_id); |
| 406 | if (current_session == NULL) { |
| 407 | ret = LTTCOMM_SELECT_SESS; |
| 408 | goto end; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /* Default return code. |
| 413 | * In our world, everything is OK... right? ;) |
| 414 | */ |
| 415 | llm.ret_code = LTTCOMM_OK; |
| 416 | |
| 417 | header_size = sizeof(struct lttcomm_lttng_msg); |
| 418 | |
| 419 | /* Process by command type */ |
| 420 | switch (lsm->cmd_type) { |
| 421 | case LTTNG_CREATE_SESSION: |
| 422 | { |
| 423 | ret = create_session(lsm->session_name, &llm.session_id); |
| 424 | if (ret < 0) { |
| 425 | if (ret == -1) { |
| 426 | ret = LTTCOMM_EXIST_SESS; |
| 427 | } else { |
| 428 | ret = LTTCOMM_FATAL; |
| 429 | } |
| 430 | goto end; |
| 431 | } |
| 432 | |
| 433 | buf_size = setup_data_buffer(&send_buf, 0, &llm); |
| 434 | if (buf_size < 0) { |
| 435 | ret = LTTCOMM_FATAL; |
| 436 | goto end; |
| 437 | } |
| 438 | |
| 439 | break; |
| 440 | } |
| 441 | case LTTNG_DESTROY_SESSION: |
| 442 | { |
| 443 | ret = destroy_session(&lsm->session_id); |
| 444 | if (ret < 0) { |
| 445 | ret = LTTCOMM_NO_SESS; |
| 446 | } else { |
| 447 | ret = LTTCOMM_OK; |
| 448 | } |
| 449 | |
| 450 | /* No auxiliary data so only send the llm struct. */ |
| 451 | goto end; |
| 452 | } |
| 453 | case UST_CREATE_TRACE: |
| 454 | { |
| 455 | ret = ust_create_trace(lsm->pid); |
| 456 | if (ret < 0) { |
| 457 | ret = LTTCOMM_CREATE_FAIL; |
| 458 | goto end; |
| 459 | } |
| 460 | |
| 461 | /* No auxiliary data so only send the llm struct. */ |
| 462 | goto end; |
| 463 | } |
| 464 | case UST_LIST_APPS: |
| 465 | { |
| 466 | unsigned int app_count = get_app_count(); |
| 467 | /* Stop right now if no apps */ |
| 468 | if (app_count == 0) { |
| 469 | ret = LTTCOMM_NO_APPS; |
| 470 | goto end; |
| 471 | } |
| 472 | |
| 473 | /* Setup data buffer and details for transmission */ |
| 474 | buf_size = setup_data_buffer(&send_buf, |
| 475 | sizeof(pid_t) * app_count, &llm); |
| 476 | if (buf_size < 0) { |
| 477 | ret = LTTCOMM_FATAL; |
| 478 | goto end; |
| 479 | } |
| 480 | |
| 481 | get_app_list_pids((pid_t *)(send_buf + header_size)); |
| 482 | |
| 483 | break; |
| 484 | } |
| 485 | case LTTNG_LIST_SESSIONS: |
| 486 | { |
| 487 | unsigned int session_count = get_session_count(); |
| 488 | /* Stop right now if no session */ |
| 489 | if (session_count == 0) { |
| 490 | ret = LTTCOMM_NO_SESS; |
| 491 | goto end; |
| 492 | } |
| 493 | |
| 494 | /* Setup data buffer and details for transmission */ |
| 495 | buf_size = setup_data_buffer(&send_buf, |
| 496 | (sizeof(struct lttng_session) * session_count), &llm); |
| 497 | if (buf_size < 0) { |
| 498 | ret = LTTCOMM_FATAL; |
| 499 | goto end; |
| 500 | } |
| 501 | |
| 502 | get_lttng_session((struct lttng_session *)(send_buf + header_size)); |
| 503 | |
| 504 | break; |
| 505 | } |
| 506 | default: |
| 507 | { |
| 508 | /* Undefined command */ |
| 509 | ret = LTTCOMM_UND; |
| 510 | goto end; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | ret = send_unix_sock(sock, send_buf, buf_size); |
| 515 | |
| 516 | if (send_buf != NULL) { |
| 517 | free(send_buf); |
| 518 | } |
| 519 | |
| 520 | return ret; |
| 521 | |
| 522 | end: |
| 523 | /* Notify client of error */ |
| 524 | llm.ret_code = ret; |
| 525 | llm.size_payload = 0; |
| 526 | send_unix_sock(sock, (void*) &llm, sizeof(llm)); |
| 527 | |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * usage function on stderr |
| 533 | */ |
| 534 | static void usage(void) |
| 535 | { |
| 536 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
| 537 | fprintf(stderr, " -h, --help Display this usage.\n"); |
| 538 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); |
| 539 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket.\n"); |
| 540 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); |
| 541 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); |
| 542 | fprintf(stderr, " -V, --version Show version number.\n"); |
| 543 | fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n"); |
| 544 | fprintf(stderr, " -q, --quiet No output at all.\n"); |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * daemon argument parsing |
| 549 | */ |
| 550 | static int parse_args(int argc, char **argv) |
| 551 | { |
| 552 | int c; |
| 553 | |
| 554 | static struct option long_options[] = { |
| 555 | { "client-sock", 1, 0, 'c' }, |
| 556 | { "apps-sock", 1, 0, 'a' }, |
| 557 | { "daemonize", 0, 0, 'd' }, |
| 558 | { "sig-parent", 0, 0, 'S' }, |
| 559 | { "help", 0, 0, 'h' }, |
| 560 | { "group", 1, 0, 'g' }, |
| 561 | { "version", 0, 0, 'V' }, |
| 562 | { "quiet", 0, 0, 'q' }, |
| 563 | { NULL, 0, 0, 0 } |
| 564 | }; |
| 565 | |
| 566 | while (1) { |
| 567 | int option_index = 0; |
| 568 | c = getopt_long(argc, argv, "dhqVS" "a:c:g:s:", long_options, &option_index); |
| 569 | if (c == -1) { |
| 570 | break; |
| 571 | } |
| 572 | |
| 573 | switch (c) { |
| 574 | case 0: |
| 575 | fprintf(stderr, "option %s", long_options[option_index].name); |
| 576 | if (optarg) { |
| 577 | fprintf(stderr, " with arg %s\n", optarg); |
| 578 | } |
| 579 | break; |
| 580 | case 'c': |
| 581 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
| 582 | break; |
| 583 | case 'a': |
| 584 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); |
| 585 | break; |
| 586 | case 'd': |
| 587 | opt_daemon = 1; |
| 588 | break; |
| 589 | case 'g': |
| 590 | opt_tracing_group = strdup(optarg); |
| 591 | break; |
| 592 | case 'h': |
| 593 | usage(); |
| 594 | exit(EXIT_FAILURE); |
| 595 | case 'V': |
| 596 | fprintf(stdout, "%s\n", VERSION); |
| 597 | exit(EXIT_SUCCESS); |
| 598 | case 'S': |
| 599 | opt_sig_parent = 1; |
| 600 | break; |
| 601 | case 'q': |
| 602 | opt_quiet = 1; |
| 603 | break; |
| 604 | default: |
| 605 | /* Unknown option or other error. |
| 606 | * Error is printed by getopt, just return */ |
| 607 | return -1; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * init_daemon_socket |
| 616 | * |
| 617 | * Creates the two needed socket by the daemon. |
| 618 | * apps_socket - The communication socket for all UST apps. |
| 619 | * client_socket - The communication of the cli tool (lttng). |
| 620 | */ |
| 621 | static int init_daemon_socket() |
| 622 | { |
| 623 | int ret = 0; |
| 624 | mode_t old_umask; |
| 625 | |
| 626 | old_umask = umask(0); |
| 627 | |
| 628 | /* Create client tool unix socket */ |
| 629 | client_socket = lttcomm_create_unix_sock(client_unix_sock_path); |
| 630 | if (client_socket < 0) { |
| 631 | ret = -1; |
| 632 | goto end; |
| 633 | } |
| 634 | |
| 635 | /* File permission MUST be 660 */ |
| 636 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 637 | if (ret < 0) { |
| 638 | perror("chmod"); |
| 639 | goto end; |
| 640 | } |
| 641 | |
| 642 | /* Create the application unix socket */ |
| 643 | apps_socket = lttcomm_create_unix_sock(apps_unix_sock_path); |
| 644 | if (apps_socket < 0) { |
| 645 | ret = -1; |
| 646 | goto end; |
| 647 | } |
| 648 | |
| 649 | /* File permission MUST be 660 */ |
| 650 | ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 651 | if (ret < 0) { |
| 652 | perror("chmod"); |
| 653 | goto end; |
| 654 | } |
| 655 | |
| 656 | end: |
| 657 | umask(old_umask); |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * check_existing_daemon |
| 663 | * |
| 664 | * Check if the global socket is available. |
| 665 | * If yes, error is returned. |
| 666 | */ |
| 667 | static int check_existing_daemon() |
| 668 | { |
| 669 | int ret; |
| 670 | |
| 671 | ret = access(client_unix_sock_path, F_OK); |
| 672 | if (ret == 0) { |
| 673 | ret = access(apps_unix_sock_path, F_OK); |
| 674 | } |
| 675 | |
| 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * get_home_dir |
| 681 | * |
| 682 | * Return pointer to home directory path using |
| 683 | * the env variable HOME. |
| 684 | * |
| 685 | * Default : /tmp |
| 686 | */ |
| 687 | static const char *get_home_dir(void) |
| 688 | { |
| 689 | const char *home_path; |
| 690 | |
| 691 | if ((home_path = (const char *) getenv("HOME")) == NULL) { |
| 692 | home_path = default_home_dir; |
| 693 | } |
| 694 | |
| 695 | return home_path; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * set_socket_perms |
| 700 | * |
| 701 | * Set the tracing group gid onto the client socket. |
| 702 | */ |
| 703 | static int set_socket_perms(void) |
| 704 | { |
| 705 | int ret; |
| 706 | struct group *grp; |
| 707 | |
| 708 | /* Decide which group name to use */ |
| 709 | (opt_tracing_group != NULL) ? |
| 710 | (grp = getgrnam(opt_tracing_group)) : |
| 711 | (grp = getgrnam(default_tracing_group)); |
| 712 | |
| 713 | if (grp == NULL) { |
| 714 | ERR("Missing tracing group. Aborting execution.\n"); |
| 715 | ret = -1; |
| 716 | goto end; |
| 717 | } |
| 718 | |
| 719 | ret = chown(client_unix_sock_path, 0, grp->gr_gid); |
| 720 | if (ret < 0) { |
| 721 | perror("chown"); |
| 722 | } |
| 723 | |
| 724 | end: |
| 725 | return ret; |
| 726 | } |
| 727 | |
| 728 | /* |
| 729 | * set_signal_handler |
| 730 | * |
| 731 | * Setup signal handler for : |
| 732 | * SIGINT, SIGTERM, SIGPIPE |
| 733 | */ |
| 734 | static int set_signal_handler(void) |
| 735 | { |
| 736 | int ret = 0; |
| 737 | struct sigaction sa; |
| 738 | sigset_t sigset; |
| 739 | |
| 740 | if ((ret = sigemptyset(&sigset)) < 0) { |
| 741 | perror("sigemptyset"); |
| 742 | return ret; |
| 743 | } |
| 744 | |
| 745 | sa.sa_handler = sighandler; |
| 746 | sa.sa_mask = sigset; |
| 747 | sa.sa_flags = 0; |
| 748 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
| 749 | perror("sigaction"); |
| 750 | return ret; |
| 751 | } |
| 752 | |
| 753 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
| 754 | perror("sigaction"); |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
| 759 | perror("sigaction"); |
| 760 | return ret; |
| 761 | } |
| 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * sighandler |
| 768 | * |
| 769 | * Signal handler for the daemon |
| 770 | */ |
| 771 | static void sighandler(int sig) |
| 772 | { |
| 773 | switch (sig) { |
| 774 | case SIGPIPE: |
| 775 | return; |
| 776 | case SIGINT: |
| 777 | case SIGTERM: |
| 778 | cleanup(); |
| 779 | break; |
| 780 | default: |
| 781 | break; |
| 782 | } |
| 783 | |
| 784 | exit(EXIT_SUCCESS); |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | * cleanup |
| 789 | * |
| 790 | * Cleanup the daemon on exit |
| 791 | */ |
| 792 | static void cleanup() |
| 793 | { |
| 794 | /* <fun> */ |
| 795 | MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0); |
| 796 | MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0); |
| 797 | /* </fun> */ |
| 798 | |
| 799 | unlink(client_unix_sock_path); |
| 800 | unlink(apps_unix_sock_path); |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * main |
| 805 | */ |
| 806 | int main(int argc, char **argv) |
| 807 | { |
| 808 | int i; |
| 809 | int ret = 0; |
| 810 | void *status; |
| 811 | pthread_t threads[2]; |
| 812 | |
| 813 | /* Parse arguments */ |
| 814 | progname = argv[0]; |
| 815 | if ((ret = parse_args(argc, argv) < 0)) { |
| 816 | goto error; |
| 817 | } |
| 818 | |
| 819 | /* Daemonize */ |
| 820 | if (opt_daemon) { |
| 821 | ret = daemon(0, 0); |
| 822 | if (ret < 0) { |
| 823 | perror("daemon"); |
| 824 | goto error; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | /* Check if daemon is UID = 0 */ |
| 829 | is_root = !getuid(); |
| 830 | |
| 831 | /* Set all sockets path */ |
| 832 | if (is_root) { |
| 833 | if (strlen(apps_unix_sock_path) == 0) { |
| 834 | (snprintf(apps_unix_sock_path, PATH_MAX, |
| 835 | DEFAULT_GLOBAL_APPS_UNIX_SOCK)); |
| 836 | } |
| 837 | |
| 838 | if (strlen(client_unix_sock_path) == 0) { |
| 839 | (snprintf(client_unix_sock_path, PATH_MAX, |
| 840 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK)); |
| 841 | } |
| 842 | } else { |
| 843 | if (strlen(apps_unix_sock_path) == 0) { |
| 844 | (snprintf(apps_unix_sock_path, PATH_MAX, |
| 845 | DEFAULT_HOME_APPS_UNIX_SOCK, get_home_dir())); |
| 846 | } |
| 847 | |
| 848 | /* Set the cli tool unix socket path */ |
| 849 | if (strlen(client_unix_sock_path) == 0) { |
| 850 | (snprintf(client_unix_sock_path, PATH_MAX, |
| 851 | DEFAULT_HOME_CLIENT_UNIX_SOCK, get_home_dir())); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | /* See if daemon already exist. If any of the two |
| 856 | * socket needed by the daemon are present, this test fails |
| 857 | */ |
| 858 | if ((ret = check_existing_daemon()) == 0) { |
| 859 | ERR("Already running daemon.\n"); |
| 860 | /* We do not goto error because we must not |
| 861 | * cleanup() because a daemon is already working. |
| 862 | */ |
| 863 | return EXIT_FAILURE; |
| 864 | } |
| 865 | |
| 866 | if (set_signal_handler() < 0) { |
| 867 | goto error; |
| 868 | } |
| 869 | |
| 870 | /* Setup the two needed unix socket */ |
| 871 | if (init_daemon_socket() < 0) { |
| 872 | goto error; |
| 873 | } |
| 874 | |
| 875 | /* Set credentials to socket */ |
| 876 | if (is_root && (set_socket_perms() < 0)) { |
| 877 | goto error; |
| 878 | } |
| 879 | |
| 880 | /* Get parent pid if -S, --sig-parent is specified. */ |
| 881 | if (opt_sig_parent) { |
| 882 | ppid = getppid(); |
| 883 | } |
| 884 | |
| 885 | while (1) { |
| 886 | /* Create thread to manage the client socket */ |
| 887 | ret = pthread_create(&threads[0], NULL, thread_manage_clients, (void *) NULL); |
| 888 | if (ret != 0) { |
| 889 | perror("pthread_create"); |
| 890 | goto error; |
| 891 | } |
| 892 | |
| 893 | /* Create thread to manage application socket */ |
| 894 | ret = pthread_create(&threads[1], NULL, thread_manage_apps, (void *) NULL); |
| 895 | if (ret != 0) { |
| 896 | perror("pthread_create"); |
| 897 | goto error; |
| 898 | } |
| 899 | |
| 900 | for (i = 0; i < 2; i++) { |
| 901 | ret = pthread_join(threads[i], &status); |
| 902 | if (ret != 0) { |
| 903 | perror("pthread_join"); |
| 904 | goto error; |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | cleanup(); |
| 910 | return 0; |
| 911 | |
| 912 | error: |
| 913 | cleanup(); |
| 914 | |
| 915 | return EXIT_FAILURE; |
| 916 | } |