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