1 | /* |
---|
2 | * $Id: unmount.c,v 1.8 1993-05-05 17:05:21 vrt Exp $ |
---|
3 | * |
---|
4 | * Copyright (c) 1988,1991 by the Massachusetts Institute of Technology. |
---|
5 | * |
---|
6 | * For redistribution rights, see "mit-copyright.h" |
---|
7 | */ |
---|
8 | |
---|
9 | static char *rcsid_mount_c = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/attach/unmount.c,v 1.8 1993-05-05 17:05:21 vrt Exp $"; |
---|
10 | |
---|
11 | #include "attach.h" |
---|
12 | |
---|
13 | |
---|
14 | #if !defined(ultrix) && !defined(_IBMR2) && !defined(SOLARIS) |
---|
15 | #include <mntent.h> |
---|
16 | #endif |
---|
17 | |
---|
18 | #if defined(_AIX) && (AIXV < 30) |
---|
19 | #include <rpc/nfsmount.h> |
---|
20 | #include <rpc/rpcmount.h> |
---|
21 | #endif |
---|
22 | |
---|
23 | #ifdef _AIX |
---|
24 | #define unmount(x) umount(x) |
---|
25 | #endif |
---|
26 | |
---|
27 | #ifdef _IBMR2 |
---|
28 | #include <sys/id.h> |
---|
29 | #endif |
---|
30 | |
---|
31 | |
---|
32 | /* |
---|
33 | * Unmount a filesystem. |
---|
34 | */ |
---|
35 | unmount_42(errname, mntpt, dev) |
---|
36 | char *errname; |
---|
37 | char *mntpt; |
---|
38 | char *dev; |
---|
39 | { |
---|
40 | #ifdef UMOUNT_CMD |
---|
41 | int status; |
---|
42 | |
---|
43 | #ifdef _IBMR2 |
---|
44 | if (setuidx(ID_REAL|ID_EFFECTIVE, 0)) |
---|
45 | #else |
---|
46 | if (setreuid(0,0)) |
---|
47 | #endif |
---|
48 | { |
---|
49 | fprintf(stderr,"%s: unable to change the uid to 0\n", errname); |
---|
50 | return(FAILURE); |
---|
51 | } |
---|
52 | |
---|
53 | switch (fork()) { |
---|
54 | case -1: |
---|
55 | fprintf(stderr, "%s: unable to fork\n", errname); |
---|
56 | return(FAILURE); |
---|
57 | /* NOTREACHED */ |
---|
58 | case 0: |
---|
59 | execl(UMOUNT_CMD, UMOUNT_CMD, mntpt, (char *)0); |
---|
60 | exit(1); |
---|
61 | /* NOTREACHED */ |
---|
62 | default: |
---|
63 | wait(&status); |
---|
64 | break; |
---|
65 | } |
---|
66 | return(status ? FAILURE : SUCCESS); |
---|
67 | |
---|
68 | #else /* !UMOUNT_CMD */ |
---|
69 | |
---|
70 | #if defined(_AIX) && (AIXV > 30) |
---|
71 | #include <sys/fullstat.h> |
---|
72 | |
---|
73 | struct stat statb; |
---|
74 | |
---|
75 | if (statx(mntpt, &statb, 0, STX_NORMAL) < 0) { |
---|
76 | fprintf(stderr, |
---|
77 | "%s: Directory %s appears to have already been removed\n", |
---|
78 | errname, mntpt); |
---|
79 | return(SUCCESS); |
---|
80 | } |
---|
81 | if ((statb.st_flag & FS_MOUNT) == 0) { |
---|
82 | fprintf(stderr, |
---|
83 | "%s: Directory %s is no longer a mount point\n", |
---|
84 | errname, mntpt); |
---|
85 | return(SUCCESS); |
---|
86 | } |
---|
87 | if (uvmount(statb.st_vfs, 0)) { |
---|
88 | if (errno == EINVAL || errno == ENOENT) { |
---|
89 | fprintf(stderr, |
---|
90 | "%s: Directory %s appears to already be unmounted\n", |
---|
91 | errname, mntpt); |
---|
92 | return(SUCCESS); |
---|
93 | } else { |
---|
94 | fprintf(stderr, "%s: Unable to unmount %s: %s\n", errname, |
---|
95 | mntpt, sys_errlist[errno]); |
---|
96 | return (FAILURE); |
---|
97 | } |
---|
98 | } |
---|
99 | return(SUCCESS); |
---|
100 | |
---|
101 | #else /* !AIX 3.1 */ |
---|
102 | |
---|
103 | FILE *tmpmnt, *mnted; |
---|
104 | char *tmpname; |
---|
105 | #ifndef ultrix |
---|
106 | struct mntent *mnt; |
---|
107 | #endif |
---|
108 | int tmpfd; |
---|
109 | |
---|
110 | #ifdef ultrix |
---|
111 | int fsloc = 0, found = 0; |
---|
112 | struct fs_data fsdata; |
---|
113 | |
---|
114 | while (getmountent(&fsloc, &fsdata, 1) > 0) { |
---|
115 | if (!strcmp(fsdata.fd_path, mntpt)) { |
---|
116 | found = 1; |
---|
117 | break; |
---|
118 | } |
---|
119 | } |
---|
120 | if (!found) { |
---|
121 | fprintf(stderr, |
---|
122 | "%s: Directory %s appears to already be unmounted\n", |
---|
123 | errname, mntpt); |
---|
124 | return(SUCCESS); |
---|
125 | } |
---|
126 | /* this hack to avoid ugly ifdef's */ |
---|
127 | #define unmount(x) umount(fsdata.fd_dev) |
---|
128 | #endif /* ultrix */ |
---|
129 | |
---|
130 | #if defined(_AIX) && (AIXV < 30) |
---|
131 | if (unmount(dev ? dev : mntpt) < 0) |
---|
132 | #else |
---|
133 | if (unmount(mntpt) < 0) |
---|
134 | #endif |
---|
135 | { |
---|
136 | if (errno == EINVAL || errno == ENOENT |
---|
137 | #ifdef _AIX |
---|
138 | || errno == ENOTBLK |
---|
139 | #endif |
---|
140 | ) { |
---|
141 | fprintf(stderr, |
---|
142 | "%s: Directory %s appears to already be unmounted\n", |
---|
143 | errname, mntpt); |
---|
144 | /* Continue on, to flush mtab if necessary */ |
---|
145 | } else { |
---|
146 | fprintf(stderr, "%s: Unable to unmount %s: %s\n", errname, |
---|
147 | mntpt, sys_errlist[errno]); |
---|
148 | return (FAILURE); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | #ifdef ultrix |
---|
153 | #undef unmount |
---|
154 | return(SUCCESS); |
---|
155 | #else /* !ultrix */ |
---|
156 | |
---|
157 | lock_mtab(); |
---|
158 | if (!(tmpname = malloc(strlen(mtab_fn)+7))) { |
---|
159 | fprintf(stderr, "Can't malloc temp filename for unmount!\n"); |
---|
160 | exit(ERR_FATAL); |
---|
161 | } |
---|
162 | (void) strcpy(tmpname, mtab_fn); |
---|
163 | (void) strcat(tmpname, "XXXXXX"); |
---|
164 | mktemp(tmpname); |
---|
165 | if ((tmpfd = open(tmpname, O_RDWR|O_CREAT|O_TRUNC, 0644)) < 0) { |
---|
166 | fprintf(stderr, "Can't open temporary file for umount!\n"); |
---|
167 | exit(ERR_FATAL); |
---|
168 | } |
---|
169 | close(tmpfd); |
---|
170 | tmpmnt = setmntent(tmpname, "w"); |
---|
171 | if (!tmpmnt) { |
---|
172 | fprintf(stderr, |
---|
173 | "Can't open temporary file for writing in umount!\n"); |
---|
174 | exit(ERR_FATAL); |
---|
175 | } |
---|
176 | mnted = setmntent(mtab_fn, "r"); |
---|
177 | if (!mnted) { |
---|
178 | fprintf(stderr, "Can't open %s for read:%s\n", mtab_fn, |
---|
179 | sys_errlist[errno]); |
---|
180 | exit(ERR_FATAL); |
---|
181 | } |
---|
182 | /* Delete filesystem from /etc/mtab */ |
---|
183 | while (mnt = getmntent(mnted)) |
---|
184 | if (strcmp(mnt->mnt_dir, mntpt)) |
---|
185 | addmntent(tmpmnt, mnt); |
---|
186 | |
---|
187 | endmntent(tmpmnt); |
---|
188 | endmntent(mnted); |
---|
189 | if (rename(tmpname, mtab_fn) < 0) { |
---|
190 | fprintf(stderr, "Unable to rename %s to %s: %s\n", tmpname, |
---|
191 | mtab_fn, sys_errlist[errno]); |
---|
192 | exit(ERR_FATAL); |
---|
193 | } |
---|
194 | unlock_mtab(); |
---|
195 | |
---|
196 | return (SUCCESS); |
---|
197 | #endif /* ultrix */ |
---|
198 | #endif /* !AIX 3.1 */ |
---|
199 | #endif /* !UMOUNT_CMD */ |
---|
200 | } |
---|
201 | |
---|
202 | #ifdef NFS |
---|
203 | /* |
---|
204 | * Unmount an NFS filesystem |
---|
205 | */ |
---|
206 | nfs_unmount(errname, host, hostaddr, mntpt, rmntpt) |
---|
207 | char *errname; |
---|
208 | char *host; |
---|
209 | struct in_addr hostaddr; |
---|
210 | char *mntpt; |
---|
211 | char *rmntpt; |
---|
212 | { |
---|
213 | static struct sockaddr_in sin; |
---|
214 | struct timeval timeout; |
---|
215 | CLIENT *client; |
---|
216 | enum clnt_stat rpc_stat; |
---|
217 | |
---|
218 | if (unmount_42(errname, mntpt, NULL) == FAILURE) |
---|
219 | return (FAILURE); |
---|
220 | |
---|
221 | /* |
---|
222 | * If we can't contact the host, don't bother complaining; |
---|
223 | * it won't actually hurt anything except that hosts rmtab. |
---|
224 | */ |
---|
225 | if (errored_out(hostaddr)) |
---|
226 | return (SUCCESS); |
---|
227 | |
---|
228 | if ((client = (CLIENT *)rpc_create(hostaddr, &sin)) == NULL) { |
---|
229 | fprintf(stderr, |
---|
230 | "%s: Server %s not responding\n", |
---|
231 | errname, host); |
---|
232 | return (SUCCESS); |
---|
233 | } |
---|
234 | |
---|
235 | client->cl_auth = spoofunix_create_default(spoofhost, real_uid); |
---|
236 | |
---|
237 | timeout.tv_usec = 0; |
---|
238 | timeout.tv_sec = 20; |
---|
239 | rpc_stat = clnt_call(client, MOUNTPROC_UMNT, xdr_path, &rmntpt, |
---|
240 | xdr_void, NULL, timeout); |
---|
241 | if (rpc_stat != RPC_SUCCESS) { |
---|
242 | mark_errored(hostaddr); |
---|
243 | switch (rpc_stat) { |
---|
244 | case RPC_TIMEDOUT: |
---|
245 | fprintf(stderr, "%s: Timeout while contacting mount daemon on %s\n", |
---|
246 | errname, host); |
---|
247 | break; |
---|
248 | case RPC_AUTHERROR: |
---|
249 | fprintf(stderr, "%s: Authentication failed\n", |
---|
250 | errname, host); |
---|
251 | break; |
---|
252 | case RPC_PMAPFAILURE: |
---|
253 | fprintf(stderr, "%s: Can't find mount daemon on %s\n", |
---|
254 | errname, host); |
---|
255 | break; |
---|
256 | case RPC_PROGUNAVAIL: |
---|
257 | case RPC_PROGNOTREGISTERED: |
---|
258 | fprintf(stderr, "%s: Mount daemon not available on %s\n", |
---|
259 | errname, host); |
---|
260 | break; |
---|
261 | default: |
---|
262 | fprintf(stderr, "%s: System error contacting server %s\n", |
---|
263 | errname, host); |
---|
264 | break; |
---|
265 | } |
---|
266 | if (debug_flag) |
---|
267 | clnt_perror(client, "RPC return status"); |
---|
268 | return (SUCCESS); |
---|
269 | } |
---|
270 | |
---|
271 | return (SUCCESS); |
---|
272 | } |
---|
273 | #endif |
---|
274 | |
---|
275 | #ifdef SOLARIS |
---|
276 | bool_t |
---|
277 | xdr_path(xdrs, pathp) |
---|
278 | XDR *xdrs; |
---|
279 | char **pathp; |
---|
280 | { |
---|
281 | if (xdr_string(xdrs, pathp, 1024)) { |
---|
282 | return(TRUE); |
---|
283 | } |
---|
284 | return(FALSE); |
---|
285 | } |
---|
286 | |
---|
287 | xdr_fhstatus(xdrs, fhsp) |
---|
288 | XDR *xdrs; |
---|
289 | struct fhstatus *fhsp; |
---|
290 | { |
---|
291 | if (!xdr_int(xdrs, &fhsp->fhs_status)) |
---|
292 | return FALSE; |
---|
293 | if (fhsp->fhs_status == 0) { |
---|
294 | if (!xdr_fhandle(xdrs, &fhsp->fhs_fh)) |
---|
295 | return FALSE; |
---|
296 | } |
---|
297 | } |
---|
298 | xdr_fhandle(xdrs, fhp) |
---|
299 | XDR *xdrs; |
---|
300 | fhandle_t *fhp; |
---|
301 | { |
---|
302 | if (xdr_opaque(xdrs, fhp, NFS_FHSIZE)) { |
---|
303 | return (TRUE); |
---|
304 | } |
---|
305 | return (FALSE); |
---|
306 | } |
---|
307 | |
---|
308 | #endif |
---|
309 | |
---|