]>
git.saurik.com Git - apple/network_cmds.git/blob - rbootd.tproj/rbootd.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1988, 1992 The University of Utah and the Center
26 * for Software Science (CSS).
27 * Copyright (c) 1992, 1993
28 * The Regents of the University of California. All rights reserved.
30 * This code is derived from software contributed to Berkeley by
31 * the Center for Software Science of the University of Utah Computer
32 * Science Department. CSS requests users of this software to return
33 * to css-dist@cs.utah.edu any improvements that they make and grant
34 * CSS redistribution rights.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)rbootd.c 8.2 (Berkeley) 2/22/94
66 * Utah $Hdr: rbootd.c 3.1 92/07/06$
67 * Author: Jeff Forys, University of Utah CSS
71 static char copyright
[] =
72 "@(#) Copyright (c) 1992, 1993\n\
73 The Regents of the University of California. All rights reserved.\n";
77 static char sccsid
[] = "@(#)rbootd.c 8.2 (Berkeley) 2/22/94";
80 #include <sys/param.h>
81 #include <sys/ioctl.h>
96 /* fd mask macros (backward compatibility with 4.2BSD) */
99 typedef struct fd_set
{ /* this should already be in 4.2 */
103 #define FD_ZERO(p) ((p)->fds_bits[0] = 0)
104 #define FD_SET(n, p) ((p)->fds_bits[0] |= (1 << (n)))
105 #define FD_CLR(n, p) ((p)->fds_bits[0] &= ~(1 << (n)))
106 #define FD_ISSET(n, p) ((p)->fds_bits[0] & (1 << (n)))
114 int c
, fd
, omask
, maxfds
;
118 * Find what name we are running under.
120 ProgName
= (ProgName
= rindex(argv
[0],'/')) ? ++ProgName
: *argv
;
123 * Close any open file descriptors.
124 * Temporarily leave stdin & stdout open for `-d',
125 * and stderr open for any pre-syslog error messages.
128 int i
, nfds
= getdtablesize();
130 for (i
= 0; i
< nfds
; i
++)
131 if (i
!= fileno(stdin
) && i
!= fileno(stdout
) &&
137 * Parse any arguments.
139 while ((c
= getopt(argc
, argv
, "adi:")) != EOF
)
151 for (; optind
< argc
; optind
++) {
152 if (ConfigFile
== NULL
)
153 ConfigFile
= argv
[optind
];
156 "%s: too many config files (`%s' ignored)\n",
157 ProgName
, argv
[optind
]);
161 if (ConfigFile
== NULL
) /* use default config file */
162 ConfigFile
= DfltConfig
;
165 DbgFp
= stdout
; /* output to stdout */
167 (void) signal(SIGUSR1
, SIG_IGN
); /* dont muck w/DbgFp */
168 (void) signal(SIGUSR2
, SIG_IGN
);
170 (void) fclose(stdin
); /* dont need these */
171 (void) fclose(stdout
);
174 * Fork off a child to do the work & exit.
177 case -1: /* fork failed */
178 fprintf(stderr
, "%s: ", ProgName
);
181 case 0: /* this is the CHILD */
183 default: /* this is the PARENT */
188 * Try to disassociate from the current tty.
191 char *devtty
= "/dev/tty";
194 if ((i
= open(devtty
, O_RDWR
)) < 0) {
195 /* probably already disassociated */
196 if (setpgrp(0, 0) < 0) {
197 fprintf(stderr
, "%s: ", ProgName
);
201 if (ioctl(i
, (u_long
)TIOCNOTTY
, (char *)0) < 0){
202 fprintf(stderr
, "%s: ", ProgName
);
209 (void) signal(SIGUSR1
, DebugOn
);
210 (void) signal(SIGUSR2
, DebugOff
);
213 (void) fclose(stderr
); /* finished with it */
216 openlog(ProgName
, LOG_PID
);
218 openlog(ProgName
, LOG_PID
, LOG_DAEMON
);
222 * If no interface was specified, get one now.
224 * This is convoluted because we want to get the default interface
225 * name for the syslog("restarted") message. If BpfGetIntfName()
226 * runs into an error, it will return a syslog-able error message
227 * (in `errmsg') which will be displayed here.
229 if (IntfName
== NULL
) {
232 if ((IntfName
= BpfGetIntfName(&errmsg
)) == NULL
) {
233 syslog(LOG_NOTICE
, "restarted (??)");
234 syslog(LOG_ERR
, errmsg
);
239 syslog(LOG_NOTICE
, "restarted (%s)", IntfName
);
241 (void) signal(SIGHUP
, ReConfig
);
242 (void) signal(SIGINT
, Exit
);
243 (void) signal(SIGTERM
, Exit
);
246 * Grab our host name and pid.
248 if (gethostname(MyHost
, MAXHOSTNAMELEN
) < 0) {
249 syslog(LOG_ERR
, "gethostname: %m");
252 MyHost
[MAXHOSTNAMELEN
] = '\0';
257 * Write proc's pid to a file.
262 if ((fp
= fopen(PidFile
, "w")) != NULL
) {
263 (void) fprintf(fp
, "%d\n", MyPid
);
266 syslog(LOG_WARNING
, "fopen: failed (%s)", PidFile
);
271 * All boot files are relative to the boot directory, we might
272 * as well chdir() there to make life easier.
274 if (chdir(BootDir
) < 0) {
275 syslog(LOG_ERR
, "chdir: %m (%s)", BootDir
);
280 * Initial configuration.
282 omask
= sigblock(sigmask(SIGHUP
)); /* prevent reconfig's */
283 if (GetBootFiles() == 0) /* get list of boot files */
285 if (ParseConfig() == 0) /* parse config file */
289 * Open and initialize a BPF device for the appropriate interface.
290 * If an error is encountered, a message is displayed and Exit()
295 (void) sigsetmask(omask
); /* allow reconfig's */
298 * Main loop: receive a packet, determine where it came from,
299 * and if we service this host, call routine to handle request.
305 struct timeval timeout
;
311 if (RmpConns
== NULL
) { /* timeout isnt necessary */
312 nsel
= select(maxfds
, &r
, (fd_set
*)0, (fd_set
*)0,
313 (struct timeval
*)0);
315 timeout
.tv_sec
= RMP_TIMEOUT
;
317 nsel
= select(maxfds
, &r
, (fd_set
*)0, (fd_set
*)0,
324 syslog(LOG_ERR
, "select: %m");
326 } else if (nsel
== 0) { /* timeout */
327 DoTimeout(); /* clear stale conns */
331 if (FD_ISSET(fd
, &r
)) {
333 CLIENT
*client
, *FindClient();
336 while (BpfRead(&rconn
, doread
)) {
339 if (DbgFp
!= NULL
) /* display packet */
340 DispPkt(&rconn
,DIR_RCVD
);
342 omask
= sigblock(sigmask(SIGHUP
));
345 * If we do not restrict service, set the
346 * client to NULL (ProcessPacket() handles
347 * this). Otherwise, check that we can
348 * service this host; if not, log a message
349 * and ignore the packet.
353 } else if ((client
=FindClient(&rconn
))==NULL
) {
355 "%s: boot packet ignored",
357 (void) sigsetmask(omask
);
361 ProcessPacket(&rconn
,client
);
363 (void) sigsetmask(omask
);
370 ** DoTimeout -- Free any connections that have timed out.
379 ** - Timed out connections in `RmpConns' will be freed.
384 register RMPCONN
*rtmp
;
387 (void) gettimeofday(&now
, (struct timezone
*)0);
390 * For each active connection, if RMP_TIMEOUT seconds have passed
391 * since the last packet was sent, delete the connection.
393 for (rtmp
= RmpConns
; rtmp
!= NULL
; rtmp
= rtmp
->next
)
394 if ((rtmp
->tstamp
.tv_sec
+ RMP_TIMEOUT
) < now
.tv_sec
) {
395 syslog(LOG_WARNING
, "%s: connection timed out (%u)",
396 EnetStr(rtmp
), rtmp
->rmp
.r_type
);
402 ** FindClient -- Find client associated with a packet.
405 ** rconn - the new packet.
408 ** Pointer to client info if found, NULL otherwise.
414 ** - This routine must be called with SIGHUP blocked since
415 ** a reconfigure can invalidate the information returned.
420 register RMPCONN
*rconn
;
422 register CLIENT
*ctmp
;
424 for (ctmp
= Clients
; ctmp
!= NULL
; ctmp
= ctmp
->next
)
425 if (bcmp((char *)&rconn
->rmp
.hp_hdr
.saddr
[0],
426 (char *)&ctmp
->addr
[0], RMP_ADDRLEN
) == 0)
433 ** Exit -- Log an error message and exit.
436 ** sig - caught signal (or zero if not dying on a signal).
442 ** - This process ceases to exist.
449 syslog(LOG_ERR
, "going down on signal %d", sig
);
451 syslog(LOG_ERR
, "going down with fatal error");
457 ** ReConfig -- Get new list of boot files and reread config files.
466 ** - All active connections are dropped.
467 ** - List of boot-able files is changed.
468 ** - List of clients is changed.
471 ** - This routine must be called with SIGHUP blocked.
477 syslog(LOG_NOTICE
, "reconfiguring boot server");
481 if (GetBootFiles() == 0)
484 if (ParseConfig() == 0)
489 ** DebugOff -- Turn off debugging.
498 ** - Debug file is closed.
505 (void) fclose(DbgFp
);
511 ** DebugOn -- Turn on debugging.
520 ** - Debug file is opened/truncated if not already opened,
521 ** otherwise do nothing.
528 if ((DbgFp
= fopen(DbgFile
, "w")) == NULL
)
529 syslog(LOG_ERR
, "can't open debug file (%s)", DbgFile
);