/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2015 Nexenta Systems, Inc. All rights reserved.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <stdio.h>
#include <stdio_ext.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/resource.h>
#include <locale.h>
#include <stdarg.h>
#include "fslib.h"
extern char *optarg; /* used by getopt */
static char *myname;
int no_mnttab;
/* umounting */
int exitcode;
/*
* The basic mount struct that describes an mnttab entry.
* It is used both in an array and as a linked list elem.
*/
typedef struct mountent {
#define RDPIPE 0
} mountent_t;
/* mountents */
/*
* If the automatic flag (-a) is given and mount points are not specified
* on the command line, then do not attempt to umount these. These
* generally need to be kept mounted until system shutdown.
*/
static const char *keeplist[] = {
"/",
"/dev",
"/devices",
"/lib",
"/proc",
"/sbin",
"/tmp",
"/usr",
"/var",
};
static void nomem();
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();
int
{
int cc;
int fscnt;
#if !defined(TEXT_DOMAIN)
#endif
(void) textdomain(TEXT_DOMAIN);
if (myname)
myname++;
else
/*
* Process the args.
* "-d" for compatibility
*/
switch (cc) {
case 'a':
aflg++;
break;
#ifdef DEBUG
case 'd':
dflg++;
break;
#endif
case '?':
usage();
break;
case 'o':
if (oflg)
usage();
else {
oflg++;
}
break;
case 'f':
fflg++;
break;
case 'V':
if (Vflg)
usage();
else
Vflg++;
break;
default:
usage();
break;
}
usage();
/* copy '--' to specific */
dashflg++;
/*
* mnttab may be a symlink to a file in another file system.
* This happens during install when / is mounted read-only
* If this is the case, we need to follow the symlink to the
* read-write file itself so that the subsequent mnttab.temp
* open and rename will work.
*/
}
/*
* bugid 1205242
* call the realpath() here, so that if the user is
* trying to umount an autofs directory, the directory
* is forced to mount.
*/
/*
* Read the whole mnttab into memory.
*/
aflg = 0;
if (listlength == 0) {
"%s: warning: no entries found in %s\n"),
no_mnttab++;
exit(0);
}
/*
* if realpath fails, it can't be a mount point, so we'll
* go straight to the code that treats the arg as a special.
* if realpath succeeds, it could be a special or a mount point;
* we'll start by assuming it's a mount point, and if it's not,
* try to treat it as a special.
*/
if (is_special != NULL) {
/*
* if this succeeds,
* we'll have the appropriate record; if it fails
* we'll assume the arg is a special of some sort
*/
}
/*
* Since stackable mount is allowed (RFE 2001535),
* we will un-mount the last entry in the MNTTAB that matches.
*/
/*
* Perhaps there is a bogus mnttab entry that
* can't be resolved:
*/
/*
* assume it's a device (special) now
*/
if (mp) {
/*
* Found it.
* This is a device. Now we want to know if
* it stackmounted on by something else.
* The original fix for bug 1103850 has a
* problem with lockfs (bug 1119731). This
* is a revised method.
*/
exit(1);
}
} else {
"%s: warning: %s not in mnttab\n"),
if (Vflg)
exit(1);
/*
* same error as mount -V
* would give for unknown
* mount point
*/
}
}
if (mp)
else
return (0);
}
void
{
int ret;
#ifdef DEBUG
if (dflg)
#endif
/* try to exec the dependent portion */
int ii;
"%s: FSType %s exceeds %d characters\n"),
exit(1);
}
/* build the full pathname of the fstype dependent command. */
myname);
myname);
/*
* create the new arg list, and end the list with a
* null pointer
*/
ii = 2;
if (oflg) {
}
if (dashflg) {
}
if (fflg) {
}
/* set the new argv[0] to the filename */
if (Vflg) {
printf("\n");
exit(0);
}
/* Try to exec the fstype dependent umount. */
newargv[0] = "sh";
}
newargv[0] = "sh";
}
/* exec failed */
exit(1);
}
}
/*
* No fstype independent executable then. We'll go generic
* from here.
*/
/* don't use -o with generic */
if (oflg) {
"%s: %s specific umount does not exist;"
" -o suboption ignored\n"),
}
/*
* Try to umount the mountpoint.
* If that fails, try the corresponding special.
* (This ordering is necessary for nfs umounts.)
* (for remote resources: if the first umount returns EBUSY
* don't call umount again - umount() with a resource name
* will return a misleading error to the user
*/
if (fflg) {
} else {
}
if (ret < 0) {
exit(1);
exitcode = 1;
}
}
void
{
switch (errno) {
case EPERM:
break;
case ENXIO:
break;
case ENOENT:
gettext("%s: %s no such file or directory\n"),
break;
case EINVAL:
break;
case EBUSY:
break;
case ENOTBLK:
break;
case ECOMM:
break;
default:
}
}
void
usage(void)
{
"Usage:\n%s [-f] [-V] [-o specific_options] {special | mount-point}\n"),
myname);
"%s -a [-f] [-V] [-o specific_options] [mount_point ...]\n"), myname);
exit(1);
}
void
{
switch (flag) {
case MNT_TOOLONG:
gettext("%s: line in mnttab exceeds %d characters\n"),
break;
case MNT_TOOFEW:
gettext("%s: line in mnttab has too few entries\n"),
myname);
break;
default:
break;
}
}
/*
* Search the mlist linked list for the
* first match of specp or mntp. The list is expected to be in reverse
* If both are specified, then both have to match.
* Returns the (mountent_t *) of the match, otherwise returns NULL.
*/
{
return (mlist);
mfound++;
}
return (mlist);
sfound++;
}
return (mlist);
}
return (NULL);
}
/*
* Perform the parallel version of umount. Returns 0 if no errors occurred,
* non zero otherwise.
*/
int
{
/*
* If no mount points are specified and none were found in mnttab,
* then end it all here.
*/
return (0);
/*
* This is the process scaling section. After running a series
* of tests based on the number of simultaneous processes and
* processors available, optimum performance was achieved near or
* at (PROCN * 2).
*/
maxrun = 4;
else
}
/*
* The parent needs to maintain 3 of its own fd's, plus 2 for
* each child (the stdout and stderr pipes).
*/
/* periods of open fds */
if (maxrun < 4)
if (listlength == 0) {
if (count == 0) /* not an error, just none found */
return (0);
return (1);
}
/*
* Sort the entries based on their mount level only if lofs's are
* not present.
*/
if (lofscnt == 0) {
mcompar);
/*
* If we do not detect a lofs by now, we never will.
*/
lofscnt = -1;
}
/*
* Now link them up so that a given pid is easier to find when
* we go to clean up after they are done.
*/
/*
* Try to handle interrupts in a reasonable way.
*/
return (exitcode);
}
/*
* Returns a mountent_t array based on mntlist. If mntlist is NULL, then
* it returns all mnttab entries with a few exceptions. Sets the global
* variable listlength to the number of entries in the array.
*/
mountent_t **
{
int ndx;
char *cp;
if (count > 0)
listlength = count;
nomem();
if (count == 0) {
listlength = 0;
return (NULL);
}
/*
* No mount list specified: take all mnttab mount points
* except for a few cases.
*/
continue;
lofscnt++;
}
listlength = ndx;
return (mpp);
}
/*
* A list of mount points was specified on the command line.
* Build an array out of these.
*/
gettext("%s: warning: can't resolve %s\n"),
exitcode = 1;
} else
/*
* Then we've reached the end without finding
* what we are looking for, but we still have to
* try to umount it: append it to mntarray.
*/
"%s: warning: %s not found in %s\n"),
exitcode = 1;
nomem();
}
lofscnt++;
}
listlength = ndx;
return (mpp);
}
/*
* Returns the tail of a linked list of all mnttab entries. I.e, it's faster
* to return the mnttab in reverse order.
* Sets listlength to the number of entries in the list.
* Returns NULL if none are found.
*/
getmntall(void)
{
return (0);
}
if (ret > 0) {
continue;
}
cnt++;
}
listlength = 0;
return (NULL);
}
listlength = cnt;
return (mtail);
}
void
{
/*
* Main loop for the forked children:
*/
/*
* Check to see if we cross a mount level: e.g.,
* /a/b/c -> /a/b. If so, we need to wait for all current
* umounts to finish before umounting the rest.
*
* Also, we unmount serially as long as there are lofs's
* to mount to avoid improper umount ordering.
*/
;
if (lofscnt == 0) {
/*
* We can now go to parallel umounting.
*/
lofscnt--; /* so we don't do this again */
}
;
;
perror("fork");
cleanup(-1);
/* not reached */
}
#ifdef DEBUG
}
#endif
if (pid == 0) { /* child */
perror("exec");
exit(1);
}
/* parent */
nrun++;
}
cleanup(0);
}
/*
* cleanup the existing children and exit with an error
* if asig != 0.
*/
void
{
/*
* Let the stragglers finish.
*/
;
if (asig != 0)
exit(1);
}
/*
* Waits for 1 child to die.
*
* Returns -1 if no children are left to wait for.
* Returns 0 if a child died without an error.
* Returns 1 if a child died with an error.
* Sets the global exitcode if an error occurred.
*/
int
dowait(void)
{
return (-1);
else
nrun--;
if (ret)
exitcode = 1;
/*
* Find our child so we can process its std output, if any.
* This search gets smaller and smaller as children are cleaned
* up.
*/
continue;
}
/*
* Found: let's remove it from this list.
*/
if (prevp) {
}
break;
}
/*
* This should never happen.
*/
#ifdef DEBUG
#endif
exitcode = 1;
return (1);
}
lofscnt--;
return (ret);
}
{
nomem();
if (ment->mnt_special &&
nomem();
if (ment->mnt_mountp &&
nomem();
if (ment->mnt_fstype &&
nomem();
return (new);
}
/*
* Sort in descending order of "mount level". For example, /a/b/c is
* placed before /a/b .
*/
int
mcompar(const void *a, const void *b)
{
a1 = *(mountent_t **)a;
b1 = *(mountent_t **)b;
}
/*
* The purpose of this routine is to form stdout and stderr
* pipes for the children's output. The parent then reads and writes it
* out it serially in order to ensure that the output is
* not garbled.
*/
int
{
/*
* Make a stdout and stderr pipe. This should never fail.
*/
return (-1);
return (-1);
}
/*
* Don't block on an empty pipe.
*/
return (0);
}
/*
* Called by a child to attach its stdout and stderr to the write side of
* the pipes.
*/
void
{
}
/*
* Parent uses this to print any stdout or stderr output issued by
* the child.
*/
static void
{
int bytes;
}
void
nomem(void)
{
/*
* Let the stragglers finish.
*/
;
exit(1);
}