1/*	$NetBSD: t_ttyio.c,v 1.3 2017/01/10 01:31:40 christos 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.3 2017/01/10 01:31:40 christos 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__)
52int 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
63ATF_TC(ioctl);
64ATF_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 */
72static void
73sigchld(int nsig)
74{
75	REQUIRE_ERRNO(wait(NULL), -1);
76}
77
78ATF_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	/*
89	 * Create default termios settings for later use
90	 */
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);
97
98	/* get a tty */
99	REQUIRE_ERRNO(openpty(&m, &s, name, &term, NULL), -1);
100
101	switch (fork()) {
102	case -1:
103		atf_tc_fail("fork(): %s", strerror(errno));
104		/* NOTREACHED */
105	case 0:
106		/* wait for parent to get set up */
107		(void)sleep(1);
108		(void)printf("child1: exiting\n");
109		exit(0);
110		/* NOTREACHED */
111	default:
112		(void)printf("parent: spawned child1\n");
113		break;
114	}
115
116	switch (fork()) {
117	case -1:
118		atf_tc_fail("fork(): %s", strerror(errno));
119		/* NOTREACHED */
120	case 0:
121		/* wait for parent to get upset */
122		(void)sleep(2);
123		/* drain the tty q */
124		if (read(m, buf, sizeof(buf)) == -1)
125			err(1, "read");
126		(void)printf("child2: exiting\n");
127		exit(0);
128		/* NOTREACHED */
129	default:
130		(void)printf("parent: spawned child2\n");
131		break;
132	}
133
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);
139
140	/* put something in the output q */
141	REQUIRE_ERRNO(write(s, "Hello world\n", 12), -1);
142
143	/* ask for output to drain but don't drain it */
144	rc = 0;
145	if (tcsetattr(s, TCSADRAIN, &term) == -1) {
146		(void)printf("parent: tcsetattr: %s\n", strerror(errno));
147		rc = 1;
148	}
149
150	/* wait for last child */
151	sa.sa_handler = SIG_DFL;
152	REQUIRE_ERRNO(sigaction(SIGCHLD, &sa, NULL), -1);
153	(void)wait(NULL);
154
155	(void)close(s);
156	ATF_REQUIRE_EQ(rc, 0);
157}
158
159ATF_TP_ADD_TCS(tp)
160{
161	ATF_TP_ADD_TC(tp, ioctl);
162
163	return atf_no_error();
164}
165