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
24 #include <lib/version.h>
37 #include "ripd/ripd.h"
40 static struct option longopts
[] =
42 { "daemon", no_argument
, NULL
, 'd'},
43 { "config_file", required_argument
, NULL
, 'f'},
44 { "pid_file", required_argument
, NULL
, 'i'},
45 { "socket", required_argument
, NULL
, 'z'},
46 { "help", no_argument
, NULL
, 'h'},
47 { "dryrun", no_argument
, NULL
, 'C'},
48 { "vty_addr", required_argument
, NULL
, 'A'},
49 { "vty_port", required_argument
, NULL
, 'P'},
50 { "retain", no_argument
, NULL
, 'r'},
51 { "user", required_argument
, NULL
, 'u'},
52 { "group", required_argument
, NULL
, 'g'},
53 { "version", no_argument
, NULL
, 'v'},
58 zebra_capabilities_t _caps_p
[] =
64 struct zebra_privs_t ripd_privs
=
66 #if defined(QUAGGA_USER)
69 #if defined QUAGGA_GROUP
70 .group
= QUAGGA_GROUP
,
73 .vty_group
= VTY_GROUP
,
80 /* Configuration file and directory. */
81 char config_default
[] = SYSCONFDIR RIPD_DEFAULT_CONFIG
;
82 char *config_file
= NULL
;
84 /* ripd program name */
86 /* Route retain mode flag. */
89 /* RIP VTY bind address. */
90 char *vty_addr
= NULL
;
92 /* RIP VTY connection port. */
93 int vty_port
= RIP_VTY_PORT
;
95 /* Master of threads. */
96 struct thread_master
*master
;
98 /* Process ID saved for use by init system */
99 const char *pid_file
= PATH_RIPD_PID
;
101 /* Help information display. */
103 usage (char *progname
, int status
)
106 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
109 printf ("Usage : %s [OPTION...]\n\
110 Daemon which manages RIP version 1 and 2.\n\n\
111 -d, --daemon Runs in daemon mode\n\
112 -f, --config_file Set configuration file name\n\
113 -i, --pid_file Set process identifier file name\n\
114 -z, --socket Set path of zebra socket\n\
115 -A, --vty_addr Set vty's bind address\n\
116 -P, --vty_port Set vty's port number\n\
117 -C, --dryrun Check configuration for validity and exit\n\
118 -r, --retain When program terminates, retain added route by ripd.\n\
119 -u, --user User to run as\n\
120 -g, --group Group to run as\n\
121 -v, --version Print program version\n\
122 -h, --help Display this help and exit\n\
124 Report bugs to %s\n", progname
, ZEBRA_BUG_ADDRESS
);
130 /* SIGHUP handler. */
134 zlog_info ("SIGHUP received");
137 zlog_info ("ripd restarting!");
139 /* Reload config file. */
140 vty_read_config (config_file
, config_default
);
142 /* Create VTY's socket */
143 vty_serv_sock (vty_addr
, vty_port
, RIP_VTYSH_PATH
);
145 /* Try to return to normal operation. */
148 /* SIGINT handler. */
152 zlog_notice ("Terminating on signal");
160 /* SIGUSR1 handler. */
167 static struct quagga_signal_t ripd_signals
[] =
187 /* Main routine of ripd. */
189 main (int argc
, char **argv
)
195 struct thread thread
;
197 /* Set umask before anything for security */
200 /* Get program name. */
201 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
203 /* First of all we need logging init. */
204 zlog_default
= openzlog (progname
, ZLOG_RIP
,
205 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
207 /* Command line option parse. */
212 opt
= getopt_long (argc
, argv
, "df:i:z:hA:P:u:g:rvC", longopts
, 0);
225 config_file
= optarg
;
234 zclient_serv_path_set (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 if (vty_port
<= 0 || vty_port
> 0xffff)
246 vty_port
= RIP_VTY_PORT
;
255 ripd_privs
.user
= optarg
;
258 ripd_privs
.group
= optarg
;
261 print_version (progname
);
273 /* Prepare master thread. */
274 master
= thread_master_create ();
276 /* Library initialization. */
277 zprivs_init (&ripd_privs
);
278 signal_init (master
, array_size(ripd_signals
), ripd_signals
);
284 /* RIP related initialization. */
290 /* Get configuration file. */
291 vty_read_config (config_file
, config_default
);
293 /* Start execution only if not in dry-run mode */
297 /* Change to the daemon program. */
298 if (daemon_mode
&& daemon (0, 0) < 0)
300 zlog_err("RIPd daemon failed: %s", strerror(errno
));
304 /* Pid file create. */
305 pid_output (pid_file
);
307 /* Create VTY's socket */
308 vty_serv_sock (vty_addr
, vty_port
, RIP_VTYSH_PATH
);
311 zlog_notice ("RIPd %s starting: vty@%d", QUAGGA_VERSION
, vty_port
);
313 /* Execute each thread. */
314 while (thread_fetch (master
, &thread
))
315 thread_call (&thread
);