1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24#
25
26#
27# This test checks whether the Solaris kernel can directly execute compiled
28# shell code.
29#
30# This was reported as CR #6862121 ("shbinexec kernel module defunct"):
31# ------------ snip ------------
32# [Originally reported by Sun Japan]
33# The new shbinexec kernel module added in B106 is defunct, originally
34# caused by my mismerge of the original development tree and later
35# because the matching test module didn't test it correctly (April
36# quickly discovered the problem but the issue drowned in the cleanup
37# putbacks ).
38# Frequency
39#    Always
40# Regression
41#    No
42# Steps to Reproduce
43#    $ cat test1.sh
44# print hello
45# printf "args=%s\n" "$@"
46# $ shcomp test1.sh test1
47# # note: this MUST be bash since ksh93 has special support for compiled shell
48# # scripts which causes the kernel module to be bypassed (that's why the tes
49# # never worked)
50# $ bash -c './test1 "a b" "c" "d"'
51# Expected Result
52#    hello
53# args=a a1
54# args=b
55# args=c
56# Actual Result
57#    ./test1: line 1: a: not found
58# Error Message(s)
59#    ./test1: line 1: a: not found
60# Test Case
61#    See above.
62# ------------ snip ------------
63#
64
65# test setup
66function err_exit
67{
68	print -u2 -n "\t"
69	print -u2 -r ${Command}[$1]: "${@:2}"
70	(( Errors < 127 && Errors++ ))
71}
72alias err_exit='err_exit $LINENO'
73
74set -o nounset
75Command=${0##*/}
76integer Errors=0
77
78typeset ocwd
79typeset tmpdir
80typeset out
81
82# create temporary test directory
83ocwd="$PWD"
84tmpdir="$(mktemp -t -d "test_sun_solaris_cr_6862121_shbinexec_kernel_module_defunct.XXXXXXXX")" || err_exit "Cannot create temporary directory"
85
86cd "${tmpdir}" || { err_exit "cd ${tmpdir} failed." ; exit $((Errors)) ; }
87
88
89# run tests
90{
91cat <<EOF
92	print hello
93	printf "args=%s\n" "\$@"
94EOF
95} >script1.sh
96
97# Compile script (note we use the platform's /usr/bin/shcomp, _not_ ${SHCOMP})
98/usr/bin/shcomp "script1.sh" "script1" || err_exit "shcomp failed with error=$?"
99
100[[ -x "./script1" ]] || err_exit "Script script1 not executable"
101out="$(/usr/bin/bash -c './script1 a b "c d"' 2>&1 )" || err_exit "Compiled script failed to execute, error=$?"
102[[ "${out}" == $'hello\nargs=a\nargs=b\nargs=c d' ]] || err_exit "Expected xxx, got $(printf "%q\n" "$out")"
103
104# cleanup
105rm "script1" "script1.sh"
106cd "${ocwd}"
107rmdir "${tmpdir}" || err_exit "Cannot remove temporary directory ${tmpdir}".
108
109# tests done
110exit $((Errors))
111