From 2816014184dc5def0356281eb6fe8e7f4ffc9602 Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Tue, 11 Mar 2008 10:52:05 +0100 Subject: [PATCH] Split the main UDEV script into separate "add" and "remove" scripts --- .../etc/udev/rules.d/85-remote-dig528-2-run.rules | 8 +- contrib/etc/udev/rules.d/85-remote-micaz-run.rules | 6 +- .../udev/rules.d/85-remote-micaz-soekris-run.rules | 6 +- .../etc/udev/rules.d/85-remote-tmotesky-run.rules | 6 +- .../rules.d/85-remote-tmotesky-soekris-run.rules | 6 +- .../{remote-device-manager => remote-device-add} | 98 +++++++--------- contrib/lib/udev/remote-device-remove | 125 +++++++++++++++++++++ 7 files changed, 182 insertions(+), 73 deletions(-) rename contrib/lib/udev/{remote-device-manager => remote-device-add} (57%) create mode 100755 contrib/lib/udev/remote-device-remove diff --git a/contrib/etc/udev/rules.d/85-remote-dig528-2-run.rules b/contrib/etc/udev/rules.d/85-remote-dig528-2-run.rules index 3a245c9..de97fab 100644 --- a/contrib/etc/udev/rules.d/85-remote-dig528-2-run.rules +++ b/contrib/etc/udev/rules.d/85-remote-dig528-2-run.rules @@ -4,10 +4,10 @@ ## Create /dev/remote/{serial}/* files # For the path file use the physical device path given (only) for # the usb_device event via the PHYSDEVPATH env variable. -DEVPATH=="/class/usb_device/*", ATTRS{idVendor}=="0050", ATTRS{idProduct}=="c237", RUN+="/lib/udev/remote-device-manager dig582-2 0050C237%s{serial} %E{PHYSDEVPATH} /sbin/hc08sprg" +DEVPATH=="/class/usb_device/*", ATTRS{idVendor}=="0050", ATTRS{idProduct}=="c237", RUN+="/lib/udev/remote-device-add dig582-2 0050C237%s{serial} %E{PHYSDEVPATH} /sbin/hc08sprg" ## Remove /dev/remote/{serial}/* files # The idVendor and idProduct attributes are not defined on some UDEV -# systems. Instead, catch all tty events and run the remote-device-manager -# script in "device cleanup" mode. -DEVPATH=="/class/tty/*", ACTION=="remove", RUN+="/lib/udev/remote-device-manager" +# systems. Instead, catch all tty events and run the +# remote-device-remove script to cleanup stale mote device files. +DEVPATH=="/class/tty/*", ACTION=="remove", RUN+="/lib/udev/remote-device-remove" diff --git a/contrib/etc/udev/rules.d/85-remote-micaz-run.rules b/contrib/etc/udev/rules.d/85-remote-micaz-run.rules index fa806d7..1ccf0c8 100644 --- a/contrib/etc/udev/rules.d/85-remote-micaz-run.rules +++ b/contrib/etc/udev/rules.d/85-remote-micaz-run.rules @@ -3,18 +3,18 @@ ################################################################# ## Create /dev/remote/{platform}{serial}/* files # -# See remote-device-manager for description of all parameters +# See remote-device-add for description of all parameters # # Rules tested on the laptop running Gentoo and having # both TMoteSky and MicaZ connected to the USB hub. -DEVPATH=="/class/usb_device/*", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", RUN+="/lib/udev/remote-device-manager MicaZ 04036010%s{serial} %E{PHYSDEVPATH} /sbin/program-mote.sh /sbin/control-mote.sh" +DEVPATH=="/class/usb_device/*", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", RUN+="/lib/udev/remote-device-add MicaZ 04036010%s{serial} %E{PHYSDEVPATH} /sbin/program-mote.sh /sbin/control-mote.sh" ## Remove /dev/remote/{platform}{serial}/* files # The idVendor and idProduct attributes are not defined on some # udev systems. Instead, catch all tty events and use a general # device cleanup script. -SUBSYSTEM=="tty", ACTION=="remove", RUN+="/lib/udev/remote-device-manager" +SUBSYSTEM=="tty", ACTION=="remove", RUN+="/lib/udev/remote-device-remove" diff --git a/contrib/etc/udev/rules.d/85-remote-micaz-soekris-run.rules b/contrib/etc/udev/rules.d/85-remote-micaz-soekris-run.rules index fcc20d8..1fc8a57 100644 --- a/contrib/etc/udev/rules.d/85-remote-micaz-soekris-run.rules +++ b/contrib/etc/udev/rules.d/85-remote-micaz-soekris-run.rules @@ -3,7 +3,7 @@ ################################################################# ## Create /dev/remote/{platform}{serial}/* files # -# See remote-device-manager for description of all parameters +# See remote-device-add for description of all parameters # # Rules tested on the Soekris board NET4521 running # stripped down version of the Gentoo and having both @@ -11,12 +11,12 @@ # NET4521 board comes without USB we had to use # PCMCIA-USB card as a interface for USB hubs. -SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", RUN+="/bin/bash -c '/lib/udev/remote-device-manager MicaZ 04036010%s{serial} ${DEVPATH%%%%/tty*} /sbin/program-mote.sh /sbin/control-mote.sh'" +SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", RUN+="/bin/bash -c '/lib/udev/remote-device-add MicaZ 04036010%s{serial} ${DEVPATH%%%%/tty*} /sbin/program-mote.sh /sbin/control-mote.sh'" ## Remove /dev/remote/{platform}{serial}/* files # The idVendor and idProduct attributes are not defined on some # udev systems. Instead, catch all tty events and use a general # device cleanup script. -SUBSYSTEM=="tty", ACTION=="remove", RUN+="/lib/udev/remote-device-manager" +SUBSYSTEM=="tty", ACTION=="remove", RUN+="/lib/udev/remote-device-remove" diff --git a/contrib/etc/udev/rules.d/85-remote-tmotesky-run.rules b/contrib/etc/udev/rules.d/85-remote-tmotesky-run.rules index 646a5c0..6f46d12 100644 --- a/contrib/etc/udev/rules.d/85-remote-tmotesky-run.rules +++ b/contrib/etc/udev/rules.d/85-remote-tmotesky-run.rules @@ -3,17 +3,17 @@ ################################################################# ## Create /dev/remote/{platform}{serial}/* files # -# See remote-device-manager for description of all parameters +# See remote-device-add for description of all parameters # # Rules tested on the laptop running Gentoo and having # both TMoteSky and MicaZ connected to the USB hub. -DEVPATH=="/class/usb_device/*", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", RUN+="/lib/udev/remote-device-manager TMoteSky 04036001%s{serial} %E{PHYSDEVPATH} /sbin/program-mote.sh /sbin/control-mote.sh" +DEVPATH=="/class/usb_device/*", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", RUN+="/lib/udev/remote-device-add TMoteSky 04036001%s{serial} %E{PHYSDEVPATH} /sbin/program-mote.sh /sbin/control-mote.sh" ## Remove /dev/remote/{platform}{serial}/* files # The idVendor and idProduct attributes are not defined on some # udev systems. Instead, catch all tty events and use a general # device cleanup script. -SUBSYSTEM=="tty", ACTION=="remove", RUN+="/lib/udev/remote-device-manager" +SUBSYSTEM=="tty", ACTION=="remove", RUN+="/lib/udev/remote-device-remove" diff --git a/contrib/etc/udev/rules.d/85-remote-tmotesky-soekris-run.rules b/contrib/etc/udev/rules.d/85-remote-tmotesky-soekris-run.rules index 9505ab4..aab0839 100644 --- a/contrib/etc/udev/rules.d/85-remote-tmotesky-soekris-run.rules +++ b/contrib/etc/udev/rules.d/85-remote-tmotesky-soekris-run.rules @@ -3,7 +3,7 @@ ################################################################# ## Create /dev/remote/{platform}{serial}/* files # -# See remote-device-manager for description of all parameters +# See remote-device-add for description of all parameters # # Rules tested on the Soekris board NET4521 running # stripped down version of the Gentoo and having both @@ -11,12 +11,12 @@ # NET4521 board comes without USB we had to use # PCMCIA-USB card as a interface for USB hubs. -SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", RUN+="/bin/bash -c '/lib/udev/remote-device-manager TMoteSky 04036001%s{serial} ${DEVPATH%%%%/tty*} /sbin/program-mote.sh /sbin/control-mote.sh'" +SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", RUN+="/bin/bash -c '/lib/udev/remote-device-add TMoteSky 04036001%s{serial} ${DEVPATH%%%%/tty*} /sbin/program-mote.sh /sbin/control-mote.sh'" ## Remove /dev/remote/{platform}{serial}/* files # The idVendor and idProduct attributes are not defined on some # udev systems. Instead, catch all tty events and use a general # device cleanup script. -SUBSYSTEM=="tty", ACTION=="remove", RUN+="/lib/udev/remote-device-manager" +SUBSYSTEM=="tty", ACTION=="remove", RUN+="/lib/udev/remote-device-remove" diff --git a/contrib/lib/udev/remote-device-manager b/contrib/lib/udev/remote-device-add similarity index 57% rename from contrib/lib/udev/remote-device-manager rename to contrib/lib/udev/remote-device-add index 3585ecd..8edfa2a 100755 --- a/contrib/lib/udev/remote-device-manager +++ b/contrib/lib/udev/remote-device-add @@ -1,24 +1,22 @@ #!/bin/sh # +# REMOTE-DEVICE-ADD(7) +# ==================== +# # NAME # ---- -# remote.sh - script for maintaining the /dev/remote device hierarchy +# remote-device-add - script for adding motes to the Re-Mote device hierarchy # # SYNOPSIS # -------- -# /etc/udev/scripts/remote.sh PLATFORM MOTEMAC MOTEPATH PROGRAMMER CONTROLLER +# /lib/udev/remote-device-add PLATFORM MOTEMAC MOTEPATH PROGRAMMER CONTROLLER # # DESCRIPTION # ----------- # This script is designed to be called via a UDEV rule's RUN key. It -# sets up extra information for added motes and cleans up after removed -# motes, as well as notifies the motehost about new events. It assumes -# that when a device is added the associated TTY has already been -# created. -# -# When called for an add ACTION, the script expects two command line -# arguments described below. No arguments are expected when handling a -# remove ACTION. +# sets up extra information for added motes and notifies the motehost +# about new events. It assumes that when a device is added the +# associated TTY has already been created. # # OPTIONS # ------- @@ -50,11 +48,10 @@ # # ENVIRONMENT VARIABLES # --------------------- -# The script has been designed to rely on as few environment variables -# as possible to make it more generic. Instead it relies on being passed -# all important variables as command line arguments. The only -# environment variable used is the ACTION environment variable set by -# UDEV. +# ACTION:: +# +# This environment variable is set by UDEV. Unless the value is +# "add" the script will exit. # # FILES # ----- @@ -95,13 +92,22 @@ # As an example, the dig528-2 and MicaZ UDEV rules use the following # arguments: # -# /etc/udev/scripts/remote.sh dig528-2 0050C237%s{serial} %E{PHYSDEVPATH} /sbin/hc08sprg -# /etc/udev/scripts/remote.sh MicaZ 04036010%s{serial} %E{PHYSDEVPATH} /sbin/program-mote.sh /sbin/control-mote.sh +# /lib/udev/remote-device-add dig528-2 0050C237%s{serial} %E{PHYSDEVPATH} /sbin/hc08sprg +# /lib/udev/remote-device-add MicaZ 04036010%s{serial} %E{PHYSDEVPATH} /sbin/program-mote.sh /sbin/control-mote.sh # -# AUTHOR -# ------ +# COPYRIGHT +# --------- # Copyright (c) 2007 Jonas Fonseca -# 2008 Rostislav Spinar +# Copyright (c) 2008 Rostislav Spinar +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# SEE ALSO +# -------- +# remote-device-remove(7) # ######################################################################## # Configuration section @@ -140,46 +146,24 @@ die () exit 0 } +test "$ACTION" = "add" || die "Requested action not add ($ACTION)" test -d "$DEVROOT" || die "Device root is not a directory ($DEVROOT)" -case "$ACTION" in -add) - test -n "$MOTEMAC" || die "No mote MAC defined" - test -n "$MOTEPATH" || die "No device path defined" - test -e "$DEVROOT/$MOTEMAC/tty/control" || die "No control TTY exists for '$MOTEMAC'" - test -e "$DEVROOT/$MOTEMAC/tty/data" || die "No data TTY exists for '$MOTEMAC'" - test -e "$DEVROOT/$MOTEMAC/path" && die "Path exists for '$MOTEMAC'" - echo "$MOTEPATH" > "$DEVROOT/$MOTEMAC/path" - echo "$PLATFORM" > "$DEVROOT/$MOTEMAC/platform" - ln -s "$PROGRAMMER" "$DEVROOT/$MOTEMAC/programmer" || - die "Failed to create program symlink" - if [ -n "$CONTROLLER" ]; then - ln -s "$CONTROLLER" "$DEVROOT/$MOTEMAC/controller" || - die "Failed to create program symlink" - fi - info "mote '$MOTEMAC' with path '$MOTEPATH' and platform '$PLATFORM'" - ;; +test -n "$MOTEMAC" || die "No mote MAC defined" +test -n "$MOTEPATH" || die "No device path defined" +test -e "$DEVROOT/$MOTEMAC/tty/control" || die "No control TTY exists for '$MOTEMAC'" +test -e "$DEVROOT/$MOTEMAC/tty/data" || die "No data TTY exists for '$MOTEMAC'" +test -e "$DEVROOT/$MOTEMAC/path" && die "Path exists for '$MOTEMAC'" -remove) - tmpdir="$DEVROOT/.tmp" - # Use the tmpdir as a semaphore when multiple motes are removed - test -d "$tmpdir" && return - mkdir -p "$tmpdir" || die "Failed to create temp dir '$tmpdir'" - # Remove devices where no tty link exists. - ls "$DEVROOT/" | while read motedir; - do - dir="$DEVROOT/$motedir" - test -e "$dir/tty" && continue; - info "Removing mote '$motedir'" - # Cannot die here since we need to remove the temp dir - mv "$dir" "$tmpdir" || info "Failed to move '$dir' to '$tmpdir'" - done - # The temp directory is empty? - rmdir "$tmpdir" && return - # Fatal but the mote host should still be notified - rm -rf "$tmpdir" || info "Failed to remove directory '$tmpdir'" - ;; -esac +echo "$MOTEPATH" > "$DEVROOT/$MOTEMAC/path" +echo "$PLATFORM" > "$DEVROOT/$MOTEMAC/platform" +ln -s "$PROGRAMMER" "$DEVROOT/$MOTEMAC/programmer" || + die "Failed to create program symlink" +if [ -n "$CONTROLLER" ]; then + ln -s "$CONTROLLER" "$DEVROOT/$MOTEMAC/controller" || + die "Failed to create program symlink" +fi +info "mote '$MOTEMAC' with path '$MOTEPATH' and platform '$PLATFORM'" # Notify mote host test -p "$MCHPIPE" || die "Event pipe does not exist at '$MCHPIPE'" diff --git a/contrib/lib/udev/remote-device-remove b/contrib/lib/udev/remote-device-remove new file mode 100755 index 0000000..9635ac5 --- /dev/null +++ b/contrib/lib/udev/remote-device-remove @@ -0,0 +1,125 @@ +#!/bin/sh +# +# REMOTE-DEVICE-REMOVE(7) +# ======================= +# +# NAME +# ---- +# remote-device-remove - script for cleaning up the Re-Mote device hierarchy +# +# SYNOPSIS +# -------- +# /lib/udev/remote-device-remove +# +# DESCRIPTION +# ----------- +# This script is designed to be called via a UDEV rule's RUN key. It +# cleans up after removed motes and notifies the motehost about the +# changed state of the device hierarchy. +# +# ENVIRONMENT VARIABLES +# --------------------- +# ACTION:: +# +# This environment variable is set by UDEV. Unless the value is +# "remove" the script will exit. +# +# FILES +# ----- +# /dev/remote/MOTEMAC/tty/control:: +# /dev/remote/MOTEMAC/tty/data:: +# +# Symlink to the mote's data and control TTY devices. These are +# checked for existence in order to determine whether the mote +# directory and files created by remote-device-add(7) should be +# cleanup. +# +# /var/log/remote-udev.log:: +# +# Log file for the Re-Mote UDEV scripts. +# +# /var/run/motehost.events:: +# +# Path to the pipe where the mote host can be notified about the +# remove event. +# +# EXAMPLE +# ------- +# This script should usually be run using a UDEV rule similar to the +# following: +# +# SUBSYSTEM=="tty", ACTION=="remove", RUN+="/lib/udev/remote-device-remove" +# +# COPYRIGHT +# --------- +# Copyright (c) 2007 Jonas Fonseca +# Copyright (c) 2008 Rostislav Spinar +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# SEE ALSO +# -------- +# remote-device-add(7) +# +######################################################################## +# Configuration section +######################################################################## + +# The path to the device hierarchy +DEVROOT="/dev/remote" + +# File to log messages to; leave empty to disable logging +LOGFILE="/var/log/remote-udev.log" + +# Path to the MCH event pipe +MCHPIPE="/var/run/motehost.events" + +# Path to the program that should be used for signaling MCH +PLUGPIPER="/sbin/plugpiper" + +######################################################################## +# The script +######################################################################## + +info () +{ + test -n "$LOGFILE" && echo "[$(date)] $ACTION: $*" >> "$LOGFILE" +} + +die () +{ + info "$*" + exit 0 +} + +test "$ACTION" = "remove" || die "Requested action not remove ($ACTION)" +test -d "$DEVROOT" || die "Device root is not a directory ($DEVROOT)" + +tmpdir="$DEVROOT/.tmp" + +# Use the tmpdir as a semaphore when multiple motes are removed +test -d "$tmpdir" && return +mkdir -p "$tmpdir" || die "Failed to create temp dir '$tmpdir'" + +# Remove devices where no tty link exists. +ls "$DEVROOT/" | while read motedir; +do + dir="$DEVROOT/$motedir" + test -e "$dir/tty/data" -a -e "$dir/tty/control" && continue; + info "Removing mote '$motedir'" + # Cannot die here since we need to remove the temp dir + mv "$dir" "$tmpdir" || info "Failed to move '$dir' to '$tmpdir'" +done + +# The temp directory is empty? +rmdir "$tmpdir" && return + +# Fatal but the mote host should still be notified +rm -rf "$tmpdir" || info "Failed to remove directory '$tmpdir'" + +# Notify mote host +test -p "$MCHPIPE" || die "Event pipe does not exist at '$MCHPIPE'" +"$PLUGPIPER" "$MCHPIPE" || die "Event program returned error" -- 2.11.4.GIT