]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
2 | * RIPngd main routine. | |
3 | * Copyright (C) 1998, 1999 Kunihiro Ishiguro | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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. | |
21 | */ | |
22 | ||
23 | #include <zebra.h> | |
24 | ||
5e4fa164 | 25 | #include <lib/version.h> |
718e3744 | 26 | #include "getopt.h" |
27 | #include "vector.h" | |
28 | #include "vty.h" | |
29 | #include "command.h" | |
a94434b6 | 30 | #include "memory.h" |
fc7948fa | 31 | #include "memory_vty.h" |
718e3744 | 32 | #include "thread.h" |
33 | #include "log.h" | |
34 | #include "prefix.h" | |
35 | #include "if.h" | |
edd7c245 | 36 | #include "privs.h" |
2d75d052 | 37 | #include "sigevent.h" |
6a69b354 | 38 | #include "vrf.h" |
718e3744 | 39 | |
40 | #include "ripngd/ripngd.h" | |
41 | ||
42 | /* Configuration filename and directory. */ | |
718e3744 | 43 | char config_default[] = SYSCONFDIR RIPNG_DEFAULT_CONFIG; |
a94434b6 | 44 | char *config_file = NULL; |
718e3744 | 45 | |
46 | /* RIPngd options. */ | |
47 | struct option longopts[] = | |
48 | { | |
49 | { "daemon", no_argument, NULL, 'd'}, | |
50 | { "config_file", required_argument, NULL, 'f'}, | |
51 | { "pid_file", required_argument, NULL, 'i'}, | |
b5114685 | 52 | { "socket", required_argument, NULL, 'z'}, |
876b8be0 | 53 | { "dryrun", no_argument, NULL, 'C'}, |
718e3744 | 54 | { "help", no_argument, NULL, 'h'}, |
55 | { "vty_addr", required_argument, NULL, 'A'}, | |
56 | { "vty_port", required_argument, NULL, 'P'}, | |
57 | { "retain", no_argument, NULL, 'r'}, | |
edd7c245 | 58 | { "user", required_argument, NULL, 'u'}, |
c065230a | 59 | { "group", required_argument, NULL, 'g'}, |
718e3744 | 60 | { "version", no_argument, NULL, 'v'}, |
61 | { 0 } | |
62 | }; | |
63 | ||
edd7c245 | 64 | /* ripngd privileges */ |
65 | zebra_capabilities_t _caps_p [] = | |
66 | { | |
ceacedba | 67 | ZCAP_NET_RAW, |
edd7c245 | 68 | ZCAP_BIND |
69 | }; | |
70 | ||
71 | struct zebra_privs_t ripngd_privs = | |
72 | { | |
b2f36157 DL |
73 | #if defined(FRR_USER) |
74 | .user = FRR_USER, | |
edd7c245 | 75 | #endif |
b2f36157 DL |
76 | #if defined FRR_GROUP |
77 | .group = FRR_GROUP, | |
d81fadfd | 78 | #endif |
79 | #ifdef VTY_GROUP | |
80 | .vty_group = VTY_GROUP, | |
edd7c245 | 81 | #endif |
82 | .caps_p = _caps_p, | |
83 | .cap_num_p = 2, | |
84 | .cap_num_i = 0 | |
85 | }; | |
86 | ||
87 | ||
718e3744 | 88 | /* RIPngd program name */ |
89 | ||
90 | /* Route retain mode flag. */ | |
91 | int retain_mode = 0; | |
92 | ||
a94434b6 | 93 | /* RIPng VTY bind address. */ |
94 | char *vty_addr = NULL; | |
95 | ||
96 | /* RIPng VTY connection port. */ | |
97 | int vty_port = RIPNG_VTY_PORT; | |
98 | ||
718e3744 | 99 | /* Master of threads. */ |
100 | struct thread_master *master; | |
101 | ||
102 | /* Process ID saved for use by init system */ | |
7a1d583c | 103 | const char *pid_file = PATH_RIPNGD_PID; |
718e3744 | 104 | |
105 | /* Help information display. */ | |
106 | static void | |
107 | usage (char *progname, int status) | |
108 | { | |
109 | if (status != 0) | |
110 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); | |
111 | else | |
112 | { | |
113 | printf ("Usage : %s [OPTION...]\n\ | |
114 | Daemon which manages RIPng.\n\n\ | |
115 | -d, --daemon Runs in daemon mode\n\ | |
116 | -f, --config_file Set configuration file name\n\ | |
117 | -i, --pid_file Set process identifier file name\n\ | |
b5114685 | 118 | -z, --socket Set path of zebra socket\n\ |
718e3744 | 119 | -A, --vty_addr Set vty's bind address\n\ |
120 | -P, --vty_port Set vty's port number\n\ | |
121 | -r, --retain When program terminates, retain added route by ripngd.\n\ | |
c065230a | 122 | -u, --user User to run as\n\ |
123 | -g, --group Group to run as\n\ | |
718e3744 | 124 | -v, --version Print program version\n\ |
876b8be0 | 125 | -C, --dryrun Check configuration for validity and exit\n\ |
718e3744 | 126 | -h, --help Display this help and exit\n\ |
127 | \n\ | |
b2f36157 | 128 | Report bugs to %s\n", progname, FRR_BUG_ADDRESS); |
718e3744 | 129 | } |
130 | exit (status); | |
131 | } | |
6b0655a2 | 132 | |
718e3744 | 133 | /* SIGHUP handler. */ |
6ac29a51 | 134 | static void |
2d75d052 | 135 | sighup (void) |
718e3744 | 136 | { |
a94434b6 | 137 | zlog_info ("SIGHUP received"); |
138 | ripng_clean (); | |
139 | ripng_reset (); | |
a94434b6 | 140 | |
141 | /* Reload config file. */ | |
320ec10a | 142 | vty_read_config (config_file, config_default); |
a94434b6 | 143 | /* Create VTY's socket */ |
144 | vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH); | |
145 | ||
146 | /* Try to return to normal operation. */ | |
718e3744 | 147 | } |
148 | ||
149 | /* SIGINT handler. */ | |
6ac29a51 | 150 | static void |
2d75d052 | 151 | sigint (void) |
718e3744 | 152 | { |
887c44a4 | 153 | zlog_notice ("Terminating on signal"); |
718e3744 | 154 | |
155 | if (! retain_mode) | |
a94434b6 | 156 | ripng_clean (); |
718e3744 | 157 | |
158 | exit (0); | |
159 | } | |
160 | ||
161 | /* SIGUSR1 handler. */ | |
6ac29a51 | 162 | static void |
2d75d052 | 163 | sigusr1 (void) |
718e3744 | 164 | { |
165 | zlog_rotate (NULL); | |
166 | } | |
167 | ||
2d75d052 | 168 | struct quagga_signal_t ripng_signals[] = |
718e3744 | 169 | { |
2d75d052 | 170 | { |
171 | .signal = SIGHUP, | |
172 | .handler = &sighup, | |
173 | }, | |
174 | { | |
175 | .signal = SIGUSR1, | |
176 | .handler = &sigusr1, | |
177 | }, | |
178 | { | |
179 | .signal = SIGINT, | |
180 | .handler = &sigint, | |
181 | }, | |
f571dab0 | 182 | { |
183 | .signal = SIGTERM, | |
184 | .handler = &sigint, | |
185 | }, | |
2d75d052 | 186 | }; |
6b0655a2 | 187 | |
718e3744 | 188 | /* RIPngd main routine. */ |
189 | int | |
190 | main (int argc, char **argv) | |
191 | { | |
192 | char *p; | |
4fc4e7ab | 193 | int vty_port = RIPNG_VTY_PORT; |
718e3744 | 194 | int daemon_mode = 0; |
718e3744 | 195 | char *progname; |
196 | struct thread thread; | |
876b8be0 | 197 | int dryrun = 0; |
718e3744 | 198 | |
199 | /* Set umask before anything for security */ | |
200 | umask (0027); | |
201 | ||
202 | /* get program name */ | |
203 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); | |
204 | ||
7c8ff89e | 205 | zlog_default = openzlog(progname, ZLOG_RIPNG, 0, |
718e3744 | 206 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
2caa9b39 | 207 | zprivs_init (&ripngd_privs); |
c05795b1 SK |
208 | #if defined(HAVE_CUMULUS) |
209 | zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); | |
210 | #endif | |
718e3744 | 211 | |
212 | while (1) | |
213 | { | |
214 | int opt; | |
215 | ||
b5114685 | 216 | opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:vC", longopts, 0); |
718e3744 | 217 | |
218 | if (opt == EOF) | |
219 | break; | |
220 | ||
221 | switch (opt) | |
222 | { | |
223 | case 0: | |
224 | break; | |
225 | case 'd': | |
226 | daemon_mode = 1; | |
227 | break; | |
718e3744 | 228 | case 'f': |
229 | config_file = optarg; | |
230 | break; | |
231 | case 'A': | |
232 | vty_addr = optarg; | |
233 | break; | |
234 | case 'i': | |
235 | pid_file = optarg; | |
b5114685 VT |
236 | break; |
237 | case 'z': | |
238 | zclient_serv_path_set (optarg); | |
239 | break; | |
718e3744 | 240 | case 'P': |
4fc4e7ab | 241 | /* Deal with atoi() returning 0 on failure, and ripngd not |
242 | listening on ripngd port... */ | |
243 | if (strcmp(optarg, "0") == 0) | |
244 | { | |
245 | vty_port = 0; | |
246 | break; | |
247 | } | |
248 | vty_port = atoi (optarg); | |
0d6b2ee2 PJ |
249 | if (vty_port <= 0 || vty_port > 0xffff) |
250 | vty_port = RIPNG_VTY_PORT; | |
4fc4e7ab | 251 | break; |
718e3744 | 252 | case 'r': |
253 | retain_mode = 1; | |
254 | break; | |
c065230a | 255 | case 'u': |
256 | ripngd_privs.user = optarg; | |
257 | break; | |
258 | case 'g': | |
259 | ripngd_privs.group = optarg; | |
260 | break; | |
718e3744 | 261 | case 'v': |
262 | print_version (progname); | |
263 | exit (0); | |
264 | break; | |
876b8be0 PJ |
265 | case 'C': |
266 | dryrun = 1; | |
267 | break; | |
718e3744 | 268 | case 'h': |
269 | usage (progname, 0); | |
270 | break; | |
271 | default: | |
272 | usage (progname, 1); | |
273 | break; | |
274 | } | |
275 | } | |
276 | ||
277 | master = thread_master_create (); | |
278 | ||
279 | /* Library inits. */ | |
837d16cc | 280 | signal_init (master, array_size(ripng_signals), ripng_signals); |
718e3744 | 281 | cmd_init (1); |
b21b19c5 | 282 | vty_init (master); |
a94434b6 | 283 | memory_init (); |
6a69b354 | 284 | vrf_init (); |
718e3744 | 285 | |
286 | /* RIPngd inits. */ | |
287 | ripng_init (); | |
4140ca4d | 288 | zebra_init(master); |
a94434b6 | 289 | ripng_peer_init (); |
290 | ||
718e3744 | 291 | /* Get configuration file. */ |
320ec10a | 292 | vty_read_config (config_file, config_default); |
718e3744 | 293 | |
876b8be0 PJ |
294 | /* Start execution only if not in dry-run mode */ |
295 | if(dryrun) | |
296 | return(0); | |
297 | ||
718e3744 | 298 | /* Change to the daemon program. */ |
065de903 SH |
299 | if (daemon_mode && daemon (0, 0) < 0) |
300 | { | |
301 | zlog_err("RIPNGd daemon failed: %s", strerror(errno)); | |
302 | exit (1); | |
303 | } | |
718e3744 | 304 | |
305 | /* Create VTY socket */ | |
4fc4e7ab | 306 | vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH); |
718e3744 | 307 | |
308 | /* Process id file create. */ | |
309 | pid_output (pid_file); | |
310 | ||
887c44a4 | 311 | /* Print banner. */ |
b2f36157 | 312 | zlog_notice ("RIPNGd %s starting: vty@%d", FRR_VERSION, vty_port); |
887c44a4 | 313 | |
718e3744 | 314 | /* Fetch next active thread. */ |
315 | while (thread_fetch (master, &thread)) | |
316 | thread_call (&thread); | |
317 | ||
318 | /* Not reached. */ | |
e8e1946e | 319 | return 0; |
718e3744 | 320 | } |