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