xref: /titanic_51/usr/src/lib/libshell/common/tests/exit.sh (revision 126530de59706e11b663297e8dced72dc740d8cc)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27
28Command=${0##*/}
29integer Errors=0
30
31tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32trap "cd /; rm -rf $tmp" EXIT
33
34function abspath
35{
36        base=$(basename $SHELL)
37        cd ${SHELL%/$base}
38        newdir=$(pwd)
39        cd ~-
40        print $newdir/$base
41}
42#test for proper exit of shell
43builtin getconf
44ABSHELL=$(abspath)
45cd $tmp || { err_exit "cd $tmp failed"; exit 1; }
46print exit 0 >.profile
47${ABSHELL}  <<!
48HOME=$PWD \
49PATH=$PATH \
50SHELL=$ABSSHELL \
51$(
52	v=$(getconf LIBPATH)
53	for v in ${v//,/ }
54	do	v=${v#*:}
55		v=${v%%:*}
56		eval [[ \$$v ]] && eval print -n \" \"\$v=\"\$$v\"
57	done
58) \
59exec -c -a -ksh ${ABSHELL} -c "exit 1" 1>/dev/null 2>&1
60!
61status=$(echo $?)
62if	[[ -o noprivileged && $status != 0 ]]
63then	err_exit 'exit in .profile is ignored'
64elif	[[ -o privileged && $status == 0 ]]
65then	err_exit 'privileged .profile not ignored'
66fi
67if	[[ $(trap 'code=$?; echo $code; trap 0; exit $code' 0; exit 123) != 123 ]]
68then	err_exit 'exit not setting $?'
69fi
70cat > run.sh <<- "EOF"
71	trap 'code=$?; echo $code; trap 0; exit $code' 0
72	( trap 0; exit 123 )
73EOF
74if	[[ $($SHELL ./run.sh) != 123 ]]
75then	err_exit 'subshell trap on exit overwrites parent trap'
76fi
77cd ~- || err_exit "cd back failed"
78$SHELL -c 'builtin -f cmd getconf; getconf --"?-version"; exit 0' >/dev/null 2>&1 || err_exit 'ksh plugin exit failed -- was ksh built with CCFLAGS+=$(CC.EXPORT.DYNAMIC)?'
79
80exit $((Errors))
81