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