1178476Sjb#!/bin/ksh -p
2178476Sjb#
3178476Sjb# CDDL HEADER START
4178476Sjb#
5178476Sjb# The contents of this file are subject to the terms of the
6178476Sjb# Common Development and Distribution License (the "License").
7178476Sjb# You may not use this file except in compliance with the License.
8178476Sjb#
9178476Sjb# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10178476Sjb# or http://www.opensolaris.org/os/licensing.
11178476Sjb# See the License for the specific language governing permissions
12178476Sjb# and limitations under the License.
13178476Sjb#
14178476Sjb# When distributing Covered Code, include this CDDL HEADER in each
15178476Sjb# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16178476Sjb# If applicable, add the following below this CDDL HEADER, with the
17178476Sjb# fields enclosed by brackets "[]" replaced with your own identifying
18178476Sjb# information: Portions Copyright [yyyy] [name of copyright owner]
19178476Sjb#
20178476Sjb# CDDL HEADER END
21178476Sjb#
22178476Sjb
23178476Sjb#
24178476Sjb# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25178476Sjb# Use is subject to license terms.
26178476Sjb#
27178476Sjb# ident	"%Z%%M%	%I%	%E% SMI"
28178476Sjb
29178476Sjb#
30178476Sjb# This test verifies that performing a dlclose(3dl) on a library doesn't
31178476Sjb# cause existing pid provider probes to become invalid.
32178476Sjb#
33178476Sjb
34178476Sjbif [ $# != 1 ]; then
35178476Sjb	echo expected one argument: '<'dtrace-path'>'
36178476Sjb	exit 2
37178476Sjbfi
38178476Sjb
39178476Sjbdtrace=$1
40178476SjbDIR=/var/tmp/dtest.$$
41178476Sjb
42178476Sjbmkdir $DIR
43178476Sjbcd $DIR
44178476Sjb
45178476Sjbcat > Makefile <<EOF
46178476Sjball: main livelib.so deadlib.so
47178476Sjb
48178476Sjbmain: main.o prov.o
49178476Sjb	cc -o main main.o
50178476Sjb
51178476Sjbmain.o: main.c
52178476Sjb	cc -c main.c
53178476Sjb
54178476Sjb
55178476Sjblivelib.so: livelib.o prov.o
56178476Sjb	cc -z defs -G -o livelib.so livelib.o prov.o -lc
57178476Sjb
58178476Sjblivelib.o: livelib.c prov.h
59178476Sjb	cc -c livelib.c
60178476Sjb
61178476Sjbprov.o: livelib.o prov.d
62178476Sjb	$dtrace -G -s prov.d livelib.o
63178476Sjb
64178476Sjbprov.h: prov.d
65178476Sjb	$dtrace -h -s prov.d
66178476Sjb
67178476Sjb
68178476Sjbdeadlib.so: deadlib.o
69178476Sjb	cc -z defs -G -o deadlib.so deadlib.o -lc
70178476Sjb
71178476Sjbdeadlib.o: deadlib.c
72178476Sjb	cc -c deadlib.c
73178476Sjb
74178476Sjbclean:
75178476Sjb	rm -f main.o livelib.o prov.o prov.h deadlib.o
76178476Sjb
77178476Sjbclobber: clean
78178476Sjb	rm -f main livelib.so deadlib.so
79178476SjbEOF
80178476Sjb
81178476Sjbcat > prov.d <<EOF
82178476Sjbprovider test_prov {
83178476Sjb	probe go();
84178476Sjb};
85178476SjbEOF
86178476Sjb
87178476Sjbcat > livelib.c <<EOF
88178476Sjb#include "prov.h"
89178476Sjb
90178476Sjbvoid
91178476Sjbgo(void)
92178476Sjb{
93178476Sjb	TEST_PROV_GO();
94178476Sjb}
95178476SjbEOF
96178476Sjb
97178476Sjbcat > deadlib.c <<EOF
98178476Sjbvoid
99178476Sjbgo(void)
100178476Sjb{
101178476Sjb}
102178476SjbEOF
103178476Sjb
104178476Sjb
105178476Sjbcat > main.c <<EOF
106178476Sjb#include <dlfcn.h>
107178476Sjb#include <unistd.h>
108178476Sjb#include <stdio.h>
109178476Sjb
110178476Sjbstatic void
111178476Sjbfoo(void)
112178476Sjb{
113178476Sjb	(void) close(-1);
114178476Sjb}
115178476Sjb
116178476Sjbint
117178476Sjbmain(int argc, char **argv)
118178476Sjb{
119178476Sjb	void *live;
120178476Sjb
121178476Sjb	if ((live = dlopen("./livelib.so", RTLD_LAZY | RTLD_LOCAL)) == NULL) {
122178476Sjb		printf("dlopen of livelib.so failed: %s\n", dlerror());
123178476Sjb		return (1);
124178476Sjb	}
125178476Sjb
126178476Sjb	(void) dlclose(live);
127178476Sjb
128178476Sjb	foo();
129178476Sjb
130178476Sjb	return (0);
131178476Sjb}
132178476SjbEOF
133178476Sjb
134211545Srpaulo/usr/bin/make > /dev/null
135178476Sjbif [ $? -ne 0 ]; then
136178476Sjb	print -u2 "failed to build"
137178476Sjb	exit 1
138178476Sjbfi
139178476Sjb
140178476Sjbscript() {
141178476Sjb	$dtrace -c ./main -s /dev/stdin <<EOF
142178476Sjb	pid\$target:a.out:foo:entry
143178476Sjb	{
144178476Sjb		gotit = 1;
145178476Sjb		exit(0);
146178476Sjb	}
147178476Sjb
148178476Sjb	tick-1s
149178476Sjb	/i++ == 5/
150178476Sjb	{
151178476Sjb		printf("test timed out");
152178476Sjb		exit(1);
153178476Sjb	}
154178476Sjb
155178476Sjb	END
156178476Sjb	/!gotit/
157178476Sjb	{
158178476Sjb		printf("program ended without hitting probe");
159178476Sjb		exit(1);
160178476Sjb	}
161178476SjbEOF
162178476Sjb}
163178476Sjb
164178476Sjbscript
165178476Sjbstatus=$?
166178476Sjb
167178476Sjbcd /
168211545Srpaulo/bin/rm -rf $DIR
169178476Sjb
170178476Sjbexit $status
171