Use err(3). Add usage.

Use syslog instead of fprintf when being a daemon.
Change sprintf to snprintf obtained from OpenBSD.
Obtained from: OpenBSD
This commit is contained in:
Philippe Charnier 1997-10-13 11:27:55 +00:00
parent 7f1550ed10
commit 11588fbd01
2 changed files with 49 additions and 29 deletions

View File

@ -58,14 +58,16 @@ As a producer of information it periodically
queries the state of the system and constructs queries the state of the system and constructs
status messages which are broadcasted or multicasted on a network. status messages which are broadcasted or multicasted on a network.
As a consumer of information, it listens for other As a consumer of information, it listens for other
.Nm rwhod .Nm
servers' status messages, validating them, then recording servers' status messages, validating them, then recording
them in a collection of files located in the directory them in a collection of files located in the directory
.Pa /var/rwho . .Pa /var/rwho .
.Pp .Pp
The The
.Fl m .Fl m
option causes rwhod to use IP multicast (instead of option causes
.Nm
to use IP multicast (instead of
broadcast) on all interfaces that have broadcast) on all interfaces that have
the IFF_MULTICAST flag set in their "ifnet" structs the IFF_MULTICAST flag set in their "ifnet" structs
(excluding the loopback interface). The multicast (excluding the loopback interface). The multicast
@ -76,9 +78,15 @@ If the optional
.Ar ttl .Ar ttl
argument is supplied with the argument is supplied with the
.Fl m .Fl m
flag, rwhod will send IP multicast datagrams with a flag,
time-to-live of <ttl>, via a SINGLE interface rather .Nm
than all interfaces. <ttl> must be between 0 and will send IP multicast datagrams with a
time-to-live of
.Ar ttl ,
via a SINGLE interface rather
than all interfaces.
.Ar ttl
must be between 0 and
32 (or MAX_MULTICAST_SCOPE). Note that 32 (or MAX_MULTICAST_SCOPE). Note that
.Fl m Ar 1 .Fl m Ar 1
is different than is different than
@ -92,14 +100,21 @@ When
is used without a is used without a
.Ar ttl .Ar ttl
argument, the program accepts multicast argument, the program accepts multicast
rwhod reports from all multicast-capable interfaces. If a .Nm
reports from all multicast-capable interfaces. If a
.Ar ttl .Ar ttl
argument is given, it accepts multicast reports from only one interface, the argument is given, it accepts multicast reports from only one interface, the
one on which reports are sent (which may be controlled via the host's routing one on which reports are sent (which may be controlled via the host's routing
table). Regardless of the "-m" option, the program accepts broadcast or table). Regardless of the
.Fl m
option, the program accepts broadcast or
unicast reports from all interfaces. Thus, this program will hear the unicast reports from all interfaces. Thus, this program will hear the
reports of old, non-multicasting rwhods, but, if multicasting is used, reports of old, non-multicasting
those old rwhods won't hear the reports generated by this program. .Nm rwhod Ns s ,
but, if multicasting is used,
those old
.Nm rwhod Ns s
won't hear the reports generated by this program.
.Pp .Pp
The server transmits and receives messages at the port indicated The server transmits and receives messages at the port indicated
in the ``rwho'' service specification; see in the ``rwho'' service specification; see
@ -153,7 +168,7 @@ in the message, contains any unprintable
.Tn ASCII .Tn ASCII
characters, the characters, the
message is discarded. Valid messages received by message is discarded. Valid messages received by
.Nm rwhod .Nm
are placed in files named are placed in files named
.Pa whod.hostname .Pa whod.hostname
in the directory in the directory

View File

@ -32,13 +32,17 @@
*/ */
#ifndef lint #ifndef lint
static char copyright[] = static const char copyright[] =
"@(#) Copyright (c) 1983, 1993\n\ "@(#) Copyright (c) 1983, 1993\n\
The Regents of the University of California. All rights reserved.\n"; The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */ #endif /* not lint */
#ifndef lint #ifndef lint
#if 0
static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) 6/6/93"; static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] =
"$Id$";
#endif /* not lint */ #endif /* not lint */
#include <sys/param.h> #include <sys/param.h>
@ -55,6 +59,7 @@ static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) 6/6/93";
#include <protocols/rwhod.h> #include <protocols/rwhod.h>
#include <ctype.h> #include <ctype.h>
#include <err.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <netdb.h> #include <netdb.h>
@ -149,6 +154,7 @@ void onalrm __P((int));
void quit __P((char *)); void quit __P((char *));
void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *)); void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
int verify __P((char *, int)); int verify __P((char *, int));
static void usage __P((void));
#ifdef DEBUG #ifdef DEBUG
char *interval __P((int, char *)); char *interval __P((int, char *));
void Sendto __P((int, const void *, size_t, int, void Sendto __P((int, const void *, size_t, int,
@ -170,10 +176,8 @@ main(argc, argv)
uid_t unpriv_uid; uid_t unpriv_uid;
gid_t unpriv_gid; gid_t unpriv_gid;
if (getuid()) { if (getuid())
fprintf(stderr, "rwhod: not super user\n"); errx(1, "not super user");
exit(1);
}
run_as(&unpriv_uid, &unpriv_gid); run_as(&unpriv_uid, &unpriv_gid);
@ -184,22 +188,18 @@ main(argc, argv)
argv++, argc--; argv++, argc--;
multicast_mode = SCOPED_MULTICAST; multicast_mode = SCOPED_MULTICAST;
multicast_scope = atoi(*argv); multicast_scope = atoi(*argv);
if (multicast_scope > MAX_MULTICAST_SCOPE) { if (multicast_scope > MAX_MULTICAST_SCOPE)
fprintf(stderr, errx(1, "ttl must not exceed %u",
"rwhod: ttl must not exceed %u\n",
MAX_MULTICAST_SCOPE); MAX_MULTICAST_SCOPE);
exit(1);
}
} }
else multicast_mode = PER_INTERFACE_MULTICAST; else multicast_mode = PER_INTERFACE_MULTICAST;
} }
else goto usage; else
usage();
argv++, argc--; argv++, argc--;
} }
if (argc > 0) { if (argc > 0)
usage: fprintf(stderr, "usage: rwhod [ -m [ ttl ] ]\n"); usage();
exit(1);
}
#ifndef DEBUG #ifndef DEBUG
daemon(1, 0); daemon(1, 0);
#endif #endif
@ -279,7 +279,7 @@ usage: fprintf(stderr, "usage: rwhod [ -m [ ttl ] ]\n");
from.sin_addr); from.sin_addr);
continue; continue;
} }
(void) sprintf(path, "whod.%s", wd.wd_hostname); (void) snprintf(path, sizeof path, "whod.%s", wd.wd_hostname);
/* /*
* Rather than truncating and growing the file each time, * Rather than truncating and growing the file each time,
* use ftruncate if size is less than previous size. * use ftruncate if size is less than previous size.
@ -316,6 +316,12 @@ usage: fprintf(stderr, "usage: rwhod [ -m [ ttl ] ]\n");
} }
} }
static void
usage()
{
fprintf(stderr, "usage: rwhod [-m [ttl]]\n");
exit(1);
}
void void
run_as(uid, gid) run_as(uid, gid)
@ -393,7 +399,7 @@ onalrm(signo)
else else
utmp = (struct utmp *)malloc(utmpsize); utmp = (struct utmp *)malloc(utmpsize);
if (! utmp) { if (! utmp) {
fprintf(stderr, "rwhod: malloc failed\n"); syslog(LOG_WARNING, "malloc failed");
utmpsize = 0; utmpsize = 0;
goto done; goto done;
} }
@ -401,8 +407,7 @@ onalrm(signo)
(void) lseek(utmpf, (off_t)0, L_SET); (void) lseek(utmpf, (off_t)0, L_SET);
cc = read(utmpf, (char *)utmp, stb.st_size); cc = read(utmpf, (char *)utmp, stb.st_size);
if (cc < 0) { if (cc < 0) {
fprintf(stderr, "rwhod: %s: %s\n", syslog(LOG_ERR, "read(%s): %m", _PATH_UTMP);
_PATH_UTMP, strerror(errno));
goto done; goto done;
} }
wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1]; wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];