umount.c revision 1.11 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.11 1995/01/30 17:03:15 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, vflag;
68 char **typelist;
69 char *nfshost;
70
71 char *getmntname __P((char *, mntwhat, char *));
72 void maketypelist __P((char *));
73 int selected __P((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 maketypelist(optarg);
108 break;
109 case 'v':
110 vflag = 1;
111 break;
112 default:
113 usage();
114 /* NOTREACHED */
115 }
116 argc -= optind;
117 argv += optind;
118
119 if (argc == 0 && !all || argc != 0 && all)
120 usage();
121
122 /* -h implies "-t nfs" if no -t flag. */
123 if ((nfshost != NULL) && (typelist == NULL))
124 maketypelist("nfs");
125
126 if (all) {
127 if (setfsent() == 0)
128 err(1, "%s", _PATH_FSTAB);
129 errs = umountall();
130 } else
131 for (errs = 0; *argv != NULL; ++argv)
132 if (umountfs(*argv) != 0)
133 errs = 1;
134 exit(errs);
135 }
136
137 int
138 umountall()
139 {
140 struct fstab *fs;
141 int rval;
142 char *cp;
143
144 while ((fs = getfsent()) != NULL) {
145 /* Ignore the root. */
146 if (strcmp(fs->fs_file, "/") == 0)
147 continue;
148 /*
149 * !!!
150 * Historic practice: ignore unknown FSTAB_* fields.
151 */
152 if (strcmp(fs->fs_type, FSTAB_RW) &&
153 strcmp(fs->fs_type, FSTAB_RO) &&
154 strcmp(fs->fs_type, FSTAB_RQ))
155 continue;
156
157 if (!selected(fs->fs_vfstype))
158 continue;
159
160 /*
161 * We want to unmount the file systems in the reverse order
162 * that they were mounted. So, we save off the file name
163 * in some allocated memory, and then call recursively.
164 */
165 if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
166 err(1, NULL);
167 (void)strcpy(cp, fs->fs_file);
168 rval = umountall();
169 return (umountfs(cp) || rval);
170 }
171 return (0);
172 }
173
174 int
175 umountfs(name)
176 char *name;
177 {
178 enum clnt_stat clnt_stat;
179 struct hostent *hp;
180 struct sockaddr_in saddr;
181 struct stat sb;
182 struct timeval pertry, try;
183 CLIENT *clp;
184 int so;
185 char *delimp, *hostp, *mntpt, rname[MAXPATHLEN], type[MFSNAMELEN];
186
187 if (realpath(name, rname) == NULL) {
188 warn("%s", rname);
189 return (1);
190 }
191
192 name = rname;
193
194 if (stat(name, &sb) < 0) {
195 if (((mntpt = getmntname(name, MNTFROM, type)) == NULL) &&
196 ((mntpt = getmntname(name, MNTON, type)) == NULL)) {
197 warnx("%s: not currently mounted", name);
198 return (1);
199 }
200 } else if (S_ISBLK(sb.st_mode)) {
201 if ((mntpt = getmntname(name, MNTON, type)) == NULL) {
202 warnx("%s: not currently mounted", name);
203 return (1);
204 }
205 } else if (S_ISDIR(sb.st_mode)) {
206 mntpt = name;
207 if ((name = getmntname(mntpt, MNTFROM, type)) == NULL) {
208 warnx("%s: not currently mounted", mntpt);
209 return (1);
210 }
211 } else {
212 warnx("%s: not a directory or special device", name);
213 return (1);
214 }
215
216 if (!selected(type))
217 return (1);
218
219 if (!strcmp(type, MOUNT_NFS)) {
220 if ((delimp = strchr(name, '@')) != NULL) {
221 hostp = delimp + 1;
222 *delimp = '\0';
223 hp = gethostbyname(hostp);
224 *delimp = '@';
225 } else if ((delimp = strchr(name, ':')) != NULL) {
226 *delimp = '\0';
227 hostp = name;
228 hp = gethostbyname(hostp);
229 name = delimp + 1;
230 *delimp = ':';
231 } else
232 hp = NULL;
233 if (!namematch(hp))
234 return (1);
235 }
236
237 if (vflag)
238 (void)printf("%s: unmount from %s\n", name, mntpt);
239 if (fake)
240 return (0);
241
242 if (unmount(mntpt, fflag) < 0) {
243 warn("%s", mntpt);
244 return (1);
245 }
246
247 if (!strcmp(type, MOUNT_NFS) &&
248 (hp != NULL) && !(fflag & MNT_FORCE)) {
249 *delimp = '\0';
250 memset(&saddr, 0, sizeof(saddr));
251 saddr.sin_family = AF_INET;
252 saddr.sin_port = 0;
253 memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
254 pertry.tv_sec = 3;
255 pertry.tv_usec = 0;
256 so = RPC_ANYSOCK;
257 if ((clp = clntudp_create(&saddr,
258 RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
259 clnt_pcreateerror("Cannot MNT PRC");
260 return (1);
261 }
262 clp->cl_auth = authunix_create_default();
263 try.tv_sec = 20;
264 try.tv_usec = 0;
265 clnt_stat = clnt_call(clp,
266 RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, try);
267 if (clnt_stat != RPC_SUCCESS) {
268 clnt_perror(clp, "Bad MNT RPC");
269 return (1);
270 }
271 auth_destroy(clp->cl_auth);
272 clnt_destroy(clp);
273 }
274 return (0);
275 }
276
277 char *
278 getmntname(name, what, type)
279 char *name;
280 mntwhat what;
281 char *type;
282 {
283 struct statfs *mntbuf;
284 int i, mntsize;
285
286 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
287 warn("getmntinfo");
288 return (NULL);
289 }
290 for (i = 0; i < mntsize; i++) {
291 if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
292 if (type)
293 memcpy(type, mntbuf[i].f_fstypename,
294 sizeof(mntbuf[i].f_fstypename));
295 return (mntbuf[i].f_mntonname);
296 }
297 if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
298 if (type)
299 memcpy(type, mntbuf[i].f_fstypename,
300 sizeof(mntbuf[i].f_fstypename));
301 return (mntbuf[i].f_mntfromname);
302 }
303 }
304 return (NULL);
305 }
306
307 static enum { IN_LIST, NOT_IN_LIST } which;
308
309 int
310 selected(type)
311 char *type;
312 {
313 char **av;
314
315 /* If no type specified, it's always selected. */
316 if (typelist == NULL)
317 return (1);
318 for (av = typelist; *av != NULL; ++av)
319 if (!strcmp(type, *av))
320 return (which == IN_LIST ? 1 : 0);
321 return (which == IN_LIST ? 0 : 1);
322 }
323
324 void
325 maketypelist(fslist)
326 char *fslist;
327 {
328 int i;
329 char *nextcp, **av;
330
331 if ((fslist == NULL) || (fslist[0] == '\0'))
332 errx(1, "empty type list");
333
334 /*
335 * XXX
336 * Note: the syntax is "noxxx,yyy" for no xxx's and
337 * no yyy's, not the more intuitive "noyyy,noyyy".
338 */
339 if (fslist[0] == 'n' && fslist[1] == 'o') {
340 fslist += 2;
341 which = NOT_IN_LIST;
342 } else
343 which = IN_LIST;
344
345 /* Count the number of types. */
346 for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++)
347 ++nextcp;
348
349 /* Build an array of that many types. */
350 if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
351 err(1, NULL);
352 av[0] = fslist;
353 for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++) {
354 *nextcp = '\0';
355 av[i] = ++nextcp;
356 }
357 /* Terminate the array. */
358 av[i] = NULL;
359 }
360
361 int
362 namematch(hp)
363 struct hostent *hp;
364 {
365 char *cp, **np;
366
367 if ((hp == NULL) || (nfshost == NULL))
368 return (1);
369
370 if (strcasecmp(nfshost, hp->h_name) == 0)
371 return (1);
372
373 if ((cp = strchr(hp->h_name, '.')) != NULL) {
374 *cp = '\0';
375 if (strcasecmp(nfshost, hp->h_name) == 0)
376 return (1);
377 }
378 for (np = hp->h_aliases; *np; np++) {
379 if (strcasecmp(nfshost, *np) == 0)
380 return (1);
381 if ((cp = strchr(*np, '.')) != NULL) {
382 *cp = '\0';
383 if (strcasecmp(nfshost, *np) == 0)
384 return (1);
385 }
386 }
387 return (0);
388 }
389
390 /*
391 * xdr routines for mount rpc's
392 */
393 int
394 xdr_dir(xdrsp, dirp)
395 XDR *xdrsp;
396 char *dirp;
397 {
398 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
399 }
400
401 void
402 usage()
403 {
404 (void)fprintf(stderr,
405 "usage: %s\n %s\n",
406 "umount [-fv] [-t fstypelist] special | node",
407 "umount -a[fv] [-h host] [-t fstypelist]");
408 exit(1);
409 }
410