2 * Copyright (c) 1988, 1992 The University of Utah and the Center
3 * for Software Science (CSS).
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
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
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 static const char copyright[] =
45 "@(#) Copyright (c) 1992, 1993\n\
46 The Regents of the University of California. All rights reserved.\n";
51 static const char sccsid[] = "@(#)rbootd.c 8.1 (Berkeley) 6/4/93";
54 #include <sys/cdefs.h>
55 __FBSDID("$FreeBSD$");
57 #include <sys/param.h>
71 static void usage(void);
74 main(int argc, char *argv[])
76 int c, fd, omask, maxfds;
80 * Close any open file descriptors.
81 * Temporarily leave stdin & stdout open for `-d',
82 * and stderr open for any pre-syslog error messages.
85 int i, nfds = getdtablesize();
87 for (i = 0; i < nfds; i++)
88 if (i != fileno(stdin) && i != fileno(stdout) &&
94 * Parse any arguments.
96 while ((c = getopt(argc, argv, "adi:")) != -1)
110 for (; optind < argc; optind++) {
111 if (ConfigFile == NULL)
112 ConfigFile = argv[optind];
114 warnx("too many config files (`%s' ignored)",
119 if (ConfigFile == NULL) /* use default config file */
120 ConfigFile = DfltConfig;
123 DbgFp = stdout; /* output to stdout */
125 (void) signal(SIGUSR1, SIG_IGN); /* dont muck w/DbgFp */
126 (void) signal(SIGUSR2, SIG_IGN);
127 (void) fclose(stderr); /* finished with it */
130 err(1, "can't detach from terminal");
132 (void) signal(SIGUSR1, DebugOn);
133 (void) signal(SIGUSR2, DebugOff);
136 openlog("rbootd", LOG_PID, LOG_DAEMON);
139 * If no interface was specified, get one now.
141 * This is convoluted because we want to get the default interface
142 * name for the syslog("restarted") message. If BpfGetIntfName()
143 * runs into an error, it will return a syslog-able error message
144 * (in `errmsg') which will be displayed here.
146 if (IntfName == NULL) {
149 if ((IntfName = BpfGetIntfName(&errmsg)) == NULL) {
150 /* Backslash to avoid trigraph '??)'. */
151 syslog(LOG_NOTICE, "restarted (?\?)");
152 /* BpfGetIntfName() returns safe names, using %m */
153 syslog(LOG_ERR, "%s", errmsg);
158 syslog(LOG_NOTICE, "restarted (%s)", IntfName);
160 (void) signal(SIGHUP, ReConfig);
161 (void) signal(SIGINT, Exit);
162 (void) signal(SIGTERM, Exit);
165 * Grab our host name and pid.
167 if (gethostname(MyHost, MAXHOSTNAMELEN - 1) < 0) {
168 syslog(LOG_ERR, "gethostname: %m");
171 MyHost[MAXHOSTNAMELEN - 1] = '\0';
176 * Write proc's pid to a file.
181 if ((fp = fopen(PidFile, "w")) != NULL) {
182 (void) fprintf(fp, "%d\n", (int) MyPid);
185 syslog(LOG_WARNING, "fopen: failed (%s)", PidFile);
190 * All boot files are relative to the boot directory, we might
191 * as well chdir() there to make life easier.
193 if (chdir(BootDir) < 0) {
194 syslog(LOG_ERR, "chdir: %m (%s)", BootDir);
199 * Initial configuration.
201 omask = sigblock(sigmask(SIGHUP)); /* prevent reconfig's */
202 if (GetBootFiles() == 0) /* get list of boot files */
204 if (ParseConfig() == 0) /* parse config file */
208 * Open and initialize a BPF device for the appropriate interface.
209 * If an error is encountered, a message is displayed and Exit()
214 (void) sigsetmask(omask); /* allow reconfig's */
217 * Main loop: receive a packet, determine where it came from,
218 * and if we service this host, call routine to handle request.
224 struct timeval timeout;
230 if (RmpConns == NULL) { /* timeout isn't necessary */
231 nsel = select(maxfds, &r, NULL, NULL, NULL);
233 timeout.tv_sec = RMP_TIMEOUT;
235 nsel = select(maxfds, &r, NULL, NULL, &timeout);
241 syslog(LOG_ERR, "select: %m");
243 } else if (nsel == 0) { /* timeout */
244 DoTimeout(); /* clear stale conns */
248 if (FD_ISSET(fd, &r)) {
250 CLIENT *client, *FindClient();
253 while (BpfRead(&rconn, doread)) {
256 if (DbgFp != NULL) /* display packet */
257 DispPkt(&rconn,DIR_RCVD);
259 omask = sigblock(sigmask(SIGHUP));
262 * If we do not restrict service, set the
263 * client to NULL (ProcessPacket() handles
264 * this). Otherwise, check that we can
265 * service this host; if not, log a message
266 * and ignore the packet.
270 } else if ((client=FindClient(&rconn))==NULL) {
272 "%s: boot packet ignored",
274 (void) sigsetmask(omask);
278 ProcessPacket(&rconn,client);
280 (void) sigsetmask(omask);
289 fprintf(stderr, "usage: rbootd [-ad] [-i interface] [config_file]\n");
294 ** DoTimeout -- Free any connections that have timed out.
303 ** - Timed out connections in `RmpConns' will be freed.
312 * For each active connection, if RMP_TIMEOUT seconds have passed
313 * since the last packet was sent, delete the connection.
316 for (rtmp = RmpConns; rtmp != NULL; rtmp = rtmp->next)
317 if ((rtmp->tstamp.tv_sec + RMP_TIMEOUT) < now) {
318 syslog(LOG_WARNING, "%s: connection timed out (%u)",
319 EnetStr(rtmp), rtmp->rmp.r_type);
325 ** FindClient -- Find client associated with a packet.
328 ** rconn - the new packet.
331 ** Pointer to client info if found, NULL otherwise.
337 ** - This routine must be called with SIGHUP blocked since
338 ** a reconfigure can invalidate the information returned.
342 FindClient(RMPCONN *rconn)
346 for (ctmp = Clients; ctmp != NULL; ctmp = ctmp->next)
347 if (bcmp((char *)&rconn->rmp.hp_hdr.saddr[0],
348 (char *)&ctmp->addr[0], RMP_ADDRLEN) == 0)
355 ** Exit -- Log an error message and exit.
358 ** sig - caught signal (or zero if not dying on a signal).
364 ** - This process ceases to exist.
370 syslog(LOG_ERR, "going down on signal %d", sig);
372 syslog(LOG_ERR, "going down with fatal error");
378 ** ReConfig -- Get new list of boot files and reread config files.
387 ** - All active connections are dropped.
388 ** - List of boot-able files is changed.
389 ** - List of clients is changed.
392 ** - This routine must be called with SIGHUP blocked.
395 ReConfig(int signo __unused)
397 syslog(LOG_NOTICE, "reconfiguring boot server");
401 if (GetBootFiles() == 0)
404 if (ParseConfig() == 0)
409 ** DebugOff -- Turn off debugging.
418 ** - Debug file is closed.
421 DebugOff(int signo __unused)
424 (void) fclose(DbgFp);
430 ** DebugOn -- Turn on debugging.
439 ** - Debug file is opened/truncated if not already opened,
440 ** otherwise do nothing.
443 DebugOn(int signo __unused)
446 if ((DbgFp = fopen(DbgFile, "w")) == NULL)
447 syslog(LOG_ERR, "can't open debug file (%s)", DbgFile);