2 * Copyright (C) 1997, 98 Kunihiro Ishiguro <kunihiro@zebra.org>
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 "ripd/ripd.h"
37 static struct option longopts
[] =
39 { "daemon", no_argument
, NULL
, 'd'},
40 { "config_file", required_argument
, NULL
, 'f'},
41 { "pid_file", required_argument
, NULL
, 'i'},
42 { "help", no_argument
, NULL
, 'h'},
43 { "vty_addr", required_argument
, NULL
, 'A'},
44 { "vty_port", required_argument
, NULL
, 'P'},
45 { "retain", no_argument
, NULL
, 'r'},
46 { "version", no_argument
, NULL
, 'v'},
50 /* Configuration file and directory. */
51 char config_current
[] = RIPD_DEFAULT_CONFIG
;
52 char config_default
[] = SYSCONFDIR RIPD_DEFAULT_CONFIG
;
53 char *config_file
= NULL
;
55 /* ripd program name */
57 /* Route retain mode flag. */
60 /* RIP VTY bind address. */
61 char *vty_addr
= NULL
;
63 /* RIP VTY connection port. */
64 int vty_port
= RIP_VTY_PORT
;
66 /* Master of threads. */
67 struct thread_master
*master
;
69 /* Process ID saved for use by init system */
70 char *pid_file
= PATH_RIPD_PID
;
72 /* Help information display. */
74 usage (char *progname
, int status
)
77 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
80 printf ("Usage : %s [OPTION...]\n\
81 Daemon which manages RIP version 1 and 2.\n\n\
82 -d, --daemon Runs in daemon mode\n\
83 -f, --config_file Set configuration file name\n\
84 -i, --pid_file Set process identifier file name\n\
85 -A, --vty_addr Set vty's bind address\n\
86 -P, --vty_port Set vty's port number\n\
87 -r, --retain When program terminates, retain added route by ripd.\n\
88 -v, --version Print program version\n\
89 -h, --help Display this help and exit\n\
91 Report bugs to %s\n", progname
, ZEBRA_BUG_ADDRESS
);
97 /* Signale wrapper. */
99 signal_set (int signo
, void (*func
)(int))
102 struct sigaction sig
;
103 struct sigaction osig
;
105 sig
.sa_handler
= func
;
106 sigemptyset (&sig
.sa_mask
);
109 sig
.sa_flags
|= SA_RESTART
;
110 #endif /* SA_RESTART */
112 ret
= sigaction (signo
, &sig
, &osig
);
117 return (osig
.sa_handler
);
120 /* SIGHUP handler. */
124 zlog_info ("SIGHUP received");
127 zlog_info ("ripd restarting!");
129 /* Reload config file. */
130 vty_read_config (config_file
, config_current
, config_default
);
132 /* Create VTY's socket */
133 vty_serv_sock (vty_addr
, vty_port
, RIP_VTYSH_PATH
);
135 /* Try to return to normal operation. */
138 /* SIGINT handler. */
142 zlog (NULL
, LOG_INFO
, "Terminating on signal");
150 /* SIGUSR1 handler. */
157 /* Initialization of signal handles. */
161 signal_set (SIGHUP
, sighup
);
162 signal_set (SIGINT
, sigint
);
163 signal_set (SIGTERM
, sigint
);
164 signal_set (SIGPIPE
, SIG_IGN
);
165 signal_set (SIGUSR1
, sigusr1
);
168 /* Main routine of ripd. */
170 main (int argc
, char **argv
)
175 struct thread thread
;
177 /* Set umask before anything for security */
180 /* Get program name. */
181 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
183 /* First of all we need logging init. */
184 zlog_default
= openzlog (progname
, ZLOG_NOLOG
, ZLOG_RIP
,
185 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
187 /* Command line option parse. */
192 opt
= getopt_long (argc
, argv
, "df:hA:P:rv", longopts
, 0);
205 config_file
= optarg
;
214 vty_port
= atoi (optarg
);
220 print_version (progname
);
232 /* Prepare master thread. */
233 master
= thread_master_create ();
235 /* Library initialization. */
242 /* RIP related initialization. */
248 /* Sort all installed commands. */
251 /* Get configuration file. */
252 vty_read_config (config_file
, config_current
, config_default
);
254 /* Change to the daemon program. */
258 /* Pid file create. */
259 pid_output (pid_file
);
261 /* Create VTY's socket */
262 vty_serv_sock (vty_addr
, vty_port
, RIP_VTYSH_PATH
);
264 /* Execute each thread. */
265 while (thread_fetch (master
, &thread
))
266 thread_call (&thread
);