]> git.proxmox.com Git - mirror_frr.git/blob - ripd/rip_main.c
*: add frr_init() infrastructure
[mirror_frr.git] / ripd / rip_main.c
1 /* RIPd main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro <kunihiro@zebra.org>
3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
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
19 * 02111-1307, USA.
20 */
21
22 #include <zebra.h>
23
24 #include <lib/version.h>
25 #include "getopt.h"
26 #include "thread.h"
27 #include "command.h"
28 #include "memory.h"
29 #include "memory_vty.h"
30 #include "prefix.h"
31 #include "filter.h"
32 #include "keychain.h"
33 #include "log.h"
34 #include "privs.h"
35 #include "sigevent.h"
36 #include "zclient.h"
37 #include "vrf.h"
38 #include "libfrr.h"
39
40 #include "ripd/ripd.h"
41
42 /* ripd options. */
43 static struct option longopts[] =
44 {
45 { "daemon", no_argument, NULL, 'd'},
46 { "config_file", required_argument, NULL, 'f'},
47 { "pid_file", required_argument, NULL, 'i'},
48 { "socket", required_argument, NULL, 'z'},
49 { "dryrun", no_argument, NULL, 'C'},
50 { "retain", no_argument, NULL, 'r'},
51 { 0 }
52 };
53
54 /* ripd privileges */
55 zebra_capabilities_t _caps_p [] =
56 {
57 ZCAP_NET_RAW,
58 ZCAP_BIND
59 };
60
61 struct zebra_privs_t ripd_privs =
62 {
63 #if defined(FRR_USER)
64 .user = FRR_USER,
65 #endif
66 #if defined FRR_GROUP
67 .group = FRR_GROUP,
68 #endif
69 #ifdef VTY_GROUP
70 .vty_group = VTY_GROUP,
71 #endif
72 .caps_p = _caps_p,
73 .cap_num_p = 2,
74 .cap_num_i = 0
75 };
76
77 /* Configuration file and directory. */
78 char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG;
79 char *config_file = NULL;
80
81 /* ripd program name */
82
83 /* VTY Socket prefix */
84 char vty_sock_path[MAXPATHLEN] = RIP_VTYSH_PATH;
85
86 /* Route retain mode flag. */
87 int retain_mode = 0;
88
89 /* Master of threads. */
90 struct thread_master *master;
91
92 /* Process ID saved for use by init system */
93 const char *pid_file = PATH_RIPD_PID;
94
95 /* SIGHUP handler. */
96 static void
97 sighup (void)
98 {
99 zlog_info ("SIGHUP received");
100 rip_clean ();
101 rip_reset ();
102 zlog_info ("ripd restarting!");
103
104 /* Reload config file. */
105 vty_read_config (config_file, config_default);
106
107 /* Try to return to normal operation. */
108 }
109
110 /* SIGINT handler. */
111 static void
112 sigint (void)
113 {
114 zlog_notice ("Terminating on signal");
115
116 if (! retain_mode)
117 rip_clean ();
118
119 exit (0);
120 }
121
122 /* SIGUSR1 handler. */
123 static void
124 sigusr1 (void)
125 {
126 zlog_rotate (NULL);
127 }
128
129 static struct quagga_signal_t ripd_signals[] =
130 {
131 {
132 .signal = SIGHUP,
133 .handler = &sighup,
134 },
135 {
136 .signal = SIGUSR1,
137 .handler = &sigusr1,
138 },
139 {
140 .signal = SIGINT,
141 .handler = &sigint,
142 },
143 {
144 .signal = SIGTERM,
145 .handler = &sigint,
146 },
147 };
148
149 FRR_DAEMON_INFO(ripd, RIP,
150 .vty_port = RIP_VTY_PORT,
151
152 .proghelp = "Implementation of the RIP routing protocol.",
153
154 .signals = ripd_signals,
155 .n_signals = array_size(ripd_signals),
156
157 .privs = &ripd_privs,
158 )
159
160 /* Main routine of ripd. */
161 int
162 main (int argc, char **argv)
163 {
164 int daemon_mode = 0;
165 int dryrun = 0;
166 struct thread thread;
167
168 frr_preinit (&ripd_di, argc, argv);
169 frr_opt_add ("df:i:z:rC", longopts,
170 " -d, --daemon Runs in daemon mode\n"
171 " -f, --config_file Set configuration file name\n"
172 " -i, --pid_file Set process identifier file name\n"
173 " -z, --socket Set path of zebra socket\n"
174 " -C, --dryrun Check configuration for validity and exit\n"
175 " -r, --retain When program terminates, retain added route by ripd.\n");
176
177 /* Command line option parse. */
178 while (1)
179 {
180 int opt;
181
182 opt = frr_getopt (argc, argv, NULL);
183
184 if (opt == EOF)
185 break;
186
187 switch (opt)
188 {
189 case 0:
190 break;
191 case 'd':
192 daemon_mode = 1;
193 break;
194 case 'f':
195 config_file = optarg;
196 break;
197 case 'i':
198 pid_file = optarg;
199 break;
200 case 'z':
201 zclient_serv_path_set (optarg);
202 break;
203 case 'r':
204 retain_mode = 1;
205 break;
206 case 'C':
207 dryrun = 1;
208 break;
209 default:
210 frr_help_exit (1);
211 break;
212 }
213 }
214
215 /* Prepare master thread. */
216 master = frr_init ();
217
218 /* Library initialization. */
219 cmd_init (1);
220 vty_init (master);
221 memory_init ();
222 keychain_init ();
223 vrf_init ();
224
225 /* RIP related initialization. */
226 rip_init ();
227 rip_if_init ();
228 rip_zclient_init(master);
229 rip_peer_init ();
230
231 /* Get configuration file. */
232 vty_read_config (config_file, config_default);
233
234 /* Start execution only if not in dry-run mode */
235 if(dryrun)
236 return (0);
237
238 /* Change to the daemon program. */
239 if (daemon_mode && daemon (0, 0) < 0)
240 {
241 zlog_err("RIPd daemon failed: %s", strerror(errno));
242 exit (1);
243 }
244
245 /* Pid file create. */
246 pid_output (pid_file);
247
248 /* Create VTY's socket */
249 frr_vty_serv (RIP_VTYSH_PATH);
250
251 /* Print banner. */
252 zlog_notice ("RIPd %s starting: vty@%d", FRR_VERSION, ripd_di.vty_port);
253
254 /* Execute each thread. */
255 while (thread_fetch (master, &thread))
256 thread_call (&thread);
257
258 /* Not reached. */
259 return (0);
260 }