isaexec(1) as a shell script

/usr/lib/isaexecis often used to provide automatic selection of a 32 vs 64 bit binary, however it can actually do much more than that it can pick between sparcv8+vis and sparcv8 for example. What it can't do in its distributed form is pick between SPARC and x86 variants because it is a 32 bit binary.

I wanted a single ~/bin in my home dir that could cope with 32 vs 64 and SPARC vs x86 and also allow me to have CPU capability variants as well, ie sparcv9+vis2 and a generic sparcv8 variant. So I rewrote isaexec as a simple shell script, I don't know how long ago I did this but it was probably some time during Solaris 7 development (which is when isaexec first appeared), anyway below is the shell script. I have subdirs in ~/bin for each cpu/architecture and all the binaries are links to ~/bin/isaexec.sh

#!/bin/ksh

fname=`basename $0`
pathname=`dirname $0`

if [ ! -x /usr/bin/isalist ]; then
        arch=`arch`
        if [ ! -x $pathname/$arch/$fname ]; then
                echo "$0: cannot find the ISA list";
        else
                exec $pathname/$arch/$fname
                echo "$0: cannon find/execute $fname in ISA subdirectories"
        fi
fi


for isa in `/usr/bin/isalist` ; do
        execpath="${pathname}/${isa}/${fname}"
        
        if [ -x $execpath ]; then
                exec $execpath "$@"
                echo "$0 exec $execpath failed"
        fi
done

echo "$0: cannon find/execute $fname in ISA subdirectories"
exit 1;

This far from perfect shell script from a performance view point and could probably use much more shell builtin functionality if ksh (or ksh93) was used instead.

Comments:

The ksh93 version of the script would look like this (per http://www.opensolaris.org/os/project/shell/shellstyle/ and some security issues fixed):
-- snip --
#!/usr/bin/ksh93

builtin basename
builtin dirname

IFS=''

typeset fname="$(basename "$0")"
typeset pathname="$(dirname $0)"

if [[ ! -x /usr/bin/isalist ]]; then
arch="$(/usr/bin/arch)"
if [[ ! -x "$pathname/$arch/$fname" ]]; then
print -n2 -f "%s: cannot find the ISA list" "$0";
else
exec "$pathname/$arch/$fname"
print -n2 -f "%s: cannon find/execute %s in ISA subdirectories" "$0" "$fname"
fi
fi

for isa in $(/usr/bin/isalist) ; do
execpath="${pathname}/${isa}/${fname}"

if [[ -x "$execpath" ]]; then
exec $execpath "$@"
print -n2 -f "%s: exec %s failed" "$0" "$execpath"
fi
done

print -n2 -f "%s: cannon find/execute %s in ISA subdirectories" "$0" "$fname"
exit 1;
-- snip --

Posted by Roland Mainz on February 04, 2008 at 03:11 PM GMT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

DarrenMoffat

Search

Categories
Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today