[Novalug] Infinite loop in init script on embedded linux -- hopeless?

Jason Kohles jkohles at palantir.com
Tue Apr 24 09:29:48 EDT 2012


If it waits there forever, then I would guess that the script is running
before the network is initialized.  Embedded systems are often stripped
down, but I can confirm that this technique will work on a regular system,
as long as the script is running after the network is initialized (I.e. on a
CentOS/RHEL/Fedora machine it has to run after S10network).

The easiest way to timeout after the script has run for "too long" is just
to keep a counter that counts down and escapes if it runs out..

#!/bin/sh
counter=100
while [ $counter -gt 0 ]; do
    sleep 3
    ifconfig eth0 && break
    let counter--
done


-- 
Jason Kohles
Palantir Technologies | UNIX Systems Engineer
jkohles at palantir.com  | 703.957.5784


From:  Jeremy Trimble <jeremy.trimble at gmail.com>
Date:  Tuesday, April 24, 2012 9:08 AM
To:  "novalug at calypso.tux.org" <novalug at calypso.tux.org>
Subject:  [Novalug] Infinite loop in init script on embedded linux --
hopeless?

Hi all,

My company is developing some software for an embedded (timesys) Linux
target.  One of our developers (name omitted to protect the innocent) wrote
an script that would attempt to wait until the Ethernet interface comes up,
then try to set the system time using ntpdate.  It does this by continually
running 'ifconfig eth0' until that command returns 0 (indicating that 'eth0'
is now a valid interface on the machine).

Unfortunately, it seems that the interface won't come up until after all the
init scripts have run, so this script sits in an infinte loop, waiting for
the appearance of an interface which won't come up until this script exits.
Since the ethernet interface is effectively our only connectivity to the
processor where this code is running, this basically locks us out of the
unit.  The init script itself is stored on some integrated flash chip
somewhere in the system, not on removable media.

Our last hope is that maybe there's some quirk in the script or init itself
that we can take advantage of to make this script exit and allow the init
processing to continue, which will eventually result in the ethernet
interface becoming available.

Here's the script itself:

-----
#! /bin/sh

while [ 1 ]
do
    sleep 3
    ifconfig eth0
    test `echo $?` -eq 0 && break
done

# Run ntpdate here, but we never get here... :-(
-----

Any thoughts?  Does init have some kind of built-in timeout that will kick
in after a script has run for "too long"?

Thanks!
-Jeremy Trimble


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://calypso.tux.org/pipermail/novalug/attachments/20120424/5f1e54b0/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4634 bytes
Desc: not available
Url : http://calypso.tux.org/pipermail/novalug/attachments/20120424/5f1e54b0/attachment-0001.bin 


More information about the Novalug mailing list