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