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
35 #include "ripd/ripd.h"
38 static struct option longopts
[] =
40 { "daemon", no_argument
, NULL
, 'd'},
41 { "config_file", required_argument
, NULL
, 'f'},
42 { "pid_file", required_argument
, NULL
, 'i'},
43 { "help", no_argument
, NULL
, 'h'},
44 { "vty_addr", required_argument
, NULL
, 'A'},
45 { "vty_port", required_argument
, NULL
, 'P'},
46 { "retain", no_argument
, NULL
, 'r'},
47 { "user", required_argument
, NULL
, 'u'},
48 { "version", no_argument
, NULL
, 'v'},
53 zebra_capabilities_t _caps_p
[] =
59 struct zebra_privs_t ripd_privs
=
61 #if defined(ZEBRA_USER)
64 #if defined ZEBRA_GROUP
72 /* Configuration file and directory. */
73 char config_current
[] = RIPD_DEFAULT_CONFIG
;
74 char config_default
[] = SYSCONFDIR RIPD_DEFAULT_CONFIG
;
75 char *config_file
= NULL
;
77 /* ripd program name */
79 /* Route retain mode flag. */
82 /* RIP VTY bind address. */
83 char *vty_addr
= NULL
;
85 /* RIP VTY connection port. */
86 int vty_port
= RIP_VTY_PORT
;
88 /* Master of threads. */
89 struct thread_master
*master
;
91 /* Process ID saved for use by init system */
92 char *pid_file
= PATH_RIPD_PID
;
94 /* Help information display. */
96 usage (char *progname
, int status
)
99 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
102 printf ("Usage : %s [OPTION...]\n\
103 Daemon which manages RIP version 1 and 2.\n\n\
104 -d, --daemon Runs in daemon mode\n\
105 -f, --config_file Set configuration file name\n\
106 -i, --pid_file Set process identifier file name\n\
107 -A, --vty_addr Set vty's bind address\n\
108 -P, --vty_port Set vty's port number\n\
109 -r, --retain When program terminates, retain added route by ripd.\n\
110 -u, --user User and group to run as\n\
111 -v, --version Print program version\n\
112 -h, --help Display this help and exit\n\
114 Report bugs to %s\n", progname
, ZEBRA_BUG_ADDRESS
);
120 /* Signale wrapper. */
122 signal_set (int signo
, void (*func
)(int))
125 struct sigaction sig
;
126 struct sigaction osig
;
128 sig
.sa_handler
= func
;
129 sigemptyset (&sig
.sa_mask
);
132 sig
.sa_flags
|= SA_RESTART
;
133 #endif /* SA_RESTART */
135 ret
= sigaction (signo
, &sig
, &osig
);
140 return (osig
.sa_handler
);
143 /* SIGHUP handler. */
147 zlog_info ("SIGHUP received");
150 zlog_info ("ripd restarting!");
152 /* Reload config file. */
153 vty_read_config (config_file
, config_current
, config_default
);
155 /* Create VTY's socket */
156 vty_serv_sock (vty_addr
, vty_port
, RIP_VTYSH_PATH
);
158 /* Try to return to normal operation. */
161 /* SIGINT handler. */
165 zlog (NULL
, LOG_INFO
, "Terminating on signal");
173 /* SIGUSR1 handler. */
180 /* Initialization of signal handles. */
184 signal_set (SIGHUP
, sighup
);
185 signal_set (SIGINT
, sigint
);
186 signal_set (SIGTERM
, sigint
);
187 signal_set (SIGPIPE
, SIG_IGN
);
188 signal_set (SIGUSR1
, sigusr1
);
191 /* Main routine of ripd. */
193 main (int argc
, char **argv
)
198 struct thread thread
;
200 /* Set umask before anything for security */
203 /* Get program name. */
204 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
206 /* First of all we need logging init. */
207 zlog_default
= openzlog (progname
, ZLOG_NOLOG
, ZLOG_RIP
,
208 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
210 /* Command line option parse. */
215 opt
= getopt_long (argc
, argv
, "df:hA:P:u:rv", longopts
, 0);
228 config_file
= optarg
;
237 /* Deal with atoi() returning 0 on failure, and ripd not
238 listening on rip port... */
239 if (strcmp(optarg
, "0") == 0)
244 vty_port
= atoi (optarg
);
245 vty_port
= (vty_port
? vty_port
: RIP_VTY_PORT
);
251 ripd_privs
.group
= ripd_privs
.user
= optarg
;
254 print_version (progname
);
266 /* Prepare master thread. */
267 master
= thread_master_create ();
269 /* Library initialization. */
270 zprivs_init (&ripd_privs
);
277 /* RIP related initialization. */
283 /* Sort all installed commands. */
286 /* Get configuration file. */
287 vty_read_config (config_file
, config_current
, config_default
);
289 /* Change to the daemon program. */
293 /* Pid file create. */
294 pid_output (pid_file
);
296 /* Create VTY's socket */
297 vty_serv_sock (vty_addr
, vty_port
, RIP_VTYSH_PATH
);
299 /* Execute each thread. */
300 while (thread_fetch (master
, &thread
))
301 thread_call (&thread
);