umount.c revision 1.19 1 /* $NetBSD: umount.c,v 1.19 1997/09/15 11:34:00 lukem 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: umount.c,v 1.19 1997/09/15 11:34:00 lukem 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 { MNTANY, 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 main __P((int, char *[]));
79 int namematch __P((struct hostent *));
80 int selected __P((const char *));
81 int umountall __P((void));
82 int umountfs __P((char *));
83 void usage __P((void));
84 int xdr_dir __P((XDR *, char *));
85
86 int
87 main(argc, argv)
88 int argc;
89 char *argv[];
90 {
91 int all, ch, errs;
92
93 /* Start disks transferring immediately. */
94 sync();
95
96 all = 0;
97 while ((ch = getopt(argc, argv, "aFfh:t:v")) != -1)
98 switch (ch) {
99 case 'a':
100 all = 1;
101 break;
102 case 'F':
103 fake = 1;
104 break;
105 case 'f':
106 fflag = MNT_FORCE;
107 break;
108 case 'h': /* -h implies -a. */
109 all = 1;
110 nfshost = optarg;
111 break;
112 case 't':
113 if (typelist != NULL)
114 errx(1, "only one -t option may be specified.");
115 maketypelist(optarg);
116 break;
117 case 'v':
118 verbose = 1;
119 break;
120 default:
121 usage();
122 /* NOTREACHED */
123 }
124 argc -= optind;
125 argv += optind;
126
127 if ((argc == 0 && !all) || (argc != 0 && all))
128 usage();
129
130 /* -h implies "-t nfs" if no -t flag. */
131 if ((nfshost != NULL) && (typelist == NULL))
132 maketypelist("nfs");
133
134 if (all)
135 errs = umountall();
136 else
137 for (errs = 0; *argv != NULL; ++argv)
138 if (umountfs(*argv) != 0)
139 errs = 1;
140 exit(errs);
141 }
142
143 int
144 umountall()
145 {
146 struct statfs *fs;
147 int n;
148 int rval;
149
150 n = getmntinfo(&fs, MNT_NOWAIT);
151 if (n == 0)
152 err(1, "%s", "");
153
154 rval = 0;
155 while (--n >= 0) {
156 /* Ignore the root. */
157 if (strncmp(fs[n].f_mntonname, "/", MNAMELEN) == 0)
158 continue;
159
160 if (!selected(fs[n].f_fstypename))
161 continue;
162
163 if (umountfs(fs[n].f_mntonname))
164 rval = 1;
165 }
166 return (rval);
167 }
168
169 int
170 umountfs(name)
171 char *name;
172 {
173 enum clnt_stat clnt_stat;
174 struct hostent *hp;
175 struct sockaddr_in saddr;
176 struct stat sb;
177 struct timeval pertry, try;
178 CLIENT *clp;
179 int so;
180 char *delimp, *hostp, *mntpt, rname[MAXPATHLEN], type[MFSNAMELEN];
181 mntwhat what;
182
183 hp = NULL;
184 delimp = NULL;
185 if (realpath(name, rname) == NULL) {
186 warn("%s", rname);
187 return (1);
188 }
189
190 what = MNTANY;
191 mntpt = name = rname;
192
193 if (stat(name, &sb) == 0) {
194 if (S_ISBLK(sb.st_mode))
195 what = MNTON;
196 else if (S_ISDIR(sb.st_mode))
197 what = MNTFROM;
198 }
199
200 switch (what) {
201 case MNTON:
202 if ((mntpt = getmntname(name, MNTON, type)) == NULL) {
203 warnx("%s: not currently mounted", name);
204 return (1);
205 }
206 break;
207 case MNTFROM:
208 if ((name = getmntname(mntpt, MNTFROM, type)) == NULL) {
209 warnx("%s: not currently mounted", mntpt);
210 return (1);
211 }
212 break;
213 default:
214 if ((name = getmntname(mntpt, MNTFROM, type)) == NULL) {
215 name = rname;
216 if ((mntpt = getmntname(name, MNTON, type)) == NULL) {
217 warnx("%s: not currently mounted", name);
218 return (1);
219 }
220 }
221 }
222
223 if (!selected(type))
224 return (1);
225
226 if (!strncmp(type, MOUNT_NFS, MFSNAMELEN)) {
227 if ((delimp = strchr(name, '@')) != NULL) {
228 hostp = delimp + 1;
229 *delimp = '\0';
230 hp = gethostbyname(hostp);
231 *delimp = '@';
232 } else if ((delimp = strchr(name, ':')) != NULL) {
233 *delimp = '\0';
234 hostp = name;
235 hp = gethostbyname(hostp);
236 name = delimp + 1;
237 *delimp = ':';
238 } else
239 hp = NULL;
240 if (!namematch(hp))
241 return (1);
242 }
243
244 if (verbose)
245 (void)printf("%s: unmount from %s\n", name, mntpt);
246 if (fake)
247 return (0);
248
249 if (unmount(mntpt, fflag) < 0) {
250 warn("%s", mntpt);
251 return (1);
252 }
253
254 if (!strncmp(type, MOUNT_NFS, MFSNAMELEN) &&
255 (hp != NULL) && !(fflag & MNT_FORCE)) {
256 *delimp = '\0';
257 memset(&saddr, 0, sizeof(saddr));
258 saddr.sin_family = AF_INET;
259 saddr.sin_port = 0;
260 memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
261 pertry.tv_sec = 3;
262 pertry.tv_usec = 0;
263 so = RPC_ANYSOCK;
264 if ((clp = clntudp_create(&saddr,
265 RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
266 clnt_pcreateerror("Cannot MNT PRC");
267 return (1);
268 }
269 clp->cl_auth = authunix_create_default();
270 try.tv_sec = 20;
271 try.tv_usec = 0;
272 clnt_stat = clnt_call(clp,
273 RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, try);
274 if (clnt_stat != RPC_SUCCESS) {
275 clnt_perror(clp, "Bad MNT RPC");
276 return (1);
277 }
278 auth_destroy(clp->cl_auth);
279 clnt_destroy(clp);
280 }
281 return (0);
282 }
283
284 char *
285 getmntname(name, what, type)
286 char *name;
287 mntwhat what;
288 char *type;
289 {
290 struct statfs *mntbuf;
291 int i, mntsize;
292
293 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
294 warn("getmntinfo");
295 return (NULL);
296 }
297 for (i = 0; i < mntsize; i++) {
298 if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
299 if (type)
300 memcpy(type, mntbuf[i].f_fstypename,
301 sizeof(mntbuf[i].f_fstypename));
302 return (mntbuf[i].f_mntonname);
303 }
304 if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
305 if (type)
306 memcpy(type, mntbuf[i].f_fstypename,
307 sizeof(mntbuf[i].f_fstypename));
308 return (mntbuf[i].f_mntfromname);
309 }
310 }
311 return (NULL);
312 }
313
314 static enum { IN_LIST, NOT_IN_LIST } which;
315
316 int
317 selected(type)
318 const char *type;
319 {
320 char **av;
321
322 /* If no type specified, it's always selected. */
323 if (typelist == NULL)
324 return (1);
325 for (av = typelist; *av != NULL; ++av)
326 if (!strncmp(type, *av, MFSNAMELEN))
327 return (which == IN_LIST ? 1 : 0);
328 return (which == IN_LIST ? 0 : 1);
329 }
330
331 void
332 maketypelist(fslist)
333 char *fslist;
334 {
335 int i;
336 char *nextcp, **av;
337
338 if ((fslist == NULL) || (fslist[0] == '\0'))
339 errx(1, "empty type list");
340
341 /*
342 * XXX
343 * Note: the syntax is "noxxx,yyy" for no xxx's and
344 * no yyy's, not the more intuitive "noyyy,noyyy".
345 */
346 if (fslist[0] == 'n' && fslist[1] == 'o') {
347 fslist += 2;
348 which = NOT_IN_LIST;
349 } else
350 which = IN_LIST;
351
352 /* Count the number of types. */
353 for (i = 1, nextcp = fslist;
354 (nextcp = strchr(nextcp, ',')) != NULL;
355 i++)
356 ++nextcp;
357
358 /* Build an array of that many types. */
359 if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
360 err(1, "%s", "");
361 av[0] = fslist;
362 for (i = 1, nextcp = fslist;
363 (nextcp = strchr(nextcp, ',')) != NULL;
364 i++) {
365 *nextcp = '\0';
366 av[i] = ++nextcp;
367 }
368 /* Terminate the array. */
369 av[i] = NULL;
370 }
371
372 int
373 namematch(hp)
374 struct hostent *hp;
375 {
376 char *cp, **np;
377
378 if ((hp == NULL) || (nfshost == NULL))
379 return (1);
380
381 if (strcasecmp(nfshost, hp->h_name) == 0)
382 return (1);
383
384 if ((cp = strchr(hp->h_name, '.')) != NULL) {
385 *cp = '\0';
386 if (strcasecmp(nfshost, hp->h_name) == 0)
387 return (1);
388 }
389 for (np = hp->h_aliases; *np; np++) {
390 if (strcasecmp(nfshost, *np) == 0)
391 return (1);
392 if ((cp = strchr(*np, '.')) != NULL) {
393 *cp = '\0';
394 if (strcasecmp(nfshost, *np) == 0)
395 return (1);
396 }
397 }
398 return (0);
399 }
400
401 /*
402 * xdr routines for mount rpc's
403 */
404 int
405 xdr_dir(xdrsp, dirp)
406 XDR *xdrsp;
407 char *dirp;
408 {
409 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
410 }
411
412 void
413 usage()
414 {
415 (void)fprintf(stderr,
416 "usage: %s\n %s\n",
417 "umount [-fv] [-t fstypelist] special | node",
418 "umount -a[fv] [-h host] [-t fstypelist]");
419 exit(1);
420 }
421