Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.sbin / rbootd / rbootd.c
blobe1d0fcc062439fb91f523d39c8553e4da45cc0bc
1 /* $NetBSD: rbootd.c,v 1.20 2007/02/21 20:57:55 hubertf Exp $ */
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * the Center for Software Science of the University of Utah Computer
9 * Science Department. CSS requests users of this software to return
10 * to css-dist@cs.utah.edu any improvements that they make and grant
11 * CSS redistribution rights.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
37 * from: @(#)rbootd.c 8.1 (Berkeley) 6/4/93
39 * From: Utah Hdr: rbootd.c 3.1 92/07/06
40 * Author: Jeff Forys, University of Utah CSS
44 * Copyright (c) 1988, 1992 The University of Utah and the Center
45 * for Software Science (CSS).
47 * This code is derived from software contributed to Berkeley by
48 * the Center for Software Science of the University of Utah Computer
49 * Science Department. CSS requests users of this software to return
50 * to css-dist@cs.utah.edu any improvements that they make and grant
51 * CSS redistribution rights.
53 * Redistribution and use in source and binary forms, with or without
54 * modification, are permitted provided that the following conditions
55 * are met:
56 * 1. Redistributions of source code must retain the above copyright
57 * notice, this list of conditions and the following disclaimer.
58 * 2. Redistributions in binary form must reproduce the above copyright
59 * notice, this list of conditions and the following disclaimer in the
60 * documentation and/or other materials provided with the distribution.
61 * 3. All advertising materials mentioning features or use of this software
62 * must display the following acknowledgement:
63 * This product includes software developed by the University of
64 * California, Berkeley and its contributors.
65 * 4. Neither the name of the University nor the names of its contributors
66 * may be used to endorse or promote products derived from this software
67 * without specific prior written permission.
69 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
70 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
71 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
72 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
73 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
74 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
75 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
76 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
77 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
78 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
79 * SUCH DAMAGE.
81 * from: @(#)rbootd.c 8.1 (Berkeley) 6/4/93
83 * From: Utah Hdr: rbootd.c 3.1 92/07/06
84 * Author: Jeff Forys, University of Utah CSS
87 #include <sys/cdefs.h>
88 #ifndef lint
89 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
90 The Regents of the University of California. All rights reserved.");
91 #endif /* not lint */
93 #ifndef lint
94 #if 0
95 static char sccsid[] = "@(#)rbootd.c 8.1 (Berkeley) 6/4/93";
96 #else
97 __RCSID("$NetBSD: rbootd.c,v 1.20 2007/02/21 20:57:55 hubertf Exp $");
98 #endif
99 #endif /* not lint */
101 #include <sys/param.h>
102 #include <sys/time.h>
103 #include <poll.h>
104 #include <err.h>
105 #include <errno.h>
106 #include <fcntl.h>
107 #include <signal.h>
108 #include <stdio.h>
109 #include <stdlib.h>
110 #include <string.h>
111 #include <syslog.h>
112 #include <unistd.h>
113 #include <util.h>
114 #include "defs.h"
116 int main (int, char *[]);
119 main(int argc, char *argv[])
121 int c, fd, omask;
122 struct pollfd set[1];
125 * Close any open file descriptors.
126 * Temporarily leave stdin & stdout open for `-d',
127 * and stderr open for any pre-syslog error messages.
130 int i, nfds = getdtablesize();
132 for (i = 0; i < nfds; i++)
133 if (i != STDIN_FILENO && i != STDOUT_FILENO &&
134 i != STDERR_FILENO)
135 (void) close(i);
139 * Parse any arguments.
141 while ((c = getopt(argc, argv, "adi:")) != -1)
142 switch(c) {
143 case 'a':
144 BootAny++;
145 break;
146 case 'd':
147 DebugFlg++;
148 break;
149 case 'i':
150 IntfName = optarg;
151 break;
153 for (; optind < argc; optind++) {
154 if (ConfigFile == NULL)
155 ConfigFile = argv[optind];
156 else {
157 warnx("too many config files (`%s' ignored)",
158 argv[optind]);
162 if (ConfigFile == NULL) /* use default config file */
163 ConfigFile = DfltConfig;
165 if (DebugFlg) {
166 DbgFp = stdout; /* output to stdout */
168 (void) signal(SIGUSR1, SIG_IGN); /* dont muck w/DbgFp */
169 (void) signal(SIGUSR2, SIG_IGN);
170 (void) fclose(stderr); /* finished with it */
171 } else {
172 if (daemon(0, 0))
173 err(1, "can't detach from terminal");
174 pidfile(NULL);
176 (void) signal(SIGUSR1, DebugOn);
177 (void) signal(SIGUSR2, DebugOff);
180 openlog("rbootd", LOG_PID, LOG_DAEMON);
183 * If no interface was specified, get one now.
185 * This is convoluted because we want to get the default interface
186 * name for the syslog("restarted") message. If BpfGetIntfName()
187 * runs into an error, it will return a syslog-able error message
188 * (in `errmsg') which will be displayed here.
190 if (IntfName == NULL) {
191 char *errmsg;
193 if ((IntfName = BpfGetIntfName(&errmsg)) == NULL) {
194 /* backslash to avoid trigraph ??) */
195 syslog(LOG_NOTICE, "restarted (?\?)");
196 syslog(LOG_ERR, errmsg);
197 Exit(0);
201 syslog(LOG_NOTICE, "restarted (%s)", IntfName);
203 (void) signal(SIGHUP, ReConfig);
204 (void) signal(SIGINT, Exit);
205 (void) signal(SIGTERM, Exit);
208 * Grab our host name and pid.
210 if (gethostname(MyHost, sizeof MyHost) < 0) {
211 syslog(LOG_ERR, "gethostname: %m");
212 Exit(0);
214 MyHost[sizeof(MyHost) - 1] = '\0';
217 * All boot files are relative to the boot directory, we might
218 * as well chdir() there to make life easier.
220 if (chdir(BootDir) < 0) {
221 syslog(LOG_ERR, "chdir: %m (%s)", BootDir);
222 Exit(0);
226 * Initial configuration.
228 omask = sigblock(sigmask(SIGHUP)); /* prevent reconfig's */
229 if (GetBootFiles() == 0) /* get list of boot files */
230 Exit(0);
231 if (ParseConfig() == 0) /* parse config file */
232 Exit(0);
235 * Open and initialize a BPF device for the appropriate interface.
236 * If an error is encountered, a message is displayed and Exit()
237 * is called.
239 fd = BpfOpen();
241 (void) sigsetmask(omask); /* allow reconfig's */
244 * Main loop: receive a packet, determine where it came from,
245 * and if we service this host, call routine to handle request.
247 set[0].fd = fd;
248 set[0].events = POLLIN;
249 for (;;) {
250 int nsel;
252 nsel = poll(set, 1, RmpConns ? RMP_TIMEOUT * 1000 : INFTIM);
254 if (nsel < 0) {
255 if (errno == EINTR)
256 continue;
257 syslog(LOG_ERR, "poll: %m");
258 Exit(0);
259 } else if (nsel == 0) { /* timeout */
260 DoTimeout(); /* clear stale conns */
261 continue;
264 if (set[0].revents & POLLIN) {
265 RMPCONN rconn;
266 CLIENT *client;
267 int doread = 1;
269 while (BpfRead(&rconn, doread)) {
270 doread = 0;
272 if (DbgFp != NULL) /* display packet */
273 DispPkt(&rconn,DIR_RCVD);
275 omask = sigblock(sigmask(SIGHUP));
278 * If we do not restrict service, set the
279 * client to NULL (ProcessPacket() handles
280 * this). Otherwise, check that we can
281 * service this host; if not, log a message
282 * and ignore the packet.
284 if (BootAny) {
285 client = NULL;
286 } else if ((client=FindClient(&rconn))==NULL) {
287 syslog(LOG_INFO,
288 "%s: boot packet ignored",
289 EnetStr(&rconn));
290 (void) sigsetmask(omask);
291 continue;
294 ProcessPacket(&rconn,client);
296 (void) sigsetmask(omask);
303 ** DoTimeout -- Free any connections that have timed out.
305 ** Parameters:
306 ** None.
308 ** Returns:
309 ** Nothing.
311 ** Side Effects:
312 ** - Timed out connections in `RmpConns' will be freed.
314 void
315 DoTimeout(void)
317 RMPCONN *rtmp;
318 struct timeval now;
320 (void) gettimeofday(&now, (struct timezone *)0);
323 * For each active connection, if RMP_TIMEOUT seconds have passed
324 * since the last packet was sent, delete the connection.
326 for (rtmp = RmpConns; rtmp != NULL; rtmp = rtmp->next)
327 if ((rtmp->tstamp.tv_sec + RMP_TIMEOUT) < now.tv_sec) {
328 syslog(LOG_WARNING, "%s: connection timed out (%u)",
329 EnetStr(rtmp), rtmp->rmp.r_type);
330 RemoveConn(rtmp);
335 ** FindClient -- Find client associated with a packet.
337 ** Parameters:
338 ** rconn - the new packet.
340 ** Returns:
341 ** Pointer to client info if found, NULL otherwise.
343 ** Side Effects:
344 ** None.
346 ** Warnings:
347 ** - This routine must be called with SIGHUP blocked since
348 ** a reconfigure can invalidate the information returned.
351 CLIENT *
352 FindClient(RMPCONN *rconn)
354 CLIENT *ctmp;
356 for (ctmp = Clients; ctmp != NULL; ctmp = ctmp->next)
357 if (memcmp((char *)&rconn->rmp.hp_hdr.saddr[0],
358 (char *)&ctmp->addr[0], RMP_ADDRLEN) == 0)
359 break;
361 return(ctmp);
365 ** Exit -- Log an error message and exit.
367 ** Parameters:
368 ** sig - caught signal (or zero if not dying on a signal).
370 ** Returns:
371 ** Does not return.
373 ** Side Effects:
374 ** - This process ceases to exist.
376 void
377 Exit(int sig)
379 if (sig > 0)
380 syslog(LOG_ERR, "going down on signal %d", sig);
381 else
382 syslog(LOG_ERR, "going down with fatal error");
383 BpfClose();
384 exit(1);
388 ** ReConfig -- Get new list of boot files and reread config files.
390 ** Parameters:
391 ** None.
393 ** Returns:
394 ** Nothing.
396 ** Side Effects:
397 ** - All active connections are dropped.
398 ** - List of boot-able files is changed.
399 ** - List of clients is changed.
401 ** Warnings:
402 ** - This routine must be called with SIGHUP blocked.
404 void
405 ReConfig(int signo)
407 syslog(LOG_NOTICE, "reconfiguring boot server");
409 FreeConns();
411 if (GetBootFiles() == 0)
412 Exit(0);
414 if (ParseConfig() == 0)
415 Exit(0);
419 ** DebugOff -- Turn off debugging.
421 ** Parameters:
422 ** None.
424 ** Returns:
425 ** Nothing.
427 ** Side Effects:
428 ** - Debug file is closed.
430 void
431 DebugOff(int signo)
433 if (DbgFp != NULL)
434 (void) fclose(DbgFp);
436 DbgFp = NULL;
440 ** DebugOn -- Turn on debugging.
442 ** Parameters:
443 ** None.
445 ** Returns:
446 ** Nothing.
448 ** Side Effects:
449 ** - Debug file is opened/truncated if not already opened,
450 ** otherwise do nothing.
452 void
453 DebugOn(int signo)
455 if (DbgFp == NULL) {
456 if ((DbgFp = fopen(DbgFile, "w")) == NULL)
457 syslog(LOG_ERR, "can't open debug file (%s)", DbgFile);