1 1.7 martin /* $NetBSD: t_pr.c,v 1.7 2011/04/26 20:42:01 martin Exp $ */ 2 1.7 martin 3 1.7 martin /*- 4 1.7 martin * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc. 5 1.7 martin * All rights reserved. 6 1.7 martin * 7 1.7 martin * This code is derived from software contributed to The NetBSD Foundation 8 1.7 martin * by Antti Kantee <pooka (at) NetBSD.org> and Martin Husemann <martin (at) NetBSD.org>. 9 1.7 martin * 10 1.7 martin * Redistribution and use in source and binary forms, with or without 11 1.7 martin * modification, are permitted provided that the following conditions 12 1.7 martin * are met: 13 1.7 martin * 1. Redistributions of source code must retain the above copyright 14 1.7 martin * notice, this list of conditions and the following disclaimer. 15 1.7 martin * 2. Redistributions in binary form must reproduce the above copyright 16 1.7 martin * notice, this list of conditions and the following disclaimer in the 17 1.7 martin * documentation and/or other materials provided with the distribution. 18 1.7 martin * 19 1.7 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.7 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.7 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.7 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.7 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.7 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.7 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.7 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.7 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.7 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.7 martin * POSSIBILITY OF SUCH DAMAGE. 30 1.7 martin */ 31 1.1 pooka 32 1.1 pooka #include <sys/types.h> 33 1.1 pooka #include <sys/ioctl.h> 34 1.1 pooka #include <sys/tty.h> 35 1.1 pooka 36 1.1 pooka #include <atf-c.h> 37 1.1 pooka #include <fcntl.h> 38 1.1 pooka 39 1.5 martin #include <stdio.h> 40 1.5 martin #include <string.h> 41 1.5 martin #include <errno.h> 42 1.1 pooka #include <rump/rump.h> 43 1.1 pooka #include <rump/rump_syscalls.h> 44 1.1 pooka 45 1.5 martin static int 46 1.5 martin sendsome(int from, int to) 47 1.5 martin { 48 1.5 martin size_t i; 49 1.5 martin ssize_t cnt; 50 1.5 martin static const char msg[] = "hello world\n"; 51 1.5 martin char buf[sizeof(msg)+10]; 52 1.5 martin 53 1.5 martin memset(buf, 0, sizeof(buf)); 54 1.5 martin rump_sys_write(from, msg, strlen(msg)); 55 1.5 martin cnt = rump_sys_read(to, buf, sizeof(buf)); 56 1.5 martin if (cnt < (ssize_t)strlen(msg)) { 57 1.5 martin printf("short message read: %zd chars: \"%s\"\n", cnt, buf); 58 1.5 martin return 1; 59 1.5 martin } 60 1.5 martin for (i = 0; i < sizeof(buf); i++) { 61 1.5 martin if (buf[i] == '\r' || buf[i] == '\n') { 62 1.5 martin buf[i] = '\n'; 63 1.5 martin buf[i+1] = '\0'; 64 1.5 martin break; 65 1.5 martin } 66 1.5 martin } 67 1.5 martin 68 1.5 martin return strcmp(buf, msg) != 0; 69 1.5 martin } 70 1.5 martin 71 1.5 martin static int 72 1.5 martin exercise_ptytty(int master, int slave) 73 1.5 martin { 74 1.5 martin int error, flags; 75 1.5 martin 76 1.5 martin /* 77 1.5 martin * send a few bytes from master to slave and read them back 78 1.5 martin */ 79 1.5 martin error = sendsome(master, slave); 80 1.5 martin if (error) 81 1.5 martin return error; 82 1.5 martin 83 1.5 martin flags = FREAD|FWRITE; 84 1.5 martin rump_sys_ioctl(master, TIOCFLUSH, &flags); 85 1.5 martin 86 1.5 martin /* 87 1.5 martin * and the same in the other direction 88 1.5 martin */ 89 1.5 martin error = sendsome(slave, master); 90 1.5 martin if (error) 91 1.5 martin return error; 92 1.5 martin 93 1.5 martin flags = FREAD|FWRITE; 94 1.5 martin rump_sys_ioctl(master, TIOCFLUSH, &flags); 95 1.5 martin return 0; 96 1.5 martin } 97 1.5 martin 98 1.5 martin ATF_TC(client_first); 99 1.5 martin ATF_TC_HEAD(client_first, tc) 100 1.5 martin { 101 1.5 martin 102 1.5 martin atf_tc_set_md_var(tc, "descr", 103 1.5 martin "test basic tty/pty operation when opening client side first"); 104 1.5 martin } 105 1.5 martin 106 1.5 martin ATF_TC_BODY(client_first, tc) 107 1.5 martin { 108 1.5 martin int master, slave, error, v; 109 1.5 martin 110 1.6 martin rump_init(); 111 1.5 martin slave = rump_sys_open("/dev/ttyp1", O_RDWR|O_NONBLOCK); 112 1.5 martin ATF_CHECK(slave != -1); 113 1.5 martin 114 1.5 martin master = rump_sys_open("/dev/ptyp1", O_RDWR); 115 1.5 martin ATF_CHECK(master != -1); 116 1.5 martin 117 1.5 martin v = 0; 118 1.5 martin rump_sys_ioctl(slave, FIOASYNC, &v); 119 1.5 martin error = exercise_ptytty(master, slave); 120 1.5 martin ATF_CHECK(error == 0); 121 1.5 martin 122 1.5 martin rump_sys_close(master); 123 1.5 martin rump_sys_close(slave); 124 1.5 martin } 125 1.5 martin 126 1.5 martin ATF_TC(master_first); 127 1.5 martin ATF_TC_HEAD(master_first, tc) 128 1.5 martin { 129 1.5 martin 130 1.5 martin atf_tc_set_md_var(tc, "descr", 131 1.5 martin "test basic tty/pty operation when opening master side first"); 132 1.5 martin } 133 1.5 martin 134 1.5 martin ATF_TC_BODY(master_first, tc) 135 1.5 martin { 136 1.5 martin int master, slave, error; 137 1.5 martin 138 1.6 martin rump_init(); 139 1.5 martin master = rump_sys_open("/dev/ptyp1", O_RDWR); 140 1.5 martin ATF_CHECK(master != -1); 141 1.5 martin 142 1.5 martin slave = rump_sys_open("/dev/ttyp1", O_RDWR); 143 1.5 martin ATF_CHECK(slave != -1); 144 1.5 martin 145 1.5 martin error = exercise_ptytty(master, slave); 146 1.5 martin ATF_CHECK(error == 0); 147 1.5 martin 148 1.5 martin rump_sys_close(master); 149 1.5 martin rump_sys_close(slave); 150 1.5 martin } 151 1.5 martin 152 1.1 pooka ATF_TC(ptyioctl); 153 1.1 pooka ATF_TC_HEAD(ptyioctl, tc) 154 1.1 pooka { 155 1.1 pooka 156 1.5 martin atf_tc_set_md_var(tc, "descr", 157 1.5 martin "ioctl on pty with client side not open"); 158 1.1 pooka } 159 1.1 pooka 160 1.1 pooka ATF_TC_BODY(ptyioctl, tc) 161 1.1 pooka { 162 1.1 pooka struct termios tio; 163 1.1 pooka int fd; 164 1.1 pooka 165 1.6 martin rump_init(); 166 1.1 pooka fd = rump_sys_open("/dev/ptyp1", O_RDWR); 167 1.3 christos ATF_CHECK(fd != -1); 168 1.1 pooka 169 1.4 martin /* 170 1.4 martin * This used to die with null deref under ptcwakeup() 171 1.4 martin * atf_tc_expect_signal(-1, "PR kern/40688"); 172 1.4 martin */ 173 1.1 pooka rump_sys_ioctl(fd, TIOCGETA, &tio); 174 1.5 martin 175 1.5 martin rump_sys_close(fd); 176 1.1 pooka } 177 1.1 pooka 178 1.1 pooka ATF_TP_ADD_TCS(tp) 179 1.1 pooka { 180 1.1 pooka 181 1.1 pooka ATF_TP_ADD_TC(tp, ptyioctl); 182 1.5 martin ATF_TP_ADD_TC(tp, client_first); 183 1.5 martin ATF_TP_ADD_TC(tp, master_first); 184 1.1 pooka 185 1.1 pooka return atf_no_error(); 186 1.1 pooka } 187