1 /* $NetBSD: rbootd.c,v 1.20 2007/02/21 20:57:55 hubertf Exp $ */
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 * 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
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
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>
89 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
90 The Regents of the University of California. All rights reserved.");
95 static char sccsid
[] = "@(#)rbootd.c 8.1 (Berkeley) 6/4/93";
97 __RCSID("$NetBSD: rbootd.c,v 1.20 2007/02/21 20:57:55 hubertf Exp $");
101 #include <sys/param.h>
102 #include <sys/time.h>
116 int main (int, char *[]);
119 main(int argc
, char *argv
[])
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
&&
139 * Parse any arguments.
141 while ((c
= getopt(argc
, argv
, "adi:")) != -1)
153 for (; optind
< argc
; optind
++) {
154 if (ConfigFile
== NULL
)
155 ConfigFile
= argv
[optind
];
157 warnx("too many config files (`%s' ignored)",
162 if (ConfigFile
== NULL
) /* use default config file */
163 ConfigFile
= DfltConfig
;
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 */
173 err(1, "can't detach from terminal");
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
) {
193 if ((IntfName
= BpfGetIntfName(&errmsg
)) == NULL
) {
194 /* backslash to avoid trigraph ??) */
195 syslog(LOG_NOTICE
, "restarted (?\?)");
196 syslog(LOG_ERR
, errmsg
);
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");
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
);
226 * Initial configuration.
228 omask
= sigblock(sigmask(SIGHUP
)); /* prevent reconfig's */
229 if (GetBootFiles() == 0) /* get list of boot files */
231 if (ParseConfig() == 0) /* parse config file */
235 * Open and initialize a BPF device for the appropriate interface.
236 * If an error is encountered, a message is displayed and Exit()
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.
248 set
[0].events
= POLLIN
;
252 nsel
= poll(set
, 1, RmpConns
? RMP_TIMEOUT
* 1000 : INFTIM
);
257 syslog(LOG_ERR
, "poll: %m");
259 } else if (nsel
== 0) { /* timeout */
260 DoTimeout(); /* clear stale conns */
264 if (set
[0].revents
& POLLIN
) {
269 while (BpfRead(&rconn
, doread
)) {
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.
286 } else if ((client
=FindClient(&rconn
))==NULL
) {
288 "%s: boot packet ignored",
290 (void) sigsetmask(omask
);
294 ProcessPacket(&rconn
,client
);
296 (void) sigsetmask(omask
);
303 ** DoTimeout -- Free any connections that have timed out.
312 ** - Timed out connections in `RmpConns' will be freed.
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
);
335 ** FindClient -- Find client associated with a packet.
338 ** rconn - the new packet.
341 ** Pointer to client info if found, NULL otherwise.
347 ** - This routine must be called with SIGHUP blocked since
348 ** a reconfigure can invalidate the information returned.
352 FindClient(RMPCONN
*rconn
)
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)
365 ** Exit -- Log an error message and exit.
368 ** sig - caught signal (or zero if not dying on a signal).
374 ** - This process ceases to exist.
380 syslog(LOG_ERR
, "going down on signal %d", sig
);
382 syslog(LOG_ERR
, "going down with fatal error");
388 ** ReConfig -- Get new list of boot files and reread config files.
397 ** - All active connections are dropped.
398 ** - List of boot-able files is changed.
399 ** - List of clients is changed.
402 ** - This routine must be called with SIGHUP blocked.
407 syslog(LOG_NOTICE
, "reconfiguring boot server");
411 if (GetBootFiles() == 0)
414 if (ParseConfig() == 0)
419 ** DebugOff -- Turn off debugging.
428 ** - 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.
456 if ((DbgFp
= fopen(DbgFile
, "w")) == NULL
)
457 syslog(LOG_ERR
, "can't open debug file (%s)", DbgFile
);