2 Unmount utility program for Linux CIFS VFS (virtual filesystem) client
3 Copyright (C) 2005 Steve French (sfrench@us.ibm.com)
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.
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.
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. */
27 #include <sys/types.h>
28 #include <sys/mount.h>
29 #include <sys/ioctl.h>
39 #define UNMOUNT_CIFS_VERSION_MAJOR "0"
40 #define UNMOUNT_CIFS_VERSION_MINOR "4"
42 #ifndef UNMOUNT_CIFS_VENDOR_SUFFIX
43 #define UNMOUNT_CIFS_VENDOR_SUFFIX ""
47 #define MNT_DETACH 0x02
51 #define MNT_EXPIRE 0x04
55 #define MOUNTED_LOCK "/etc/mtab~"
58 #define MOUNTED_TEMP "/etc/mtab.tmp"
61 #define CIFS_IOC_CHECKUMOUNT _IO(0xCF, 2)
62 #define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDU */
64 static struct option longopts[] = {
65 { "all", 0, NULL, 'a' },
66 { "help",0, NULL, 'h' },
67 { "read-only", 0, NULL, 'r' },
68 { "ro", 0, NULL, 'r' },
69 { "verbose", 0, NULL, 'v' },
70 { "version", 0, NULL, 'V' },
71 { "expire", 0, NULL, 'e' },
72 { "force", 0, 0, 'f' },
73 { "lazy", 0, 0, 'l' },
74 { "no-mtab", 0, 0, 'n' },
81 static void umount_cifs_usage(void)
83 printf("\nUsage: %s <remotetarget> <dir>\n", thisprogram);
84 printf("\nUnmount the specified directory\n");
85 printf("\nLess commonly used options:");
86 printf("\n\t-r\tIf mount fails, retry with readonly remount.");
87 printf("\n\t-n\tDo not write to mtab.");
88 printf("\n\t-f\tAttempt a forced unmount, even if the fs is busy.");
89 printf("\n\t-l\tAttempt lazy unmount, Unmount now, cleanup later.");
90 printf("\n\t-v\tEnable verbose mode (may be useful for debugging).");
91 printf("\n\t-h\tDisplay this help.");
92 printf("\n\nOptions are described in more detail in the manual page");
93 printf("\n\tman 8 umount.cifs\n");
94 printf("\nTo display the version number of the cifs umount utility:");
95 printf("\n\t%s -V\n",thisprogram);
96 printf("\nNote that invoking the umount utility on cifs mounts, can execute /sbin/umount.cifs (if it is present and -i is not specified to umount).\n");
99 static int umount_check_perm(char * dir)
104 /* allow root to unmount, no matter what */
108 /* presumably can not chdir into the target as we do on mount */
109 fileid = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0);
112 printf("error opening mountpoint %d %s",errno,strerror(errno));
116 rc = ioctl(fileid, CIFS_IOC_CHECKUMOUNT, NULL);
119 printf("ioctl returned %d with errno %d %s\n",rc,errno,strerror(errno));
122 printf("user unmounting via %s is an optional feature of the cifs filesystem driver (cifs.ko)\n\tand requires cifs.ko version 1.32 or later\n",thisprogram);
124 printf("user unmount of %s failed with %d %s\n",dir,errno,strerror(errno));
130 int remove_from_mtab(char * mountpoint)
135 struct mntent * mount_entry;
137 /* Do we need to check if it is a symlink to e.g. /proc/mounts
138 in which case we probably do not want to update it? */
140 /* Do we first need to check if it is writable? */
142 /* BB lock_mtab - the smbumount and mount-utils package appear to lock
143 in a different incompat. way, and it seems that mount-utils have
144 changed the way they lock a few times. We should fix this someday
145 or at least find out how the mount-utils are supposed to handle this */
148 printf("attempting to remove from mtab\n");
150 org_fd = setmntent(MOUNTED, "r");
153 printf("Can not open %s\n",MOUNTED);
157 new_fd = setmntent(MOUNTED_TEMP,"w");
159 printf("Can not open temp file %s", MOUNTED_TEMP);
164 /* BB fix so we only remove the last entry that matches BB */
165 while((mount_entry = getmntent(org_fd)) != NULL) {
166 if(strcmp(mount_entry->mnt_dir, mountpoint) != 0) {
167 addmntent(new_fd, mount_entry);
170 printf("entry not copied (removed)\n");
176 printf("done updating tmp file\n");
177 rc = fchmod (fileno (new_fd), S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
179 printf("error %s changing mode of %s\n", strerror(errno),
184 rc = rename(MOUNTED_TEMP, MOUNTED);
187 printf("failure %s renaming %s to %s\n",strerror(errno),
188 MOUNTED_TEMP, MOUNTED);
192 /* BB unlock_mtab - unlink(MOUNTED_LOCK) */
197 int main(int argc, char ** argv)
203 int retry_remount = 0;
204 struct statfs statbuf;
208 thisprogram = argv[0];
219 if(thisprogram == NULL)
220 thisprogram = "umount.cifs";
222 /* add sharename in opts string as unc= parm */
224 while ((c = getopt_long (argc, argv, "afhilnrvV",
225 longopts, NULL)) != -1) {
227 /* No code to do the following option yet */
242 flags |= MNT_DETACH; /* lazy unmount */
245 flags |= MNT_EXPIRE; /* gradually timeout */
254 printf ("umount.cifs version: %s.%s%s\n",
255 UNMOUNT_CIFS_VERSION_MAJOR,
256 UNMOUNT_CIFS_VERSION_MINOR,
257 UNMOUNT_CIFS_VENDOR_SUFFIX);
260 printf("unknown unmount option %c\n",c);
266 /* move past the umount options */
270 mountpoint = argv[0];
272 if((argc < 1) || (argv[0] == NULL)) {
273 printf("\nMissing name of unmount directory\n");
279 printf("optind %d unmount dir %s\n",optind, mountpoint);
281 /* check if running effectively root */
283 printf("Trying to unmount when %s not installed suid\n",thisprogram);
285 printf("euid = %d\n",geteuid());
289 /* fixup path if needed */
291 /* make sure that this is a cifs filesystem */
292 rc = statfs(mountpoint, &statbuf);
294 if(rc || (statbuf.f_type != CIFS_MAGIC_NUMBER)) {
295 printf("Wrong filesystem. This utility only unmounts cifs filesystems.\n");
299 /* check if our uid was the one who mounted */
300 rc = umount_check_perm(mountpoint);
302 printf("Not permitted to unmount\n");
306 if(umount2(mountpoint, flags)) {
307 /* remember to kill daemon on error */
310 printf("unmount failed but no error number set\n");
313 printf("unmount error %d = %s\n",errno,strerror(errno));
315 printf("Refer to the umount.cifs(8) manual page (e.g.man 8 umount.cifs)\n");
319 printf("umount2 succeeded\n");
321 remove_from_mtab(mountpoint);