/* $Id: t_pty.c,v 1.5 2020/06/24 07:02:57 rin Exp $ */
/*
* Allocates a pty(4) device, and sends the specified number of packets of the
* specified length though it, while a child reader process reads and reports
* results.
*
* Written by Matthew Mondor
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: t_pty.c,v 1.5 2020/06/24 07:02:57 rin Exp $");
static int pty_open(void);
static int tty_open(const char *);
static void fd_nonblock(int);
static pid_t child_spawn(const char *);
static void run(void);
static size_t buffer_size = 4096;
static size_t packets = 2;
static uint8_t *dbuf;
static int verbose;
static int qsize;
static
void run(void)
{
size_t i;
int pty;
int status;
pid_t child;
if ((dbuf = calloc(1, buffer_size)) == NULL)
atf_tc_fail_errno("malloc(%zu)", buffer_size);
if (verbose)
(void)printf(
"parent: started; opening PTY and spawning child\n");
pty = pty_open();
child = child_spawn(ptsname(pty));
if (verbose)
(void)printf("parent: sleeping to make sure child is ready\n");
(void)sleep(1);
for (i = 0; i < buffer_size; i++)
dbuf[i] = i & 0xff;
if (verbose)
(void)printf("parent: writing\n");
for (i = 0; i < packets; i++) {
ssize_t size;
if (verbose)
(void)printf(
"parent: attempting to write %zu bytes to PTY\n",
buffer_size);
if ((size = write(pty, dbuf, buffer_size)) == -1) {
atf_tc_fail_errno("parent: write()");
break;
}
if (verbose)
(void)printf("parent: wrote %zd bytes to PTY\n", size);
}
if (verbose)
(void)printf("parent: waiting for child to exit\n");
if (waitpid(child, &status, 0) == -1)
atf_tc_fail_errno("waitpid");
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
atf_tc_fail("child failed");
if (verbose)
(void)printf("parent: closing PTY\n");
(void)close(pty);
if (verbose)
(void)printf("parent: exiting\n");
}
static void
parse_args(int argc, char **argv)
{
int ch;
while ((ch = getopt(argc, argv, "n:q:s:v")) != -1) {
switch (ch) {
case 'n':
packets = (size_t)atoi(optarg);
break;
case 'q':
qsize = atoi(optarg);
break;
case 's':
buffer_size = (size_t)atoi(optarg);
break;
case 'v':
verbose++;
break;
default:
usage(NULL);
break;
}
}
if (buffer_size < 0 || buffer_size > 65536)
usage("-s must be between 0 and 65536");
if (packets < 1 || packets > 100)
usage("-p must be between 1 and 100");
}
ATF_TC_HEAD(pty_no_queue, tc)
{
atf_tc_set_md_var(tc, "descr", "Checks that writing to pty "
"does not lose data with the default queue size of 1024");
}