Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/tests/kernel Add a pty test, written by Matthew Mondor
details: https://anonhg.NetBSD.org/src/rev/3e2ac9e9cd5b
branches: trunk
changeset: 769818:3e2ac9e9cd5b
user: christos <christos%NetBSD.org@localhost>
date: Sat Sep 24 15:53:01 2011 +0000
description:
Add a pty test, written by Matthew Mondor
diffstat:
tests/kernel/Makefile | 3 +-
tests/kernel/t_pty.c | 351 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 353 insertions(+), 1 deletions(-)
diffs (truncated from 372 to 300 lines):
diff -r ccd623ab6f51 -r 3e2ac9e9cd5b tests/kernel/Makefile
--- a/tests/kernel/Makefile Sat Sep 24 14:44:11 2011 +0000
+++ b/tests/kernel/Makefile Sat Sep 24 15:53:01 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.12 2011/05/28 16:12:56 tron Exp $
+# $NetBSD: Makefile,v 1.13 2011/09/24 15:53:01 christos Exp $
NOMAN= # defined
@@ -14,6 +14,7 @@
TESTS_C+= t_pipe
TESTS_C+= t_poll3w
TESTS_C+= t_posix_fadvise
+TESTS_C+= t_pty
TESTS_C+= t_rnd
TESTS_C+= t_time
TESTS_C+= t_ucontext
diff -r ccd623ab6f51 -r 3e2ac9e9cd5b tests/kernel/t_pty.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/kernel/t_pty.c Sat Sep 24 15:53:01 2011 +0000
@@ -0,0 +1,351 @@
+/* $Id: t_pty.c,v 1.1 2011/09/24 15:53:01 christos 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.1 2011/09/24 15:53:01 christos Exp $");
+
+#include <errno.h>
+#include <err.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <stdio.h>
+#ifdef __linux__
+#define _XOPEN_SOURCE
+#define __USE_XOPEN
+#endif
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <termios.h>
+#include <unistd.h>
+
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#ifdef STANDALONE
+static __dead void usage(const char *);
+static void parse_args(int, char **);
+#else
+#include <atf-c.h>
+#include "../h_macros.h"
+#endif
+
+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)
+ err(EXIT_FAILURE, "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) {
+ err(EXIT_FAILURE, "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)
+ err(EXIT_FAILURE, "waitpid");
+ if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
+ errx(EXIT_FAILURE, "child failed");
+
+ if (verbose)
+ (void)printf("parent: closing PTY\n");
+ (void)close(pty);
+ if (verbose)
+ (void)printf("parent: exiting\n");
+}
+
+static void
+condition(int fd)
+{
+ struct termios tios;
+
+ if (qsize) {
+ int opt = qsize;
+ if (ioctl(fd, TIOCSQSIZE, &opt) == -1)
+ err(EXIT_FAILURE, "Couldn't set tty(4) buffer size");
+ if (ioctl(fd, TIOCGQSIZE, &opt) == -1)
+ err(EXIT_FAILURE, "Couldn't get tty(4) buffer size");
+ if (opt != qsize)
+ errx(EXIT_FAILURE, "Wrong qsize %d != %d\n",
+ qsize, opt);
+ }
+ if (tcgetattr(fd, &tios) == -1)
+ err(EXIT_FAILURE, "tcgetattr()");
+ cfmakeraw(&tios);
+ cfsetspeed(&tios, B921600);
+ if (tcsetattr(fd, TCSANOW, &tios) == -1)
+ err(EXIT_FAILURE, "tcsetattr()");
+}
+
+static int
+pty_open(void)
+{
+ int fd;
+
+ if ((fd = posix_openpt(O_RDWR)) == -1)
+ err(EXIT_FAILURE, "Couldn't pty(4) device");
+ condition(fd);
+ if (grantpt(fd) == -1)
+ err(EXIT_FAILURE,
+ "Couldn't grant permissions on tty(4) device");
+
+
+ condition(fd);
+
+ if (unlockpt(fd) == -1)
+ err(EXIT_FAILURE, "unlockpt()");
+
+ return fd;
+}
+
+static int
+tty_open(const char *ttydev)
+{
+ int fd;
+
+ if ((fd = open(ttydev, O_RDWR, 0)) == -1)
+ err(EXIT_FAILURE, "Couldn't open tty(4) device");
+
+#ifdef USE_PPP_DISCIPLINE
+ {
+ int opt = PPPDISC;
+ if (ioctl(fd, TIOCSETD, &opt) == -1)
+ err(EXIT_FAILURE,
+ "Couldn't set tty(4) discipline to PPP");
+ }
+#endif
+
+ condition(fd);
+
+ return fd;
+}
+
+static void
+fd_nonblock(int fd)
+{
+ int opt;
+
+ if ((opt = fcntl(fd, F_GETFL, NULL)) == -1)
+ err(EXIT_FAILURE, "fcntl()");
+ if (fcntl(fd, F_SETFL, opt | O_NONBLOCK) == -1)
+ err(EXIT_FAILURE, "fcntl()");
+}
+
+static pid_t
+child_spawn(const char *ttydev)
+{
+ pid_t pid;
+ int tty;
+ struct pollfd pfd;
+ size_t total = 0;
+
+ if ((pid = fork()) == -1)
+ err(EXIT_FAILURE, "fork()");
+ (void)setsid();
+ if (pid != 0)
+ return pid;
+
+ if (verbose)
+ (void)printf("child: started; open \"%s\"\n", ttydev);
+ tty = tty_open(ttydev);
+ fd_nonblock(tty);
+
+ if (verbose)
+ (void)printf("child: TTY open, starting read loop\n");
+ pfd.fd = tty;
+ pfd.events = POLLIN;
+ pfd.revents = 0;
+ for (;;) {
+ int ret;
+ ssize_t size;
+
+ if (verbose)
+ (void)printf("child: polling\n");
+ if ((ret = poll(&pfd, 1, 2000)) == -1)
+ err(EXIT_FAILURE, "child: poll()");
+ if (ret == 0)
+ break;
+ if ((pfd.revents & POLLERR) != 0)
+ break;
+ if ((pfd.revents & POLLIN) != 0) {
+ for (;;) {
+ if (verbose)
+ (void)printf(
+ "child: attempting to read %zu"
+ " bytes\n", buffer_size);
+ if ((size = read(tty, dbuf, buffer_size))
+ == -1) {
+ if (errno == EAGAIN)
+ break;
+ err(EXIT_FAILURE, "child: read()");
+ }
+ if (qsize && size < qsize &&
+ (size_t)size < buffer_size)
+ errx(EXIT_FAILURE, "read returned %zd "
+ "less than the queue size %d",
+ size, qsize);
+ if (verbose)
+ (void)printf(
+ "child: read %zd bytes from TTY\n",
+ size);
+ if (size == 0)
+ goto end;
+ total += size;
+ }
+ }
+ }
+end:
+ if (verbose)
+ (void)printf("child: closing TTY %zu\n", total);
+ (void)close(tty);
+ if (verbose)
+ (void)printf("child: exiting\n");
+ if (total != buffer_size * packets)
+ errx(EXIT_FAILURE,
+ "Lost data %zu != %zu\n", total, buffer_size * packets);
+
+ exit(EXIT_SUCCESS);
+}
+
+#ifdef STANDALONE
+static void
+usage(const char *msg)
+{
+
+ if (msg != NULL)
+ (void) fprintf(stderr, "\n%s\n\n", msg);
+
+ (void)fprintf(stderr,
+ "Usage: %s [-v] [-q <qsize>] [-s <packetsize>] [-n <packets>]\n",
+ getprogname());
+
+ exit(EXIT_FAILURE);
+}
+
+static void
+parse_args(int argc, char **argv)
+{
+ int ch;
Home |
Main Index |
Thread Index |
Old Index