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