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. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * from: @(#)rbootd.c 8.1 (Berkeley) 6/4/93
43 * From: Utah Hdr: rbootd.c 3.1 92/07/06
44 * Author: Jeff Forys, University of Utah CSS
48 static const char copyright[] =
49 "@(#) Copyright (c) 1992, 1993\n\
50 The Regents of the University of California. All rights reserved.\n";
55 static const char sccsid[] = "@(#)rbootd.c 8.1 (Berkeley) 6/4/93";
57 static const char rcsid[] =
58 "$FreeBSD: src/libexec/rbootd/rbootd.c,v 1.11.2.1 2001/02/18 02:54:11 kris Exp $";
61 #include <sys/param.h>
75 static void usage __P((void));
82 int c, fd, omask, maxfds;
86 * Close any open file descriptors.
87 * Temporarily leave stdin & stdout open for `-d',
88 * and stderr open for any pre-syslog error messages.
91 int i, nfds = getdtablesize();
93 for (i = 0; i < nfds; i++)
94 if (i != fileno(stdin) && i != fileno(stdout) &&
100 * Parse any arguments.
102 while ((c = getopt(argc, argv, "adi:")) != -1)
116 for (; optind < argc; optind++) {
117 if (ConfigFile == NULL)
118 ConfigFile = argv[optind];
120 warnx("too many config files (`%s' ignored)",
125 if (ConfigFile == NULL) /* use default config file */
126 ConfigFile = DfltConfig;
129 DbgFp = stdout; /* output to stdout */
131 (void) signal(SIGUSR1, SIG_IGN); /* dont muck w/DbgFp */
132 (void) signal(SIGUSR2, SIG_IGN);
133 (void) fclose(stderr); /* finished with it */
136 err(1, "can't detach from terminal");
138 (void) signal(SIGUSR1, DebugOn);
139 (void) signal(SIGUSR2, DebugOff);
142 openlog("rbootd", LOG_PID, LOG_DAEMON);
145 * If no interface was specified, get one now.
147 * This is convoluted because we want to get the default interface
148 * name for the syslog("restarted") message. If BpfGetIntfName()
149 * runs into an error, it will return a syslog-able error message
150 * (in `errmsg') which will be displayed here.
152 if (IntfName == NULL) {
155 if ((IntfName = BpfGetIntfName(&errmsg)) == NULL) {
156 syslog(LOG_NOTICE, "restarted (??)");
157 /* BpfGetIntfName() returns safe names, using %m */
158 syslog(LOG_ERR, "%s", errmsg);
163 syslog(LOG_NOTICE, "restarted (%s)", IntfName);
165 (void) signal(SIGHUP, ReConfig);
166 (void) signal(SIGINT, Exit);
167 (void) signal(SIGTERM, Exit);
170 * Grab our host name and pid.
172 if (gethostname(MyHost, MAXHOSTNAMELEN - 1) < 0) {
173 syslog(LOG_ERR, "gethostname: %m");
176 MyHost[MAXHOSTNAMELEN - 1] = '\0';
181 * Write proc's pid to a file.
186 if ((fp = fopen(PidFile, "w")) != NULL) {
187 (void) fprintf(fp, "%d\n", (int) MyPid);
190 syslog(LOG_WARNING, "fopen: failed (%s)", PidFile);
195 * All boot files are relative to the boot directory, we might
196 * as well chdir() there to make life easier.
198 if (chdir(BootDir) < 0) {
199 syslog(LOG_ERR, "chdir: %m (%s)", BootDir);
204 * Initial configuration.
206 omask = sigblock(sigmask(SIGHUP)); /* prevent reconfig's */
207 if (GetBootFiles() == 0) /* get list of boot files */
209 if (ParseConfig() == 0) /* parse config file */
213 * Open and initialize a BPF device for the appropriate interface.
214 * If an error is encountered, a message is displayed and Exit()
219 (void) sigsetmask(omask); /* allow reconfig's */
222 * Main loop: receive a packet, determine where it came from,
223 * and if we service this host, call routine to handle request.
229 struct timeval timeout;
235 if (RmpConns == NULL) { /* timeout isnt necessary */
236 nsel = select(maxfds, &r, NULL, NULL, NULL);
238 timeout.tv_sec = RMP_TIMEOUT;
240 nsel = select(maxfds, &r, NULL, NULL, &timeout);
246 syslog(LOG_ERR, "select: %m");
248 } else if (nsel == 0) { /* timeout */
249 DoTimeout(); /* clear stale conns */
253 if (FD_ISSET(fd, &r)) {
255 CLIENT *client, *FindClient();
258 while (BpfRead(&rconn, doread)) {
261 if (DbgFp != NULL) /* display packet */
262 DispPkt(&rconn,DIR_RCVD);
264 omask = sigblock(sigmask(SIGHUP));
267 * If we do not restrict service, set the
268 * client to NULL (ProcessPacket() handles
269 * this). Otherwise, check that we can
270 * service this host; if not, log a message
271 * and ignore the packet.
275 } else if ((client=FindClient(&rconn))==NULL) {
277 "%s: boot packet ignored",
279 (void) sigsetmask(omask);
283 ProcessPacket(&rconn,client);
285 (void) sigsetmask(omask);
294 fprintf(stderr, "usage: rbootd [-ad] [-i interface] [config_file]\n");
299 ** DoTimeout -- Free any connections that have timed out.
308 ** - Timed out connections in `RmpConns' will be freed.
316 (void) gettimeofday(&now, (struct timezone *)0);
319 * For each active connection, if RMP_TIMEOUT seconds have passed
320 * since the last packet was sent, delete the connection.
322 for (rtmp = RmpConns; rtmp != NULL; rtmp = rtmp->next)
323 if ((rtmp->tstamp.tv_sec + RMP_TIMEOUT) < now.tv_sec) {
324 syslog(LOG_WARNING, "%s: connection timed out (%u)",
325 EnetStr(rtmp), rtmp->rmp.r_type);
331 ** FindClient -- Find client associated with a packet.
334 ** rconn - the new packet.
337 ** Pointer to client info if found, NULL otherwise.
343 ** - This routine must be called with SIGHUP blocked since
344 ** a reconfigure can invalidate the information returned.
353 for (ctmp = Clients; ctmp != NULL; ctmp = ctmp->next)
354 if (bcmp((char *)&rconn->rmp.hp_hdr.saddr[0],
355 (char *)&ctmp->addr[0], RMP_ADDRLEN) == 0)
362 ** Exit -- Log an error message and exit.
365 ** sig - caught signal (or zero if not dying on a signal).
371 ** - This process ceases to exist.
378 syslog(LOG_ERR, "going down on signal %d", sig);
380 syslog(LOG_ERR, "going down with fatal error");
386 ** ReConfig -- Get new list of boot files and reread config files.
395 ** - All active connections are dropped.
396 ** - List of boot-able files is changed.
397 ** - List of clients is changed.
400 ** - This routine must be called with SIGHUP blocked.
406 syslog(LOG_NOTICE, "reconfiguring boot server");
410 if (GetBootFiles() == 0)
413 if (ParseConfig() == 0)
418 ** DebugOff -- Turn off debugging.
427 ** - Debug file is closed.
434 (void) fclose(DbgFp);
440 ** DebugOn -- Turn on debugging.
449 ** - Debug file is opened/truncated if not already opened,
450 ** otherwise do nothing.
457 if ((DbgFp = fopen(DbgFile, "w")) == NULL)
458 syslog(LOG_ERR, "can't open debug file (%s)", DbgFile);