Import kernel control interface
[lttng-tools.git] / ltt-sessiond / main.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
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.
91d76f53 8 *
fac6795d
DG
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.
91d76f53 13 *
fac6795d
DG
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.
fac6795d
DG
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) */
5b97ec60 38#include <lttng/lttng.h>
fac6795d
DG
39
40#include "liblttsessiondcomm.h"
41#include "ltt-sessiond.h"
75462a81 42#include "lttngerr.h"
5b74c7b1 43#include "session.h"
fda89c9b 44#include "trace.h"
91d76f53 45#include "traceable-app.h"
fac6795d 46
5e16da05
MD
47/*
48 * TODO:
49 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
50 * with epoll on pipe and on other pipes/sockets for commands. Main
51 * simply waits on pthread join.
52 */
53
75462a81 54/* Const values */
686204ab
MD
55const char default_home_dir[] = DEFAULT_HOME_DIR;
56const char default_tracing_group[] = DEFAULT_TRACING_GROUP;
57const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
58const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
59
fac6795d 60/* Static functions */
fac6795d 61static int check_existing_daemon(void);
471d1693 62static int ust_connect_app(pid_t pid);
fac6795d 63static int init_daemon_socket(void);
62bd06d8 64static int notify_apps(const char* name);
5461b305 65static int process_client_msg(struct command_ctx *cmd_ctx);
e065084a 66static int send_unix_sock(int sock, void *buf, size_t len);
62bd06d8 67static int set_signal_handler(void);
d6f42150 68static int set_permissions(void);
5461b305 69static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size);
d6f42150
DG
70static int create_lttng_rundir(void);
71static int set_kconsumerd_sockets(void);
62bd06d8 72static void cleanup(void);
62bd06d8 73static void sighandler(int sig);
5461b305 74static void clean_command_ctx(struct command_ctx *cmd_ctx);
fac6795d 75
1fd70b72
DG
76static void *thread_manage_clients(void *data);
77static void *thread_manage_apps(void *data);
d6f42150 78static void *thread_manage_kconsumerd(void *data);
fac6795d 79
fac6795d 80/* Variables */
62bd06d8
DG
81int opt_verbose;
82int opt_quiet;
fac6795d
DG
83const char *progname;
84const char *opt_tracing_group;
5b8719f5 85static int opt_sig_parent;
fac6795d
DG
86static int opt_daemon;
87static int is_root; /* Set to 1 if the daemon is running as root */
5b8719f5 88static pid_t ppid;
fac6795d 89
d6f42150
DG
90static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */
91static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */
92static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */
93static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */
fac6795d 94
d6f42150
DG
95static int client_sock;
96static int apps_sock;
97static int kconsumerd_err_sock;
fac6795d 98
d6f42150
DG
99/*
100 * thread_manage_kconsumerd
101 *
102 * This thread manage the kconsumerd error sent
103 * back to the session daemon.
104 */
105static void *thread_manage_kconsumerd(void *data)
106{
107 int sock, ret;
108
109 DBG("[thread] Manage kconsumerd started");
110
111 ret = lttcomm_listen_unix_sock(kconsumerd_err_sock);
112 if (ret < 0) {
113 goto error;
114 }
115
116 sock = lttcomm_accept_unix_sock(kconsumerd_err_sock);
117 if (sock < 0) {
118 goto error;
119 }
120
121 while (1) {
122 //ret = lttcomm_recv_unix_sock(sock, &lsm, sizeof(lsm));
123 if (ret <= 0) {
124 /* TODO: Consumerd died? */
125 continue;
126 }
127 }
128
129error:
130 return NULL;
131}
132
fac6795d
DG
133/*
134 * thread_manage_apps
135 *
136 * This thread manage the application socket communication
137 */
138static void *thread_manage_apps(void *data)
139{
140 int sock, ret;
fac6795d
DG
141
142 /* TODO: Something more elegant is needed but fine for now */
5e16da05
MD
143 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
144 * for 32-bit vs 64-bit compat processes. */
145 /* replicate in ust with version number */
686204ab 146 struct {
fac6795d 147 int reg; /* 1:register, 0:unregister */
686204ab
MD
148 pid_t pid;
149 uid_t uid;
150 } reg_msg;
fac6795d 151
e07ae692
DG
152 DBG("[thread] Manage apps started");
153
d6f42150 154 ret = lttcomm_listen_unix_sock(apps_sock);
fac6795d
DG
155 if (ret < 0) {
156 goto error;
157 }
158
5e16da05
MD
159 /* Notify all applications to register */
160 notify_apps(default_global_apps_pipe);
161
fac6795d
DG
162 while (1) {
163 /* Blocking call, waiting for transmission */
d6f42150 164 sock = lttcomm_accept_unix_sock(apps_sock);
fac6795d
DG
165 if (sock < 0) {
166 goto error;
167 }
168
169 /* Basic recv here to handle the very simple data
170 * that the libust send to register (reg_msg).
171 */
172 ret = recv(sock, &reg_msg, sizeof(reg_msg), 0);
173 if (ret < 0) {
174 perror("recv");
175 continue;
176 }
177
178 /* Add application to the global traceable list */
179 if (reg_msg.reg == 1) {
180 /* Registering */
91d76f53
DG
181 ret = register_traceable_app(reg_msg.pid, reg_msg.uid);
182 if (ret < 0) {
183 /* register_traceable_app only return an error with
184 * ENOMEM. At this point, we better stop everything.
185 */
186 goto error;
187 }
fac6795d
DG
188 } else {
189 /* Unregistering */
91d76f53 190 unregister_traceable_app(reg_msg.pid);
fac6795d
DG
191 }
192 }
193
194error:
195
196 return NULL;
197}
198
199/*
200 * thread_manage_clients
201 *
202 * This thread manage all clients request using the unix
203 * client socket for communication.
204 */
205static void *thread_manage_clients(void *data)
206{
207 int sock, ret;
5461b305 208 struct command_ctx *cmd_ctx;
fac6795d 209
e07ae692
DG
210 DBG("[thread] Manage client started");
211
d6f42150 212 ret = lttcomm_listen_unix_sock(client_sock);
fac6795d
DG
213 if (ret < 0) {
214 goto error;
215 }
216
5b8719f5
DG
217 /* Notify parent pid that we are ready
218 * to accept command for client side.
219 */
220 if (opt_sig_parent) {
221 kill(ppid, SIGCHLD);
222 }
223
fac6795d
DG
224 while (1) {
225 /* Blocking call, waiting for transmission */
5461b305 226 DBG("Accepting client command ...");
d6f42150 227 sock = lttcomm_accept_unix_sock(client_sock);
fac6795d
DG
228 if (sock < 0) {
229 goto error;
230 }
231
5461b305
DG
232 /* Allocate context command to process the client request */
233 cmd_ctx = malloc(sizeof(struct command_ctx));
234
235 /* Allocate data buffer for reception */
236 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
237 cmd_ctx->llm = NULL;
238 cmd_ctx->session = NULL;
239
fac6795d
DG
240 /*
241 * Data is received from the lttng client. The struct
5461b305
DG
242 * lttcomm_session_msg (lsm) contains the command and data request of
243 * the client.
fac6795d 244 */
5461b305
DG
245 DBG("Receiving data from client ...");
246 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
87378cf5 247 if (ret <= 0) {
fac6795d
DG
248 continue;
249 }
250
5461b305
DG
251 /*
252 * This function dispatch the work to the kernel or userspace tracer
253 * libs and fill the lttcomm_lttng_msg data structure of all the needed
254 * informations for the client. The command context struct contains
255 * everything this function may needs.
fac6795d 256 */
5461b305 257 ret = process_client_msg(cmd_ctx);
e065084a 258 if (ret < 0) {
5461b305
DG
259 /* TODO: Inform client somehow of the fatal error. At this point,
260 * ret < 0 means that a malloc failed (ENOMEM). */
e065084a 261 /* Error detected but still accept command */
5461b305 262 clean_command_ctx(cmd_ctx);
e065084a 263 continue;
fac6795d 264 }
5461b305
DG
265
266 DBG("Sending response to client (size: %d)", cmd_ctx->lttng_msg_size);
267 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
268 if (ret < 0) {
269 ERR("Failed to send data back to client");
270 }
271
272 clean_command_ctx(cmd_ctx);
fac6795d
DG
273 }
274
275error:
276 return NULL;
277}
278
e065084a
DG
279/*
280 * send_unix_sock
281 *
282 * Send data on a unix socket using the liblttsessiondcomm API.
283 *
284 * Return lttcomm error code.
285 */
286static int send_unix_sock(int sock, void *buf, size_t len)
287{
288 /* Check valid length */
289 if (len <= 0) {
290 return -1;
291 }
292
293 return lttcomm_send_unix_sock(sock, buf, len);
294}
295
5461b305
DG
296/*
297 * clean_command_ctx
298 *
299 * Free memory of a command context structure.
300 */
301static void clean_command_ctx(struct command_ctx *cmd_ctx)
302{
303 DBG("Clean command context structure %p", cmd_ctx);
304 if (cmd_ctx) {
305 if (cmd_ctx->llm) {
306 free(cmd_ctx->llm);
307 }
308 if (cmd_ctx->lsm) {
309 free(cmd_ctx->lsm);
310 }
311 free(cmd_ctx);
312 cmd_ctx = NULL;
313 }
314}
315
fac6795d 316/*
471d1693 317 * ust_connect_app
fac6795d
DG
318 *
319 * Return a socket connected to the libust communication socket
320 * of the application identified by the pid.
379473d2
DG
321 *
322 * If the pid is not found in the traceable list,
323 * return -1 to indicate error.
fac6795d 324 */
471d1693 325static int ust_connect_app(pid_t pid)
fac6795d 326{
91d76f53
DG
327 int sock;
328 struct ltt_traceable_app *lta;
379473d2 329
e07ae692
DG
330 DBG("Connect to application pid %d", pid);
331
91d76f53
DG
332 lta = find_app_by_pid(pid);
333 if (lta == NULL) {
334 /* App not found */
7442b2ba 335 DBG("Application pid %d not found", pid);
379473d2
DG
336 return -1;
337 }
fac6795d 338
91d76f53 339 sock = ustctl_connect_pid(lta->pid);
fac6795d 340 if (sock < 0) {
75462a81 341 ERR("Fail connecting to the PID %d\n", pid);
fac6795d
DG
342 }
343
344 return sock;
345}
346
347/*
348 * notify_apps
349 *
350 * Notify apps by writing 42 to a named pipe using name.
351 * Every applications waiting for a ltt-sessiond will be notified
352 * and re-register automatically to the session daemon.
353 *
354 * Return open or write error value.
355 */
356static int notify_apps(const char *name)
357{
358 int fd;
359 int ret = -1;
360
e07ae692
DG
361 DBG("Notify the global application pipe");
362
fac6795d
DG
363 /* Try opening the global pipe */
364 fd = open(name, O_WRONLY);
365 if (fd < 0) {
366 goto error;
367 }
368
369 /* Notify by writing on the pipe */
370 ret = write(fd, "42", 2);
371 if (ret < 0) {
372 perror("write");
373 }
374
375error:
376 return ret;
377}
378
e065084a 379/*
5461b305 380 * setup_lttng_msg
ca95a216 381 *
5461b305
DG
382 * Setup the outgoing data buffer for the response (llm) by allocating the
383 * right amount of memory and copying the original information from the lsm
384 * structure.
ca95a216
DG
385 *
386 * Return total size of the buffer pointed by buf.
387 */
5461b305 388static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
ca95a216 389{
5461b305 390 int ret, buf_size, trace_name_size;
ca95a216 391
5461b305
DG
392 /*
393 * Check for the trace_name. If defined, it's part of the payload data of
394 * the llm structure.
395 */
396 trace_name_size = strlen(cmd_ctx->lsm->trace_name);
397 buf_size = trace_name_size + size;
398
399 cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
400 if (cmd_ctx->llm == NULL) {
ca95a216 401 perror("malloc");
5461b305 402 ret = -ENOMEM;
ca95a216
DG
403 goto error;
404 }
405
5461b305
DG
406 /* Copy common data */
407 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
408 cmd_ctx->llm->pid = cmd_ctx->lsm->pid;
409 if (!uuid_is_null(cmd_ctx->lsm->session_uuid)) {
410 uuid_copy(cmd_ctx->llm->session_uuid, cmd_ctx->lsm->session_uuid);
411 }
412
413 cmd_ctx->llm->trace_name_offset = trace_name_size;
414 cmd_ctx->llm->data_size = size;
415 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
416
417 /* Copy trace name to the llm structure. Begining of the payload. */
418 memcpy(cmd_ctx->llm->payload, cmd_ctx->lsm->trace_name, trace_name_size);
ca95a216
DG
419
420 return buf_size;
421
422error:
423 return ret;
424}
425
fac6795d
DG
426/*
427 * process_client_msg
428 *
5461b305
DG
429 * Process the command requested by the lttng client within the command
430 * context structure. This function make sure that the return structure (llm)
431 * is set and ready for transmission before returning.
fac6795d 432 *
e065084a 433 * Return any error encountered or 0 for success.
fac6795d 434 */
5461b305 435static int process_client_msg(struct command_ctx *cmd_ctx)
fac6795d 436{
5461b305 437 int ret;
fac6795d 438
5461b305 439 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
fac6795d 440
379473d2 441 /* Check command that needs a session */
5461b305 442 switch (cmd_ctx->lsm->cmd_type) {
5e16da05
MD
443 case LTTNG_CREATE_SESSION:
444 case LTTNG_LIST_SESSIONS:
445 case UST_LIST_APPS:
446 break;
447 default:
5461b305
DG
448 cmd_ctx->session = find_session_by_uuid(cmd_ctx->lsm->session_uuid);
449 if (cmd_ctx->session == NULL) {
379473d2 450 ret = LTTCOMM_SELECT_SESS;
5461b305 451 goto error;
379473d2 452 }
5e16da05 453 break;
379473d2
DG
454 }
455
471d1693 456 /* Connect to ust apps if available pid */
5461b305 457 if (cmd_ctx->lsm->pid > 0) {
471d1693 458 /* Connect to app using ustctl API */
5461b305
DG
459 cmd_ctx->ust_sock = ust_connect_app(cmd_ctx->lsm->pid);
460 if (cmd_ctx->ust_sock < 0) {
471d1693 461 ret = LTTCOMM_NO_TRACEABLE;
5461b305 462 goto error;
471d1693
DG
463 }
464 }
465
fac6795d 466 /* Process by command type */
5461b305 467 switch (cmd_ctx->lsm->cmd_type) {
5e16da05
MD
468 case LTTNG_CREATE_SESSION:
469 {
5461b305
DG
470 /* Setup lttng message with no payload */
471 ret = setup_lttng_msg(cmd_ctx, 0);
472 if (ret < 0) {
473 goto setup_error;
474 }
475
476 ret = create_session(cmd_ctx->lsm->session_name, &cmd_ctx->llm->session_uuid);
5e16da05
MD
477 if (ret < 0) {
478 if (ret == -1) {
479 ret = LTTCOMM_EXIST_SESS;
480 } else {
aaf97519 481 ret = LTTCOMM_FATAL;
aaf97519 482 }
5461b305 483 goto error;
8028d920 484 }
1657e9bb 485
5461b305 486 ret = LTTCOMM_OK;
5e16da05
MD
487 break;
488 }
489 case LTTNG_DESTROY_SESSION:
490 {
5461b305
DG
491 /* Setup lttng message with no payload */
492 ret = setup_lttng_msg(cmd_ctx, 0);
493 if (ret < 0) {
494 goto setup_error;
495 }
496
497 ret = destroy_session(&cmd_ctx->lsm->session_uuid);
5e16da05
MD
498 if (ret < 0) {
499 ret = LTTCOMM_NO_SESS;
5461b305 500 goto error;
5e16da05 501 }
1657e9bb 502
5461b305
DG
503 ret = LTTCOMM_OK;
504 break;
5e16da05
MD
505 }
506 case LTTNG_LIST_TRACES:
507 {
5461b305 508 unsigned int trace_count;
1657e9bb 509
5461b305 510 trace_count = get_trace_count_per_session(cmd_ctx->session);
5e16da05
MD
511 if (trace_count == 0) {
512 ret = LTTCOMM_NO_TRACE;
5461b305 513 goto error;
1657e9bb 514 }
df0da139 515
5461b305
DG
516 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
517 if (ret < 0) {
518 goto setup_error;
df0da139 519 }
ca95a216 520
5461b305
DG
521 get_traces_per_session(cmd_ctx->session,
522 (struct lttng_trace *)(cmd_ctx->llm->payload));
523
524 ret = LTTCOMM_OK;
5e16da05
MD
525 break;
526 }
527 case UST_CREATE_TRACE:
528 {
5461b305
DG
529 /* Setup lttng message with no payload */
530 ret = setup_lttng_msg(cmd_ctx, 0);
5e16da05 531 if (ret < 0) {
5461b305 532 goto setup_error;
fac6795d 533 }
ce3d728c 534
5461b305
DG
535 ret = ust_create_trace(cmd_ctx);
536 if (ret < 0) {
537 goto setup_error;
538 }
539 break;
5e16da05
MD
540 }
541 case UST_LIST_APPS:
542 {
5461b305
DG
543 unsigned int app_count;
544
545 app_count = get_app_count();
546 DBG("Traceable application count : %d", app_count);
5e16da05
MD
547 if (app_count == 0) {
548 ret = LTTCOMM_NO_APPS;
5461b305 549 goto error;
ce3d728c 550 }
520ff687 551
5461b305
DG
552 ret = setup_lttng_msg(cmd_ctx, sizeof(pid_t) * app_count);
553 if (ret < 0) {
554 goto setup_error;
520ff687 555 }
57167058 556
5461b305 557 get_app_list_pids((pid_t *)(cmd_ctx->llm->payload));
5e16da05 558
5461b305 559 ret = LTTCOMM_OK;
5e16da05
MD
560 break;
561 }
562 case UST_START_TRACE:
563 {
5461b305
DG
564 /* Setup lttng message with no payload */
565 ret = setup_lttng_msg(cmd_ctx, 0);
566 if (ret < 0) {
567 goto setup_error;
568 }
ca95a216 569
5461b305
DG
570 ret = ust_start_trace(cmd_ctx);
571 if (ret < 0) {
572 goto setup_error;
573 }
574 break;
5e16da05
MD
575 }
576 case UST_STOP_TRACE:
577 {
5461b305
DG
578 /* Setup lttng message with no payload */
579 ret = setup_lttng_msg(cmd_ctx, 0);
580 if (ret < 0) {
581 goto setup_error;
582 }
ca95a216 583
5461b305
DG
584 ret = ust_stop_trace(cmd_ctx);
585 if (ret < 0) {
586 goto setup_error;
587 }
588 break;
5e16da05
MD
589 }
590 case LTTNG_LIST_SESSIONS:
591 {
5461b305
DG
592 unsigned int session_count;
593
594 session_count = get_session_count();
5e16da05
MD
595 if (session_count == 0) {
596 ret = LTTCOMM_NO_SESS;
5461b305 597 goto error;
57167058 598 }
5e16da05 599
5461b305
DG
600 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * session_count);
601 if (ret < 0) {
602 goto setup_error;
e065084a 603 }
5e16da05 604
5461b305 605 get_lttng_session((struct lttng_session *)(cmd_ctx->llm->payload));
5e16da05 606
5461b305 607 ret = LTTCOMM_OK;
5e16da05
MD
608 break;
609 }
610 default:
611 /* Undefined command */
5461b305
DG
612 ret = setup_lttng_msg(cmd_ctx, 0);
613 if (ret < 0) {
614 goto setup_error;
615 }
616
5e16da05 617 ret = LTTCOMM_UND;
5461b305 618 break;
fac6795d
DG
619 }
620
5461b305
DG
621 /* Set return code */
622 cmd_ctx->llm->ret_code = ret;
ca95a216 623
e065084a
DG
624 return ret;
625
5461b305 626error:
7442b2ba 627 DBG("Return code to client %d", ret);
5461b305
DG
628
629 if (cmd_ctx->llm == NULL) {
630 DBG("Missing llm structure. Allocating one.");
631 ret = setup_lttng_msg(cmd_ctx, 0);
632 if (ret < 0) {
633 goto setup_error;
634 }
635 }
e065084a 636 /* Notify client of error */
5461b305 637 cmd_ctx->llm->ret_code = ret;
e065084a 638
5461b305 639setup_error:
8028d920 640 return ret;
fac6795d
DG
641}
642
643/*
644 * usage function on stderr
645 */
646static void usage(void)
647{
b716ce68 648 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
d6f42150
DG
649 fprintf(stderr, " -h, --help Display this usage.\n");
650 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
651 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
652 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
653 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
654 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
655 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
656 fprintf(stderr, " -V, --version Show version number.\n");
657 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
658 fprintf(stderr, " -q, --quiet No output at all.\n");
659 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
fac6795d
DG
660}
661
662/*
663 * daemon argument parsing
664 */
665static int parse_args(int argc, char **argv)
666{
667 int c;
668
669 static struct option long_options[] = {
670 { "client-sock", 1, 0, 'c' },
671 { "apps-sock", 1, 0, 'a' },
d6f42150
DG
672 { "kconsumerd-cmd-sock", 1, 0, 0 },
673 { "kconsumerd-err-sock", 1, 0, 0 },
fac6795d 674 { "daemonize", 0, 0, 'd' },
5b8719f5 675 { "sig-parent", 0, 0, 'S' },
fac6795d
DG
676 { "help", 0, 0, 'h' },
677 { "group", 1, 0, 'g' },
678 { "version", 0, 0, 'V' },
75462a81 679 { "quiet", 0, 0, 'q' },
3f9947db 680 { "verbose", 0, 0, 'v' },
fac6795d
DG
681 { NULL, 0, 0, 0 }
682 };
683
684 while (1) {
685 int option_index = 0;
d6f42150 686 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:", long_options, &option_index);
fac6795d
DG
687 if (c == -1) {
688 break;
689 }
690
691 switch (c) {
692 case 0:
693 fprintf(stderr, "option %s", long_options[option_index].name);
694 if (optarg) {
695 fprintf(stderr, " with arg %s\n", optarg);
696 }
697 break;
b716ce68 698 case 'c':
fac6795d
DG
699 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
700 break;
701 case 'a':
702 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
703 break;
704 case 'd':
705 opt_daemon = 1;
706 break;
707 case 'g':
708 opt_tracing_group = strdup(optarg);
709 break;
710 case 'h':
711 usage();
712 exit(EXIT_FAILURE);
713 case 'V':
714 fprintf(stdout, "%s\n", VERSION);
715 exit(EXIT_SUCCESS);
5b8719f5
DG
716 case 'S':
717 opt_sig_parent = 1;
718 break;
d6f42150
DG
719 case 'E':
720 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
721 break;
722 case 'C':
723 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
724 break;
75462a81
DG
725 case 'q':
726 opt_quiet = 1;
727 break;
3f9947db
DG
728 case 'v':
729 opt_verbose = 1;
730 break;
fac6795d
DG
731 default:
732 /* Unknown option or other error.
733 * Error is printed by getopt, just return */
734 return -1;
735 }
736 }
737
738 return 0;
739}
740
741/*
742 * init_daemon_socket
743 *
744 * Creates the two needed socket by the daemon.
d6f42150
DG
745 * apps_sock - The communication socket for all UST apps.
746 * client_sock - The communication of the cli tool (lttng).
fac6795d
DG
747 */
748static int init_daemon_socket()
749{
750 int ret = 0;
751 mode_t old_umask;
752
753 old_umask = umask(0);
754
755 /* Create client tool unix socket */
d6f42150
DG
756 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
757 if (client_sock < 0) {
758 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
759 ret = -1;
760 goto end;
761 }
762
763 /* File permission MUST be 660 */
764 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
765 if (ret < 0) {
d6f42150 766 ERR("Set file permissions failed: %s", client_unix_sock_path);
fac6795d
DG
767 perror("chmod");
768 goto end;
769 }
770
771 /* Create the application unix socket */
d6f42150
DG
772 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
773 if (apps_sock < 0) {
774 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
775 ret = -1;
776 goto end;
777 }
778
d6f42150 779 /* File permission MUST be 666 */
fac6795d
DG
780 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
781 if (ret < 0) {
d6f42150 782 ERR("Set file permissions failed: %s", apps_unix_sock_path);
fac6795d
DG
783 perror("chmod");
784 goto end;
785 }
786
787end:
788 umask(old_umask);
789 return ret;
790}
791
792/*
793 * check_existing_daemon
794 *
795 * Check if the global socket is available.
62bd06d8 796 * If yes, error is returned.
fac6795d
DG
797 */
798static int check_existing_daemon()
799{
800 int ret;
801
802 ret = access(client_unix_sock_path, F_OK);
803 if (ret == 0) {
804 ret = access(apps_unix_sock_path, F_OK);
805 }
806
807 return ret;
808}
809
810/*
62bd06d8 811 * get_home_dir
fac6795d 812 *
62bd06d8
DG
813 * Return pointer to home directory path using
814 * the env variable HOME.
fac6795d 815 *
62bd06d8 816 * Default : /tmp
fac6795d
DG
817 */
818static const char *get_home_dir(void)
819{
820 const char *home_path;
821
686204ab 822 if ((home_path = (const char *) getenv("HOME")) == NULL) {
fac6795d
DG
823 home_path = default_home_dir;
824 }
825
826 return home_path;
827}
828
829/*
d6f42150 830 * set_permissions
fac6795d 831 *
5e16da05
MD
832 * Set the tracing group gid onto the client socket.
833 *
834 * Race window between mkdir and chown is OK because we are going from
835 * less permissive (root.root) to more permissive (root.tracing).
fac6795d 836 */
d6f42150 837static int set_permissions(void)
fac6795d
DG
838{
839 int ret;
840 struct group *grp;
841
842 /* Decide which group name to use */
843 (opt_tracing_group != NULL) ?
844 (grp = getgrnam(opt_tracing_group)) :
845 (grp = getgrnam(default_tracing_group));
846
847 if (grp == NULL) {
75462a81 848 ERR("Missing tracing group. Aborting execution.\n");
fac6795d
DG
849 ret = -1;
850 goto end;
851 }
852
d6f42150
DG
853 /* Set lttng run dir */
854 ret = chown(LTTNG_RUNDIR, 0, grp->gr_gid);
855 if (ret < 0) {
856 ERR("Unable to set group on " LTTNG_RUNDIR);
857 perror("chown");
858 }
859
860 /* lttng client socket path */
fac6795d
DG
861 ret = chown(client_unix_sock_path, 0, grp->gr_gid);
862 if (ret < 0) {
d6f42150
DG
863 ERR("Unable to set group on %s", client_unix_sock_path);
864 perror("chown");
865 }
866
867 /* kconsumerd error socket path */
868 ret = chown(kconsumerd_err_unix_sock_path, 0, grp->gr_gid);
869 if (ret < 0) {
870 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
fac6795d
DG
871 perror("chown");
872 }
873
d6f42150 874 DBG("All permissions are set");
e07ae692 875
fac6795d
DG
876end:
877 return ret;
878}
879
d6f42150
DG
880/*
881 * create_lttng_rundir
882 *
883 * Create the lttng run directory needed for all
884 * global sockets and pipe.
885 */
886static int create_lttng_rundir(void)
887{
888 int ret;
889
890 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
891 if (ret < 0) {
892 ERR("Unable to create " LTTNG_RUNDIR);
893 goto error;
894 }
895
896error:
897 return ret;
898}
899
900/*
901 * set_kconsumerd_sockets
902 *
903 * Setup sockets and directory needed by the kconsumerd
904 * communication with the session daemon.
905 */
906static int set_kconsumerd_sockets(void)
907{
908 int ret;
909
910 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
911 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
912 }
913
914 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
915 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
916 }
917
918 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
919 if (ret < 0) {
920 ERR("Failed to create " KCONSUMERD_PATH);
921 goto error;
922 }
923
924 /* Create the kconsumerd error unix socket */
925 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
926 if (kconsumerd_err_sock < 0) {
927 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
928 ret = -1;
929 goto error;
930 }
931
932 /* File permission MUST be 660 */
933 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
934 if (ret < 0) {
935 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
936 perror("chmod");
937 goto error;
938 }
939
940error:
941 return ret;
942}
943
fac6795d 944/*
62bd06d8 945 * set_signal_handler
fac6795d 946 *
62bd06d8 947 * Setup signal handler for :
fac6795d
DG
948 * SIGINT, SIGTERM, SIGPIPE
949 */
950static int set_signal_handler(void)
951{
952 int ret = 0;
953 struct sigaction sa;
954 sigset_t sigset;
955
956 if ((ret = sigemptyset(&sigset)) < 0) {
957 perror("sigemptyset");
958 return ret;
959 }
960
961 sa.sa_handler = sighandler;
962 sa.sa_mask = sigset;
963 sa.sa_flags = 0;
964 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
965 perror("sigaction");
966 return ret;
967 }
968
969 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
970 perror("sigaction");
971 return ret;
972 }
973
974 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
975 perror("sigaction");
976 return ret;
977 }
978
e07ae692
DG
979 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
980
fac6795d
DG
981 return ret;
982}
983
984/**
62bd06d8 985 * sighandler
fac6795d 986 *
62bd06d8 987 * Signal handler for the daemon
fac6795d
DG
988 */
989static void sighandler(int sig)
990{
991 switch (sig) {
992 case SIGPIPE:
e07ae692 993 DBG("SIGPIPE catched");
87378cf5 994 return;
fac6795d 995 case SIGINT:
e07ae692
DG
996 DBG("SIGINT catched");
997 cleanup();
998 break;
fac6795d 999 case SIGTERM:
e07ae692 1000 DBG("SIGTERM catched");
fac6795d 1001 cleanup();
686204ab 1002 break;
fac6795d
DG
1003 default:
1004 break;
1005 }
1006
1007 exit(EXIT_SUCCESS);
1008}
1009
1010/*
62bd06d8 1011 * cleanup
fac6795d 1012 *
62bd06d8 1013 * Cleanup the daemon on exit
fac6795d
DG
1014 */
1015static void cleanup()
1016{
d6f42150
DG
1017 int ret;
1018 char *cmd;
1019
e07ae692
DG
1020 DBG("Cleaning up");
1021
fac6795d 1022 /* <fun> */
b716ce68
DG
1023 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
1024 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
fac6795d
DG
1025 /* </fun> */
1026
1027 unlink(client_unix_sock_path);
1028 unlink(apps_unix_sock_path);
d6f42150
DG
1029 unlink(kconsumerd_err_unix_sock_path);
1030
1031 ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR);
1032 if (ret < 0) {
1033 ERR("asprintf failed. Something is really wrong!");
1034 }
1035
1036 /* Remove lttng run directory */
1037 ret = system(cmd);
1038 if (ret < 0) {
1039 ERR("Unable to clean " LTTNG_RUNDIR);
1040 }
fac6795d
DG
1041}
1042
1043/*
1044 * main
1045 */
1046int main(int argc, char **argv)
1047{
1048 int i;
1049 int ret = 0;
1050 void *status;
1051 pthread_t threads[2];
1052
1053 /* Parse arguments */
1054 progname = argv[0];
1055 if ((ret = parse_args(argc, argv) < 0)) {
1056 goto error;
1057 }
1058
1059 /* Daemonize */
1060 if (opt_daemon) {
53094c05
DG
1061 ret = daemon(0, 0);
1062 if (ret < 0) {
1063 perror("daemon");
1064 goto error;
1065 }
fac6795d
DG
1066 }
1067
1068 /* Check if daemon is UID = 0 */
1069 is_root = !getuid();
1070
1071 /* Set all sockets path */
1072 if (is_root) {
d6f42150
DG
1073 ret = create_lttng_rundir();
1074 if (ret < 0) {
1075 goto error;
1076 }
1077
fac6795d 1078 if (strlen(apps_unix_sock_path) == 0) {
d6f42150
DG
1079 snprintf(apps_unix_sock_path, PATH_MAX,
1080 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
fac6795d
DG
1081 }
1082
1083 if (strlen(client_unix_sock_path) == 0) {
d6f42150
DG
1084 snprintf(client_unix_sock_path, PATH_MAX,
1085 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
1086 }
1087
1088 ret = set_kconsumerd_sockets();
1089 if (ret < 0) {
1090 goto error;
fac6795d
DG
1091 }
1092 } else {
1093 if (strlen(apps_unix_sock_path) == 0) {
d6f42150
DG
1094 snprintf(apps_unix_sock_path, PATH_MAX,
1095 DEFAULT_HOME_APPS_UNIX_SOCK, get_home_dir());
fac6795d
DG
1096 }
1097
1098 /* Set the cli tool unix socket path */
1099 if (strlen(client_unix_sock_path) == 0) {
d6f42150
DG
1100 snprintf(client_unix_sock_path, PATH_MAX,
1101 DEFAULT_HOME_CLIENT_UNIX_SOCK, get_home_dir());
fac6795d
DG
1102 }
1103 }
1104
847177cd
DG
1105 DBG("Client socket path %s", client_unix_sock_path);
1106 DBG("Application socket path %s", apps_unix_sock_path);
1107
fac6795d
DG
1108 /* See if daemon already exist. If any of the two
1109 * socket needed by the daemon are present, this test fails
1110 */
1111 if ((ret = check_existing_daemon()) == 0) {
75462a81 1112 ERR("Already running daemon.\n");
ab118b20 1113 /* We do not goto error because we must not
d6f42150 1114 * cleanup() because a daemon is already running.
ab118b20 1115 */
5e16da05 1116 exit(EXIT_FAILURE);
fac6795d
DG
1117 }
1118
1119 if (set_signal_handler() < 0) {
1120 goto error;
1121 }
1122
d6f42150 1123 /* Setup the needed unix socket */
fac6795d
DG
1124 if (init_daemon_socket() < 0) {
1125 goto error;
1126 }
1127
1128 /* Set credentials to socket */
d6f42150 1129 if (is_root && (set_permissions() < 0)) {
fac6795d
DG
1130 goto error;
1131 }
1132
5b8719f5
DG
1133 /* Get parent pid if -S, --sig-parent is specified. */
1134 if (opt_sig_parent) {
1135 ppid = getppid();
1136 }
1137
fac6795d
DG
1138 while (1) {
1139 /* Create thread to manage the client socket */
1140 ret = pthread_create(&threads[0], NULL, thread_manage_clients, (void *) NULL);
1141 if (ret != 0) {
1142 perror("pthread_create");
1143 goto error;
1144 }
1145
1146 /* Create thread to manage application socket */
1147 ret = pthread_create(&threads[1], NULL, thread_manage_apps, (void *) NULL);
1148 if (ret != 0) {
1149 perror("pthread_create");
1150 goto error;
1151 }
1152
1153 for (i = 0; i < 2; i++) {
1154 ret = pthread_join(threads[i], &status);
1155 if (ret != 0) {
1156 perror("pthread_join");
1157 goto error;
1158 }
1159 }
1160 }
1161
1162 cleanup();
5e16da05 1163 exit(EXIT_SUCCESS);
fac6795d
DG
1164
1165error:
1166 cleanup();
5e16da05 1167 exit(EXIT_FAILURE);
fac6795d 1168}
This page took 0.084306 seconds and 4 git commands to generate.