2008-04-21 20:07:05 +04:00
/*
* find matching entry in fstab and export it
*
* Copyright ( C ) 2008 Kay Sievers < kay . sievers @ vrfy . org >
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 2 of the License , or
* ( at your option ) any later version .
*/
# ifndef _GNU_SOURCE
# define _GNU_SOURCE 1
# endif
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <fcntl.h>
# include <ctype.h>
# include <string.h>
# include <errno.h>
# include <getopt.h>
# include <mntent.h>
# include <sys/stat.h>
2009-06-10 00:47:48 +04:00
# include "libudev.h"
# include "libudev-private.h"
2008-04-21 20:07:05 +04:00
static int debug ;
2008-09-06 17:45:31 +04:00
static void log_fn ( struct udev * udev , int priority ,
const char * file , int line , const char * fn ,
const char * format , va_list args )
2008-04-21 20:07:05 +04:00
{
if ( debug ) {
2008-09-06 17:45:31 +04:00
fprintf ( stderr , " %s: " , fn ) ;
2008-04-21 20:07:05 +04:00
vfprintf ( stderr , format , args ) ;
2008-09-06 17:45:31 +04:00
} else {
2008-04-21 20:07:05 +04:00
vsyslog ( priority , format , args ) ;
2008-09-06 17:45:31 +04:00
}
2008-04-21 20:07:05 +04:00
}
2008-09-06 17:45:31 +04:00
static int matches_device_list ( struct udev * udev , char * * devices , const char * name )
2008-04-21 20:07:05 +04:00
{
int i ;
for ( i = 0 ; devices [ i ] ! = NULL ; i + + ) {
2008-09-06 17:45:31 +04:00
info ( udev , " compare '%s' == '%s' \n " , name , devices [ i ] ) ;
2008-04-21 20:07:05 +04:00
if ( strcmp ( devices [ i ] , name ) = = 0 )
return 1 ;
}
return 0 ;
}
2008-09-06 17:45:31 +04:00
static void print_fstab_entry ( struct udev * udev , struct mntent * mnt )
2008-04-21 20:07:05 +04:00
{
printf ( " FSTAB_NAME=%s \n " , mnt - > mnt_fsname ) ;
printf ( " FSTAB_DIR=%s \n " , mnt - > mnt_dir ) ;
printf ( " FSTAB_TYPE=%s \n " , mnt - > mnt_type ) ;
printf ( " FSTAB_OPTS=%s \n " , mnt - > mnt_opts ) ;
printf ( " FSTAB_FREQ=%d \n " , mnt - > mnt_freq ) ;
printf ( " FSTAB_PASSNO=%d \n " , mnt - > mnt_passno ) ;
}
int main ( int argc , char * argv [ ] )
{
2008-09-06 17:45:31 +04:00
struct udev * udev ;
2008-04-21 20:07:05 +04:00
static const struct option options [ ] = {
2008-10-02 18:49:05 +04:00
{ " export " , no_argument , NULL , ' x ' } ,
{ " debug " , no_argument , NULL , ' d ' } ,
{ " help " , no_argument , NULL , ' h ' } ,
2008-04-21 20:07:05 +04:00
{ }
} ;
2008-09-06 17:45:31 +04:00
char * * devices ;
2008-04-21 20:07:05 +04:00
FILE * fp ;
struct mntent * mnt ;
2008-09-06 17:45:31 +04:00
int rc = 1 ;
udev = udev_new ( ) ;
if ( udev = = NULL )
goto exit ;
2008-04-21 20:07:05 +04:00
2009-06-10 00:47:48 +04:00
udev_log_init ( " fstab_id " ) ;
2008-09-06 17:45:31 +04:00
udev_set_log_fn ( udev , log_fn ) ;
2008-04-21 20:07:05 +04:00
while ( 1 ) {
int option ;
2008-09-06 17:45:31 +04:00
option = getopt_long ( argc , argv , " dxh " , options , NULL ) ;
2008-04-21 20:07:05 +04:00
if ( option = = - 1 )
break ;
switch ( option ) {
case ' d ' :
debug = 1 ;
2008-09-06 17:45:31 +04:00
if ( udev_get_log_priority ( udev ) < LOG_INFO )
udev_set_log_priority ( udev , LOG_INFO ) ;
2008-04-21 20:07:05 +04:00
break ;
case ' h ' :
printf ( " Usage: fstab_id [OPTIONS] name [...] \n "
" --export print environment keys \n "
" --debug debug to stderr \n "
" --help print this help text \n \n " ) ;
2008-09-06 17:45:31 +04:00
goto exit ;
case ' x ' :
break ;
2008-04-21 20:07:05 +04:00
default :
2008-09-06 17:45:31 +04:00
rc = 2 ;
2008-04-21 20:07:05 +04:00
goto exit ;
}
}
devices = & argv [ optind ] ;
if ( devices [ 0 ] = = NULL ) {
fprintf ( stderr , " error: missing device(s) to match \n " ) ;
2008-09-06 17:45:31 +04:00
rc = 3 ;
2008-04-21 20:07:05 +04:00
goto exit ;
}
fp = setmntent ( " /etc/fstab " , " r " ) ;
if ( fp = = NULL ) {
fprintf ( stderr , " error: opening fstab: %s \n " , strerror ( errno ) ) ;
2008-09-06 17:45:31 +04:00
rc = 4 ;
2008-04-21 20:07:05 +04:00
goto exit ;
}
while ( 1 ) {
mnt = getmntent ( fp ) ;
if ( mnt = = NULL )
break ;
2008-09-06 17:45:31 +04:00
info ( udev , " found '%s'@'%s' \n " , mnt - > mnt_fsname , mnt - > mnt_dir ) ;
2008-04-21 20:07:05 +04:00
/* skip root device */
if ( strcmp ( mnt - > mnt_dir , " / " ) = = 0 )
continue ;
/* match LABEL */
if ( strncmp ( mnt - > mnt_fsname , " LABEL= " , 6 ) = = 0 ) {
const char * label ;
char str [ 256 ] ;
label = & mnt - > mnt_fsname [ 6 ] ;
if ( label [ 0 ] = = ' " ' | | label [ 0 ] = = ' \' ' ) {
char * pos ;
2009-05-20 19:57:52 +04:00
util_strscpy ( str , sizeof ( str ) , & label [ 1 ] ) ;
2008-04-21 20:07:05 +04:00
pos = strrchr ( str , label [ 0 ] ) ;
if ( pos = = NULL )
continue ;
pos [ 0 ] = ' \0 ' ;
label = str ;
}
2009-09-09 00:11:04 +04:00
if ( matches_device_list ( udev , devices , label ) ) {
2008-09-06 17:45:31 +04:00
print_fstab_entry ( udev , mnt ) ;
rc = 0 ;
2008-04-21 20:07:05 +04:00
break ;
}
continue ;
}
/* match UUID */
if ( strncmp ( mnt - > mnt_fsname , " UUID= " , 5 ) = = 0 ) {
const char * uuid ;
char str [ 256 ] ;
uuid = & mnt - > mnt_fsname [ 5 ] ;
if ( uuid [ 0 ] = = ' " ' | | uuid [ 0 ] = = ' \' ' ) {
char * pos ;
2009-05-20 19:57:52 +04:00
util_strscpy ( str , sizeof ( str ) , & uuid [ 1 ] ) ;
2008-04-21 20:07:05 +04:00
pos = strrchr ( str , uuid [ 0 ] ) ;
if ( pos = = NULL )
continue ;
pos [ 0 ] = ' \0 ' ;
uuid = str ;
}
2009-09-09 00:11:04 +04:00
if ( matches_device_list ( udev , devices , uuid ) ) {
2008-09-06 17:45:31 +04:00
print_fstab_entry ( udev , mnt ) ;
rc = 0 ;
2008-04-21 20:07:05 +04:00
break ;
}
continue ;
}
/* only devices */
2008-09-06 17:45:31 +04:00
if ( strncmp ( mnt - > mnt_fsname , udev_get_dev_path ( udev ) , strlen ( udev_get_dev_path ( udev ) ) ) ! = 0 )
2008-04-21 20:07:05 +04:00
continue ;
2008-09-06 17:45:31 +04:00
if ( matches_device_list ( udev , devices , & mnt - > mnt_fsname [ strlen ( udev_get_dev_path ( udev ) ) + 1 ] ) ) {
print_fstab_entry ( udev , mnt ) ;
rc = 0 ;
2008-04-21 20:07:05 +04:00
break ;
}
}
endmntent ( fp ) ;
exit :
2008-09-06 17:45:31 +04:00
udev_unref ( udev ) ;
2009-06-10 00:47:48 +04:00
udev_log_close ( ) ;
2008-04-21 20:07:05 +04:00
return rc ;
}