1239310Sdim/* $NetBSD: t_pipe.c,v 1.2 2017/01/13 21:30:41 christos Exp $ */ 2239310Sdim 3239310Sdim/*- 4239310Sdim * Copyright (c) 2002 The NetBSD Foundation, Inc. 5239310Sdim * All rights reserved. 6239310Sdim * 7239310Sdim * This code is derived from software contributed to The NetBSD Foundation 8239310Sdim * by Luke Mewburn and Jaromir Dolecek. 9239310Sdim * 10239310Sdim * Redistribution and use in source and binary forms, with or without 11239310Sdim * modification, are permitted provided that the following conditions 12239310Sdim * are met: 13239310Sdim * 1. Redistributions of source code must retain the above copyright 14239310Sdim * notice, this list of conditions and the following disclaimer. 15239310Sdim * 2. Redistributions in binary form must reproduce the above copyright 16239310Sdim * notice, this list of conditions and the following disclaimer in the 17239310Sdim * documentation and/or other materials provided with the distribution. 18249423Sdim * 19251662Sdim * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20249423Sdim * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21249423Sdim * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22239310Sdim * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23239310Sdim * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24239310Sdim * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25239310Sdim * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26239310Sdim * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27239310Sdim * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28239310Sdim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29239310Sdim * POSSIBILITY OF SUCH DAMAGE. 30263508Sdim */ 31239310Sdim 32239310Sdim#include <sys/cdefs.h> 33239310Sdim__COPYRIGHT("@(#) Copyright (c) 2008\ 34239310Sdim The NetBSD Foundation, inc. All rights reserved."); 35239310Sdim__RCSID("$NetBSD: t_pipe.c,v 1.2 2017/01/13 21:30:41 christos Exp $"); 36239310Sdim 37239310Sdim#include <sys/event.h> 38239310Sdim#include <sys/wait.h> 39239310Sdim 40239310Sdim#include <fcntl.h> 41239310Sdim#include <stdio.h> 42239310Sdim#include <stdlib.h> 43239310Sdim#include <unistd.h> 44239310Sdim 45239310Sdim#include <atf-c.h> 46249423Sdim 47249423Sdim#include "h_macros.h" 48249423Sdim 49249423SdimATF_TC(pipe1); 50249423SdimATF_TC_HEAD(pipe1, tc) 51249423Sdim{ 52249423Sdim atf_tc_set_md_var(tc, "descr", 53249423Sdim "Checks EVFILT_WRITE for pipes. This test used to trigger " 54249423Sdim "problem fixed in rev. 1.5.2.7 of sys/kern/sys_pipe.c"); 55249423Sdim} 56249423SdimATF_TC_BODY(pipe1, tc) 57249423Sdim{ 58239310Sdim struct kevent event[1]; 59239310Sdim int fds[2]; 60239310Sdim int kq, n; 61239310Sdim 62249423Sdim RL(pipe(fds)); 63249423Sdim RL(kq = kqueue()); 64251662Sdim RL(close(fds[0])); 65251662Sdim 66251662Sdim EV_SET(&event[0], fds[1], EVFILT_WRITE, EV_ADD|EV_ENABLE, 0, 0, 0); 67263508Sdim ATF_REQUIRE_EQ_MSG((n = kevent(kq, event, 1, NULL, 0, NULL)), 68239310Sdim -1, "got: %d", n); 69239310Sdim ATF_REQUIRE_EQ_MSG(errno, EBADF, "got: %s", strerror(errno)); 70239310Sdim} 71239310Sdim 72239310SdimATF_TC(pipe2); 73239310SdimATF_TC_HEAD(pipe2, tc) 74249423Sdim{ 75239310Sdim atf_tc_set_md_var(tc, "descr", 76239310Sdim "Checks EVFILT_WRITE for pipes. This test used to trigger problem " 77263508Sdim "fixed in rev. 1.5.2.9 of sys/kern/sys_pipe.c"); 78239310Sdim} 79239310SdimATF_TC_BODY(pipe2, tc) 80239310Sdim{ 81239310Sdim struct kevent event[1]; 82239310Sdim char buffer[128]; 83239310Sdim int fds[2]; 84239310Sdim int kq, n; 85239310Sdim int status; 86239310Sdim pid_t child; 87239310Sdim 88239310Sdim RL(pipe(fds)); 89239310Sdim RL(kq = kqueue()); 90239310Sdim 91239310Sdim EV_SET(&event[0], fds[1], EVFILT_WRITE, EV_ADD|EV_ENABLE, 0, 0, 0); 92239310Sdim RL(kevent(kq, event, 1, NULL, 0, NULL)); 93239310Sdim 94239310Sdim /* spawn child reader */ 95239310Sdim RL(child = fork()); 96239310Sdim if (child == 0) { 97239310Sdim int sz = read(fds[0], buffer, 128); 98239310Sdim if (sz > 0) 99239310Sdim (void)printf("pipe: child read '%.*s'\n", sz, buffer); 100239310Sdim exit(sz <= 0); 101239310Sdim } 102239310Sdim 103239310Sdim RL(n = kevent(kq, NULL, 0, event, 1, NULL)); 104239310Sdim 105239310Sdim (void)printf("kevent num %d flags: %#x, fflags: %#x, data: " 106239310Sdim "%" PRId64 "\n", n, event[0].flags, event[0].fflags, event[0].data); 107239310Sdim 108239310Sdim RL(n = write(fds[1], "foo", 3)); 109239310Sdim RL(close(fds[1])); 110239310Sdim 111239310Sdim (void)waitpid(child, &status, 0); 112249423Sdim} 113239310Sdim 114239310SdimATF_TC(pipe3); 115239310SdimATF_TC_HEAD(pipe3, tc) 116239310Sdim{ 117239310Sdim atf_tc_set_md_var(tc, "descr", 118239310Sdim "Checks EVFILT_WRITE for pipes. This test used to trigger problem " 119239310Sdim "fixed in rev. 1.5.2.10 of sys/kern/sys_pipe.c"); 120239310Sdim} 121239310SdimATF_TC_BODY(pipe3, tc) 122239310Sdim{ 123239310Sdim struct kevent event[1]; 124239310Sdim int fds[2]; 125239310Sdim int kq; 126239310Sdim 127239310Sdim RL(pipe(fds)); 128239310Sdim RL(kq = kqueue()); 129239310Sdim 130239310Sdim EV_SET(&event[0], fds[1], EVFILT_WRITE, EV_ADD|EV_ENABLE, 0, 0, 0); 131263508Sdim RL(kevent(kq, event, 1, NULL, 0, NULL)); 132263508Sdim 133263508Sdim /* close 'read' end first, then 'write' */ 134263508Sdim 135263508Sdim RL(close(fds[0])); 136263508Sdim RL(close(fds[1])); 137263508Sdim} 138263508Sdim 139263508Sdim 140263508SdimATF_TP_ADD_TCS(tp) 141263508Sdim{ 142263508Sdim ATF_TP_ADD_TC(tp, pipe1); 143263508Sdim ATF_TP_ADD_TC(tp, pipe2); 144263508Sdim ATF_TP_ADD_TC(tp, pipe3); 145263508Sdim 146263508Sdim return atf_no_error(); 147263508Sdim} 148263508Sdim