umount.c revision 1.12 1 /*-
2 * Copyright (c) 1980, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char copyright[] =
36 "@(#) Copyright (c) 1980, 1989, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)umount.c 8.3 (Berkeley) 2/20/94";*/
42 static char *rcsid = "$Id: umount.c,v 1.12 1995/01/30 17:20:06 mycroft Exp $";
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 #include <sys/mount.h>
48 #include <sys/time.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51
52 #include <netdb.h>
53 #include <rpc/rpc.h>
54 #include <rpc/pmap_clnt.h>
55 #include <rpc/pmap_prot.h>
56 #include <nfs/rpcv2.h>
57
58 #include <err.h>
59 #include <fstab.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64
65 typedef enum { MNTON, MNTFROM } mntwhat;
66
67 int fake, fflag, verbose;
68 char **typelist = NULL;
69 char *nfshost;
70
71 char *getmntname __P((char *, mntwhat, char *));
72 void maketypelist __P((char *));
73 int selected __P((const char *));
74 int namematch __P((struct hostent *));
75 int umountall __P((void));
76 int umountfs __P((char *));
77 void usage __P((void));
78 int xdr_dir __P((XDR *, char *));
79
80 int
81 main(argc, argv)
82 int argc;
83 char *argv[];
84 {
85 int all, ch, errs;
86
87 /* Start disks transferring immediately. */
88 sync();
89
90 all = 0;
91 while ((ch = getopt(argc, argv, "aFfh:t:v")) != EOF)
92 switch (ch) {
93 case 'a':
94 all = 1;
95 break;
96 case 'F':
97 fake = 1;
98 break;
99 case 'f':
100 fflag = MNT_FORCE;
101 break;
102 case 'h': /* -h implies -a. */
103 all = 1;
104 nfshost = optarg;
105 break;
106 case 't':
107 if (typelist != NULL)
108 errx(1, "only one -t option may be specified.");
109 maketypelist(optarg);
110 break;
111 case 'v':
112 verbose = 1;
113 break;
114 default:
115 usage();
116 /* NOTREACHED */
117 }
118 argc -= optind;
119 argv += optind;
120
121 if (argc == 0 && !all || argc != 0 && all)
122 usage();
123
124 /* -h implies "-t nfs" if no -t flag. */
125 if ((nfshost != NULL) && (typelist == NULL))
126 maketypelist("nfs");
127
128 if (all) {
129 if (setfsent() == 0)
130 err(1, "%s", _PATH_FSTAB);
131 errs = umountall();
132 } else
133 for (errs = 0; *argv != NULL; ++argv)
134 if (umountfs(*argv) != 0)
135 errs = 1;
136 exit(errs);
137 }
138
139 int
140 umountall()
141 {
142 struct fstab *fs;
143 int rval;
144 char *cp;
145
146 while ((fs = getfsent()) != NULL) {
147 /* Ignore the root. */
148 if (strcmp(fs->fs_file, "/") == 0)
149 continue;
150 /*
151 * !!!
152 * Historic practice: ignore unknown FSTAB_* fields.
153 */
154 if (strcmp(fs->fs_type, FSTAB_RW) &&
155 strcmp(fs->fs_type, FSTAB_RO) &&
156 strcmp(fs->fs_type, FSTAB_RQ))
157 continue;
158
159 if (!selected(fs->fs_vfstype))
160 continue;
161
162 /*
163 * We want to unmount the file systems in the reverse order
164 * that they were mounted. So, we save off the file name
165 * in some allocated memory, and then call recursively.
166 */
167 if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
168 err(1, NULL);
169 (void)strcpy(cp, fs->fs_file);
170 rval = umountall();
171 return (umountfs(cp) || rval);
172 }
173 return (0);
174 }
175
176 int
177 umountfs(name)
178 char *name;
179 {
180 enum clnt_stat clnt_stat;
181 struct hostent *hp;
182 struct sockaddr_in saddr;
183 struct stat sb;
184 struct timeval pertry, try;
185 CLIENT *clp;
186 int so;
187 char *delimp, *hostp, *mntpt, rname[MAXPATHLEN], type[MFSNAMELEN];
188
189 if (realpath(name, rname) == NULL) {
190 warn("%s", rname);
191 return (1);
192 }
193
194 name = rname;
195
196 if (stat(name, &sb) < 0) {
197 if (((mntpt = getmntname(name, MNTFROM, type)) == NULL) &&
198 ((mntpt = getmntname(name, MNTON, type)) == NULL)) {
199 warnx("%s: not currently mounted", name);
200 return (1);
201 }
202 } else if (S_ISBLK(sb.st_mode)) {
203 if ((mntpt = getmntname(name, MNTON, type)) == NULL) {
204 warnx("%s: not currently mounted", name);
205 return (1);
206 }
207 } else if (S_ISDIR(sb.st_mode)) {
208 mntpt = name;
209 if ((name = getmntname(mntpt, MNTFROM, type)) == NULL) {
210 warnx("%s: not currently mounted", mntpt);
211 return (1);
212 }
213 } else {
214 warnx("%s: not a directory or special device", name);
215 return (1);
216 }
217
218 if (!selected(type))
219 return (1);
220
221 if (!strcmp(type, MOUNT_NFS)) {
222 if ((delimp = strchr(name, '@')) != NULL) {
223 hostp = delimp + 1;
224 *delimp = '\0';
225 hp = gethostbyname(hostp);
226 *delimp = '@';
227 } else if ((delimp = strchr(name, ':')) != NULL) {
228 *delimp = '\0';
229 hostp = name;
230 hp = gethostbyname(hostp);
231 name = delimp + 1;
232 *delimp = ':';
233 } else
234 hp = NULL;
235 if (!namematch(hp))
236 return (1);
237 }
238
239 if (verbose)
240 (void)printf("%s: unmount from %s\n", name, mntpt);
241 if (fake)
242 return (0);
243
244 if (unmount(mntpt, fflag) < 0) {
245 warn("%s", mntpt);
246 return (1);
247 }
248
249 if (!strcmp(type, MOUNT_NFS) &&
250 (hp != NULL) && !(fflag & MNT_FORCE)) {
251 *delimp = '\0';
252 memset(&saddr, 0, sizeof(saddr));
253 saddr.sin_family = AF_INET;
254 saddr.sin_port = 0;
255 memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
256 pertry.tv_sec = 3;
257 pertry.tv_usec = 0;
258 so = RPC_ANYSOCK;
259 if ((clp = clntudp_create(&saddr,
260 RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
261 clnt_pcreateerror("Cannot MNT PRC");
262 return (1);
263 }
264 clp->cl_auth = authunix_create_default();
265 try.tv_sec = 20;
266 try.tv_usec = 0;
267 clnt_stat = clnt_call(clp,
268 RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, try);
269 if (clnt_stat != RPC_SUCCESS) {
270 clnt_perror(clp, "Bad MNT RPC");
271 return (1);
272 }
273 auth_destroy(clp->cl_auth);
274 clnt_destroy(clp);
275 }
276 return (0);
277 }
278
279 char *
280 getmntname(name, what, type)
281 char *name;
282 mntwhat what;
283 char *type;
284 {
285 struct statfs *mntbuf;
286 int i, mntsize;
287
288 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
289 warn("getmntinfo");
290 return (NULL);
291 }
292 for (i = 0; i < mntsize; i++) {
293 if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
294 if (type)
295 memcpy(type, mntbuf[i].f_fstypename,
296 sizeof(mntbuf[i].f_fstypename));
297 return (mntbuf[i].f_mntonname);
298 }
299 if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
300 if (type)
301 memcpy(type, mntbuf[i].f_fstypename,
302 sizeof(mntbuf[i].f_fstypename));
303 return (mntbuf[i].f_mntfromname);
304 }
305 }
306 return (NULL);
307 }
308
309 static enum { IN_LIST, NOT_IN_LIST } which;
310
311 int
312 selected(type)
313 const char *type;
314 {
315 char **av;
316
317 /* If no type specified, it's always selected. */
318 if (typelist == NULL)
319 return (1);
320 for (av = typelist; *av != NULL; ++av)
321 if (!strcmp(type, *av))
322 return (which == IN_LIST ? 1 : 0);
323 return (which == IN_LIST ? 0 : 1);
324 }
325
326 void
327 maketypelist(fslist)
328 char *fslist;
329 {
330 int i;
331 char *nextcp, **av;
332
333 if ((fslist == NULL) || (fslist[0] == '\0'))
334 errx(1, "empty type list");
335
336 /*
337 * XXX
338 * Note: the syntax is "noxxx,yyy" for no xxx's and
339 * no yyy's, not the more intuitive "noyyy,noyyy".
340 */
341 if (fslist[0] == 'n' && fslist[1] == 'o') {
342 fslist += 2;
343 which = NOT_IN_LIST;
344 } else
345 which = IN_LIST;
346
347 /* Count the number of types. */
348 for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++)
349 ++nextcp;
350
351 /* Build an array of that many types. */
352 if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
353 err(1, NULL);
354 av[0] = fslist;
355 for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++) {
356 *nextcp = '\0';
357 av[i] = ++nextcp;
358 }
359 /* Terminate the array. */
360 av[i] = NULL;
361 }
362
363 int
364 namematch(hp)
365 struct hostent *hp;
366 {
367 char *cp, **np;
368
369 if ((hp == NULL) || (nfshost == NULL))
370 return (1);
371
372 if (strcasecmp(nfshost, hp->h_name) == 0)
373 return (1);
374
375 if ((cp = strchr(hp->h_name, '.')) != NULL) {
376 *cp = '\0';
377 if (strcasecmp(nfshost, hp->h_name) == 0)
378 return (1);
379 }
380 for (np = hp->h_aliases; *np; np++) {
381 if (strcasecmp(nfshost, *np) == 0)
382 return (1);
383 if ((cp = strchr(*np, '.')) != NULL) {
384 *cp = '\0';
385 if (strcasecmp(nfshost, *np) == 0)
386 return (1);
387 }
388 }
389 return (0);
390 }
391
392 /*
393 * xdr routines for mount rpc's
394 */
395 int
396 xdr_dir(xdrsp, dirp)
397 XDR *xdrsp;
398 char *dirp;
399 {
400 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
401 }
402
403 void
404 usage()
405 {
406 (void)fprintf(stderr,
407 "usage: %s\n %s\n",
408 "umount [-fv] [-t fstypelist] special | node",
409 "umount -a[fv] [-h host] [-t fstypelist]");
410 exit(1);
411 }
412