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