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