1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * -----------------------------------------------------------------
24*0Sstevel@tonic-gate *
25*0Sstevel@tonic-gate * umount.c
26*0Sstevel@tonic-gate *
27*0Sstevel@tonic-gate * CFS specific umount command.
28*0Sstevel@tonic-gate */
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate /*
33*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
34*0Sstevel@tonic-gate * Use is subject to license terms.
35*0Sstevel@tonic-gate */
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate #include <locale.h>
38*0Sstevel@tonic-gate #include <stdio.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <string.h>
41*0Sstevel@tonic-gate #include <stdarg.h>
42*0Sstevel@tonic-gate #include <unistd.h>
43*0Sstevel@tonic-gate #include <limits.h>
44*0Sstevel@tonic-gate #include <errno.h>
45*0Sstevel@tonic-gate #include <wait.h>
46*0Sstevel@tonic-gate #include <ctype.h>
47*0Sstevel@tonic-gate #include <fcntl.h>
48*0Sstevel@tonic-gate #include <signal.h>
49*0Sstevel@tonic-gate #include <sys/types.h>
50*0Sstevel@tonic-gate #include <sys/param.h>
51*0Sstevel@tonic-gate #include <sys/stat.h>
52*0Sstevel@tonic-gate #include <sys/fcntl.h>
53*0Sstevel@tonic-gate #include <sys/mount.h>
54*0Sstevel@tonic-gate #include <sys/mntent.h>
55*0Sstevel@tonic-gate #include <sys/mnttab.h>
56*0Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
57*0Sstevel@tonic-gate #include <fslib.h>
58*0Sstevel@tonic-gate #include <sys/utsname.h>
59*0Sstevel@tonic-gate #include <rpc/rpc.h>
60*0Sstevel@tonic-gate #include "../common/subr.h"
61*0Sstevel@tonic-gate #include "../common/cachefsd.h"
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate /* forward references */
64*0Sstevel@tonic-gate void pr_err(char *fmt, ...);
65*0Sstevel@tonic-gate void usage(char *msgp);
66*0Sstevel@tonic-gate int daemon_unmount(char *mntptp, int);
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate *
70*0Sstevel@tonic-gate * main
71*0Sstevel@tonic-gate *
72*0Sstevel@tonic-gate * Description:
73*0Sstevel@tonic-gate * Main routine for the cfs umount program.
74*0Sstevel@tonic-gate * Arguments:
75*0Sstevel@tonic-gate * argc number of command line arguments
76*0Sstevel@tonic-gate * argv list of command line arguments
77*0Sstevel@tonic-gate * Returns:
78*0Sstevel@tonic-gate * Returns 0 for success or 1 if an error occurs.
79*0Sstevel@tonic-gate * Preconditions:
80*0Sstevel@tonic-gate */
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate int
main(int argc,char ** argv)83*0Sstevel@tonic-gate main(int argc, char **argv)
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate char *strp;
86*0Sstevel@tonic-gate int xx;
87*0Sstevel@tonic-gate int ret;
88*0Sstevel@tonic-gate struct mnttab mget;
89*0Sstevel@tonic-gate struct mnttab mref;
90*0Sstevel@tonic-gate FILE *finp;
91*0Sstevel@tonic-gate char mnt_front[PATH_MAX];
92*0Sstevel@tonic-gate char mnt_back[PATH_MAX];
93*0Sstevel@tonic-gate char *p, mnt_frontns[PATH_MAX];
94*0Sstevel@tonic-gate pid_t pid;
95*0Sstevel@tonic-gate int stat_loc;
96*0Sstevel@tonic-gate char *cachedirp, *s;
97*0Sstevel@tonic-gate int ufd, c;
98*0Sstevel@tonic-gate int flag = 0;
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
101*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
102*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
103*0Sstevel@tonic-gate #endif
104*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate /*
107*0Sstevel@tonic-gate * Set options
108*0Sstevel@tonic-gate */
109*0Sstevel@tonic-gate while ((c = getopt(argc, argv, "f")) != EOF) {
110*0Sstevel@tonic-gate switch (c) {
111*0Sstevel@tonic-gate case 'f':
112*0Sstevel@tonic-gate flag |= MS_FORCE; /* forced unmount is desired */
113*0Sstevel@tonic-gate break;
114*0Sstevel@tonic-gate default:
115*0Sstevel@tonic-gate usage("Incorrect number of arguments specified");
116*0Sstevel@tonic-gate return (1);
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate if (flag & MS_FORCE)
120*0Sstevel@tonic-gate strcpy(mnt_front, argv[2]);
121*0Sstevel@tonic-gate else
122*0Sstevel@tonic-gate strcpy(mnt_front, argv[1]);
123*0Sstevel@tonic-gate mnt_back[0] = '\0';
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate if (strlen(argv[1]) >= (size_t)PATH_MAX) {
126*0Sstevel@tonic-gate pr_err(gettext("name too long"));
127*0Sstevel@tonic-gate return (1);
128*0Sstevel@tonic-gate }
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate /*
131*0Sstevel@tonic-gate * An unmount from autofs may have a
132*0Sstevel@tonic-gate * space appended to the path.
133*0Sstevel@tonic-gate * Trim it off before attempting to
134*0Sstevel@tonic-gate * match the mountpoint in mnttab
135*0Sstevel@tonic-gate */
136*0Sstevel@tonic-gate strcpy(mnt_frontns, mnt_front);
137*0Sstevel@tonic-gate p = &mnt_frontns[strlen(mnt_frontns) - 1];
138*0Sstevel@tonic-gate if (*p == ' ')
139*0Sstevel@tonic-gate *p = '\0';
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate /* get the mount point and the back file system mount point */
142*0Sstevel@tonic-gate finp = fopen(MNTTAB, "r");
143*0Sstevel@tonic-gate if (finp) {
144*0Sstevel@tonic-gate mntnull(&mref);
145*0Sstevel@tonic-gate mref.mnt_mountp = mnt_frontns;
146*0Sstevel@tonic-gate mref.mnt_fstype = "cachefs";
147*0Sstevel@tonic-gate ret = getmntany(finp, &mget, &mref);
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate cachedirp = (char *)malloc(strlen(mget.mnt_special) + 1);
150*0Sstevel@tonic-gate strcpy(cachedirp, mget.mnt_special);
151*0Sstevel@tonic-gate /*
152*0Sstevel@tonic-gate * AutoClient mounts do not have .cfs_mnt_points string in
153*0Sstevel@tonic-gate * them, so strstr would return NULL. So .cfs_unmnt file
154*0Sstevel@tonic-gate * is not updated.
155*0Sstevel@tonic-gate */
156*0Sstevel@tonic-gate if ((s = strstr(cachedirp, ".cfs_mnt_points")) != NULL) {
157*0Sstevel@tonic-gate time32_t btime;
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate xx = -1;
160*0Sstevel@tonic-gate cachedirp[s-cachedirp] = '\0';
161*0Sstevel@tonic-gate strcat(cachedirp, CACHEFS_UNMNT_FILE);
162*0Sstevel@tonic-gate if ((ufd = open(cachedirp, O_WRONLY|O_CREAT|O_TRUNC,
163*0Sstevel@tonic-gate 0600)) > 0) {
164*0Sstevel@tonic-gate if ((btime = get_boottime()) != -1)
165*0Sstevel@tonic-gate xx = write(ufd, &btime, sizeof (time32_t));
166*0Sstevel@tonic-gate close(ufd);
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate if (xx < 0)
169*0Sstevel@tonic-gate pr_err(gettext(".cfs_unmnt error"));
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate
172*0Sstevel@tonic-gate if (ret != -1) {
173*0Sstevel@tonic-gate if (mget.mnt_special)
174*0Sstevel@tonic-gate strcpy(mnt_back, mget.mnt_special);
175*0Sstevel@tonic-gate } else {
176*0Sstevel@tonic-gate mref.mnt_special = mref.mnt_mountp;
177*0Sstevel@tonic-gate mref.mnt_mountp = NULL;
178*0Sstevel@tonic-gate rewind(finp);
179*0Sstevel@tonic-gate ret = getmntany(finp, &mget, &mref);
180*0Sstevel@tonic-gate if (ret != -1) {
181*0Sstevel@tonic-gate strcpy(mnt_front, mget.mnt_mountp);
182*0Sstevel@tonic-gate if (mget.mnt_special)
183*0Sstevel@tonic-gate strcpy(mnt_back, mget.mnt_special);
184*0Sstevel@tonic-gate } else {
185*0Sstevel@tonic-gate pr_err(gettext("warning: %s not in mnttab"),
186*0Sstevel@tonic-gate mref.mnt_special);
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate fclose(finp);
190*0Sstevel@tonic-gate }
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate /* try to get the daemon to unmount this file system for us */
193*0Sstevel@tonic-gate xx = daemon_unmount(mnt_front, flag);
194*0Sstevel@tonic-gate if (xx == ENOTSUP)
195*0Sstevel@tonic-gate return (1);
196*0Sstevel@tonic-gate if (xx == EBUSY) {
197*0Sstevel@tonic-gate pr_err(gettext("%s %s"), mnt_front, strerror(xx));
198*0Sstevel@tonic-gate return (1);
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate if (xx == EIO) {
201*0Sstevel@tonic-gate /* try to unmount the file system directly */
202*0Sstevel@tonic-gate if (umount2(mnt_front, flag) == -1) {
203*0Sstevel@tonic-gate pr_err(gettext("%s %s"), mnt_front, strerror(errno));
204*0Sstevel@tonic-gate return (1);
205*0Sstevel@tonic-gate }
206*0Sstevel@tonic-gate }
207*0Sstevel@tonic-gate
208*0Sstevel@tonic-gate /* if we do not know the name of the back file system mount point */
209*0Sstevel@tonic-gate if (mnt_back[0] == '\0') {
210*0Sstevel@tonic-gate /* all done */
211*0Sstevel@tonic-gate return (0);
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gate /*
215*0Sstevel@tonic-gate * If the back file system was mounted on a directory with a
216*0Sstevel@tonic-gate * parent name of BACKMNT_NAME then we assume that we
217*0Sstevel@tonic-gate * mounted it and that it is okay to try to umount it.
218*0Sstevel@tonic-gate */
219*0Sstevel@tonic-gate if (strstr(mnt_back, BACKMNT_NAME) == NULL)
220*0Sstevel@tonic-gate return (0);
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate /* if no back file system mounted */
223*0Sstevel@tonic-gate if (strcmp(mnt_back, "nobackfs") == 0)
224*0Sstevel@tonic-gate return (0);
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate /* fork */
227*0Sstevel@tonic-gate if ((pid = fork()) == -1) {
228*0Sstevel@tonic-gate pr_err(gettext("could not fork %s"), strerror(errno));
229*0Sstevel@tonic-gate return (1);
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gate /* if the child */
233*0Sstevel@tonic-gate if (pid == 0) {
234*0Sstevel@tonic-gate /* invoke the umount command on the back file system */
235*0Sstevel@tonic-gate xx = execl("/sbin/umount", "/sbin/umount", mnt_back, NULL);
236*0Sstevel@tonic-gate pr_err(gettext("could not exec /sbin/umount"
237*0Sstevel@tonic-gate " on back file system %s"), strerror(errno));
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate
240*0Sstevel@tonic-gate /* else if the parent */
241*0Sstevel@tonic-gate else {
242*0Sstevel@tonic-gate /* wait for the child to exit */
243*0Sstevel@tonic-gate if (wait(&stat_loc) == -1) {
244*0Sstevel@tonic-gate pr_err(gettext("wait failed %s"), strerror(errno));
245*0Sstevel@tonic-gate return (1);
246*0Sstevel@tonic-gate }
247*0Sstevel@tonic-gate
248*0Sstevel@tonic-gate if (!WIFEXITED(stat_loc)) {
249*0Sstevel@tonic-gate pr_err(gettext("back umount did not exit"));
250*0Sstevel@tonic-gate return (1);
251*0Sstevel@tonic-gate }
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gate xx = WEXITSTATUS(stat_loc);
254*0Sstevel@tonic-gate if (xx) {
255*0Sstevel@tonic-gate pr_err(gettext("back umount failed"));
256*0Sstevel@tonic-gate return (xx);
257*0Sstevel@tonic-gate }
258*0Sstevel@tonic-gate }
259*0Sstevel@tonic-gate
260*0Sstevel@tonic-gate /* delete the back file system mount point since we created it */
261*0Sstevel@tonic-gate rmdir(mnt_back);
262*0Sstevel@tonic-gate
263*0Sstevel@tonic-gate return (xx);
264*0Sstevel@tonic-gate }
265*0Sstevel@tonic-gate
266*0Sstevel@tonic-gate /*
267*0Sstevel@tonic-gate *
268*0Sstevel@tonic-gate * pr_err
269*0Sstevel@tonic-gate *
270*0Sstevel@tonic-gate * Description:
271*0Sstevel@tonic-gate * Prints an error message to stderr.
272*0Sstevel@tonic-gate * Arguments:
273*0Sstevel@tonic-gate * fmt printf style format
274*0Sstevel@tonic-gate * ... arguments for fmt
275*0Sstevel@tonic-gate * Returns:
276*0Sstevel@tonic-gate * Preconditions:
277*0Sstevel@tonic-gate * precond(fmt)
278*0Sstevel@tonic-gate */
279*0Sstevel@tonic-gate
280*0Sstevel@tonic-gate void
pr_err(char * fmt,...)281*0Sstevel@tonic-gate pr_err(char *fmt, ...)
282*0Sstevel@tonic-gate {
283*0Sstevel@tonic-gate va_list ap;
284*0Sstevel@tonic-gate
285*0Sstevel@tonic-gate va_start(ap, fmt);
286*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("umount -F cachefs: "));
287*0Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap);
288*0Sstevel@tonic-gate (void) fprintf(stderr, "\n");
289*0Sstevel@tonic-gate va_end(ap);
290*0Sstevel@tonic-gate }
291*0Sstevel@tonic-gate
292*0Sstevel@tonic-gate /*
293*0Sstevel@tonic-gate *
294*0Sstevel@tonic-gate * usage
295*0Sstevel@tonic-gate *
296*0Sstevel@tonic-gate * Description:
297*0Sstevel@tonic-gate * Prints a usage message.
298*0Sstevel@tonic-gate * Arguments:
299*0Sstevel@tonic-gate * An optional additional message to be displayed.
300*0Sstevel@tonic-gate * Returns:
301*0Sstevel@tonic-gate * Preconditions:
302*0Sstevel@tonic-gate */
303*0Sstevel@tonic-gate
304*0Sstevel@tonic-gate void
usage(char * msgp)305*0Sstevel@tonic-gate usage(char *msgp)
306*0Sstevel@tonic-gate {
307*0Sstevel@tonic-gate if (msgp)
308*0Sstevel@tonic-gate pr_err(gettext("%s"), msgp);
309*0Sstevel@tonic-gate fprintf(stderr, gettext("Usage: umount -F cachefs dir"));
310*0Sstevel@tonic-gate }
311*0Sstevel@tonic-gate
312*0Sstevel@tonic-gate /*
313*0Sstevel@tonic-gate *
314*0Sstevel@tonic-gate * daemon_unmount
315*0Sstevel@tonic-gate *
316*0Sstevel@tonic-gate * Description:
317*0Sstevel@tonic-gate * Notifies the cachefsd of an unmount request.
318*0Sstevel@tonic-gate * Arguments:
319*0Sstevel@tonic-gate * Mount point to unmount.
320*0Sstevel@tonic-gate * Returns:
321*0Sstevel@tonic-gate * Returns 0 if the cachefsd unmounted the file system
322*0Sstevel@tonic-gate * EIO if should try unmount directly
323*0Sstevel@tonic-gate * EBUSY if did not unmount because busy
324*0Sstevel@tonic-gate * EAGAIN if umounted but should not unmount nfs mount
325*0Sstevel@tonic-gate *
326*0Sstevel@tonic-gate * Preconditions:
327*0Sstevel@tonic-gate * precond(mntptp)
328*0Sstevel@tonic-gate */
329*0Sstevel@tonic-gate
330*0Sstevel@tonic-gate int
daemon_unmount(char * mntptp,int flag)331*0Sstevel@tonic-gate daemon_unmount(char *mntptp, int flag)
332*0Sstevel@tonic-gate {
333*0Sstevel@tonic-gate CLIENT *clnt;
334*0Sstevel@tonic-gate enum clnt_stat retval;
335*0Sstevel@tonic-gate int xx;
336*0Sstevel@tonic-gate int result;
337*0Sstevel@tonic-gate char *hostp;
338*0Sstevel@tonic-gate struct utsname info;
339*0Sstevel@tonic-gate static struct timeval TIMEOUT = { 60*60, 0 };
340*0Sstevel@tonic-gate static struct timeval create_timeout = { 5, 0};
341*0Sstevel@tonic-gate struct cachefsd_fs_unmounted uargs;
342*0Sstevel@tonic-gate
343*0Sstevel@tonic-gate /* get the host name */
344*0Sstevel@tonic-gate xx = uname(&info);
345*0Sstevel@tonic-gate if (xx == -1) {
346*0Sstevel@tonic-gate pr_err(gettext("cannot get host name, errno %d"), errno);
347*0Sstevel@tonic-gate return (EIO);
348*0Sstevel@tonic-gate }
349*0Sstevel@tonic-gate hostp = info.nodename;
350*0Sstevel@tonic-gate uargs.mntpt = mntptp;
351*0Sstevel@tonic-gate uargs.flag = flag;
352*0Sstevel@tonic-gate
353*0Sstevel@tonic-gate /* creat the connection to the daemon */
354*0Sstevel@tonic-gate clnt = clnt_create_timed(hostp, CACHEFSDPROG, CACHEFSDVERS, "local",
355*0Sstevel@tonic-gate &create_timeout);
356*0Sstevel@tonic-gate if (clnt == NULL) {
357*0Sstevel@tonic-gate pr_err(gettext("cachefsd is not running"));
358*0Sstevel@tonic-gate return (EIO);
359*0Sstevel@tonic-gate }
360*0Sstevel@tonic-gate clnt_control(clnt, CLSET_TIMEOUT, (char *)&TIMEOUT);
361*0Sstevel@tonic-gate
362*0Sstevel@tonic-gate retval = cachefsd_fs_unmounted_1(&uargs, &result, clnt);
363*0Sstevel@tonic-gate if (retval != RPC_SUCCESS) {
364*0Sstevel@tonic-gate clnt_perror(clnt, gettext("cachefsd is not responding"));
365*0Sstevel@tonic-gate clnt_destroy(clnt);
366*0Sstevel@tonic-gate return (EIO);
367*0Sstevel@tonic-gate }
368*0Sstevel@tonic-gate
369*0Sstevel@tonic-gate clnt_destroy(clnt);
370*0Sstevel@tonic-gate
371*0Sstevel@tonic-gate return (result);
372*0Sstevel@tonic-gate }
373