Problem running hush shell script from within a script

Denys Vlasenko vda.linux at googlemail.com
Sat Aug 7 14:18:04 UTC 2010


On Friday 06 August 2010 20:56, Adam Rosenberg wrote:
> I have made two scripts based on my tests so that you can see exactly
> what I am doing.  I can now reproduce this problem using the following
> steps:
> 
> boot the system
> 1.  mainscript.sh start
> 2.  module is loaded but the character device does not work
> 
> it works 100% of the time this way:
> boot the system
> 1.  subscript.sh start
> 2.  subscript.sh write "test"
> 3.  the "test" message appears on my LCD
> 
> if you then remove the module and try mainscript it works 50% of the time:
> 
> boot the system
> 1.  subscript.sh start
> 2.  subscript.sh write "test"
> 3.  the "test" message appears on my LCD
> 4.  subscript.sh stop
> 5.  mainscript.sh start
> 6.  sometimes the message "Test Message" appears and sometimes it does not

Looks like a race


> Here are the scripts:
> 
> mainscript.sh follows:
> <----------------- cut here ----------------->
> #!/bin/hush
> 
> SUBSCRIPT="/bin/subscript.sh"
> 
> if [ -z "$1" ]
> then
> 	echo "Usage $0 [start|stop]"
> 	exit
> fi
> 
> if [ "$1" == "start" ]
> then
> 	echo "Main: Starting subscript"
> 	$SUBSCRIPT start
> 	sleep 2
> 	echo "Main: Writing to device"
>         $SUBSCRIPT write "Test Message"
> 	exit
> fi

This looks correct: you gave the system 2 seconds to finish initialization
before you start using the device. One paranoid check you can add here for
debugging: print current time before and after the sleep, and check that
they do show 2 second gap:

date
sleep 2
date

> if [ "$1" = "stop" ]
> then
> 	$SUBSCRIPT stop
> 	exit
> fi
> <----------------- cut here ----------------->
> 
> subscript.sh follows:
> <----------------- cut here ----------------->
> #!/bin/hush
> 
> DEVMODULE="traxxLCDDrv.ko"
> DEVDEVICE="/dev/ami_traxx_lcd"
> 
> if [ -z "$1" ]
> then
> 	echo "Usage $0 [start|stop|write]"
> 	exit
> fi
> 
> if [ "$1" == "start" ]
> then
> 	echo "Sub: Loading module"
> 	modprobe $DEVMODULE
> 	exit
> fi
> 
> if [ "$1" = "stop" ]
> then
> 	echo "Sub: Removing module"
> 	rmmod $DEVMODULE
> 	exit
> fi
> 
> if [ "$1" = "write" ]
> then
> 	echo "Sub: echo -n \"000 000 $2\" > $DEVDEVICE"
> 	echo -n "000 000 $2" > $DEVDEVICE
> 	exit
> fi

Here you a writing blindly to $DEVDEVICE.
Imagine that it does not exist (yet).
What will happen? echo will create it as _an ordinary file_!

Let's add more paranoia/debugging before echo:
(1) let's see what kind of node it is,
(2) don't proceed to echo'ing if it isn't a char device,
(3) examine exitcode (failed redirection sets $? to 1):

	ls -l $DEVDEVICE
	test -c $DEVDEVICE || { echo "$DEVDEVICE is not a char dev node!"; exit 1; }
        echo -n "000 000 $2" > $DEVDEVICE || { echo "Writing to $DEVDEVICE failed: $?"; exit 1; }

Let me know what you observe when you try this.

-- 
vda


More information about the busybox mailing list