1 |
#!/bin/bash |
2 |
# $Id: libusbscanner,v 1.2 2004/08/26 10:26:08 twaugh Exp $ |
3 |
# |
4 |
# /etc/hotplug/usb/libusbscanner |
5 |
# |
6 |
# Sets up newly plugged in USB scanner so that the user who owns |
7 |
# the console according to pam_console can access it from user space |
8 |
# |
9 |
# Note that for this script to work, you'll need all of the following: |
10 |
# a) a line in the file /etc/hotplug/usermap that corresponds to the |
11 |
# camera you are using. |
12 |
# b) a setup using pam_console creates the respective lock files |
13 |
# containing the name of the respective user. You can check for that |
14 |
# by executing "echo `cat /var/{run,lock}/console.lock`" and |
15 |
# verifying the appropriate user is mentioned somewhere there. |
16 |
# c) a Linux kernel supporting hotplug and usbdevfs |
17 |
# d) the hotplug package (http://linux-hotplug.sourceforge.net/) |
18 |
# |
19 |
# In the usermap file, the first field "usb module" should be named |
20 |
# "libusbscanner" like this script. |
21 |
# |
22 |
|
23 |
if [ -z "${DEVICE}" ] |
24 |
then |
25 |
exit 0 |
26 |
fi |
27 |
|
28 |
DEVDIR=/dev |
29 |
DEVNAME="scanner-usb-$(echo ${DEVICE#../proc/bus/usb/} | sed -e 's;/;:;g')" |
30 |
|
31 |
wait_and_add () { |
32 |
local limit=90 |
33 |
# wait until the action can be performed but no longer |
34 |
# than $limit seconds |
35 |
while ! ln -snf "${DEVICE}" "${DEVDIR}/${DEVNAME}" ; do |
36 |
[ "$limit" = 0 ] && return |
37 |
sleep 1 |
38 |
limit=$((limit - 1)) |
39 |
done |
40 |
|
41 |
/sbin/pam_console_apply "${DEVDIR}/${DEVNAME}" |
42 |
} |
43 |
|
44 |
if [ "${ACTION}" = "add" ] && [ -f "${DEVICE}" ] |
45 |
then |
46 |
|
47 |
# Make a symlink from /dev/scanner-usb-BBB:DDD to /proc/bus/usb/BBB/DDD. |
48 |
# If /etc/security/console.perms is set up properly, pam_console will |
49 |
# follow the symlink and set the permissions of /proc/bus/usb/BBB/DDD such |
50 |
# that "console" users will be able to use their USB scanner. |
51 |
# |
52 |
# If the scanner is "cold plugged", and this event will occurred before |
53 |
# the root filesystem was writeable, then we wait in a background |
54 |
# until a link in ${DEVDIR} can be created. Permissions on |
55 |
# /proc/bus/usb/BBB/DDD will be of a "revert" type; see |
56 |
# man 5 console.perms. This is fine; when a device does not exist |
57 |
# at all we do not particularly care about its permissions. |
58 |
# |
59 |
( wait_and_add >/dev/null 2>&1 & ) |
60 |
|
61 |
# See /etc/hotplug/usb.agent for a description of how REMOVER works. |
62 |
ln -sf "${0}" "${REMOVER}" |
63 |
fi |
64 |
|
65 |
if [ "${ACTION}" = "remove" ] |
66 |
then |
67 |
rm -f "${DEVDIR}/${DEVNAME}" |
68 |
fi |