1 /* zebra daemon main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
34 #include "zebra/rib.h"
35 #include "zebra/zserv.h"
36 #include "zebra/debug.h"
37 #include "zebra/rib.h"
39 /* Master of threads. */
40 struct thread_master
*master
;
46 /* Route retain mode flag. */
49 /* Don't delete kernel route. */
50 int keep_kernel_mode
= 0;
52 /* Command line options. */
53 struct option longopts
[] =
55 { "batch", no_argument
, NULL
, 'b'},
56 { "daemon", no_argument
, NULL
, 'd'},
57 { "keep_kernel", no_argument
, NULL
, 'k'},
58 { "log_mode", no_argument
, NULL
, 'l'},
59 { "config_file", required_argument
, NULL
, 'f'},
60 { "pid_file", required_argument
, NULL
, 'i'},
61 { "help", no_argument
, NULL
, 'h'},
62 { "vty_addr", required_argument
, NULL
, 'A'},
63 { "vty_port", required_argument
, NULL
, 'P'},
64 { "retain", no_argument
, NULL
, 'r'},
65 { "user", required_argument
, NULL
, 'u'},
66 { "version", no_argument
, NULL
, 'v'},
70 zebra_capabilities_t _caps_p
[] =
76 /* zebra privileges to run with */
77 struct zebra_privs_t zserv_privs
=
79 #if defined(ZEBRA_USER) && defined(ZEBRA_GROUP)
84 .vty_group
= VTY_GROUP
,
87 .cap_num_p
= sizeof(_caps_p
)/sizeof(_caps_p
[0]),
91 /* Default configuration file path. */
92 char config_current
[] = DEFAULT_CONFIG_FILE
;
93 char config_default
[] = SYSCONFDIR DEFAULT_CONFIG_FILE
;
95 /* Process ID saved for use by init system */
96 char *pid_file
= PATH_ZEBRA_PID
;
98 /* Help information display. */
100 usage (char *progname
, int status
)
103 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
106 printf ("Usage : %s [OPTION...]\n\n\
107 Daemon which manages kernel routing table management and \
108 redistribution between different routing protocols.\n\n\
109 -b, --batch Runs in batch mode\n\
110 -d, --daemon Runs in daemon mode\n\
111 -f, --config_file Set configuration file name\n\
112 -i, --pid_file Set process identifier file name\n\
113 -k, --keep_kernel Don't delete old routes which installed by zebra.\n\
114 -l, --log_mode Set verbose log mode flag\n\
115 -A, --vty_addr Set vty's bind address\n\
116 -P, --vty_port Set vty's port number\n\
117 -r, --retain When program terminates, retain added route by zebra.\n\
118 -u, --user User and group to run as\n\
119 -v, --version Print program version\n\
120 -h, --help Display this help and exit\n\
122 Report bugs to %s\n", progname
, ZEBRA_BUG_ADDRESS
);
128 /* SIGHUP handler. */
132 zlog_info ("SIGHUP received");
134 /* Reload of config file. */
138 /* SIGINT handler. */
142 /* Decrared in rib.c */
145 zlog_info ("Terminating on signal");
153 /* SIGUSR1 handler. */
160 /* Signale wrapper. */
162 signal_set (int signo
, void (*func
)(int))
165 struct sigaction sig
;
166 struct sigaction osig
;
168 sig
.sa_handler
= func
;
169 sigemptyset (&sig
.sa_mask
);
172 sig
.sa_flags
|= SA_RESTART
;
173 #endif /* SA_RESTART */
175 ret
= sigaction (signo
, &sig
, &osig
);
180 return (osig
.sa_handler
);
183 /* Initialization of signal handles. */
187 signal_set (SIGHUP
, sighup
);
188 signal_set (SIGINT
, sigint
);
189 signal_set (SIGTERM
, sigint
);
190 signal_set (SIGPIPE
, SIG_IGN
);
191 signal_set (SIGUSR1
, sigusr1
);
194 /* Main startup routine. */
196 main (int argc
, char **argv
)
199 char *vty_addr
= NULL
;
200 int vty_port
= ZEBRA_VTY_PORT
;
203 char *config_file
= NULL
;
205 struct thread thread
;
206 void rib_weed_tables ();
207 void zebra_vty_init ();
209 /* Set umask before anything for security */
212 /* preserve my name */
213 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
215 zlog_default
= openzlog (progname
, ZLOG_STDOUT
, ZLOG_ZEBRA
,
216 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
222 opt
= getopt_long (argc
, argv
, "bdklf:hA:P:ru:v", longopts
, 0);
237 keep_kernel_mode
= 1;
243 config_file
= optarg
;
252 /* Deal with atoi() returning 0 on failure, and zebra not
253 listening on zebra port... */
254 if (strcmp(optarg
, "0") == 0)
259 vty_port
= atoi (optarg
);
260 vty_port
= (vty_port
? vty_port
: ZEBRA_VTY_PORT
);
266 zserv_privs
.user
= zserv_privs
.group
= optarg
;
269 print_version (progname
);
281 /* Make master thread emulator. */
282 master
= thread_master_create ();
284 /* privs initialise */
285 zprivs_init (&zserv_privs
);
287 /* Vty related initialize. */
293 /* Zebra related initialize. */
302 /* For debug purpose. */
303 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
305 /* Make kernel routing socket. */
310 /* Sort VTY commands. */
315 #endif /* HAVE_SNMP */
317 /* Clean up self inserted route. */
318 if (! keep_kernel_mode
)
321 /* Configuration file read*/
322 vty_read_config (config_file
, config_current
, config_default
);
327 /* Exit when zebra is working in batch mode. */
331 /* Needed for BSD routing socket. */
338 /* Output pid of zebra. */
339 pid_output (pid_file
);
341 /* Needed for BSD routing socket. */
344 /* Make vty server socket. */
345 vty_serv_sock (vty_addr
, vty_port
, ZEBRA_VTYSH_PATH
);
347 while (thread_fetch (master
, &thread
))
348 thread_call (&thread
);