xref: /illumos-gate/usr/src/cmd/dtrace/test/tst/common/funcs/tst.badfreopen.ksh (revision 23b5c241225a8ade2b6b9f06ebb891ee459e3b02)
19512fe85Sahl#
29512fe85Sahl# CDDL HEADER START
39512fe85Sahl#
49512fe85Sahl# The contents of this file are subject to the terms of the
59512fe85Sahl# Common Development and Distribution License (the "License").
69512fe85Sahl# You may not use this file except in compliance with the License.
79512fe85Sahl#
89512fe85Sahl# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99512fe85Sahl# or http://www.opensolaris.org/os/licensing.
109512fe85Sahl# See the License for the specific language governing permissions
119512fe85Sahl# and limitations under the License.
129512fe85Sahl#
139512fe85Sahl# When distributing Covered Code, include this CDDL HEADER in each
149512fe85Sahl# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
159512fe85Sahl# If applicable, add the following below this CDDL HEADER, with the
169512fe85Sahl# fields enclosed by brackets "[]" replaced with your own identifying
179512fe85Sahl# information: Portions Copyright [yyyy] [name of copyright owner]
189512fe85Sahl#
199512fe85Sahl# CDDL HEADER END
209512fe85Sahl#
219512fe85Sahl
229512fe85Sahl#
239512fe85Sahl# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
249512fe85Sahl# Use is subject to license terms.
259512fe85Sahl#
269512fe85Sahl#ident	"%Z%%M%	%I%	%E% SMI"
279512fe85Sahl
289512fe85Sahlscript()
299512fe85Sahl{
309512fe85Sahl	$dtrace -wq -o $tmpfile -s /dev/stdin 2> $errfile <<EOF
319512fe85Sahl	BEGIN
329512fe85Sahl	{
339512fe85Sahl		/*
349512fe85Sahl		 * All of these should fail...
359512fe85Sahl		 */
369512fe85Sahl		freopen("..");
379512fe85Sahl		freopen("%s", ".");
389512fe85Sahl		freopen("%c%c", '.', '.');
399512fe85Sahl		freopen("%c", '.');
409512fe85Sahl
419512fe85Sahl		/*
429512fe85Sahl		 * ...so stdout should still be open here.
439512fe85Sahl		 */
449512fe85Sahl		printf("%d", ++i);
459512fe85Sahl
469512fe85Sahl		freopen("%s%s", ".", ".");
479512fe85Sahl		freopen("%s%s", ".", ".");
489512fe85Sahl
499512fe85Sahl		printf("%d", ++i);
509512fe85Sahl	}
519512fe85Sahl
529512fe85Sahl	BEGIN
539512fe85Sahl	/i == 2/
549512fe85Sahl	{
559512fe85Sahl		/*
569512fe85Sahl		 * ...and here.
579512fe85Sahl		 */
589512fe85Sahl		printf("%d\n", ++i);
599512fe85Sahl		exit(0);
609512fe85Sahl	}
619512fe85Sahl
629512fe85Sahl	BEGIN
639512fe85Sahl	{
649512fe85Sahl		exit(1);
659512fe85Sahl	}
669512fe85SahlEOF
679512fe85Sahl}
689512fe85Sahl
69*23b5c241Stomeeif [ $# != 1 ]; then
70*23b5c241Stomee	echo expected one argument: '<'dtrace-path'>'
71*23b5c241Stomee	exit 2
72*23b5c241Stomeefi
73*23b5c241Stomee
74*23b5c241Stomeedtrace=$1
759512fe85Sahltmpfile=/tmp/tst.badfreopen.$$
769512fe85Sahlerrfile=/tmp/tst.badfreopen.$$.stderr
779512fe85Sahl
789512fe85Sahlscript
799512fe85Sahlstatus=$?
809512fe85Sahl
819512fe85Sahlif [ "$status" -eq 0 ]; then
829512fe85Sahl	i=`cat $tmpfile`
839512fe85Sahl
849512fe85Sahl	if [[ $i != "123" ]]; then
859512fe85Sahl		echo "$0: unexpected contents in $tmpfile: " \
869512fe85Sahl		    "expected 123, found $i"
879512fe85Sahl		status=100
889512fe85Sahl	fi
899512fe85Sahl
909512fe85Sahl	i=`wc -l $errfile | nawk '{ print $1 }'`
919512fe85Sahl
929512fe85Sahl	if [ "$i" -lt 6 ]; then
939512fe85Sahl		echo "$0: expected at least 6 lines of stderr, found $i lines"
949512fe85Sahl		status=101
959512fe85Sahl	fi
969512fe85Sahlelse
979512fe85Sahl	cat $errfile > /dev/fd/2
989512fe85Sahlfi
999512fe85Sahl
1009512fe85Sahlrm $tmpfile $errfile
1019512fe85Sahl
1029512fe85Sahlexit $status
103