#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <unistd.h>
#include <kstat.h>
#include <rpc/rpc.h>
#include <sys/mnttab.h>
#include <sys/mount.h>
#include <sys/mntent.h>
#include <nfs/nfs.h>
#include <nfs/nfs_clnt.h>
#include <rpcsvc/mount.h>
#include <errno.h>
#include <locale.h>
#include <fslib.h>
#include <priv.h>
#include <tsol/label.h>
#include "replica.h"
#define RET_OK 0
#define RET_ERR 32
#ifdef __STDC__
static void pr_err(const char *fmt, ...);
#else
static void pr_err(char *fmt, va_dcl);
#endif
static void usage();
static int nfs_unmount(char *, int);
static void inform_server(char *, char *, bool_t);
static struct extmnttab *mnttab_find();
extern int __clnt_bindresvport(CLIENT *);
static int is_v4_mount(struct extmnttab *);
static char *myname;
static char typename[64];
int
main(int argc, char *argv[])
{
extern int optind;
int c;
int umnt_flag = 0;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
myname = strrchr(argv[0], '/');
myname = myname ? myname+1 : argv[0];
(void) snprintf(typename, sizeof (typename), "nfs %s", myname);
argv[0] = typename;
while ((c = getopt(argc, argv, "f")) != EOF) {
switch (c) {
case 'f':
umnt_flag |= MS_FORCE;
break;
default:
usage();
exit(RET_ERR);
}
}
if (argc - optind != 1) {
usage();
exit(RET_ERR);
}
if (!priv_ineffect(PRIV_SYS_MOUNT) ||
!priv_ineffect(PRIV_NET_PRIVADDR)) {
pr_err(gettext("insufficient privileges\n"));
exit(RET_ERR);
}
if (is_system_labeled())
(void) setpflags(NET_MAC_AWARE, 1);
return (nfs_unmount(argv[optind], umnt_flag));
}
static void
pr_err(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(void) fprintf(stderr, "%s: ", typename);
(void) vfprintf(stderr, fmt, ap);
(void) fflush(stderr);
va_end(ap);
}
static void
usage()
{
(void) fprintf(stderr,
gettext("Usage: nfs umount [-f] {server:path | dir}\n"));
exit(RET_ERR);
}
static int
nfs_unmount(char *pathname, int umnt_flag)
{
struct extmnttab *mntp;
bool_t quick = FALSE;
int is_v4 = FALSE;
mntp = mnttab_find(pathname);
if (mntp) {
pathname = mntp->mnt_mountp;
}
if (mntp)
is_v4 = is_v4_mount(mntp);
if (umount2(pathname, umnt_flag) < 0) {
if (errno == EBUSY) {
pr_err(gettext("%s: is busy\n"), pathname);
} else {
pr_err(gettext("%s: not mounted\n"), pathname);
}
return (RET_ERR);
}
if (is_v4 == TRUE)
return (RET_OK);
if (umnt_flag & MS_FORCE)
quick = TRUE;
if (mntp) {
inform_server(mntp->mnt_special, mntp->mnt_mntopts, quick);
}
return (RET_OK);
}
static struct extmnttab *
mnttab_find(dirname)
char *dirname;
{
FILE *fp;
struct extmnttab mnt;
struct extmnttab *res = NULL;
fp = fopen(MNTTAB, "r");
if (fp == NULL) {
pr_err("%s: %s\n", MNTTAB, strerror(errno));
return (NULL);
}
while (getextmntent(fp, &mnt, sizeof (struct extmnttab)) == 0) {
if (strcmp(mnt.mnt_mountp, dirname) == 0 ||
strcmp(mnt.mnt_special, dirname) == 0) {
if (res)
fsfreemnttab(res);
res = fsdupmnttab(&mnt);
}
}
(void) fclose(fp);
return (res);
}
static void
inform_server(char *string, char *opts, bool_t quick)
{
struct timeval timeout;
CLIENT *cl;
enum clnt_stat rpc_stat;
struct replica *list;
int i, n;
char *p = NULL;
static struct timeval create_timeout = {5, 0};
static struct timeval *timep;
list = parse_replica(string, &n);
if (list == NULL) {
if (n < 0)
pr_err(gettext("%s is not hostname:path format\n"),
string);
else
pr_err(gettext("no memory\n"));
return;
}
if (opts != NULL)
p = strstr(opts, MNTOPT_PUBLIC);
if (p != NULL) {
i = strlen(MNTOPT_PUBLIC);
if (((p == opts) || (*(p-1) == ',')) &&
((p[i] == ',') || (p[i] == '\0')))
return;
}
for (i = 0; i < n; i++) {
int vers;
if (strcmp(list[i].host, "nfs") == 0 && strncmp(list[i].path,
"//", 2) == 0)
continue;
vers = MOUNTVERS;
retry:
timep = (quick ? &create_timeout : NULL);
cl = clnt_create_timed(list[i].host, MOUNTPROG, vers,
"datagram_n", timep);
if (cl == NULL) {
if (!quick)
pr_err("%s:%s %s\n", list[i].host, list[i].path,
clnt_spcreateerror(
"server not responding"));
continue;
}
if (__clnt_bindresvport(cl) < 0) {
if (!quick)
pr_err(gettext(
"couldn't bind to reserved port\n"));
clnt_destroy(cl);
return;
}
if ((cl->cl_auth = authsys_create_default()) == NULL) {
if (!quick)
pr_err(gettext(
"couldn't create authsys structure\n"));
clnt_destroy(cl);
return;
}
timeout.tv_usec = 0;
timeout.tv_sec = 5;
clnt_control(cl, CLSET_RETRY_TIMEOUT, (char *)&timeout);
timeout.tv_sec = 25;
rpc_stat = clnt_call(cl, MOUNTPROC_UMNT, xdr_dirpath,
(char *)&list[i].path, xdr_void, (char *)NULL,
timeout);
AUTH_DESTROY(cl->cl_auth);
clnt_destroy(cl);
if (rpc_stat == RPC_PROGVERSMISMATCH && vers == MOUNTVERS) {
vers = MOUNTVERS3;
goto retry;
}
if (rpc_stat != RPC_SUCCESS)
pr_err("%s\n", clnt_sperror(cl, "unmount"));
}
free_replica(list, n);
}
int
is_v4_mount(struct extmnttab *mntp)
{
kstat_ctl_t *kc = NULL;
kstat_t *ksp;
struct mntinfo_kstat mik;
if (mntp == NULL)
return (FALSE);
if ((kc = kstat_open()) == NULL)
return (FALSE);
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
if (ksp->ks_type != KSTAT_TYPE_RAW)
continue;
if (strcmp(ksp->ks_module, "nfs") != 0)
continue;
if (strcmp(ksp->ks_name, "mntinfo") != 0)
continue;
if (mntp->mnt_minor != ksp->ks_instance)
continue;
if (kstat_read(kc, ksp, &mik) == -1)
continue;
if (mik.mik_vers == 4)
return (TRUE);
else
return (FALSE);
}
return (FALSE);
}