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 _IO('c', 2)
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);
98 printf("error opening mountpoint %d %s",errno
,strerror(errno
));
102 rc
= ioctl(fileid
, CIFS_IOC_CHECKUMOUNT
, NULL
);
105 printf("ioctl returned %d with errno %d %s\n",rc
,errno
,strerror(errno
));
108 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
);
110 printf("user unmount of %s failed with %d %s",dir
,errno
,strerror(errno
));
116 int main(int argc
, char ** argv
)
122 int retry_remount
= 0;
123 struct mntent mountent
;
128 thisprogram
= argv
[0];
139 if(thisprogram
== NULL
)
140 thisprogram
= "umount.cifs";
142 /* add sharename in opts string as unc= parm */
144 while ((c
= getopt_long (argc
, argv
, "afhilnrvV",
145 longopts
, NULL
)) != -1) {
147 /* No code to do the following option yet */
162 flags
|= MNT_DETACH
; /* lazy unmount */
165 flags
|= MNT_EXPIRE
; /* gradually timeout */
174 printf ("umount.cifs version: %s.%s%s\n",
175 UNMOUNT_CIFS_VERSION_MAJOR
,
176 UNMOUNT_CIFS_VERSION_MINOR
,
177 UNMOUNT_CIFS_VENDOR_SUFFIX
);
180 printf("unknown unmount option %c\n",c
);
186 /* move past the umount options */
190 mountpoint
= argv
[0];
192 if((argc
< 1) || (argv
[0] == NULL
)) {
193 printf("\nMissing name of unmount directory\n");
199 printf("optind %d unmount dir %s\n",optind
, mountpoint
);
201 /* check if running effectively root */
203 printf("Trying to unmount when %s not installed suid\n",thisprogram
);
205 /* fixup path if needed */
207 /* check if our uid was the one who mounted */
208 rc
= umount_check_perm(mountpoint
);
213 if(umount2(mountpoint
, flags
)) {
214 /* remember to kill daemon on error */
218 printf("mount failed but no error number set\n");
222 printf("mount error %d = %s\n",errno
,strerror(errno
));
224 printf("Refer to the umount.cifs(8) manual page (e.g.man 8 umount.cifs)\n");
227 pmntfile
= setmntent(MOUNTED
, "a+");
229 /* mountent.mnt_fsname = share_name;
230 mountent.mnt_dir = mountpoint;
231 mountent.mnt_type = "cifs";
232 mountent.mnt_opts = malloc(220);
233 if(mountent.mnt_opts) {
234 char * mount_user = getusername();
235 memset(mountent.mnt_opts,0,200);
236 if(flags & MS_RDONLY)
237 strcat(mountent.mnt_opts,"ro");
239 strcat(mountent.mnt_opts,"rw");
240 if(flags & MS_MANDLOCK)
241 strcat(mountent.mnt_opts,",mand");
243 strcat(mountent.mnt_opts,",nomand");
244 if(flags & MS_NOEXEC)
245 strcat(mountent.mnt_opts,",noexec");
246 if(flags & MS_NOSUID)
247 strcat(mountent.mnt_opts,",nosuid");
249 strcat(mountent.mnt_opts,",nodev");
250 if(flags & MS_SYNCHRONOUS)
251 strcat(mountent.mnt_opts,",synch");
254 strcat(mountent.mnt_opts,",user=");
255 strcat(mountent.mnt_opts,mount_user);
260 mountent.mnt_freq = 0;
261 mountent.mnt_passno = 0;
262 rc = addmntent(pmntfile,&mountent);
264 if(mountent.mnt_opts)
265 free(mountent.mnt_opts);*/
267 printf("could not update mount table\n");