1*9512fe85Sahl /*
2*9512fe85Sahl  * CDDL HEADER START
3*9512fe85Sahl  *
4*9512fe85Sahl  * The contents of this file are subject to the terms of the
5*9512fe85Sahl  * Common Development and Distribution License (the "License").
6*9512fe85Sahl  * You may not use this file except in compliance with the License.
7*9512fe85Sahl  *
8*9512fe85Sahl  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9512fe85Sahl  * or http://www.opensolaris.org/os/licensing.
10*9512fe85Sahl  * See the License for the specific language governing permissions
11*9512fe85Sahl  * and limitations under the License.
12*9512fe85Sahl  *
13*9512fe85Sahl  * When distributing Covered Code, include this CDDL HEADER in each
14*9512fe85Sahl  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9512fe85Sahl  * If applicable, add the following below this CDDL HEADER, with the
16*9512fe85Sahl  * fields enclosed by brackets "[]" replaced with your own identifying
17*9512fe85Sahl  * information: Portions Copyright [yyyy] [name of copyright owner]
18*9512fe85Sahl  *
19*9512fe85Sahl  * CDDL HEADER END
20*9512fe85Sahl  */
21*9512fe85Sahl 
22*9512fe85Sahl /*
23*9512fe85Sahl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*9512fe85Sahl  * Use is subject to license terms.
25*9512fe85Sahl  */
26*9512fe85Sahl 
27*9512fe85Sahl #include <assert.h>
28*9512fe85Sahl #include <setjmp.h>
29*9512fe85Sahl #include <signal.h>
30*9512fe85Sahl #include <unistd.h>
31*9512fe85Sahl #include <stdlib.h>
32*9512fe85Sahl #include <stdio.h>
33*9512fe85Sahl #include <fcntl.h>
34*9512fe85Sahl 
35*9512fe85Sahl static sigjmp_buf env;
36*9512fe85Sahl 
37*9512fe85Sahl static void
interrupt(int sig)38*9512fe85Sahl interrupt(int sig)
39*9512fe85Sahl {
40*9512fe85Sahl 	siglongjmp(env, sig);
41*9512fe85Sahl }
42*9512fe85Sahl 
43*9512fe85Sahl int
main(int argc,char * argv[])44*9512fe85Sahl main(int argc, char *argv[])
45*9512fe85Sahl {
46*9512fe85Sahl 	const char *file = "/dev/null";
47*9512fe85Sahl 	int i, n, fds[10];
48*9512fe85Sahl 	struct sigaction act;
49*9512fe85Sahl 
50*9512fe85Sahl 	if (argc > 1) {
51*9512fe85Sahl 		(void) fprintf(stderr, "Usage: %s\n", argv[0]);
52*9512fe85Sahl 		return (EXIT_FAILURE);
53*9512fe85Sahl 	}
54*9512fe85Sahl 
55*9512fe85Sahl 	act.sa_handler = interrupt;
56*9512fe85Sahl 	act.sa_flags = 0;
57*9512fe85Sahl 
58*9512fe85Sahl 	(void) sigemptyset(&act.sa_mask);
59*9512fe85Sahl 	(void) sigaction(SIGUSR1, &act, NULL);
60*9512fe85Sahl 
61*9512fe85Sahl 	closefrom(0);
62*9512fe85Sahl 	n = 0;
63*9512fe85Sahl 
64*9512fe85Sahl 	/*
65*9512fe85Sahl 	 * With all of our file descriptors closed, wait here spinning in bogus
66*9512fe85Sahl 	 * ioctl() calls until DTrace hits us with a SIGUSR1 to start the test.
67*9512fe85Sahl 	 */
68*9512fe85Sahl 	if (sigsetjmp(env, 1) == 0) {
69*9512fe85Sahl 		for (;;)
70*9512fe85Sahl 			(void) ioctl(-1, -1, NULL);
71*9512fe85Sahl 	}
72*9512fe85Sahl 
73*9512fe85Sahl 	/*
74*9512fe85Sahl 	 * To test the fds[] array, we open /dev/null (a file with reliable
75*9512fe85Sahl 	 * pathname and properties) using various flags and seek offsets.
76*9512fe85Sahl 	 */
77*9512fe85Sahl 	fds[n++] = open(file, O_RDONLY);
78*9512fe85Sahl 	fds[n++] = open(file, O_WRONLY);
79*9512fe85Sahl 	fds[n++] = open(file, O_RDWR);
80*9512fe85Sahl 
81*9512fe85Sahl 	fds[n++] = open(file, O_RDWR | O_APPEND | O_CREAT | O_DSYNC |
82*9512fe85Sahl 	    O_LARGEFILE | O_NOCTTY | O_NONBLOCK | O_NDELAY | O_RSYNC |
83*9512fe85Sahl 	    O_SYNC | O_TRUNC | O_XATTR);
84*9512fe85Sahl 
85*9512fe85Sahl 	fds[n++] = open(file, O_RDWR);
86*9512fe85Sahl 	(void) lseek(fds[n - 1], 123, SEEK_SET);
87*9512fe85Sahl 
88*9512fe85Sahl 	/*
89*9512fe85Sahl 	 * Once we have all the file descriptors in the state we want to test,
90*9512fe85Sahl 	 * issue a bogus ioctl() on each fd with cmd -1 and arg NULL to whack
91*9512fe85Sahl 	 * our DTrace script into recording the content of the fds[] array.
92*9512fe85Sahl 	 */
93*9512fe85Sahl 	for (i = 0; i < n; i++)
94*9512fe85Sahl 		(void) ioctl(fds[i], -1, NULL);
95*9512fe85Sahl 
96*9512fe85Sahl 	assert(n <= sizeof (fds) / sizeof (fds[0]));
97*9512fe85Sahl 	exit(0);
98*9512fe85Sahl }
99