17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5004388ebScasper * Common Development and Distribution License (the "License"). 6004388ebScasper * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 2242d15982SGordon Ross * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 24*0463c800SGordon Ross * 25*0463c800SGordon Ross * Copyright 2019 Nexenta by DDN, Inc. All rights reserved. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 29*0463c800SGordon Ross /* All Rights Reserved */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <stdio.h> 33004388ebScasper #include <stdio_ext.h> 347c478bd9Sstevel@tonic-gate #include <limits.h> 357c478bd9Sstevel@tonic-gate #include <unistd.h> 367c478bd9Sstevel@tonic-gate #include <stdlib.h> 377c478bd9Sstevel@tonic-gate #include <string.h> 387c478bd9Sstevel@tonic-gate #include <sys/signal.h> 397c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 407c478bd9Sstevel@tonic-gate #include <errno.h> 417c478bd9Sstevel@tonic-gate #include <sys/types.h> 427c478bd9Sstevel@tonic-gate #include <sys/stat.h> 437c478bd9Sstevel@tonic-gate #include <sys/param.h> 447c478bd9Sstevel@tonic-gate #include <sys/wait.h> 457c478bd9Sstevel@tonic-gate #include <sys/vfstab.h> 467c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 477c478bd9Sstevel@tonic-gate #include <sys/resource.h> 487c478bd9Sstevel@tonic-gate #include <sys/mntent.h> 497c478bd9Sstevel@tonic-gate #include <sys/ctfs.h> 507c478bd9Sstevel@tonic-gate #include <locale.h> 51*0463c800SGordon Ross #include <priv.h> 527c478bd9Sstevel@tonic-gate #include <stdarg.h> 537c478bd9Sstevel@tonic-gate #include <sys/mount.h> 547c478bd9Sstevel@tonic-gate #include <sys/objfs.h> 557c478bd9Sstevel@tonic-gate #include "fslib.h" 56a237e38eSth #include <sharefs/share.h> 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate #define FS_PATH "/usr/lib/fs" 597c478bd9Sstevel@tonic-gate #define ALT_PATH "/etc/fs" 607c478bd9Sstevel@tonic-gate #define FULLPATH_MAX 32 617c478bd9Sstevel@tonic-gate #define FSTYPE_MAX 8 627c478bd9Sstevel@tonic-gate #define ARGV_MAX 16 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate int aflg, oflg, Vflg, dashflg, dflg, fflg; 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate extern void rpterr(), usage(), mnterror(); 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate extern char *optarg; /* used by getopt */ 697c478bd9Sstevel@tonic-gate extern int optind, opterr; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate static char *myname; 727c478bd9Sstevel@tonic-gate char fs_path[] = FS_PATH; 737c478bd9Sstevel@tonic-gate char alt_path[] = ALT_PATH; 747c478bd9Sstevel@tonic-gate char mnttab[MAXPATHLEN + 1]; 757c478bd9Sstevel@tonic-gate char *oarg, *farg; 767c478bd9Sstevel@tonic-gate int maxrun, nrun; 777c478bd9Sstevel@tonic-gate int no_mnttab; 787c478bd9Sstevel@tonic-gate int lofscnt; /* presence of lofs prohibits parallel */ 797c478bd9Sstevel@tonic-gate /* umounting */ 807c478bd9Sstevel@tonic-gate int exitcode; 817c478bd9Sstevel@tonic-gate char resolve[MAXPATHLEN]; 827c478bd9Sstevel@tonic-gate static char ibuf[BUFSIZ]; 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate /* 857c478bd9Sstevel@tonic-gate * The basic mount struct that describes an mnttab entry. 867c478bd9Sstevel@tonic-gate * It is used both in an array and as a linked list elem. 877c478bd9Sstevel@tonic-gate */ 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate typedef struct mountent { 907c478bd9Sstevel@tonic-gate struct mnttab ment; /* the mnttab data */ 917c478bd9Sstevel@tonic-gate int mlevel; /* mount level of the mount pt */ 927c478bd9Sstevel@tonic-gate pid_t pid; /* the pid of this mount process */ 937c478bd9Sstevel@tonic-gate #define RDPIPE 0 947c478bd9Sstevel@tonic-gate #define WRPIPE 1 957c478bd9Sstevel@tonic-gate int sopipe[2]; /* pipe attached to child's stdout */ 967c478bd9Sstevel@tonic-gate int sepipe[2]; /* pipe attached to child's stderr */ 977c478bd9Sstevel@tonic-gate struct mountent *link; /* used when in linked list */ 987c478bd9Sstevel@tonic-gate } mountent_t; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate static mountent_t *mntll; /* head of global linked list of */ 1017c478bd9Sstevel@tonic-gate /* mountents */ 1027c478bd9Sstevel@tonic-gate int listlength; /* # of elems in this list */ 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate /* 1057c478bd9Sstevel@tonic-gate * If the automatic flag (-a) is given and mount points are not specified 1067c478bd9Sstevel@tonic-gate * on the command line, then do not attempt to umount these. These 1077c478bd9Sstevel@tonic-gate * generally need to be kept mounted until system shutdown. 1087c478bd9Sstevel@tonic-gate */ 1097c478bd9Sstevel@tonic-gate static const char *keeplist[] = { 1107c478bd9Sstevel@tonic-gate "/", 1117c478bd9Sstevel@tonic-gate "/dev", 1127c478bd9Sstevel@tonic-gate "/dev/fd", 1137c478bd9Sstevel@tonic-gate "/devices", 1147c478bd9Sstevel@tonic-gate "/etc/mnttab", 1157c478bd9Sstevel@tonic-gate "/etc/svc/volatile", 1167c478bd9Sstevel@tonic-gate "/lib", 1177c478bd9Sstevel@tonic-gate "/proc", 1187c478bd9Sstevel@tonic-gate "/sbin", 1197c478bd9Sstevel@tonic-gate CTFS_ROOT, 1207c478bd9Sstevel@tonic-gate OBJFS_ROOT, 1217c478bd9Sstevel@tonic-gate "/tmp", 1227c478bd9Sstevel@tonic-gate "/usr", 1237c478bd9Sstevel@tonic-gate "/var", 1247c478bd9Sstevel@tonic-gate "/var/adm", 1257c478bd9Sstevel@tonic-gate "/var/run", 126a237e38eSth SHARETAB, 1277c478bd9Sstevel@tonic-gate NULL 1287c478bd9Sstevel@tonic-gate }; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate static void nomem(); 1317c478bd9Sstevel@tonic-gate static void doexec(struct mnttab *); 1327c478bd9Sstevel@tonic-gate static int setup_iopipe(mountent_t *); 1337c478bd9Sstevel@tonic-gate static void setup_output(mountent_t *); 1347c478bd9Sstevel@tonic-gate static void doio(mountent_t *); 1357c478bd9Sstevel@tonic-gate static void do_umounts(mountent_t **); 1367c478bd9Sstevel@tonic-gate static int dowait(); 1377c478bd9Sstevel@tonic-gate static int parumount(); 1387c478bd9Sstevel@tonic-gate static int mcompar(const void *, const void *); 1397c478bd9Sstevel@tonic-gate static void cleanup(int); 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate static mountent_t **make_mntarray(char **, int); 1427c478bd9Sstevel@tonic-gate static mountent_t *getmntall(); 143*0463c800SGordon Ross static mountent_t *new_mountent(struct mnttab *); 1447c478bd9Sstevel@tonic-gate static mountent_t *getmntlast(mountent_t *, char *, char *); 1457c478bd9Sstevel@tonic-gate 14608190127Sdh int 14708190127Sdh main(int argc, char **argv) 1487c478bd9Sstevel@tonic-gate { 149*0463c800SGordon Ross int cc; 1507c478bd9Sstevel@tonic-gate struct mnttab mget; 151*0463c800SGordon Ross char *mname, *is_special; 1527c478bd9Sstevel@tonic-gate int fscnt; 1537c478bd9Sstevel@tonic-gate mountent_t *mp; 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 1587c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 1597c478bd9Sstevel@tonic-gate #endif 1607c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate myname = strrchr(argv[0], '/'); 1637c478bd9Sstevel@tonic-gate if (myname) 1647c478bd9Sstevel@tonic-gate myname++; 1657c478bd9Sstevel@tonic-gate else 1667c478bd9Sstevel@tonic-gate myname = argv[0]; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate /* 1697c478bd9Sstevel@tonic-gate * Process the args. 1707c478bd9Sstevel@tonic-gate * "-d" for compatibility 1717c478bd9Sstevel@tonic-gate */ 1727c478bd9Sstevel@tonic-gate while ((cc = getopt(argc, argv, "ado:Vf?")) != -1) 1737c478bd9Sstevel@tonic-gate switch (cc) { 1747c478bd9Sstevel@tonic-gate case 'a': 1757c478bd9Sstevel@tonic-gate aflg++; 1767c478bd9Sstevel@tonic-gate break; 1777c478bd9Sstevel@tonic-gate #ifdef DEBUG 1787c478bd9Sstevel@tonic-gate case 'd': 1797c478bd9Sstevel@tonic-gate dflg++; 1807c478bd9Sstevel@tonic-gate break; 1817c478bd9Sstevel@tonic-gate #endif 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate case '?': 1847c478bd9Sstevel@tonic-gate usage(); 1857c478bd9Sstevel@tonic-gate break; 1867c478bd9Sstevel@tonic-gate case 'o': 1877c478bd9Sstevel@tonic-gate if (oflg) 1887c478bd9Sstevel@tonic-gate usage(); 1897c478bd9Sstevel@tonic-gate else { 1907c478bd9Sstevel@tonic-gate oflg++; 1917c478bd9Sstevel@tonic-gate oarg = optarg; 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate break; 1947c478bd9Sstevel@tonic-gate case 'f': 1957c478bd9Sstevel@tonic-gate fflg++; 1967c478bd9Sstevel@tonic-gate break; 1977c478bd9Sstevel@tonic-gate case 'V': 1987c478bd9Sstevel@tonic-gate if (Vflg) 1997c478bd9Sstevel@tonic-gate usage(); 2007c478bd9Sstevel@tonic-gate else 2017c478bd9Sstevel@tonic-gate Vflg++; 2027c478bd9Sstevel@tonic-gate break; 2037c478bd9Sstevel@tonic-gate default: 2047c478bd9Sstevel@tonic-gate usage(); 2057c478bd9Sstevel@tonic-gate break; 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate fscnt = argc - optind; 2097c478bd9Sstevel@tonic-gate if (!aflg && fscnt != 1) 2107c478bd9Sstevel@tonic-gate usage(); 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate /* copy '--' to specific */ 2137c478bd9Sstevel@tonic-gate if (strcmp(argv[optind-1], "--") == 0) 2147c478bd9Sstevel@tonic-gate dashflg++; 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate /* 2177c478bd9Sstevel@tonic-gate * mnttab may be a symlink to a file in another file system. 2187c478bd9Sstevel@tonic-gate * This happens during install when / is mounted read-only 2197c478bd9Sstevel@tonic-gate * and /etc/mnttab is symlinked to a file in /tmp. 2207c478bd9Sstevel@tonic-gate * If this is the case, we need to follow the symlink to the 2217c478bd9Sstevel@tonic-gate * read-write file itself so that the subsequent mnttab.temp 2227c478bd9Sstevel@tonic-gate * open and rename will work. 2237c478bd9Sstevel@tonic-gate */ 2247c478bd9Sstevel@tonic-gate if (realpath(MNTTAB, mnttab) == NULL) { 2257c478bd9Sstevel@tonic-gate strcpy(mnttab, MNTTAB); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate /* 2297c478bd9Sstevel@tonic-gate * bugid 1205242 2307c478bd9Sstevel@tonic-gate * call the realpath() here, so that if the user is 2317c478bd9Sstevel@tonic-gate * trying to umount an autofs directory, the directory 2327c478bd9Sstevel@tonic-gate * is forced to mount. 2337c478bd9Sstevel@tonic-gate */ 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate mname = argv[optind]; 2367c478bd9Sstevel@tonic-gate is_special = realpath(mname, resolve); 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate /* 2397c478bd9Sstevel@tonic-gate * Read the whole mnttab into memory. 2407c478bd9Sstevel@tonic-gate */ 2417c478bd9Sstevel@tonic-gate mntll = getmntall(); 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate if (aflg && fscnt != 1) 2447c478bd9Sstevel@tonic-gate exit(parumount(argv + optind, fscnt)); 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate aflg = 0; 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate mntnull(&mget); 2497c478bd9Sstevel@tonic-gate if (listlength == 0) { 2507c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 2514bff34e3Sthurlow "%s: warning: no entries found in %s\n"), 2524bff34e3Sthurlow myname, mnttab); 2537c478bd9Sstevel@tonic-gate mget.mnt_mountp = mname; /* assume mount point */ 2547c478bd9Sstevel@tonic-gate no_mnttab++; 2557c478bd9Sstevel@tonic-gate doexec(&mget); 2567c478bd9Sstevel@tonic-gate exit(0); 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate mp = NULL; 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate /* 2627c478bd9Sstevel@tonic-gate * if realpath fails, it can't be a mount point, so we'll 2637c478bd9Sstevel@tonic-gate * go straight to the code that treats the arg as a special. 2647c478bd9Sstevel@tonic-gate * if realpath succeeds, it could be a special or a mount point; 2657c478bd9Sstevel@tonic-gate * we'll start by assuming it's a mount point, and if it's not, 2667c478bd9Sstevel@tonic-gate * try to treat it as a special. 2677c478bd9Sstevel@tonic-gate */ 2687c478bd9Sstevel@tonic-gate if (is_special != NULL) { 2697c478bd9Sstevel@tonic-gate /* 2707c478bd9Sstevel@tonic-gate * if this succeeds, 2717c478bd9Sstevel@tonic-gate * we'll have the appropriate record; if it fails 2727c478bd9Sstevel@tonic-gate * we'll assume the arg is a special of some sort 2737c478bd9Sstevel@tonic-gate */ 2747c478bd9Sstevel@tonic-gate mp = getmntlast(mntll, NULL, resolve); 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate /* 2777c478bd9Sstevel@tonic-gate * Since stackable mount is allowed (RFE 2001535), 2787c478bd9Sstevel@tonic-gate * we will un-mount the last entry in the MNTTAB that matches. 2797c478bd9Sstevel@tonic-gate */ 2807c478bd9Sstevel@tonic-gate if (mp == NULL) { 2817c478bd9Sstevel@tonic-gate /* 2827c478bd9Sstevel@tonic-gate * Perhaps there is a bogus mnttab entry that 2837c478bd9Sstevel@tonic-gate * can't be resolved: 2847c478bd9Sstevel@tonic-gate */ 2857c478bd9Sstevel@tonic-gate if ((mp = getmntlast(mntll, NULL, mname)) == NULL) 2867c478bd9Sstevel@tonic-gate /* 2877c478bd9Sstevel@tonic-gate * assume it's a device (special) now 2887c478bd9Sstevel@tonic-gate */ 2897c478bd9Sstevel@tonic-gate mp = getmntlast(mntll, mname, NULL); 2907c478bd9Sstevel@tonic-gate if (mp) { 2917c478bd9Sstevel@tonic-gate /* 2927c478bd9Sstevel@tonic-gate * Found it. 2937c478bd9Sstevel@tonic-gate * This is a device. Now we want to know if 2947c478bd9Sstevel@tonic-gate * it stackmounted on by something else. 2957c478bd9Sstevel@tonic-gate * The original fix for bug 1103850 has a 2967c478bd9Sstevel@tonic-gate * problem with lockfs (bug 1119731). This 2977c478bd9Sstevel@tonic-gate * is a revised method. 2987c478bd9Sstevel@tonic-gate */ 2997c478bd9Sstevel@tonic-gate mountent_t *lmp; 3007c478bd9Sstevel@tonic-gate lmp = getmntlast(mntll, NULL, mp->ment.mnt_mountp); 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate if (lmp && strcmp(lmp->ment.mnt_special, 3034bff34e3Sthurlow mp->ment.mnt_special)) { 3047c478bd9Sstevel@tonic-gate errno = EBUSY; 3057c478bd9Sstevel@tonic-gate rpterr(mname); 3067c478bd9Sstevel@tonic-gate exit(1); 3077c478bd9Sstevel@tonic-gate } 3087c478bd9Sstevel@tonic-gate } else { 3097c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 3104bff34e3Sthurlow "%s: warning: %s not in mnttab\n"), 3114bff34e3Sthurlow myname, mname); 3127c478bd9Sstevel@tonic-gate if (Vflg) 3137c478bd9Sstevel@tonic-gate exit(1); 3147c478bd9Sstevel@tonic-gate /* 3157c478bd9Sstevel@tonic-gate * same error as mount -V 3167c478bd9Sstevel@tonic-gate * would give for unknown 3177c478bd9Sstevel@tonic-gate * mount point 3187c478bd9Sstevel@tonic-gate */ 3197c478bd9Sstevel@tonic-gate mget.mnt_special = mget.mnt_mountp = mname; 3207c478bd9Sstevel@tonic-gate } 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate if (mp) 3247c478bd9Sstevel@tonic-gate doexec(&mp->ment); 3257c478bd9Sstevel@tonic-gate else 3267c478bd9Sstevel@tonic-gate doexec(&mget); 3277c478bd9Sstevel@tonic-gate 32808190127Sdh return (0); 3297c478bd9Sstevel@tonic-gate } 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate void 3327c478bd9Sstevel@tonic-gate doexec(struct mnttab *ment) 3337c478bd9Sstevel@tonic-gate { 334*0463c800SGordon Ross int ret; 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate #ifdef DEBUG 3377c478bd9Sstevel@tonic-gate if (dflg) 3387c478bd9Sstevel@tonic-gate fprintf(stderr, "%d: umounting %s\n", 3394bff34e3Sthurlow getpid(), ment->mnt_mountp); 3407c478bd9Sstevel@tonic-gate #endif 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate /* try to exec the dependent portion */ 3437c478bd9Sstevel@tonic-gate if ((ment->mnt_fstype != NULL) || Vflg) { 3447c478bd9Sstevel@tonic-gate char full_path[FULLPATH_MAX]; 3457c478bd9Sstevel@tonic-gate char alter_path[FULLPATH_MAX]; 3467c478bd9Sstevel@tonic-gate char *newargv[ARGV_MAX]; 347*0463c800SGordon Ross int ii; 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate if (strlen(ment->mnt_fstype) > (size_t)FSTYPE_MAX) { 3507c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 3514bff34e3Sthurlow "%s: FSType %s exceeds %d characters\n"), 3524bff34e3Sthurlow myname, ment->mnt_fstype, FSTYPE_MAX); 3537c478bd9Sstevel@tonic-gate exit(1); 3547c478bd9Sstevel@tonic-gate } 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate /* build the full pathname of the fstype dependent command. */ 3577c478bd9Sstevel@tonic-gate sprintf(full_path, "%s/%s/%s", fs_path, ment->mnt_fstype, 3584bff34e3Sthurlow myname); 3597c478bd9Sstevel@tonic-gate sprintf(alter_path, "%s/%s/%s", alt_path, ment->mnt_fstype, 3604bff34e3Sthurlow myname); 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate /* 3637c478bd9Sstevel@tonic-gate * create the new arg list, and end the list with a 3647c478bd9Sstevel@tonic-gate * null pointer 3657c478bd9Sstevel@tonic-gate */ 3667c478bd9Sstevel@tonic-gate ii = 2; 3677c478bd9Sstevel@tonic-gate if (oflg) { 3687c478bd9Sstevel@tonic-gate newargv[ii++] = "-o"; 3697c478bd9Sstevel@tonic-gate newargv[ii++] = oarg; 3707c478bd9Sstevel@tonic-gate } 3717c478bd9Sstevel@tonic-gate if (dashflg) { 3727c478bd9Sstevel@tonic-gate newargv[ii++] = "--"; 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate if (fflg) { 3757c478bd9Sstevel@tonic-gate newargv[ii++] = "-f"; 3767c478bd9Sstevel@tonic-gate } 3777c478bd9Sstevel@tonic-gate newargv[ii++] = (ment->mnt_mountp) 3784bff34e3Sthurlow ? ment->mnt_mountp : ment->mnt_special; 3797c478bd9Sstevel@tonic-gate newargv[ii] = NULL; 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate /* set the new argv[0] to the filename */ 3827c478bd9Sstevel@tonic-gate newargv[1] = myname; 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate if (Vflg) { 3857c478bd9Sstevel@tonic-gate printf("%s", myname); 3867c478bd9Sstevel@tonic-gate for (ii = 2; newargv[ii]; ii++) 3877c478bd9Sstevel@tonic-gate printf(" %s", newargv[ii]); 3887c478bd9Sstevel@tonic-gate printf("\n"); 3897c478bd9Sstevel@tonic-gate fflush(stdout); 3907c478bd9Sstevel@tonic-gate exit(0); 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate 393*0463c800SGordon Ross /* 394*0463c800SGordon Ross * Some file system types need pfexec. 395*0463c800SGordon Ross */ 396*0463c800SGordon Ross if (strcmp(ment->mnt_fstype, "smbfs") == 0 && 397*0463c800SGordon Ross setpflags(PRIV_PFEXEC, 1) != 0) { 398*0463c800SGordon Ross (void) fprintf(stderr, 399*0463c800SGordon Ross gettext("umount: unable to set PFEXEC flag: %s\n"), 400*0463c800SGordon Ross strerror(errno)); 401*0463c800SGordon Ross /* Keep going as best we can */ 402*0463c800SGordon Ross } 403*0463c800SGordon Ross 4047c478bd9Sstevel@tonic-gate /* Try to exec the fstype dependent umount. */ 4057c478bd9Sstevel@tonic-gate execv(full_path, &newargv[1]); 4067c478bd9Sstevel@tonic-gate if (errno == ENOEXEC) { 4077c478bd9Sstevel@tonic-gate newargv[0] = "sh"; 4087c478bd9Sstevel@tonic-gate newargv[1] = full_path; 4097c478bd9Sstevel@tonic-gate execv("/sbin/sh", &newargv[0]); 4107c478bd9Sstevel@tonic-gate } 4117c478bd9Sstevel@tonic-gate newargv[1] = myname; 4127c478bd9Sstevel@tonic-gate execv(alter_path, &newargv[1]); 4137c478bd9Sstevel@tonic-gate if (errno == ENOEXEC) { 4147c478bd9Sstevel@tonic-gate newargv[0] = "sh"; 4157c478bd9Sstevel@tonic-gate newargv[1] = alter_path; 4167c478bd9Sstevel@tonic-gate execv("/sbin/sh", &newargv[0]); 4177c478bd9Sstevel@tonic-gate } 4187c478bd9Sstevel@tonic-gate /* exec failed */ 4197c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 4207c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("umount: cannot execute %s\n"), 4214bff34e3Sthurlow full_path); 4227c478bd9Sstevel@tonic-gate exit(1); 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate } 4257c478bd9Sstevel@tonic-gate /* 4267c478bd9Sstevel@tonic-gate * No fstype independent executable then. We'll go generic 4277c478bd9Sstevel@tonic-gate * from here. 4287c478bd9Sstevel@tonic-gate */ 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate /* don't use -o with generic */ 4317c478bd9Sstevel@tonic-gate if (oflg) { 4327c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4334bff34e3Sthurlow "%s: %s specific umount does not exist;" 4344bff34e3Sthurlow " -o suboption ignored\n"), 4354bff34e3Sthurlow myname, ment->mnt_fstype ? ment->mnt_fstype : "<null>"); 4367c478bd9Sstevel@tonic-gate } 4377c478bd9Sstevel@tonic-gate 4387c478bd9Sstevel@tonic-gate signal(SIGHUP, SIG_IGN); 4397c478bd9Sstevel@tonic-gate signal(SIGQUIT, SIG_IGN); 4407c478bd9Sstevel@tonic-gate signal(SIGINT, SIG_IGN); 4417c478bd9Sstevel@tonic-gate /* 4427c478bd9Sstevel@tonic-gate * Try to umount the mountpoint. 4437c478bd9Sstevel@tonic-gate * If that fails, try the corresponding special. 4447c478bd9Sstevel@tonic-gate * (This ordering is necessary for nfs umounts.) 4457c478bd9Sstevel@tonic-gate * (for remote resources: if the first umount returns EBUSY 4467c478bd9Sstevel@tonic-gate * don't call umount again - umount() with a resource name 4477c478bd9Sstevel@tonic-gate * will return a misleading error to the user 4487c478bd9Sstevel@tonic-gate */ 4497c478bd9Sstevel@tonic-gate if (fflg) { 4507c478bd9Sstevel@tonic-gate if (((ret = umount2(ment->mnt_mountp, MS_FORCE)) < 0) && 4514bff34e3Sthurlow (errno != EBUSY && errno != ENOTSUP && 4524bff34e3Sthurlow errno != EPERM)) 4537c478bd9Sstevel@tonic-gate ret = umount2(ment->mnt_special, MS_FORCE); 4547c478bd9Sstevel@tonic-gate } else { 4557c478bd9Sstevel@tonic-gate if (((ret = umount2(ment->mnt_mountp, 0)) < 0) && 4564bff34e3Sthurlow (errno != EBUSY) && (errno != EPERM)) 4577c478bd9Sstevel@tonic-gate ret = umount2(ment->mnt_special, 0); 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate if (ret < 0) { 4617c478bd9Sstevel@tonic-gate rpterr(ment->mnt_mountp); 4627c478bd9Sstevel@tonic-gate if (errno != EINVAL && errno != EFAULT) 4637c478bd9Sstevel@tonic-gate exit(1); 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate exitcode = 1; 4667c478bd9Sstevel@tonic-gate } 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate exit(exitcode); 4697c478bd9Sstevel@tonic-gate } 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate void 47208190127Sdh rpterr(char *sp) 4737c478bd9Sstevel@tonic-gate { 4747c478bd9Sstevel@tonic-gate switch (errno) { 4757c478bd9Sstevel@tonic-gate case EPERM: 4767c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("%s: permission denied\n"), myname); 4777c478bd9Sstevel@tonic-gate break; 4787c478bd9Sstevel@tonic-gate case ENXIO: 4797c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("%s: %s no device\n"), myname, sp); 4807c478bd9Sstevel@tonic-gate break; 4817c478bd9Sstevel@tonic-gate case ENOENT: 4827c478bd9Sstevel@tonic-gate fprintf(stderr, 4834bff34e3Sthurlow gettext("%s: %s no such file or directory\n"), 4844bff34e3Sthurlow myname, sp); 4857c478bd9Sstevel@tonic-gate break; 4867c478bd9Sstevel@tonic-gate case EINVAL: 4877c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("%s: %s not mounted\n"), myname, sp); 4887c478bd9Sstevel@tonic-gate break; 4897c478bd9Sstevel@tonic-gate case EBUSY: 4907c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("%s: %s busy\n"), myname, sp); 4917c478bd9Sstevel@tonic-gate break; 4927c478bd9Sstevel@tonic-gate case ENOTBLK: 4937c478bd9Sstevel@tonic-gate fprintf(stderr, 4944bff34e3Sthurlow gettext("%s: %s block device required\n"), myname, sp); 4957c478bd9Sstevel@tonic-gate break; 4967c478bd9Sstevel@tonic-gate case ECOMM: 4977c478bd9Sstevel@tonic-gate fprintf(stderr, 4984bff34e3Sthurlow gettext("%s: warning: broken link detected\n"), myname); 4997c478bd9Sstevel@tonic-gate break; 5007c478bd9Sstevel@tonic-gate default: 5017c478bd9Sstevel@tonic-gate perror(myname); 5027c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("%s: cannot unmount %s\n"), myname, sp); 5037c478bd9Sstevel@tonic-gate } 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate void 50708190127Sdh usage(void) 5087c478bd9Sstevel@tonic-gate { 5097c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 5107c478bd9Sstevel@tonic-gate "Usage:\n%s [-f] [-V] [-o specific_options] {special | mount-point}\n"), 5114bff34e3Sthurlow myname); 5127c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 5137c478bd9Sstevel@tonic-gate "%s -a [-f] [-V] [-o specific_options] [mount_point ...]\n"), myname); 5147c478bd9Sstevel@tonic-gate exit(1); 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate void 51808190127Sdh mnterror(int flag) 5197c478bd9Sstevel@tonic-gate { 5207c478bd9Sstevel@tonic-gate switch (flag) { 5217c478bd9Sstevel@tonic-gate case MNT_TOOLONG: 5227c478bd9Sstevel@tonic-gate fprintf(stderr, 5234bff34e3Sthurlow gettext("%s: line in mnttab exceeds %d characters\n"), 5244bff34e3Sthurlow myname, MNT_LINE_MAX-2); 5257c478bd9Sstevel@tonic-gate break; 5267c478bd9Sstevel@tonic-gate case MNT_TOOFEW: 5277c478bd9Sstevel@tonic-gate fprintf(stderr, 5284bff34e3Sthurlow gettext("%s: line in mnttab has too few entries\n"), 5294bff34e3Sthurlow myname); 5307c478bd9Sstevel@tonic-gate break; 5317c478bd9Sstevel@tonic-gate default: 5327c478bd9Sstevel@tonic-gate break; 5337c478bd9Sstevel@tonic-gate } 5347c478bd9Sstevel@tonic-gate } 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate /* 5377c478bd9Sstevel@tonic-gate * Search the mlist linked list for the 5387c478bd9Sstevel@tonic-gate * first match of specp or mntp. The list is expected to be in reverse 5397c478bd9Sstevel@tonic-gate * order of /etc/mnttab. 5407c478bd9Sstevel@tonic-gate * If both are specified, then both have to match. 5417c478bd9Sstevel@tonic-gate * Returns the (mountent_t *) of the match, otherwise returns NULL. 5427c478bd9Sstevel@tonic-gate */ 5437c478bd9Sstevel@tonic-gate mountent_t * 5447c478bd9Sstevel@tonic-gate getmntlast(mountent_t *mlist, char *specp, char *mntp) 5457c478bd9Sstevel@tonic-gate { 5467c478bd9Sstevel@tonic-gate int mfound, sfound; 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate for (/* */; mlist; mlist = mlist->link) { 5497c478bd9Sstevel@tonic-gate mfound = sfound = 0; 5507c478bd9Sstevel@tonic-gate if (mntp && (strcmp(mlist->ment.mnt_mountp, mntp) == 0)) { 5517c478bd9Sstevel@tonic-gate if (specp == NULL) 5527c478bd9Sstevel@tonic-gate return (mlist); 5537c478bd9Sstevel@tonic-gate mfound++; 5547c478bd9Sstevel@tonic-gate } 5557c478bd9Sstevel@tonic-gate if (specp && (strcmp(mlist->ment.mnt_special, specp) == 0)) { 5567c478bd9Sstevel@tonic-gate if (mntp == NULL) 5577c478bd9Sstevel@tonic-gate return (mlist); 5587c478bd9Sstevel@tonic-gate sfound++; 5597c478bd9Sstevel@tonic-gate } 5607c478bd9Sstevel@tonic-gate if (mfound && sfound) 5617c478bd9Sstevel@tonic-gate return (mlist); 5627c478bd9Sstevel@tonic-gate } 5637c478bd9Sstevel@tonic-gate return (NULL); 5647c478bd9Sstevel@tonic-gate } 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate /* 5697c478bd9Sstevel@tonic-gate * Perform the parallel version of umount. Returns 0 if no errors occurred, 5707c478bd9Sstevel@tonic-gate * non zero otherwise. 5717c478bd9Sstevel@tonic-gate */ 5727c478bd9Sstevel@tonic-gate int 5737c478bd9Sstevel@tonic-gate parumount(char **mntlist, int count) 5747c478bd9Sstevel@tonic-gate { 575*0463c800SGordon Ross int maxfd = OPEN_MAX; 576*0463c800SGordon Ross struct rlimit rl; 5777c478bd9Sstevel@tonic-gate mountent_t **mntarray, **ml, *mp; 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate /* 5807c478bd9Sstevel@tonic-gate * If no mount points are specified and none were found in mnttab, 5817c478bd9Sstevel@tonic-gate * then end it all here. 5827c478bd9Sstevel@tonic-gate */ 5837c478bd9Sstevel@tonic-gate if (count == 0 && mntll == NULL) 5847c478bd9Sstevel@tonic-gate return (0); 5857c478bd9Sstevel@tonic-gate 5867c478bd9Sstevel@tonic-gate /* 5877c478bd9Sstevel@tonic-gate * This is the process scaling section. After running a series 5887c478bd9Sstevel@tonic-gate * of tests based on the number of simultaneous processes and 5897c478bd9Sstevel@tonic-gate * processors available, optimum performance was achieved near or 5907c478bd9Sstevel@tonic-gate * at (PROCN * 2). 5917c478bd9Sstevel@tonic-gate */ 5927c478bd9Sstevel@tonic-gate if ((maxrun = sysconf(_SC_NPROCESSORS_ONLN)) == -1) 5937c478bd9Sstevel@tonic-gate maxrun = 4; 5947c478bd9Sstevel@tonic-gate else 5957c478bd9Sstevel@tonic-gate maxrun = maxrun * 2 + 1; 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rl) == 0) { 5987c478bd9Sstevel@tonic-gate rl.rlim_cur = rl.rlim_max; 5997c478bd9Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rl) == 0) 6007c478bd9Sstevel@tonic-gate maxfd = (int)rl.rlim_cur; 601004388ebScasper (void) enable_extended_FILE_stdio(-1, -1); 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate /* 6057c478bd9Sstevel@tonic-gate * The parent needs to maintain 3 of its own fd's, plus 2 for 6067c478bd9Sstevel@tonic-gate * each child (the stdout and stderr pipes). 6077c478bd9Sstevel@tonic-gate */ 6087c478bd9Sstevel@tonic-gate maxfd = (maxfd / 2) - 6; /* 6 takes care of temporary */ 6097c478bd9Sstevel@tonic-gate /* periods of open fds */ 6107c478bd9Sstevel@tonic-gate if (maxfd < maxrun) 6117c478bd9Sstevel@tonic-gate maxrun = maxfd; 6127c478bd9Sstevel@tonic-gate if (maxrun < 4) 6137c478bd9Sstevel@tonic-gate maxrun = 4; /* sanity check */ 6147c478bd9Sstevel@tonic-gate 6157c478bd9Sstevel@tonic-gate mntarray = make_mntarray(mntlist, count); 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate if (listlength == 0) { 6187c478bd9Sstevel@tonic-gate if (count == 0) /* not an error, just none found */ 6197c478bd9Sstevel@tonic-gate return (0); 6207c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("%s: no valid entries found in %s\n"), 6214bff34e3Sthurlow myname, mnttab); 6227c478bd9Sstevel@tonic-gate return (1); 6237c478bd9Sstevel@tonic-gate } 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate /* 6267c478bd9Sstevel@tonic-gate * Sort the entries based on their mount level only if lofs's are 6277c478bd9Sstevel@tonic-gate * not present. 6287c478bd9Sstevel@tonic-gate */ 6297c478bd9Sstevel@tonic-gate if (lofscnt == 0) { 6307c478bd9Sstevel@tonic-gate qsort((void *)mntarray, listlength, sizeof (mountent_t *), 6314bff34e3Sthurlow mcompar); 6327c478bd9Sstevel@tonic-gate /* 6337c478bd9Sstevel@tonic-gate * If we do not detect a lofs by now, we never will. 6347c478bd9Sstevel@tonic-gate */ 6357c478bd9Sstevel@tonic-gate lofscnt = -1; 6367c478bd9Sstevel@tonic-gate } 6377c478bd9Sstevel@tonic-gate /* 6387c478bd9Sstevel@tonic-gate * Now link them up so that a given pid is easier to find when 6397c478bd9Sstevel@tonic-gate * we go to clean up after they are done. 6407c478bd9Sstevel@tonic-gate */ 6417c478bd9Sstevel@tonic-gate mntll = mntarray[0]; 6427c478bd9Sstevel@tonic-gate for (ml = mntarray; mp = *ml; /* */) 6437c478bd9Sstevel@tonic-gate mp->link = *++ml; 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate /* 6467c478bd9Sstevel@tonic-gate * Try to handle interrupts in a reasonable way. 6477c478bd9Sstevel@tonic-gate */ 6487c478bd9Sstevel@tonic-gate sigset(SIGHUP, cleanup); 6497c478bd9Sstevel@tonic-gate sigset(SIGQUIT, cleanup); 6507c478bd9Sstevel@tonic-gate sigset(SIGINT, cleanup); 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate do_umounts(mntarray); /* do the umounts */ 6537c478bd9Sstevel@tonic-gate return (exitcode); 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate 6567c478bd9Sstevel@tonic-gate /* 6577c478bd9Sstevel@tonic-gate * Returns a mountent_t array based on mntlist. If mntlist is NULL, then 6587c478bd9Sstevel@tonic-gate * it returns all mnttab entries with a few exceptions. Sets the global 6597c478bd9Sstevel@tonic-gate * variable listlength to the number of entries in the array. 6607c478bd9Sstevel@tonic-gate */ 6617c478bd9Sstevel@tonic-gate mountent_t ** 6627c478bd9Sstevel@tonic-gate make_mntarray(char **mntlist, int count) 6637c478bd9Sstevel@tonic-gate { 664*0463c800SGordon Ross mountent_t *mp, **mpp; 665*0463c800SGordon Ross int ndx; 6667c478bd9Sstevel@tonic-gate char *cp; 6677c478bd9Sstevel@tonic-gate 6687c478bd9Sstevel@tonic-gate if (count > 0) 6697c478bd9Sstevel@tonic-gate listlength = count; 6707c478bd9Sstevel@tonic-gate 6717c478bd9Sstevel@tonic-gate mpp = (mountent_t **)malloc(sizeof (*mp) * (listlength + 1)); 6727c478bd9Sstevel@tonic-gate if (mpp == NULL) 6737c478bd9Sstevel@tonic-gate nomem(); 6747c478bd9Sstevel@tonic-gate 6757c478bd9Sstevel@tonic-gate if (count == 0) { 6767c478bd9Sstevel@tonic-gate if (mntll == NULL) { /* no entries? */ 6777c478bd9Sstevel@tonic-gate listlength = 0; 6787c478bd9Sstevel@tonic-gate return (NULL); 6797c478bd9Sstevel@tonic-gate } 6807c478bd9Sstevel@tonic-gate /* 6817c478bd9Sstevel@tonic-gate * No mount list specified: take all mnttab mount points 6827c478bd9Sstevel@tonic-gate * except for a few cases. 6837c478bd9Sstevel@tonic-gate */ 6847c478bd9Sstevel@tonic-gate for (ndx = 0, mp = mntll; mp; mp = mp->link) { 6857c478bd9Sstevel@tonic-gate if (fsstrinlist(mp->ment.mnt_mountp, keeplist)) 6867c478bd9Sstevel@tonic-gate continue; 6877c478bd9Sstevel@tonic-gate mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp); 6887c478bd9Sstevel@tonic-gate if (mp->ment.mnt_fstype && 6897c478bd9Sstevel@tonic-gate (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0)) 6907c478bd9Sstevel@tonic-gate lofscnt++; 6917c478bd9Sstevel@tonic-gate 6927c478bd9Sstevel@tonic-gate mpp[ndx++] = mp; 6937c478bd9Sstevel@tonic-gate } 6947c478bd9Sstevel@tonic-gate mpp[ndx] = NULL; 6957c478bd9Sstevel@tonic-gate listlength = ndx; 6967c478bd9Sstevel@tonic-gate return (mpp); 6977c478bd9Sstevel@tonic-gate } 6987c478bd9Sstevel@tonic-gate 6997c478bd9Sstevel@tonic-gate /* 7007c478bd9Sstevel@tonic-gate * A list of mount points was specified on the command line. 7017c478bd9Sstevel@tonic-gate * Build an array out of these. 7027c478bd9Sstevel@tonic-gate */ 7037c478bd9Sstevel@tonic-gate for (ndx = 0; count--; ) { 7047c478bd9Sstevel@tonic-gate cp = *mntlist++; 7057c478bd9Sstevel@tonic-gate if (realpath(cp, resolve) == NULL) { 7067c478bd9Sstevel@tonic-gate fprintf(stderr, 7074bff34e3Sthurlow gettext("%s: warning: can't resolve %s\n"), 7084bff34e3Sthurlow myname, cp); 7097c478bd9Sstevel@tonic-gate exitcode = 1; 7107c478bd9Sstevel@tonic-gate mp = getmntlast(mntll, NULL, cp); /* try anyways */ 7117c478bd9Sstevel@tonic-gate } else 7127c478bd9Sstevel@tonic-gate mp = getmntlast(mntll, NULL, resolve); 7137c478bd9Sstevel@tonic-gate if (mp == NULL) { 7147c478bd9Sstevel@tonic-gate struct mnttab mnew; 7157c478bd9Sstevel@tonic-gate /* 7167c478bd9Sstevel@tonic-gate * Then we've reached the end without finding 7177c478bd9Sstevel@tonic-gate * what we are looking for, but we still have to 7187c478bd9Sstevel@tonic-gate * try to umount it: append it to mntarray. 7197c478bd9Sstevel@tonic-gate */ 7207c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 7214bff34e3Sthurlow "%s: warning: %s not found in %s\n"), 7224bff34e3Sthurlow myname, resolve, mnttab); 7237c478bd9Sstevel@tonic-gate exitcode = 1; 7247c478bd9Sstevel@tonic-gate mntnull(&mnew); 7257c478bd9Sstevel@tonic-gate mnew.mnt_special = mnew.mnt_mountp = strdup(resolve); 7267c478bd9Sstevel@tonic-gate if (mnew.mnt_special == NULL) 7277c478bd9Sstevel@tonic-gate nomem(); 7287c478bd9Sstevel@tonic-gate mp = new_mountent(&mnew); 7297c478bd9Sstevel@tonic-gate } 7307c478bd9Sstevel@tonic-gate if (mp->ment.mnt_fstype && 7317c478bd9Sstevel@tonic-gate (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0)) 7327c478bd9Sstevel@tonic-gate lofscnt++; 7337c478bd9Sstevel@tonic-gate 7347c478bd9Sstevel@tonic-gate mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp); 7357c478bd9Sstevel@tonic-gate mpp[ndx++] = mp; 7367c478bd9Sstevel@tonic-gate } 7377c478bd9Sstevel@tonic-gate mpp[ndx] = NULL; 7387c478bd9Sstevel@tonic-gate listlength = ndx; 7397c478bd9Sstevel@tonic-gate return (mpp); 7407c478bd9Sstevel@tonic-gate } 7417c478bd9Sstevel@tonic-gate 7427c478bd9Sstevel@tonic-gate /* 7437c478bd9Sstevel@tonic-gate * Returns the tail of a linked list of all mnttab entries. I.e, it's faster 7447c478bd9Sstevel@tonic-gate * to return the mnttab in reverse order. 7457c478bd9Sstevel@tonic-gate * Sets listlength to the number of entries in the list. 7467c478bd9Sstevel@tonic-gate * Returns NULL if none are found. 7477c478bd9Sstevel@tonic-gate */ 7487c478bd9Sstevel@tonic-gate mountent_t * 74908190127Sdh getmntall(void) 7507c478bd9Sstevel@tonic-gate { 7517c478bd9Sstevel@tonic-gate FILE *fp; 7527c478bd9Sstevel@tonic-gate mountent_t *mtail; 7537c478bd9Sstevel@tonic-gate int cnt = 0, ret; 7547c478bd9Sstevel@tonic-gate struct mnttab mget; 7557c478bd9Sstevel@tonic-gate 7567c478bd9Sstevel@tonic-gate if ((fp = fopen(mnttab, "r")) == NULL) { 7577c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("%s: warning cannot open %s\n"), 7584bff34e3Sthurlow myname, mnttab); 7597c478bd9Sstevel@tonic-gate return (0); 7607c478bd9Sstevel@tonic-gate } 7617c478bd9Sstevel@tonic-gate mtail = NULL; 7627c478bd9Sstevel@tonic-gate 7637c478bd9Sstevel@tonic-gate while ((ret = getmntent(fp, &mget)) != -1) { 7647c478bd9Sstevel@tonic-gate mountent_t *mp; 7657c478bd9Sstevel@tonic-gate 7667c478bd9Sstevel@tonic-gate if (ret > 0) { 7677c478bd9Sstevel@tonic-gate mnterror(ret); 7687c478bd9Sstevel@tonic-gate continue; 7697c478bd9Sstevel@tonic-gate } 7707c478bd9Sstevel@tonic-gate 7717c478bd9Sstevel@tonic-gate mp = new_mountent(&mget); 7727c478bd9Sstevel@tonic-gate mp->link = mtail; 7737c478bd9Sstevel@tonic-gate mtail = mp; 7747c478bd9Sstevel@tonic-gate cnt++; 7757c478bd9Sstevel@tonic-gate } 7767c478bd9Sstevel@tonic-gate fclose(fp); 7777c478bd9Sstevel@tonic-gate if (mtail == NULL) { 7787c478bd9Sstevel@tonic-gate listlength = 0; 7797c478bd9Sstevel@tonic-gate return (NULL); 7807c478bd9Sstevel@tonic-gate } 7817c478bd9Sstevel@tonic-gate listlength = cnt; 7827c478bd9Sstevel@tonic-gate return (mtail); 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate 7857c478bd9Sstevel@tonic-gate void 7867c478bd9Sstevel@tonic-gate do_umounts(mountent_t **mntarray) 7877c478bd9Sstevel@tonic-gate { 7887c478bd9Sstevel@tonic-gate mountent_t *mp, *mpprev, **ml = mntarray; 7897c478bd9Sstevel@tonic-gate int cnt = listlength; 7907c478bd9Sstevel@tonic-gate 7917c478bd9Sstevel@tonic-gate /* 7927c478bd9Sstevel@tonic-gate * Main loop for the forked children: 7937c478bd9Sstevel@tonic-gate */ 7947c478bd9Sstevel@tonic-gate for (mpprev = *ml; mp = *ml; mpprev = mp, ml++, cnt--) { 7957c478bd9Sstevel@tonic-gate pid_t pid; 7967c478bd9Sstevel@tonic-gate 7977c478bd9Sstevel@tonic-gate /* 7987c478bd9Sstevel@tonic-gate * Check to see if we cross a mount level: e.g., 7997c478bd9Sstevel@tonic-gate * /a/b/c -> /a/b. If so, we need to wait for all current 8007c478bd9Sstevel@tonic-gate * umounts to finish before umounting the rest. 8017c478bd9Sstevel@tonic-gate * 8027c478bd9Sstevel@tonic-gate * Also, we unmount serially as long as there are lofs's 8037c478bd9Sstevel@tonic-gate * to mount to avoid improper umount ordering. 8047c478bd9Sstevel@tonic-gate */ 8057c478bd9Sstevel@tonic-gate if (mp->mlevel < mpprev->mlevel || lofscnt > 0) 8067c478bd9Sstevel@tonic-gate while (nrun > 0 && (dowait() != -1)) 8077c478bd9Sstevel@tonic-gate ; 8087c478bd9Sstevel@tonic-gate 8097c478bd9Sstevel@tonic-gate if (lofscnt == 0) { 8107c478bd9Sstevel@tonic-gate /* 8117c478bd9Sstevel@tonic-gate * We can now go to parallel umounting. 8127c478bd9Sstevel@tonic-gate */ 8137c478bd9Sstevel@tonic-gate qsort((void *)ml, cnt, sizeof (mountent_t *), mcompar); 8147c478bd9Sstevel@tonic-gate mp = *ml; /* possible first entry */ 8157c478bd9Sstevel@tonic-gate lofscnt--; /* so we don't do this again */ 8167c478bd9Sstevel@tonic-gate } 8177c478bd9Sstevel@tonic-gate 8187c478bd9Sstevel@tonic-gate while (setup_iopipe(mp) == -1 && (dowait() != -1)) 8197c478bd9Sstevel@tonic-gate ; 8207c478bd9Sstevel@tonic-gate 8217c478bd9Sstevel@tonic-gate while (nrun >= maxrun && (dowait() != -1)) /* throttle */ 8227c478bd9Sstevel@tonic-gate ; 8237c478bd9Sstevel@tonic-gate 8247c478bd9Sstevel@tonic-gate if ((pid = fork()) == -1) { 8257c478bd9Sstevel@tonic-gate perror("fork"); 8267c478bd9Sstevel@tonic-gate cleanup(-1); 8277c478bd9Sstevel@tonic-gate /* not reached */ 8287c478bd9Sstevel@tonic-gate } 8297c478bd9Sstevel@tonic-gate #ifdef DEBUG 8307c478bd9Sstevel@tonic-gate if (dflg && pid > 0) { 8317c478bd9Sstevel@tonic-gate fprintf(stderr, "parent %d: umounting %d %s\n", 8324bff34e3Sthurlow getpid(), pid, mp->ment.mnt_mountp); 8337c478bd9Sstevel@tonic-gate } 8347c478bd9Sstevel@tonic-gate #endif 8357c478bd9Sstevel@tonic-gate if (pid == 0) { /* child */ 8367c478bd9Sstevel@tonic-gate signal(SIGHUP, SIG_IGN); 8377c478bd9Sstevel@tonic-gate signal(SIGQUIT, SIG_IGN); 8387c478bd9Sstevel@tonic-gate signal(SIGINT, SIG_IGN); 8397c478bd9Sstevel@tonic-gate setup_output(mp); 8407c478bd9Sstevel@tonic-gate doexec(&mp->ment); 8417c478bd9Sstevel@tonic-gate perror("exec"); 8427c478bd9Sstevel@tonic-gate exit(1); 8437c478bd9Sstevel@tonic-gate } 8447c478bd9Sstevel@tonic-gate 8457c478bd9Sstevel@tonic-gate /* parent */ 8467c478bd9Sstevel@tonic-gate (void) close(mp->sopipe[WRPIPE]); 8477c478bd9Sstevel@tonic-gate (void) close(mp->sepipe[WRPIPE]); 8487c478bd9Sstevel@tonic-gate mp->pid = pid; 8497c478bd9Sstevel@tonic-gate nrun++; 8507c478bd9Sstevel@tonic-gate } 8517c478bd9Sstevel@tonic-gate cleanup(0); 8527c478bd9Sstevel@tonic-gate } 8537c478bd9Sstevel@tonic-gate 8547c478bd9Sstevel@tonic-gate /* 8557c478bd9Sstevel@tonic-gate * cleanup the existing children and exit with an error 8567c478bd9Sstevel@tonic-gate * if asig != 0. 8577c478bd9Sstevel@tonic-gate */ 8587c478bd9Sstevel@tonic-gate void 8597c478bd9Sstevel@tonic-gate cleanup(int asig) 8607c478bd9Sstevel@tonic-gate { 8617c478bd9Sstevel@tonic-gate /* 8627c478bd9Sstevel@tonic-gate * Let the stragglers finish. 8637c478bd9Sstevel@tonic-gate */ 8647c478bd9Sstevel@tonic-gate while (nrun > 0 && (dowait() != -1)) 8657c478bd9Sstevel@tonic-gate ; 8667c478bd9Sstevel@tonic-gate if (asig != 0) 8677c478bd9Sstevel@tonic-gate exit(1); 8687c478bd9Sstevel@tonic-gate } 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate 8717c478bd9Sstevel@tonic-gate /* 8727c478bd9Sstevel@tonic-gate * Waits for 1 child to die. 8737c478bd9Sstevel@tonic-gate * 8747c478bd9Sstevel@tonic-gate * Returns -1 if no children are left to wait for. 8757c478bd9Sstevel@tonic-gate * Returns 0 if a child died without an error. 8767c478bd9Sstevel@tonic-gate * Returns 1 if a child died with an error. 8777c478bd9Sstevel@tonic-gate * Sets the global exitcode if an error occurred. 8787c478bd9Sstevel@tonic-gate */ 8797c478bd9Sstevel@tonic-gate int 88008190127Sdh dowait(void) 8817c478bd9Sstevel@tonic-gate { 8827c478bd9Sstevel@tonic-gate int wstat, child, ret; 883*0463c800SGordon Ross mountent_t *mp, *prevp; 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate if ((child = wait(&wstat)) == -1) 8867c478bd9Sstevel@tonic-gate return (-1); 8877c478bd9Sstevel@tonic-gate 8887c478bd9Sstevel@tonic-gate if (WIFEXITED(wstat)) /* this should always be true */ 8897c478bd9Sstevel@tonic-gate ret = WEXITSTATUS(wstat); 8907c478bd9Sstevel@tonic-gate else 8917c478bd9Sstevel@tonic-gate ret = 1; /* assume some kind of error */ 8927c478bd9Sstevel@tonic-gate nrun--; 8937c478bd9Sstevel@tonic-gate if (ret) 8947c478bd9Sstevel@tonic-gate exitcode = 1; 8957c478bd9Sstevel@tonic-gate 8967c478bd9Sstevel@tonic-gate /* 8977c478bd9Sstevel@tonic-gate * Find our child so we can process its std output, if any. 8987c478bd9Sstevel@tonic-gate * This search gets smaller and smaller as children are cleaned 8997c478bd9Sstevel@tonic-gate * up. 9007c478bd9Sstevel@tonic-gate */ 9017c478bd9Sstevel@tonic-gate for (prevp = NULL, mp = mntll; mp; mp = mp->link) { 9027c478bd9Sstevel@tonic-gate if (mp->pid != child) { 9037c478bd9Sstevel@tonic-gate prevp = mp; 9047c478bd9Sstevel@tonic-gate continue; 9057c478bd9Sstevel@tonic-gate } 9067c478bd9Sstevel@tonic-gate /* 9077c478bd9Sstevel@tonic-gate * Found: let's remove it from this list. 9087c478bd9Sstevel@tonic-gate */ 9097c478bd9Sstevel@tonic-gate if (prevp) { 9107c478bd9Sstevel@tonic-gate prevp->link = mp->link; 9117c478bd9Sstevel@tonic-gate mp->link = NULL; 9127c478bd9Sstevel@tonic-gate } 9137c478bd9Sstevel@tonic-gate break; 9147c478bd9Sstevel@tonic-gate } 9157c478bd9Sstevel@tonic-gate 9167c478bd9Sstevel@tonic-gate if (mp == NULL) { 9177c478bd9Sstevel@tonic-gate /* 9187c478bd9Sstevel@tonic-gate * This should never happen. 9197c478bd9Sstevel@tonic-gate */ 9207c478bd9Sstevel@tonic-gate #ifdef DEBUG 9217c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 9224bff34e3Sthurlow "%s: unknown child %d\n"), myname, child); 9237c478bd9Sstevel@tonic-gate #endif 9247c478bd9Sstevel@tonic-gate exitcode = 1; 9257c478bd9Sstevel@tonic-gate return (1); 9267c478bd9Sstevel@tonic-gate } 9277c478bd9Sstevel@tonic-gate doio(mp); /* Any output? */ 9287c478bd9Sstevel@tonic-gate 9297c478bd9Sstevel@tonic-gate if (mp->ment.mnt_fstype && 9307c478bd9Sstevel@tonic-gate (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0)) 9317c478bd9Sstevel@tonic-gate lofscnt--; 9327c478bd9Sstevel@tonic-gate 9337c478bd9Sstevel@tonic-gate return (ret); 9347c478bd9Sstevel@tonic-gate } 9357c478bd9Sstevel@tonic-gate 9367c478bd9Sstevel@tonic-gate static const mountent_t zmount = { 0 }; 9377c478bd9Sstevel@tonic-gate 9387c478bd9Sstevel@tonic-gate mountent_t * 9397c478bd9Sstevel@tonic-gate new_mountent(struct mnttab *ment) 9407c478bd9Sstevel@tonic-gate { 9417c478bd9Sstevel@tonic-gate mountent_t *new; 9427c478bd9Sstevel@tonic-gate 9437c478bd9Sstevel@tonic-gate new = (mountent_t *)malloc(sizeof (*new)); 9447c478bd9Sstevel@tonic-gate if (new == NULL) 9457c478bd9Sstevel@tonic-gate nomem(); 9467c478bd9Sstevel@tonic-gate 9477c478bd9Sstevel@tonic-gate *new = zmount; 9487c478bd9Sstevel@tonic-gate if (ment->mnt_special && 9497c478bd9Sstevel@tonic-gate (new->ment.mnt_special = strdup(ment->mnt_special)) == NULL) 9507c478bd9Sstevel@tonic-gate nomem(); 9517c478bd9Sstevel@tonic-gate if (ment->mnt_mountp && 9527c478bd9Sstevel@tonic-gate (new->ment.mnt_mountp = strdup(ment->mnt_mountp)) == NULL) 9537c478bd9Sstevel@tonic-gate nomem(); 9547c478bd9Sstevel@tonic-gate if (ment->mnt_fstype && 9557c478bd9Sstevel@tonic-gate (new->ment.mnt_fstype = strdup(ment->mnt_fstype)) == NULL) 9567c478bd9Sstevel@tonic-gate nomem(); 9577c478bd9Sstevel@tonic-gate return (new); 9587c478bd9Sstevel@tonic-gate } 9597c478bd9Sstevel@tonic-gate 9607c478bd9Sstevel@tonic-gate 9617c478bd9Sstevel@tonic-gate /* 9627c478bd9Sstevel@tonic-gate * Sort in descending order of "mount level". For example, /a/b/c is 9637c478bd9Sstevel@tonic-gate * placed before /a/b . 9647c478bd9Sstevel@tonic-gate */ 9657c478bd9Sstevel@tonic-gate int 9667c478bd9Sstevel@tonic-gate mcompar(const void *a, const void *b) 9677c478bd9Sstevel@tonic-gate { 9687c478bd9Sstevel@tonic-gate mountent_t *a1, *b1; 9697c478bd9Sstevel@tonic-gate 9707c478bd9Sstevel@tonic-gate a1 = *(mountent_t **)a; 9717c478bd9Sstevel@tonic-gate b1 = *(mountent_t **)b; 9727c478bd9Sstevel@tonic-gate return (b1->mlevel - a1->mlevel); 9737c478bd9Sstevel@tonic-gate } 9747c478bd9Sstevel@tonic-gate 9757c478bd9Sstevel@tonic-gate /* 9767c478bd9Sstevel@tonic-gate * The purpose of this routine is to form stdout and stderr 9777c478bd9Sstevel@tonic-gate * pipes for the children's output. The parent then reads and writes it 9787c478bd9Sstevel@tonic-gate * out it serially in order to ensure that the output is 9797c478bd9Sstevel@tonic-gate * not garbled. 9807c478bd9Sstevel@tonic-gate */ 9817c478bd9Sstevel@tonic-gate 9827c478bd9Sstevel@tonic-gate int 9837c478bd9Sstevel@tonic-gate setup_iopipe(mountent_t *mp) 9847c478bd9Sstevel@tonic-gate { 9857c478bd9Sstevel@tonic-gate /* 9867c478bd9Sstevel@tonic-gate * Make a stdout and stderr pipe. This should never fail. 9877c478bd9Sstevel@tonic-gate */ 9887c478bd9Sstevel@tonic-gate if (pipe(mp->sopipe) == -1) 9897c478bd9Sstevel@tonic-gate return (-1); 9907c478bd9Sstevel@tonic-gate if (pipe(mp->sepipe) == -1) { 9917c478bd9Sstevel@tonic-gate (void) close(mp->sopipe[RDPIPE]); 9927c478bd9Sstevel@tonic-gate (void) close(mp->sopipe[WRPIPE]); 9937c478bd9Sstevel@tonic-gate return (-1); 9947c478bd9Sstevel@tonic-gate } 9957c478bd9Sstevel@tonic-gate /* 9967c478bd9Sstevel@tonic-gate * Don't block on an empty pipe. 9977c478bd9Sstevel@tonic-gate */ 9987c478bd9Sstevel@tonic-gate (void) fcntl(mp->sopipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK); 9997c478bd9Sstevel@tonic-gate (void) fcntl(mp->sepipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK); 10007c478bd9Sstevel@tonic-gate return (0); 10017c478bd9Sstevel@tonic-gate } 10027c478bd9Sstevel@tonic-gate 10037c478bd9Sstevel@tonic-gate /* 10047c478bd9Sstevel@tonic-gate * Called by a child to attach its stdout and stderr to the write side of 10057c478bd9Sstevel@tonic-gate * the pipes. 10067c478bd9Sstevel@tonic-gate */ 10077c478bd9Sstevel@tonic-gate void 10087c478bd9Sstevel@tonic-gate setup_output(mountent_t *mp) 10097c478bd9Sstevel@tonic-gate { 10107c478bd9Sstevel@tonic-gate (void) close(fileno(stdout)); 10117c478bd9Sstevel@tonic-gate (void) dup(mp->sopipe[WRPIPE]); 10127c478bd9Sstevel@tonic-gate (void) close(mp->sopipe[WRPIPE]); 10137c478bd9Sstevel@tonic-gate 10147c478bd9Sstevel@tonic-gate (void) close(fileno(stderr)); 10157c478bd9Sstevel@tonic-gate (void) dup(mp->sepipe[WRPIPE]); 10167c478bd9Sstevel@tonic-gate (void) close(mp->sepipe[WRPIPE]); 10177c478bd9Sstevel@tonic-gate } 10187c478bd9Sstevel@tonic-gate 10197c478bd9Sstevel@tonic-gate /* 10207c478bd9Sstevel@tonic-gate * Parent uses this to print any stdout or stderr output issued by 10217c478bd9Sstevel@tonic-gate * the child. 10227c478bd9Sstevel@tonic-gate */ 10237c478bd9Sstevel@tonic-gate static void 10247c478bd9Sstevel@tonic-gate doio(mountent_t *mp) 10257c478bd9Sstevel@tonic-gate { 10267c478bd9Sstevel@tonic-gate int bytes; 10277c478bd9Sstevel@tonic-gate 10287c478bd9Sstevel@tonic-gate while ((bytes = read(mp->sepipe[RDPIPE], ibuf, sizeof (ibuf))) > 0) 10297c478bd9Sstevel@tonic-gate write(fileno(stderr), ibuf, bytes); 10307c478bd9Sstevel@tonic-gate while ((bytes = read(mp->sopipe[RDPIPE], ibuf, sizeof (ibuf))) > 0) 10317c478bd9Sstevel@tonic-gate write(fileno(stdout), ibuf, bytes); 10327c478bd9Sstevel@tonic-gate 10337c478bd9Sstevel@tonic-gate (void) close(mp->sopipe[RDPIPE]); 10347c478bd9Sstevel@tonic-gate (void) close(mp->sepipe[RDPIPE]); 10357c478bd9Sstevel@tonic-gate } 10367c478bd9Sstevel@tonic-gate 10377c478bd9Sstevel@tonic-gate void 103808190127Sdh nomem(void) 10397c478bd9Sstevel@tonic-gate { 10407c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("%s: out of memory\n"), myname); 10417c478bd9Sstevel@tonic-gate /* 10427c478bd9Sstevel@tonic-gate * Let the stragglers finish. 10437c478bd9Sstevel@tonic-gate */ 10447c478bd9Sstevel@tonic-gate while (nrun > 0 && (dowait() != -1)) 10457c478bd9Sstevel@tonic-gate ; 10467c478bd9Sstevel@tonic-gate exit(1); 10477c478bd9Sstevel@tonic-gate } 1048