You are not logged in.

#1 2009-08-25 15:00:01

adamd
Member
From: Melbourne/Aus
Registered: 2008-10-03
Posts: 2

udev bash script for autodialing usb huawei modem

Hey

I've got an interesting one here.

I have scripted up a bash script to be triggered by a udev add event for my usb modem. As far as I can tell the udev event is firing correctly according to udevadm.

ACTION=="add", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="1003", RUN+="/bin/sh /usr/local/bin/huawei_key.sh add"
ACTION=="remove", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="1003", RUN+="/bin/sh /usr/local/bin/huawei_key.sh remove"

And heres the code from the script

#!/bin/bash
#returns
#    0 Successfully ran script for case
#    1 wvdial process already running
#    2 wvdial was not terminated
#       3 script was not ran with ADD or REMOVE events
function getpid #get process id of running program
{
    
    pidof wvdial
    if [ "$?" = 0 ];
    then    
        pid=`pidof wvdial`
        running=1
    else
        running=0
    fi
}

function checkrun #check for a running copy of the program
{
    
    pidof wvdial
    if [ "$?" == "0" ];
    then  
            running=1
        else
            running=0
    fi
}

function killloop #a set of time kill commands to let the program exit gracefully
#two terms, one int and one kill with suitable gaps
{
        getpid
        checkrun
        if [ "$running" = "0" ];
        then
            return
        fi
        
    kill $pid
        sleep 10
    checkrun 
        if [ "$running" = "0" ];
        then
            return
       fi
        
    kill $pid
       sleep 10
        checkrun
        if [ "$running" = "0" ];
        then
            return
        fi
    
        kill -INT $pid
       sleep 5
        checkrun
        if [ "$running" = "0" ];
        then
            return
        fi
    
        kill -KILL $pid
        sleep 5
        checkrun
        if [ "$running" = "1" ];
        then
            exit 2 #process fails to die
        fi
    
}

function getlaststatus
{

    if [ -e "/var/run/huawie" ];
    then
        last=`cat /var/run/huawie`
    else
        last=0
    fi

}

if [ "$1" = "add" ]; 
then
    getlaststatus
    if [ "$last" = "1" ];
    then
        exit 1
    fi
    
    echo 1 > /var/run/huawie #block future udev events
    getpid
    
    if [ "$running" = "1" ];
    then
              exit 1
    fi
    modprobe usbserial
    sleep 6 #modem init time
    wvdial &
    exit 0

elif [ "$1" = "remove" ]; 
then
    getlaststatus
    if [ "$last" = "2" ];
    then
        exit 0
    fi

    echo 2 > /var/run/huawie
    killloop
    exit 0

else
    echo 3 > /var/run/huawie
    exit 3
fi

I'd appreciate any ideas, this one has me stumped

Edit:

Forgot to add that when I call the script manually it works like a charm.

Last edited by adamd (2009-08-25 15:04:06)

Offline

Board footer

Powered by FluxBB