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