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 2006 Sun Microsystems, Inc.  All rights reserved.
24178476Sjb * Use is subject to license terms.
25178476Sjb */
26178476Sjb
27178476Sjb#pragma ident	"%Z%%M%	%I%	%E% SMI"
28178476Sjb
29178476Sjb#include <assert.h>
30178476Sjb#include <setjmp.h>
31178476Sjb#include <signal.h>
32178476Sjb#include <unistd.h>
33178476Sjb#include <stdlib.h>
34178476Sjb#include <stdio.h>
35178476Sjb#include <fcntl.h>
36178476Sjb
37178476Sjbstatic sigjmp_buf env;
38178476Sjb
39178476Sjbstatic void
40178476Sjbinterrupt(int sig)
41178476Sjb{
42178476Sjb	siglongjmp(env, sig);
43178476Sjb}
44178476Sjb
45178476Sjbint
46178476Sjbmain(int argc, char *argv[])
47178476Sjb{
48178476Sjb	const char *file = "/dev/null";
49178476Sjb	int i, n, fds[10];
50178476Sjb	struct sigaction act;
51178476Sjb
52178476Sjb	if (argc > 1) {
53178476Sjb		(void) fprintf(stderr, "Usage: %s\n", argv[0]);
54178476Sjb		return (EXIT_FAILURE);
55178476Sjb	}
56178476Sjb
57178476Sjb	act.sa_handler = interrupt;
58178476Sjb	act.sa_flags = 0;
59178476Sjb
60178476Sjb	(void) sigemptyset(&act.sa_mask);
61178476Sjb	(void) sigaction(SIGUSR1, &act, NULL);
62178476Sjb
63178476Sjb	closefrom(0);
64178476Sjb	n = 0;
65178476Sjb
66178476Sjb	/*
67178476Sjb	 * With all of our file descriptors closed, wait here spinning in bogus
68178476Sjb	 * ioctl() calls until DTrace hits us with a SIGUSR1 to start the test.
69178476Sjb	 */
70178476Sjb	if (sigsetjmp(env, 1) == 0) {
71178476Sjb		for (;;)
72178476Sjb			(void) ioctl(-1, -1, NULL);
73178476Sjb	}
74178476Sjb
75178476Sjb	/*
76178476Sjb	 * To test the fds[] array, we open /dev/null (a file with reliable
77178476Sjb	 * pathname and properties) using various flags and seek offsets.
78178476Sjb	 */
79178476Sjb	fds[n++] = open(file, O_RDONLY);
80178476Sjb	fds[n++] = open(file, O_WRONLY);
81178476Sjb	fds[n++] = open(file, O_RDWR);
82178476Sjb
83178476Sjb	fds[n++] = open(file, O_RDWR | O_APPEND | O_CREAT | O_DSYNC |
84178476Sjb	    O_LARGEFILE | O_NOCTTY | O_NONBLOCK | O_NDELAY | O_RSYNC |
85229468Spjd	    O_SYNC | O_TRUNC | O_XATTR, 0666);
86178476Sjb
87178476Sjb	fds[n++] = open(file, O_RDWR);
88178476Sjb	(void) lseek(fds[n - 1], 123, SEEK_SET);
89178476Sjb
90178476Sjb	/*
91178476Sjb	 * Once we have all the file descriptors in the state we want to test,
92178476Sjb	 * issue a bogus ioctl() on each fd with cmd -1 and arg NULL to whack
93178476Sjb	 * our DTrace script into recording the content of the fds[] array.
94178476Sjb	 */
95178476Sjb	for (i = 0; i < n; i++)
96178476Sjb		(void) ioctl(fds[i], -1, NULL);
97178476Sjb
98178476Sjb	assert(n <= sizeof (fds) / sizeof (fds[0]));
99178476Sjb	exit(0);
100178476Sjb}
101