]>
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" |
718e3744 | 31 | #include "thread.h" |
32 | #include "log.h" | |
33 | #include "prefix.h" | |
34 | #include "if.h" | |
edd7c245 | 35 | #include "privs.h" |
2d75d052 | 36 | #include "sigevent.h" |
718e3744 | 37 | |
38 | #include "ripngd/ripngd.h" | |
39 | ||
40 | /* Configuration filename and directory. */ | |
718e3744 | 41 | char config_default[] = SYSCONFDIR RIPNG_DEFAULT_CONFIG; |
a94434b6 | 42 | char *config_file = NULL; |
718e3744 | 43 | |
44 | /* RIPngd options. */ | |
45 | struct option longopts[] = | |
46 | { | |
47 | { "daemon", no_argument, NULL, 'd'}, | |
48 | { "config_file", required_argument, NULL, 'f'}, | |
49 | { "pid_file", required_argument, NULL, 'i'}, | |
50 | { "log_mode", no_argument, NULL, 'l'}, | |
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'}, | |
edd7c245 | 55 | { "user", required_argument, NULL, 'u'}, |
c065230a | 56 | { "group", required_argument, NULL, 'g'}, |
718e3744 | 57 | { "version", no_argument, NULL, 'v'}, |
58 | { 0 } | |
59 | }; | |
60 | ||
edd7c245 | 61 | /* ripngd privileges */ |
62 | zebra_capabilities_t _caps_p [] = | |
63 | { | |
64 | ZCAP_RAW, | |
65 | ZCAP_BIND | |
66 | }; | |
67 | ||
68 | struct zebra_privs_t ripngd_privs = | |
69 | { | |
d81fadfd | 70 | #if defined(QUAGGA_USER) |
71 | .user = QUAGGA_USER, | |
edd7c245 | 72 | #endif |
d81fadfd | 73 | #if defined QUAGGA_GROUP |
74 | .group = QUAGGA_GROUP, | |
75 | #endif | |
76 | #ifdef VTY_GROUP | |
77 | .vty_group = VTY_GROUP, | |
edd7c245 | 78 | #endif |
79 | .caps_p = _caps_p, | |
80 | .cap_num_p = 2, | |
81 | .cap_num_i = 0 | |
82 | }; | |
83 | ||
84 | ||
718e3744 | 85 | /* RIPngd program name */ |
86 | ||
87 | /* Route retain mode flag. */ | |
88 | int retain_mode = 0; | |
89 | ||
a94434b6 | 90 | /* RIPng VTY bind address. */ |
91 | char *vty_addr = NULL; | |
92 | ||
93 | /* RIPng VTY connection port. */ | |
94 | int vty_port = RIPNG_VTY_PORT; | |
95 | ||
718e3744 | 96 | /* Master of threads. */ |
97 | struct thread_master *master; | |
98 | ||
99 | /* Process ID saved for use by init system */ | |
7a1d583c | 100 | const char *pid_file = PATH_RIPNGD_PID; |
718e3744 | 101 | |
102 | /* Help information display. */ | |
103 | static void | |
104 | usage (char *progname, int status) | |
105 | { | |
106 | if (status != 0) | |
107 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); | |
108 | else | |
109 | { | |
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 | -l. --log_mode Set verbose log mode flag\n\ | |
116 | -A, --vty_addr Set vty's bind address\n\ | |
117 | -P, --vty_port Set vty's port number\n\ | |
118 | -r, --retain When program terminates, retain added route by ripngd.\n\ | |
c065230a | 119 | -u, --user User to run as\n\ |
120 | -g, --group Group to run as\n\ | |
718e3744 | 121 | -v, --version Print program version\n\ |
122 | -h, --help Display this help and exit\n\ | |
123 | \n\ | |
124 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); | |
125 | } | |
126 | exit (status); | |
127 | } | |
128 | \f | |
129 | /* SIGHUP handler. */ | |
130 | void | |
2d75d052 | 131 | sighup (void) |
718e3744 | 132 | { |
a94434b6 | 133 | zlog_info ("SIGHUP received"); |
134 | ripng_clean (); | |
135 | ripng_reset (); | |
a94434b6 | 136 | |
137 | /* Reload config file. */ | |
320ec10a | 138 | vty_read_config (config_file, config_default); |
a94434b6 | 139 | /* Create VTY's socket */ |
140 | vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH); | |
141 | ||
142 | /* Try to return to normal operation. */ | |
718e3744 | 143 | } |
144 | ||
145 | /* SIGINT handler. */ | |
146 | void | |
2d75d052 | 147 | sigint (void) |
718e3744 | 148 | { |
887c44a4 | 149 | zlog_notice ("Terminating on signal"); |
718e3744 | 150 | |
151 | if (! retain_mode) | |
a94434b6 | 152 | ripng_clean (); |
718e3744 | 153 | |
154 | exit (0); | |
155 | } | |
156 | ||
157 | /* SIGUSR1 handler. */ | |
158 | void | |
2d75d052 | 159 | sigusr1 (void) |
718e3744 | 160 | { |
161 | zlog_rotate (NULL); | |
162 | } | |
163 | ||
2d75d052 | 164 | struct quagga_signal_t ripng_signals[] = |
718e3744 | 165 | { |
2d75d052 | 166 | { |
167 | .signal = SIGHUP, | |
168 | .handler = &sighup, | |
169 | }, | |
170 | { | |
171 | .signal = SIGUSR1, | |
172 | .handler = &sigusr1, | |
173 | }, | |
174 | { | |
175 | .signal = SIGINT, | |
176 | .handler = &sigint, | |
177 | }, | |
f571dab0 | 178 | { |
179 | .signal = SIGTERM, | |
180 | .handler = &sigint, | |
181 | }, | |
2d75d052 | 182 | }; |
718e3744 | 183 | \f |
184 | /* RIPngd main routine. */ | |
185 | int | |
186 | main (int argc, char **argv) | |
187 | { | |
188 | char *p; | |
4fc4e7ab | 189 | int vty_port = RIPNG_VTY_PORT; |
718e3744 | 190 | int daemon_mode = 0; |
718e3744 | 191 | char *progname; |
192 | struct thread thread; | |
193 | ||
194 | /* Set umask before anything for security */ | |
195 | umask (0027); | |
196 | ||
197 | /* get program name */ | |
198 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); | |
199 | ||
274a4a44 | 200 | zlog_default = openzlog(progname, ZLOG_RIPNG, |
718e3744 | 201 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
202 | ||
203 | while (1) | |
204 | { | |
205 | int opt; | |
206 | ||
c065230a | 207 | opt = getopt_long (argc, argv, "dlf:i:hA:P:u:g:v", longopts, 0); |
718e3744 | 208 | |
209 | if (opt == EOF) | |
210 | break; | |
211 | ||
212 | switch (opt) | |
213 | { | |
214 | case 0: | |
215 | break; | |
216 | case 'd': | |
217 | daemon_mode = 1; | |
218 | break; | |
219 | case 'l': | |
220 | /* log_mode = 1; */ | |
221 | break; | |
222 | case 'f': | |
223 | config_file = optarg; | |
224 | break; | |
225 | case 'A': | |
226 | vty_addr = optarg; | |
227 | break; | |
228 | case 'i': | |
229 | pid_file = optarg; | |
4fc4e7ab | 230 | break; |
718e3744 | 231 | case 'P': |
4fc4e7ab | 232 | /* Deal with atoi() returning 0 on failure, and ripngd not |
233 | listening on ripngd port... */ | |
234 | if (strcmp(optarg, "0") == 0) | |
235 | { | |
236 | vty_port = 0; | |
237 | break; | |
238 | } | |
239 | vty_port = atoi (optarg); | |
240 | vty_port = (vty_port ? vty_port : RIPNG_VTY_PORT); | |
241 | break; | |
718e3744 | 242 | case 'r': |
243 | retain_mode = 1; | |
244 | break; | |
c065230a | 245 | case 'u': |
246 | ripngd_privs.user = optarg; | |
247 | break; | |
248 | case 'g': | |
249 | ripngd_privs.group = optarg; | |
250 | break; | |
718e3744 | 251 | case 'v': |
252 | print_version (progname); | |
253 | exit (0); | |
254 | break; | |
255 | case 'h': | |
256 | usage (progname, 0); | |
257 | break; | |
258 | default: | |
259 | usage (progname, 1); | |
260 | break; | |
261 | } | |
262 | } | |
263 | ||
264 | master = thread_master_create (); | |
265 | ||
266 | /* Library inits. */ | |
edd7c245 | 267 | zprivs_init (&ripngd_privs); |
2d75d052 | 268 | signal_init (master, Q_SIGC(ripng_signals), ripng_signals); |
718e3744 | 269 | cmd_init (1); |
b21b19c5 | 270 | vty_init (master); |
a94434b6 | 271 | memory_init (); |
718e3744 | 272 | |
273 | /* RIPngd inits. */ | |
274 | ripng_init (); | |
275 | zebra_init (); | |
a94434b6 | 276 | ripng_peer_init (); |
277 | ||
278 | /* Sort all installed commands. */ | |
718e3744 | 279 | sort_node (); |
280 | ||
281 | /* Get configuration file. */ | |
320ec10a | 282 | vty_read_config (config_file, config_default); |
718e3744 | 283 | |
284 | /* Change to the daemon program. */ | |
285 | if (daemon_mode) | |
286 | daemon (0, 0); | |
287 | ||
288 | /* Create VTY socket */ | |
4fc4e7ab | 289 | vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH); |
718e3744 | 290 | |
291 | /* Process id file create. */ | |
292 | pid_output (pid_file); | |
293 | ||
887c44a4 | 294 | /* Print banner. */ |
295 | zlog_notice ("RIPNGd %s starting: vty@%d", QUAGGA_VERSION, vty_port); | |
296 | ||
718e3744 | 297 | /* Fetch next active thread. */ |
298 | while (thread_fetch (master, &thread)) | |
299 | thread_call (&thread); | |
300 | ||
301 | /* Not reached. */ | |
302 | exit (0); | |
303 | } |