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