* Copyright (c) 1992 Regents of the University of California.
* Copyright (c) 1988, 1992 The University of Utah and the Center
* for Software Science (CSS).
* This code is derived from software contributed to Berkeley by
* the Center for Software Science of the University of Utah Computer
* Science Department. CSS requests users of this software to return
* to css-dist@cs.utah.edu any improvements that they make and grant
* CSS redistribution rights.
* %sccs.include.redist.c%
* @(#)rbootd.c 5.2 (Berkeley) %G%
* Utah $Hdr: rbootd.c 3.1 92/07/06$
* Author: Jeff Forys, University of Utah CSS
static char sccsid
[] = "@(#)rbootd.c 5.2 (Berkeley) %G%";
/* fd mask macros (backward compatibility with 4.2BSD) */
typedef struct fd_set
{ /* this should already be in 4.2 */
#define FD_ZERO(p) ((p)->fds_bits[0] = 0)
#define FD_SET(n, p) ((p)->fds_bits[0] |= (1 << (n)))
#define FD_CLR(n, p) ((p)->fds_bits[0] &= ~(1 << (n)))
#define FD_ISSET(n, p) ((p)->fds_bits[0] & (1 << (n)))
int c
, fd
, omask
, maxfds
;
* Find what name we are running under.
ProgName
= (ProgName
= rindex(argv
[0],'/')) ? ++ProgName
: *argv
;
* Close any open file descriptors.
* Temporarily leave stdin & stdout open for `-d',
* and stderr open for any pre-syslog error messages.
int i
, nfds
= getdtablesize();
for (i
= 0; i
< nfds
; i
++)
if (i
!= fileno(stdin
) && i
!= fileno(stdout
) &&
while ((c
= getopt(argc
, argv
, "adi:")) != EOF
)
for (; optind
< argc
; optind
++) {
ConfigFile
= argv
[optind
];
"%s: too many config files (`%s' ignored)\n",
if (ConfigFile
== NULL
) /* use default config file */
DbgFp
= stdout
; /* output to stdout */
(void) signal(SIGUSR1
, SIG_IGN
); /* dont muck w/DbgFp */
(void) signal(SIGUSR2
, SIG_IGN
);
(void) fclose(stdin
); /* dont need these */
* Fork off a child to do the work & exit.
case -1: /* fork failed */
fprintf(stderr
, "%s: ", ProgName
);
case 0: /* this is the CHILD */
default: /* this is the PARENT */
* Try to disassociate from the current tty.
char *devtty
= "/dev/tty";
if ((i
= open(devtty
, O_RDWR
)) < 0) {
/* probably already disassociated */
fprintf(stderr
, "%s: ", ProgName
);
if (ioctl(i
, (u_long
)TIOCNOTTY
, (char *)0) < 0){
fprintf(stderr
, "%s: ", ProgName
);
(void) signal(SIGUSR1
, DebugOn
);
(void) signal(SIGUSR2
, DebugOff
);
(void) fclose(stderr
); /* finished with it */
openlog(ProgName
, LOG_PID
);
openlog(ProgName
, LOG_PID
, LOG_DAEMON
);
* If no interface was specified, get one now.
* This is convoluted because we want to get the default interface
* name for the syslog("restarted") message. If BpfGetIntfName()
* runs into an error, it will return a syslog-able error message
* (in `errmsg') which will be displayed here.
if ((IntfName
= BpfGetIntfName(&errmsg
)) == NULL
) {
syslog(LOG_NOTICE
, "restarted (??)");
syslog(LOG_NOTICE
, "restarted (%s)", IntfName
);
(void) signal(SIGHUP
, ReConfig
);
(void) signal(SIGINT
, Exit
);
(void) signal(SIGTERM
, Exit
);
* Grab our host name and pid.
if (gethostname(MyHost
, MAXHOSTNAMELEN
) < 0) {
syslog(LOG_ERR
, "gethostname: %m");
MyHost
[MAXHOSTNAMELEN
] = '\0';
* Write proc's pid to a file.
if ((fp
= fopen(PidFile
, "w")) != NULL
) {
(void) fprintf(fp
, "%d\n", MyPid
);
syslog(LOG_WARNING
, "fopen: failed (%s)", PidFile
);
* All boot files are relative to the boot directory, we might
* as well chdir() there to make life easier.
if (chdir(BootDir
) < 0) {
syslog(LOG_ERR
, "chdir: %m (%s)", BootDir
);
omask
= sigblock(sigmask(SIGHUP
)); /* prevent reconfig's */
if (GetBootFiles() == 0) /* get list of boot files */
if (ParseConfig() == 0) /* parse config file */
* Open and initialize a BPF device for the appropriate interface.
* If an error is encountered, a message is displayed and Exit()
(void) sigsetmask(omask
); /* allow reconfig's */
* Main loop: receive a packet, determine where it came from,
* and if we service this host, call routine to handle request.
if (RmpConns
== NULL
) { /* timeout isnt necessary */
nsel
= select(maxfds
, &r
, (fd_set
*)0, (fd_set
*)0,
timeout
.tv_sec
= RMP_TIMEOUT
;
nsel
= select(maxfds
, &r
, (fd_set
*)0, (fd_set
*)0,
syslog(LOG_ERR
, "select: %m");
} else if (nsel
== 0) { /* timeout */
DoTimeout(); /* clear stale conns */
CLIENT
*client
, *FindClient();
while (BpfRead(&rconn
, doread
)) {
if (DbgFp
!= NULL
) /* display packet */
DispPkt(&rconn
,DIR_RCVD
);
omask
= sigblock(sigmask(SIGHUP
));
* If we do not restrict service, set the
* client to NULL (ProcessPacket() handles
* this). Otherwise, check that we can
* service this host; if not, log a message
} else if ((client
=FindClient(&rconn
))==NULL
) {
"%s: boot packet ignored",
(void) sigsetmask(omask
);
ProcessPacket(&rconn
,client
);
(void) sigsetmask(omask
);
** DoTimeout -- Free any connections that have timed out.
** - Timed out connections in `RmpConns' will be freed.
(void) gettimeofday(&now
, (struct timezone
*)0);
* For each active connection, if RMP_TIMEOUT seconds have passed
* since the last packet was sent, delete the connection.
for (rtmp
= RmpConns
; rtmp
!= NULL
; rtmp
= rtmp
->next
)
if ((rtmp
->tstamp
.tv_sec
+ RMP_TIMEOUT
) < now
.tv_sec
) {
syslog(LOG_WARNING
, "%s: connection timed out (%u)",
EnetStr(rtmp
), rtmp
->rmp
.r_type
);
** FindClient -- Find client associated with a packet.
** rconn - the new packet.
** Pointer to client info if found, NULL otherwise.
** - This routine must be called with SIGHUP blocked since
** a reconfigure can invalidate the information returned.
for (ctmp
= Clients
; ctmp
!= NULL
; ctmp
= ctmp
->next
)
if (bcmp((char *)&rconn
->rmp
.hp_hdr
.saddr
[0],
(char *)&ctmp
->addr
[0], RMP_ADDRLEN
) == 0)
** Exit -- Log an error message and exit.
** sig - caught signal (or zero if not dying on a signal).
** - This process ceases to exist.
syslog(LOG_ERR
, "going down on signal %d", sig
);
syslog(LOG_ERR
, "going down with fatal error");
** ReConfig -- Get new list of boot files and reread config files.
** - All active connections are dropped.
** - List of boot-able files is changed.
** - List of clients is changed.
** - This routine must be called with SIGHUP blocked.
syslog(LOG_NOTICE
, "reconfiguring boot server");
** DebugOff -- Turn off debugging.
** - Debug file is closed.
** DebugOn -- Turn on debugging.
** - Debug file is opened/truncated if not already opened,
if ((DbgFp
= fopen(DbgFile
, "w")) == NULL
)
syslog(LOG_ERR
, "can't open debug file (%s)", DbgFile
);