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