xref: /netbsd-src/tests/lib/libc/ttyio/t_ttyio.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: t_ttyio.c,v 1.1 2011/01/07 02:47:41 pgoyette Exp $ */
2 
3 /*
4  * Copyright (c) 2001, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Brown.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2008\
34  The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_ttyio.c,v 1.1 2011/01/07 02:47:41 pgoyette Exp $");
36 
37 #include <sys/types.h>
38 #include <sys/wait.h>
39 
40 #include <err.h>
41 #include <errno.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <termios.h>
47 #include <unistd.h>
48 
49 #if defined(__NetBSD__)
50 #include <util.h>
51 #elif defined(__bsdi__)
52 int openpty(int *, int *, char *, struct termios *, struct winsize *);
53 #elif defined(__FreeBSD__)
54 #include <libutil.h>
55 #else
56 #error where openpty?
57 #endif
58 
59 #include <atf-c.h>
60 
61 #define REQUIRE_ERRNO(x, v) ATF_REQUIRE_MSG(x != v, "%s: %s", #x, strerror(errno))
62 
63 ATF_TC(ioctl);
64 ATF_TC_HEAD(ioctl, tc)
65 {
66 	atf_tc_set_md_var(tc, "descr",
67 		"Checks that ioctl calls are restarted "
68 		"properly after being interrupted");
69 }
70 
71 /* ARGSUSED */
72 static void
73 sigchld(int nsig)
74 {
75 	REQUIRE_ERRNO(wait(NULL), -1);
76 }
77 
78 ATF_TC_BODY(ioctl, tc)
79 {
80 	int m, s, rc;
81 	char name[128], buf[128];
82 	struct termios term;
83 	struct sigaction sa;
84 
85 	/* unbuffer stdout */
86 	setbuf(stdout, NULL);
87 
88 	/* get terminal settings for later use */
89 	REQUIRE_ERRNO(tcgetattr(STDIN_FILENO, &term), -1);
90 
91 	/* get a tty */
92 	REQUIRE_ERRNO(openpty(&m, &s, name, &term, NULL), -1);
93 
94 	switch (fork()) {
95 	case -1:
96 		atf_tc_fail("fork(): %s", strerror(errno));
97 		/* NOTREACHED */
98 	case 0:
99 		/* wait for parent to get set up */
100 		(void)sleep(1);
101 		(void)printf("child1: exiting\n");
102 		exit(0);
103 		/* NOTREACHED */
104 	default:
105 		(void)printf("parent: spawned child1\n");
106 		break;
107 	}
108 
109 	switch (fork()) {
110 	case -1:
111 		atf_tc_fail("fork(): %s", strerror(errno));
112 		/* NOTREACHED */
113 	case 0:
114 		/* wait for parent to get upset */
115 		(void)sleep(2);
116 		/* drain the tty q */
117 		if (read(m, buf, sizeof(buf)) == -1)
118 			err(1, "read");
119 		(void)printf("child2: exiting\n");
120 		exit(0);
121 		/* NOTREACHED */
122 	default:
123 		(void)printf("parent: spawned child2\n");
124 		break;
125 	}
126 
127 	/* set up a restarting signal handler */
128 	(void)sigemptyset(&sa.sa_mask);
129 	sa.sa_handler = sigchld;
130 	sa.sa_flags = SA_RESTART;
131 	REQUIRE_ERRNO(sigaction(SIGCHLD, &sa, NULL), -1);
132 
133 	/* put something in the output q */
134 	REQUIRE_ERRNO(write(s, "Hello world\n", 12), -1);
135 
136 	/* ask for output to drain but don't drain it */
137 	rc = 0;
138 	if (tcsetattr(s, TCSADRAIN, &term) == -1) {
139 		(void)printf("parent: tcsetattr: %s\n", strerror(errno));
140 		rc = 1;
141 	}
142 
143 	/* wait for last child */
144 	sa.sa_handler = SIG_DFL;
145 	REQUIRE_ERRNO(sigaction(SIGCHLD, &sa, NULL), -1);
146 	(void) wait(NULL);
147 
148 	ATF_REQUIRE_EQ(rc, 0);
149 }
150 
151 ATF_TP_ADD_TCS(tp)
152 {
153 	ATF_TP_ADD_TC(tp, ioctl);
154 
155 	return atf_no_error();
156 }
157