#include <stdio.h>
#include <stdio_ext.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/signal.h>
#include <sys/mnttab.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <sys/vfstab.h>
#include <sys/fcntl.h>
#include <sys/resource.h>
#include <sys/mntent.h>
#include <sys/ctfs.h>
#include <locale.h>
#include <priv.h>
#include <stdarg.h>
#include <sys/mount.h>
#include <sys/objfs.h>
#include "fslib.h"
#include <sharefs/share.h>
#define FS_PATH "/usr/lib/fs"
#define ALT_PATH "/etc/fs"
#define FULLPATH_MAX 32
#define FSTYPE_MAX 8
#define ARGV_MAX 16
int aflg, oflg, Vflg, dashflg, dflg, fflg;
extern void rpterr(), usage(), mnterror();
extern char *optarg;
extern int optind, opterr;
static char *myname;
char fs_path[] = FS_PATH;
char alt_path[] = ALT_PATH;
char mnttab[MAXPATHLEN + 1];
char *oarg, *farg;
int maxrun, nrun;
int no_mnttab;
int lofscnt;
int exitcode;
char resolve[MAXPATHLEN];
static char ibuf[BUFSIZ];
typedef struct mountent {
struct mnttab ment;
int mlevel;
pid_t pid;
#define RDPIPE 0
#define WRPIPE 1
int sopipe[2];
int sepipe[2];
struct mountent *link;
} mountent_t;
static mountent_t *mntll;
int listlength;
static const char *keeplist[] = {
"/",
"/dev",
"/dev/fd",
"/devices",
"/etc/mnttab",
"/etc/svc/volatile",
"/lib",
"/proc",
"/sbin",
CTFS_ROOT,
OBJFS_ROOT,
"/tmp",
"/usr",
"/var",
"/var/adm",
"/var/run",
SHARETAB,
NULL
};
static void nomem();
static void doexec(struct mnttab *);
static int setup_iopipe(mountent_t *);
static void setup_output(mountent_t *);
static void doio(mountent_t *);
static void do_umounts(mountent_t **);
static int dowait();
static int parumount();
static int mcompar(const void *, const void *);
static void cleanup(int);
static mountent_t **make_mntarray(char **, int);
static mountent_t *getmntall();
static mountent_t *new_mountent(struct mnttab *);
static mountent_t *getmntlast(mountent_t *, char *, char *);
int
main(int argc, char **argv)
{
int cc;
struct mnttab mget;
char *mname, *is_special;
int fscnt;
mountent_t *mp;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
myname = strrchr(argv[0], '/');
if (myname)
myname++;
else
myname = argv[0];
while ((cc = getopt(argc, argv, "ado:Vf?")) != -1)
switch (cc) {
case 'a':
aflg++;
break;
#ifdef DEBUG
case 'd':
dflg++;
break;
#endif
case '?':
usage();
break;
case 'o':
if (oflg)
usage();
else {
oflg++;
oarg = optarg;
}
break;
case 'f':
fflg++;
break;
case 'V':
if (Vflg)
usage();
else
Vflg++;
break;
default:
usage();
break;
}
fscnt = argc - optind;
if (!aflg && fscnt != 1)
usage();
if (strcmp(argv[optind-1], "--") == 0)
dashflg++;
if (realpath(MNTTAB, mnttab) == NULL) {
strcpy(mnttab, MNTTAB);
}
mname = argv[optind];
is_special = realpath(mname, resolve);
mntll = getmntall();
if (aflg && fscnt != 1)
exit(parumount(argv + optind, fscnt));
aflg = 0;
mntnull(&mget);
if (listlength == 0) {
fprintf(stderr, gettext(
"%s: warning: no entries found in %s\n"),
myname, mnttab);
mget.mnt_mountp = mname;
no_mnttab++;
doexec(&mget);
exit(0);
}
mp = NULL;
if (is_special != NULL) {
mp = getmntlast(mntll, NULL, resolve);
}
if (mp == NULL) {
if ((mp = getmntlast(mntll, NULL, mname)) == NULL)
mp = getmntlast(mntll, mname, NULL);
if (mp) {
mountent_t *lmp;
lmp = getmntlast(mntll, NULL, mp->ment.mnt_mountp);
if (lmp && strcmp(lmp->ment.mnt_special,
mp->ment.mnt_special)) {
errno = EBUSY;
rpterr(mname);
exit(1);
}
} else {
fprintf(stderr, gettext(
"%s: warning: %s not in mnttab\n"),
myname, mname);
if (Vflg)
exit(1);
mget.mnt_special = mget.mnt_mountp = mname;
}
}
if (mp)
doexec(&mp->ment);
else
doexec(&mget);
return (0);
}
void
doexec(struct mnttab *ment)
{
int ret;
#ifdef DEBUG
if (dflg)
fprintf(stderr, "%d: umounting %s\n",
getpid(), ment->mnt_mountp);
#endif
if ((ment->mnt_fstype != NULL) || Vflg) {
char full_path[FULLPATH_MAX];
char alter_path[FULLPATH_MAX];
char *newargv[ARGV_MAX];
int ii;
if (strlen(ment->mnt_fstype) > (size_t)FSTYPE_MAX) {
fprintf(stderr, gettext(
"%s: FSType %s exceeds %d characters\n"),
myname, ment->mnt_fstype, FSTYPE_MAX);
exit(1);
}
sprintf(full_path, "%s/%s/%s", fs_path, ment->mnt_fstype,
myname);
sprintf(alter_path, "%s/%s/%s", alt_path, ment->mnt_fstype,
myname);
ii = 2;
if (oflg) {
newargv[ii++] = "-o";
newargv[ii++] = oarg;
}
if (dashflg) {
newargv[ii++] = "--";
}
if (fflg) {
newargv[ii++] = "-f";
}
newargv[ii++] = (ment->mnt_mountp)
? ment->mnt_mountp : ment->mnt_special;
newargv[ii] = NULL;
newargv[1] = myname;
if (Vflg) {
printf("%s", myname);
for (ii = 2; newargv[ii]; ii++)
printf(" %s", newargv[ii]);
printf("\n");
fflush(stdout);
exit(0);
}
if (strcmp(ment->mnt_fstype, "smbfs") == 0 &&
setpflags(PRIV_PFEXEC, 1) != 0) {
(void) fprintf(stderr,
gettext("umount: unable to set PFEXEC flag: %s\n"),
strerror(errno));
}
execv(full_path, &newargv[1]);
if (errno == ENOEXEC) {
newargv[0] = "sh";
newargv[1] = full_path;
execv("/sbin/sh", &newargv[0]);
}
newargv[1] = myname;
execv(alter_path, &newargv[1]);
if (errno == ENOEXEC) {
newargv[0] = "sh";
newargv[1] = alter_path;
execv("/sbin/sh", &newargv[0]);
}
if (errno != ENOENT) {
fprintf(stderr, gettext("umount: cannot execute %s\n"),
full_path);
exit(1);
}
}
if (oflg) {
fprintf(stderr, gettext(
"%s: %s specific umount does not exist;"
" -o suboption ignored\n"),
myname, ment->mnt_fstype ? ment->mnt_fstype : "<null>");
}
signal(SIGHUP, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGINT, SIG_IGN);
if (fflg) {
if (((ret = umount2(ment->mnt_mountp, MS_FORCE)) < 0) &&
(errno != EBUSY && errno != ENOTSUP &&
errno != EPERM))
ret = umount2(ment->mnt_special, MS_FORCE);
} else {
if (((ret = umount2(ment->mnt_mountp, 0)) < 0) &&
(errno != EBUSY) && (errno != EPERM))
ret = umount2(ment->mnt_special, 0);
}
if (ret < 0) {
rpterr(ment->mnt_mountp);
if (errno != EINVAL && errno != EFAULT)
exit(1);
exitcode = 1;
}
exit(exitcode);
}
void
rpterr(char *sp)
{
switch (errno) {
case EPERM:
fprintf(stderr, gettext("%s: permission denied\n"), myname);
break;
case ENXIO:
fprintf(stderr, gettext("%s: %s no device\n"), myname, sp);
break;
case ENOENT:
fprintf(stderr,
gettext("%s: %s no such file or directory\n"),
myname, sp);
break;
case EINVAL:
fprintf(stderr, gettext("%s: %s not mounted\n"), myname, sp);
break;
case EBUSY:
fprintf(stderr, gettext("%s: %s busy\n"), myname, sp);
break;
case ENOTBLK:
fprintf(stderr,
gettext("%s: %s block device required\n"), myname, sp);
break;
case ECOMM:
fprintf(stderr,
gettext("%s: warning: broken link detected\n"), myname);
break;
default:
perror(myname);
fprintf(stderr, gettext("%s: cannot unmount %s\n"), myname, sp);
}
}
void
usage(void)
{
fprintf(stderr, gettext(
"Usage:\n%s [-f] [-V] [-o specific_options] {special | mount-point}\n"),
myname);
fprintf(stderr, gettext(
"%s -a [-f] [-V] [-o specific_options] [mount_point ...]\n"), myname);
exit(1);
}
void
mnterror(int flag)
{
switch (flag) {
case MNT_TOOLONG:
fprintf(stderr,
gettext("%s: line in mnttab exceeds %d characters\n"),
myname, MNT_LINE_MAX-2);
break;
case MNT_TOOFEW:
fprintf(stderr,
gettext("%s: line in mnttab has too few entries\n"),
myname);
break;
default:
break;
}
}
mountent_t *
getmntlast(mountent_t *mlist, char *specp, char *mntp)
{
int mfound, sfound;
for (; mlist; mlist = mlist->link) {
mfound = sfound = 0;
if (mntp && (strcmp(mlist->ment.mnt_mountp, mntp) == 0)) {
if (specp == NULL)
return (mlist);
mfound++;
}
if (specp && (strcmp(mlist->ment.mnt_special, specp) == 0)) {
if (mntp == NULL)
return (mlist);
sfound++;
}
if (mfound && sfound)
return (mlist);
}
return (NULL);
}
int
parumount(char **mntlist, int count)
{
int maxfd = OPEN_MAX;
struct rlimit rl;
mountent_t **mntarray, **ml, *mp;
if (count == 0 && mntll == NULL)
return (0);
if ((maxrun = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
maxrun = 4;
else
maxrun = maxrun * 2 + 1;
if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
rl.rlim_cur = rl.rlim_max;
if (setrlimit(RLIMIT_NOFILE, &rl) == 0)
maxfd = (int)rl.rlim_cur;
(void) enable_extended_FILE_stdio(-1, -1);
}
maxfd = (maxfd / 2) - 6;
if (maxfd < maxrun)
maxrun = maxfd;
if (maxrun < 4)
maxrun = 4;
mntarray = make_mntarray(mntlist, count);
if (listlength == 0) {
if (count == 0)
return (0);
fprintf(stderr, gettext("%s: no valid entries found in %s\n"),
myname, mnttab);
return (1);
}
if (lofscnt == 0) {
qsort((void *)mntarray, listlength, sizeof (mountent_t *),
mcompar);
lofscnt = -1;
}
mntll = mntarray[0];
for (ml = mntarray; mp = *ml; )
mp->link = *++ml;
sigset(SIGHUP, cleanup);
sigset(SIGQUIT, cleanup);
sigset(SIGINT, cleanup);
do_umounts(mntarray);
return (exitcode);
}
mountent_t **
make_mntarray(char **mntlist, int count)
{
mountent_t *mp, **mpp;
int ndx;
char *cp;
if (count > 0)
listlength = count;
mpp = (mountent_t **)malloc(sizeof (*mp) * (listlength + 1));
if (mpp == NULL)
nomem();
if (count == 0) {
if (mntll == NULL) {
listlength = 0;
return (NULL);
}
for (ndx = 0, mp = mntll; mp; mp = mp->link) {
if (fsstrinlist(mp->ment.mnt_mountp, keeplist))
continue;
mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp);
if (mp->ment.mnt_fstype &&
(strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
lofscnt++;
mpp[ndx++] = mp;
}
mpp[ndx] = NULL;
listlength = ndx;
return (mpp);
}
for (ndx = 0; count--; ) {
cp = *mntlist++;
if (realpath(cp, resolve) == NULL) {
fprintf(stderr,
gettext("%s: warning: can't resolve %s\n"),
myname, cp);
exitcode = 1;
mp = getmntlast(mntll, NULL, cp);
} else
mp = getmntlast(mntll, NULL, resolve);
if (mp == NULL) {
struct mnttab mnew;
fprintf(stderr, gettext(
"%s: warning: %s not found in %s\n"),
myname, resolve, mnttab);
exitcode = 1;
mntnull(&mnew);
mnew.mnt_special = mnew.mnt_mountp = strdup(resolve);
if (mnew.mnt_special == NULL)
nomem();
mp = new_mountent(&mnew);
}
if (mp->ment.mnt_fstype &&
(strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
lofscnt++;
mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp);
mpp[ndx++] = mp;
}
mpp[ndx] = NULL;
listlength = ndx;
return (mpp);
}
mountent_t *
getmntall(void)
{
FILE *fp;
mountent_t *mtail;
int cnt = 0, ret;
struct mnttab mget;
if ((fp = fopen(mnttab, "r")) == NULL) {
fprintf(stderr, gettext("%s: warning cannot open %s\n"),
myname, mnttab);
return (0);
}
mtail = NULL;
while ((ret = getmntent(fp, &mget)) != -1) {
mountent_t *mp;
if (ret > 0) {
mnterror(ret);
continue;
}
mp = new_mountent(&mget);
mp->link = mtail;
mtail = mp;
cnt++;
}
fclose(fp);
if (mtail == NULL) {
listlength = 0;
return (NULL);
}
listlength = cnt;
return (mtail);
}
void
do_umounts(mountent_t **mntarray)
{
mountent_t *mp, *mpprev, **ml = mntarray;
int cnt = listlength;
for (mpprev = *ml; mp = *ml; mpprev = mp, ml++, cnt--) {
pid_t pid;
if (mp->mlevel < mpprev->mlevel || lofscnt > 0)
while (nrun > 0 && (dowait() != -1))
;
if (lofscnt == 0) {
qsort((void *)ml, cnt, sizeof (mountent_t *), mcompar);
mp = *ml;
lofscnt--;
}
while (setup_iopipe(mp) == -1 && (dowait() != -1))
;
while (nrun >= maxrun && (dowait() != -1))
;
if ((pid = fork()) == -1) {
perror("fork");
cleanup(-1);
}
#ifdef DEBUG
if (dflg && pid > 0) {
fprintf(stderr, "parent %d: umounting %d %s\n",
getpid(), pid, mp->ment.mnt_mountp);
}
#endif
if (pid == 0) {
signal(SIGHUP, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGINT, SIG_IGN);
setup_output(mp);
doexec(&mp->ment);
perror("exec");
exit(1);
}
(void) close(mp->sopipe[WRPIPE]);
(void) close(mp->sepipe[WRPIPE]);
mp->pid = pid;
nrun++;
}
cleanup(0);
}
void
cleanup(int asig)
{
while (nrun > 0 && (dowait() != -1))
;
if (asig != 0)
exit(1);
}
int
dowait(void)
{
int wstat, child, ret;
mountent_t *mp, *prevp;
if ((child = wait(&wstat)) == -1)
return (-1);
if (WIFEXITED(wstat))
ret = WEXITSTATUS(wstat);
else
ret = 1;
nrun--;
if (ret)
exitcode = 1;
for (prevp = NULL, mp = mntll; mp; mp = mp->link) {
if (mp->pid != child) {
prevp = mp;
continue;
}
if (prevp) {
prevp->link = mp->link;
mp->link = NULL;
}
break;
}
if (mp == NULL) {
#ifdef DEBUG
fprintf(stderr, gettext(
"%s: unknown child %d\n"), myname, child);
#endif
exitcode = 1;
return (1);
}
doio(mp);
if (mp->ment.mnt_fstype &&
(strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
lofscnt--;
return (ret);
}
static const mountent_t zmount = { 0 };
mountent_t *
new_mountent(struct mnttab *ment)
{
mountent_t *new;
new = (mountent_t *)malloc(sizeof (*new));
if (new == NULL)
nomem();
*new = zmount;
if (ment->mnt_special &&
(new->ment.mnt_special = strdup(ment->mnt_special)) == NULL)
nomem();
if (ment->mnt_mountp &&
(new->ment.mnt_mountp = strdup(ment->mnt_mountp)) == NULL)
nomem();
if (ment->mnt_fstype &&
(new->ment.mnt_fstype = strdup(ment->mnt_fstype)) == NULL)
nomem();
return (new);
}
int
mcompar(const void *a, const void *b)
{
mountent_t *a1, *b1;
a1 = *(mountent_t **)a;
b1 = *(mountent_t **)b;
return (b1->mlevel - a1->mlevel);
}
int
setup_iopipe(mountent_t *mp)
{
if (pipe(mp->sopipe) == -1)
return (-1);
if (pipe(mp->sepipe) == -1) {
(void) close(mp->sopipe[RDPIPE]);
(void) close(mp->sopipe[WRPIPE]);
return (-1);
}
(void) fcntl(mp->sopipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
(void) fcntl(mp->sepipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
return (0);
}
void
setup_output(mountent_t *mp)
{
(void) close(fileno(stdout));
(void) dup(mp->sopipe[WRPIPE]);
(void) close(mp->sopipe[WRPIPE]);
(void) close(fileno(stderr));
(void) dup(mp->sepipe[WRPIPE]);
(void) close(mp->sepipe[WRPIPE]);
}
static void
doio(mountent_t *mp)
{
int bytes;
while ((bytes = read(mp->sepipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
write(fileno(stderr), ibuf, bytes);
while ((bytes = read(mp->sopipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
write(fileno(stdout), ibuf, bytes);
(void) close(mp->sopipe[RDPIPE]);
(void) close(mp->sepipe[RDPIPE]);
}
void
nomem(void)
{
fprintf(stderr, gettext("%s: out of memory\n"), myname);
while (nrun > 0 && (dowait() != -1))
;
exit(1);
}