summaryrefslogtreecommitdiffstats
path: root/tdeioslave/media/contrib/mediamanager_usbstorage.dev
blob: d12a3e6873d7795d690dae1f2d279513a7a4bc96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/sh
# /etc/dev.d/default/mediamanager_usbstorage.dev
# Notify all KDE sessions (thanks to the mediamanager) that a new
# usb_storage device appeared or disappeared
# 

# to debug this script, uncomment the next line and see /tmp/mediamanager_usbstorage.debug after execution
#DEBUG=1

# exit immediately if /usr/bin/ is not yet available (during boot if /usr is a separate partition)
/bin/ls -d /usr/bin/ >/dev/null 2>&1 || exit

DEBUGOUT=/tmp/mediamanager_usbstorage.debug.$$
if [ "$DEBUG" = "1" -a -z "$2" ]; then
  echo "executing $0 $@" > $DEBUGOUT
  echo "with the following environment variables:" >> $DEBUGOUT
  env >> $DEBUGOUT
  echo "----" >> $DEBUGOUT
  sh -x $0 $@ debug >> $DEBUGOUT 2>&1
  exit
fi

# we only manage block devices
if [ "$1" != "block" ]; then exit; fi

# we only manage usb_storage devices
if [ "$ACTION" = "add" ]; then
  device="`ls /sys$DEVPATH/../device/../../../ 2> /dev/NULL | grep ':' | head -1`"
  if [ -z "$device" -o ! -e /sys/bus/usb/drivers/usb-storage/$device ]; then
    # The behavior is not the same for every kernel it seems.
    # Testing the driver/ directory just in case.
    device="`ls /sys$DEVPATH/../device/../../../driver 2> /dev/NULL | grep ':' | head -1`"
    if [ -z "$device" -o ! -e /sys/bus/usb/drivers/usb-storage/$device ]; then
      exit
    fi
  fi
fi

# functions for syslog
LOGGER="logger -t `basename $0`[$$] -p user.notice"
write_syslog () {
  echo ${@} | $LOGGER
}

# be sure the drivers are loaded
/sbin/modprobe -q usb_storage
/sbin/modprobe -q vfat

# create the FSH required /media directory
# See: http://www.pathname.com/fhs/pub/fhs-2.3.html#MEDIAMOUNTPOINT
MNT=media
if [ ! -d /$MNT ]; then
  mkdir /$MNT
  write_syslog "Created the /$MNT directory"
fi

# we need DEVPATH, DEVNAME and ACTION, so we warn the user that executes this script by hand
if [ -z "$DEVPATH" -a -z "$DEVNAME" -a -z "$ACTION" ]; then
  echo
  echo "This script must be called by udevd because it needs the following environment variables: DEVPATH, DEVNAME, ACTION"
  echo "So you must copy this script as /etc/dev.d/default/updfstab-2.6.dev and set it executable"
  echo "See: http://www.kernel.org/pub/linux/utils/kernel/hotplug/RFC-dev.d"
  echo
  exit
fi

# if $DEVPATH/device exists, we are a device, not a partition, so exit
if [ -d /sys${DEVPATH}/device ]; then exit; fi

dcop_users="`ps aux | grep dcopserver | grep -v grep | awk '{print $1}' | sort | uniq`"

# if the current device is being added
if [ "$ACTION" = "add" ]; then
  # get partition information
  partition="/sys${DEVPATH}/../device/../../.."
  # We check twice again... marvelous random kernel behaviour changes...
  if [ -e $partition/product ]; then
    product="`cat $partition/product`"
  else
    product="`cat $partition/../product`"
  fi
  if [ -e $partition/manufacturer ]; then
    manufacturer="`cat $partition/manufacturer`"
  else
    manufacturer="`cat $partition/../manufacturer`"
  fi

  write_syslog "Invoking dcop..."
  write_syslog "kded mediamanager removablePlug $DEVNAME \"$manufacturer $product\""

  method="kded mediamanager removablePlug"
  for user in $dcop_users ; do
    dcop --user $user --all-sessions $method $DEVNAME "$manufacturer $product"
  done
  
elif [ "$ACTION" = "remove" ]; then
  write_syslog "Invoking dcop..."
  write_syslog "kded mediamanager removableUnplug $DEVNAME"

  method="kded mediamanager removableUnplug"
  for user in $dcop_users ; do
    dcop --user $user --all-sessions $method $DEVNAME
  done
		  
  umount $DEVNAME
fi