4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
32 #include <stdio_ext.h>
37 #include <sys/signal.h>
38 #include <sys/mnttab.h>
40 #include <sys/types.h>
42 #include <sys/param.h>
44 #include <sys/vfstab.h>
45 #include <sys/fcntl.h>
46 #include <sys/resource.h>
47 #include <sys/mntent.h>
51 #include <sys/mount.h>
52 #include <sys/objfs.h>
54 #include <sharefs/share.h>
56 #define FS_PATH "/usr/lib/fs"
57 #define ALT_PATH "/etc/fs"
58 #define FULLPATH_MAX 32
62 int aflg
, oflg
, Vflg
, dashflg
, dflg
, fflg
;
64 extern void rpterr(), usage(), mnterror();
66 extern char *optarg
; /* used by getopt */
67 extern int optind
, opterr
;
70 char fs_path
[] = FS_PATH
;
71 char alt_path
[] = ALT_PATH
;
72 char mnttab
[MAXPATHLEN
+ 1];
76 int lofscnt
; /* presence of lofs prohibits parallel */
79 char resolve
[MAXPATHLEN
];
80 static char ibuf
[BUFSIZ
];
83 * The basic mount struct that describes an mnttab entry.
84 * It is used both in an array and as a linked list elem.
87 typedef struct mountent
{
88 struct mnttab ment
; /* the mnttab data */
89 int mlevel
; /* mount level of the mount pt */
90 pid_t pid
; /* the pid of this mount process */
93 int sopipe
[2]; /* pipe attached to child's stdout */
94 int sepipe
[2]; /* pipe attached to child's stderr */
95 struct mountent
*link
; /* used when in linked list */
98 static mountent_t
*mntll
; /* head of global linked list of */
100 int listlength
; /* # of elems in this list */
103 * If the automatic flag (-a) is given and mount points are not specified
104 * on the command line, then do not attempt to umount these. These
105 * generally need to be kept mounted until system shutdown.
107 static const char *keeplist
[] = {
129 static void doexec(struct mnttab
*);
130 static int setup_iopipe(mountent_t
*);
131 static void setup_output(mountent_t
*);
132 static void doio(mountent_t
*);
133 static void do_umounts(mountent_t
**);
135 static int parumount();
136 static int mcompar(const void *, const void *);
137 static void cleanup(int);
139 static mountent_t
**make_mntarray(char **, int);
140 static mountent_t
*getmntall();
141 static mountent_t
*new_mountent(struct mnttab
*);
142 static mountent_t
*getmntlast(mountent_t
*, char *, char *);
145 main(int argc
, char **argv
)
149 char *mname
, *is_special
;
153 (void) setlocale(LC_ALL
, "");
155 #if !defined(TEXT_DOMAIN)
156 #define TEXT_DOMAIN "SYS_TEST"
158 (void) textdomain(TEXT_DOMAIN
);
160 myname
= strrchr(argv
[0], '/');
168 * "-d" for compatibility
170 while ((cc
= getopt(argc
, argv
, "ado:Vf?")) != -1)
206 fscnt
= argc
- optind
;
207 if (!aflg
&& fscnt
!= 1)
210 /* copy '--' to specific */
211 if (strcmp(argv
[optind
-1], "--") == 0)
215 * mnttab may be a symlink to a file in another file system.
216 * This happens during install when / is mounted read-only
217 * and /etc/mnttab is symlinked to a file in /tmp.
218 * If this is the case, we need to follow the symlink to the
219 * read-write file itself so that the subsequent mnttab.temp
220 * open and rename will work.
222 if (realpath(MNTTAB
, mnttab
) == NULL
) {
223 strcpy(mnttab
, MNTTAB
);
228 * call the realpath() here, so that if the user is
229 * trying to umount an autofs directory, the directory
230 * is forced to mount.
233 mname
= argv
[optind
];
234 is_special
= realpath(mname
, resolve
);
237 * Read the whole mnttab into memory.
241 if (aflg
&& fscnt
!= 1)
242 exit(parumount(argv
+ optind
, fscnt
));
247 if (listlength
== 0) {
248 fprintf(stderr
, gettext(
249 "%s: warning: no entries found in %s\n"),
251 mget
.mnt_mountp
= mname
; /* assume mount point */
260 * if realpath fails, it can't be a mount point, so we'll
261 * go straight to the code that treats the arg as a special.
262 * if realpath succeeds, it could be a special or a mount point;
263 * we'll start by assuming it's a mount point, and if it's not,
264 * try to treat it as a special.
266 if (is_special
!= NULL
) {
269 * we'll have the appropriate record; if it fails
270 * we'll assume the arg is a special of some sort
272 mp
= getmntlast(mntll
, NULL
, resolve
);
275 * Since stackable mount is allowed (RFE 2001535),
276 * we will un-mount the last entry in the MNTTAB that matches.
280 * Perhaps there is a bogus mnttab entry that
283 if ((mp
= getmntlast(mntll
, NULL
, mname
)) == NULL
)
285 * assume it's a device (special) now
287 mp
= getmntlast(mntll
, mname
, NULL
);
291 * This is a device. Now we want to know if
292 * it stackmounted on by something else.
293 * The original fix for bug 1103850 has a
294 * problem with lockfs (bug 1119731). This
295 * is a revised method.
298 lmp
= getmntlast(mntll
, NULL
, mp
->ment
.mnt_mountp
);
300 if (lmp
&& strcmp(lmp
->ment
.mnt_special
,
301 mp
->ment
.mnt_special
)) {
307 fprintf(stderr
, gettext(
308 "%s: warning: %s not in mnttab\n"),
313 * same error as mount -V
314 * would give for unknown
317 mget
.mnt_special
= mget
.mnt_mountp
= mname
;
330 doexec(struct mnttab
*ment
)
336 fprintf(stderr
, "%d: umounting %s\n",
337 getpid(), ment
->mnt_mountp
);
340 /* try to exec the dependent portion */
341 if ((ment
->mnt_fstype
!= NULL
) || Vflg
) {
342 char full_path
[FULLPATH_MAX
];
343 char alter_path
[FULLPATH_MAX
];
344 char *newargv
[ARGV_MAX
];
347 if (strlen(ment
->mnt_fstype
) > (size_t)FSTYPE_MAX
) {
348 fprintf(stderr
, gettext(
349 "%s: FSType %s exceeds %d characters\n"),
350 myname
, ment
->mnt_fstype
, FSTYPE_MAX
);
354 /* build the full pathname of the fstype dependent command. */
355 sprintf(full_path
, "%s/%s/%s", fs_path
, ment
->mnt_fstype
,
357 sprintf(alter_path
, "%s/%s/%s", alt_path
, ment
->mnt_fstype
,
361 * create the new arg list, and end the list with a
366 newargv
[ii
++] = "-o";
367 newargv
[ii
++] = oarg
;
370 newargv
[ii
++] = "--";
373 newargv
[ii
++] = "-f";
375 newargv
[ii
++] = (ment
->mnt_mountp
)
376 ? ment
->mnt_mountp
: ment
->mnt_special
;
379 /* set the new argv[0] to the filename */
383 printf("%s", myname
);
384 for (ii
= 2; newargv
[ii
]; ii
++)
385 printf(" %s", newargv
[ii
]);
391 /* Try to exec the fstype dependent umount. */
392 execv(full_path
, &newargv
[1]);
393 if (errno
== ENOEXEC
) {
395 newargv
[1] = full_path
;
396 execv("/sbin/sh", &newargv
[0]);
399 execv(alter_path
, &newargv
[1]);
400 if (errno
== ENOEXEC
) {
402 newargv
[1] = alter_path
;
403 execv("/sbin/sh", &newargv
[0]);
406 if (errno
!= ENOENT
) {
407 fprintf(stderr
, gettext("umount: cannot execute %s\n"),
413 * No fstype independent executable then. We'll go generic
417 /* don't use -o with generic */
419 fprintf(stderr
, gettext(
420 "%s: %s specific umount does not exist;"
421 " -o suboption ignored\n"),
422 myname
, ment
->mnt_fstype
? ment
->mnt_fstype
: "<null>");
425 signal(SIGHUP
, SIG_IGN
);
426 signal(SIGQUIT
, SIG_IGN
);
427 signal(SIGINT
, SIG_IGN
);
429 * Try to umount the mountpoint.
430 * If that fails, try the corresponding special.
431 * (This ordering is necessary for nfs umounts.)
432 * (for remote resources: if the first umount returns EBUSY
433 * don't call umount again - umount() with a resource name
434 * will return a misleading error to the user
437 if (((ret
= umount2(ment
->mnt_mountp
, MS_FORCE
)) < 0) &&
438 (errno
!= EBUSY
&& errno
!= ENOTSUP
&&
440 ret
= umount2(ment
->mnt_special
, MS_FORCE
);
442 if (((ret
= umount2(ment
->mnt_mountp
, 0)) < 0) &&
443 (errno
!= EBUSY
) && (errno
!= EPERM
))
444 ret
= umount2(ment
->mnt_special
, 0);
448 rpterr(ment
->mnt_mountp
);
449 if (errno
!= EINVAL
&& errno
!= EFAULT
)
463 fprintf(stderr
, gettext("%s: permission denied\n"), myname
);
466 fprintf(stderr
, gettext("%s: %s no device\n"), myname
, sp
);
470 gettext("%s: %s no such file or directory\n"),
474 fprintf(stderr
, gettext("%s: %s not mounted\n"), myname
, sp
);
477 fprintf(stderr
, gettext("%s: %s busy\n"), myname
, sp
);
481 gettext("%s: %s block device required\n"), myname
, sp
);
485 gettext("%s: warning: broken link detected\n"), myname
);
489 fprintf(stderr
, gettext("%s: cannot unmount %s\n"), myname
, sp
);
496 fprintf(stderr
, gettext(
497 "Usage:\n%s [-f] [-V] [-o specific_options] {special | mount-point}\n"),
499 fprintf(stderr
, gettext(
500 "%s -a [-f] [-V] [-o specific_options] [mount_point ...]\n"), myname
);
510 gettext("%s: line in mnttab exceeds %d characters\n"),
511 myname
, MNT_LINE_MAX
-2);
515 gettext("%s: line in mnttab has too few entries\n"),
524 * Search the mlist linked list for the
525 * first match of specp or mntp. The list is expected to be in reverse
526 * order of /etc/mnttab.
527 * If both are specified, then both have to match.
528 * Returns the (mountent_t *) of the match, otherwise returns NULL.
531 getmntlast(mountent_t
*mlist
, char *specp
, char *mntp
)
535 for (/* */; mlist
; mlist
= mlist
->link
) {
537 if (mntp
&& (strcmp(mlist
->ment
.mnt_mountp
, mntp
) == 0)) {
542 if (specp
&& (strcmp(mlist
->ment
.mnt_special
, specp
) == 0)) {
547 if (mfound
&& sfound
)
556 * Perform the parallel version of umount. Returns 0 if no errors occurred,
557 * non zero otherwise.
560 parumount(char **mntlist
, int count
)
562 int maxfd
= OPEN_MAX
;
564 mountent_t
**mntarray
, **ml
, *mp
;
567 * If no mount points are specified and none were found in mnttab,
568 * then end it all here.
570 if (count
== 0 && mntll
== NULL
)
574 * This is the process scaling section. After running a series
575 * of tests based on the number of simultaneous processes and
576 * processors available, optimum performance was achieved near or
579 if ((maxrun
= sysconf(_SC_NPROCESSORS_ONLN
)) == -1)
582 maxrun
= maxrun
* 2 + 1;
584 if (getrlimit(RLIMIT_NOFILE
, &rl
) == 0) {
585 rl
.rlim_cur
= rl
.rlim_max
;
586 if (setrlimit(RLIMIT_NOFILE
, &rl
) == 0)
587 maxfd
= (int)rl
.rlim_cur
;
588 (void) enable_extended_FILE_stdio(-1, -1);
592 * The parent needs to maintain 3 of its own fd's, plus 2 for
593 * each child (the stdout and stderr pipes).
595 maxfd
= (maxfd
/ 2) - 6; /* 6 takes care of temporary */
596 /* periods of open fds */
600 maxrun
= 4; /* sanity check */
602 mntarray
= make_mntarray(mntlist
, count
);
604 if (listlength
== 0) {
605 if (count
== 0) /* not an error, just none found */
607 fprintf(stderr
, gettext("%s: no valid entries found in %s\n"),
613 * Sort the entries based on their mount level only if lofs's are
617 qsort((void *)mntarray
, listlength
, sizeof (mountent_t
*),
620 * If we do not detect a lofs by now, we never will.
625 * Now link them up so that a given pid is easier to find when
626 * we go to clean up after they are done.
629 for (ml
= mntarray
; mp
= *ml
; /* */)
633 * Try to handle interrupts in a reasonable way.
635 sigset(SIGHUP
, cleanup
);
636 sigset(SIGQUIT
, cleanup
);
637 sigset(SIGINT
, cleanup
);
639 do_umounts(mntarray
); /* do the umounts */
644 * Returns a mountent_t array based on mntlist. If mntlist is NULL, then
645 * it returns all mnttab entries with a few exceptions. Sets the global
646 * variable listlength to the number of entries in the array.
649 make_mntarray(char **mntlist
, int count
)
651 mountent_t
*mp
, **mpp
;
658 mpp
= (mountent_t
**)malloc(sizeof (*mp
) * (listlength
+ 1));
663 if (mntll
== NULL
) { /* no entries? */
668 * No mount list specified: take all mnttab mount points
669 * except for a few cases.
671 for (ndx
= 0, mp
= mntll
; mp
; mp
= mp
->link
) {
672 if (fsstrinlist(mp
->ment
.mnt_mountp
, keeplist
))
674 mp
->mlevel
= fsgetmlevel(mp
->ment
.mnt_mountp
);
675 if (mp
->ment
.mnt_fstype
&&
676 (strcmp(mp
->ment
.mnt_fstype
, MNTTYPE_LOFS
) == 0))
687 * A list of mount points was specified on the command line.
688 * Build an array out of these.
690 for (ndx
= 0; count
--; ) {
692 if (realpath(cp
, resolve
) == NULL
) {
694 gettext("%s: warning: can't resolve %s\n"),
697 mp
= getmntlast(mntll
, NULL
, cp
); /* try anyways */
699 mp
= getmntlast(mntll
, NULL
, resolve
);
703 * Then we've reached the end without finding
704 * what we are looking for, but we still have to
705 * try to umount it: append it to mntarray.
707 fprintf(stderr
, gettext(
708 "%s: warning: %s not found in %s\n"),
709 myname
, resolve
, mnttab
);
712 mnew
.mnt_special
= mnew
.mnt_mountp
= strdup(resolve
);
713 if (mnew
.mnt_special
== NULL
)
715 mp
= new_mountent(&mnew
);
717 if (mp
->ment
.mnt_fstype
&&
718 (strcmp(mp
->ment
.mnt_fstype
, MNTTYPE_LOFS
) == 0))
721 mp
->mlevel
= fsgetmlevel(mp
->ment
.mnt_mountp
);
730 * Returns the tail of a linked list of all mnttab entries. I.e, it's faster
731 * to return the mnttab in reverse order.
732 * Sets listlength to the number of entries in the list.
733 * Returns NULL if none are found.
743 if ((fp
= fopen(mnttab
, "r")) == NULL
) {
744 fprintf(stderr
, gettext("%s: warning cannot open %s\n"),
750 while ((ret
= getmntent(fp
, &mget
)) != -1) {
758 mp
= new_mountent(&mget
);
773 do_umounts(mountent_t
**mntarray
)
775 mountent_t
*mp
, *mpprev
, **ml
= mntarray
;
776 int cnt
= listlength
;
779 * Main loop for the forked children:
781 for (mpprev
= *ml
; mp
= *ml
; mpprev
= mp
, ml
++, cnt
--) {
785 * Check to see if we cross a mount level: e.g.,
786 * /a/b/c -> /a/b. If so, we need to wait for all current
787 * umounts to finish before umounting the rest.
789 * Also, we unmount serially as long as there are lofs's
790 * to mount to avoid improper umount ordering.
792 if (mp
->mlevel
< mpprev
->mlevel
|| lofscnt
> 0)
793 while (nrun
> 0 && (dowait() != -1))
798 * We can now go to parallel umounting.
800 qsort((void *)ml
, cnt
, sizeof (mountent_t
*), mcompar
);
801 mp
= *ml
; /* possible first entry */
802 lofscnt
--; /* so we don't do this again */
805 while (setup_iopipe(mp
) == -1 && (dowait() != -1))
808 while (nrun
>= maxrun
&& (dowait() != -1)) /* throttle */
811 if ((pid
= fork()) == -1) {
817 if (dflg
&& pid
> 0) {
818 fprintf(stderr
, "parent %d: umounting %d %s\n",
819 getpid(), pid
, mp
->ment
.mnt_mountp
);
822 if (pid
== 0) { /* child */
823 signal(SIGHUP
, SIG_IGN
);
824 signal(SIGQUIT
, SIG_IGN
);
825 signal(SIGINT
, SIG_IGN
);
833 (void) close(mp
->sopipe
[WRPIPE
]);
834 (void) close(mp
->sepipe
[WRPIPE
]);
842 * cleanup the existing children and exit with an error
849 * Let the stragglers finish.
851 while (nrun
> 0 && (dowait() != -1))
859 * Waits for 1 child to die.
861 * Returns -1 if no children are left to wait for.
862 * Returns 0 if a child died without an error.
863 * Returns 1 if a child died with an error.
864 * Sets the global exitcode if an error occurred.
869 int wstat
, child
, ret
;
870 mountent_t
*mp
, *prevp
;
872 if ((child
= wait(&wstat
)) == -1)
875 if (WIFEXITED(wstat
)) /* this should always be true */
876 ret
= WEXITSTATUS(wstat
);
878 ret
= 1; /* assume some kind of error */
884 * Find our child so we can process its std output, if any.
885 * This search gets smaller and smaller as children are cleaned
888 for (prevp
= NULL
, mp
= mntll
; mp
; mp
= mp
->link
) {
889 if (mp
->pid
!= child
) {
894 * Found: let's remove it from this list.
897 prevp
->link
= mp
->link
;
905 * This should never happen.
908 fprintf(stderr
, gettext(
909 "%s: unknown child %d\n"), myname
, child
);
914 doio(mp
); /* Any output? */
916 if (mp
->ment
.mnt_fstype
&&
917 (strcmp(mp
->ment
.mnt_fstype
, MNTTYPE_LOFS
) == 0))
923 static const mountent_t zmount
= { 0 };
926 new_mountent(struct mnttab
*ment
)
930 new = (mountent_t
*)malloc(sizeof (*new));
935 if (ment
->mnt_special
&&
936 (new->ment
.mnt_special
= strdup(ment
->mnt_special
)) == NULL
)
938 if (ment
->mnt_mountp
&&
939 (new->ment
.mnt_mountp
= strdup(ment
->mnt_mountp
)) == NULL
)
941 if (ment
->mnt_fstype
&&
942 (new->ment
.mnt_fstype
= strdup(ment
->mnt_fstype
)) == NULL
)
949 * Sort in descending order of "mount level". For example, /a/b/c is
950 * placed before /a/b .
953 mcompar(const void *a
, const void *b
)
957 a1
= *(mountent_t
**)a
;
958 b1
= *(mountent_t
**)b
;
959 return (b1
->mlevel
- a1
->mlevel
);
963 * The purpose of this routine is to form stdout and stderr
964 * pipes for the children's output. The parent then reads and writes it
965 * out it serially in order to ensure that the output is
970 setup_iopipe(mountent_t
*mp
)
973 * Make a stdout and stderr pipe. This should never fail.
975 if (pipe(mp
->sopipe
) == -1)
977 if (pipe(mp
->sepipe
) == -1) {
978 (void) close(mp
->sopipe
[RDPIPE
]);
979 (void) close(mp
->sopipe
[WRPIPE
]);
983 * Don't block on an empty pipe.
985 (void) fcntl(mp
->sopipe
[RDPIPE
], F_SETFL
, O_NDELAY
|O_NONBLOCK
);
986 (void) fcntl(mp
->sepipe
[RDPIPE
], F_SETFL
, O_NDELAY
|O_NONBLOCK
);
991 * Called by a child to attach its stdout and stderr to the write side of
995 setup_output(mountent_t
*mp
)
997 (void) close(fileno(stdout
));
998 (void) dup(mp
->sopipe
[WRPIPE
]);
999 (void) close(mp
->sopipe
[WRPIPE
]);
1001 (void) close(fileno(stderr
));
1002 (void) dup(mp
->sepipe
[WRPIPE
]);
1003 (void) close(mp
->sepipe
[WRPIPE
]);
1007 * Parent uses this to print any stdout or stderr output issued by
1011 doio(mountent_t
*mp
)
1015 while ((bytes
= read(mp
->sepipe
[RDPIPE
], ibuf
, sizeof (ibuf
))) > 0)
1016 write(fileno(stderr
), ibuf
, bytes
);
1017 while ((bytes
= read(mp
->sopipe
[RDPIPE
], ibuf
, sizeof (ibuf
))) > 0)
1018 write(fileno(stdout
), ibuf
, bytes
);
1020 (void) close(mp
->sopipe
[RDPIPE
]);
1021 (void) close(mp
->sepipe
[RDPIPE
]);
1027 fprintf(stderr
, gettext("%s: out of memory\n"), myname
);
1029 * Let the stragglers finish.
1031 while (nrun
> 0 && (dowait() != -1))