1 /* $NetBSD: t_ttyio.c,v 1.2 2011/04/19 20:07:53 martin Exp $ */
4 * Copyright (c) 2001, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
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.2 2011/04/19 20:07:53 martin Exp $");
37 #include <sys/types.h>
49 #if defined(__NetBSD__)
51 #elif defined(__bsdi__)
52 int openpty(int *, int *, char *, struct termios *, struct winsize *);
53 #elif defined(__FreeBSD__)
61 #define REQUIRE_ERRNO(x, v) ATF_REQUIRE_MSG(x != v, "%s: %s", #x, strerror(errno))
64 ATF_TC_HEAD(ioctl, tc)
66 atf_tc_set_md_var(tc, "descr",
67 "Checks that ioctl calls are restarted "
68 "properly after being interrupted");
75 REQUIRE_ERRNO(wait(NULL), -1);
78 ATF_TC_BODY(ioctl, tc)
81 char name[128], buf[128];
89 * Create default termios settings for later use
91 memset(&term, 0, sizeof(term));
92 term.c_iflag = TTYDEF_IFLAG;
93 term.c_oflag = TTYDEF_OFLAG;
94 term.c_cflag = TTYDEF_CFLAG;
95 term.c_lflag = TTYDEF_LFLAG;
96 cfsetspeed(&term, TTYDEF_SPEED);
99 REQUIRE_ERRNO(openpty(&m, &s, name, &term, NULL), -1);
103 atf_tc_fail("fork(): %s", strerror(errno));
106 /* wait for parent to get set up */
108 (void)printf("child1: exiting\n");
112 (void)printf("parent: spawned child1\n");
118 atf_tc_fail("fork(): %s", strerror(errno));
121 /* wait for parent to get upset */
123 /* drain the tty q */
124 if (read(m, buf, sizeof(buf)) == -1)
126 (void)printf("child2: exiting\n");
130 (void)printf("parent: spawned child2\n");
134 /* set up a restarting signal handler */
135 (void)sigemptyset(&sa.sa_mask);
136 sa.sa_handler = sigchld;
137 sa.sa_flags = SA_RESTART;
138 REQUIRE_ERRNO(sigaction(SIGCHLD, &sa, NULL), -1);
140 /* put something in the output q */
141 REQUIRE_ERRNO(write(s, "Hello world\n", 12), -1);
143 /* ask for output to drain but don't drain it */
145 if (tcsetattr(s, TCSADRAIN, &term) == -1) {
146 (void)printf("parent: tcsetattr: %s\n", strerror(errno));
150 /* wait for last child */
151 sa.sa_handler = SIG_DFL;
152 REQUIRE_ERRNO(sigaction(SIGCHLD, &sa, NULL), -1);
155 ATF_REQUIRE_EQ(rc, 0);
160 ATF_TP_ADD_TC(tp, ioctl);
162 return atf_no_error();