Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/perl
- # Install this script by placing the following statement at the end of
- # /etc.devd.conf:
- #
- # notify 0 {
- # action "/usr/local/bin/usb-linker attach $device-name $sernum";
- # };
- #
- # When a USB device is connected with serial number "the-serial-number", a
- # link will be created from /devious/by-sernum/the-serial-number to
- # /dev/dawhatever.
- #
- # When the USB device is disconnected, the link will go away.
- #
- # To facilitate handling disconnections, directory /devious/to-sernum contains
- # symbolic links which allow association of devices to serial numbers.
- #
- # We use directory /devious, because we can't introduce subdirectories into
- # /dev. Devious, no?
- use strict;
- use warnings FATAL=>"all";
- my $devious="devious";
- my $by_sernum="by-sernum";
- my $to_sernum="to-sernum";
- my $error;
- my $sernum="[unknowable]";
- my $truncated;
- if(scalar(@ARGV)!=2)
- {
- exit(0);
- }
- if($ARGV[1]!~/^da[0-9]+./)
- {
- exit(0);
- }
- if($ARGV[0] eq "CREATE")
- {
- system("fstyp /dev/$ARGV[1] > /dev/null 2> /dev/null");
- if($?)
- {
- exit(0);
- }
- }
- $truncated=$ARGV[1];
- $truncated=~s/^(da[0-9]+).*$/$1/;
- #-----------------------------------------------------------------------------
- sub
- do_destroy
- {
- my $own_sernum;
- $own_sernum=readlink("/$devious/$to_sernum/$ARGV[1]");
- if(!defined($own_sernum))
- {
- return; # <---------
- }
- unlink "/$devious/$by_sernum/$own_sernum";
- unlink "/$devious/$to_sernum/$ARGV[1]";
- } # do_destroy
- #-----------------------------------------------------------------------------
- sub
- do_create
- {
- do_destroy();
- mkdir("/$devious");
- mkdir("/$devious/$to_sernum");
- mkdir("/$devious/$by_sernum");
- symlink("$sernum",
- "/$devious/$to_sernum/$ARGV[1]"
- )
- or
- return; # <---------
- symlink("/dev/$ARGV[1]",
- "/$devious/$by_sernum/$sernum"
- )
- or
- return; # <---------
- }
- #-----------------------------------------------------------------------------
- umask 022;
- if($ARGV[0] eq "CREATE")
- {
- $sernum=`camcontrol inquiry $truncated -S 2> /dev/null`;
- $error=$?;
- if($error)
- {
- $sernum="[erroneous]";
- }
- else
- {
- $sernum=~s/\r?\n?$//;
- do_create();
- }
- }
- elsif($ARGV[0] eq "DESTROY")
- {
- $sernum="[inapplicable]";
- do_destroy();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement