| 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 | |
| 39 | #include "liblttsessiondcomm.h" |
| 40 | #include "ltt-sessiond.h" |
| 41 | |
| 42 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
| 43 | const char default_tracing_group[] = DEFAULT_TRACING_GROUP; |
| 44 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; |
| 45 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; |
| 46 | |
| 47 | /* Static functions */ |
| 48 | static int set_signal_handler(void); |
| 49 | static int set_socket_perms(void); |
| 50 | static void sighandler(int); |
| 51 | static void cleanup(void); |
| 52 | static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm); |
| 53 | static int check_existing_daemon(void); |
| 54 | static int notify_apps(const char*); |
| 55 | static int connect_app(pid_t); |
| 56 | static int init_daemon_socket(void); |
| 57 | static int process_client_msg(int sock, struct lttcomm_session_msg*); |
| 58 | static int send_unix_sock(int sock, void *buf, size_t len); |
| 59 | static size_t ust_list_apps(pid_t **pids); |
| 60 | |
| 61 | static void *thread_manage_clients(void *); |
| 62 | static void *thread_manage_apps(void *); |
| 63 | |
| 64 | static int create_session(const char*, uuid_t *); |
| 65 | static void destroy_session(uuid_t); |
| 66 | |
| 67 | static struct ltt_session *find_session(uuid_t); |
| 68 | |
| 69 | /* Variables */ |
| 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; |
| 76 | |
| 77 | static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */ |
| 78 | static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */ |
| 79 | |
| 80 | static int client_socket; |
| 81 | static int apps_socket; |
| 82 | |
| 83 | static struct ltt_session *current_session; |
| 84 | |
| 85 | /* Number of element for the list below. */ |
| 86 | static unsigned int session_count; |
| 87 | static unsigned int traceable_app_count; |
| 88 | |
| 89 | /* Init session's list */ |
| 90 | static struct ltt_session_list ltt_session_list = { |
| 91 | .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), |
| 92 | }; |
| 93 | |
| 94 | /* Init ust traceabl application's list */ |
| 95 | static struct ltt_traceable_app_list ltt_traceable_app_list = { |
| 96 | .head = CDS_LIST_HEAD_INIT(ltt_traceable_app_list.head), |
| 97 | }; |
| 98 | |
| 99 | /* |
| 100 | * thread_manage_apps |
| 101 | * |
| 102 | * This thread manage the application socket communication |
| 103 | */ |
| 104 | static void *thread_manage_apps(void *data) |
| 105 | { |
| 106 | int sock, ret; |
| 107 | struct ltt_traceable_app *lta; |
| 108 | |
| 109 | /* TODO: Something more elegant is needed but fine for now */ |
| 110 | struct { |
| 111 | int reg; /* 1:register, 0:unregister */ |
| 112 | pid_t pid; |
| 113 | uid_t uid; |
| 114 | } reg_msg; |
| 115 | |
| 116 | /* Notify all applications to register */ |
| 117 | notify_apps(default_global_apps_pipe); |
| 118 | |
| 119 | ret = lttcomm_listen_unix_sock(apps_socket); |
| 120 | if (ret < 0) { |
| 121 | goto error; |
| 122 | } |
| 123 | |
| 124 | while (1) { |
| 125 | /* Blocking call, waiting for transmission */ |
| 126 | sock = lttcomm_accept_unix_sock(apps_socket); |
| 127 | if (sock < 0) { |
| 128 | goto error; |
| 129 | } |
| 130 | |
| 131 | /* Basic recv here to handle the very simple data |
| 132 | * that the libust send to register (reg_msg). |
| 133 | */ |
| 134 | ret = recv(sock, ®_msg, sizeof(reg_msg), 0); |
| 135 | if (ret < 0) { |
| 136 | perror("recv"); |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | /* Add application to the global traceable list */ |
| 141 | if (reg_msg.reg == 1) { |
| 142 | /* Registering */ |
| 143 | lta = malloc(sizeof(struct ltt_traceable_app)); |
| 144 | lta->pid = reg_msg.pid; |
| 145 | lta->uid = reg_msg.uid; |
| 146 | cds_list_add(<a->list, <t_traceable_app_list.head); |
| 147 | traceable_app_count++; |
| 148 | } else { |
| 149 | /* Unregistering */ |
| 150 | lta = NULL; |
| 151 | cds_list_for_each_entry(lta, <t_traceable_app_list.head, list) { |
| 152 | if (lta->pid == reg_msg.pid && lta->uid == reg_msg.uid) { |
| 153 | cds_list_del(<a->list); |
| 154 | /* Check to not overflow here */ |
| 155 | if (traceable_app_count != 0) { |
| 156 | traceable_app_count--; |
| 157 | } |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /* If an item was found, free it from memory */ |
| 163 | if (lta) { |
| 164 | free(lta); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | error: |
| 170 | |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * thread_manage_clients |
| 176 | * |
| 177 | * This thread manage all clients request using the unix |
| 178 | * client socket for communication. |
| 179 | */ |
| 180 | static void *thread_manage_clients(void *data) |
| 181 | { |
| 182 | int sock, ret; |
| 183 | struct lttcomm_session_msg lsm; |
| 184 | |
| 185 | ret = lttcomm_listen_unix_sock(client_socket); |
| 186 | if (ret < 0) { |
| 187 | goto error; |
| 188 | } |
| 189 | |
| 190 | /* Notify parent pid that we are ready |
| 191 | * to accept command for client side. |
| 192 | */ |
| 193 | if (opt_sig_parent) { |
| 194 | kill(ppid, SIGCHLD); |
| 195 | } |
| 196 | |
| 197 | while (1) { |
| 198 | /* Blocking call, waiting for transmission */ |
| 199 | sock = lttcomm_accept_unix_sock(client_socket); |
| 200 | if (sock < 0) { |
| 201 | goto error; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Data is received from the lttng client. The struct |
| 206 | * lttcomm_session_msg (lsm) contains the command and data |
| 207 | * request of the client. |
| 208 | */ |
| 209 | ret = lttcomm_recv_unix_sock(sock, &lsm, sizeof(lsm)); |
| 210 | if (ret < 0) { |
| 211 | continue; |
| 212 | } |
| 213 | |
| 214 | /* This function dispatch the work to the LTTng or UST libs |
| 215 | * and then sends back the response to the client. This is needed |
| 216 | * because there might be more then one lttcomm_lttng_msg to |
| 217 | * send out so process_client_msg do both jobs. |
| 218 | */ |
| 219 | ret = process_client_msg(sock, &lsm); |
| 220 | if (ret < 0) { |
| 221 | /* Error detected but still accept command */ |
| 222 | continue; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | error: |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * send_unix_sock |
| 232 | * |
| 233 | * Send data on a unix socket using the liblttsessiondcomm API. |
| 234 | * |
| 235 | * Return lttcomm error code. |
| 236 | */ |
| 237 | static int send_unix_sock(int sock, void *buf, size_t len) |
| 238 | { |
| 239 | /* Check valid length */ |
| 240 | if (len <= 0) { |
| 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | return lttcomm_send_unix_sock(sock, buf, len); |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * connect_app |
| 249 | * |
| 250 | * Return a socket connected to the libust communication socket |
| 251 | * of the application identified by the pid. |
| 252 | */ |
| 253 | static int connect_app(pid_t pid) |
| 254 | { |
| 255 | int sock; |
| 256 | |
| 257 | sock = ustctl_connect_pid(pid); |
| 258 | if (sock < 0) { |
| 259 | fprintf(stderr, "Fail connecting to the PID %d\n", pid); |
| 260 | } |
| 261 | |
| 262 | return sock; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * notify_apps |
| 267 | * |
| 268 | * Notify apps by writing 42 to a named pipe using name. |
| 269 | * Every applications waiting for a ltt-sessiond will be notified |
| 270 | * and re-register automatically to the session daemon. |
| 271 | * |
| 272 | * Return open or write error value. |
| 273 | */ |
| 274 | static int notify_apps(const char *name) |
| 275 | { |
| 276 | int fd; |
| 277 | int ret = -1; |
| 278 | |
| 279 | /* Try opening the global pipe */ |
| 280 | fd = open(name, O_WRONLY); |
| 281 | if (fd < 0) { |
| 282 | goto error; |
| 283 | } |
| 284 | |
| 285 | /* Notify by writing on the pipe */ |
| 286 | ret = write(fd, "42", 2); |
| 287 | if (ret < 0) { |
| 288 | perror("write"); |
| 289 | } |
| 290 | |
| 291 | error: |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * find_session |
| 297 | * |
| 298 | * Return a ltt_session structure ptr that matches the uuid. |
| 299 | */ |
| 300 | static struct ltt_session *find_session(uuid_t session_id) |
| 301 | { |
| 302 | struct ltt_session *iter = NULL; |
| 303 | |
| 304 | /* Sanity check for NULL session_id */ |
| 305 | if (uuid_is_null(session_id)) { |
| 306 | goto end; |
| 307 | } |
| 308 | |
| 309 | cds_list_for_each_entry(iter, <t_session_list.head, list) { |
| 310 | if (uuid_compare(iter->uuid, session_id)) { |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | end: |
| 316 | return iter; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * destroy_session |
| 321 | * |
| 322 | * Delete session from the global session list |
| 323 | * and free the memory. |
| 324 | */ |
| 325 | static void destroy_session(uuid_t session_id) |
| 326 | { |
| 327 | struct ltt_session *iter = NULL; |
| 328 | |
| 329 | cds_list_for_each_entry(iter, <t_session_list.head, list) { |
| 330 | if (uuid_compare(iter->uuid, session_id)) { |
| 331 | cds_list_del(&iter->list); |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | if (iter) { |
| 337 | free(iter); |
| 338 | session_count--; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * create_session |
| 344 | * |
| 345 | * Create a brand new session, |
| 346 | */ |
| 347 | static int create_session(const char *name, uuid_t *session_id) |
| 348 | { |
| 349 | struct ltt_session *new_session; |
| 350 | |
| 351 | /* Allocate session data structure */ |
| 352 | new_session = malloc(sizeof(struct ltt_session)); |
| 353 | if (new_session == NULL) { |
| 354 | perror("malloc"); |
| 355 | goto error; |
| 356 | } |
| 357 | |
| 358 | if (name != NULL) { |
| 359 | if (asprintf(&new_session->name, "%s", name) < 0) { |
| 360 | goto error; |
| 361 | } |
| 362 | } else { |
| 363 | /* Generate session name based on the session count */ |
| 364 | if (asprintf(&new_session->name, "%s%d", "auto", session_count) < 0) { |
| 365 | goto error; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /* UUID generation */ |
| 370 | uuid_generate(new_session->uuid); |
| 371 | uuid_copy(*session_id, new_session->uuid); |
| 372 | |
| 373 | /* Set consumer (identifier) to 0. This means that there is |
| 374 | * NO consumer attach to that session yet. |
| 375 | */ |
| 376 | new_session->ust_consumer = 0; |
| 377 | new_session->lttng_consumer = 0; |
| 378 | |
| 379 | /* Init list */ |
| 380 | CDS_INIT_LIST_HEAD(&new_session->ust_traces); |
| 381 | CDS_INIT_LIST_HEAD(&new_session->lttng_traces); |
| 382 | |
| 383 | /* Add new session to the global session list */ |
| 384 | cds_list_add(&new_session->list, <t_session_list.head); |
| 385 | |
| 386 | session_count++; |
| 387 | |
| 388 | return 0; |
| 389 | |
| 390 | error: |
| 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * ust_list_apps |
| 396 | * |
| 397 | * List traceable user-space application and fill an |
| 398 | * array of pids. |
| 399 | * |
| 400 | * Return size of the array. |
| 401 | */ |
| 402 | static size_t ust_list_apps(pid_t **pids) |
| 403 | { |
| 404 | size_t size = 0; |
| 405 | struct ltt_traceable_app *iter = NULL; |
| 406 | pid_t *p; |
| 407 | |
| 408 | if (traceable_app_count == 0) { |
| 409 | /* No dynamic allocation is done */ |
| 410 | goto end; |
| 411 | } |
| 412 | |
| 413 | p = malloc(sizeof(pid_t) * traceable_app_count); |
| 414 | |
| 415 | /* TODO: Mutex needed to access this list */ |
| 416 | cds_list_for_each_entry(iter, <t_traceable_app_list.head, list) { |
| 417 | p[size] = iter->pid; |
| 418 | size++; |
| 419 | } |
| 420 | |
| 421 | *pids = p; |
| 422 | |
| 423 | end: |
| 424 | return size; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * copy_common_data |
| 429 | * |
| 430 | * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg |
| 431 | */ |
| 432 | static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm) |
| 433 | { |
| 434 | llm->cmd_type = lsm->cmd_type; |
| 435 | llm->pid = lsm->pid; |
| 436 | if (!uuid_is_null(lsm->session_id)) { |
| 437 | uuid_copy(llm->session_id, lsm->session_id); |
| 438 | } |
| 439 | strncpy(llm->trace_name, lsm->trace_name, sizeof(llm->trace_name)); |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * process_client_msg |
| 444 | * |
| 445 | * This takes the lttcomm_session_msg struct and process the command requested |
| 446 | * by the client. It then creates response(s) and send it back to the |
| 447 | * given socket (sock). |
| 448 | * |
| 449 | * Return any error encountered or 0 for success. |
| 450 | */ |
| 451 | static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) |
| 452 | { |
| 453 | int ret; |
| 454 | struct lttcomm_lttng_msg llm; |
| 455 | |
| 456 | /* Copy common data to identify the response |
| 457 | * on the lttng client side. |
| 458 | */ |
| 459 | copy_common_data(&llm, lsm); |
| 460 | |
| 461 | /* Default return code. |
| 462 | * In our world, everything is OK... right? ;) |
| 463 | */ |
| 464 | llm.ret_code = LTTCOMM_OK; |
| 465 | |
| 466 | /* Process by command type */ |
| 467 | switch (lsm->cmd_type) { |
| 468 | case UST_LIST_APPS: |
| 469 | { |
| 470 | pid_t *pids; |
| 471 | llm.num_pckt = ust_list_apps(&pids); |
| 472 | if (llm.num_pckt == 0) { |
| 473 | ret = LTTCOMM_NO_APPS; |
| 474 | goto error; |
| 475 | } |
| 476 | |
| 477 | /* Send all packets */ |
| 478 | while (llm.num_pckt != 0) { |
| 479 | llm.u.list_apps.pid = pids[traceable_app_count - llm.num_pckt]; |
| 480 | ret = send_unix_sock(sock, (void*) &llm, sizeof(llm)); |
| 481 | if (ret < 0) { |
| 482 | goto send_error; |
| 483 | } |
| 484 | llm.num_pckt--; |
| 485 | } |
| 486 | /* Allocated array by ust_list_apps() */ |
| 487 | free(pids); |
| 488 | |
| 489 | break; |
| 490 | } |
| 491 | case LTTNG_LIST_SESSIONS: |
| 492 | { |
| 493 | struct ltt_session *iter = NULL; |
| 494 | |
| 495 | llm.num_pckt = session_count; |
| 496 | if (llm.num_pckt == 0) { |
| 497 | ret = LTTCOMM_NO_SESS; |
| 498 | goto error; |
| 499 | } |
| 500 | |
| 501 | cds_list_for_each_entry(iter, <t_session_list.head, list) { |
| 502 | uuid_unparse(iter->uuid, llm.u.list_sessions.uuid); |
| 503 | strncpy(llm.u.list_sessions.name, iter->name, |
| 504 | sizeof(llm.u.list_sessions.name)); |
| 505 | ret = send_unix_sock(sock, (void*) &llm, sizeof(llm)); |
| 506 | if (ret < 0) { |
| 507 | goto send_error; |
| 508 | } |
| 509 | llm.num_pckt--; |
| 510 | } |
| 511 | |
| 512 | break; |
| 513 | } |
| 514 | default: |
| 515 | { |
| 516 | /* Undefined command */ |
| 517 | ret = LTTCOMM_UND; |
| 518 | break; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | return 0; |
| 523 | |
| 524 | send_error: |
| 525 | return ret; |
| 526 | |
| 527 | error: |
| 528 | /* Notify client of error */ |
| 529 | llm.ret_code = ret; |
| 530 | send_unix_sock(sock, (void*) &llm, sizeof(llm)); |
| 531 | |
| 532 | return -1; |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * usage function on stderr |
| 537 | */ |
| 538 | static void usage(void) |
| 539 | { |
| 540 | fprintf(stderr, "Usage:\n%s OPTIONS\n\nOptions:\n" |
| 541 | "\t-h, --help\t\tDisplay this usage.\n" |
| 542 | "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n" |
| 543 | "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n" |
| 544 | "\t-d, --daemonize\t\tStart as a daemon.\n" |
| 545 | "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n" |
| 546 | "\t-V, --version\t\tShow version number.\n" |
| 547 | "\t-S, --sig-parent\t\tSend SIGCHLD to parent pid to notify readiness.\n", |
| 548 | progname); |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * daemon argument parsing |
| 553 | */ |
| 554 | static int parse_args(int argc, char **argv) |
| 555 | { |
| 556 | int c; |
| 557 | |
| 558 | static struct option long_options[] = { |
| 559 | { "client-sock", 1, 0, 'c' }, |
| 560 | { "apps-sock", 1, 0, 'a' }, |
| 561 | { "daemonize", 0, 0, 'd' }, |
| 562 | { "sig-parent", 0, 0, 'S' }, |
| 563 | { "help", 0, 0, 'h' }, |
| 564 | { "group", 1, 0, 'g' }, |
| 565 | { "version", 0, 0, 'V' }, |
| 566 | { NULL, 0, 0, 0 } |
| 567 | }; |
| 568 | |
| 569 | while (1) { |
| 570 | int option_index = 0; |
| 571 | c = getopt_long(argc, argv, "dhVS" "a:c:g:s:", long_options, &option_index); |
| 572 | if (c == -1) { |
| 573 | break; |
| 574 | } |
| 575 | |
| 576 | switch (c) { |
| 577 | case 0: |
| 578 | fprintf(stderr, "option %s", long_options[option_index].name); |
| 579 | if (optarg) { |
| 580 | fprintf(stderr, " with arg %s\n", optarg); |
| 581 | } |
| 582 | break; |
| 583 | case 's': |
| 584 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
| 585 | break; |
| 586 | case 'a': |
| 587 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); |
| 588 | break; |
| 589 | case 'd': |
| 590 | opt_daemon = 1; |
| 591 | break; |
| 592 | case 'g': |
| 593 | opt_tracing_group = strdup(optarg); |
| 594 | break; |
| 595 | case 'h': |
| 596 | usage(); |
| 597 | exit(EXIT_FAILURE); |
| 598 | case 'V': |
| 599 | fprintf(stdout, "%s\n", VERSION); |
| 600 | exit(EXIT_SUCCESS); |
| 601 | case 'S': |
| 602 | opt_sig_parent = 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 | fprintf(stderr, "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 | 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 | fprintf(stdout, "\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0); |
| 795 | fprintf(stdout, "%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",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 | fprintf(stderr, "Already running daemon.\n"); |
| 859 | goto error; |
| 860 | } |
| 861 | |
| 862 | if (set_signal_handler() < 0) { |
| 863 | goto error; |
| 864 | } |
| 865 | |
| 866 | /* Setup the two needed unix socket */ |
| 867 | if (init_daemon_socket() < 0) { |
| 868 | goto error; |
| 869 | } |
| 870 | |
| 871 | /* Set credentials to socket */ |
| 872 | if (is_root && (set_socket_perms() < 0)) { |
| 873 | goto error; |
| 874 | } |
| 875 | |
| 876 | /* Get parent pid if -S, --sig-parent is specified. */ |
| 877 | if (opt_sig_parent) { |
| 878 | ppid = getppid(); |
| 879 | } |
| 880 | |
| 881 | while (1) { |
| 882 | /* Create thread to manage the client socket */ |
| 883 | ret = pthread_create(&threads[0], NULL, thread_manage_clients, (void *) NULL); |
| 884 | if (ret != 0) { |
| 885 | perror("pthread_create"); |
| 886 | goto error; |
| 887 | } |
| 888 | |
| 889 | /* Create thread to manage application socket */ |
| 890 | ret = pthread_create(&threads[1], NULL, thread_manage_apps, (void *) NULL); |
| 891 | if (ret != 0) { |
| 892 | perror("pthread_create"); |
| 893 | goto error; |
| 894 | } |
| 895 | |
| 896 | for (i = 0; i < 2; i++) { |
| 897 | ret = pthread_join(threads[i], &status); |
| 898 | if (ret != 0) { |
| 899 | perror("pthread_join"); |
| 900 | goto error; |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | cleanup(); |
| 906 | return 0; |
| 907 | |
| 908 | error: |
| 909 | cleanup(); |
| 910 | |
| 911 | return EXIT_FAILURE; |
| 912 | } |