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>
37 #define UNMOUNT_CIFS_VERSION_MAJOR "0"
38 #define UNMOUNT_CIFS_VERSION_MINOR "1"
40 #ifndef UNMOUNT_CIFS_VENDOR_SUFFIX
41 #define UNMOUNT_CIFS_VENDOR_SUFFIX ""
45 #define MNT_DETACH 0x02
49 #define MNT_EXPIRE 0x04
52 #define CIFS_IOC_CHECKUMOUNT _IOR('u', 2, int)
54 static struct option longopts[] = {
55 { "all", 0, NULL, 'a' },
56 { "help",0, NULL, 'h' },
57 { "read-only", 0, NULL, 'r' },
58 { "ro", 0, NULL, 'r' },
59 { "verbose", 0, NULL, 'v' },
60 { "version", 0, NULL, 'V' },
61 { "expire", 0, NULL, 'e' },
62 { "force", 0, 0, 'f' },
63 { "lazy", 0, 0, 'l' },
64 { "no-mtab", 0, 0, 'n' },
71 static void umount_cifs_usage(void)
73 printf("\nUsage: %s <remotetarget> <dir>\n", thisprogram);
74 printf("\nUnmount the specified directory\n");
75 printf("\nLess commonly used options:");
76 printf("\n\t-r\tIf mount fails, retry with readonly remount.");
77 printf("\n\t-n\tDo not write to mtab.");
78 printf("\n\t-f\tAttempt a forced unmount, even if the fs is busy.");
79 printf("\n\t-l\tAttempt lazy unmount, Unmount now, cleanup later.");
80 printf("\n\t-v\tEnable verbose mode (may be useful for debugging).");
81 printf("\n\t-h\tDisplay this help.");
82 printf("\n\nOptions are described in more detail in the manual page");
83 printf("\n\tman 8 umount.cifs\n");
84 printf("\nTo display the version number of the cifs umount utility:");
85 printf("\n\t%s -V\n",thisprogram);
88 static int umount_check_perm(char * dir)
93 /* presumably can not chdir into the target as we do on mount */
95 fileid = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0);
97 /* check if fileid valid if fileid == -1 BB FIXME */
99 rc = ioctl(fileid, CIFS_IOC_CHECKUMOUNT, NULL);
102 printf("ioctl returned %d with errno %d\n",rc,errno);
109 int main(int argc, char ** argv)
115 int retry_remount = 0;
116 struct mntent mountent;
121 thisprogram = argv[0];
132 if(thisprogram == NULL)
133 thisprogram = "umount.cifs";
135 /* add sharename in opts string as unc= parm */
137 while ((c = getopt_long (argc, argv, "afhilnrvV",
138 longopts, NULL)) != -1) {
140 /* No code to do the following option yet */
155 flags |= MNT_DETACH; /* lazy unmount */
158 flags |= MNT_EXPIRE; /* gradually timeout */
167 printf ("umount.cifs version: %s.%s%s\n",
168 UNMOUNT_CIFS_VERSION_MAJOR,
169 UNMOUNT_CIFS_VERSION_MINOR,
170 UNMOUNT_CIFS_VENDOR_SUFFIX);
173 printf("unknown unmount option %c\n",c);
179 /* move past the umount options */
183 mountpoint = argv[0];
185 if((argc < 1) || (argv[0] == NULL)) {
186 printf("\nMissing name of unmount directory\n");
192 printf("optind %d unmount dir %s\n",optind, mountpoint);
194 /* check if running effectively root */
196 printf("Trying to unmount when %s not installed suid\n",thisprogram);
198 /* fixup path if needed */
200 /* check if our uid was the one who mounted */
201 rc = umount_check_perm(mountpoint);
206 if(umount2(mountpoint, flags)) {
207 /* remember to kill daemon on error */
211 printf("mount failed but no error number set\n");
215 printf("mount error %d = %s\n",errno,strerror(errno));
217 printf("Refer to the umount.cifs(8) manual page (e.g.man 8 umount.cifs)\n");
220 pmntfile = setmntent(MOUNTED, "a+");
222 /* mountent.mnt_fsname = share_name;
223 mountent.mnt_dir = mountpoint;
224 mountent.mnt_type = "cifs";
225 mountent.mnt_opts = malloc(220);
226 if(mountent.mnt_opts) {
227 char * mount_user = getusername();
228 memset(mountent.mnt_opts,0,200);
229 if(flags & MS_RDONLY)
230 strcat(mountent.mnt_opts,"ro");
232 strcat(mountent.mnt_opts,"rw");
233 if(flags & MS_MANDLOCK)
234 strcat(mountent.mnt_opts,",mand");
236 strcat(mountent.mnt_opts,",nomand");
237 if(flags & MS_NOEXEC)
238 strcat(mountent.mnt_opts,",noexec");
239 if(flags & MS_NOSUID)
240 strcat(mountent.mnt_opts,",nosuid");
242 strcat(mountent.mnt_opts,",nodev");
243 if(flags & MS_SYNCHRONOUS)
244 strcat(mountent.mnt_opts,",synch");
247 strcat(mountent.mnt_opts,",user=");
248 strcat(mountent.mnt_opts,mount_user);
253 mountent.mnt_freq = 0;
254 mountent.mnt_passno = 0;
255 rc = addmntent(pmntfile,&mountent);
257 if(mountent.mnt_opts)
258 free(mountent.mnt_opts);*/
260 printf("could not update mount table\n");