Fix a selection of bugs, and improve code layout:

- Remove unnecessary and unused local variables.
- Include useful information in error and warning messages.
- Fix the logic for expiring mounttab entries.
- Remove calls to getaddrinfo - the results were not used.
- Simplify some string handling by using snprintf.
- Fix usage.
This commit is contained in:
Ian Dowse 2001-07-22 01:25:25 +00:00
parent 605d466b60
commit d730d3b491
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=80123

View File

@ -59,14 +59,12 @@ struct mtablist *mtabhead;
int
main(int argc, char **argv) {
int ch, keep, success, pathlen;
time_t expire, *now;
time_t expire, now;
char *host, *path;
struct mtablist *mtab;
mtab = NULL;
now = NULL;
expire = 0;
host = path = '\0';
host = path = NULL;
success = keep = verbose = 0;
while ((ch = getopt(argc, argv, "h:kp:ve:")) != -1)
switch (ch) {
@ -99,68 +97,74 @@ main(int argc, char **argv) {
/* Default expiretime is one day */
if (expire == 0)
expire = 86400;
/*
* Read PATH_MOUNTTAB and check each entry
* and do finally the unmounts.
*/
time(&now);
/* Read PATH_MOUNTTAB. */
if (!read_mtab(NULL)) {
if (verbose)
warnx("no mounttab entries (%s does not exist)",
PATH_MOUNTTAB);
mtabhead = NULL;
}
if (host == NULL && path == NULL) {
if (!read_mtab(mtab)) {
if (verbose)
warnx("nothing to do, %s does not exist",
PATH_MOUNTTAB);
}
/* Check each entry and do any necessary unmount RPCs. */
for (mtab = mtabhead; mtab != NULL; mtab = mtab->mtab_next) {
if (*mtab->mtab_host != '\0' ||
mtab->mtab_time <= (time(now) - expire)) {
if (keep && is_mounted(mtab->mtab_host,
mtab->mtab_dirp)) {
if (verbose) {
warnx("skip entry %s:%s",
mtab->mtab_host,
mtab->mtab_dirp);
}
} else if (do_umount(mtab->mtab_host,
mtab->mtab_dirp) ||
mtab->mtab_time <= (time(now) - expire)) {
clean_mtab(mtab->mtab_host,
mtab->mtab_dirp);
}
if (*mtab->mtab_host == '\0')
continue;
if (mtab->mtab_time + expire < now) {
/* Clear expired entry. */
if (verbose)
warnx("remove expired entry %s:%s",
mtab->mtab_host, mtab->mtab_dirp);
bzero(mtab->mtab_host,
sizeof(mtab->mtab_host));
continue;
}
if (keep && is_mounted(mtab->mtab_host,
mtab->mtab_dirp)) {
if (verbose)
warnx("skip entry %s:%s",
mtab->mtab_host, mtab->mtab_dirp);
continue;
}
if (do_umount(mtab->mtab_host, mtab->mtab_dirp)) {
if (verbose)
warnx("umount RPC for %s:%s succeeded",
mtab->mtab_host, mtab->mtab_dirp);
/* Remove all entries for this host + path. */
clean_mtab(mtab->mtab_host, mtab->mtab_dirp);
}
}
/* Only do a RPC UMNTALL for this specific host */
} else if (host != NULL && path == NULL) {
if (!do_umntall(host))
exit(1);
else
success = 1;
/* Someone forgot to enter a hostname */
} else if (host == NULL && path != NULL)
usage();
/* Only do a RPC UMOUNT for this specific mount */
else {
for (pathlen = strlen(path);
pathlen > 1 && path[pathlen - 1] == '/'; pathlen--)
path[pathlen - 1] = '\0';
if (!do_umount(host, path))
exit(1);
else
success = 1;
success = 1;
} else {
if (host == NULL && path != NULL)
/* Missing hostname. */
usage();
if (path == NULL) {
/* Do a RPC UMNTALL for this specific host */
success = do_umntall(host);
if (verbose && success)
warnx("umntall RPC for %s succeeded", host);
} else {
/* Do a RPC UMNTALL for this specific mount */
for (pathlen = strlen(path);
pathlen > 1 && path[pathlen - 1] == '/'; pathlen--)
path[pathlen - 1] = '\0';
success = do_umount(host, path);
if (verbose && success)
warnx("umount RPC for %s:%s succeeded", host,
path);
}
/* If successful, remove any corresponding mounttab entries. */
if (success)
clean_mtab(host, path);
}
/* Write and unlink PATH_MOUNTTAB if necessary */
if (success) {
if (verbose)
warnx("UMOUNT RPC successfully sent to %s", host);
if (read_mtab(mtab)) {
mtab = mtabhead;
clean_mtab(host, path);
}
}
if (!write_mtab()) {
free_mtab();
exit(1);
}
if (success)
success = write_mtab();
free_mtab();
exit(0);
exit (success ? 0 : 1);
}
/*
@ -171,22 +175,12 @@ main(int argc, char **argv) {
int
do_umntall(char *hostname) {
enum clnt_stat clnt_stat;
struct addrinfo *ai, hints;
struct timeval try;
int so, ecode;
CLIENT *clp;
memset(&hints, 0, sizeof hints);
hints.ai_flags = AI_NUMERICHOST;
ecode = getaddrinfo(hostname, NULL, &hints, &ai);
if (ecode != 0) {
warnx("can't get net id for host/nfs: %s",
gai_strerror(ecode));
return (1);
}
clp = clnt_create(hostname, RPCPROG_MNT, RPCMNT_VER1, "udp");
if (clp == NULL) {
clnt_pcreateerror("Cannot MNT PRC");
if (clp == NULL) {
warnx("%s: %s", hostname, clnt_spcreateerror("RPCPROG_MNT"));
return (1);
}
clp->cl_auth = authunix_create_default();
@ -194,8 +188,9 @@ do_umntall(char *hostname) {
try.tv_usec = 0;
clnt_stat = clnt_call(clp, RPCMNT_UMNTALL, xdr_void, (caddr_t)0,
xdr_void, (caddr_t)0, try);
clnt_destroy(clp);
if (clnt_stat != RPC_SUCCESS) {
clnt_perror(clp, "Bad MNT RPC");
warnx("%s: %s", hostname, clnt_sperror(clp, "RPCMNT_UMNTALL"));
return (0);
} else
return (1);
@ -207,22 +202,12 @@ do_umntall(char *hostname) {
int
do_umount(char *hostname, char *dirp) {
enum clnt_stat clnt_stat;
struct addrinfo *ai, hints;
struct timeval try;
CLIENT *clp;
int so, ecode;
memset(&hints, 0, sizeof hints);
hints.ai_flags = AI_NUMERICHOST;
ecode = getaddrinfo(hostname, NULL, &hints, &ai);
if (ecode != 0) {
warnx("can't get net id for host/nfs: %s",
gai_strerror(ecode));
return (1);
}
clp = clnt_create(hostname, RPCPROG_MNT, RPCMNT_VER1, "udp");
if (clp == NULL) {
clnt_pcreateerror("Cannot MNT PRC");
warnx("%s: %s", hostname, clnt_spcreateerror("RPCPROG_MNT"));
return (1);
}
clp->cl_auth = authsys_create_default();
@ -230,8 +215,9 @@ do_umount(char *hostname, char *dirp) {
try.tv_usec = 0;
clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir, dirp,
xdr_void, (caddr_t)0, try);
clnt_destroy(clp);
if (clnt_stat != RPC_SUCCESS) {
clnt_perror(clp, "Bad MNT RPC");
warnx("%s: %s", hostname, clnt_sperror(clp, "RPCMNT_UMOUNT"));
return (0);
}
return (1);
@ -244,17 +230,12 @@ int
is_mounted(char *hostname, char *dirp) {
struct statfs *mntbuf;
char name[MNAMELEN + 1];
size_t bufsize, hostlen, dirlen;
size_t bufsize;
int mntsize, i;
hostlen = strlen(hostname);
dirlen = strlen(dirp);
if ((hostlen + dirlen) >= MNAMELEN)
if (strlen(hostname) + strlen(dirp) >= MNAMELEN)
return (0);
memmove(name, hostname, hostlen);
name[hostlen] = ':';
memmove(name + hostlen + 1, dirp, dirlen);
name[hostlen + dirlen + 1] = '\0';
snprintf(name, sizeof(name), "%s:%s", hostname, dirp);
mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
if (mntsize <= 0)
return (0);
@ -277,14 +258,12 @@ is_mounted(char *hostname, char *dirp) {
*/
int
xdr_dir(XDR *xdrsp, char *dirp) {
return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
}
static void
usage() {
(void)fprintf(stderr, "%s\n",
"usage: rpc.umntall [-h host] [-k] [-p path] [-t expire] [-v]");
"usage: rpc.umntall [-kv] [-e expire] [-h host] [-p path]");
exit(1);
}