Re: GNU bash, 3.00.15(1)-release, referenced cmd in cwd executes alternate cmd

2007-03-01 Thread Bob Proulx
Matthew Woehlke wrote:
> Apparently selectively shadowing libc is non-trivial... any 
> suggestions/hints?

Not so much non-trivial as perhaps non-obvious.  The dynamic loader is
part of libc and so by the time the program tries to use
LD_LIBRARY_PATH it is already too late because it has already loaded
the system libc.

Therefore LD_LIBRARY_PATH is not useful for overriding libc and only
works for non-libc libraries.  To override libc you have to call the
ld.so directly.  Also you don't want LD_LIBRARY_PATH to hang around
causing trouble for other children processes that might need something
still different so you want to use the --library-path option instead,
then there is no lingering smell.

Given the following setup with copies of glibc and other lib files in
a local "sandbox".

  ./mylib/ld-linux.so.2
  ./mylib/libc.so.6
  ./mylibexec/myprog

Then this script in ./mybin/myprog works:

  #!/bin/bash
  MYAPPDIR=$(dirname $(dirname $0))
  exec -a $MYAPPDIR/mybin/myprog $MYAPPDIR/mylib/ld-linux.so.2 --library-path 
$MYAPPDIR/mylib $MYAPPDIR/mylibexec/myprog "$@"

Using the bash specific 'exec -a ALIAS' works well here.  It keeps the
application itself from knowing that it has been overridden.  It does
not need to know, and depending upon the application knowing would
confuse it, such as when it tries to use $(dirname $0) for any reason.

The key points are:

 * Call the ld.so (aka ld-linux.so.2) directly
 * Don't use LD_LIBRARY_PATH (because it messes up later progs)
 * Put the real program off to the side and use a wrapper
 * Use 'exec -a NAME' to hide this from the program itself

Here are pointers to previous messages that I have posted about how to
use ld.so wrapping to get a particular libc.

  http://gcc.gnu.org/ml/gcc-help/2006-07/msg00126.html
  http://svn.haxx.se/users/archive-2005-05/1727.shtml

Hope that helps!
Bob


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: Bash getopts option

2007-03-01 Thread Bob Proulx
Matthew Woehlke wrote:
> Shanks wrote:
> >The above code does not parse -help option. Any suggestions
> 
> I don't think getopts knows how to parse words as options (and not as 
> non-GNU-style, certainly). You are probably better off writing your own 
> parser, something like this:

Personally I prefer 'getopt'.  On my Debian system there are examples
shipped with the system in:
  /usr/share/doc/util-linux/examples/getopt-parse.bash.gz 
  /usr/share/doc/util-linux/examples/getopt-test.bash.gz

And it is useful to review the GNU coding standards.

  
http://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html#Command_002dLine-Interfaces

Here is a simple but complete example.  This produces output suitable
to generate the man page with help2man.

Enjoy,
Bob

#!/bin/sh
#
# A sample program illustrating shell option parsing.
#
# Copyright (C) 2007 by Bob Proulx <[EMAIL PROTECTED]>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

version=0.0

progname=$(basename $0)

print_help()
{
cat <<'EOF'
Usage: exampleprog [options] SOURCE...
  or:  exampleprog [options] DIRECTORY

Do something with something.  Write your man page description here.

Options:
  --help  print this help message
  --version   print program version
  -a, --alpha do alpa
  -b, --bravo=STRING  do bravo
  -d, --debug debug program
  -q, --quiet quiet output
  -v, --verbose   verbose output

Examples:

The most common use is to run it like this.

  $ exampleprog

But sometimes like this.

  $ exampleprog -q -a --bravo=foo

Report bugs to .
EOF
}

print_version()
{
cat .
There is NO WARRANTY, to the extent permitted by law.

Written by Bob Proulx.
EOF
}

SHORTOPTS="ab:dqv"
LONGOPTS="help,version,alpha,bravo:,debug,quiet,verbose"

if $(getopt -T >/dev/null 2>&1) ; [ $? = 4 ] ; then # New longopts getopt.
OPTS=$(getopt -o $SHORTOPTS --long $LONGOPTS -n "$progname" -- "$@")
else # Old classic getopt.
# Special handling for --help and --version on old getopt.
case $1 in --help) print_help ; exit 0 ;; esac
case $1 in --version) print_version ; exit 0 ;; esac
OPTS=$(getopt $SHORTOPTS "$@")
fi

if [ $? -ne 0 ]; then
echo "'$progname --help' for more information" 1>&2
exit 1
fi

eval set -- "$OPTS"

alpha=false
bravo=""
debug=false
quiet=false
verbose=false
while [ $# -gt 0 ]; do
: debug: $1
case $1 in
--help)
print_help
exit 0
;;
--version)
print_version
exit 0
;;
-a|--alpha)
alpha=true
shift
;;
-b|--bravo)
bravo=$2
shift 2
;;
-d|--debug)
debug=true
shift
;;
-q|--quiet)
quiet=true
shift
;;
-v|--verbose)
verbose=true
shift
;;
--)
shift
break
;;
*)
echo "Internal Error: option processing error: $1" 1>&2
exit 1
;;
esac
done

if $verbose; then
   echo "Verbose output here."
fi

if ! $quiet; then
echo Say stuff here.
fi

$debug && echo No bugs found here.

exit 0


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash