Advertisement
BillEvansAtMariposa

20160525-01

May 25th, 2016
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #!/usr/local/bin/perl
  2.  
  3. # Install this script by placing the following statement at the end of
  4. # /etc.devd.conf:
  5. #
  6. # notify 0 {
  7. # action "/usr/local/bin/usb-linker attach $device-name $sernum";
  8. # };
  9. #
  10. # When a USB device is connected with serial number "the-serial-number", a
  11. # link will be created from /devious/by-sernum/the-serial-number to
  12. # /dev/dawhatever.
  13. #
  14. # When the USB device is disconnected, the link will go away.
  15. #
  16. # To facilitate handling disconnections, directory /devious/to-sernum contains
  17. # symbolic links which allow association of devices to serial numbers.
  18. #
  19. # We use directory /devious, because we can't introduce subdirectories into
  20. # /dev. Devious, no?
  21.  
  22. use strict;
  23. use warnings FATAL=>"all";
  24.  
  25. my $devious="devious";
  26. my $by_sernum="by-sernum";
  27. my $to_sernum="to-sernum";
  28.  
  29. my $error;
  30. my $sernum="[unknowable]";
  31. my $truncated;
  32.  
  33. if(scalar(@ARGV)!=2)
  34. {
  35. exit(0);
  36. }
  37.  
  38. if($ARGV[1]!~/^da[0-9]+./)
  39. {
  40. exit(0);
  41. }
  42.  
  43. if($ARGV[0] eq "CREATE")
  44. {
  45. system("fstyp /dev/$ARGV[1] > /dev/null 2> /dev/null");
  46.  
  47. if($?)
  48. {
  49. exit(0);
  50. }
  51. }
  52.  
  53. $truncated=$ARGV[1];
  54. $truncated=~s/^(da[0-9]+).*$/$1/;
  55.  
  56. #-----------------------------------------------------------------------------
  57.  
  58. sub
  59. do_destroy
  60. {
  61. my $own_sernum;
  62.  
  63. $own_sernum=readlink("/$devious/$to_sernum/$ARGV[1]");
  64.  
  65. if(!defined($own_sernum))
  66. {
  67. return; # <---------
  68. }
  69.  
  70. unlink "/$devious/$by_sernum/$own_sernum";
  71. unlink "/$devious/$to_sernum/$ARGV[1]";
  72.  
  73. } # do_destroy
  74.  
  75. #-----------------------------------------------------------------------------
  76.  
  77. sub
  78. do_create
  79. {
  80. do_destroy();
  81.  
  82. mkdir("/$devious");
  83. mkdir("/$devious/$to_sernum");
  84. mkdir("/$devious/$by_sernum");
  85.  
  86. symlink("$sernum",
  87. "/$devious/$to_sernum/$ARGV[1]"
  88. )
  89. or
  90. return; # <---------
  91.  
  92. symlink("/dev/$ARGV[1]",
  93. "/$devious/$by_sernum/$sernum"
  94. )
  95. or
  96. return; # <---------
  97. }
  98.  
  99. #-----------------------------------------------------------------------------
  100.  
  101. umask 022;
  102.  
  103. if($ARGV[0] eq "CREATE")
  104. {
  105. $sernum=`camcontrol inquiry $truncated -S 2> /dev/null`;
  106. $error=$?;
  107.  
  108. if($error)
  109. {
  110. $sernum="[erroneous]";
  111. }
  112. else
  113. {
  114. $sernum=~s/\r?\n?$//;
  115. do_create();
  116. }
  117. }
  118. elsif($ARGV[0] eq "DESTROY")
  119. {
  120. $sernum="[inapplicable]";
  121.  
  122. do_destroy();
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement