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
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=30380
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
status messages which are broadcasted or multicasted on a network.
As a consumer of information, it listens for other
.Nm rwhod
.Nm
servers' status messages, validating them, then recording
them in a collection of files located in the directory
.Pa /var/rwho .
.Pp
The
.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
the IFF_MULTICAST flag set in their "ifnet" structs
(excluding the loopback interface). The multicast
@ -76,9 +78,15 @@ If the optional
.Ar ttl
argument is supplied with the
.Fl m
flag, rwhod will send IP multicast datagrams with a
time-to-live of <ttl>, via a SINGLE interface rather
than all interfaces. <ttl> must be between 0 and
flag,
.Nm
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
.Fl m Ar 1
is different than
@ -92,14 +100,21 @@ When
is used without a
.Ar ttl
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
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
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
reports of old, non-multicasting rwhods, but, if multicasting is used,
those old rwhods won't hear the reports generated by this program.
reports of old, non-multicasting
.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
The server transmits and receives messages at the port indicated
in the ``rwho'' service specification; see
@ -153,7 +168,7 @@ in the message, contains any unprintable
.Tn ASCII
characters, the
message is discarded. Valid messages received by
.Nm rwhod
.Nm
are placed in files named
.Pa whod.hostname
in the directory

View File

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