umount.c revision 1.1.1.3 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.3 lukem static char sccsid[] = "@(#)umount.c 8.8 (Berkeley) 5/8/95";
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.3 lukem int fake, fflag, vflag;
67 1.1.1.2 mycroft char *nfshost;
68 1.1 cgd
69 1.1.1.3 lukem int checkvfsname __P((const char *, char **));
70 1.1.1.3 lukem char *getmntname __P((char *, mntwhat, char **));
71 1.1.1.3 lukem char **makevfslist __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.3 lukem int umountall __P((char **));
75 1.1.1.3 lukem int umountfs __P((char *, 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.3 lukem int all, ch, errs, mnts;
85 1.1.1.3 lukem char **typelist = NULL;
86 1.1.1.3 lukem struct statfs *mntbuf;
87 1.1 cgd
88 1.1.1.2 mycroft /* Start disks transferring immediately. */
89 1.1 cgd sync();
90 1.1.1.2 mycroft
91 1.1.1.2 mycroft all = 0;
92 1.1.1.3 lukem while ((ch = getopt(argc, argv, "AaFfh:t:v")) != EOF)
93 1.1.1.2 mycroft switch (ch) {
94 1.1.1.3 lukem case 'A':
95 1.1.1.3 lukem all = 2;
96 1.1.1.3 lukem break;
97 1.1.1.2 mycroft case 'a':
98 1.1.1.2 mycroft all = 1;
99 1.1.1.2 mycroft break;
100 1.1.1.2 mycroft case 'F':
101 1.1.1.2 mycroft fake = 1;
102 1.1 cgd break;
103 1.1 cgd case 'f':
104 1.1 cgd fflag = MNT_FORCE;
105 1.1 cgd break;
106 1.1.1.3 lukem case 'h': /* -h implies -A. */
107 1.1.1.3 lukem all = 2;
108 1.1.1.2 mycroft nfshost = optarg;
109 1.1 cgd break;
110 1.1 cgd case 't':
111 1.1.1.3 lukem if (typelist != NULL)
112 1.1.1.3 lukem errx(1, "only one -t option may be specified.");
113 1.1.1.3 lukem typelist = makevfslist(optarg);
114 1.1 cgd break;
115 1.1.1.2 mycroft case 'v':
116 1.1.1.2 mycroft vflag = 1;
117 1.1 cgd break;
118 1.1 cgd default:
119 1.1 cgd usage();
120 1.1 cgd /* NOTREACHED */
121 1.1 cgd }
122 1.1 cgd argc -= optind;
123 1.1 cgd argv += optind;
124 1.1 cgd
125 1.1.1.2 mycroft if (argc == 0 && !all || argc != 0 && all)
126 1.1 cgd usage();
127 1.1.1.2 mycroft
128 1.1.1.2 mycroft /* -h implies "-t nfs" if no -t flag. */
129 1.1.1.2 mycroft if ((nfshost != NULL) && (typelist == NULL))
130 1.1.1.3 lukem typelist = makevfslist("nfs");
131 1.1.1.2 mycroft
132 1.1.1.3 lukem switch (all) {
133 1.1.1.3 lukem case 2:
134 1.1.1.3 lukem if ((mnts = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
135 1.1.1.3 lukem warn("getmntinfo");
136 1.1.1.3 lukem errs = 1;
137 1.1.1.3 lukem break;
138 1.1.1.3 lukem }
139 1.1.1.3 lukem for (errs = 0, mnts--; mnts > 0; mnts--) {
140 1.1.1.3 lukem if (checkvfsname(mntbuf[mnts].f_fstypename, typelist))
141 1.1.1.3 lukem continue;
142 1.1.1.3 lukem if (umountfs(mntbuf[mnts].f_mntonname, typelist) != 0)
143 1.1.1.3 lukem errs = 1;
144 1.1.1.3 lukem }
145 1.1.1.3 lukem break;
146 1.1.1.3 lukem case 1:
147 1.1 cgd if (setfsent() == 0)
148 1.1.1.2 mycroft err(1, "%s", _PATH_FSTAB);
149 1.1.1.3 lukem errs = umountall(typelist);
150 1.1.1.3 lukem break;
151 1.1.1.3 lukem case 0:
152 1.1.1.2 mycroft for (errs = 0; *argv != NULL; ++argv)
153 1.1.1.3 lukem if (umountfs(*argv, typelist) != 0)
154 1.1.1.2 mycroft errs = 1;
155 1.1.1.3 lukem break;
156 1.1.1.3 lukem }
157 1.1 cgd exit(errs);
158 1.1 cgd }
159 1.1 cgd
160 1.1.1.2 mycroft int
161 1.1.1.3 lukem umountall(typelist)
162 1.1.1.3 lukem char **typelist;
163 1.1 cgd {
164 1.1.1.2 mycroft struct fstab *fs;
165 1.1.1.2 mycroft int rval, type;
166 1.1.1.2 mycroft char *cp;
167 1.1.1.3 lukem struct vfsconf vfc;
168 1.1.1.2 mycroft
169 1.1.1.2 mycroft while ((fs = getfsent()) != NULL) {
170 1.1.1.2 mycroft /* Ignore the root. */
171 1.1.1.2 mycroft if (strcmp(fs->fs_file, "/") == 0)
172 1.1.1.2 mycroft continue;
173 1.1.1.2 mycroft /*
174 1.1.1.2 mycroft * !!!
175 1.1.1.2 mycroft * Historic practice: ignore unknown FSTAB_* fields.
176 1.1.1.2 mycroft */
177 1.1.1.2 mycroft if (strcmp(fs->fs_type, FSTAB_RW) &&
178 1.1.1.2 mycroft strcmp(fs->fs_type, FSTAB_RO) &&
179 1.1.1.2 mycroft strcmp(fs->fs_type, FSTAB_RQ))
180 1.1.1.2 mycroft continue;
181 1.1.1.2 mycroft /* If an unknown file system type, complain. */
182 1.1.1.3 lukem if (getvfsbyname(fs->fs_vfstype, &vfc) < 0) {
183 1.1.1.2 mycroft warnx("%s: unknown mount type", fs->fs_vfstype);
184 1.1.1.2 mycroft continue;
185 1.1.1.2 mycroft }
186 1.1.1.3 lukem if (checkvfsname(fs->fs_vfstype, typelist))
187 1.1.1.2 mycroft continue;
188 1.1 cgd
189 1.1.1.2 mycroft /*
190 1.1.1.2 mycroft * We want to unmount the file systems in the reverse order
191 1.1.1.2 mycroft * that they were mounted. So, we save off the file name
192 1.1.1.2 mycroft * in some allocated memory, and then call recursively.
193 1.1.1.2 mycroft */
194 1.1.1.2 mycroft if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
195 1.1.1.2 mycroft err(1, NULL);
196 1.1.1.2 mycroft (void)strcpy(cp, fs->fs_file);
197 1.1.1.3 lukem rval = umountall(typelist);
198 1.1.1.3 lukem return (umountfs(cp, typelist) || rval);
199 1.1 cgd }
200 1.1.1.2 mycroft return (0);
201 1.1 cgd }
202 1.1 cgd
203 1.1.1.2 mycroft int
204 1.1.1.3 lukem umountfs(name, typelist)
205 1.1 cgd char *name;
206 1.1.1.3 lukem char **typelist;
207 1.1 cgd {
208 1.1.1.2 mycroft enum clnt_stat clnt_stat;
209 1.1.1.2 mycroft struct hostent *hp;
210 1.1 cgd struct sockaddr_in saddr;
211 1.1.1.2 mycroft struct stat sb;
212 1.1 cgd struct timeval pertry, try;
213 1.1.1.2 mycroft CLIENT *clp;
214 1.1.1.3 lukem int so;
215 1.1.1.3 lukem char *type, *delimp, *hostp, *mntpt, rname[MAXPATHLEN];
216 1.1.1.2 mycroft
217 1.1.1.2 mycroft if (realpath(name, rname) == NULL) {
218 1.1.1.2 mycroft warn("%s", rname);
219 1.1.1.3 lukem return (1);
220 1.1.1.2 mycroft }
221 1.1.1.2 mycroft
222 1.1.1.2 mycroft name = rname;
223 1.1.1.2 mycroft
224 1.1.1.2 mycroft if (stat(name, &sb) < 0) {
225 1.1.1.2 mycroft if (((mntpt = getmntname(name, MNTFROM, &type)) == NULL) &&
226 1.1.1.2 mycroft ((mntpt = getmntname(name, MNTON, &type)) == NULL)) {
227 1.1.1.2 mycroft warnx("%s: not currently mounted", name);
228 1.1.1.2 mycroft return (1);
229 1.1 cgd }
230 1.1.1.2 mycroft } else if (S_ISBLK(sb.st_mode)) {
231 1.1.1.2 mycroft if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
232 1.1.1.2 mycroft warnx("%s: not currently mounted", name);
233 1.1.1.2 mycroft return (1);
234 1.1 cgd }
235 1.1.1.2 mycroft } else if (S_ISDIR(sb.st_mode)) {
236 1.1 cgd mntpt = name;
237 1.1.1.2 mycroft if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
238 1.1.1.2 mycroft warnx("%s: not currently mounted", mntpt);
239 1.1.1.2 mycroft return (1);
240 1.1 cgd }
241 1.1 cgd } else {
242 1.1.1.2 mycroft warnx("%s: not a directory or special device", name);
243 1.1.1.2 mycroft return (1);
244 1.1 cgd }
245 1.1 cgd
246 1.1.1.3 lukem if (checkvfsname(type, typelist))
247 1.1.1.3 lukem return (1);
248 1.1.1.3 lukem
249 1.1.1.3 lukem hp = NULL;
250 1.1.1.3 lukem if (!strcmp(type, "nfs")) {
251 1.1.1.3 lukem if ((delimp = strchr(name, '@')) != NULL) {
252 1.1.1.3 lukem hostp = delimp + 1;
253 1.1.1.3 lukem *delimp = '\0';
254 1.1.1.3 lukem hp = gethostbyname(hostp);
255 1.1.1.3 lukem *delimp = '@';
256 1.1.1.3 lukem } else if ((delimp = strchr(name, ':')) != NULL) {
257 1.1.1.3 lukem *delimp = '\0';
258 1.1.1.3 lukem hostp = name;
259 1.1.1.3 lukem hp = gethostbyname(hostp);
260 1.1.1.3 lukem name = delimp + 1;
261 1.1.1.3 lukem *delimp = ':';
262 1.1.1.3 lukem }
263 1.1.1.3 lukem }
264 1.1.1.2 mycroft
265 1.1.1.2 mycroft if (!namematch(hp))
266 1.1.1.3 lukem return (1);
267 1.1 cgd
268 1.1.1.2 mycroft if (vflag)
269 1.1.1.2 mycroft (void)printf("%s: unmount from %s\n", name, mntpt);
270 1.1.1.2 mycroft if (fake)
271 1.1 cgd return (0);
272 1.1.1.2 mycroft
273 1.1.1.2 mycroft if (unmount(mntpt, fflag) < 0) {
274 1.1.1.2 mycroft warn("%s", mntpt);
275 1.1.1.2 mycroft return (1);
276 1.1 cgd }
277 1.1 cgd
278 1.1.1.2 mycroft if ((hp != NULL) && !(fflag & MNT_FORCE)) {
279 1.1 cgd *delimp = '\0';
280 1.1.1.2 mycroft memset(&saddr, 0, sizeof(saddr));
281 1.1 cgd saddr.sin_family = AF_INET;
282 1.1 cgd saddr.sin_port = 0;
283 1.1.1.2 mycroft memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
284 1.1 cgd pertry.tv_sec = 3;
285 1.1 cgd pertry.tv_usec = 0;
286 1.1.1.2 mycroft so = RPC_ANYSOCK;
287 1.1.1.2 mycroft if ((clp = clntudp_create(&saddr,
288 1.1.1.2 mycroft RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
289 1.1 cgd clnt_pcreateerror("Cannot MNT PRC");
290 1.1 cgd return (1);
291 1.1 cgd }
292 1.1 cgd clp->cl_auth = authunix_create_default();
293 1.1 cgd try.tv_sec = 20;
294 1.1 cgd try.tv_usec = 0;
295 1.1.1.2 mycroft clnt_stat = clnt_call(clp,
296 1.1.1.2 mycroft RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, try);
297 1.1 cgd if (clnt_stat != RPC_SUCCESS) {
298 1.1 cgd clnt_perror(clp, "Bad MNT RPC");
299 1.1 cgd return (1);
300 1.1 cgd }
301 1.1 cgd auth_destroy(clp->cl_auth);
302 1.1 cgd clnt_destroy(clp);
303 1.1 cgd }
304 1.1.1.2 mycroft return (0);
305 1.1 cgd }
306 1.1 cgd
307 1.1 cgd char *
308 1.1 cgd getmntname(name, what, type)
309 1.1 cgd char *name;
310 1.1.1.2 mycroft mntwhat what;
311 1.1.1.3 lukem char **type;
312 1.1 cgd {
313 1.1.1.3 lukem static struct statfs *mntbuf;
314 1.1.1.3 lukem static int mntsize;
315 1.1.1.3 lukem int i;
316 1.1 cgd
317 1.1.1.3 lukem if (mntbuf == NULL &&
318 1.1.1.3 lukem (mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
319 1.1.1.2 mycroft warn("getmntinfo");
320 1.1.1.2 mycroft return (NULL);
321 1.1 cgd }
322 1.1 cgd for (i = 0; i < mntsize; i++) {
323 1.1.1.2 mycroft if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
324 1.1 cgd if (type)
325 1.1.1.3 lukem *type = mntbuf[i].f_fstypename;
326 1.1 cgd return (mntbuf[i].f_mntonname);
327 1.1 cgd }
328 1.1.1.2 mycroft if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
329 1.1 cgd if (type)
330 1.1.1.3 lukem *type = mntbuf[i].f_fstypename;
331 1.1 cgd return (mntbuf[i].f_mntfromname);
332 1.1 cgd }
333 1.1 cgd }
334 1.1.1.2 mycroft return (NULL);
335 1.1 cgd }
336 1.1 cgd
337 1.1.1.2 mycroft int
338 1.1.1.2 mycroft namematch(hp)
339 1.1 cgd struct hostent *hp;
340 1.1 cgd {
341 1.1.1.2 mycroft char *cp, **np;
342 1.1.1.2 mycroft
343 1.1.1.2 mycroft if ((hp == NULL) || (nfshost == NULL))
344 1.1.1.2 mycroft return (1);
345 1.1 cgd
346 1.1 cgd if (strcasecmp(nfshost, hp->h_name) == 0)
347 1.1.1.2 mycroft return (1);
348 1.1.1.2 mycroft
349 1.1.1.2 mycroft if ((cp = strchr(hp->h_name, '.')) != NULL) {
350 1.1 cgd *cp = '\0';
351 1.1 cgd if (strcasecmp(nfshost, hp->h_name) == 0)
352 1.1.1.2 mycroft return (1);
353 1.1 cgd }
354 1.1 cgd for (np = hp->h_aliases; *np; np++) {
355 1.1 cgd if (strcasecmp(nfshost, *np) == 0)
356 1.1.1.2 mycroft return (1);
357 1.1.1.2 mycroft if ((cp = strchr(*np, '.')) != NULL) {
358 1.1 cgd *cp = '\0';
359 1.1 cgd if (strcasecmp(nfshost, *np) == 0)
360 1.1.1.2 mycroft return (1);
361 1.1 cgd }
362 1.1 cgd }
363 1.1.1.2 mycroft return (0);
364 1.1 cgd }
365 1.1 cgd
366 1.1 cgd /*
367 1.1 cgd * xdr routines for mount rpc's
368 1.1 cgd */
369 1.1.1.2 mycroft int
370 1.1 cgd xdr_dir(xdrsp, dirp)
371 1.1 cgd XDR *xdrsp;
372 1.1 cgd char *dirp;
373 1.1 cgd {
374 1.1 cgd return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
375 1.1 cgd }
376 1.1.1.2 mycroft
377 1.1.1.2 mycroft void
378 1.1.1.2 mycroft usage()
379 1.1.1.2 mycroft {
380 1.1.1.2 mycroft (void)fprintf(stderr,
381 1.1.1.2 mycroft "usage: %s\n %s\n",
382 1.1.1.2 mycroft "umount [-fv] [-t fstypelist] special | node",
383 1.1.1.2 mycroft "umount -a[fv] [-h host] [-t fstypelist]");
384 1.1.1.2 mycroft exit(1);
385 1.1.1.2 mycroft }
386