/ base / scripts / rtai-load.in
rtai-load.in
  1  #!/bin/sh
  2  # RTAI application loader script.
  3  # (Processes the .runinfo files)
  4  # April 2003, <rpm@xenomai.org>
  5  
  6  usage='usage: rtai-load [[dir:]target]'
  7  prefix="@prefix@"
  8  exec_prefix="@exec_prefix@"
  9  insmod=/sbin/insmod
 10  modprobe=/sbin/modprobe
 11  rmmod=/sbin/rmmod
 12  modext=@RTAI_MODULE_EXT@
 13  verbose=0
 14  spc=" 	"
 15  
 16  if test "x$1" = x--help; then
 17    echo $usage
 18    exit 0
 19  fi
 20  
 21  if test "x$1" = x--verbose; then
 22    verbose=1
 23    shift
 24  fi
 25  
 26  qual_target=$1
 27  target_dir=`echo $qual_target|cut -d: -f1`
 28  target_name=`echo $qual_target|cut -s -d: -f2`
 29  if test "x$target_dir" = x; then
 30     target_dir=`pwd`
 31  fi
 32  if test "x$target_name" = x; then
 33     if `echo $target_dir | grep -q /`; then
 34        target_name=default
 35     else
 36        target_name=$target_dir
 37        target_dir=.
 38     fi
 39  fi
 40  
 41  run_info_file=$target_dir/.runinfo
 42  
 43  if test \! -r $run_info_file; then
 44     echo "rtai-load: cannot find run info: $run_info_file"
 45     exit 2
 46  fi
 47  
 48  if test $target_name = default; then
 49    target_name=`cut -s -d: -f1 $run_info_file | head -1`
 50    if test "x$target_name" = x; then
 51       echo "rtai-load: no target defined in $run_info_file"
 52       exit 2
 53    fi
 54  fi
 55  
 56  stanza=`grep "^[$spc]*$target_name[$spc]*:" $run_info_file`
 57  
 58  if test "x$stanza" = x; then
 59     have_user_command_option=`cut -d: -f1 .runinfo | grep user_command`
 60     if test "x$have_user_command_option" = x; then
 61         echo "passo di qui"
 62         echo "rtai-load: cannot find run info for target $target_name"
 63         exit 2
 64     else
 65         target_name=user_command
 66         stanza=`grep "^[$spc]*$target_name[$spc]*:" $run_info_file`
 67     fi
 68  fi
 69  
 70  rtai_moddir=`grep "^[$spc]*rtai_moddir[$spc]*=" $run_info_file`
 71  user_moddir=`grep "^[$spc]*user_moddir[$spc]*=" $run_info_file`
 72  
 73  if test "x$rtai_moddir" = x; then
 74    eval rtai_moddir='${DESTDIR}@RTAI_MODULE_DIR@'
 75  else
 76    eval $rtai_moddir
 77  fi
 78  
 79  eval $user_moddir
 80  
 81  if `$modprobe -nq adeos > /dev/null 2>&1`; then
 82     target_deps="adeos+"
 83  else
 84     target_deps=
 85  fi
 86  
 87  target_deps="${target_deps}trace+hal+malloc+`echo $stanza | cut -d: -f2`"
 88  target_info=`echo $stanza | cut -d: -f3`
 89  target_mesg=`echo $stanza | cut -s -d: -f4`
 90  
 91  if test "$target_name" = user_command; then
 92      target_info=`echo $target_info | sed s/";"/" $*\;"/` 
 93      shift $#
 94  fi
 95  
 96  test $verbose = 1 && echo "Running $target_name from $run_info_file"
 97  
 98  if test \! "x`type -t sudo`" = x; then
 99    sudo=sudo
100  fi
101  
102  if test \! "x$target_mesg" = x; then
103    case "$target_mesg" in
104    control_c)
105      target_mesg="Type ^C to stop this application."
106      ;;
107    esac
108    echo "*"
109    echo "*"
110    echo "* $target_mesg"
111    echo "*"
112    echo "*"
113  fi
114  
115  sync
116  
117  loadlist=`cat /proc/modules | ( \
118    while read lm; do \
119      set -- $lm; \
120      loadlist="$loadlist $1"; \
121    done  ; echo $loadlist )`
122  
123  ( cd $target_dir ;
124  
125  inslist=""
126  children_pids=""
127  
128  cleanup () {
129  
130      if test \! "x$children_pids" = x; then
131  #      $sudo kill -SIGINT $children_pids > /dev/null 2>&1 see comment below
132         $sudo kill -INT $children_pids > /dev/null 2>&1
133         children_pids=""
134         sleep 1
135      fi
136      if test \! "x$inslist" = x; then
137         for mod in $inslist ; do
138  	  test $verbose = 1 && echo "+ $rmmod $mod"
139  	  $sudo $rmmod $mod
140         done
141         inslist=""
142      fi
143  }
144  
145  #trap cleanup SIGINT  this is an old reminder, it seems it must be like below
146  trap cleanup INT
147  
148  while test -n "$target_deps" ; do
149    mod=`echo $target_deps|cut -d+ -f1`
150    target_deps=`echo $target_deps|cut -s -d+ -f2-`
151    if test -h $rtai_moddir/rtai_$mod$modext; then
152       # Substitute the symlink with the target name since
153       # new modutils (>= 2.5.48) will use the actual name.
154       mod=`ls -l $rtai_moddir/rtai_$mod$modext|sed -e "s,.* \-> .*rtai_\([a-z0-9A-Z]*\)$modext.*,\1,"`
155    fi
156    if `echo $loadlist | grep -vq "\\brtai_$mod\\b"`; then
157       # If we cannot find the module in our install dir, try Linux's
158       # one. If the latter fails too, assume the feature is built into
159       # the scheduler or not needed. Obviously, we should _always_ find
160       # the scheduler...
161       if test -r $rtai_moddir/rtai_$mod$modext; then
162          test $verbose = 1 && echo "+ $insmod $rtai_moddir/rtai_$mod$modext"
163          $sudo $insmod $rtai_moddir/rtai_$mod$modext
164  	if test $? = 0 ; then
165             inslist="rtai_$mod $inslist"
166             loadlist="$loadlist $mod"
167  	else
168  	   echo "ERROR: cannot load $rtai_moddir/rtai_$mod$modext"
169  	   break
170          fi
171       elif `$modprobe -n $mod > /dev/null 2>&1`; then
172          test $verbose = 1 && echo "+ $modprobe $mod"
173          $sudo $modprobe $mod
174          inslist="$mod $inslist"
175          loadlist="$loadlist $mod"
176       fi
177    fi
178  done
179  
180  while test -n "$target_info" ; do
181    action=`echo $target_info|cut -d';' -f1`
182    target_info=`echo $target_info|cut -s -d';' -f2-`
183    set -- $action
184  
185    case "$1" in
186  
187      push)
188  
189  	mod=`basename $2 $modext`
190          if test -r $rtai_moddir/$mod$modext; then
191             modpath="$rtai_moddir/$mod"
192          elif test -r $user_moddir/$mod$modext; then
193             modpath="$user_moddir/$mod"
194          else
195             modpath="$mod"
196          fi
197  	if `echo $loadlist | grep -vq "\\b$mod\\b"`; then
198  	   test $verbose = 1 && echo "+ $insmod $modpath$modext"
199  	   $sudo $insmod $modpath$modext
200  	   if test $? = 0; then
201                loadlist="$loadlist $mod"
202  	      inslist="$mod $inslist"
203  	   else
204  	      echo "ERROR: cannot load $modpath$modext"
205  	   fi
206  	fi
207  	;;
208  
209      pop)
210  
211  	shift
212  	if test "x$*" = x; then
213             rmlist=`echo $inslist | cut -s -f1 -d' '`
214          else
215             rmlist=$*
216  	fi
217          _inslist="$inslist"
218  	if test \! "x$rmlist" = x; then
219             for mod in $rmlist ; do
220  	      test $verbose = 1 && echo "+ $rmmod $mod"
221  	      $sudo $rmmod $mod && _inslist=`echo $_inslist | sed -e "s,$mod,,g"`
222             done
223          inslist="$_inslist"
224  	fi
225  	;;
226  
227      popall|flush)
228  
229          cleanup
230  	;;
231  
232      *)
233  
234          case "$1" in
235  	  exec|spawn) shift ;;
236  	  klog) set -- \! tail -f /var/log/messages ;;
237  	esac
238  	e=`echo $* | sed -e "s,^[$spc]*![$spc]*\\(.*\\)$,\\1,"`
239          test "$e" = "$*" && suflag= || suflag=$sudo
240  	set -- $e
241  	e=`echo $* | sed -e "s,\\(.*\\)&[$spc]*$,\\1,"`
242          test "$e" = "$*" && waitflag=1 || waitflag=0
243  	set -- $e
244  	test $verbose = 1 && echo "+ spawning command ($*)"
245  	if test $waitflag = 1; then
246             $suflag $*
247          else
248             $suflag $* &
249  	   children_pids="$! $children_pids"
250  	fi
251  	;;
252    esac
253  
254  done )
255  
256  exit 0