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