2005-07-19 19:14:12 +04:00
/*
* create_floppy_devices
*
* Create all possible floppy device based on the CMOS type .
* Based upon code from drivers / block / floppy . c
*
* Copyright ( C ) 2005 , SUSE Linux Products GmbH
*
2005-09-27 18:27:35 +04:00
* Author :
* Hannes Reinecke < hare @ suse . de >
2005-07-19 19:14:12 +04:00
*
2005-09-27 18:27:35 +04:00
* 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 version 2 of the License .
2005-07-19 19:14:12 +04:00
*/
# include <stdio.h>
# include <stdlib.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
# include <string.h>
2007-02-15 00:29:03 +03:00
# include <pwd.h>
# include <grp.h>
2008-07-30 03:45:23 +04:00
# include "../../udev/udev.h"
2005-07-19 19:14:12 +04:00
static char * table [ ] = {
" " , " d360 " , " h1200 " , " u360 " , " u720 " , " h360 " , " h720 " ,
" u1440 " , " u2880 " , " CompaQ " , " h1440 " , " u1680 " , " h410 " ,
" u820 " , " h1476 " , " u1722 " , " h420 " , " u830 " , " h1494 " , " u1743 " ,
" h880 " , " u1040 " , " u1120 " , " h1600 " , " u1760 " , " u1920 " ,
" u3200 " , " u3520 " , " u3840 " , " u1840 " , " u800 " , " u1600 " ,
NULL
} ;
2005-12-15 17:56:50 +03:00
static int t360 [ ] = { 1 , 0 } ;
static int t1200 [ ] = { 2 , 5 , 6 , 10 , 12 , 14 , 16 , 18 , 20 , 23 , 0 } ;
static int t3in [ ] = { 8 , 9 , 26 , 27 , 28 , 7 , 11 , 15 , 19 , 24 , 25 , 29 , 31 , 3 , 4 , 13 , 17 , 21 , 22 , 30 , 0 } ;
2007-02-15 00:29:03 +03:00
static int * table_sup [ ] = { NULL , t360 , t1200 , t3in + 5 + 8 , t3in + 5 , t3in , t3in } ;
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 )
2007-02-15 00:29:03 +03:00
{
vsyslog ( priority , format , args ) ;
}
2005-07-19 19:14:12 +04:00
int main ( int argc , char * * argv )
{
2008-09-06 17:45:31 +04:00
struct udev * udev ;
2005-07-19 19:14:12 +04:00
char * dev ;
2007-12-17 05:41:30 +03:00
char * devname ;
2005-07-19 19:14:12 +04:00
char node [ 64 ] ;
int type = 0 , i , fdnum , c ;
int major = 2 , minor ;
2007-02-15 00:29:03 +03:00
uid_t uid = 0 ;
gid_t gid = 0 ;
2007-03-08 14:28:03 +03:00
mode_t mode = 0660 ;
2005-07-19 19:14:12 +04:00
int create_nodes = 0 ;
int print_nodes = 0 ;
int is_err = 0 ;
2008-09-06 17:45:31 +04:00
udev = udev_new ( ) ;
if ( udev = = NULL )
goto exit ;
logging_init ( " create_floppy_devices " ) ;
udev_set_log_fn ( udev , log_fn ) ;
2008-10-18 22:12:55 +04:00
udev_selinux_init ( udev ) ;
2008-09-06 17:45:31 +04:00
2007-02-15 00:29:03 +03:00
while ( ( c = getopt ( argc , argv , " cudm:U:G:M:t: " ) ) ! = - 1 ) {
2005-07-19 19:14:12 +04:00
switch ( c ) {
case ' c ' :
create_nodes = 1 ;
2007-02-15 00:29:03 +03:00
break ;
2005-07-19 19:14:12 +04:00
case ' d ' :
print_nodes = 1 ;
break ;
2007-02-15 00:29:03 +03:00
case ' U ' :
2008-10-18 22:12:55 +04:00
uid = util_lookup_user ( udev , optarg ) ;
2007-02-15 00:29:03 +03:00
break ;
case ' G ' :
2008-10-18 22:12:55 +04:00
gid = util_lookup_group ( udev , optarg ) ;
2007-02-15 00:29:03 +03:00
break ;
2005-07-19 19:14:12 +04:00
case ' M ' :
2007-02-15 00:29:03 +03:00
mode = strtol ( optarg , NULL , 0 ) ;
mode = mode & 0666 ;
2005-07-19 19:14:12 +04:00
break ;
case ' m ' :
2007-02-15 00:29:03 +03:00
major = strtol ( optarg , NULL , 0 ) ;
2005-07-19 19:14:12 +04:00
break ;
case ' t ' :
2007-02-15 00:29:03 +03:00
type = strtol ( optarg , NULL , 0 ) ;
2005-07-19 19:14:12 +04:00
break ;
default :
is_err + + ;
break ;
}
}
if ( is_err | | optind > = argc ) {
2007-02-15 00:29:03 +03:00
printf ( " Usage: %s [OPTION] device \n "
" -c create \n "
" -d debug \n "
" -m Major number \n "
" -t floppy type number \n "
" -U device node user ownership \n "
" -G device node group owner \n "
" -M device node mode \n "
" \n " , argv [ 0 ] ) ;
2005-07-19 19:14:12 +04:00
return 1 ;
}
dev = argv [ optind ] ;
2007-12-17 05:41:30 +03:00
devname = strrchr ( dev , ' / ' ) ;
if ( devname ! = NULL )
devname = & devname [ 1 ] ;
else
devname = dev ;
if ( strncmp ( devname , " fd " , 2 ) ! = 0 ) {
2005-07-19 19:14:12 +04:00
fprintf ( stderr , " Device '%s' is not a floppy device \n " , dev ) ;
return 1 ;
}
2007-12-17 05:41:30 +03:00
fdnum = strtol ( & devname [ 2 ] , NULL , 10 ) ;
2005-07-19 19:14:12 +04:00
if ( fdnum < 0 | | fdnum > 7 ) {
fprintf ( stderr , " Floppy device number %d out of range (0-7) \n " , fdnum ) ;
return 1 ;
}
if ( fdnum > 3 )
2008-11-23 19:22:32 +03:00
fdnum + = 124 ;
2005-07-19 19:14:12 +04:00
if ( major < 1 ) {
fprintf ( stderr , " Invalid major number %d \n " , major ) ;
return 1 ;
}
2008-11-23 19:22:32 +03:00
if ( type < 0 | | type > = ( int ) ( sizeof ( table_sup ) / sizeof ( table_sup [ 0 ] ) ) ) {
2005-07-19 19:14:12 +04:00
fprintf ( stderr , " Invalid CMOS type %d \n " , type ) ;
return 1 ;
}
if ( type = = 0 )
return 0 ;
i = 0 ;
while ( table_sup [ type ] [ i ] ) {
2007-02-15 00:29:03 +03:00
sprintf ( node , " %s%s " , dev , table [ table_sup [ type ] [ i ] ] ) ;
2005-07-19 19:14:12 +04:00
minor = ( table_sup [ type ] [ i ] < < 2 ) + fdnum ;
if ( print_nodes )
2008-11-23 19:22:32 +03:00
printf ( " %s b %.4o %d %d \n " , node , mode , major , minor ) ;
2007-02-15 00:29:03 +03:00
if ( create_nodes ) {
unlink ( node ) ;
2008-09-10 02:46:17 +04:00
udev_selinux_setfscreatecon ( udev , node , S_IFBLK | mode ) ;
2005-07-19 19:14:12 +04:00
mknod ( node , S_IFBLK | mode , makedev ( major , minor ) ) ;
2008-09-10 02:46:17 +04:00
udev_selinux_resetfscreatecon ( udev ) ;
2007-02-15 00:29:03 +03:00
chown ( node , uid , gid ) ;
2007-03-08 14:28:03 +03:00
chmod ( node , S_IFBLK | mode ) ;
2007-02-15 00:29:03 +03:00
}
2005-07-19 19:14:12 +04:00
i + + ;
}
2008-10-18 22:12:55 +04:00
udev_selinux_exit ( udev ) ;
2008-09-06 17:45:31 +04:00
udev_unref ( udev ) ;
exit :
2005-07-19 19:14:12 +04:00
return 0 ;
}