2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1988, 1992 The University of Utah and the Center
5 * for Software Science (CSS).
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * the Center for Software Science of the University of Utah Computer
11 * Science Department. CSS requests users of this software to return
12 * to css-dist@cs.utah.edu any improvements that they make and grant
13 * CSS redistribution rights.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * from: @(#)rbootd.c 8.1 (Berkeley) 6/4/93
41 * From: Utah Hdr: rbootd.c 3.1 92/07/06
42 * Author: Jeff Forys, University of Utah CSS
46 static const char copyright[] =
47 "@(#) Copyright (c) 1992, 1993\n\
48 The Regents of the University of California. All rights reserved.\n";
53 static const char sccsid[] = "@(#)rbootd.c 8.1 (Berkeley) 6/4/93";
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
59 #include <sys/param.h>
73 static void usage(void);
76 main(int argc, char *argv[])
78 int c, fd, omask, maxfds;
82 * Close any open file descriptors.
83 * Temporarily leave stdin & stdout open for `-d',
84 * and stderr open for any pre-syslog error messages.
87 int i, nfds = getdtablesize();
89 for (i = 0; i < nfds; i++)
90 if (i != fileno(stdin) && i != fileno(stdout) &&
96 * Parse any arguments.
98 while ((c = getopt(argc, argv, "adi:")) != -1)
112 for (; optind < argc; optind++) {
113 if (ConfigFile == NULL)
114 ConfigFile = argv[optind];
116 warnx("too many config files (`%s' ignored)",
121 if (ConfigFile == NULL) /* use default config file */
122 ConfigFile = DfltConfig;
125 DbgFp = stdout; /* output to stdout */
127 (void) signal(SIGUSR1, SIG_IGN); /* dont muck w/DbgFp */
128 (void) signal(SIGUSR2, SIG_IGN);
129 (void) fclose(stderr); /* finished with it */
132 err(1, "can't detach from terminal");
134 (void) signal(SIGUSR1, DebugOn);
135 (void) signal(SIGUSR2, DebugOff);
138 openlog("rbootd", LOG_PID, LOG_DAEMON);
141 * If no interface was specified, get one now.
143 * This is convoluted because we want to get the default interface
144 * name for the syslog("restarted") message. If BpfGetIntfName()
145 * runs into an error, it will return a syslog-able error message
146 * (in `errmsg') which will be displayed here.
148 if (IntfName == NULL) {
151 if ((IntfName = BpfGetIntfName(&errmsg)) == NULL) {
152 /* Backslash to avoid trigraph '??)'. */
153 syslog(LOG_NOTICE, "restarted (?\?)");
154 /* BpfGetIntfName() returns safe names, using %m */
155 syslog(LOG_ERR, "%s", errmsg);
160 syslog(LOG_NOTICE, "restarted (%s)", IntfName);
162 (void) signal(SIGHUP, ReConfig);
163 (void) signal(SIGINT, Exit);
164 (void) signal(SIGTERM, Exit);
167 * Grab our host name and pid.
169 if (gethostname(MyHost, MAXHOSTNAMELEN - 1) < 0) {
170 syslog(LOG_ERR, "gethostname: %m");
173 MyHost[MAXHOSTNAMELEN - 1] = '\0';
178 * Write proc's pid to a file.
183 if ((fp = fopen(PidFile, "w")) != NULL) {
184 (void) fprintf(fp, "%d\n", (int) MyPid);
187 syslog(LOG_WARNING, "fopen: failed (%s)", PidFile);
192 * All boot files are relative to the boot directory, we might
193 * as well chdir() there to make life easier.
195 if (chdir(BootDir) < 0) {
196 syslog(LOG_ERR, "chdir: %m (%s)", BootDir);
201 * Initial configuration.
203 omask = sigblock(sigmask(SIGHUP)); /* prevent reconfig's */
204 if (GetBootFiles() == 0) /* get list of boot files */
206 if (ParseConfig() == 0) /* parse config file */
210 * Open and initialize a BPF device for the appropriate interface.
211 * If an error is encountered, a message is displayed and Exit()
216 (void) sigsetmask(omask); /* allow reconfig's */
219 * Main loop: receive a packet, determine where it came from,
220 * and if we service this host, call routine to handle request.
226 struct timeval timeout;
232 if (RmpConns == NULL) { /* timeout isn't necessary */
233 nsel = select(maxfds, &r, NULL, NULL, NULL);
235 timeout.tv_sec = RMP_TIMEOUT;
237 nsel = select(maxfds, &r, NULL, NULL, &timeout);
243 syslog(LOG_ERR, "select: %m");
245 } else if (nsel == 0) { /* timeout */
246 DoTimeout(); /* clear stale conns */
250 if (FD_ISSET(fd, &r)) {
252 CLIENT *client, *FindClient();
255 while (BpfRead(&rconn, doread)) {
258 if (DbgFp != NULL) /* display packet */
259 DispPkt(&rconn,DIR_RCVD);
261 omask = sigblock(sigmask(SIGHUP));
264 * If we do not restrict service, set the
265 * client to NULL (ProcessPacket() handles
266 * this). Otherwise, check that we can
267 * service this host; if not, log a message
268 * and ignore the packet.
272 } else if ((client=FindClient(&rconn))==NULL) {
274 "%s: boot packet ignored",
276 (void) sigsetmask(omask);
280 ProcessPacket(&rconn,client);
282 (void) sigsetmask(omask);
291 fprintf(stderr, "usage: rbootd [-ad] [-i interface] [config_file]\n");
296 ** DoTimeout -- Free any connections that have timed out.
305 ** - Timed out connections in `RmpConns' will be freed.
314 * For each active connection, if RMP_TIMEOUT seconds have passed
315 * since the last packet was sent, delete the connection.
318 for (rtmp = RmpConns; rtmp != NULL; rtmp = rtmp->next)
319 if ((rtmp->tstamp.tv_sec + RMP_TIMEOUT) < now) {
320 syslog(LOG_WARNING, "%s: connection timed out (%u)",
321 EnetStr(rtmp), rtmp->rmp.r_type);
327 ** FindClient -- Find client associated with a packet.
330 ** rconn - the new packet.
333 ** Pointer to client info if found, NULL otherwise.
339 ** - This routine must be called with SIGHUP blocked since
340 ** a reconfigure can invalidate the information returned.
344 FindClient(RMPCONN *rconn)
348 for (ctmp = Clients; ctmp != NULL; ctmp = ctmp->next)
349 if (bcmp((char *)&rconn->rmp.hp_hdr.saddr[0],
350 (char *)&ctmp->addr[0], RMP_ADDRLEN) == 0)
357 ** Exit -- Log an error message and exit.
360 ** sig - caught signal (or zero if not dying on a signal).
366 ** - This process ceases to exist.
372 syslog(LOG_ERR, "going down on signal %d", sig);
374 syslog(LOG_ERR, "going down with fatal error");
380 ** ReConfig -- Get new list of boot files and reread config files.
389 ** - All active connections are dropped.
390 ** - List of boot-able files is changed.
391 ** - List of clients is changed.
394 ** - This routine must be called with SIGHUP blocked.
397 ReConfig(int signo __unused)
399 syslog(LOG_NOTICE, "reconfiguring boot server");
403 if (GetBootFiles() == 0)
406 if (ParseConfig() == 0)
411 ** DebugOff -- Turn off debugging.
420 ** - Debug file is closed.
423 DebugOff(int signo __unused)
426 (void) fclose(DbgFp);
432 ** DebugOn -- Turn on debugging.
441 ** - Debug file is opened/truncated if not already opened,
442 ** otherwise do nothing.
445 DebugOn(int signo __unused)
448 if ((DbgFp = fopen(DbgFile, "w")) == NULL)
449 syslog(LOG_ERR, "can't open debug file (%s)", DbgFile);