"Fossies" - the Fresh Open Source Software Archive

Member "zuluCrypt-5.7.0/zuluCrypt-cli/lib/unmount_volume.c" (12 Sep 2019, 4988 Bytes) of package /linux/misc/zuluCrypt-5.7.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "unmount_volume.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.6.0_vs_5.7.0.

    1 /*
    2  *
    3  *  Copyright (c) 2011-2015
    4  *  name : Francis Banyikwa
    5  *  email: mhogomchungu@gmail.com
    6  *  This program is free software: you can redistribute it and/or modify
    7  *  it under the terms of the GNU General Public License as published by
    8  *  the Free Software Foundation, either version 2 of the License, or
    9  *  (at your option) any later version.
   10  *
   11  *  This program is distributed in the hope that it will be useful,
   12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  *  GNU General Public License for more details.
   15  *
   16  *  You should have received a copy of the GNU General Public License
   17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18  */
   19 
   20 #include "includes.h"
   21 
   22 #include <mntent.h>
   23 #include <sys/mount.h>
   24 #include <stdlib.h>
   25 #include <unistd.h>
   26 #include <sys/types.h>
   27 #include <sys/stat.h>
   28 #include <stdio.h>
   29 
   30 static int _unmount_rest( const char * m_dir )
   31 {
   32     return umount( m_dir ) ;
   33 }
   34 
   35 static int _unmount_fuse( const char * m_dir )
   36 {
   37     /*
   38      * ZULUCRYPTumount is defined in ../constants.h
   39      */
   40     return ProcessExecute( ZULUCRYPTumount,m_dir,NULL ) ;
   41 }
   42 
   43 static int _unmount( int( *function )( const char * m_dir ),const char * m_dir )
   44 {
   45     int h ;
   46     int i ;
   47 
   48     /*
   49      * try 5 times on one second intervals to umount the volume.
   50      * Trying to unmount more than once seem to be necessary sometimes
   51      */
   52     for( i = 0 ; i < 5 ; i++ ){
   53 
   54         h = function( m_dir ) ;
   55 
   56         if( h == 0 ){
   57 
   58             break ;
   59         }else{
   60             sleep( 1 ) ;
   61         }
   62     }
   63 
   64     return h ;
   65 }
   66 
   67 static int _zuluCryptUnmountVolume_0( string_t st,char ** m_point,char ** m_point_2 )
   68 {
   69     int h ;
   70 
   71     stringList_t stl = StringListStringSplit( st,' ' ) ;
   72 
   73     StringListIterator it = StringListBegin( stl ) ;
   74 
   75     /*
   76      * zuluCryptDecodeMountEntry() is defined in mount_volume.c
   77      */
   78     const char * mout_point = zuluCryptDecodeMountEntry( *( it + 1 ) ) ;
   79 
   80     if( StringContains( *( it + 2 ),"fuse" ) ){
   81 
   82         /*
   83          * Dont know whats going on but FUSE based file systems do not seem to work with umount()
   84          */
   85         h = _unmount( _unmount_fuse,mout_point ) ;
   86     }else{
   87         h = _unmount( _unmount_rest,mout_point ) ;
   88     }
   89 
   90     if( h == 0 && m_point != NULL ){
   91 
   92         *m_point = StringCopy_2( mout_point ) ;
   93     }
   94 
   95     if( m_point_2 != NULL ){
   96 
   97         *m_point_2 = StringCopy_2( mout_point ) ;
   98     }
   99 
  100     StringListDelete( &stl ) ;
  101 
  102     return h ;
  103 }
  104 
  105 static stringList_t _get_mount_entries( const char * device )
  106 {
  107     /*
  108      * zuluCryptGetMountEntry() is defined in mountinfo.c
  109      */
  110     stringList_t stl = zuluCryptGetMoutedList() ;
  111 
  112     StringListIterator it ;
  113     StringListIterator end ;
  114 
  115     string_t st = String_1( device," ",NULL ) ;
  116 
  117     StringListGetIterators( stl,&it,&end ) ;
  118 
  119     while( it != end ){
  120 
  121         if( StringStartsWith_1( *it,st ) ){
  122 
  123             it++ ;
  124         }else{
  125             StringListRemoveAt_1( stl,it,&end ) ;
  126         }
  127     }
  128 
  129     StringDelete( &st ) ;
  130 
  131     return stl ;
  132 }
  133 
  134 int zuluCryptUnmountVolume( const char * device,char ** m_point )
  135 {
  136     int h = 3 ;
  137 
  138     char * e ;
  139 
  140     size_t s ;
  141 
  142     stringList_t stl ;
  143 
  144     StringListIterator it ;
  145     StringListIterator end ;
  146 
  147     ssize_t r ;
  148 
  149     string_t st ;
  150     string_t xt ;
  151 
  152     if( zuluCryptNoPartitionLoopDevice( device ) ){
  153 
  154         /*
  155          * zuluCryptLoopDeviceAddress() is defined in create_loop_device.c
  156          */
  157 
  158         e = zuluCryptLoopDeviceAddress( device ) ;
  159 
  160         if( e == NULL ){
  161 
  162             return h ;
  163         }else{
  164             stl = _get_mount_entries( e ) ;
  165             StringFree( e ) ;
  166         }
  167     }else{
  168         stl = _get_mount_entries( device ) ;
  169     }
  170 
  171     s = StringListSize( stl ) ;
  172 
  173     if( s == 0 ){
  174 
  175         /*
  176          * volume appear to not be mounted.
  177          */
  178 
  179     }else if( s == 1 ){
  180 
  181         /*
  182          * there is only one mount point for the volume,unmount it normally
  183          */
  184 
  185         h = _zuluCryptUnmountVolume_0( StringListStringAtFirstPlace( stl ),m_point,NULL ) ;
  186     }else{
  187         /*
  188          * There are multiple mount points for the same volume.
  189          *
  190          * Try to figure out which one among the mount points is ours and then try
  191          * first to unmount the rest of them.
  192          */
  193 
  194         r = StringListHasSequence( stl," /run/media/private/" ) ;
  195 
  196         if( r == -1 ){
  197 
  198             /*
  199              * Probable reason for getting here is if a user use a home mount point path,
  200              * we dont know the path because we dont know the user we are serving
  201              * and hence we bail out with an error.
  202              */
  203             h = 10 ;
  204         }else{
  205             /*
  206              * We got our mount point,take it out of the list to use it last
  207              */
  208             st = StringListDetachAt( stl,r ) ;
  209 
  210             StringListGetIterators( stl,&it,&end ) ;
  211 
  212             while( it != end ){
  213 
  214                 xt = *it ;
  215 
  216                 it++ ;
  217 
  218                 if( _zuluCryptUnmountVolume_0( xt,NULL,&e ) != 0 ){
  219 
  220                     printf( "Failed to unmount third party mount point: \"%s\"\n",e ) ;
  221 
  222                     StringFree( e ) ;
  223                     /*
  224                      * Failed to unmount one of the extra mount points,
  225                      * bail out with an error.
  226                      */
  227                     h = 10 ;
  228 
  229                     break ;
  230                 }
  231             }
  232 
  233             if( h != 10 ){
  234 
  235                 /*
  236                  * Attempt to unmount our mount point last.
  237                  */
  238                 h = _zuluCryptUnmountVolume_0( st,m_point,NULL ) ;
  239             }
  240 
  241             StringDelete( &st ) ;
  242         }
  243     }
  244 
  245     if( h != 0 && h != 3 && h != 4 && h != 1 && h != 10 ){
  246 
  247         h = 2 ;
  248     }
  249 
  250     StringListDelete( &stl ) ;
  251 
  252     return h ;
  253 }