all: check return value from daemon() call
[jleu-quagga.git] / ripngd / ripng_main.c
blob85209a15842432085cf79296d545f8458dac991a
1 /*
2 * RIPngd main routine.
3 * Copyright (C) 1998, 1999 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
23 #include <zebra.h>
25 #include <lib/version.h>
26 #include "getopt.h"
27 #include "vector.h"
28 #include "vty.h"
29 #include "command.h"
30 #include "memory.h"
31 #include "thread.h"
32 #include "log.h"
33 #include "prefix.h"
34 #include "if.h"
35 #include "privs.h"
36 #include "sigevent.h"
38 #include "ripngd/ripngd.h"
40 /* Configuration filename and directory. */
41 char config_default[] = SYSCONFDIR RIPNG_DEFAULT_CONFIG;
42 char *config_file = NULL;
44 /* RIPngd options. */
45 struct option longopts[] =
47 { "daemon", no_argument, NULL, 'd'},
48 { "config_file", required_argument, NULL, 'f'},
49 { "pid_file", required_argument, NULL, 'i'},
50 { "dryrun", no_argument, NULL, 'C'},
51 { "help", no_argument, NULL, 'h'},
52 { "vty_addr", required_argument, NULL, 'A'},
53 { "vty_port", required_argument, NULL, 'P'},
54 { "retain", no_argument, NULL, 'r'},
55 { "user", required_argument, NULL, 'u'},
56 { "group", required_argument, NULL, 'g'},
57 { "version", no_argument, NULL, 'v'},
58 { 0 }
61 /* ripngd privileges */
62 zebra_capabilities_t _caps_p [] =
64 ZCAP_NET_RAW,
65 ZCAP_BIND
68 struct zebra_privs_t ripngd_privs =
70 #if defined(QUAGGA_USER)
71 .user = QUAGGA_USER,
72 #endif
73 #if defined QUAGGA_GROUP
74 .group = QUAGGA_GROUP,
75 #endif
76 #ifdef VTY_GROUP
77 .vty_group = VTY_GROUP,
78 #endif
79 .caps_p = _caps_p,
80 .cap_num_p = 2,
81 .cap_num_i = 0
85 /* RIPngd program name */
87 /* Route retain mode flag. */
88 int retain_mode = 0;
90 /* RIPng VTY bind address. */
91 char *vty_addr = NULL;
93 /* RIPng VTY connection port. */
94 int vty_port = RIPNG_VTY_PORT;
96 /* Master of threads. */
97 struct thread_master *master;
99 /* Process ID saved for use by init system */
100 const char *pid_file = PATH_RIPNGD_PID;
102 /* Help information display. */
103 static void
104 usage (char *progname, int status)
106 if (status != 0)
107 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
108 else
110 printf ("Usage : %s [OPTION...]\n\
111 Daemon which manages RIPng.\n\n\
112 -d, --daemon Runs in daemon mode\n\
113 -f, --config_file Set configuration file name\n\
114 -i, --pid_file Set process identifier file name\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 ripngd.\n\
118 -u, --user User to run as\n\
119 -g, --group Group to run as\n\
120 -v, --version Print program version\n\
121 -C, --dryrun Check configuration for validity and exit\n\
122 -h, --help Display this help and exit\n\
124 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
126 exit (status);
129 /* SIGHUP handler. */
130 static void
131 sighup (void)
133 zlog_info ("SIGHUP received");
134 ripng_clean ();
135 ripng_reset ();
137 /* Reload config file. */
138 vty_read_config (config_file, config_default);
139 /* Create VTY's socket */
140 vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
142 /* Try to return to normal operation. */
145 /* SIGINT handler. */
146 static void
147 sigint (void)
149 zlog_notice ("Terminating on signal");
151 if (! retain_mode)
152 ripng_clean ();
154 exit (0);
157 /* SIGUSR1 handler. */
158 static void
159 sigusr1 (void)
161 zlog_rotate (NULL);
164 struct quagga_signal_t ripng_signals[] =
167 .signal = SIGHUP,
168 .handler = &sighup,
171 .signal = SIGUSR1,
172 .handler = &sigusr1,
175 .signal = SIGINT,
176 .handler = &sigint,
179 .signal = SIGTERM,
180 .handler = &sigint,
184 /* RIPngd main routine. */
186 main (int argc, char **argv)
188 char *p;
189 int vty_port = RIPNG_VTY_PORT;
190 int daemon_mode = 0;
191 char *progname;
192 struct thread thread;
193 int dryrun = 0;
195 /* Set umask before anything for security */
196 umask (0027);
198 /* get program name */
199 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
201 zlog_default = openzlog(progname, ZLOG_RIPNG,
202 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
204 while (1)
206 int opt;
208 opt = getopt_long (argc, argv, "df:i:hA:P:u:g:vC", longopts, 0);
210 if (opt == EOF)
211 break;
213 switch (opt)
215 case 0:
216 break;
217 case 'd':
218 daemon_mode = 1;
219 break;
220 case 'f':
221 config_file = optarg;
222 break;
223 case 'A':
224 vty_addr = optarg;
225 break;
226 case 'i':
227 pid_file = optarg;
228 break;
229 case 'P':
230 /* Deal with atoi() returning 0 on failure, and ripngd not
231 listening on ripngd port... */
232 if (strcmp(optarg, "0") == 0)
234 vty_port = 0;
235 break;
237 vty_port = atoi (optarg);
238 if (vty_port <= 0 || vty_port > 0xffff)
239 vty_port = RIPNG_VTY_PORT;
240 break;
241 case 'r':
242 retain_mode = 1;
243 break;
244 case 'u':
245 ripngd_privs.user = optarg;
246 break;
247 case 'g':
248 ripngd_privs.group = optarg;
249 break;
250 case 'v':
251 print_version (progname);
252 exit (0);
253 break;
254 case 'C':
255 dryrun = 1;
256 break;
257 case 'h':
258 usage (progname, 0);
259 break;
260 default:
261 usage (progname, 1);
262 break;
266 master = thread_master_create ();
268 /* Library inits. */
269 zprivs_init (&ripngd_privs);
270 signal_init (master, Q_SIGC(ripng_signals), ripng_signals);
271 cmd_init (1);
272 vty_init (master);
273 memory_init ();
275 /* RIPngd inits. */
276 ripng_init ();
277 zebra_init ();
278 ripng_peer_init ();
280 /* Sort all installed commands. */
281 sort_node ();
283 /* Get configuration file. */
284 vty_read_config (config_file, config_default);
286 /* Start execution only if not in dry-run mode */
287 if(dryrun)
288 return(0);
290 /* Change to the daemon program. */
291 if (daemon_mode && daemon (0, 0) < 0)
293 zlog_err("RIPNGd daemon failed: %s", strerror(errno));
294 exit (1);
297 /* Create VTY socket */
298 vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
300 /* Process id file create. */
301 pid_output (pid_file);
303 /* Print banner. */
304 zlog_notice ("RIPNGd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
306 /* Fetch next active thread. */
307 while (thread_fetch (master, &thread))
308 thread_call (&thread);
310 /* Not reached. */
311 return 0;