r6303: Setting up for 3.0.15pre1
[Samba.git] / source / client / umount.cifs.c
blob18dbc3b1cf660722611cef8b190296c191f249d5
1 /*
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. */
19 #ifndef _GNU_SOURCE
20 #define _GNU_SOURCE
21 #endif
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 <fcntl.h>
32 #include <getopt.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <mntent.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 ""
42 #endif
44 #ifndef MNT_DETACH
45 #define MNT_DETACH 0x02
46 #endif
48 #ifndef MNT_EXPIRE
49 #define MNT_EXPIRE 0x04
50 #endif
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' },
65 { NULL, 0, NULL, 0 }
68 char * thisprogram;
69 int verboseflg = 0;
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)
90 int fileid;
91 int rc;
93 /* presumably can not chdir into the target as we do on mount */
95 fileid = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0);
96 if(fileid == -1) {
97 if(verboseflg)
98 printf("error opening mountpoint %d %s",errno,strerror(errno));
99 return errno;
102 rc = ioctl(fileid, CIFS_IOC_CHECKUMOUNT, NULL);
104 if(verboseflg)
105 printf("ioctl returned %d with errno %d %s\n",rc,errno,strerror(errno));
107 if(rc == ENOTTY)
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);
109 else if (rc > 0)
110 printf("user unmount of %s failed with %d %s",dir,errno,strerror(errno));
111 close(fileid);
113 return rc;
116 int main(int argc, char ** argv)
118 int c;
119 int rc;
120 int flags = 0;
121 int nomtab = 0;
122 int retry_remount = 0;
123 struct mntent mountent;
124 char * mountpoint;
125 FILE * pmntfile;
127 if(argc && argv) {
128 thisprogram = argv[0];
129 } else {
130 umount_cifs_usage();
131 return -EINVAL;
134 if(argc < 2) {
135 umount_cifs_usage();
136 return -EINVAL;
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) {
146 switch (c) {
147 /* No code to do the following option yet */
148 /* case 'a':
149 ++umount_all;
150 break; */
151 case '?':
152 case 'h': /* help */
153 umount_cifs_usage();
154 exit(1);
155 case 'n':
156 ++nomtab;
157 break;
158 case 'f':
159 flags |= MNT_FORCE;
160 break;
161 case 'l':
162 flags |= MNT_DETACH; /* lazy unmount */
163 break;
164 case 'e':
165 flags |= MNT_EXPIRE; /* gradually timeout */
166 break;
167 case 'r':
168 ++retry_remount;
169 break;
170 case 'v':
171 ++verboseflg;
172 break;
173 case 'V':
174 printf ("umount.cifs version: %s.%s%s\n",
175 UNMOUNT_CIFS_VERSION_MAJOR,
176 UNMOUNT_CIFS_VERSION_MINOR,
177 UNMOUNT_CIFS_VENDOR_SUFFIX);
178 exit (0);
179 default:
180 printf("unknown unmount option %c\n",c);
181 umount_cifs_usage();
182 exit(1);
186 /* move past the umount options */
187 argv += optind;
188 argc -= optind;
190 mountpoint = argv[0];
192 if((argc < 1) || (argv[0] == NULL)) {
193 printf("\nMissing name of unmount directory\n");
194 umount_cifs_usage();
195 return -EINVAL;
198 if(verboseflg)
199 printf("optind %d unmount dir %s\n",optind, mountpoint);
201 /* check if running effectively root */
202 if(geteuid() != 0)
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);
209 if (rc) {
210 return rc;
213 if(umount2(mountpoint, flags)) {
214 /* remember to kill daemon on error */
216 switch (errno) {
217 case 0:
218 printf("mount failed but no error number set\n");
219 break;
220 default:
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");
225 return -1;
226 } else {
227 pmntfile = setmntent(MOUNTED, "a+");
228 if(pmntfile) {
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");
238 else
239 strcat(mountent.mnt_opts,"rw");
240 if(flags & MS_MANDLOCK)
241 strcat(mountent.mnt_opts,",mand");
242 else
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");
248 if(flags & MS_NODEV)
249 strcat(mountent.mnt_opts,",nodev");
250 if(flags & MS_SYNCHRONOUS)
251 strcat(mountent.mnt_opts,",synch");
252 if(mount_user) {
253 if(getuid() != 0) {
254 strcat(mountent.mnt_opts,",user=");
255 strcat(mountent.mnt_opts,mount_user);
257 free(mount_user);
260 mountent.mnt_freq = 0;
261 mountent.mnt_passno = 0;
262 rc = addmntent(pmntfile,&mountent);
263 endmntent(pmntfile);
264 if(mountent.mnt_opts)
265 free(mountent.mnt_opts);*/
266 } else {
267 printf("could not update mount table\n");
271 return 0;