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