umount.c revision 1.8 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.8 1994/08/23 20:57:04 cgd 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 (0);
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 (0);
218
219 if ((delimp = strchr(name, '@')) != NULL) {
220 hostp = delimp + 1;
221 *delimp = '\0';
222 hp = gethostbyname(hostp);
223 *delimp = '@';
224 } else if ((delimp = strchr(name, ':')) != NULL) {
225 *delimp = '\0';
226 hostp = name;
227 hp = gethostbyname(hostp);
228 name = delimp + 1;
229 *delimp = ':';
230 } else
231 hp = NULL;
232 if (!namematch(hp))
233 return (0);
234
235 if (vflag)
236 (void)printf("%s: unmount from %s\n", name, mntpt);
237 if (fake)
238 return (0);
239
240 if (unmount(mntpt, fflag) < 0) {
241 warn("%s", mntpt);
242 return (1);
243 }
244
245 if ((hp != NULL) && !(fflag & MNT_FORCE)) {
246 *delimp = '\0';
247 memset(&saddr, 0, sizeof(saddr));
248 saddr.sin_family = AF_INET;
249 saddr.sin_port = 0;
250 memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
251 pertry.tv_sec = 3;
252 pertry.tv_usec = 0;
253 so = RPC_ANYSOCK;
254 if ((clp = clntudp_create(&saddr,
255 RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
256 clnt_pcreateerror("Cannot MNT PRC");
257 return (1);
258 }
259 clp->cl_auth = authunix_create_default();
260 try.tv_sec = 20;
261 try.tv_usec = 0;
262 clnt_stat = clnt_call(clp,
263 RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, try);
264 if (clnt_stat != RPC_SUCCESS) {
265 clnt_perror(clp, "Bad MNT RPC");
266 return (1);
267 }
268 auth_destroy(clp->cl_auth);
269 clnt_destroy(clp);
270 }
271 return (0);
272 }
273
274 char *
275 getmntname(name, what, type)
276 char *name;
277 mntwhat what;
278 char *type;
279 {
280 struct statfs *mntbuf;
281 int i, mntsize;
282
283 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
284 warn("getmntinfo");
285 return (NULL);
286 }
287 for (i = 0; i < mntsize; i++) {
288 if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
289 if (type)
290 memcpy(type, mntbuf[i].f_fstypename,
291 sizeof(mntbuf[i].f_fstypename));
292 return (mntbuf[i].f_mntonname);
293 }
294 if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
295 if (type)
296 memcpy(type, mntbuf[i].f_fstypename,
297 sizeof(mntbuf[i].f_fstypename));
298 return (mntbuf[i].f_mntfromname);
299 }
300 }
301 return (NULL);
302 }
303
304 static enum { IN_LIST, NOT_IN_LIST } which;
305
306 int
307 selected(type)
308 char *type;
309 {
310 char **av;
311
312 /* If no type specified, it's always selected. */
313 if (typelist == NULL)
314 return (1);
315 for (av = typelist; *av != NULL; ++av)
316 if (!strcmp(type, *av))
317 return (which == IN_LIST ? 1 : 0);
318 return (which == IN_LIST ? 0 : 1);
319 }
320
321 void
322 maketypelist(fslist)
323 char *fslist;
324 {
325 int i;
326 char *nextcp, **av;
327
328 if ((fslist == NULL) || (fslist[0] == '\0'))
329 errx(1, "empty type list");
330
331 /*
332 * XXX
333 * Note: the syntax is "noxxx,yyy" for no xxx's and
334 * no yyy's, not the more intuitive "noyyy,noyyy".
335 */
336 if (fslist[0] == 'n' && fslist[1] == 'o') {
337 fslist += 2;
338 which = NOT_IN_LIST;
339 } else
340 which = IN_LIST;
341
342 /* Count the number of types. */
343 for (i = 0, nextcp = fslist; *nextcp != NULL; ++nextcp)
344 if (*nextcp == ',')
345 i++;
346
347 /* Build an array of that many types. */
348 if ((av = typelist = malloc((i + 2) * sizeof(char *))) == NULL)
349 err(1, NULL);
350 for (i = 0; fslist != NULL; fslist = nextcp, ++i) {
351 if ((nextcp = strchr(fslist, ',')) != NULL)
352 *nextcp++ = '\0';
353 /* Note that we're keeping pointers into the input string. */
354 av[i] = fslist;
355 }
356 /* Terminate the array. */
357 av[i++] = NULL;
358 }
359
360 int
361 namematch(hp)
362 struct hostent *hp;
363 {
364 char *cp, **np;
365
366 if ((hp == NULL) || (nfshost == NULL))
367 return (1);
368
369 if (strcasecmp(nfshost, hp->h_name) == 0)
370 return (1);
371
372 if ((cp = strchr(hp->h_name, '.')) != NULL) {
373 *cp = '\0';
374 if (strcasecmp(nfshost, hp->h_name) == 0)
375 return (1);
376 }
377 for (np = hp->h_aliases; *np; np++) {
378 if (strcasecmp(nfshost, *np) == 0)
379 return (1);
380 if ((cp = strchr(*np, '.')) != NULL) {
381 *cp = '\0';
382 if (strcasecmp(nfshost, *np) == 0)
383 return (1);
384 }
385 }
386 return (0);
387 }
388
389 /*
390 * xdr routines for mount rpc's
391 */
392 int
393 xdr_dir(xdrsp, dirp)
394 XDR *xdrsp;
395 char *dirp;
396 {
397 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
398 }
399
400 void
401 usage()
402 {
403 (void)fprintf(stderr,
404 "usage: %s\n %s\n",
405 "umount [-fv] [-t fstypelist] special | node",
406 "umount -a[fv] [-h host] [-t fstypelist]");
407 exit(1);
408 }
409