2fcd923a1897d0f58b2b2f53568c83b2f526162d
[lttng-tools.git] / ltt-sessiond / main.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <fcntl.h>
21 #include <getopt.h>
22 #include <grp.h>
23 #include <limits.h>
24 #include <pthread.h>
25 #include <semaphore.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/ipc.h>
31 #include <sys/shm.h>
32 #include <sys/socket.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <sys/resource.h>
37 #include <unistd.h>
38
39 #include <urcu/list.h> /* URCU list library (-lurcu) */
40 #include <lttng/lttng.h>
41
42 #include "liblttsessiondcomm.h"
43 #include "ltt-sessiond.h"
44 #include "lttngerr.h"
45 #include "kernel-ctl.h"
46 #include "ust-ctl.h"
47 #include "session.h"
48 #include "traceable-app.h"
49 #include "lttng-kconsumerd.h"
50 #include "libustctl.h"
51
52 /*
53 * TODO:
54 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
55 * with epoll on pipe and on other pipes/sockets for commands. Main
56 * simply waits on pthread join.
57 */
58
59 /* Const values */
60 const char default_home_dir[] = DEFAULT_HOME_DIR;
61 const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP;
62 const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
63 const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
64
65 /* Variables */
66 int opt_verbose; /* Not static for lttngerr.h */
67 int opt_quiet; /* Not static for lttngerr.h */
68 const char *progname;
69 const char *opt_tracing_group;
70 static int opt_sig_parent;
71 static int opt_daemon;
72 static int is_root; /* Set to 1 if the daemon is running as root */
73 static pid_t ppid; /* Parent PID for --sig-parent option */
74 static pid_t kconsumerd_pid;
75
76 static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */
77 static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */
78 static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */
79 static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */
80
81 /* Sockets and FDs */
82 static int client_sock;
83 static int apps_sock;
84 static int kconsumerd_err_sock;
85 static int kconsumerd_cmd_sock;
86 static int kernel_tracer_fd;
87
88 /* Pthread, Mutexes and Semaphores */
89 static pthread_t kconsumerd_thread;
90 static pthread_t apps_thread;
91 static pthread_t client_thread;
92 static sem_t kconsumerd_sem;
93
94 static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */
95
96 /*
97 * teardown_kernel_session
98 *
99 * Complete teardown of a kernel session. This free all data structure
100 * related to a kernel session and update counter.
101 */
102 static void teardown_kernel_session(struct ltt_session *session)
103 {
104 if (session->kernel_session != NULL) {
105 DBG("Tearing down kernel session");
106 trace_destroy_kernel_session(session->kernel_session);
107 /* Extra precaution */
108 session->kernel_session = NULL;
109 /* Decrement session count */
110 session->kern_session_count--;
111 }
112 }
113
114 /*
115 * cleanup
116 *
117 * Cleanup the daemon on exit
118 */
119 static void cleanup()
120 {
121 int ret;
122 char *cmd;
123 struct ltt_session *sess;
124
125 DBG("Cleaning up");
126
127 /* <fun> */
128 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
129 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
130 /* </fun> */
131
132 /* Stopping all threads */
133 DBG("Terminating all threads");
134 pthread_cancel(client_thread);
135 pthread_cancel(apps_thread);
136 if (kconsumerd_pid != 0) {
137 pthread_cancel(kconsumerd_thread);
138 }
139
140 DBG("Unlinking all unix socket");
141 unlink(client_unix_sock_path);
142 unlink(apps_unix_sock_path);
143 unlink(kconsumerd_err_unix_sock_path);
144
145 DBG("Removing %s directory", LTTNG_RUNDIR);
146 ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR);
147 if (ret < 0) {
148 ERR("asprintf failed. Something is really wrong!");
149 }
150
151 /* Remove lttng run directory */
152 ret = system(cmd);
153 if (ret < 0) {
154 ERR("Unable to clean " LTTNG_RUNDIR);
155 }
156
157 DBG("Cleaning up all session");
158 /* Cleanup ALL session */
159 cds_list_for_each_entry(sess, &ltt_session_list.head, list) {
160 teardown_kernel_session(sess);
161 // TODO complete session cleanup (including UST)
162 }
163
164 DBG("Closing kernel fd");
165 close(kernel_tracer_fd);
166 }
167
168 /*
169 * send_unix_sock
170 *
171 * Send data on a unix socket using the liblttsessiondcomm API.
172 *
173 * Return lttcomm error code.
174 */
175 static int send_unix_sock(int sock, void *buf, size_t len)
176 {
177 /* Check valid length */
178 if (len <= 0) {
179 return -1;
180 }
181
182 return lttcomm_send_unix_sock(sock, buf, len);
183 }
184
185 /*
186 * clean_command_ctx
187 *
188 * Free memory of a command context structure.
189 */
190 static void clean_command_ctx(struct command_ctx *cmd_ctx)
191 {
192 DBG("Clean command context structure %p", cmd_ctx);
193 if (cmd_ctx) {
194 if (cmd_ctx->llm) {
195 free(cmd_ctx->llm);
196 }
197 if (cmd_ctx->lsm) {
198 free(cmd_ctx->lsm);
199 }
200 free(cmd_ctx);
201 cmd_ctx = NULL;
202 }
203 }
204
205 /*
206 * send_kconsumerd_fds
207 *
208 * Send all stream fds of the kernel session to the consumer.
209 */
210 static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session)
211 {
212 int ret;
213 size_t nb_fd;
214 struct ltt_kernel_stream *stream;
215 struct ltt_kernel_channel *chan;
216 struct lttcomm_kconsumerd_header lkh;
217 struct lttcomm_kconsumerd_msg lkm;
218
219 nb_fd = session->stream_count_global;
220
221 /* Setup header */
222 lkh.payload_size = (nb_fd + 1) * sizeof(struct lttcomm_kconsumerd_msg);
223 lkh.cmd_type = ADD_STREAM;
224
225 DBG("Sending kconsumerd header");
226
227 ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
228 if (ret < 0) {
229 perror("send kconsumerd header");
230 goto error;
231 }
232
233 DBG("Sending metadata stream fd");
234
235 /* Send metadata stream fd first */
236 lkm.fd = session->metadata_stream_fd;
237 lkm.state = ACTIVE_FD;
238 lkm.max_sb_size = session->metadata->conf->attr.subbuf_size;
239 strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX);
240
241 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
242 if (ret < 0) {
243 perror("send kconsumerd fd");
244 goto error;
245 }
246
247 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
248 cds_list_for_each_entry(stream, &chan->stream_list.head, list) {
249 lkm.fd = stream->fd;
250 lkm.state = stream->state;
251 lkm.max_sb_size = chan->channel->attr.subbuf_size;
252 strncpy(lkm.path_name, stream->pathname, PATH_MAX);
253
254 DBG("Sending fd %d to kconsumerd", lkm.fd);
255
256 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
257 if (ret < 0) {
258 perror("send kconsumerd fd");
259 goto error;
260 }
261 }
262 }
263
264 DBG("Kconsumerd fds sent");
265
266 return 0;
267
268 error:
269 return ret;
270 }
271
272 /*
273 * create_trace_dir
274 *
275 * Create the trace output directory.
276 */
277 static int create_trace_dir(struct ltt_kernel_session *session)
278 {
279 int ret;
280 struct ltt_kernel_channel *chan;
281
282 /* Create all channel directories */
283 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
284 DBG("Creating trace directory at %s", chan->pathname);
285 // TODO: recursive create dir
286 ret = mkdir(chan->pathname, S_IRWXU | S_IRWXG );
287 if (ret < 0) {
288 if (ret != EEXIST) {
289 perror("mkdir trace path");
290 ret = -errno;
291 goto error;
292 }
293 }
294 }
295
296 return 0;
297
298 error:
299 return ret;
300 }
301
302 /*
303 * ust_connect_app
304 *
305 * Return a socket connected to the libust communication socket
306 * of the application identified by the pid.
307 *
308 * If the pid is not found in the traceable list,
309 * return -1 to indicate error.
310 */
311 static int ust_connect_app(pid_t pid)
312 {
313 int sock;
314 struct ltt_traceable_app *lta;
315
316 DBG("Connect to application pid %d", pid);
317
318 lta = find_app_by_pid(pid);
319 if (lta == NULL) {
320 /* App not found */
321 DBG("Application pid %d not found", pid);
322 return -1;
323 }
324
325 sock = ustctl_connect_pid(lta->pid);
326 if (sock < 0) {
327 ERR("Fail connecting to the PID %d", pid);
328 }
329
330 return sock;
331 }
332
333 /*
334 * notify_apps
335 *
336 * Notify apps by writing 42 to a named pipe using name.
337 * Every applications waiting for a ltt-sessiond will be notified
338 * and re-register automatically to the session daemon.
339 *
340 * Return open or write error value.
341 */
342 static int notify_apps(const char *name)
343 {
344 int fd;
345 int ret = -1;
346
347 DBG("Notify the global application pipe");
348
349 /* Try opening the global pipe */
350 fd = open(name, O_WRONLY);
351 if (fd < 0) {
352 goto error;
353 }
354
355 /* Notify by writing on the pipe */
356 ret = write(fd, "42", 2);
357 if (ret < 0) {
358 perror("write");
359 }
360
361 error:
362 return ret;
363 }
364
365 /*
366 * setup_lttng_msg
367 *
368 * Setup the outgoing data buffer for the response (llm) by allocating the
369 * right amount of memory and copying the original information from the lsm
370 * structure.
371 *
372 * Return total size of the buffer pointed by buf.
373 */
374 static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
375 {
376 int ret, buf_size;
377
378 buf_size = size;
379
380 cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
381 if (cmd_ctx->llm == NULL) {
382 perror("malloc");
383 ret = -ENOMEM;
384 goto error;
385 }
386
387 /* Copy common data */
388 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
389 cmd_ctx->llm->pid = cmd_ctx->lsm->pid;
390
391 cmd_ctx->llm->data_size = size;
392 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
393
394 return buf_size;
395
396 error:
397 return ret;
398 }
399
400 /*
401 * thread_manage_kconsumerd
402 *
403 * This thread manage the kconsumerd error sent
404 * back to the session daemon.
405 */
406 static void *thread_manage_kconsumerd(void *data)
407 {
408 int sock, ret;
409 enum lttcomm_return_code code;
410
411 DBG("[thread] Manage kconsumerd started");
412
413 ret = lttcomm_listen_unix_sock(kconsumerd_err_sock);
414 if (ret < 0) {
415 goto error;
416 }
417
418 sock = lttcomm_accept_unix_sock(kconsumerd_err_sock);
419 if (sock < 0) {
420 goto error;
421 }
422
423 /* Getting status code from kconsumerd */
424 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
425 if (ret <= 0) {
426 goto error;
427 }
428
429 if (code == KCONSUMERD_COMMAND_SOCK_READY) {
430 kconsumerd_cmd_sock = lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path);
431 if (kconsumerd_cmd_sock < 0) {
432 sem_post(&kconsumerd_sem);
433 perror("kconsumerd connect");
434 goto error;
435 }
436 /* Signal condition to tell that the kconsumerd is ready */
437 sem_post(&kconsumerd_sem);
438 DBG("Kconsumerd command socket ready");
439 } else {
440 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
441 lttcomm_get_readable_code(-code));
442 goto error;
443 }
444
445 /* Wait for any kconsumerd error */
446 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
447 if (ret <= 0) {
448 ERR("Kconsumerd closed the command socket");
449 goto error;
450 }
451
452 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code));
453
454 error:
455 kconsumerd_pid = 0;
456 DBG("Kconsumerd thread dying");
457 return NULL;
458 }
459
460 /*
461 * thread_manage_apps
462 *
463 * This thread manage the application socket communication
464 */
465 static void *thread_manage_apps(void *data)
466 {
467 int sock, ret;
468
469 /* TODO: Something more elegant is needed but fine for now */
470 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
471 * for 32-bit vs 64-bit compat processes. */
472 /* replicate in ust with version number */
473 struct {
474 int reg; /* 1:register, 0:unregister */
475 pid_t pid;
476 uid_t uid;
477 } reg_msg;
478
479 DBG("[thread] Manage apps started");
480
481 ret = lttcomm_listen_unix_sock(apps_sock);
482 if (ret < 0) {
483 goto error;
484 }
485
486 /* Notify all applications to register */
487 notify_apps(default_global_apps_pipe);
488
489 while (1) {
490 DBG("Accepting application registration");
491 /* Blocking call, waiting for transmission */
492 sock = lttcomm_accept_unix_sock(apps_sock);
493 if (sock < 0) {
494 goto error;
495 }
496
497 /* Basic recv here to handle the very simple data
498 * that the libust send to register (reg_msg).
499 */
500 ret = recv(sock, &reg_msg, sizeof(reg_msg), 0);
501 if (ret < 0) {
502 perror("recv");
503 continue;
504 }
505
506 /* Add application to the global traceable list */
507 if (reg_msg.reg == 1) {
508 /* Registering */
509 ret = register_traceable_app(reg_msg.pid, reg_msg.uid);
510 if (ret < 0) {
511 /* register_traceable_app only return an error with
512 * ENOMEM. At this point, we better stop everything.
513 */
514 goto error;
515 }
516 } else {
517 /* Unregistering */
518 unregister_traceable_app(reg_msg.pid);
519 }
520 }
521
522 error:
523
524 return NULL;
525 }
526
527 /*
528 * spawn_kconsumerd_thread
529 *
530 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
531 * exec or it will fails.
532 */
533 static int spawn_kconsumerd_thread(void)
534 {
535 int ret;
536
537 /* Setup semaphore */
538 sem_init(&kconsumerd_sem, 0, 0);
539
540 ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL);
541 if (ret != 0) {
542 perror("pthread_create kconsumerd");
543 goto error;
544 }
545
546 /* Wait for the kconsumerd thread to be ready */
547 sem_wait(&kconsumerd_sem);
548
549 if (kconsumerd_pid == 0) {
550 ERR("Kconsumerd did not start");
551 goto error;
552 }
553
554 return 0;
555
556 error:
557 ret = LTTCOMM_KERN_CONSUMER_FAIL;
558 return ret;
559 }
560
561 /*
562 * spawn_kconsumerd
563 *
564 * Fork and exec a kernel consumer daemon (kconsumerd).
565 *
566 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
567 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
568 * kernel consumer MUST only be spawned before creating a kernel session.
569 *
570 * Return pid if successful else -1.
571 */
572 static pid_t spawn_kconsumerd(void)
573 {
574 int ret;
575 pid_t pid;
576
577 DBG("Spawning kconsumerd");
578
579 pid = fork();
580 if (pid == 0) {
581 /*
582 * Exec kconsumerd.
583 */
584 execlp("kconsumerd", "kconsumerd", "--verbose", NULL);
585 if (errno != 0) {
586 perror("kernel start consumer exec");
587 }
588 exit(EXIT_FAILURE);
589 } else if (pid > 0) {
590 ret = pid;
591 goto error;
592 } else {
593 perror("kernel start consumer fork");
594 ret = -errno;
595 goto error;
596 }
597
598 error:
599 return ret;
600 }
601
602 /*
603 * start_kconsumerd
604 *
605 * Spawn the kconsumerd daemon and session daemon thread.
606 */
607 static int start_kconsumerd(void)
608 {
609 int ret;
610
611 pthread_mutex_lock(&kconsumerd_pid_mutex);
612 if (kconsumerd_pid != 0) {
613 goto end;
614 }
615
616 ret = spawn_kconsumerd();
617 if (ret < 0) {
618 ERR("Spawning kconsumerd failed");
619 ret = LTTCOMM_KERN_CONSUMER_FAIL;
620 pthread_mutex_unlock(&kconsumerd_pid_mutex);
621 goto error;
622 }
623
624 /* Setting up the global kconsumerd_pid */
625 kconsumerd_pid = ret;
626 pthread_mutex_unlock(&kconsumerd_pid_mutex);
627
628 DBG("Kconsumerd pid %d", ret);
629
630 DBG("Spawning kconsumerd thread");
631 ret = spawn_kconsumerd_thread();
632 if (ret < 0) {
633 ERR("Fatal error spawning kconsumerd thread");
634 goto error;
635 }
636
637 end:
638 pthread_mutex_unlock(&kconsumerd_pid_mutex);
639 return 0;
640
641 error:
642 return ret;
643 }
644
645 /*
646 * init_kernel_tracer
647 *
648 * Setup necessary data for kernel tracer action.
649 */
650 static void init_kernel_tracer(void)
651 {
652 /* Set the global kernel tracer fd */
653 kernel_tracer_fd = open(DEFAULT_KERNEL_TRACER_PATH, O_RDWR);
654 if (kernel_tracer_fd < 0) {
655 WARN("No kernel tracer available");
656 kernel_tracer_fd = 0;
657 }
658
659 DBG("Kernel tracer fd %d", kernel_tracer_fd);
660 }
661
662 /*
663 * start_kernel_trace
664 *
665 * Start tracing by creating trace directory and sending FDs to the kernel
666 * consumer.
667 */
668 static int start_kernel_trace(struct ltt_kernel_session *session)
669 {
670 int ret;
671
672 /* Create trace directory */
673 ret = create_trace_dir(session);
674 if (ret < 0) {
675 if (ret == -EEXIST) {
676 ret = LTTCOMM_KERN_DIR_EXIST;
677 } else {
678 ret = LTTCOMM_KERN_DIR_FAIL;
679 goto error;
680 }
681 }
682
683 if (session->kconsumer_fds_sent == 0) {
684 ret = send_kconsumerd_fds(kconsumerd_cmd_sock, session);
685 if (ret < 0) {
686 ERR("Send kconsumerd fds failed");
687 ret = LTTCOMM_KERN_CONSUMER_FAIL;
688 goto error;
689 }
690
691 session->kconsumer_fds_sent = 1;
692 }
693
694 error:
695 return ret;
696 }
697
698 /*
699 * init_default_channel
700 *
701 * Allocate a channel structure and fill it.
702 */
703 static struct lttng_channel *init_default_channel(void)
704 {
705 struct lttng_channel *chan;
706
707 chan = malloc(sizeof(struct lttng_channel));
708 if (chan == NULL) {
709 perror("init channel malloc");
710 goto error;
711 }
712
713 if (snprintf(chan->name, NAME_MAX, DEFAULT_CHANNEL_NAME) < 0) {
714 perror("snprintf defautl channel name");
715 return NULL;
716 }
717
718 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
719 chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
720 chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
721 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
722 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
723 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
724
725 error:
726 return chan;
727 }
728
729 /*
730 * create_kernel_session
731 *
732 * Create a kernel tracer session then create the default channel.
733 */
734 static int create_kernel_session(struct ltt_session *session)
735 {
736 int ret;
737 struct lttng_channel *chan;
738
739 DBG("Creating kernel session");
740
741 ret = kernel_create_session(session, kernel_tracer_fd);
742 if (ret < 0) {
743 ret = LTTCOMM_KERN_SESS_FAIL;
744 goto error;
745 }
746
747 chan = init_default_channel();
748 if (chan == NULL) {
749 ret = LTTCOMM_FATAL;
750 goto error;
751 }
752
753 DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME);
754
755 ret = kernel_create_channel(session->kernel_session, chan);
756 if (ret < 0) {
757 ret = LTTCOMM_KERN_CHAN_FAIL;
758 goto error;
759 }
760
761 error:
762 return ret;
763 }
764
765 /*
766 * process_client_msg
767 *
768 * Process the command requested by the lttng client within the command
769 * context structure. This function make sure that the return structure (llm)
770 * is set and ready for transmission before returning.
771 *
772 * Return any error encountered or 0 for success.
773 */
774 static int process_client_msg(struct command_ctx *cmd_ctx)
775 {
776 int ret;
777
778 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
779
780 /* Listing commands don't need a session */
781 switch (cmd_ctx->lsm->cmd_type) {
782 case LTTNG_CREATE_SESSION:
783 case LTTNG_LIST_SESSIONS:
784 case LTTNG_LIST_EVENTS:
785 case LTTNG_KERNEL_LIST_EVENTS:
786 case LTTNG_LIST_TRACEABLE_APPS:
787 break;
788 default:
789 DBG("Getting session %s by name", cmd_ctx->lsm->session_name);
790 cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session_name);
791 if (cmd_ctx->session == NULL) {
792 /* If session name not found */
793 if (cmd_ctx->lsm->session_name != NULL) {
794 ret = LTTCOMM_SESS_NOT_FOUND;
795 } else { /* If no session name specified */
796 ret = LTTCOMM_SELECT_SESS;
797 }
798 goto error;
799 }
800 break;
801 }
802
803 /*
804 * Check kernel command for kernel session.
805 */
806 switch (cmd_ctx->lsm->cmd_type) {
807 case LTTNG_KERNEL_ADD_CONTEXT:
808 case LTTNG_KERNEL_CREATE_CHANNEL:
809 case LTTNG_KERNEL_DISABLE_ALL_EVENT:
810 case LTTNG_KERNEL_DISABLE_CHANNEL:
811 case LTTNG_KERNEL_DISABLE_EVENT:
812 case LTTNG_KERNEL_ENABLE_ALL_EVENT:
813 case LTTNG_KERNEL_ENABLE_CHANNEL:
814 case LTTNG_KERNEL_ENABLE_EVENT:
815 case LTTNG_KERNEL_LIST_EVENTS:
816 /* Kernel tracer check */
817 if (kernel_tracer_fd == 0) {
818 init_kernel_tracer();
819 if (kernel_tracer_fd == 0) {
820 ret = LTTCOMM_KERN_NA;
821 goto error;
822 }
823 }
824
825 /* Need a session for kernel command */
826 if (cmd_ctx->lsm->cmd_type != LTTNG_KERNEL_LIST_EVENTS &&
827 cmd_ctx->session->kernel_session == NULL) {
828
829 ret = create_kernel_session(cmd_ctx->session);
830 if (ret < 0) {
831 ret = LTTCOMM_KERN_SESS_FAIL;
832 goto error;
833 }
834
835 /* Start the kernel consumer daemon */
836 if (kconsumerd_pid == 0) {
837 ret = start_kconsumerd();
838 if (ret < 0) {
839 goto error;
840 }
841 }
842 }
843 }
844
845 /* Connect to ust apps if available pid */
846 if (cmd_ctx->lsm->pid > 0) {
847 /* Connect to app using ustctl API */
848 cmd_ctx->ust_sock = ust_connect_app(cmd_ctx->lsm->pid);
849 if (cmd_ctx->ust_sock < 0) {
850 ret = LTTCOMM_NO_TRACEABLE;
851 goto error;
852 }
853 }
854
855 /* Process by command type */
856 switch (cmd_ctx->lsm->cmd_type) {
857 case LTTNG_KERNEL_ADD_CONTEXT:
858 {
859 int found = 0, no_event = 0;
860 struct ltt_kernel_channel *chan;
861 struct ltt_kernel_event *event;
862
863 /* Setup lttng message with no payload */
864 ret = setup_lttng_msg(cmd_ctx, 0);
865 if (ret < 0) {
866 goto setup_error;
867 }
868
869 /* Check if event name is given */
870 if (strlen(cmd_ctx->lsm->u.context.event_name) == 0) {
871 no_event = 1;
872 }
873
874 if (strlen(cmd_ctx->lsm->u.context.channel_name) == 0) {
875 /* Go over all channels */
876 DBG("Adding context to all channels");
877 cds_list_for_each_entry(chan,
878 &cmd_ctx->session->kernel_session->channel_list.head, list) {
879 if (no_event) {
880 ret = kernel_add_channel_context(chan,
881 &cmd_ctx->lsm->u.context.ctx);
882 if (ret < 0) {
883 continue;
884 }
885 } else {
886 event = get_kernel_event_by_name(cmd_ctx->lsm->u.context.event_name, chan);
887 if (event != NULL) {
888 ret = kernel_add_event_context(event,
889 &cmd_ctx->lsm->u.context.ctx);
890 if (ret < 0) {
891 ret = LTTCOMM_KERN_CONTEXT_FAIL;
892 goto error;
893 }
894 found = 1;
895 break;
896 }
897 }
898 }
899 } else {
900 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.context.channel_name,
901 cmd_ctx->session->kernel_session);
902 if (chan == NULL) {
903 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
904 goto error;
905 }
906
907 if (no_event) {
908 ret = kernel_add_channel_context(chan,
909 &cmd_ctx->lsm->u.context.ctx);
910 if (ret < 0) {
911 ret = LTTCOMM_KERN_CONTEXT_FAIL;
912 goto error;
913 }
914 } else {
915 event = get_kernel_event_by_name(cmd_ctx->lsm->u.context.event_name, chan);
916 if (event != NULL) {
917 ret = kernel_add_event_context(event,
918 &cmd_ctx->lsm->u.context.ctx);
919 if (ret < 0) {
920 ret = LTTCOMM_KERN_CONTEXT_FAIL;
921 goto error;
922 }
923 }
924 }
925 }
926
927 if (!found && !no_event) {
928 ret = LTTCOMM_NO_EVENT;
929 goto error;
930 }
931
932 ret = LTTCOMM_OK;
933 break;
934 }
935 case LTTNG_KERNEL_CREATE_CHANNEL:
936 {
937 /* Setup lttng message with no payload */
938 ret = setup_lttng_msg(cmd_ctx, 0);
939 if (ret < 0) {
940 goto setup_error;
941 }
942
943 /* Kernel tracer */
944 DBG("Creating kernel channel");
945
946 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
947 &cmd_ctx->lsm->u.channel.chan);
948 if (ret < 0) {
949 ret = LTTCOMM_KERN_CHAN_FAIL;
950 goto error;
951 }
952
953 ret = LTTCOMM_OK;
954 break;
955 }
956 case LTTNG_KERNEL_DISABLE_CHANNEL:
957 {
958 struct ltt_kernel_channel *chan;
959
960 /* Setup lttng message with no payload */
961 ret = setup_lttng_msg(cmd_ctx, 0);
962 if (ret < 0) {
963 goto setup_error;
964 }
965
966 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
967 cmd_ctx->session->kernel_session);
968 if (chan == NULL) {
969 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
970 goto error;
971 } else if (chan->enabled == 1) {
972 ret = kernel_disable_channel(chan);
973 if (ret < 0) {
974 if (ret != EEXIST) {
975 ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL;
976 }
977 goto error;
978 }
979 }
980
981 kernel_wait_quiescent(kernel_tracer_fd);
982 ret = LTTCOMM_OK;
983 break;
984 }
985 case LTTNG_KERNEL_DISABLE_EVENT:
986 {
987 struct ltt_kernel_channel *chan;
988 struct ltt_kernel_event *ev;
989
990 /* Setup lttng message with no payload */
991 ret = setup_lttng_msg(cmd_ctx, 0);
992 if (ret < 0) {
993 goto setup_error;
994 }
995
996 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
997 cmd_ctx->session->kernel_session);
998 if (chan == NULL) {
999 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1000 goto error;
1001 }
1002
1003 ev = get_kernel_event_by_name(cmd_ctx->lsm->u.disable.name, chan);
1004 if (ev != NULL) {
1005 DBG("Disabling kernel event %s for channel %s.",
1006 cmd_ctx->lsm->u.disable.name, cmd_ctx->lsm->u.disable.channel_name);
1007 ret = kernel_disable_event(ev);
1008 if (ret < 0) {
1009 ret = LTTCOMM_KERN_ENABLE_FAIL;
1010 goto error;
1011 }
1012 }
1013
1014 kernel_wait_quiescent(kernel_tracer_fd);
1015 ret = LTTCOMM_OK;
1016 break;
1017 }
1018 case LTTNG_KERNEL_DISABLE_ALL_EVENT:
1019 {
1020 struct ltt_kernel_channel *chan;
1021 struct ltt_kernel_event *ev;
1022
1023 /* Setup lttng message with no payload */
1024 ret = setup_lttng_msg(cmd_ctx, 0);
1025 if (ret < 0) {
1026 goto setup_error;
1027 }
1028
1029 DBG("Disabling all enabled kernel events");
1030
1031 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1032 cmd_ctx->session->kernel_session);
1033 if (chan == NULL) {
1034 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1035 goto error;
1036 }
1037
1038 /* For each event in the kernel session */
1039 cds_list_for_each_entry(ev, &chan->events_list.head, list) {
1040 DBG("Disabling kernel event %s for channel %s.",
1041 ev->event->name, cmd_ctx->lsm->u.disable.channel_name);
1042 ret = kernel_disable_event(ev);
1043 if (ret < 0) {
1044 continue;
1045 }
1046 }
1047
1048 /* Quiescent wait after event disable */
1049 kernel_wait_quiescent(kernel_tracer_fd);
1050 ret = LTTCOMM_OK;
1051 break;
1052 }
1053 case LTTNG_KERNEL_ENABLE_CHANNEL:
1054 {
1055 struct ltt_kernel_channel *chan;
1056
1057 /* Setup lttng message with no payload */
1058 ret = setup_lttng_msg(cmd_ctx, 0);
1059 if (ret < 0) {
1060 goto setup_error;
1061 }
1062
1063 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1064 cmd_ctx->session->kernel_session);
1065 if (chan == NULL) {
1066 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1067 goto error;
1068 } else if (chan->enabled == 0) {
1069 ret = kernel_enable_channel(chan);
1070 if (ret < 0) {
1071 if (ret != EEXIST) {
1072 ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL;
1073 }
1074 goto error;
1075 }
1076 }
1077
1078 kernel_wait_quiescent(kernel_tracer_fd);
1079 ret = LTTCOMM_OK;
1080 break;
1081 }
1082 case LTTNG_KERNEL_ENABLE_EVENT:
1083 {
1084 struct ltt_kernel_channel *chan;
1085 struct ltt_kernel_event *ev;
1086
1087 /* Setup lttng message with no payload */
1088 ret = setup_lttng_msg(cmd_ctx, 0);
1089 if (ret < 0) {
1090 goto setup_error;
1091 }
1092
1093 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1094 cmd_ctx->session->kernel_session);
1095 if (chan == NULL) {
1096 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1097 goto error;
1098 }
1099
1100 ev = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, chan);
1101 if (ev == NULL) {
1102 DBG("Creating kernel event %s for channel %s.",
1103 cmd_ctx->lsm->u.enable.event.name, chan->channel->name);
1104 ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, chan);
1105 } else {
1106 DBG("Enabling kernel event %s for channel %s.",
1107 cmd_ctx->lsm->u.enable.event.name, chan->channel->name);
1108 ret = kernel_enable_event(ev);
1109 }
1110
1111 if (ret < 0) {
1112 ret = LTTCOMM_KERN_ENABLE_FAIL;
1113 goto error;
1114 }
1115
1116 kernel_wait_quiescent(kernel_tracer_fd);
1117 ret = LTTCOMM_OK;
1118 break;
1119 }
1120 case LTTNG_KERNEL_ENABLE_ALL_EVENT:
1121 {
1122 int pos, size;
1123 char *event_list, *event, *ptr;
1124 struct ltt_kernel_channel *chan;
1125 struct ltt_kernel_event *ev;
1126 struct lttng_event ev_attr;
1127
1128 /* Setup lttng message with no payload */
1129 ret = setup_lttng_msg(cmd_ctx, 0);
1130 if (ret < 0) {
1131 goto setup_error;
1132 }
1133
1134 DBG("Enabling all kernel event");
1135
1136 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1137 cmd_ctx->session->kernel_session);
1138 if (chan == NULL) {
1139 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1140 goto error;
1141 }
1142
1143 /* For each event in the kernel session */
1144 cds_list_for_each_entry(ev, &chan->events_list.head, list) {
1145 DBG("Enabling kernel event %s for channel %s.",
1146 ev->event->name, chan->channel->name);
1147 ret = kernel_enable_event(ev);
1148 if (ret < 0) {
1149 continue;
1150 }
1151 }
1152
1153 size = kernel_list_events(kernel_tracer_fd, &event_list);
1154 if (size < 0) {
1155 ret = LTTCOMM_KERN_LIST_FAIL;
1156 goto error;
1157 }
1158
1159 ptr = event_list;
1160 while ((size = sscanf(ptr, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
1161 ev = get_kernel_event_by_name(event, chan);
1162 if (ev == NULL) {
1163 strncpy(ev_attr.name, event, LTTNG_SYM_NAME_LEN);
1164 /* Default event type for enable all */
1165 ev_attr.type = LTTNG_EVENT_TRACEPOINTS;
1166 /* Enable each single tracepoint event */
1167 ret = kernel_create_event(&ev_attr, chan);
1168 if (ret < 0) {
1169 /* Ignore error here and continue */
1170 }
1171 }
1172
1173 /* Move pointer to the next line */
1174 ptr += pos + 1;
1175 free(event);
1176 }
1177
1178 free(event_list);
1179
1180 /* Quiescent wait after event enable */
1181 kernel_wait_quiescent(kernel_tracer_fd);
1182 ret = LTTCOMM_OK;
1183 break;
1184 }
1185 case LTTNG_KERNEL_LIST_EVENTS:
1186 {
1187 char *event_list;
1188 ssize_t size = 0;
1189
1190 DBG("Listing kernel events");
1191
1192 size = kernel_list_events(kernel_tracer_fd, &event_list);
1193 if (size < 0) {
1194 ret = LTTCOMM_KERN_LIST_FAIL;
1195 goto error;
1196 }
1197
1198 /*
1199 * Setup lttng message with payload size set to the event list size in
1200 * bytes and then copy list into the llm payload.
1201 */
1202 ret = setup_lttng_msg(cmd_ctx, size);
1203 if (ret < 0) {
1204 goto setup_error;
1205 }
1206
1207 /* Copy event list into message payload */
1208 memcpy(cmd_ctx->llm->payload, event_list, size);
1209
1210 free(event_list);
1211
1212 ret = LTTCOMM_OK;
1213 break;
1214 }
1215 case LTTNG_START_TRACE:
1216 {
1217 struct ltt_kernel_channel *chan;
1218
1219 /* Setup lttng message with no payload */
1220 ret = setup_lttng_msg(cmd_ctx, 0);
1221 if (ret < 0) {
1222 goto setup_error;
1223 }
1224
1225 /* Kernel tracing */
1226 if (cmd_ctx->session->kernel_session != NULL) {
1227 if (cmd_ctx->session->kernel_session->metadata == NULL) {
1228 DBG("Open kernel metadata");
1229 ret = kernel_open_metadata(cmd_ctx->session->kernel_session);
1230 if (ret < 0) {
1231 ret = LTTCOMM_KERN_META_FAIL;
1232 goto error;
1233 }
1234 }
1235
1236 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1237 DBG("Opening kernel metadata stream");
1238 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1239 ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session);
1240 if (ret < 0) {
1241 ERR("Kernel create metadata stream failed");
1242 ret = LTTCOMM_KERN_STREAM_FAIL;
1243 goto error;
1244 }
1245 }
1246 }
1247
1248 /* For each channel */
1249 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
1250 if (chan->stream_count == 0) {
1251 ret = kernel_open_channel_stream(chan);
1252 if (ret < 0) {
1253 ERR("Kernel create channel stream failed");
1254 ret = LTTCOMM_KERN_STREAM_FAIL;
1255 goto error;
1256 }
1257 /* Update the stream global counter */
1258 cmd_ctx->session->kernel_session->stream_count_global += ret;
1259 }
1260 }
1261
1262 DBG("Start kernel tracing");
1263 ret = kernel_start_session(cmd_ctx->session->kernel_session);
1264 if (ret < 0) {
1265 ERR("Kernel start session failed");
1266 ret = LTTCOMM_KERN_START_FAIL;
1267 goto error;
1268 }
1269
1270 ret = start_kernel_trace(cmd_ctx->session->kernel_session);
1271 if (ret < 0) {
1272 ret = LTTCOMM_KERN_START_FAIL;
1273 goto error;
1274 }
1275
1276 /* Quiescent wait after starting trace */
1277 kernel_wait_quiescent(kernel_tracer_fd);
1278 }
1279
1280 /* TODO: Start all UST traces */
1281
1282 ret = LTTCOMM_OK;
1283 break;
1284 }
1285 case LTTNG_STOP_TRACE:
1286 {
1287 struct ltt_kernel_channel *chan;
1288 /* Setup lttng message with no payload */
1289 ret = setup_lttng_msg(cmd_ctx, 0);
1290 if (ret < 0) {
1291 goto setup_error;
1292 }
1293
1294 /* Kernel tracer */
1295 if (cmd_ctx->session->kernel_session != NULL) {
1296 DBG("Stop kernel tracing");
1297
1298 ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd);
1299 if (ret < 0) {
1300 ERR("Kernel metadata flush failed");
1301 }
1302
1303 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
1304 ret = kernel_flush_buffer(chan);
1305 if (ret < 0) {
1306 ERR("Kernel flush buffer error");
1307 }
1308 }
1309
1310 ret = kernel_stop_session(cmd_ctx->session->kernel_session);
1311 if (ret < 0) {
1312 ERR("Kernel stop session failed");
1313 ret = LTTCOMM_KERN_STOP_FAIL;
1314 goto error;
1315 }
1316
1317 /* Quiescent wait after stopping trace */
1318 kernel_wait_quiescent(kernel_tracer_fd);
1319 }
1320
1321 /* TODO : User-space tracer */
1322
1323 ret = LTTCOMM_OK;
1324 break;
1325 }
1326 case LTTNG_CREATE_SESSION:
1327 {
1328 /* Setup lttng message with no payload */
1329 ret = setup_lttng_msg(cmd_ctx, 0);
1330 if (ret < 0) {
1331 goto setup_error;
1332 }
1333
1334 ret = create_session(cmd_ctx->lsm->session_name, cmd_ctx->lsm->path);
1335 if (ret < 0) {
1336 if (ret == -EEXIST) {
1337 ret = LTTCOMM_EXIST_SESS;
1338 } else {
1339 ret = LTTCOMM_FATAL;
1340 }
1341 goto error;
1342 }
1343
1344 ret = LTTCOMM_OK;
1345 break;
1346 }
1347 case LTTNG_DESTROY_SESSION:
1348 {
1349 /* Setup lttng message with no payload */
1350 ret = setup_lttng_msg(cmd_ctx, 0);
1351 if (ret < 0) {
1352 goto setup_error;
1353 }
1354
1355 /* Clean kernel session teardown */
1356 teardown_kernel_session(cmd_ctx->session);
1357
1358 ret = destroy_session(cmd_ctx->lsm->session_name);
1359 if (ret < 0) {
1360 ret = LTTCOMM_FATAL;
1361 goto error;
1362 }
1363
1364 ret = LTTCOMM_OK;
1365 break;
1366 }
1367 /*
1368 case LTTNG_LIST_TRACES:
1369 {
1370 unsigned int trace_count;
1371
1372 trace_count = get_trace_count_per_session(cmd_ctx->session);
1373 if (trace_count == 0) {
1374 ret = LTTCOMM_NO_TRACE;
1375 goto error;
1376 }
1377
1378 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1379 if (ret < 0) {
1380 goto setup_error;
1381 }
1382
1383 get_traces_per_session(cmd_ctx->session,
1384 (struct lttng_trace *)(cmd_ctx->llm->payload));
1385
1386 ret = LTTCOMM_OK;
1387 break;
1388 }
1389 */
1390 /*
1391 case UST_CREATE_TRACE:
1392 {
1393 ret = setup_lttng_msg(cmd_ctx, 0);
1394 if (ret < 0) {
1395 goto setup_error;
1396 }
1397
1398 ret = ust_create_trace(cmd_ctx);
1399 if (ret < 0) {
1400 goto error;
1401 }
1402 break;
1403 }
1404 */
1405 case LTTNG_LIST_TRACEABLE_APPS:
1406 {
1407 unsigned int app_count;
1408
1409 app_count = get_app_count();
1410 DBG("Traceable application count : %d", app_count);
1411 if (app_count == 0) {
1412 ret = LTTCOMM_NO_APPS;
1413 goto error;
1414 }
1415
1416 ret = setup_lttng_msg(cmd_ctx, sizeof(pid_t) * app_count);
1417 if (ret < 0) {
1418 goto setup_error;
1419 }
1420
1421 get_app_list_pids((pid_t *)(cmd_ctx->llm->payload));
1422
1423 ret = LTTCOMM_OK;
1424 break;
1425 }
1426 /*
1427 case UST_START_TRACE:
1428 {
1429 ret = setup_lttng_msg(cmd_ctx, 0);
1430 if (ret < 0) {
1431 goto setup_error;
1432 }
1433
1434 ret = ust_start_trace(cmd_ctx);
1435 if (ret < 0) {
1436 goto setup_error;
1437 }
1438 break;
1439 }
1440 case UST_STOP_TRACE:
1441 {
1442 ret = setup_lttng_msg(cmd_ctx, 0);
1443 if (ret < 0) {
1444 goto setup_error;
1445 }
1446
1447 ret = ust_stop_trace(cmd_ctx);
1448 if (ret < 0) {
1449 goto setup_error;
1450 }
1451 break;
1452 }
1453 */
1454 case LTTNG_LIST_SESSIONS:
1455 {
1456 unsigned int session_count;
1457
1458 session_count = get_session_count();
1459 if (session_count == 0) {
1460 ret = LTTCOMM_NO_SESSION;
1461 goto error;
1462 }
1463
1464 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * session_count);
1465 if (ret < 0) {
1466 goto setup_error;
1467 }
1468
1469 get_lttng_session((struct lttng_session *)(cmd_ctx->llm->payload));
1470
1471 ret = LTTCOMM_OK;
1472 break;
1473 }
1474 default:
1475 /* Undefined command */
1476 ret = setup_lttng_msg(cmd_ctx, 0);
1477 if (ret < 0) {
1478 goto setup_error;
1479 }
1480
1481 ret = LTTCOMM_UND;
1482 break;
1483 }
1484
1485 /* Set return code */
1486 cmd_ctx->llm->ret_code = ret;
1487
1488 return ret;
1489
1490 error:
1491 if (cmd_ctx->llm == NULL) {
1492 DBG("Missing llm structure. Allocating one.");
1493 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
1494 goto setup_error;
1495 }
1496 }
1497 /* Notify client of error */
1498 cmd_ctx->llm->ret_code = ret;
1499
1500 setup_error:
1501 return ret;
1502 }
1503
1504 /*
1505 * thread_manage_clients
1506 *
1507 * This thread manage all clients request using the unix
1508 * client socket for communication.
1509 */
1510 static void *thread_manage_clients(void *data)
1511 {
1512 int sock, ret;
1513 struct command_ctx *cmd_ctx;
1514
1515 DBG("[thread] Manage client started");
1516
1517 ret = lttcomm_listen_unix_sock(client_sock);
1518 if (ret < 0) {
1519 goto error;
1520 }
1521
1522 /* Notify parent pid that we are ready
1523 * to accept command for client side.
1524 */
1525 if (opt_sig_parent) {
1526 kill(ppid, SIGCHLD);
1527 }
1528
1529 while (1) {
1530 /* Blocking call, waiting for transmission */
1531 DBG("Accepting client command ...");
1532 sock = lttcomm_accept_unix_sock(client_sock);
1533 if (sock < 0) {
1534 goto error;
1535 }
1536
1537 /* Allocate context command to process the client request */
1538 cmd_ctx = malloc(sizeof(struct command_ctx));
1539
1540 /* Allocate data buffer for reception */
1541 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
1542 cmd_ctx->llm = NULL;
1543 cmd_ctx->session = NULL;
1544
1545 /*
1546 * Data is received from the lttng client. The struct
1547 * lttcomm_session_msg (lsm) contains the command and data request of
1548 * the client.
1549 */
1550 DBG("Receiving data from client ...");
1551 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
1552 if (ret <= 0) {
1553 continue;
1554 }
1555
1556 // TODO: Validate cmd_ctx including sanity check for security purpose.
1557
1558 /*
1559 * This function dispatch the work to the kernel or userspace tracer
1560 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1561 * informations for the client. The command context struct contains
1562 * everything this function may needs.
1563 */
1564 ret = process_client_msg(cmd_ctx);
1565 if (ret < 0) {
1566 /* TODO: Inform client somehow of the fatal error. At this point,
1567 * ret < 0 means that a malloc failed (ENOMEM). */
1568 /* Error detected but still accept command */
1569 clean_command_ctx(cmd_ctx);
1570 continue;
1571 }
1572
1573 DBG("Sending response (size: %d, retcode: %d)",
1574 cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code);
1575 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
1576 if (ret < 0) {
1577 ERR("Failed to send data back to client");
1578 }
1579
1580 clean_command_ctx(cmd_ctx);
1581
1582 /* End of transmission */
1583 close(sock);
1584 }
1585
1586 error:
1587 return NULL;
1588 }
1589
1590
1591 /*
1592 * usage function on stderr
1593 */
1594 static void usage(void)
1595 {
1596 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
1597 fprintf(stderr, " -h, --help Display this usage.\n");
1598 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
1599 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1600 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1601 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1602 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
1603 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1604 fprintf(stderr, " -V, --version Show version number.\n");
1605 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1606 fprintf(stderr, " -q, --quiet No output at all.\n");
1607 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1608 }
1609
1610 /*
1611 * daemon argument parsing
1612 */
1613 static int parse_args(int argc, char **argv)
1614 {
1615 int c;
1616
1617 static struct option long_options[] = {
1618 { "client-sock", 1, 0, 'c' },
1619 { "apps-sock", 1, 0, 'a' },
1620 { "kconsumerd-cmd-sock", 1, 0, 0 },
1621 { "kconsumerd-err-sock", 1, 0, 0 },
1622 { "daemonize", 0, 0, 'd' },
1623 { "sig-parent", 0, 0, 'S' },
1624 { "help", 0, 0, 'h' },
1625 { "group", 1, 0, 'g' },
1626 { "version", 0, 0, 'V' },
1627 { "quiet", 0, 0, 'q' },
1628 { "verbose", 0, 0, 'v' },
1629 { NULL, 0, 0, 0 }
1630 };
1631
1632 while (1) {
1633 int option_index = 0;
1634 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:", long_options, &option_index);
1635 if (c == -1) {
1636 break;
1637 }
1638
1639 switch (c) {
1640 case 0:
1641 fprintf(stderr, "option %s", long_options[option_index].name);
1642 if (optarg) {
1643 fprintf(stderr, " with arg %s\n", optarg);
1644 }
1645 break;
1646 case 'c':
1647 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
1648 break;
1649 case 'a':
1650 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
1651 break;
1652 case 'd':
1653 opt_daemon = 1;
1654 break;
1655 case 'g':
1656 opt_tracing_group = strdup(optarg);
1657 break;
1658 case 'h':
1659 usage();
1660 exit(EXIT_FAILURE);
1661 case 'V':
1662 fprintf(stdout, "%s\n", VERSION);
1663 exit(EXIT_SUCCESS);
1664 case 'S':
1665 opt_sig_parent = 1;
1666 break;
1667 case 'E':
1668 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
1669 break;
1670 case 'C':
1671 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
1672 break;
1673 case 'q':
1674 opt_quiet = 1;
1675 break;
1676 case 'v':
1677 opt_verbose = 1;
1678 break;
1679 default:
1680 /* Unknown option or other error.
1681 * Error is printed by getopt, just return */
1682 return -1;
1683 }
1684 }
1685
1686 return 0;
1687 }
1688
1689 /*
1690 * init_daemon_socket
1691 *
1692 * Creates the two needed socket by the daemon.
1693 * apps_sock - The communication socket for all UST apps.
1694 * client_sock - The communication of the cli tool (lttng).
1695 */
1696 static int init_daemon_socket()
1697 {
1698 int ret = 0;
1699 mode_t old_umask;
1700
1701 old_umask = umask(0);
1702
1703 /* Create client tool unix socket */
1704 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
1705 if (client_sock < 0) {
1706 ERR("Create unix sock failed: %s", client_unix_sock_path);
1707 ret = -1;
1708 goto end;
1709 }
1710
1711 /* File permission MUST be 660 */
1712 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1713 if (ret < 0) {
1714 ERR("Set file permissions failed: %s", client_unix_sock_path);
1715 perror("chmod");
1716 goto end;
1717 }
1718
1719 /* Create the application unix socket */
1720 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
1721 if (apps_sock < 0) {
1722 ERR("Create unix sock failed: %s", apps_unix_sock_path);
1723 ret = -1;
1724 goto end;
1725 }
1726
1727 /* File permission MUST be 666 */
1728 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
1729 if (ret < 0) {
1730 ERR("Set file permissions failed: %s", apps_unix_sock_path);
1731 perror("chmod");
1732 goto end;
1733 }
1734
1735 end:
1736 umask(old_umask);
1737 return ret;
1738 }
1739
1740 /*
1741 * check_existing_daemon
1742 *
1743 * Check if the global socket is available.
1744 * If yes, error is returned.
1745 */
1746 static int check_existing_daemon()
1747 {
1748 int ret;
1749
1750 ret = access(client_unix_sock_path, F_OK);
1751 if (ret == 0) {
1752 ret = access(apps_unix_sock_path, F_OK);
1753 }
1754
1755 return ret;
1756 }
1757
1758 /*
1759 * get_home_dir
1760 *
1761 * Return pointer to home directory path using
1762 * the env variable HOME.
1763 *
1764 * Default : /tmp
1765 */
1766 static const char *get_home_dir(void)
1767 {
1768 const char *home_path;
1769
1770 if ((home_path = (const char *) getenv("HOME")) == NULL) {
1771 home_path = default_home_dir;
1772 }
1773
1774 return home_path;
1775 }
1776
1777 /*
1778 * set_permissions
1779 *
1780 * Set the tracing group gid onto the client socket.
1781 *
1782 * Race window between mkdir and chown is OK because we are going from
1783 * less permissive (root.root) to more permissive (root.tracing).
1784 */
1785 static int set_permissions(void)
1786 {
1787 int ret;
1788 struct group *grp;
1789
1790 /* Decide which group name to use */
1791 (opt_tracing_group != NULL) ?
1792 (grp = getgrnam(opt_tracing_group)) :
1793 (grp = getgrnam(default_tracing_group));
1794
1795 if (grp == NULL) {
1796 ERR("Missing tracing group. Aborting execution.\n");
1797 ret = -1;
1798 goto end;
1799 }
1800
1801 /* Set lttng run dir */
1802 ret = chown(LTTNG_RUNDIR, 0, grp->gr_gid);
1803 if (ret < 0) {
1804 ERR("Unable to set group on " LTTNG_RUNDIR);
1805 perror("chown");
1806 }
1807
1808 /* lttng client socket path */
1809 ret = chown(client_unix_sock_path, 0, grp->gr_gid);
1810 if (ret < 0) {
1811 ERR("Unable to set group on %s", client_unix_sock_path);
1812 perror("chown");
1813 }
1814
1815 /* kconsumerd error socket path */
1816 ret = chown(kconsumerd_err_unix_sock_path, 0, grp->gr_gid);
1817 if (ret < 0) {
1818 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
1819 perror("chown");
1820 }
1821
1822 DBG("All permissions are set");
1823
1824 end:
1825 return ret;
1826 }
1827
1828 /*
1829 * create_lttng_rundir
1830 *
1831 * Create the lttng run directory needed for all
1832 * global sockets and pipe.
1833 */
1834 static int create_lttng_rundir(void)
1835 {
1836 int ret;
1837
1838 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
1839 if (ret < 0) {
1840 if (errno != EEXIST) {
1841 ERR("Unable to create " LTTNG_RUNDIR);
1842 goto error;
1843 } else {
1844 ret = 0;
1845 }
1846 }
1847
1848 error:
1849 return ret;
1850 }
1851
1852 /*
1853 * set_kconsumerd_sockets
1854 *
1855 * Setup sockets and directory needed by the kconsumerd
1856 * communication with the session daemon.
1857 */
1858 static int set_kconsumerd_sockets(void)
1859 {
1860 int ret;
1861
1862 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
1863 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
1864 }
1865
1866 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
1867 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
1868 }
1869
1870 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
1871 if (ret < 0) {
1872 if (errno != EEXIST) {
1873 ERR("Failed to create " KCONSUMERD_PATH);
1874 goto error;
1875 }
1876 ret = 0;
1877 }
1878
1879 /* Create the kconsumerd error unix socket */
1880 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
1881 if (kconsumerd_err_sock < 0) {
1882 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
1883 ret = -1;
1884 goto error;
1885 }
1886
1887 /* File permission MUST be 660 */
1888 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1889 if (ret < 0) {
1890 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
1891 perror("chmod");
1892 goto error;
1893 }
1894
1895 error:
1896 return ret;
1897 }
1898
1899 /*
1900 * sighandler
1901 *
1902 * Signal handler for the daemon
1903 */
1904 static void sighandler(int sig)
1905 {
1906 switch (sig) {
1907 case SIGPIPE:
1908 DBG("SIGPIPE catched");
1909 return;
1910 case SIGINT:
1911 DBG("SIGINT catched");
1912 cleanup();
1913 break;
1914 case SIGTERM:
1915 DBG("SIGTERM catched");
1916 cleanup();
1917 break;
1918 default:
1919 break;
1920 }
1921
1922 exit(EXIT_SUCCESS);
1923 }
1924
1925 /*
1926 * set_signal_handler
1927 *
1928 * Setup signal handler for :
1929 * SIGINT, SIGTERM, SIGPIPE
1930 */
1931 static int set_signal_handler(void)
1932 {
1933 int ret = 0;
1934 struct sigaction sa;
1935 sigset_t sigset;
1936
1937 if ((ret = sigemptyset(&sigset)) < 0) {
1938 perror("sigemptyset");
1939 return ret;
1940 }
1941
1942 sa.sa_handler = sighandler;
1943 sa.sa_mask = sigset;
1944 sa.sa_flags = 0;
1945 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
1946 perror("sigaction");
1947 return ret;
1948 }
1949
1950 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
1951 perror("sigaction");
1952 return ret;
1953 }
1954
1955 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
1956 perror("sigaction");
1957 return ret;
1958 }
1959
1960 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1961
1962 return ret;
1963 }
1964
1965 /*
1966 * set_ulimit
1967 *
1968 * Set open files limit to unlimited. This daemon can open a large number of
1969 * file descriptors in order to consumer multiple kernel traces.
1970 */
1971 static void set_ulimit(void)
1972 {
1973 int ret;
1974 struct rlimit lim;
1975
1976 lim.rlim_cur = 65535;
1977 lim.rlim_max = 65535;
1978
1979 ret = setrlimit(RLIMIT_NOFILE, &lim);
1980 if (ret < 0) {
1981 perror("failed to set open files limit");
1982 }
1983 }
1984
1985 /*
1986 * main
1987 */
1988 int main(int argc, char **argv)
1989 {
1990 int ret = 0;
1991 void *status;
1992
1993 /* Parse arguments */
1994 progname = argv[0];
1995 if ((ret = parse_args(argc, argv) < 0)) {
1996 goto error;
1997 }
1998
1999 /* Daemonize */
2000 if (opt_daemon) {
2001 ret = daemon(0, 0);
2002 if (ret < 0) {
2003 perror("daemon");
2004 goto error;
2005 }
2006 }
2007
2008 /* Check if daemon is UID = 0 */
2009 is_root = !getuid();
2010
2011 /* Set all sockets path */
2012 if (is_root) {
2013 ret = create_lttng_rundir();
2014 if (ret < 0) {
2015 goto error;
2016 }
2017
2018 if (strlen(apps_unix_sock_path) == 0) {
2019 snprintf(apps_unix_sock_path, PATH_MAX,
2020 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
2021 }
2022
2023 if (strlen(client_unix_sock_path) == 0) {
2024 snprintf(client_unix_sock_path, PATH_MAX,
2025 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
2026 }
2027
2028 ret = set_kconsumerd_sockets();
2029 if (ret < 0) {
2030 goto error;
2031 }
2032
2033 /* Setup kernel tracer */
2034 init_kernel_tracer();
2035
2036 /* Set ulimit for open files */
2037 set_ulimit();
2038 } else {
2039 if (strlen(apps_unix_sock_path) == 0) {
2040 snprintf(apps_unix_sock_path, PATH_MAX,
2041 DEFAULT_HOME_APPS_UNIX_SOCK, get_home_dir());
2042 }
2043
2044 /* Set the cli tool unix socket path */
2045 if (strlen(client_unix_sock_path) == 0) {
2046 snprintf(client_unix_sock_path, PATH_MAX,
2047 DEFAULT_HOME_CLIENT_UNIX_SOCK, get_home_dir());
2048 }
2049 }
2050
2051 DBG("Client socket path %s", client_unix_sock_path);
2052 DBG("Application socket path %s", apps_unix_sock_path);
2053
2054 /* See if daemon already exist. If any of the two
2055 * socket needed by the daemon are present, this test fails
2056 */
2057 if ((ret = check_existing_daemon()) == 0) {
2058 ERR("Already running daemon.\n");
2059 /* We do not goto error because we must not
2060 * cleanup() because a daemon is already running.
2061 */
2062 exit(EXIT_FAILURE);
2063 }
2064
2065 if (set_signal_handler() < 0) {
2066 goto error;
2067 }
2068
2069 /* Setup the needed unix socket */
2070 if (init_daemon_socket() < 0) {
2071 goto error;
2072 }
2073
2074 /* Set credentials to socket */
2075 if (is_root && (set_permissions() < 0)) {
2076 goto error;
2077 }
2078
2079 /* Get parent pid if -S, --sig-parent is specified. */
2080 if (opt_sig_parent) {
2081 ppid = getppid();
2082 }
2083
2084 while (1) {
2085 /* Create thread to manage the client socket */
2086 ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL);
2087 if (ret != 0) {
2088 perror("pthread_create");
2089 goto error;
2090 }
2091
2092 /* Create thread to manage application socket */
2093 ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL);
2094 if (ret != 0) {
2095 perror("pthread_create");
2096 goto error;
2097 }
2098
2099 ret = pthread_join(client_thread, &status);
2100 if (ret != 0) {
2101 perror("pthread_join");
2102 goto error;
2103 }
2104 }
2105
2106 cleanup();
2107 exit(EXIT_SUCCESS);
2108
2109 error:
2110 cleanup();
2111 exit(EXIT_FAILURE);
2112 }
This page took 0.10581 seconds and 3 git commands to generate.