etc/services - sync with NetBSD-8
[minix.git] / tests / kernel / tty / t_pr.c
blobc590ea5ec608f5d3a98a889ec21bfd1ce11956e0
1 /* $NetBSD: t_pr.c,v 1.7 2011/04/26 20:42:01 martin Exp $ */
3 /*-
4 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Antti Kantee <pooka@NetBSD.org> and Martin Husemann <martin@NetBSD.org>.
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.
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/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/tty.h>
36 #include <atf-c.h>
37 #include <fcntl.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <rump/rump.h>
43 #include <rump/rump_syscalls.h>
45 static int
46 sendsome(int from, int to)
48 size_t i;
49 ssize_t cnt;
50 static const char msg[] = "hello world\n";
51 char buf[sizeof(msg)+10];
53 memset(buf, 0, sizeof(buf));
54 rump_sys_write(from, msg, strlen(msg));
55 cnt = rump_sys_read(to, buf, sizeof(buf));
56 if (cnt < (ssize_t)strlen(msg)) {
57 printf("short message read: %zd chars: \"%s\"\n", cnt, buf);
58 return 1;
60 for (i = 0; i < sizeof(buf); i++) {
61 if (buf[i] == '\r' || buf[i] == '\n') {
62 buf[i] = '\n';
63 buf[i+1] = '\0';
64 break;
68 return strcmp(buf, msg) != 0;
71 static int
72 exercise_ptytty(int master, int slave)
74 int error, flags;
77 * send a few bytes from master to slave and read them back
79 error = sendsome(master, slave);
80 if (error)
81 return error;
83 flags = FREAD|FWRITE;
84 rump_sys_ioctl(master, TIOCFLUSH, &flags);
87 * and the same in the other direction
89 error = sendsome(slave, master);
90 if (error)
91 return error;
93 flags = FREAD|FWRITE;
94 rump_sys_ioctl(master, TIOCFLUSH, &flags);
95 return 0;
98 ATF_TC(client_first);
99 ATF_TC_HEAD(client_first, tc)
102 atf_tc_set_md_var(tc, "descr",
103 "test basic tty/pty operation when opening client side first");
106 ATF_TC_BODY(client_first, tc)
108 int master, slave, error, v;
110 rump_init();
111 slave = rump_sys_open("/dev/ttyp1", O_RDWR|O_NONBLOCK);
112 ATF_CHECK(slave != -1);
114 master = rump_sys_open("/dev/ptyp1", O_RDWR);
115 ATF_CHECK(master != -1);
117 v = 0;
118 rump_sys_ioctl(slave, FIOASYNC, &v);
119 error = exercise_ptytty(master, slave);
120 ATF_CHECK(error == 0);
122 rump_sys_close(master);
123 rump_sys_close(slave);
126 ATF_TC(master_first);
127 ATF_TC_HEAD(master_first, tc)
130 atf_tc_set_md_var(tc, "descr",
131 "test basic tty/pty operation when opening master side first");
134 ATF_TC_BODY(master_first, tc)
136 int master, slave, error;
138 rump_init();
139 master = rump_sys_open("/dev/ptyp1", O_RDWR);
140 ATF_CHECK(master != -1);
142 slave = rump_sys_open("/dev/ttyp1", O_RDWR);
143 ATF_CHECK(slave != -1);
145 error = exercise_ptytty(master, slave);
146 ATF_CHECK(error == 0);
148 rump_sys_close(master);
149 rump_sys_close(slave);
152 ATF_TC(ptyioctl);
153 ATF_TC_HEAD(ptyioctl, tc)
156 atf_tc_set_md_var(tc, "descr",
157 "ioctl on pty with client side not open");
160 ATF_TC_BODY(ptyioctl, tc)
162 struct termios tio;
163 int fd;
165 rump_init();
166 fd = rump_sys_open("/dev/ptyp1", O_RDWR);
167 ATF_CHECK(fd != -1);
170 * This used to die with null deref under ptcwakeup()
171 * atf_tc_expect_signal(-1, "PR kern/40688");
173 rump_sys_ioctl(fd, TIOCGETA, &tio);
175 rump_sys_close(fd);
178 ATF_TP_ADD_TCS(tp)
181 ATF_TP_ADD_TC(tp, ptyioctl);
182 ATF_TP_ADD_TC(tp, client_first);
183 ATF_TP_ADD_TC(tp, master_first);
185 return atf_no_error();