client/umount.cifs.c

説明を見る。
00001 /* 
00002    Unmount utility program for Linux CIFS VFS (virtual filesystem) client
00003    Copyright (C) 2005 Steve French  (sfrench@us.ibm.com)
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 2 of the License, or
00008    (at your option) any later version.
00009    
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014    
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software
00017    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
00018 
00019 #ifndef _GNU_SOURCE
00020 #define _GNU_SOURCE
00021 #endif
00022 
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <unistd.h>
00026 #include <ctype.h>
00027 #include <sys/types.h>
00028 #include <sys/mount.h>
00029 #include <sys/ioctl.h>
00030 #include <sys/stat.h>
00031 #include <sys/vfs.h>
00032 #include <fcntl.h>
00033 #include <getopt.h>
00034 #include <errno.h>
00035 #include <string.h>
00036 #include <mntent.h>
00037 
00038 #define UNMOUNT_CIFS_VERSION_MAJOR "0"
00039 #define UNMOUNT_CIFS_VERSION_MINOR "5"
00040 
00041 #ifndef UNMOUNT_CIFS_VENDOR_SUFFIX
00042  #ifdef _SAMBA_BUILD_
00043   #include "include/version.h"
00044   #ifdef SAMBA_VERSION_VENDOR_SUFFIX
00045    #define UNMOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING"-"SAMBA_VERSION_VENDOR_SUFFIX
00046   #else
00047    #define UNMOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING
00048   #endif /* SAMBA_VERSION_OFFICIAL_STRING and SAMBA_VERSION_VENDOR_SUFFIX */
00049  #else
00050   #define UNMOUNT_CIFS_VENDOR_SUFFIX ""
00051  #endif /* _SAMBA_BUILD_ */
00052 #endif /* UNMOUNT_CIFS_VENDOR_SUFFIX */
00053 
00054 #ifndef MNT_DETACH
00055 #define MNT_DETACH 0x02
00056 #endif
00057 
00058 #ifndef MNT_EXPIRE
00059 #define MNT_EXPIRE 0x04
00060 #endif
00061 
00062 #ifndef MOUNTED_LOCK
00063 #define MOUNTED_LOCK    "/etc/mtab~"
00064 #endif
00065 #ifndef MOUNTED_TEMP
00066 #define MOUNTED_TEMP    "/etc/mtab.tmp"
00067 #endif
00068 
00069 #define CIFS_IOC_CHECKUMOUNT _IO(0xCF, 2)
00070 #define CIFS_MAGIC_NUMBER 0xFF534D42   /* the first four bytes of SMB PDU */
00071    
00072 static struct option longopts[] = {
00073         { "all", 0, NULL, 'a' },
00074         { "help",0, NULL, 'h' },
00075         { "read-only", 0, NULL, 'r' },
00076         { "ro", 0, NULL, 'r' },
00077         { "verbose", 0, NULL, 'v' },
00078         { "version", 0, NULL, 'V' },
00079         { "expire", 0, NULL, 'e' },
00080         { "force", 0, 0, 'f' },
00081         { "lazy", 0, 0, 'l' },
00082         { "no-mtab", 0, 0, 'n' },
00083         { NULL, 0, NULL, 0 }
00084 };
00085 
00086 const char * thisprogram;
00087 int verboseflg = 0;
00088 
00089 static void umount_cifs_usage(void)
00090 {
00091         printf("\nUsage:  %s <remotetarget> <dir>\n", thisprogram);
00092         printf("\nUnmount the specified directory\n");
00093         printf("\nLess commonly used options:");
00094         printf("\n\t-r\tIf mount fails, retry with readonly remount.");
00095         printf("\n\t-n\tDo not write to mtab.");
00096         printf("\n\t-f\tAttempt a forced unmount, even if the fs is busy.");
00097         printf("\n\t-l\tAttempt lazy unmount, Unmount now, cleanup later.");
00098         printf("\n\t-v\tEnable verbose mode (may be useful for debugging).");
00099         printf("\n\t-h\tDisplay this help.");
00100         printf("\n\nOptions are described in more detail in the manual page");
00101         printf("\n\tman 8 umount.cifs\n");
00102         printf("\nTo display the version number of the cifs umount utility:");
00103         printf("\n\t%s -V\n",thisprogram);
00104         printf("\nInvoking the umount utility on cifs mounts, can execute");
00105         printf(" /sbin/umount.cifs (if present and umount -i is not specified.\n");
00106 }
00107 
00108 static int umount_check_perm(char * dir)
00109 {
00110         int fileid;
00111         int rc;
00112 
00113         /* allow root to unmount, no matter what */
00114         if(getuid() == 0)
00115                 return 0;
00116 
00117         /* presumably can not chdir into the target as we do on mount */
00118         fileid = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0);
00119         if(fileid == -1) {
00120                 if(verboseflg)
00121                         printf("error opening mountpoint %d %s",errno,strerror(errno));
00122                 return errno;
00123         }
00124 
00125         rc = ioctl(fileid, CIFS_IOC_CHECKUMOUNT, NULL);
00126 
00127         if(verboseflg)
00128                 printf("ioctl returned %d with errno %d %s\n",rc,errno,strerror(errno));
00129 
00130         if(rc == ENOTTY) {
00131                 printf("user unmounting via %s is an optional feature of",thisprogram);
00132                 printf(" the cifs filesystem driver (cifs.ko)");
00133                 printf("\n\tand requires cifs.ko version 1.32 or later\n");
00134         } else if (rc != 0)
00135                 printf("user unmount of %s failed with %d %s\n",dir,errno,strerror(errno));
00136         close(fileid);
00137 
00138         return rc;
00139 }
00140 
00141 static int lock_mtab(void)
00142 {
00143         int rc;
00144         
00145         rc = mknod(MOUNTED_LOCK , 0600, 0);
00146         if(rc == -1)
00147                 printf("\ngetting lock file %s failed with %s\n",MOUNTED_LOCK,
00148                                 strerror(errno));
00149                 
00150         return rc;      
00151         
00152 }
00153 
00154 static void unlock_mtab(void)
00155 {
00156         unlink(MOUNTED_LOCK);   
00157 }
00158 
00159 static int remove_from_mtab(char * mountpoint)
00160 {
00161         int rc;
00162         int num_matches;
00163         FILE * org_fd;
00164         FILE * new_fd;
00165         struct mntent * mount_entry;
00166 
00167         /* Do we need to check if it is a symlink to e.g. /proc/mounts
00168         in which case we probably do not want to update it? */
00169 
00170         /* Do we first need to check if it is writable? */ 
00171 
00172         if (lock_mtab()) {
00173                 printf("Mount table locked\n");
00174                 return -EACCES;
00175         }
00176         
00177         if(verboseflg)
00178                 printf("attempting to remove from mtab\n");
00179 
00180         org_fd = setmntent(MOUNTED, "r");
00181 
00182         if(org_fd == NULL) {
00183                 printf("Can not open %s\n",MOUNTED);
00184                 unlock_mtab();
00185                 return -EIO;
00186         }
00187 
00188         new_fd = setmntent(MOUNTED_TEMP,"w");
00189         if(new_fd == NULL) {
00190                 printf("Can not open temp file %s", MOUNTED_TEMP);
00191                 endmntent(org_fd);
00192                 unlock_mtab();
00193                 return -EIO;
00194         }
00195 
00196         /* BB fix so we only remove the last entry that matches BB */
00197         num_matches = 0;
00198         while((mount_entry = getmntent(org_fd)) != NULL) {
00199                 if(strcmp(mount_entry->mnt_dir, mountpoint) == 0) {
00200                         num_matches++;
00201                 }
00202         }       
00203         if(verboseflg)
00204                 printf("%d matching entries in mount table\n", num_matches);
00205                 
00206         /* Is there a better way to seek back to the first entry in mtab? */
00207         endmntent(org_fd);
00208         org_fd = setmntent(MOUNTED, "r");
00209 
00210         if(org_fd == NULL) {
00211                 printf("Can not open %s\n",MOUNTED);
00212                 unlock_mtab();
00213                 return -EIO;
00214         }
00215         
00216         while((mount_entry = getmntent(org_fd)) != NULL) {
00217                 if(strcmp(mount_entry->mnt_dir, mountpoint) != 0) {
00218                         addmntent(new_fd, mount_entry);
00219                 } else {
00220                         if(num_matches != 1) {
00221                                 addmntent(new_fd, mount_entry);
00222                                 num_matches--;
00223                         } else if(verboseflg)
00224                                 printf("entry not copied (ie entry is removed)\n");
00225                 }
00226         }
00227 
00228         if(verboseflg)
00229                 printf("done updating tmp file\n");
00230         rc = fchmod (fileno (new_fd), S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
00231         if(rc < 0) {
00232                 printf("error %s changing mode of %s\n", strerror(errno),
00233                         MOUNTED_TEMP);
00234         }
00235         endmntent(new_fd);
00236 
00237         rc = rename(MOUNTED_TEMP, MOUNTED);
00238 
00239         if(rc < 0) {
00240                 printf("failure %s renaming %s to %s\n",strerror(errno),
00241                         MOUNTED_TEMP, MOUNTED);
00242                 unlock_mtab();
00243                 return -EIO;
00244         }
00245 
00246         unlock_mtab();
00247         
00248         return rc;
00249 }
00250 
00251 int main(int argc, char ** argv)
00252 {
00253         int c;
00254         int rc;
00255         int flags = 0;
00256         int nomtab = 0;
00257         int retry_remount = 0;
00258         struct statfs statbuf;
00259         char * mountpoint;
00260 
00261         if(argc && argv) {
00262                 thisprogram = argv[0];
00263         } else {
00264                 umount_cifs_usage();
00265                 return -EINVAL;
00266         }
00267 
00268         if(argc < 2) {
00269                 umount_cifs_usage();
00270                 return -EINVAL;
00271         }
00272 
00273         if(thisprogram == NULL)
00274                 thisprogram = "umount.cifs";
00275 
00276         /* add sharename in opts string as unc= parm */
00277 
00278         while ((c = getopt_long (argc, argv, "afhilnrvV",
00279                          longopts, NULL)) != -1) {
00280                 switch (c) {
00281 /* No code to do the following  option yet */
00282 /*              case 'a':              
00283                         ++umount_all;
00284                         break; */
00285                 case '?':
00286                 case 'h':   /* help */
00287                         umount_cifs_usage();
00288                         exit(1);
00289                 case 'n':
00290                         ++nomtab;
00291                         break;
00292                 case 'f':
00293                         flags |= MNT_FORCE;
00294                         break;
00295                 case 'l':
00296                         flags |= MNT_DETACH; /* lazy unmount */
00297                         break;
00298                 case 'e':
00299                         flags |= MNT_EXPIRE; /* gradually timeout */
00300                         break;
00301                 case 'r':
00302                         ++retry_remount;
00303                         break;
00304                 case 'v':
00305                         ++verboseflg;
00306                         break;
00307                 case 'V':          
00308                         printf ("umount.cifs version: %s.%s%s\n",
00309                                 UNMOUNT_CIFS_VERSION_MAJOR,
00310                                 UNMOUNT_CIFS_VERSION_MINOR,
00311                                 UNMOUNT_CIFS_VENDOR_SUFFIX);
00312                         exit (0);
00313                 default:
00314                         printf("unknown unmount option %c\n",c);
00315                         umount_cifs_usage();
00316                         exit(1);
00317                 }
00318         }
00319 
00320         /* move past the umount options */
00321         argv += optind;
00322         argc -= optind;
00323 
00324         mountpoint = argv[0];
00325 
00326         if((argc < 1) || (argv[0] == NULL)) {
00327                 printf("\nMissing name of unmount directory\n");
00328                 umount_cifs_usage();
00329                 return -EINVAL;
00330         }
00331 
00332         if(verboseflg)
00333                 printf("optind %d unmount dir %s\n",optind, mountpoint);
00334 
00335         /* check if running effectively root */
00336         if(geteuid() != 0) {
00337                 printf("Trying to unmount when %s not installed suid\n",thisprogram);
00338                 if(verboseflg)
00339                         printf("euid = %d\n",geteuid());
00340                 return -EACCES;
00341         }
00342 
00343         /* fixup path if needed */
00344 
00345         /* Trim any trailing slashes */
00346         while ((strlen(mountpoint) > 1) &&
00347                 (mountpoint[strlen(mountpoint)-1] == '/'))
00348         {
00349                 mountpoint[strlen(mountpoint)-1] = '\0';
00350         }
00351 
00352         /* make sure that this is a cifs filesystem */
00353         rc = statfs(mountpoint, &statbuf);
00354         
00355         if(rc || (statbuf.f_type != CIFS_MAGIC_NUMBER)) {
00356                 printf("This utility only unmounts cifs filesystems.\n");
00357                 return -EINVAL;
00358         }
00359 
00360         /* check if our uid was the one who mounted */
00361         rc = umount_check_perm(mountpoint);
00362         if (rc) {
00363                 printf("Not permitted to unmount\n");
00364                 return rc;
00365         }
00366 
00367         if(umount2(mountpoint, flags)) {
00368         /* remember to kill daemon on error */
00369                 switch (errno) {
00370                 case 0:
00371                         printf("unmount failed but no error number set\n");
00372                         break;
00373                 default:
00374                         printf("unmount error %d = %s\n",errno,strerror(errno));
00375                 }
00376                 printf("Refer to the umount.cifs(8) manual page (man 8 umount.cifs)\n");
00377                 return -1;
00378         } else {
00379                 if(verboseflg)
00380                         printf("umount2 succeeded\n");
00381                 if(nomtab == 0)
00382                         remove_from_mtab(mountpoint);
00383         }
00384 
00385         return 0;
00386 }
00387 

Sambaに対してSat Aug 29 21:22:53 2009に生成されました。  doxygen 1.4.7