1 /*
2 * BIRD Internet Routing Daemon -- Unix Entry Point
3 *
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #undef LOCAL_DEBUG
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE
13 #endif
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <signal.h>
20 #include <pwd.h>
21 #include <grp.h>
22 #include <sys/stat.h>
23 #include <sys/utsname.h>
24 #include <libgen.h>
25
26 #include "nest/bird.h"
27 #include "lib/lists.h"
28 #include "lib/resource.h"
29 #include "lib/socket.h"
30 #include "lib/event.h"
31 #include "lib/timer.h"
32 #include "lib/string.h"
33 #include "nest/route.h"
34 #include "nest/protocol.h"
35 #include "nest/iface.h"
36 #include "nest/cli.h"
37 #include "nest/locks.h"
38 #include "conf/conf.h"
39 #include "filter/filter.h"
40 #include "filter/data.h"
41
42 #include "unix.h"
43 #include "krt.h"
44
45 /*
46 * Debugging
47 */
48
49 void
async_dump(void)50 async_dump(void)
51 {
52 debug("INTERNAL STATE DUMP\n\n");
53
54 rdump(&root_pool);
55 sk_dump_all();
56 // XXXX tm_dump_all();
57 if_dump_all();
58 neigh_dump_all();
59 rta_dump_all();
60 rt_dump_all();
61 protos_dump_all();
62
63 debug("\n");
64 }
65
66 /*
67 * Dropping privileges
68 */
69
70 #ifdef CONFIG_RESTRICTED_PRIVILEGES
71 #include CONFIG_INCLUDE_SYSPRIV_H
72 #else
73
74 static inline void
drop_uid(uid_t uid UNUSED)75 drop_uid(uid_t uid UNUSED)
76 {
77 die("Cannot change user on this platform");
78 }
79
80 #endif
81
82 static inline void
drop_gid(gid_t gid)83 drop_gid(gid_t gid)
84 {
85 if (setgid(gid) < 0)
86 die("setgid: %m");
87
88 if (setgroups(0, NULL) < 0)
89 die("setgroups: %m");
90 }
91
92 /*
93 * Hostname
94 */
95
96 char *
get_hostname(linpool * lp)97 get_hostname(linpool *lp)
98 {
99 struct utsname uts = {};
100
101 if (uname(&uts) < 0)
102 return NULL;
103
104 return lp_strdup(lp, uts.nodename);
105 }
106
107 /*
108 * Reading the Configuration
109 */
110
111 #ifdef PATH_IPROUTE_DIR
112
113 static inline void
add_num_const(char * name,int val,const char * file,const uint line)114 add_num_const(char *name, int val, const char *file, const uint line)
115 {
116 struct f_val *v = cfg_alloc(sizeof(struct f_val));
117 *v = (struct f_val) { .type = T_INT, .val.i = val };
118 struct symbol *sym = cf_get_symbol(name);
119 if (sym->class && (sym->scope == conf_this_scope))
120 cf_error("Error reading value for %s from %s:%d: already defined", name, file, line);
121
122 cf_define_symbol(sym, SYM_CONSTANT | T_INT, val, v);
123 }
124
125 /* the code of read_iproute_table() is based on
126 rtnl_tab_initialize() from iproute2 package */
127 static void
read_iproute_table(char * file,char * prefix,int max)128 read_iproute_table(char *file, char *prefix, int max)
129 {
130 char buf[512], namebuf[512];
131 char *name;
132 int val;
133 FILE *fp;
134
135 strcpy(namebuf, prefix);
136 name = namebuf + strlen(prefix);
137
138 fp = fopen(file, "r");
139 if (!fp)
140 return;
141
142 for (uint line = 1; fgets(buf, sizeof(buf), fp); line++)
143 {
144 char *p = buf;
145
146 while (*p == ' ' || *p == '\t')
147 p++;
148
149 if (*p == '#' || *p == '\n' || *p == 0)
150 continue;
151
152 if (sscanf(p, "0x%x %s\n", &val, name) != 2 &&
153 sscanf(p, "0x%x %s #", &val, name) != 2 &&
154 sscanf(p, "%d %s\n", &val, name) != 2 &&
155 sscanf(p, "%d %s #", &val, name) != 2)
156 continue;
157
158 if (val < 0 || val > max)
159 continue;
160
161 for(p = name; *p; p++)
162 if ((*p < 'a' || *p > 'z') && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9') && (*p != '_'))
163 *p = '_';
164
165 add_num_const(namebuf, val, file, line);
166 }
167
168 fclose(fp);
169 }
170
171 #endif // PATH_IPROUTE_DIR
172
173
174 static char *config_name = PATH_CONFIG_FILE;
175
176 static int
cf_read(byte * dest,uint len,int fd)177 cf_read(byte *dest, uint len, int fd)
178 {
179 int l = read(fd, dest, len);
180 if (l < 0)
181 cf_error("Read error");
182 return l;
183 }
184
185 void
sysdep_preconfig(struct config * c)186 sysdep_preconfig(struct config *c)
187 {
188 init_list(&c->logfiles);
189
190 c->latency_limit = UNIX_DEFAULT_LATENCY_LIMIT;
191 c->watchdog_warning = UNIX_DEFAULT_WATCHDOG_WARNING;
192
193 #ifdef PATH_IPROUTE_DIR
194 read_iproute_table(PATH_IPROUTE_DIR "/rt_protos", "ipp_", 256);
195 read_iproute_table(PATH_IPROUTE_DIR "/rt_realms", "ipr_", 256);
196 read_iproute_table(PATH_IPROUTE_DIR "/rt_scopes", "ips_", 256);
197 read_iproute_table(PATH_IPROUTE_DIR "/rt_tables", "ipt_", 256);
198 #endif
199 }
200
201 int
sysdep_commit(struct config * new,struct config * old UNUSED)202 sysdep_commit(struct config *new, struct config *old UNUSED)
203 {
204 log_switch(0, &new->logfiles, new->syslog_name);
205 return 0;
206 }
207
208 static int
unix_read_config(struct config ** cp,const char * name)209 unix_read_config(struct config **cp, const char *name)
210 {
211 struct config *conf = config_alloc(name);
212 int ret;
213
214 *cp = conf;
215 conf->file_fd = open(name, O_RDONLY);
216 if (conf->file_fd < 0)
217 return 0;
218 cf_read_hook = cf_read;
219 ret = config_parse(conf);
220 close(conf->file_fd);
221 return ret;
222 }
223
224 static struct config *
read_config(void)225 read_config(void)
226 {
227 struct config *conf;
228
229 if (!unix_read_config(&conf, config_name))
230 {
231 if (conf->err_msg)
232 die("%s:%d:%d %s", conf->err_file_name, conf->err_lino, conf->err_chno, conf->err_msg);
233 else
234 die("Unable to open configuration file %s: %m", config_name);
235 }
236
237 return conf;
238 }
239
240 void
async_config(void)241 async_config(void)
242 {
243 struct config *conf;
244
245 log(L_INFO "Reconfiguration requested by SIGHUP");
246 if (!unix_read_config(&conf, config_name))
247 {
248 if (conf->err_msg)
249 log(L_ERR "%s:%d:%d %s", conf->err_file_name, conf->err_lino, conf->err_chno, conf->err_msg);
250 else
251 log(L_ERR "Unable to open configuration file %s: %m", config_name);
252 config_free(conf);
253 }
254 else
255 config_commit(conf, RECONFIG_HARD, 0);
256 }
257
258 static struct config *
cmd_read_config(const char * name)259 cmd_read_config(const char *name)
260 {
261 struct config *conf;
262
263 if (!name)
264 name = config_name;
265
266 cli_msg(-2, "Reading configuration from %s", name);
267 if (!unix_read_config(&conf, name))
268 {
269 if (conf->err_msg)
270 cli_msg(8002, "%s:%d:%d %s", conf->err_file_name, conf->err_lino, conf->err_chno, conf->err_msg);
271 else
272 cli_msg(8002, "%s: %m", name);
273 config_free(conf);
274 conf = NULL;
275 }
276
277 return conf;
278 }
279
280 void
cmd_check_config(const char * name)281 cmd_check_config(const char *name)
282 {
283 struct config *conf = cmd_read_config(name);
284 if (!conf)
285 return;
286
287 cli_msg(20, "Configuration OK");
288 config_free(conf);
289 }
290
291 static void
cmd_reconfig_msg(int r)292 cmd_reconfig_msg(int r)
293 {
294 switch (r)
295 {
296 case CONF_DONE: cli_msg( 3, "Reconfigured"); break;
297 case CONF_PROGRESS: cli_msg( 4, "Reconfiguration in progress"); break;
298 case CONF_QUEUED: cli_msg( 5, "Reconfiguration already in progress, queueing new config"); break;
299 case CONF_UNQUEUED: cli_msg(17, "Reconfiguration already in progress, removing queued config"); break;
300 case CONF_CONFIRM: cli_msg(18, "Reconfiguration confirmed"); break;
301 case CONF_SHUTDOWN: cli_msg( 6, "Reconfiguration ignored, shutting down"); break;
302 case CONF_NOTHING: cli_msg(19, "Nothing to do"); break;
303 default: break;
304 }
305 }
306
307 /* Hack for scheduled undo notification */
308 cli *cmd_reconfig_stored_cli;
309
310 void
cmd_reconfig_undo_notify(void)311 cmd_reconfig_undo_notify(void)
312 {
313 if (cmd_reconfig_stored_cli)
314 {
315 cli *c = cmd_reconfig_stored_cli;
316 cli_printf(c, CLI_ASYNC_CODE, "Config timeout expired, starting undo");
317 cli_write_trigger(c);
318 }
319 }
320
321 void
cmd_reconfig(const char * name,int type,uint timeout)322 cmd_reconfig(const char *name, int type, uint timeout)
323 {
324 if (cli_access_restricted())
325 return;
326
327 struct config *conf = cmd_read_config(name);
328 if (!conf)
329 return;
330
331 int r = config_commit(conf, type, timeout);
332
333 if ((r >= 0) && (timeout > 0))
334 {
335 cmd_reconfig_stored_cli = this_cli;
336 cli_msg(-22, "Undo scheduled in %d s", timeout);
337 }
338
339 cmd_reconfig_msg(r);
340 }
341
342 void
cmd_reconfig_confirm(void)343 cmd_reconfig_confirm(void)
344 {
345 if (cli_access_restricted())
346 return;
347
348 int r = config_confirm();
349 cmd_reconfig_msg(r);
350 }
351
352 void
cmd_reconfig_undo(void)353 cmd_reconfig_undo(void)
354 {
355 if (cli_access_restricted())
356 return;
357
358 cli_msg(-21, "Undo requested");
359
360 int r = config_undo();
361 cmd_reconfig_msg(r);
362 }
363
364 void
cmd_reconfig_status(void)365 cmd_reconfig_status(void)
366 {
367 int s = config_status();
368 btime t = config_timer_status();
369
370 switch (s)
371 {
372 case CONF_DONE: cli_msg(-3, "Daemon is up and running"); break;
373 case CONF_PROGRESS: cli_msg(-4, "Reconfiguration in progress"); break;
374 case CONF_QUEUED: cli_msg(-5, "Reconfiguration in progress, next one enqueued"); break;
375 case CONF_SHUTDOWN: cli_msg(-6, "Shutdown in progress"); break;
376 default: break;
377 }
378
379 if (t >= 0)
380 cli_msg(-22, "Configuration unconfirmed, undo in %t s", t);
381
382 cli_msg(0, "");
383 }
384
385
386 /*
387 * Command-Line Interface
388 */
389
390 static sock *cli_sk;
391 static char *path_control_socket = PATH_CONTROL_SOCKET;
392
393
394 static void
cli_write(cli * c)395 cli_write(cli *c)
396 {
397 sock *s = c->priv;
398
399 while (c->tx_pos)
400 {
401 struct cli_out *o = c->tx_pos;
402
403 int len = o->wpos - o->outpos;
404 s->tbuf = o->outpos;
405 o->outpos = o->wpos;
406
407 if (sk_send(s, len) <= 0)
408 return;
409
410 c->tx_pos = o->next;
411 }
412
413 /* Everything is written */
414 s->tbuf = NULL;
415 cli_written(c);
416 }
417
418 void
cli_write_trigger(cli * c)419 cli_write_trigger(cli *c)
420 {
421 sock *s = c->priv;
422
423 if (s->tbuf == NULL)
424 cli_write(c);
425 }
426
427 static void
cli_tx(sock * s)428 cli_tx(sock *s)
429 {
430 cli_write(s->data);
431 }
432
433 int
cli_get_command(cli * c)434 cli_get_command(cli *c)
435 {
436 sock *s = c->priv;
437 byte *t = c->rx_aux ? : s->rbuf;
438 byte *tend = s->rpos;
439 byte *d = c->rx_pos;
440 byte *dend = c->rx_buf + CLI_RX_BUF_SIZE - 2;
441
442 while (t < tend)
443 {
444 if (*t == '\r')
445 t++;
446 else if (*t == '\n')
447 {
448 t++;
449 c->rx_pos = c->rx_buf;
450 c->rx_aux = t;
451 *d = 0;
452 return (d < dend) ? 1 : -1;
453 }
454 else if (d < dend)
455 *d++ = *t++;
456 }
457 c->rx_aux = s->rpos = s->rbuf;
458 c->rx_pos = d;
459 return 0;
460 }
461
462 static int
cli_rx(sock * s,uint size UNUSED)463 cli_rx(sock *s, uint size UNUSED)
464 {
465 cli_kick(s->data);
466 return 0;
467 }
468
469 static void
cli_err(sock * s,int err)470 cli_err(sock *s, int err)
471 {
472 if (config->cli_debug)
473 {
474 if (err)
475 log(L_INFO "CLI connection dropped: %s", strerror(err));
476 else
477 log(L_INFO "CLI connection closed");
478 }
479 cli_free(s->data);
480 }
481
482 static int
cli_connect(sock * s,uint size UNUSED)483 cli_connect(sock *s, uint size UNUSED)
484 {
485 cli *c;
486
487 if (config->cli_debug)
488 log(L_INFO "CLI connect");
489 s->rx_hook = cli_rx;
490 s->tx_hook = cli_tx;
491 s->err_hook = cli_err;
492 s->data = c = cli_new(s);
493 s->pool = c->pool; /* We need to have all the socket buffers allocated in the cli pool */
494 s->fast_rx = 1;
495 c->rx_pos = c->rx_buf;
496 c->rx_aux = NULL;
497 rmove(s, c->pool);
498 return 1;
499 }
500
501 static void
cli_init_unix(uid_t use_uid,gid_t use_gid)502 cli_init_unix(uid_t use_uid, gid_t use_gid)
503 {
504 sock *s;
505
506 cli_init();
507 s = cli_sk = sk_new(cli_pool);
508 s->type = SK_UNIX_PASSIVE;
509 s->rx_hook = cli_connect;
510 s->rbsize = 1024;
511 s->fast_rx = 1;
512
513 /* Return value intentionally ignored */
514 unlink(path_control_socket);
515
516 if (sk_open_unix(s, path_control_socket) < 0)
517 die("Cannot create control socket %s: %m", path_control_socket);
518
519 if (use_uid || use_gid)
520 if (chown(path_control_socket, use_uid, use_gid) < 0)
521 die("chown: %m");
522
523 if (chmod(path_control_socket, 0660) < 0)
524 die("chmod: %m");
525 }
526
527 /*
528 * PID file
529 */
530
531 static char *pid_file;
532 static int pid_fd;
533
534 static inline void
open_pid_file(void)535 open_pid_file(void)
536 {
537 if (!pid_file)
538 return;
539
540 pid_fd = open(pid_file, O_WRONLY|O_CREAT, 0664);
541 if (pid_fd < 0)
542 die("Cannot create PID file %s: %m", pid_file);
543 }
544
545 static inline void
write_pid_file(void)546 write_pid_file(void)
547 {
548 int pl, rv;
549 char ps[24];
550
551 if (!pid_file)
552 return;
553
554 /* We don't use PID file for uniqueness, so no need for locking */
555
556 pl = bsnprintf(ps, sizeof(ps), "%ld\n", (s64) getpid());
557 if (pl < 0)
558 bug("PID buffer too small");
559
560 rv = ftruncate(pid_fd, 0);
561 if (rv < 0)
562 die("fruncate: %m");
563
564 rv = write(pid_fd, ps, pl);
565 if(rv < 0)
566 die("write: %m");
567
568 close(pid_fd);
569 }
570
571 static inline void
unlink_pid_file(void)572 unlink_pid_file(void)
573 {
574 if (pid_file)
575 unlink(pid_file);
576 }
577
578
579 /*
580 * Shutdown
581 */
582
583 void
cmd_shutdown(void)584 cmd_shutdown(void)
585 {
586 if (cli_access_restricted())
587 return;
588
589 cli_msg(7, "Shutdown requested");
590 order_shutdown(0);
591 }
592
593 void
async_shutdown(void)594 async_shutdown(void)
595 {
596 DBG("Shutting down...\n");
597 order_shutdown(0);
598 }
599
600 void
sysdep_shutdown_done(void)601 sysdep_shutdown_done(void)
602 {
603 unlink_pid_file();
604 unlink(path_control_socket);
605 log_msg(L_FATAL "Shutdown completed");
606 exit(0);
607 }
608
609 void
cmd_graceful_restart(void)610 cmd_graceful_restart(void)
611 {
612 if (cli_access_restricted())
613 return;
614
615 cli_msg(25, "Graceful restart requested");
616 order_shutdown(1);
617 }
618
619
620 /*
621 * Signals
622 */
623
624 volatile sig_atomic_t async_config_flag;
625 volatile sig_atomic_t async_dump_flag;
626 volatile sig_atomic_t async_shutdown_flag;
627
628 static void
handle_sighup(int sig UNUSED)629 handle_sighup(int sig UNUSED)
630 {
631 DBG("Caught SIGHUP...\n");
632 async_config_flag = 1;
633 }
634
635 static void
handle_sigusr(int sig UNUSED)636 handle_sigusr(int sig UNUSED)
637 {
638 DBG("Caught SIGUSR...\n");
639 async_dump_flag = 1;
640 }
641
642 static void
handle_sigterm(int sig UNUSED)643 handle_sigterm(int sig UNUSED)
644 {
645 DBG("Caught SIGTERM...\n");
646 async_shutdown_flag = 1;
647 }
648
649 void watchdog_sigalrm(int sig UNUSED);
650
651 static void
signal_init(void)652 signal_init(void)
653 {
654 struct sigaction sa;
655
656 bzero(&sa, sizeof(sa));
657 sa.sa_handler = handle_sigusr;
658 sa.sa_flags = SA_RESTART;
659 sigaction(SIGUSR1, &sa, NULL);
660 sa.sa_handler = handle_sighup;
661 sa.sa_flags = SA_RESTART;
662 sigaction(SIGHUP, &sa, NULL);
663 sa.sa_handler = handle_sigterm;
664 sa.sa_flags = SA_RESTART;
665 sigaction(SIGTERM, &sa, NULL);
666 sa.sa_handler = watchdog_sigalrm;
667 sa.sa_flags = 0;
668 sigaction(SIGALRM, &sa, NULL);
669 signal(SIGPIPE, SIG_IGN);
670 }
671
672 /*
673 * Parsing of command-line arguments
674 */
675
676 static char *opt_list = "bc:dD:ps:P:u:g:flRh";
677 int parse_and_exit;
678 char *bird_name;
679 static char *use_user;
680 static char *use_group;
681 static int run_in_foreground = 0;
682
683 static void
display_usage(void)684 display_usage(void)
685 {
686 fprintf(stderr, "Usage: %s [--version] [--help] [-c <config-file>] [OPTIONS]\n", bird_name);
687 }
688
689 static void
display_help(void)690 display_help(void)
691 {
692 display_usage();
693
694 fprintf(stderr,
695 "\n"
696 "Options: \n"
697 " -c <config-file> Use given configuration file instead of\n"
698 " " PATH_CONFIG_FILE "\n"
699 " -d Enable debug messages and run bird in foreground\n"
700 " -D <debug-file> Log debug messages to given file instead of stderr\n"
701 " -f Run bird in foreground\n"
702 " -g <group> Use given group ID\n"
703 " -h, --help Display this information\n"
704 " -l Look for a configuration file and a control socket\n"
705 " in the current working directory\n"
706 " -p Test configuration file and exit without start\n"
707 " -P <pid-file> Create a PID file with given filename\n"
708 " -R Apply graceful restart recovery after start\n"
709 " -s <control-socket> Use given filename for a control socket\n"
710 " -u <user> Drop privileges and use given user ID\n"
711 " --version Display version of BIRD\n");
712
713 exit(0);
714 }
715
716 static void
display_version(void)717 display_version(void)
718 {
719 fprintf(stderr, "BIRD version " BIRD_VERSION "\n");
720 exit(0);
721 }
722
723 static inline char *
get_bird_name(char * s,char * def)724 get_bird_name(char *s, char *def)
725 {
726 char *t;
727 if (!s)
728 return def;
729 t = strrchr(s, '/');
730 if (!t)
731 return s;
732 if (!t[1])
733 return def;
734 return t+1;
735 }
736
737 static inline uid_t
get_uid(const char * s)738 get_uid(const char *s)
739 {
740 struct passwd *pw;
741 char *endptr;
742 long int rv;
743
744 if (!s)
745 return 0;
746
747 errno = 0;
748 rv = strtol(s, &endptr, 10);
749
750 if (!errno && !*endptr)
751 return rv;
752
753 pw = getpwnam(s);
754 if (!pw)
755 die("Cannot find user '%s'", s);
756
757 return pw->pw_uid;
758 }
759
760 static inline gid_t
get_gid(const char * s)761 get_gid(const char *s)
762 {
763 struct group *gr;
764 char *endptr;
765 long int rv;
766
767 if (!s)
768 return 0;
769
770 errno = 0;
771 rv = strtol(s, &endptr, 10);
772
773 if (!errno && !*endptr)
774 return rv;
775
776 gr = getgrnam(s);
777 if (!gr)
778 die("Cannot find group '%s'", s);
779
780 return gr->gr_gid;
781 }
782
783 static void
parse_args(int argc,char ** argv)784 parse_args(int argc, char **argv)
785 {
786 int config_changed = 0;
787 int socket_changed = 0;
788 int c;
789
790 bird_name = get_bird_name(argv[0], "bird");
791 if (argc == 2)
792 {
793 if (!strcmp(argv[1], "--version"))
794 display_version();
795 if (!strcmp(argv[1], "--help"))
796 display_help();
797 }
798 while ((c = getopt(argc, argv, opt_list)) >= 0)
799 switch (c)
800 {
801 case 'c':
802 config_name = optarg;
803 config_changed = 1;
804 break;
805 case 'd':
806 log_init_debug("");
807 run_in_foreground = 1;
808 break;
809 case 'D':
810 log_init_debug(optarg);
811 break;
812 case 'p':
813 parse_and_exit = 1;
814 break;
815 case 's':
816 path_control_socket = optarg;
817 socket_changed = 1;
818 break;
819 case 'P':
820 pid_file = optarg;
821 break;
822 case 'u':
823 use_user = optarg;
824 break;
825 case 'g':
826 use_group = optarg;
827 break;
828 case 'f':
829 run_in_foreground = 1;
830 break;
831 case 'l':
832 if (!config_changed)
833 config_name = xbasename(config_name);
834 if (!socket_changed)
835 path_control_socket = xbasename(path_control_socket);
836 break;
837 case 'R':
838 graceful_restart_recovery();
839 break;
840 case 'h':
841 display_help();
842 break;
843 default:
844 fputc('\n', stderr);
845 display_usage();
846 exit(1);
847 }
848 if (optind < argc)
849 {
850 display_usage();
851 exit(1);
852 }
853 }
854
855 /*
856 * Hic Est main()
857 */
858
859 int
main(int argc,char ** argv)860 main(int argc, char **argv)
861 {
862 #ifdef HAVE_LIBDMALLOC
863 if (!getenv("DMALLOC_OPTIONS"))
864 dmalloc_debug(0x2f03d00);
865 #endif
866
867 parse_args(argc, argv);
868 log_switch(1, NULL, NULL);
869
870 net_init();
871 resource_init();
872 timer_init();
873 olock_init();
874 io_init();
875 rt_init();
876 if_init();
877 // roa_init();
878 config_init();
879
880 uid_t use_uid = get_uid(use_user);
881 gid_t use_gid = get_gid(use_group);
882
883 if (!parse_and_exit)
884 {
885 test_old_bird(path_control_socket);
886 cli_init_unix(use_uid, use_gid);
887 }
888
889 if (use_gid)
890 drop_gid(use_gid);
891
892 if (use_uid)
893 drop_uid(use_uid);
894
895 if (!parse_and_exit)
896 open_pid_file();
897
898 protos_build();
899 proto_build(&proto_unix_kernel);
900 proto_build(&proto_unix_iface);
901
902 struct config *conf = read_config();
903
904 if (parse_and_exit)
905 exit(0);
906
907 if (!run_in_foreground)
908 {
909 pid_t pid = fork();
910 if (pid < 0)
911 die("fork: %m");
912 if (pid)
913 return 0;
914 setsid();
915 close(0);
916 if (open("/dev/null", O_RDWR) < 0)
917 die("Cannot open /dev/null: %m");
918 dup2(0, 1);
919 dup2(0, 2);
920 }
921
922 main_thread_init();
923
924 write_pid_file();
925
926 signal_init();
927
928 config_commit(conf, RECONFIG_HARD, 0);
929
930 graceful_restart_init();
931
932 #ifdef LOCAL_DEBUG
933 async_dump_flag = 1;
934 #endif
935
936 log(L_INFO "Started");
937 DBG("Entering I/O loop.\n");
938
939 io_loop();
940 bug("I/O loop died");
941 }
942