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