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