r13697: Remove unneeded header (header not present on all Linux either) for umount...
[Samba/bjacke.git] / source / client / umount.cifs.c
blobe6dd08dbf2859f0e76f0ff57a4f6e85361578ab1
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 <sys/vfs.h>
32 #include <fcntl.h>
33 #include <getopt.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <mntent.h>
38 #define UNMOUNT_CIFS_VERSION_MAJOR "0"
39 #define UNMOUNT_CIFS_VERSION_MINOR "5"
41 #ifndef UNMOUNT_CIFS_VENDOR_SUFFIX
42 #define UNMOUNT_CIFS_VENDOR_SUFFIX ""
43 #endif
45 #ifndef MNT_DETACH
46 #define MNT_DETACH 0x02
47 #endif
49 #ifndef MNT_EXPIRE
50 #define MNT_EXPIRE 0x04
51 #endif
53 #ifndef MOUNTED_LOCK
54 #define MOUNTED_LOCK "/etc/mtab~"
55 #endif
56 #ifndef MOUNTED_TEMP
57 #define MOUNTED_TEMP "/etc/mtab.tmp"
58 #endif
60 #define CIFS_IOC_CHECKUMOUNT _IO(0xCF, 2)
61 #define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDU */
63 static struct option longopts[] = {
64 { "all", 0, NULL, 'a' },
65 { "help",0, NULL, 'h' },
66 { "read-only", 0, NULL, 'r' },
67 { "ro", 0, NULL, 'r' },
68 { "verbose", 0, NULL, 'v' },
69 { "version", 0, NULL, 'V' },
70 { "expire", 0, NULL, 'e' },
71 { "force", 0, 0, 'f' },
72 { "lazy", 0, 0, 'l' },
73 { "no-mtab", 0, 0, 'n' },
74 { NULL, 0, NULL, 0 }
77 char * thisprogram;
78 int verboseflg = 0;
80 static void umount_cifs_usage(void)
82 printf("\nUsage: %s <remotetarget> <dir>\n", thisprogram);
83 printf("\nUnmount the specified directory\n");
84 printf("\nLess commonly used options:");
85 printf("\n\t-r\tIf mount fails, retry with readonly remount.");
86 printf("\n\t-n\tDo not write to mtab.");
87 printf("\n\t-f\tAttempt a forced unmount, even if the fs is busy.");
88 printf("\n\t-l\tAttempt lazy unmount, Unmount now, cleanup later.");
89 printf("\n\t-v\tEnable verbose mode (may be useful for debugging).");
90 printf("\n\t-h\tDisplay this help.");
91 printf("\n\nOptions are described in more detail in the manual page");
92 printf("\n\tman 8 umount.cifs\n");
93 printf("\nTo display the version number of the cifs umount utility:");
94 printf("\n\t%s -V\n",thisprogram);
95 printf("\nInvoking the umount utility on cifs mounts, can execute");
96 printf(" /sbin/umount.cifs (if present and umount -i is not specified.\n");
99 static int umount_check_perm(char * dir)
101 int fileid;
102 int rc;
104 /* allow root to unmount, no matter what */
105 if(getuid() == 0)
106 return 0;
108 /* presumably can not chdir into the target as we do on mount */
109 fileid = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0);
110 if(fileid == -1) {
111 if(verboseflg)
112 printf("error opening mountpoint %d %s",errno,strerror(errno));
113 return errno;
116 rc = ioctl(fileid, CIFS_IOC_CHECKUMOUNT, NULL);
118 if(verboseflg)
119 printf("ioctl returned %d with errno %d %s\n",rc,errno,strerror(errno));
121 if(rc == ENOTTY) {
122 printf("user unmounting via %s is an optional feature of",thisprogram);
123 printf(" the cifs filesystem driver (cifs.ko)");
124 printf("\n\tand requires cifs.ko version 1.32 or later\n");
125 } else if (rc > 0)
126 printf("user unmount of %s failed with %d %s\n",dir,errno,strerror(errno));
127 close(fileid);
129 return rc;
132 int lock_mtab(void)
134 int rc;
136 rc = mknod(MOUNTED_LOCK , 0600, 0);
137 if(rc == -1)
138 printf("\ngetting lock file %s failed with %s\n",MOUNTED_LOCK,
139 strerror(errno));
141 return rc;
145 void unlock_mtab(void)
147 unlink(MOUNTED_LOCK);
150 int remove_from_mtab(char * mountpoint)
152 int rc;
153 int num_matches;
154 FILE * org_fd;
155 FILE * new_fd;
156 struct mntent * mount_entry;
158 /* Do we need to check if it is a symlink to e.g. /proc/mounts
159 in which case we probably do not want to update it? */
161 /* Do we first need to check if it is writable? */
163 if (lock_mtab()) {
164 printf("Mount table locked\n");
165 return -EACCES;
168 if(verboseflg)
169 printf("attempting to remove from mtab\n");
171 org_fd = setmntent(MOUNTED, "r");
173 if(org_fd == NULL) {
174 printf("Can not open %s\n",MOUNTED);
175 unlock_mtab();
176 return -EIO;
179 new_fd = setmntent(MOUNTED_TEMP,"w");
180 if(new_fd == NULL) {
181 printf("Can not open temp file %s", MOUNTED_TEMP);
182 endmntent(org_fd);
183 unlock_mtab();
184 return -EIO;
187 /* BB fix so we only remove the last entry that matches BB */
188 num_matches = 0;
189 while((mount_entry = getmntent(org_fd)) != NULL) {
190 if(strcmp(mount_entry->mnt_dir, mountpoint) == 0) {
191 num_matches++;
194 if(verboseflg)
195 printf("%d matching entries in mount table\n", num_matches);
197 /* Is there a better way to seek back to the first entry in mtab? */
198 endmntent(org_fd);
199 org_fd = setmntent(MOUNTED, "r");
201 if(org_fd == NULL) {
202 printf("Can not open %s\n",MOUNTED);
203 unlock_mtab();
204 return -EIO;
207 while((mount_entry = getmntent(org_fd)) != NULL) {
208 if(strcmp(mount_entry->mnt_dir, mountpoint) != 0) {
209 addmntent(new_fd, mount_entry);
210 } else {
211 if(num_matches != 1) {
212 addmntent(new_fd, mount_entry);
213 num_matches--;
214 } else if(verboseflg)
215 printf("entry not copied (ie entry is removed)\n");
219 if(verboseflg)
220 printf("done updating tmp file\n");
221 rc = fchmod (fileno (new_fd), S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
222 if(rc < 0) {
223 printf("error %s changing mode of %s\n", strerror(errno),
224 MOUNTED_TEMP);
226 endmntent(new_fd);
228 rc = rename(MOUNTED_TEMP, MOUNTED);
230 if(rc < 0) {
231 printf("failure %s renaming %s to %s\n",strerror(errno),
232 MOUNTED_TEMP, MOUNTED);
233 unlock_mtab();
234 return -EIO;
237 unlock_mtab();
239 return rc;
242 int main(int argc, char ** argv)
244 int c;
245 int rc;
246 int flags = 0;
247 int nomtab = 0;
248 int retry_remount = 0;
249 struct statfs statbuf;
250 char * mountpoint;
252 if(argc && argv) {
253 thisprogram = argv[0];
254 } else {
255 umount_cifs_usage();
256 return -EINVAL;
259 if(argc < 2) {
260 umount_cifs_usage();
261 return -EINVAL;
264 if(thisprogram == NULL)
265 thisprogram = "umount.cifs";
267 /* add sharename in opts string as unc= parm */
269 while ((c = getopt_long (argc, argv, "afhilnrvV",
270 longopts, NULL)) != -1) {
271 switch (c) {
272 /* No code to do the following option yet */
273 /* case 'a':
274 ++umount_all;
275 break; */
276 case '?':
277 case 'h': /* help */
278 umount_cifs_usage();
279 exit(1);
280 case 'n':
281 ++nomtab;
282 break;
283 case 'f':
284 flags |= MNT_FORCE;
285 break;
286 case 'l':
287 flags |= MNT_DETACH; /* lazy unmount */
288 break;
289 case 'e':
290 flags |= MNT_EXPIRE; /* gradually timeout */
291 break;
292 case 'r':
293 ++retry_remount;
294 break;
295 case 'v':
296 ++verboseflg;
297 break;
298 case 'V':
299 printf ("umount.cifs version: %s.%s%s\n",
300 UNMOUNT_CIFS_VERSION_MAJOR,
301 UNMOUNT_CIFS_VERSION_MINOR,
302 UNMOUNT_CIFS_VENDOR_SUFFIX);
303 exit (0);
304 default:
305 printf("unknown unmount option %c\n",c);
306 umount_cifs_usage();
307 exit(1);
311 /* move past the umount options */
312 argv += optind;
313 argc -= optind;
315 mountpoint = argv[0];
317 if((argc < 1) || (argv[0] == NULL)) {
318 printf("\nMissing name of unmount directory\n");
319 umount_cifs_usage();
320 return -EINVAL;
323 if(verboseflg)
324 printf("optind %d unmount dir %s\n",optind, mountpoint);
326 /* check if running effectively root */
327 if(geteuid() != 0) {
328 printf("Trying to unmount when %s not installed suid\n",thisprogram);
329 if(verboseflg)
330 printf("euid = %d\n",geteuid());
331 return -EACCES;
334 /* fixup path if needed */
336 /* make sure that this is a cifs filesystem */
337 rc = statfs(mountpoint, &statbuf);
339 if(rc || (statbuf.f_type != CIFS_MAGIC_NUMBER)) {
340 printf("This utility only unmounts cifs filesystems.\n");
341 return -EINVAL;
344 /* check if our uid was the one who mounted */
345 rc = umount_check_perm(mountpoint);
346 if (rc) {
347 printf("Not permitted to unmount\n");
348 return rc;
351 if(umount2(mountpoint, flags)) {
352 /* remember to kill daemon on error */
353 switch (errno) {
354 case 0:
355 printf("unmount failed but no error number set\n");
356 break;
357 default:
358 printf("unmount error %d = %s\n",errno,strerror(errno));
360 printf("Refer to the umount.cifs(8) manual page (man 8 umount.cifs)\n");
361 return -1;
362 } else {
363 if(verboseflg)
364 printf("umount2 succeeded\n");
365 if(nomtab == 0)
366 remove_from_mtab(mountpoint);
369 return 0;