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