Home | History | Annotate | Line # | Download | only in ttyio
t_ttyio.c revision 1.2.26.1
      1  1.2.26.1  pgoyette /*	$NetBSD: t_ttyio.c,v 1.2.26.1 2017/03/20 06:57:59 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.2.26.1  pgoyette __RCSID("$NetBSD: t_ttyio.c,v 1.2.26.1 2017/03/20 06:57:59 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.2    martin 	/*
     89       1.2    martin 	 * Create default termios settings for later use
     90       1.2    martin 	 */
     91       1.2    martin 	memset(&term, 0, sizeof(term));
     92       1.2    martin 	term.c_iflag = TTYDEF_IFLAG;
     93       1.2    martin 	term.c_oflag = TTYDEF_OFLAG;
     94       1.2    martin 	term.c_cflag = TTYDEF_CFLAG;
     95       1.2    martin 	term.c_lflag = TTYDEF_LFLAG;
     96       1.2    martin 	cfsetspeed(&term, TTYDEF_SPEED);
     97       1.1  pgoyette 
     98       1.1  pgoyette 	/* get a tty */
     99       1.1  pgoyette 	REQUIRE_ERRNO(openpty(&m, &s, name, &term, NULL), -1);
    100       1.1  pgoyette 
    101       1.1  pgoyette 	switch (fork()) {
    102       1.1  pgoyette 	case -1:
    103       1.1  pgoyette 		atf_tc_fail("fork(): %s", strerror(errno));
    104       1.1  pgoyette 		/* NOTREACHED */
    105       1.1  pgoyette 	case 0:
    106       1.1  pgoyette 		/* wait for parent to get set up */
    107       1.1  pgoyette 		(void)sleep(1);
    108       1.1  pgoyette 		(void)printf("child1: exiting\n");
    109       1.1  pgoyette 		exit(0);
    110       1.1  pgoyette 		/* NOTREACHED */
    111       1.1  pgoyette 	default:
    112       1.1  pgoyette 		(void)printf("parent: spawned child1\n");
    113       1.1  pgoyette 		break;
    114       1.1  pgoyette 	}
    115       1.1  pgoyette 
    116       1.1  pgoyette 	switch (fork()) {
    117       1.1  pgoyette 	case -1:
    118       1.1  pgoyette 		atf_tc_fail("fork(): %s", strerror(errno));
    119       1.1  pgoyette 		/* NOTREACHED */
    120       1.1  pgoyette 	case 0:
    121       1.1  pgoyette 		/* wait for parent to get upset */
    122       1.1  pgoyette 		(void)sleep(2);
    123       1.1  pgoyette 		/* drain the tty q */
    124       1.1  pgoyette 		if (read(m, buf, sizeof(buf)) == -1)
    125       1.1  pgoyette 			err(1, "read");
    126       1.1  pgoyette 		(void)printf("child2: exiting\n");
    127       1.1  pgoyette 		exit(0);
    128       1.1  pgoyette 		/* NOTREACHED */
    129       1.1  pgoyette 	default:
    130       1.1  pgoyette 		(void)printf("parent: spawned child2\n");
    131       1.1  pgoyette 		break;
    132       1.1  pgoyette 	}
    133       1.1  pgoyette 
    134       1.1  pgoyette 	/* set up a restarting signal handler */
    135       1.1  pgoyette 	(void)sigemptyset(&sa.sa_mask);
    136       1.1  pgoyette 	sa.sa_handler = sigchld;
    137       1.1  pgoyette 	sa.sa_flags = SA_RESTART;
    138       1.1  pgoyette 	REQUIRE_ERRNO(sigaction(SIGCHLD, &sa, NULL), -1);
    139       1.1  pgoyette 
    140       1.1  pgoyette 	/* put something in the output q */
    141       1.1  pgoyette 	REQUIRE_ERRNO(write(s, "Hello world\n", 12), -1);
    142       1.1  pgoyette 
    143       1.1  pgoyette 	/* ask for output to drain but don't drain it */
    144       1.1  pgoyette 	rc = 0;
    145       1.1  pgoyette 	if (tcsetattr(s, TCSADRAIN, &term) == -1) {
    146       1.1  pgoyette 		(void)printf("parent: tcsetattr: %s\n", strerror(errno));
    147       1.1  pgoyette 		rc = 1;
    148       1.1  pgoyette 	}
    149       1.1  pgoyette 
    150       1.1  pgoyette 	/* wait for last child */
    151       1.1  pgoyette 	sa.sa_handler = SIG_DFL;
    152       1.1  pgoyette 	REQUIRE_ERRNO(sigaction(SIGCHLD, &sa, NULL), -1);
    153  1.2.26.1  pgoyette 	(void)wait(NULL);
    154       1.1  pgoyette 
    155  1.2.26.1  pgoyette 	(void)close(s);
    156       1.1  pgoyette 	ATF_REQUIRE_EQ(rc, 0);
    157       1.1  pgoyette }
    158       1.1  pgoyette 
    159       1.1  pgoyette ATF_TP_ADD_TCS(tp)
    160       1.1  pgoyette {
    161       1.1  pgoyette 	ATF_TP_ADD_TC(tp, ioctl);
    162       1.1  pgoyette 
    163       1.1  pgoyette 	return atf_no_error();
    164       1.1  pgoyette }
    165