1
/***************************************************************************
2
smb4k_umount - This is the unmount utility of Smb4K.
5
copyright : (C) 2004-2007 by Alexander Reinholdt
6
email : dustpuppy@users.berlios.de
7
***************************************************************************/
9
/***************************************************************************
10
* This program is free software; you can redistribute it and/or modify *
11
* it under the terms of the GNU General Public License as published by *
12
* the Free Software Foundation; either version 2 of the License, or *
13
* (at your option) any later version. *
15
* This program is distributed in the hope that it will be useful, but *
16
* WITHOUT ANY WARRANTY; without even the implied warranty of *
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
18
* General Public License for more details. *
20
* You should have received a copy of the GNU General Public License *
21
* along with this program; if not, write to the *
22
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
24
***************************************************************************/
31
#include <sys/statfs.h>
33
#include <sys/param.h>
34
#include <sys/mount.h>
38
#include <sys/utsname.h>
50
#define SMB4K_UMOUNT_VERSION "0.15"
55
cout << "This is smb4k_umount (version " << SMB4K_UMOUNT_VERSION << "), the unmount utility of Smb4K" << endl;
56
cout << "Copyright (C) 2004-2008, Alexander Reinholdt" << endl;
58
cout << "Usage:" << endl;
60
cout << " smb4k_umount {mode} {options} {mountpoint}" << endl;
62
cout << " smb4k_umount {mountpoint}" << endl;
64
cout << " smb4k_umount --help" << endl;
65
cout << " smb4k_umount --version" << endl;
67
cout << "Arguments:" << endl;
68
cout << " {mode}" << endl;
70
cout << " --no-suid\tsmb4k_umount is run in normal user mode, so smbumount or" << endl;
71
cout << "\t\tumount.cifs is invoked." << endl;
72
cout << " --suid\tsmb4k_umount is run in super user mode, so umount is invoked." << endl;
73
cout << " -n\t\tThe same as the '--no-suid' argument." << endl;
74
cout << " -s\t\tThe same as the '--suid' argument." << endl;
76
cout << " {options}" << endl;
77
cout << " -t <fs>\tThe file system that should be used for unmounting. Only 'smbfs'" << endl;
78
cout << "\t\tand 'cifs' are supported. All other file systems will result in" << endl;
79
cout << "\t\tan error. Please note, that this argument is mandatory." << endl;
82
cout << " -l\t\tPerform a lazy unmount. See the manual page of umount for" << endl;
83
cout << "\t\tmore information. Please note, that this argument is only" << endl;
84
cout << "\t\trecognized in super user mode, i.e. if '-s' or '--suid' is" << endl;
85
cout << "\t\t supplied, and that you need kernel version 2.4.11 or later." << endl;
88
cout << " {mountpoint}\tThe path where the share is mounted." << endl;
90
cout << " --help\tDisplay this help screen and exit." << endl;
91
cout << " --version\tDisplay the version information and exit." << endl;
98
cout << "Version " << SMB4K_UMOUNT_VERSION << endl;
102
bool find_program( const char *name, char *path )
104
const char *paths[] = { "/bin/", "/sbin/", "/usr/bin/", "/usr/sbin/", "/usr/local/bin/", "/usr/local/sbin/" };
107
for ( uint i = 0; i < sizeof( paths ) / sizeof( char * ); i++ )
109
string p( paths[i] );
112
if ( access( p.c_str(), X_OK ) == 0 )
119
if ( !strcmp( file.c_str(), "" ) )
121
cerr << "smb4k_umount: Could not find " << name << " binary" << endl;
126
int len = strlen( file.c_str() ) + 1;
127
strncpy( path, file.c_str(), len );
134
bool check_filesystem( const char *path, const char *fs )
138
struct statfs filesystem;
140
if ( statfs( path, &filesystem ) == -1 )
142
int err_code = errno;
144
if ( err_code != EIO && err_code != EACCES )
147
cerr << "smb4k_umount: " << strerror( err_code ) << endl;
151
ok = true; // Bypass the check below, because it would yield ok == FALSE
152
// and we want to be able to unmount broken shares as well.
159
// First entry is for CIFS, the second for SMBFS.
160
if ( (uint)filesystem.f_type == 0xFF534D42 && !strncmp( fs, "cifs", strlen( fs )+1 ) )
164
else if ( (uint)filesystem.f_type == 0x517B && !strncmp( fs, "smbfs", strlen( fs )+1 ) )
169
if ( !strncmp( filesystem.f_fstypename, fs, strlen( fs ) ) )
176
// ok is still FALSE.
177
cerr << "smb4k_umount: Wrong file system specified" << endl;
184
int main( int argc, char *argv[], char *envp[] )
186
// First of all, set the locale
187
(void) setlocale( LC_ALL, "" );
192
exit( EXIT_FAILURE );
195
int new_argc = argc + 1;
196
char *new_argv[new_argc];
200
char *mountpoint = NULL;
202
char *filesystem = NULL;
203
bool normal_user_mode = true;
204
bool have_user_mode = false;
206
bool lazy_unmount = false;
210
// Get the options that were passed:
215
int option_index = 0;
217
static struct option long_options[] =
220
{ "version", 0, 0, 0 },
223
{ "no-suid", 0, 0, 0 },
228
c = getopt_long( argc, argv, "t:nsl", long_options, &option_index );
231
c = getopt_long( argc, argv, "t:ns", long_options, &option_index );
233
c = getopt_long( argc, argv, "", long_options, &option_index );
246
int len = strlen( long_options[option_index].name ) + 1;
249
(void) strncpy( opt, long_options[option_index].name, len );
252
if ( !strncmp( opt, "help", len ) )
256
exit( EXIT_SUCCESS );
258
else if ( !strncmp( opt, "version", len ) )
262
exit( EXIT_SUCCESS );
265
else if ( !strncmp( opt, "suid", len ) )
267
// Enter super user mode
269
normal_user_mode = false;
270
have_user_mode = true;
274
else if ( !strncmp( opt, "no-suid", len ) )
276
// Enter normal user mode
278
normal_user_mode = true;
279
have_user_mode = true;
294
// Get the length of the option argument:
295
int len = strlen( optarg ) + 1;
297
if ( strncmp( "smbfs", optarg, len) != 0 &&
298
strncmp( "cifs", optarg, len ) != 0 )
300
cerr << "smb4k_umount: File system " << optarg << " is not supported" << endl;
301
exit( EXIT_FAILURE );
306
filesystem = new char[len];
307
filesystem[0] = '\0';
308
(void) strncpy( filesystem, optarg, len );
309
filesystem[len-1] = '\0';
316
// Enter super user mode
317
normal_user_mode = false;
318
have_user_mode = true;
324
// Enter normal user mode
325
normal_user_mode = true;
326
have_user_mode = true;
334
// Initiate a lazy unmount. The umount binary
335
// will complain, if '-l' is not supported.
343
// Abort the program if an unknown option
345
exit( EXIT_FAILURE );
355
// Error out if no user mode was specified.
356
if ( !have_user_mode )
358
cerr << "smb4k_umount: No mode was specified" << endl;
359
exit( EXIT_FAILURE );
362
// Error out if the user did not specify any file system.
365
cerr << "smb4k_umount: No file system was specified" << endl;
366
exit( EXIT_FAILURE );
369
if ( normal_user_mode )
371
int len = strlen( filesystem ) + 1;
373
if ( !strncmp( "smbfs", filesystem, len ) )
375
if ( !find_program( "smbumount", path ) )
377
exit( EXIT_FAILURE );
382
if ( !find_program( "umount.cifs", path ) )
384
if ( !find_program( "umount", path ) )
386
exit( EXIT_FAILURE );
391
len = strlen( path ) + 1;
393
new_argv[index] = new char[len];
394
new_argv[index][0] = '\0';
395
(void) strncpy( new_argv[index], path, len );
396
new_argv[index][len-1] = '\0';
402
if ( !find_program( "umount", path ) )
404
exit( EXIT_FAILURE );
407
int len = strlen( path ) + 1;
409
new_argv[index] = new char[len];
410
new_argv[index][0] = '\0';
411
(void) strncpy( new_argv[index], path, len );
412
new_argv[index][len-1] = '\0';
416
len = strlen( "-t" ) + 1;
418
new_argv[index] = new char[len];
419
new_argv[index][0] = '\0';
420
(void) strncpy( new_argv[index], "-t", len );
421
new_argv[index][len-1] = '\0';
425
len = strlen( filesystem ) + 1;
427
new_argv[index] = new char[len];
428
new_argv[index][0] = '\0';
429
(void) strncpy( new_argv[index], filesystem, len );
430
new_argv[index][len-1] = '\0';
438
len = strlen( "-l" ) + 1;
440
new_argv[index] = new char[len];
441
new_argv[index][0] = '\0';
442
(void) strncpy( new_argv[index], "-l", len );
443
new_argv[index][len-1] = '\0';
450
// We do not need to care about the user mode and
451
// we also need not to check for the file system,
452
// since there is only one.
453
if ( !find_program( "umount", path ) )
455
exit( EXIT_FAILURE );
458
int length = strlen( path ) + 1;
459
new_argv[index] = new char[length];
460
new_argv[index][0] = '\0';
461
(void) strncpy( new_argv[index], path, length );
462
new_argv[index][length-1] = '\0';
467
// Add the mount point:
470
while ( optind < argc )
474
if ( argv[optind][0] != '\057' )
476
cerr << "smb4k_umount: Argument " << optind << " is not a mount point" << endl;
477
exit( EXIT_FAILURE );
480
if ( !check_filesystem( argv[optind], filesystem ) )
482
if ( !check_filesystem( argv[optind], "smbfs" ) )
485
// Error message is given by check_filesystem()
486
exit( EXIT_FAILURE );
489
int len = strlen( argv[optind] ) + 1;
491
mountpoint = new char[len];
492
mountpoint[0] = '\0';
493
(void) strncpy( mountpoint, argv[optind], len );
494
mountpoint[len-1] = '\0';
507
cerr << "smb4k_umount: No mount point was specified" << endl;
508
exit( EXIT_FAILURE );
512
int len = strlen( mountpoint ) + 1;
513
new_argv[index] = new char[len];
514
new_argv[index][0] = '\0';
515
(void) strncpy( new_argv[index], mountpoint, len );
516
new_argv[index][len-1] = '\0';
522
if ( index >= new_argc )
524
cerr << "smb4k_umount: There are too many arguments" << endl;
525
exit( EXIT_FAILURE );
528
// Terminate new_argv:
529
new_argv[index] = NULL;
532
if ( execve( new_argv[0], new_argv, envp ) == -1 )
535
cerr << "smb4k_umount: " << strerror( err ) << endl;
537
exit( EXIT_FAILURE );