1178476Sjb#
2178476Sjb# CDDL HEADER START
3178476Sjb#
4178476Sjb# The contents of this file are subject to the terms of the
5178476Sjb# Common Development and Distribution License (the "License").
6178476Sjb# You may not use this file except in compliance with the License.
7178476Sjb#
8178476Sjb# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178476Sjb# or http://www.opensolaris.org/os/licensing.
10178476Sjb# See the License for the specific language governing permissions
11178476Sjb# and limitations under the License.
12178476Sjb#
13178476Sjb# When distributing Covered Code, include this CDDL HEADER in each
14178476Sjb# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178476Sjb# If applicable, add the following below this CDDL HEADER, with the
16178476Sjb# fields enclosed by brackets "[]" replaced with your own identifying
17178476Sjb# information: Portions Copyright [yyyy] [name of copyright owner]
18178476Sjb#
19178476Sjb# CDDL HEADER END
20178476Sjb#
21178476Sjb
22178476Sjb#
23178476Sjb# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24178476Sjb# Use is subject to license terms.
25178476Sjb#
26178476Sjb# ident	"%Z%%M%	%I%	%E% SMI"
27178476Sjb
28178476Sjb#
29178476Sjb# This script tests that several of the the mib:::tcp* probes fire and fire
30178476Sjb# with a valid args[0].
31178476Sjb#
32178476Sjb
33178476Sjbif [ $# != 1 ]; then
34178476Sjb	echo expected one argument: '<'dtrace-path'>'
35178476Sjb	exit 2
36178476Sjbfi
37178476Sjb
38178476Sjbdtrace=$1
39178476Sjbdtraceout=/tmp/dtrace.out.$$
40178476Sjbtimeout=15
41178476Sjbport=2000
42178476Sjb
43178476Sjbif [ -f $dtraceout ]; then
44178476Sjb	rm -f $dtraceout
45178476Sjbfi
46178476Sjb
47178476Sjbscript()
48178476Sjb{
49178476Sjb	$dtrace -o $dtraceout -s /dev/stdin <<EOF
50178476Sjb	mib:::tcpActiveOpens
51178476Sjb	{
52178476Sjb		opens = args[0];
53178476Sjb	}
54178476Sjb
55178476Sjb	mib:::tcpOutDataBytes
56178476Sjb	{
57178476Sjb		bytes = args[0];
58178476Sjb	}
59178476Sjb
60178476Sjb	mib:::tcpOutDataSegs
61178476Sjb	{
62178476Sjb		segs = args[0];
63178476Sjb	}
64178476Sjb
65178476Sjb	profile:::tick-10msec
66178476Sjb	/opens && bytes && segs/
67178476Sjb	{
68178476Sjb		exit(0);
69178476Sjb	}
70178476Sjb
71178476Sjb	profile:::tick-1s
72178476Sjb	/n++ >= 10/
73178476Sjb	{
74178476Sjb		exit(1);
75178476Sjb	}
76178476SjbEOF
77178476Sjb}
78178476Sjb
79178476Sjbserver()
80178476Sjb{
81178476Sjb	perl /dev/stdin /dev/stdout << EOF
82178476Sjb	use strict;
83178476Sjb	use Socket;
84178476Sjb
85178476Sjb	socket(S, AF_INET, SOCK_STREAM, getprotobyname('tcp'))
86178476Sjb	    or die "socket() failed: \$!";
87178476Sjb
88178476Sjb	setsockopt(S, SOL_SOCKET, SO_REUSEADDR, 1)
89178476Sjb	    or die "setsockopt() failed: \$!";
90178476Sjb
91178476Sjb	my \$addr = sockaddr_in($port, INADDR_ANY);
92178476Sjb	bind(S, \$addr) or die "bind() failed: \$!";
93178476Sjb	listen(S, SOMAXCONN) or die "listen() failed: \$!";
94178476Sjb
95178476Sjb	while (1) {
96178476Sjb		next unless my \$raddr = accept(SESSION, S);
97178476Sjb
98178476Sjb		while (<SESSION>) {
99178476Sjb		}
100178476Sjb
101178476Sjb		close SESSION;
102178476Sjb	}
103178476SjbEOF
104178476Sjb}
105178476Sjb
106178476Sjbclient()
107178476Sjb{
108178476Sjb	perl /dev/stdin /dev/stdout <<EOF
109178476Sjb	use strict;
110178476Sjb	use Socket;
111178476Sjb
112178476Sjb	my \$peer = sockaddr_in($port, INADDR_ANY);
113178476Sjb
114178476Sjb	socket(S, AF_INET, SOCK_STREAM, getprotobyname('tcp'))
115178476Sjb	    or die "socket() failed: \$!";
116178476Sjb
117178476Sjb	connect(S, \$peer) or die "connect failed: \$!";
118178476Sjb
119178476Sjb	for (my \$i = 0; \$i < 10; \$i++) {
120178476Sjb		send(S, "There!", 0) or die "send() failed: \$!";
121178476Sjb		sleep (1);
122178476Sjb	}
123178476SjbEOF
124178476Sjb}
125178476Sjb
126178476Sjbscript &
127178476Sjbdtrace_pid=$!
128178476Sjb
129178476Sjb#
130178476Sjb# Sleep while the above script fires into life. To guard against dtrace dying
131178476Sjb# and us sleeping forever we allow 15 secs for this to happen. This should be
132178476Sjb# enough for even the slowest systems.
133178476Sjb#
134178476Sjbwhile [ ! -f $dtraceout ]; do
135178476Sjb	sleep 1
136178476Sjb	timeout=$(($timeout-1))
137178476Sjb	if [ $timeout -eq 0 ]; then
138178476Sjb		echo "dtrace failed to start. Exiting."
139178476Sjb		exit 1
140178476Sjb	fi
141178476Sjbdone
142178476Sjb
143178476Sjbserver &
144178476Sjbserver_pid=$!
145178476Sjbsleep 2
146178476Sjbclient &
147178476Sjbclient_pid=$!
148178476Sjb
149178476Sjbwait $dtrace_pid
150178476Sjbstatus=$?
151178476Sjb
152178476Sjbkill $server_pid
153178476Sjbkill $client_pid
154178476Sjb
155178476Sjbexit $status
156