Commit | Line | Data |
---|---|---|
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) */ | |
38 | ||
39 | #include "liblttsessiondcomm.h" | |
40 | #include "ltt-sessiond.h" | |
41 | ||
686204ab MD |
42 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
43 | const char default_tracing_group[] = DEFAULT_TRACING_GROUP; | |
44 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; | |
45 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; | |
46 | ||
fac6795d DG |
47 | /* Static functions */ |
48 | static int set_signal_handler(void); | |
49 | static int set_socket_perms(void); | |
50 | static void sighandler(int); | |
51 | static void daemonize(void); | |
52 | static void cleanup(void); | |
e065084a | 53 | static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm); |
fac6795d DG |
54 | static int check_existing_daemon(void); |
55 | static int notify_apps(const char*); | |
56 | static int connect_app(pid_t); | |
57 | static int init_daemon_socket(void); | |
e065084a DG |
58 | static int process_client_msg(int sock, struct lttcomm_session_msg*); |
59 | static int send_unix_sock(int sock, void *buf, size_t len); | |
fac6795d DG |
60 | |
61 | static void *thread_manage_clients(void *); | |
62 | static void *thread_manage_apps(void *); | |
63 | ||
64 | static int create_session(const char*, uuid_t *); | |
65 | static void destroy_session(uuid_t); | |
66 | ||
67 | static struct ltt_session *find_session(uuid_t); | |
68 | ||
69 | /* Variables */ | |
70 | const char *progname; | |
71 | const char *opt_tracing_group; | |
72 | static int opt_daemon; | |
73 | static int is_root; /* Set to 1 if the daemon is running as root */ | |
74 | ||
75 | static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */ | |
76 | static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */ | |
77 | ||
78 | static int client_socket; | |
79 | static int apps_socket; | |
80 | ||
81 | static struct ltt_session *current_session; | |
e065084a DG |
82 | |
83 | /* Number of element for the list below. */ | |
84 | static unsigned int session_count; | |
85 | static unsigned int traceable_app_count; | |
fac6795d DG |
86 | |
87 | /* Init session's list */ | |
88 | static struct ltt_session_list ltt_session_list = { | |
89 | .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), | |
90 | }; | |
91 | ||
e065084a | 92 | /* Init ust traceabl application's list */ |
fac6795d DG |
93 | static struct ltt_traceable_app_list ltt_traceable_app_list = { |
94 | .head = CDS_LIST_HEAD_INIT(ltt_traceable_app_list.head), | |
95 | }; | |
96 | ||
97 | /* | |
98 | * thread_manage_apps | |
99 | * | |
100 | * This thread manage the application socket communication | |
101 | */ | |
102 | static void *thread_manage_apps(void *data) | |
103 | { | |
104 | int sock, ret; | |
105 | struct ltt_traceable_app *lta; | |
106 | ||
107 | /* TODO: Something more elegant is needed but fine for now */ | |
686204ab | 108 | struct { |
fac6795d | 109 | int reg; /* 1:register, 0:unregister */ |
686204ab MD |
110 | pid_t pid; |
111 | uid_t uid; | |
112 | } reg_msg; | |
fac6795d DG |
113 | |
114 | /* Notify all applications to register */ | |
115 | notify_apps(default_global_apps_pipe); | |
116 | ||
117 | ret = lttcomm_listen_unix_sock(apps_socket); | |
118 | if (ret < 0) { | |
119 | goto error; | |
120 | } | |
121 | ||
122 | while (1) { | |
123 | /* Blocking call, waiting for transmission */ | |
124 | sock = lttcomm_accept_unix_sock(apps_socket); | |
125 | if (sock < 0) { | |
126 | goto error; | |
127 | } | |
128 | ||
129 | /* Basic recv here to handle the very simple data | |
130 | * that the libust send to register (reg_msg). | |
131 | */ | |
132 | ret = recv(sock, ®_msg, sizeof(reg_msg), 0); | |
133 | if (ret < 0) { | |
134 | perror("recv"); | |
135 | continue; | |
136 | } | |
137 | ||
138 | /* Add application to the global traceable list */ | |
139 | if (reg_msg.reg == 1) { | |
140 | /* Registering */ | |
141 | lta = malloc(sizeof(struct ltt_traceable_app)); | |
142 | lta->pid = reg_msg.pid; | |
143 | lta->uid = reg_msg.uid; | |
144 | cds_list_add(<a->list, <t_traceable_app_list.head); | |
e065084a | 145 | traceable_app_count++; |
fac6795d DG |
146 | } else { |
147 | /* Unregistering */ | |
148 | lta = NULL; | |
149 | cds_list_for_each_entry(lta, <t_traceable_app_list.head, list) { | |
150 | if (lta->pid == reg_msg.pid && lta->uid == reg_msg.uid) { | |
151 | cds_list_del(<a->list); | |
e065084a DG |
152 | /* Check to not overflow here */ |
153 | if (traceable_app_count != 0) { | |
154 | traceable_app_count--; | |
155 | } | |
fac6795d DG |
156 | break; |
157 | } | |
158 | } | |
159 | ||
160 | /* If an item was found, free it from memory */ | |
161 | if (lta) { | |
162 | free(lta); | |
163 | } | |
164 | } | |
165 | } | |
166 | ||
167 | error: | |
168 | ||
169 | return NULL; | |
170 | } | |
171 | ||
172 | /* | |
173 | * thread_manage_clients | |
174 | * | |
175 | * This thread manage all clients request using the unix | |
176 | * client socket for communication. | |
177 | */ | |
178 | static void *thread_manage_clients(void *data) | |
179 | { | |
180 | int sock, ret; | |
181 | struct lttcomm_session_msg lsm; | |
fac6795d DG |
182 | |
183 | ret = lttcomm_listen_unix_sock(client_socket); | |
184 | if (ret < 0) { | |
185 | goto error; | |
186 | } | |
187 | ||
188 | while (1) { | |
189 | /* Blocking call, waiting for transmission */ | |
190 | sock = lttcomm_accept_unix_sock(client_socket); | |
191 | if (sock < 0) { | |
192 | goto error; | |
193 | } | |
194 | ||
195 | /* | |
196 | * Data is received from the lttng client. The struct | |
197 | * lttcomm_session_msg (lsm) contains the command and data | |
198 | * request of the client. | |
199 | */ | |
200 | ret = lttcomm_recv_unix_sock(sock, &lsm, sizeof(lsm)); | |
201 | if (ret < 0) { | |
202 | continue; | |
203 | } | |
204 | ||
205 | /* This function dispatch the work to the LTTng or UST libs | |
e065084a DG |
206 | * and then sends back the response to the client. This is needed |
207 | * because there might be more then one lttcomm_lttng_msg to | |
208 | * send out so process_client_msg do both jobs. | |
fac6795d | 209 | */ |
e065084a DG |
210 | ret = process_client_msg(sock, &lsm); |
211 | if (ret < 0) { | |
212 | /* Error detected but still accept command */ | |
213 | continue; | |
fac6795d DG |
214 | } |
215 | } | |
216 | ||
217 | error: | |
218 | return NULL; | |
219 | } | |
220 | ||
e065084a DG |
221 | /* |
222 | * send_unix_sock | |
223 | * | |
224 | * Send data on a unix socket using the liblttsessiondcomm API. | |
225 | * | |
226 | * Return lttcomm error code. | |
227 | */ | |
228 | static int send_unix_sock(int sock, void *buf, size_t len) | |
229 | { | |
230 | /* Check valid length */ | |
231 | if (len <= 0) { | |
232 | return -1; | |
233 | } | |
234 | ||
235 | return lttcomm_send_unix_sock(sock, buf, len); | |
236 | } | |
237 | ||
fac6795d DG |
238 | /* |
239 | * connect_app | |
240 | * | |
241 | * Return a socket connected to the libust communication socket | |
242 | * of the application identified by the pid. | |
243 | */ | |
244 | static int connect_app(pid_t pid) | |
245 | { | |
246 | int sock; | |
247 | ||
248 | sock = ustctl_connect_pid(pid); | |
249 | if (sock < 0) { | |
250 | fprintf(stderr, "Fail connecting to the PID %d\n", pid); | |
251 | } | |
252 | ||
253 | return sock; | |
254 | } | |
255 | ||
256 | /* | |
257 | * notify_apps | |
258 | * | |
259 | * Notify apps by writing 42 to a named pipe using name. | |
260 | * Every applications waiting for a ltt-sessiond will be notified | |
261 | * and re-register automatically to the session daemon. | |
262 | * | |
263 | * Return open or write error value. | |
264 | */ | |
265 | static int notify_apps(const char *name) | |
266 | { | |
267 | int fd; | |
268 | int ret = -1; | |
269 | ||
270 | /* Try opening the global pipe */ | |
271 | fd = open(name, O_WRONLY); | |
272 | if (fd < 0) { | |
273 | goto error; | |
274 | } | |
275 | ||
276 | /* Notify by writing on the pipe */ | |
277 | ret = write(fd, "42", 2); | |
278 | if (ret < 0) { | |
279 | perror("write"); | |
280 | } | |
281 | ||
282 | error: | |
283 | return ret; | |
284 | } | |
285 | ||
286 | /* | |
287 | * find_session | |
288 | * | |
289 | * Return a ltt_session structure ptr that matches the uuid. | |
290 | */ | |
291 | static struct ltt_session *find_session(uuid_t session_id) | |
292 | { | |
293 | struct ltt_session *iter = NULL; | |
294 | ||
295 | /* Sanity check for NULL session_id */ | |
296 | if (uuid_is_null(session_id)) { | |
297 | goto end; | |
298 | } | |
299 | ||
300 | cds_list_for_each_entry(iter, <t_session_list.head, list) { | |
301 | if (uuid_compare(iter->uuid, session_id)) { | |
302 | break; | |
303 | } | |
304 | } | |
305 | ||
306 | end: | |
307 | return iter; | |
308 | } | |
309 | ||
310 | /* | |
311 | * destroy_session | |
312 | * | |
313 | * Delete session from the global session list | |
314 | * and free the memory. | |
315 | */ | |
316 | static void destroy_session(uuid_t session_id) | |
317 | { | |
318 | struct ltt_session *iter = NULL; | |
319 | ||
320 | cds_list_for_each_entry(iter, <t_session_list.head, list) { | |
321 | if (uuid_compare(iter->uuid, session_id)) { | |
322 | cds_list_del(&iter->list); | |
323 | break; | |
324 | } | |
325 | } | |
326 | ||
327 | if (iter) { | |
328 | free(iter); | |
329 | session_count--; | |
330 | } | |
331 | } | |
332 | ||
333 | /* | |
334 | * create_session | |
335 | * | |
336 | * Create a brand new session, | |
337 | */ | |
338 | static int create_session(const char *name, uuid_t *session_id) | |
339 | { | |
340 | struct ltt_session *new_session; | |
341 | ||
342 | /* Allocate session data structure */ | |
343 | new_session = malloc(sizeof(struct ltt_session)); | |
344 | if (new_session == NULL) { | |
345 | perror("malloc"); | |
346 | goto error; | |
347 | } | |
348 | ||
349 | if (name != NULL) { | |
350 | if (asprintf(&new_session->name, "%s", name) < 0) { | |
351 | goto error; | |
352 | } | |
353 | } else { | |
354 | /* Generate session name based on the session count */ | |
355 | if (asprintf(&new_session->name, "%s%d", "auto", session_count) < 0) { | |
356 | goto error; | |
357 | } | |
358 | } | |
359 | ||
360 | /* UUID generation */ | |
361 | uuid_generate(new_session->uuid); | |
362 | uuid_copy(*session_id, new_session->uuid); | |
363 | ||
364 | /* Set consumer (identifier) to 0. This means that there is | |
365 | * NO consumer attach to that session yet. | |
366 | */ | |
367 | new_session->ust_consumer = 0; | |
368 | new_session->lttng_consumer = 0; | |
369 | ||
370 | /* Init list */ | |
371 | CDS_INIT_LIST_HEAD(&new_session->ust_traces); | |
372 | CDS_INIT_LIST_HEAD(&new_session->lttng_traces); | |
373 | ||
374 | /* Add new session to the global session list */ | |
375 | cds_list_add(&new_session->list, <t_session_list.head); | |
376 | ||
377 | session_count++; | |
378 | ||
379 | return 0; | |
380 | ||
381 | error: | |
382 | return -1; | |
383 | } | |
384 | ||
385 | /* | |
386 | * ust_list_apps | |
387 | * | |
388 | * List traceable user-space application and fill an | |
389 | * array of pids. | |
390 | * | |
391 | * Return size of the array. | |
392 | */ | |
e065084a | 393 | static size_t ust_list_apps(pid_t **pids) |
fac6795d DG |
394 | { |
395 | size_t size = 0; | |
396 | struct ltt_traceable_app *iter = NULL; | |
e065084a | 397 | pid_t *p; |
fac6795d | 398 | |
e065084a DG |
399 | if (traceable_app_count == 0) { |
400 | /* No dynamic allocation is done */ | |
401 | goto end; | |
402 | } | |
403 | ||
404 | p = malloc(sizeof(pid_t) * traceable_app_count); | |
fac6795d | 405 | |
e065084a DG |
406 | /* TODO: Mutex needed to access this list */ |
407 | cds_list_for_each_entry(iter, <t_traceable_app_list.head, list) { | |
408 | p[size] = iter->pid; | |
fac6795d DG |
409 | size++; |
410 | } | |
411 | ||
e065084a DG |
412 | *pids = p; |
413 | ||
414 | end: | |
fac6795d DG |
415 | return size; |
416 | } | |
417 | ||
e065084a DG |
418 | /* |
419 | * copy_common_data | |
420 | * | |
421 | * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg | |
422 | */ | |
423 | static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm) | |
424 | { | |
425 | llm->cmd_type = lsm->cmd_type; | |
426 | llm->pid = lsm->pid; | |
427 | if (!uuid_is_null(lsm->session_id)) { | |
428 | uuid_copy(llm->session_id, lsm->session_id); | |
429 | } | |
430 | strncpy(llm->trace_name, lsm->trace_name, sizeof(llm->trace_name)); | |
431 | } | |
432 | ||
fac6795d DG |
433 | /* |
434 | * process_client_msg | |
435 | * | |
436 | * This takes the lttcomm_session_msg struct and process the command requested | |
e065084a DG |
437 | * by the client. It then creates response(s) and send it back to the |
438 | * given socket (sock). | |
fac6795d | 439 | * |
e065084a | 440 | * Return any error encountered or 0 for success. |
fac6795d | 441 | */ |
e065084a | 442 | static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) |
fac6795d | 443 | { |
e065084a DG |
444 | int ret; |
445 | struct lttcomm_lttng_msg llm; | |
fac6795d DG |
446 | |
447 | /* Copy common data to identify the response | |
448 | * on the lttng client side. | |
449 | */ | |
e065084a | 450 | copy_common_data(&llm, lsm); |
fac6795d DG |
451 | |
452 | /* Default return code. | |
e065084a | 453 | * In our world, everything is OK... right? ;) |
fac6795d | 454 | */ |
e065084a | 455 | llm.ret_code = LTTCOMM_OK; |
fac6795d DG |
456 | |
457 | /* Process by command type */ | |
458 | switch (lsm->cmd_type) { | |
459 | case UST_LIST_APPS: | |
460 | { | |
e065084a DG |
461 | pid_t *pids; |
462 | llm.num_pckt = ust_list_apps(&pids); | |
463 | if (llm.num_pckt == 0) { | |
464 | ret = LTTCOMM_NO_APPS; | |
465 | goto error; | |
466 | } | |
467 | ||
468 | /* Send all packets */ | |
469 | while (llm.num_pckt != 0) { | |
470 | llm.u.list_apps.pid = pids[traceable_app_count - llm.num_pckt]; | |
471 | ret = send_unix_sock(sock, (void*) &llm, sizeof(llm)); | |
472 | if (ret < 0) { | |
473 | goto send_error; | |
474 | } | |
475 | llm.num_pckt--; | |
476 | } | |
477 | /* Allocated array by ust_list_apps() */ | |
478 | free(pids); | |
479 | ||
fac6795d DG |
480 | break; |
481 | } | |
482 | default: | |
e065084a | 483 | { |
fac6795d | 484 | /* Undefined command */ |
e065084a | 485 | ret = LTTCOMM_UND; |
fac6795d | 486 | break; |
e065084a | 487 | } |
fac6795d DG |
488 | } |
489 | ||
e065084a DG |
490 | return 0; |
491 | ||
492 | send_error: | |
493 | return ret; | |
494 | ||
495 | error: | |
496 | /* Notify client of error */ | |
497 | llm.ret_code = ret; | |
498 | send_unix_sock(sock, (void*) &llm, sizeof(llm)); | |
499 | ||
500 | return -1; | |
fac6795d DG |
501 | } |
502 | ||
503 | /* | |
504 | * usage function on stderr | |
505 | */ | |
506 | static void usage(void) | |
507 | { | |
508 | fprintf(stderr, "Usage:\n%s OPTIONS\n\nOptions:\n" | |
509 | "\t-h, --help\t\tDisplay this usage.\n" | |
510 | "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n" | |
511 | "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n" | |
512 | "\t-d, --daemonize\t\tStart as a daemon.\n" | |
513 | "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n" | |
514 | "\t-V, --version\t\tShow version number.\n", | |
515 | progname); | |
516 | } | |
517 | ||
518 | /* | |
519 | * daemon argument parsing | |
520 | */ | |
521 | static int parse_args(int argc, char **argv) | |
522 | { | |
523 | int c; | |
524 | ||
525 | static struct option long_options[] = { | |
526 | { "client-sock", 1, 0, 'c' }, | |
527 | { "apps-sock", 1, 0, 'a' }, | |
528 | { "daemonize", 0, 0, 'd' }, | |
529 | { "help", 0, 0, 'h' }, | |
530 | { "group", 1, 0, 'g' }, | |
531 | { "version", 0, 0, 'V' }, | |
532 | { NULL, 0, 0, 0 } | |
533 | }; | |
534 | ||
535 | while (1) { | |
536 | int option_index = 0; | |
537 | c = getopt_long(argc, argv, "dhV" "a:c:g:s:", long_options, &option_index); | |
538 | if (c == -1) { | |
539 | break; | |
540 | } | |
541 | ||
542 | switch (c) { | |
543 | case 0: | |
544 | fprintf(stderr, "option %s", long_options[option_index].name); | |
545 | if (optarg) { | |
546 | fprintf(stderr, " with arg %s\n", optarg); | |
547 | } | |
548 | break; | |
549 | case 's': | |
550 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); | |
551 | break; | |
552 | case 'a': | |
553 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); | |
554 | break; | |
555 | case 'd': | |
556 | opt_daemon = 1; | |
557 | break; | |
558 | case 'g': | |
559 | opt_tracing_group = strdup(optarg); | |
560 | break; | |
561 | case 'h': | |
562 | usage(); | |
563 | exit(EXIT_FAILURE); | |
564 | case 'V': | |
565 | fprintf(stdout, "%s\n", VERSION); | |
566 | exit(EXIT_SUCCESS); | |
567 | default: | |
568 | /* Unknown option or other error. | |
569 | * Error is printed by getopt, just return */ | |
570 | return -1; | |
571 | } | |
572 | } | |
573 | ||
574 | return 0; | |
575 | } | |
576 | ||
577 | /* | |
578 | * init_daemon_socket | |
579 | * | |
580 | * Creates the two needed socket by the daemon. | |
581 | * apps_socket - The communication socket for all UST apps. | |
582 | * client_socket - The communication of the cli tool (lttng). | |
583 | */ | |
584 | static int init_daemon_socket() | |
585 | { | |
586 | int ret = 0; | |
587 | mode_t old_umask; | |
588 | ||
589 | old_umask = umask(0); | |
590 | ||
591 | /* Create client tool unix socket */ | |
592 | client_socket = lttcomm_create_unix_sock(client_unix_sock_path); | |
593 | if (client_socket < 0) { | |
594 | ret = -1; | |
595 | goto end; | |
596 | } | |
597 | ||
598 | /* File permission MUST be 660 */ | |
599 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
600 | if (ret < 0) { | |
601 | perror("chmod"); | |
602 | goto end; | |
603 | } | |
604 | ||
605 | /* Create the application unix socket */ | |
606 | apps_socket = lttcomm_create_unix_sock(apps_unix_sock_path); | |
607 | if (apps_socket < 0) { | |
608 | ret = -1; | |
609 | goto end; | |
610 | } | |
611 | ||
612 | /* File permission MUST be 660 */ | |
613 | ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
614 | if (ret < 0) { | |
615 | perror("chmod"); | |
616 | goto end; | |
617 | } | |
618 | ||
619 | end: | |
620 | umask(old_umask); | |
621 | return ret; | |
622 | } | |
623 | ||
624 | /* | |
625 | * check_existing_daemon | |
626 | * | |
627 | * Check if the global socket is available. | |
628 | * If yes, error is returned. | |
629 | */ | |
630 | static int check_existing_daemon() | |
631 | { | |
632 | int ret; | |
633 | ||
634 | ret = access(client_unix_sock_path, F_OK); | |
635 | if (ret == 0) { | |
636 | ret = access(apps_unix_sock_path, F_OK); | |
637 | } | |
638 | ||
639 | return ret; | |
640 | } | |
641 | ||
642 | /* | |
643 | * get_home_dir | |
644 | * | |
645 | * Return pointer to home directory path using | |
646 | * the env variable HOME. | |
647 | * | |
648 | * Default : /tmp | |
649 | */ | |
650 | static const char *get_home_dir(void) | |
651 | { | |
652 | const char *home_path; | |
653 | ||
686204ab | 654 | if ((home_path = (const char *) getenv("HOME")) == NULL) { |
fac6795d DG |
655 | home_path = default_home_dir; |
656 | } | |
657 | ||
658 | return home_path; | |
659 | } | |
660 | ||
661 | /* | |
662 | * set_socket_perms | |
663 | * | |
664 | * Set the tracing group gid onto the client socket. | |
665 | */ | |
666 | static int set_socket_perms(void) | |
667 | { | |
668 | int ret; | |
669 | struct group *grp; | |
670 | ||
671 | /* Decide which group name to use */ | |
672 | (opt_tracing_group != NULL) ? | |
673 | (grp = getgrnam(opt_tracing_group)) : | |
674 | (grp = getgrnam(default_tracing_group)); | |
675 | ||
676 | if (grp == NULL) { | |
677 | fprintf(stderr, "Missing tracing group. Aborting execution.\n"); | |
678 | ret = -1; | |
679 | goto end; | |
680 | } | |
681 | ||
682 | ret = chown(client_unix_sock_path, 0, grp->gr_gid); | |
683 | if (ret < 0) { | |
684 | perror("chown"); | |
685 | } | |
686 | ||
687 | end: | |
688 | return ret; | |
689 | } | |
690 | ||
691 | /* | |
692 | * daemonize | |
693 | * | |
694 | * Daemonize ltt-sessiond. | |
695 | */ | |
696 | static void daemonize(void) | |
697 | { | |
698 | pid_t pid, sid; | |
699 | const char *home_dir = get_home_dir(); | |
700 | ||
701 | /* Fork off the parent process */ | |
702 | if ((pid = fork()) < 0) { | |
703 | perror("fork"); | |
704 | exit(EXIT_FAILURE); | |
705 | } | |
706 | ||
707 | /* Parent can now exit */ | |
708 | if (pid > 0) { | |
709 | exit(EXIT_SUCCESS); | |
710 | } | |
711 | ||
712 | /* Change the file mode mask */ | |
713 | umask(0); | |
714 | ||
715 | /* Create a new SID for the child process */ | |
716 | if ((sid = setsid()) < 0) { | |
717 | perror("setsid"); | |
718 | exit(EXIT_FAILURE); | |
719 | } | |
720 | ||
721 | /* Change the current working directory */ | |
722 | if ((chdir(home_dir)) < 0) { | |
723 | perror("chdir"); | |
724 | exit(EXIT_FAILURE); | |
725 | } | |
726 | ||
727 | /* Close out the standard file descriptors */ | |
728 | close(STDIN_FILENO); | |
729 | close(STDOUT_FILENO); | |
730 | close(STDERR_FILENO); | |
731 | } | |
732 | ||
733 | /* | |
734 | * set_signal_handler | |
735 | * | |
736 | * Setup signal handler for : | |
737 | * SIGINT, SIGTERM, SIGPIPE | |
738 | */ | |
739 | static int set_signal_handler(void) | |
740 | { | |
741 | int ret = 0; | |
742 | struct sigaction sa; | |
743 | sigset_t sigset; | |
744 | ||
745 | if ((ret = sigemptyset(&sigset)) < 0) { | |
746 | perror("sigemptyset"); | |
747 | return ret; | |
748 | } | |
749 | ||
750 | sa.sa_handler = sighandler; | |
751 | sa.sa_mask = sigset; | |
752 | sa.sa_flags = 0; | |
753 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
754 | perror("sigaction"); | |
755 | return ret; | |
756 | } | |
757 | ||
758 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
759 | perror("sigaction"); | |
760 | return ret; | |
761 | } | |
762 | ||
763 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
764 | perror("sigaction"); | |
765 | return ret; | |
766 | } | |
767 | ||
768 | return ret; | |
769 | } | |
770 | ||
771 | /** | |
772 | * sighandler | |
773 | * | |
774 | * Signal handler for the daemon | |
775 | */ | |
776 | static void sighandler(int sig) | |
777 | { | |
778 | switch (sig) { | |
779 | case SIGPIPE: | |
780 | case SIGINT: | |
781 | case SIGTERM: | |
782 | cleanup(); | |
686204ab | 783 | break; |
fac6795d DG |
784 | default: |
785 | break; | |
786 | } | |
787 | ||
788 | exit(EXIT_SUCCESS); | |
789 | } | |
790 | ||
791 | /* | |
792 | * cleanup | |
793 | * | |
794 | * Cleanup the daemon on exit | |
795 | */ | |
796 | static void cleanup() | |
797 | { | |
798 | /* <fun> */ | |
799 | fprintf(stdout, "\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0); | |
800 | fprintf(stdout, "%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0); | |
801 | /* </fun> */ | |
802 | ||
803 | unlink(client_unix_sock_path); | |
804 | unlink(apps_unix_sock_path); | |
805 | } | |
806 | ||
807 | /* | |
808 | * main | |
809 | */ | |
810 | int main(int argc, char **argv) | |
811 | { | |
812 | int i; | |
813 | int ret = 0; | |
814 | void *status; | |
815 | pthread_t threads[2]; | |
816 | ||
817 | /* Parse arguments */ | |
818 | progname = argv[0]; | |
819 | if ((ret = parse_args(argc, argv) < 0)) { | |
820 | goto error; | |
821 | } | |
822 | ||
823 | /* Daemonize */ | |
824 | if (opt_daemon) { | |
825 | daemonize(); | |
826 | } | |
827 | ||
828 | /* Check if daemon is UID = 0 */ | |
829 | is_root = !getuid(); | |
830 | ||
831 | /* Set all sockets path */ | |
832 | if (is_root) { | |
833 | if (strlen(apps_unix_sock_path) == 0) { | |
834 | (snprintf(apps_unix_sock_path, PATH_MAX, | |
835 | DEFAULT_GLOBAL_APPS_UNIX_SOCK)); | |
836 | } | |
837 | ||
838 | if (strlen(client_unix_sock_path) == 0) { | |
839 | (snprintf(client_unix_sock_path, PATH_MAX, | |
840 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK)); | |
841 | } | |
842 | } else { | |
843 | if (strlen(apps_unix_sock_path) == 0) { | |
844 | (snprintf(apps_unix_sock_path, PATH_MAX, | |
845 | DEFAULT_HOME_APPS_UNIX_SOCK, get_home_dir())); | |
846 | } | |
847 | ||
848 | /* Set the cli tool unix socket path */ | |
849 | if (strlen(client_unix_sock_path) == 0) { | |
850 | (snprintf(client_unix_sock_path, PATH_MAX, | |
851 | DEFAULT_HOME_CLIENT_UNIX_SOCK, get_home_dir())); | |
852 | } | |
853 | } | |
854 | ||
855 | /* See if daemon already exist. If any of the two | |
856 | * socket needed by the daemon are present, this test fails | |
857 | */ | |
858 | if ((ret = check_existing_daemon()) == 0) { | |
859 | fprintf(stderr, "Already running daemon.\n"); | |
860 | goto error; | |
861 | } | |
862 | ||
863 | if (set_signal_handler() < 0) { | |
864 | goto error; | |
865 | } | |
866 | ||
867 | /* Setup the two needed unix socket */ | |
868 | if (init_daemon_socket() < 0) { | |
869 | goto error; | |
870 | } | |
871 | ||
872 | /* Set credentials to socket */ | |
873 | if (is_root && (set_socket_perms() < 0)) { | |
874 | goto error; | |
875 | } | |
876 | ||
877 | while (1) { | |
878 | /* Create thread to manage the client socket */ | |
879 | ret = pthread_create(&threads[0], NULL, thread_manage_clients, (void *) NULL); | |
880 | if (ret != 0) { | |
881 | perror("pthread_create"); | |
882 | goto error; | |
883 | } | |
884 | ||
885 | /* Create thread to manage application socket */ | |
886 | ret = pthread_create(&threads[1], NULL, thread_manage_apps, (void *) NULL); | |
887 | if (ret != 0) { | |
888 | perror("pthread_create"); | |
889 | goto error; | |
890 | } | |
891 | ||
892 | for (i = 0; i < 2; i++) { | |
893 | ret = pthread_join(threads[i], &status); | |
894 | if (ret != 0) { | |
895 | perror("pthread_join"); | |
896 | goto error; | |
897 | } | |
898 | } | |
899 | } | |
900 | ||
901 | cleanup(); | |
902 | return 0; | |
903 | ||
904 | error: | |
905 | cleanup(); | |
906 | ||
907 | return EXIT_FAILURE; | |
908 | } |