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