1 | /*
|
---|
2 | Unmount utility program for Linux CIFS VFS (virtual filesystem) client
|
---|
3 | Copyright (C) 2005 Steve French (sfrench@us.ibm.com)
|
---|
4 |
|
---|
5 | This program is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation; either version 2 of the License, or
|
---|
8 | (at your option) any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with this program; if not, write to the Free Software
|
---|
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
---|
18 |
|
---|
19 | #ifndef _GNU_SOURCE
|
---|
20 | #define _GNU_SOURCE
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | #include <stdlib.h>
|
---|
24 | #include <stdio.h>
|
---|
25 | #include <unistd.h>
|
---|
26 | #include <ctype.h>
|
---|
27 | #include <sys/types.h>
|
---|
28 | #include <sys/mount.h>
|
---|
29 | #include <sys/ioctl.h>
|
---|
30 | #include <sys/stat.h>
|
---|
31 | #include <sys/vfs.h>
|
---|
32 | #include <fcntl.h>
|
---|
33 | #include <getopt.h>
|
---|
34 | #include <errno.h>
|
---|
35 | #include <string.h>
|
---|
36 | #include <mntent.h>
|
---|
37 |
|
---|
38 | #define UNMOUNT_CIFS_VERSION_MAJOR "0"
|
---|
39 | #define UNMOUNT_CIFS_VERSION_MINOR "5"
|
---|
40 |
|
---|
41 | #ifndef UNMOUNT_CIFS_VENDOR_SUFFIX
|
---|
42 | #ifdef _SAMBA_BUILD_
|
---|
43 | #include "include/version.h"
|
---|
44 | #ifdef SAMBA_VERSION_VENDOR_SUFFIX
|
---|
45 | #define UNMOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING"-"SAMBA_VERSION_VENDOR_SUFFIX
|
---|
46 | #else
|
---|
47 | #define UNMOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING
|
---|
48 | #endif /* SAMBA_VERSION_OFFICIAL_STRING and SAMBA_VERSION_VENDOR_SUFFIX */
|
---|
49 | #else
|
---|
50 | #define UNMOUNT_CIFS_VENDOR_SUFFIX ""
|
---|
51 | #endif /* _SAMBA_BUILD_ */
|
---|
52 | #endif /* UNMOUNT_CIFS_VENDOR_SUFFIX */
|
---|
53 |
|
---|
54 | #ifndef MNT_DETACH
|
---|
55 | #define MNT_DETACH 0x02
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | #ifndef MNT_EXPIRE
|
---|
59 | #define MNT_EXPIRE 0x04
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | #ifndef MOUNTED_LOCK
|
---|
63 | #define MOUNTED_LOCK "/etc/mtab~"
|
---|
64 | #endif
|
---|
65 | #ifndef MOUNTED_TEMP
|
---|
66 | #define MOUNTED_TEMP "/etc/mtab.tmp"
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | #define CIFS_IOC_CHECKUMOUNT _IO(0xCF, 2)
|
---|
70 | #define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDU */
|
---|
71 |
|
---|
72 | static struct option longopts[] = {
|
---|
73 | { "all", 0, NULL, 'a' },
|
---|
74 | { "help",0, NULL, 'h' },
|
---|
75 | { "read-only", 0, NULL, 'r' },
|
---|
76 | { "ro", 0, NULL, 'r' },
|
---|
77 | { "verbose", 0, NULL, 'v' },
|
---|
78 | { "version", 0, NULL, 'V' },
|
---|
79 | { "expire", 0, NULL, 'e' },
|
---|
80 | { "force", 0, 0, 'f' },
|
---|
81 | { "lazy", 0, 0, 'l' },
|
---|
82 | { "no-mtab", 0, 0, 'n' },
|
---|
83 | { NULL, 0, NULL, 0 }
|
---|
84 | };
|
---|
85 |
|
---|
86 | const char * thisprogram;
|
---|
87 | int verboseflg = 0;
|
---|
88 |
|
---|
89 | static void umount_cifs_usage(void)
|
---|
90 | {
|
---|
91 | printf("\nUsage: %s <remotetarget> <dir>\n", thisprogram);
|
---|
92 | printf("\nUnmount the specified directory\n");
|
---|
93 | printf("\nLess commonly used options:");
|
---|
94 | printf("\n\t-r\tIf mount fails, retry with readonly remount.");
|
---|
95 | printf("\n\t-n\tDo not write to mtab.");
|
---|
96 | printf("\n\t-f\tAttempt a forced unmount, even if the fs is busy.");
|
---|
97 | printf("\n\t-l\tAttempt lazy unmount, Unmount now, cleanup later.");
|
---|
98 | printf("\n\t-v\tEnable verbose mode (may be useful for debugging).");
|
---|
99 | printf("\n\t-h\tDisplay this help.");
|
---|
100 | printf("\n\nOptions are described in more detail in the manual page");
|
---|
101 | printf("\n\tman 8 umount.cifs\n");
|
---|
102 | printf("\nTo display the version number of the cifs umount utility:");
|
---|
103 | printf("\n\t%s -V\n",thisprogram);
|
---|
104 | printf("\nInvoking the umount utility on cifs mounts, can execute");
|
---|
105 | printf(" /sbin/umount.cifs (if present and umount -i is not specified.\n");
|
---|
106 | }
|
---|
107 |
|
---|
108 | static int umount_check_perm(char * dir)
|
---|
109 | {
|
---|
110 | int fileid;
|
---|
111 | int rc;
|
---|
112 |
|
---|
113 | /* allow root to unmount, no matter what */
|
---|
114 | if(getuid() == 0)
|
---|
115 | return 0;
|
---|
116 |
|
---|
117 | /* presumably can not chdir into the target as we do on mount */
|
---|
118 | fileid = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0);
|
---|
119 | if(fileid == -1) {
|
---|
120 | if(verboseflg)
|
---|
121 | printf("error opening mountpoint %d %s",errno,strerror(errno));
|
---|
122 | return errno;
|
---|
123 | }
|
---|
124 |
|
---|
125 | rc = ioctl(fileid, CIFS_IOC_CHECKUMOUNT, NULL);
|
---|
126 |
|
---|
127 | if(verboseflg)
|
---|
128 | printf("ioctl returned %d with errno %d %s\n",rc,errno,strerror(errno));
|
---|
129 |
|
---|
130 | if(rc == ENOTTY) {
|
---|
131 | printf("user unmounting via %s is an optional feature of",thisprogram);
|
---|
132 | printf(" the cifs filesystem driver (cifs.ko)");
|
---|
133 | printf("\n\tand requires cifs.ko version 1.32 or later\n");
|
---|
134 | } else if (rc > 0)
|
---|
135 | printf("user unmount of %s failed with %d %s\n",dir,errno,strerror(errno));
|
---|
136 | close(fileid);
|
---|
137 |
|
---|
138 | return rc;
|
---|
139 | }
|
---|
140 |
|
---|
141 | static int lock_mtab(void)
|
---|
142 | {
|
---|
143 | int rc;
|
---|
144 |
|
---|
145 | rc = mknod(MOUNTED_LOCK , 0600, 0);
|
---|
146 | if(rc == -1)
|
---|
147 | printf("\ngetting lock file %s failed with %s\n",MOUNTED_LOCK,
|
---|
148 | strerror(errno));
|
---|
149 |
|
---|
150 | return rc;
|
---|
151 |
|
---|
152 | }
|
---|
153 |
|
---|
154 | static void unlock_mtab(void)
|
---|
155 | {
|
---|
156 | unlink(MOUNTED_LOCK);
|
---|
157 | }
|
---|
158 |
|
---|
159 | static int remove_from_mtab(char * mountpoint)
|
---|
160 | {
|
---|
161 | int rc;
|
---|
162 | int num_matches;
|
---|
163 | FILE * org_fd;
|
---|
164 | FILE * new_fd;
|
---|
165 | struct mntent * mount_entry;
|
---|
166 |
|
---|
167 | /* Do we need to check if it is a symlink to e.g. /proc/mounts
|
---|
168 | in which case we probably do not want to update it? */
|
---|
169 |
|
---|
170 | /* Do we first need to check if it is writable? */
|
---|
171 |
|
---|
172 | if (lock_mtab()) {
|
---|
173 | printf("Mount table locked\n");
|
---|
174 | return -EACCES;
|
---|
175 | }
|
---|
176 |
|
---|
177 | if(verboseflg)
|
---|
178 | printf("attempting to remove from mtab\n");
|
---|
179 |
|
---|
180 | org_fd = setmntent(MOUNTED, "r");
|
---|
181 |
|
---|
182 | if(org_fd == NULL) {
|
---|
183 | printf("Can not open %s\n",MOUNTED);
|
---|
184 | unlock_mtab();
|
---|
185 | return -EIO;
|
---|
186 | }
|
---|
187 |
|
---|
188 | new_fd = setmntent(MOUNTED_TEMP,"w");
|
---|
189 | if(new_fd == NULL) {
|
---|
190 | printf("Can not open temp file %s", MOUNTED_TEMP);
|
---|
191 | endmntent(org_fd);
|
---|
192 | unlock_mtab();
|
---|
193 | return -EIO;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /* BB fix so we only remove the last entry that matches BB */
|
---|
197 | num_matches = 0;
|
---|
198 | while((mount_entry = getmntent(org_fd)) != NULL) {
|
---|
199 | if(strcmp(mount_entry->mnt_dir, mountpoint) == 0) {
|
---|
200 | num_matches++;
|
---|
201 | }
|
---|
202 | }
|
---|
203 | if(verboseflg)
|
---|
204 | printf("%d matching entries in mount table\n", num_matches);
|
---|
205 |
|
---|
206 | /* Is there a better way to seek back to the first entry in mtab? */
|
---|
207 | endmntent(org_fd);
|
---|
208 | org_fd = setmntent(MOUNTED, "r");
|
---|
209 |
|
---|
210 | if(org_fd == NULL) {
|
---|
211 | printf("Can not open %s\n",MOUNTED);
|
---|
212 | unlock_mtab();
|
---|
213 | return -EIO;
|
---|
214 | }
|
---|
215 |
|
---|
216 | while((mount_entry = getmntent(org_fd)) != NULL) {
|
---|
217 | if(strcmp(mount_entry->mnt_dir, mountpoint) != 0) {
|
---|
218 | addmntent(new_fd, mount_entry);
|
---|
219 | } else {
|
---|
220 | if(num_matches != 1) {
|
---|
221 | addmntent(new_fd, mount_entry);
|
---|
222 | num_matches--;
|
---|
223 | } else if(verboseflg)
|
---|
224 | printf("entry not copied (ie entry is removed)\n");
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | if(verboseflg)
|
---|
229 | printf("done updating tmp file\n");
|
---|
230 | rc = fchmod (fileno (new_fd), S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
|
---|
231 | if(rc < 0) {
|
---|
232 | printf("error %s changing mode of %s\n", strerror(errno),
|
---|
233 | MOUNTED_TEMP);
|
---|
234 | }
|
---|
235 | endmntent(new_fd);
|
---|
236 |
|
---|
237 | rc = rename(MOUNTED_TEMP, MOUNTED);
|
---|
238 |
|
---|
239 | if(rc < 0) {
|
---|
240 | printf("failure %s renaming %s to %s\n",strerror(errno),
|
---|
241 | MOUNTED_TEMP, MOUNTED);
|
---|
242 | unlock_mtab();
|
---|
243 | return -EIO;
|
---|
244 | }
|
---|
245 |
|
---|
246 | unlock_mtab();
|
---|
247 |
|
---|
248 | return rc;
|
---|
249 | }
|
---|
250 |
|
---|
251 | int main(int argc, char ** argv)
|
---|
252 | {
|
---|
253 | int c;
|
---|
254 | int rc;
|
---|
255 | int flags = 0;
|
---|
256 | int nomtab = 0;
|
---|
257 | int retry_remount = 0;
|
---|
258 | struct statfs statbuf;
|
---|
259 | char * mountpoint;
|
---|
260 |
|
---|
261 | if(argc && argv) {
|
---|
262 | thisprogram = argv[0];
|
---|
263 | } else {
|
---|
264 | umount_cifs_usage();
|
---|
265 | return -EINVAL;
|
---|
266 | }
|
---|
267 |
|
---|
268 | if(argc < 2) {
|
---|
269 | umount_cifs_usage();
|
---|
270 | return -EINVAL;
|
---|
271 | }
|
---|
272 |
|
---|
273 | if(thisprogram == NULL)
|
---|
274 | thisprogram = "umount.cifs";
|
---|
275 |
|
---|
276 | /* add sharename in opts string as unc= parm */
|
---|
277 |
|
---|
278 | while ((c = getopt_long (argc, argv, "afhilnrvV",
|
---|
279 | longopts, NULL)) != -1) {
|
---|
280 | switch (c) {
|
---|
281 | /* No code to do the following option yet */
|
---|
282 | /* case 'a':
|
---|
283 | ++umount_all;
|
---|
284 | break; */
|
---|
285 | case '?':
|
---|
286 | case 'h': /* help */
|
---|
287 | umount_cifs_usage();
|
---|
288 | exit(1);
|
---|
289 | case 'n':
|
---|
290 | ++nomtab;
|
---|
291 | break;
|
---|
292 | case 'f':
|
---|
293 | flags |= MNT_FORCE;
|
---|
294 | break;
|
---|
295 | case 'l':
|
---|
296 | flags |= MNT_DETACH; /* lazy unmount */
|
---|
297 | break;
|
---|
298 | case 'e':
|
---|
299 | flags |= MNT_EXPIRE; /* gradually timeout */
|
---|
300 | break;
|
---|
301 | case 'r':
|
---|
302 | ++retry_remount;
|
---|
303 | break;
|
---|
304 | case 'v':
|
---|
305 | ++verboseflg;
|
---|
306 | break;
|
---|
307 | case 'V':
|
---|
308 | printf ("umount.cifs version: %s.%s%s\n",
|
---|
309 | UNMOUNT_CIFS_VERSION_MAJOR,
|
---|
310 | UNMOUNT_CIFS_VERSION_MINOR,
|
---|
311 | UNMOUNT_CIFS_VENDOR_SUFFIX);
|
---|
312 | exit (0);
|
---|
313 | default:
|
---|
314 | printf("unknown unmount option %c\n",c);
|
---|
315 | umount_cifs_usage();
|
---|
316 | exit(1);
|
---|
317 | }
|
---|
318 | }
|
---|
319 |
|
---|
320 | /* move past the umount options */
|
---|
321 | argv += optind;
|
---|
322 | argc -= optind;
|
---|
323 |
|
---|
324 | mountpoint = argv[0];
|
---|
325 |
|
---|
326 | if((argc < 1) || (argv[0] == NULL)) {
|
---|
327 | printf("\nMissing name of unmount directory\n");
|
---|
328 | umount_cifs_usage();
|
---|
329 | return -EINVAL;
|
---|
330 | }
|
---|
331 |
|
---|
332 | if(verboseflg)
|
---|
333 | printf("optind %d unmount dir %s\n",optind, mountpoint);
|
---|
334 |
|
---|
335 | /* check if running effectively root */
|
---|
336 | if(geteuid() != 0) {
|
---|
337 | printf("Trying to unmount when %s not installed suid\n",thisprogram);
|
---|
338 | if(verboseflg)
|
---|
339 | printf("euid = %d\n",geteuid());
|
---|
340 | return -EACCES;
|
---|
341 | }
|
---|
342 |
|
---|
343 | /* fixup path if needed */
|
---|
344 |
|
---|
345 | /* make sure that this is a cifs filesystem */
|
---|
346 | rc = statfs(mountpoint, &statbuf);
|
---|
347 |
|
---|
348 | if(rc || (statbuf.f_type != CIFS_MAGIC_NUMBER)) {
|
---|
349 | printf("This utility only unmounts cifs filesystems.\n");
|
---|
350 | return -EINVAL;
|
---|
351 | }
|
---|
352 |
|
---|
353 | /* check if our uid was the one who mounted */
|
---|
354 | rc = umount_check_perm(mountpoint);
|
---|
355 | if (rc) {
|
---|
356 | printf("Not permitted to unmount\n");
|
---|
357 | return rc;
|
---|
358 | }
|
---|
359 |
|
---|
360 | if(umount2(mountpoint, flags)) {
|
---|
361 | /* remember to kill daemon on error */
|
---|
362 | switch (errno) {
|
---|
363 | case 0:
|
---|
364 | printf("unmount failed but no error number set\n");
|
---|
365 | break;
|
---|
366 | default:
|
---|
367 | printf("unmount error %d = %s\n",errno,strerror(errno));
|
---|
368 | }
|
---|
369 | printf("Refer to the umount.cifs(8) manual page (man 8 umount.cifs)\n");
|
---|
370 | return -1;
|
---|
371 | } else {
|
---|
372 | if(verboseflg)
|
---|
373 | printf("umount2 succeeded\n");
|
---|
374 | if(nomtab == 0)
|
---|
375 | remove_from_mtab(mountpoint);
|
---|
376 | }
|
---|
377 |
|
---|
378 | return 0;
|
---|
379 | }
|
---|
380 |
|
---|