mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-02 08:20:01 +08:00
679 lines
19 KiB
C
679 lines
19 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// Guest side of test/serial.test.ts: exercises /dev/ttyACM0 (kwserial) on a
|
|
// host loopback port (js/core/serial.ts FakeSerialBackend). Static aarch64
|
|
// musl, built by the test:
|
|
//
|
|
// zig cc -target aarch64-linux-musl -mcpu=generic -static -O2 -s -o ttyacm test/serial/ttyacm.c
|
|
//
|
|
// ttyacm run DEV [CPU] the whole sequence below, on CPU if given
|
|
// ttyacm open-as UID DEV as UID (dropping to it, without supplementary
|
|
// groups, unless it is that already), open DEV
|
|
// ttyacm park DEV OUT open DEV, say "ready", block in read(); then
|
|
// write what read() and write() returned to OUT
|
|
//
|
|
// `run` prints one line per check, "ok NAME ..." or "FAIL NAME: ...", and
|
|
// at each "step NAME ..." line waits for a line on stdin: the host checks
|
|
// its side (the backend's calls and state) and answers. "miwait N" means
|
|
// the program is about to block in TIOCMIWAIT, for the host to change the
|
|
// input signals. The last line is "done N failure(s)", exit 0 without one.
|
|
// The steps: termios round trips (bauds up to 1500000, 250000 through
|
|
// termios2 BOTHER, 8N1, 7E1, 8O2, CRTSCTS), DTR and RTS through TIOCMBIS,
|
|
// TIOCMBIC and TIOCMSET, tcsendbreak, a loopback line and 256 KiB both ways
|
|
// at once with tcdrain and TIOCSERGETLSR, 192 KiB left unread for a while
|
|
// (throttling), the input signals through
|
|
// TIOCMIWAIT, TIOCMGET and TIOCGICOUNT, receive errors with INPCK and
|
|
// PARMRK (then ignored with IGNPAR and IGNBRK), and a hangup (EOF, EIO,
|
|
// ENODEV on reopen, then a new port in the slot).
|
|
#define _GNU_SOURCE
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <poll.h>
|
|
#include <pthread.h>
|
|
#include <sched.h>
|
|
#include <signal.h>
|
|
#include <stdarg.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/ioctl.h>
|
|
#include <termios.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <grp.h>
|
|
|
|
/* The kernel's struct termios2 (asm-generic/termbits.h), for BOTHER. */
|
|
struct ktermios2 {
|
|
unsigned int c_iflag, c_oflag, c_cflag, c_lflag;
|
|
unsigned char c_line;
|
|
unsigned char c_cc[19];
|
|
unsigned int c_ispeed, c_ospeed;
|
|
};
|
|
#define KTCGETS2 _IOR('T', 0x2A, struct ktermios2)
|
|
#define KTCSETS2 _IOW('T', 0x2B, struct ktermios2)
|
|
#define KBOTHER 0010000
|
|
#define KCBAUD 0010017
|
|
|
|
/* struct serial_icounter_struct (linux/serial.h). */
|
|
struct icount {
|
|
int cts, dsr, rng, dcd;
|
|
int rx, tx;
|
|
int frame, overrun, parity, brk;
|
|
int buf_overrun;
|
|
int reserved[9];
|
|
};
|
|
#ifndef TIOCGICOUNT
|
|
#define TIOCGICOUNT 0x545D
|
|
#endif
|
|
#ifndef TIOCMIWAIT
|
|
#define TIOCMIWAIT 0x545C
|
|
#endif
|
|
#ifndef TIOCSERGETLSR
|
|
#define TIOCSERGETLSR 0x5459
|
|
#endif
|
|
#define KTIOCSER_TEMT 0x01
|
|
|
|
static int failures;
|
|
static const char *dev;
|
|
|
|
static void out(const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
vprintf(fmt, ap);
|
|
va_end(ap);
|
|
putchar('\n');
|
|
fflush(stdout);
|
|
}
|
|
|
|
#define OK(...) out("ok " __VA_ARGS__)
|
|
#define FAIL(...) (failures++, out("FAIL " __VA_ARGS__))
|
|
|
|
static double now(void)
|
|
{
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
return ts.tv_sec + ts.tv_nsec / 1e9;
|
|
}
|
|
|
|
/* "step ...", then wait for the host's line. */
|
|
static void step(const char *fmt, ...)
|
|
{
|
|
char line[256];
|
|
va_list ap;
|
|
|
|
fputs("step ", stdout);
|
|
va_start(ap, fmt);
|
|
vprintf(fmt, ap);
|
|
va_end(ap);
|
|
putchar('\n');
|
|
fflush(stdout);
|
|
if (!fgets(line, sizeof(line), stdin)) {
|
|
FAIL("step: stdin closed");
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
/* Read exactly n bytes within ms (poll + non-blocking reads); returns how many. */
|
|
static size_t read_for(int fd, void *buf, size_t n, int ms)
|
|
{
|
|
double end = now() + ms / 1000.0;
|
|
size_t got = 0;
|
|
|
|
while (got < n) {
|
|
struct pollfd p = { .fd = fd, .events = POLLIN };
|
|
int left = (int)((end - now()) * 1000);
|
|
ssize_t r;
|
|
|
|
if (left <= 0 || poll(&p, 1, left) <= 0)
|
|
break;
|
|
r = read(fd, (char *)buf + got, n - got);
|
|
if (r <= 0)
|
|
break;
|
|
got += r;
|
|
}
|
|
return got;
|
|
}
|
|
|
|
static int open_tty(void)
|
|
{
|
|
return open(dev, O_RDWR | O_NOCTTY | O_CLOEXEC);
|
|
}
|
|
|
|
/* Raw mode, CLOCAL and CREAD, with the given line settings. */
|
|
static int set_line(int fd, speed_t speed, int bits, char parity, int stop, int rtscts)
|
|
{
|
|
struct termios t;
|
|
|
|
if (tcgetattr(fd, &t))
|
|
return -1;
|
|
cfmakeraw(&t);
|
|
t.c_cflag &= ~(CSIZE | PARENB | PARODD | CSTOPB | CRTSCTS | CMSPAR);
|
|
t.c_cflag |= CLOCAL | CREAD | (bits == 7 ? CS7 : bits == 6 ? CS6 : bits == 5 ? CS5 : CS8);
|
|
if (parity == 'E')
|
|
t.c_cflag |= PARENB;
|
|
if (parity == 'O')
|
|
t.c_cflag |= PARENB | PARODD;
|
|
if (stop == 2)
|
|
t.c_cflag |= CSTOPB;
|
|
if (rtscts)
|
|
t.c_cflag |= CRTSCTS;
|
|
t.c_cc[VMIN] = 1;
|
|
t.c_cc[VTIME] = 0;
|
|
cfsetispeed(&t, speed);
|
|
cfsetospeed(&t, speed);
|
|
return tcsetattr(fd, TCSANOW, &t);
|
|
}
|
|
|
|
static const struct {
|
|
speed_t speed;
|
|
unsigned int baud;
|
|
int bits;
|
|
char parity;
|
|
int stop;
|
|
int rtscts;
|
|
} lines[] = {
|
|
{ B9600, 9600, 8, 'N', 1, 0 },
|
|
{ B115200, 115200, 8, 'N', 1, 0 },
|
|
{ B460800, 460800, 8, 'N', 1, 0 },
|
|
{ B115200, 115200, 7, 'E', 1, 0 },
|
|
{ B115200, 115200, 8, 'O', 2, 0 },
|
|
{ B921600, 921600, 8, 'N', 1, 1 },
|
|
{ B1500000, 1500000, 8, 'N', 1, 0 },
|
|
};
|
|
|
|
static void check_termios(int fd)
|
|
{
|
|
struct ktermios2 k;
|
|
struct termios t;
|
|
size_t i;
|
|
|
|
for (i = 0; i < sizeof(lines) / sizeof(lines[0]); i++) {
|
|
int bits;
|
|
|
|
if (set_line(fd, lines[i].speed, lines[i].bits, lines[i].parity, lines[i].stop, lines[i].rtscts)) {
|
|
FAIL("termios %u: tcsetattr: %s", lines[i].baud, strerror(errno));
|
|
continue;
|
|
}
|
|
if (tcgetattr(fd, &t)) {
|
|
FAIL("termios %u: tcgetattr: %s", lines[i].baud, strerror(errno));
|
|
continue;
|
|
}
|
|
bits = (t.c_cflag & CSIZE) == CS7 ? 7 : (t.c_cflag & CSIZE) == CS8 ? 8 : 0;
|
|
if (cfgetospeed(&t) != lines[i].speed || bits != lines[i].bits ||
|
|
!!(t.c_cflag & PARENB) != (lines[i].parity != 'N') ||
|
|
!!(t.c_cflag & PARODD) != (lines[i].parity == 'O') ||
|
|
!!(t.c_cflag & CSTOPB) != (lines[i].stop == 2) ||
|
|
!!(t.c_cflag & CRTSCTS) != lines[i].rtscts)
|
|
FAIL("termios %u %d%c%d%s: read back cflag %#o", lines[i].baud, lines[i].bits,
|
|
lines[i].parity, lines[i].stop, lines[i].rtscts ? " crtscts" : "", t.c_cflag);
|
|
else
|
|
OK("termios %u %d%c%d%s", lines[i].baud, lines[i].bits, lines[i].parity,
|
|
lines[i].stop, lines[i].rtscts ? " crtscts" : "");
|
|
step("cfg %u %d %c %d %d", lines[i].baud, lines[i].bits, lines[i].parity, lines[i].stop,
|
|
lines[i].rtscts);
|
|
}
|
|
|
|
/* A rate without a Bxxx constant, as pyserial sets one: termios2 with BOTHER. */
|
|
if (ioctl(fd, KTCGETS2, &k)) {
|
|
FAIL("termios2: TCGETS2: %s", strerror(errno));
|
|
} else {
|
|
k.c_cflag = (k.c_cflag & ~KCBAUD) | KBOTHER;
|
|
k.c_ispeed = k.c_ospeed = 250000;
|
|
if (ioctl(fd, KTCSETS2, &k) || ioctl(fd, KTCGETS2, &k))
|
|
FAIL("termios2: TCSETS2: %s", strerror(errno));
|
|
else if ((k.c_cflag & KCBAUD) != KBOTHER || k.c_ospeed != 250000 || k.c_ispeed != 250000)
|
|
FAIL("termios2 250000: read back cflag %#o ospeed %u ispeed %u", k.c_cflag, k.c_ospeed, k.c_ispeed);
|
|
else
|
|
OK("termios2 250000 (BOTHER)");
|
|
step("cfg 250000 8 N 1 0");
|
|
}
|
|
|
|
/* What the rest runs at. */
|
|
if (set_line(fd, B115200, 8, 'N', 1, 0))
|
|
FAIL("termios 115200: %s", strerror(errno));
|
|
step("cfg 115200 8 N 1 0");
|
|
}
|
|
|
|
static int mget(int fd)
|
|
{
|
|
int m = -1;
|
|
|
|
if (ioctl(fd, TIOCMGET, &m))
|
|
FAIL("TIOCMGET: %s", strerror(errno));
|
|
return m;
|
|
}
|
|
|
|
static void check_signals(int fd)
|
|
{
|
|
int m = mget(fd), b;
|
|
|
|
if (m >= 0 && (m & (TIOCM_DTR | TIOCM_RTS)) == (TIOCM_DTR | TIOCM_RTS))
|
|
OK("signals after open: DTR and RTS up");
|
|
else
|
|
FAIL("signals after open: TIOCMGET %#x, want DTR and RTS", m);
|
|
step("sig 1 1");
|
|
|
|
b = TIOCM_DTR;
|
|
ioctl(fd, TIOCMBIC, &b);
|
|
m = mget(fd);
|
|
if (m >= 0 && !(m & TIOCM_DTR) && (m & TIOCM_RTS))
|
|
OK("TIOCMBIC DTR");
|
|
else
|
|
FAIL("TIOCMBIC DTR: TIOCMGET %#x", m);
|
|
step("sig 0 1");
|
|
|
|
b = TIOCM_DTR;
|
|
ioctl(fd, TIOCMBIS, &b);
|
|
b = TIOCM_RTS;
|
|
ioctl(fd, TIOCMBIC, &b);
|
|
m = mget(fd);
|
|
if (m >= 0 && (m & TIOCM_DTR) && !(m & TIOCM_RTS))
|
|
OK("TIOCMBIS DTR, TIOCMBIC RTS");
|
|
else
|
|
FAIL("TIOCMBIS DTR, TIOCMBIC RTS: TIOCMGET %#x", m);
|
|
step("sig 1 0");
|
|
|
|
b = 0;
|
|
ioctl(fd, TIOCMSET, &b);
|
|
m = mget(fd);
|
|
if (m >= 0 && !(m & (TIOCM_DTR | TIOCM_RTS)))
|
|
OK("TIOCMSET 0");
|
|
else
|
|
FAIL("TIOCMSET 0: TIOCMGET %#x", m);
|
|
step("sig 0 0");
|
|
|
|
b = TIOCM_DTR | TIOCM_RTS;
|
|
ioctl(fd, TIOCMSET, &b);
|
|
m = mget(fd);
|
|
if (m >= 0 && (m & (TIOCM_DTR | TIOCM_RTS)) == (TIOCM_DTR | TIOCM_RTS))
|
|
OK("TIOCMSET DTR|RTS");
|
|
else
|
|
FAIL("TIOCMSET DTR|RTS: TIOCMGET %#x", m);
|
|
step("sig 1 1");
|
|
}
|
|
|
|
static void check_break(int fd)
|
|
{
|
|
double t = now();
|
|
|
|
if (tcsendbreak(fd, 0))
|
|
FAIL("tcsendbreak: %s", strerror(errno));
|
|
else
|
|
OK("tcsendbreak %.0f ms", (now() - t) * 1000);
|
|
step("break");
|
|
}
|
|
|
|
struct writer {
|
|
int fd;
|
|
const uint8_t *buf;
|
|
size_t n;
|
|
int err;
|
|
double secs;
|
|
};
|
|
|
|
static void *write_all(void *arg)
|
|
{
|
|
struct writer *w = arg;
|
|
double t = now();
|
|
size_t off = 0;
|
|
|
|
while (off < w->n) {
|
|
ssize_t r = write(w->fd, w->buf + off, w->n - off);
|
|
|
|
if (r < 0) {
|
|
if (errno == EINTR)
|
|
continue;
|
|
w->err = errno;
|
|
return NULL;
|
|
}
|
|
off += r;
|
|
}
|
|
if (tcdrain(w->fd))
|
|
w->err = errno;
|
|
w->secs = now() - t;
|
|
return NULL;
|
|
}
|
|
|
|
static size_t total_tx;
|
|
|
|
static void check_data(int fd)
|
|
{
|
|
static const char hello[] = "hello kwserial\n";
|
|
const size_t big = 256 * 1024;
|
|
struct writer w = { .fd = fd };
|
|
char back[sizeof(hello)];
|
|
struct icount ic0, ic1;
|
|
uint8_t *tx, *rx;
|
|
pthread_t th;
|
|
size_t i, got;
|
|
int lsr = 0;
|
|
double t;
|
|
|
|
tcflush(fd, TCIOFLUSH);
|
|
if (ioctl(fd, TIOCGICOUNT, &ic0)) {
|
|
FAIL("TIOCGICOUNT: %s", strerror(errno));
|
|
memset(&ic0, 0, sizeof(ic0));
|
|
}
|
|
if (write(fd, hello, strlen(hello)) != (ssize_t)strlen(hello) || tcdrain(fd))
|
|
FAIL("write+tcdrain: %s", strerror(errno));
|
|
total_tx += strlen(hello);
|
|
if (ioctl(fd, TIOCSERGETLSR, &lsr) || !(lsr & KTIOCSER_TEMT))
|
|
FAIL("TIOCSERGETLSR after tcdrain: %#x (%s)", lsr, strerror(errno));
|
|
else
|
|
OK("tcdrain, then TIOCSERGETLSR says TEMT");
|
|
got = read_for(fd, back, strlen(hello), 5000);
|
|
if (got != strlen(hello) || memcmp(back, hello, got))
|
|
FAIL("loopback line: got %zu bytes", got);
|
|
else
|
|
OK("loopback line");
|
|
step("drained %zu", total_tx);
|
|
|
|
/* 256 KiB out on a thread while this one reads them back. */
|
|
tx = malloc(big);
|
|
rx = malloc(big);
|
|
for (i = 0; i < big; i++)
|
|
tx[i] = (uint8_t)(i * 7 + (i >> 9));
|
|
w.buf = tx;
|
|
w.n = big;
|
|
t = now();
|
|
pthread_create(&th, NULL, write_all, &w);
|
|
got = read_for(fd, rx, big, 120000);
|
|
pthread_join(th, NULL);
|
|
t = now() - t;
|
|
total_tx += big;
|
|
if (w.err)
|
|
FAIL("bulk write: %s", strerror(w.err));
|
|
if (got != big) {
|
|
FAIL("bulk: read %zu of %zu bytes", got, big);
|
|
} else if (memcmp(tx, rx, big)) {
|
|
for (i = 0; i < big && tx[i] == rx[i]; i++)
|
|
;
|
|
FAIL("bulk: data differs at byte %zu", i);
|
|
} else {
|
|
OK("bulk 256 KiB both ways in %.2f s (%.0f KiB/s; write+tcdrain %.2f s)", t, big / 1024 / t, w.secs);
|
|
}
|
|
if (ioctl(fd, TIOCGICOUNT, &ic1) == 0) {
|
|
if ((size_t)(ic1.tx - ic0.tx) != big + strlen(hello) || (size_t)(ic1.rx - ic0.rx) != big + strlen(hello))
|
|
FAIL("icount: tx +%d rx +%d, want +%zu", ic1.tx - ic0.tx, ic1.rx - ic0.rx, big + strlen(hello));
|
|
else if (ic1.buf_overrun != ic0.buf_overrun)
|
|
FAIL("icount: buf_overrun +%d", ic1.buf_overrun - ic0.buf_overrun);
|
|
else
|
|
OK("icount tx and rx +%zu", big + strlen(hello));
|
|
}
|
|
step("drained %zu", total_tx);
|
|
|
|
/*
|
|
* 192 KiB written while nothing reads: more than the line discipline,
|
|
* the tty buffer and the host's window hold, so the driver must stop
|
|
* taking bytes from the host (throttle) and go on later, losing none.
|
|
*/
|
|
w = (struct writer){ .fd = fd, .buf = tx, .n = 192 * 1024 };
|
|
write_all(&w);
|
|
total_tx += w.n;
|
|
if (w.err)
|
|
FAIL("unread: write: %s", strerror(w.err));
|
|
usleep(500000);
|
|
got = read_for(fd, rx, w.n, 60000);
|
|
if (got != w.n || memcmp(tx, rx, w.n))
|
|
FAIL("unread 192 KiB: read back %zu bytes%s", got, got == w.n ? ", different" : "");
|
|
else
|
|
OK("unread 192 KiB: written, then read back whole");
|
|
if (ioctl(fd, TIOCGICOUNT, &ic0) == 0 && ic0.buf_overrun != ic1.buf_overrun)
|
|
FAIL("unread: icount buf_overrun +%d", ic0.buf_overrun - ic1.buf_overrun);
|
|
free(tx);
|
|
free(rx);
|
|
step("drained %zu", total_tx);
|
|
}
|
|
|
|
static void on_alarm(int sig)
|
|
{
|
|
(void)sig;
|
|
}
|
|
|
|
static void check_inputs(int fd)
|
|
{
|
|
static const int want[] = {
|
|
TIOCM_CTS, TIOCM_DSR | TIOCM_CAR, TIOCM_RNG,
|
|
TIOCM_CTS | TIOCM_DSR | TIOCM_CAR | TIOCM_RNG, 0,
|
|
};
|
|
const int all = TIOCM_CTS | TIOCM_DSR | TIOCM_CAR | TIOCM_RNG;
|
|
struct sigaction sa = { .sa_handler = on_alarm };
|
|
struct icount ic0, ic1;
|
|
int i, m, prev;
|
|
|
|
sigaction(SIGALRM, &sa, NULL); /* no SA_RESTART: a stuck TIOCMIWAIT ends with EINTR */
|
|
memset(&ic0, 0, sizeof(ic0));
|
|
ioctl(fd, TIOCGICOUNT, &ic0);
|
|
prev = mget(fd) & all;
|
|
if (prev)
|
|
FAIL("inputs before: %#x, want none", prev);
|
|
for (i = 0; i < (int)(sizeof(want) / sizeof(want[0])); i++) {
|
|
double end;
|
|
|
|
out("miwait %d", i);
|
|
alarm(10);
|
|
if (ioctl(fd, TIOCMIWAIT, all)) {
|
|
alarm(0);
|
|
FAIL("TIOCMIWAIT %d: %s", i, strerror(errno));
|
|
continue;
|
|
}
|
|
alarm(0);
|
|
/* The host may report the four in more than one change. */
|
|
for (end = now() + 2; (m = mget(fd) & all) != want[i] && now() < end;)
|
|
usleep(20000);
|
|
if (m != want[i])
|
|
FAIL("inputs %d: TIOCMGET %#x, want %#x", i, m, want[i]);
|
|
else
|
|
OK("inputs %d: TIOCMIWAIT woke, TIOCMGET %#x", i, m);
|
|
prev = m;
|
|
}
|
|
if (ioctl(fd, TIOCGICOUNT, &ic1) == 0) {
|
|
/* Edges: CTS, DSR and DCD 4 each (on, off, on, off), RI 2 (on, off). */
|
|
if (ic1.cts - ic0.cts != 4 || ic1.dsr - ic0.dsr != 4 || ic1.dcd - ic0.dcd != 4 || ic1.rng - ic0.rng != 2)
|
|
FAIL("icount inputs: cts +%d dsr +%d dcd +%d rng +%d, want +4 +4 +4 +2",
|
|
ic1.cts - ic0.cts, ic1.dsr - ic0.dsr, ic1.dcd - ic0.dcd, ic1.rng - ic0.rng);
|
|
else
|
|
OK("icount cts, dsr, dcd +4 each, rng +2");
|
|
}
|
|
}
|
|
|
|
static void check_errors(int fd)
|
|
{
|
|
static const uint8_t marked[] = { 0377, 0, 0, 0377, 0, 0, 0377, 0, 0, 'Z' };
|
|
struct icount ic0, ic1, ic2;
|
|
struct termios t;
|
|
uint8_t buf[64];
|
|
size_t got;
|
|
|
|
tcgetattr(fd, &t);
|
|
t.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | ISTRIP);
|
|
t.c_iflag |= INPCK | PARMRK;
|
|
tcsetattr(fd, TCSANOW, &t);
|
|
tcflush(fd, TCIFLUSH);
|
|
memset(&ic0, 0, sizeof(ic0));
|
|
ioctl(fd, TIOCGICOUNT, &ic0);
|
|
step("errors Z");
|
|
/* Break, framing and parity as \377 \0 \0 each (PARMRK), overrun as nothing, then Z. */
|
|
got = read_for(fd, buf, sizeof(marked), 5000);
|
|
if (got != sizeof(marked) || memcmp(buf, marked, got)) {
|
|
char hex[3 * sizeof(buf) + 1] = "";
|
|
size_t i;
|
|
|
|
for (i = 0; i < got; i++)
|
|
sprintf(hex + 3 * i, " %02x", buf[i]);
|
|
FAIL("errors with INPCK|PARMRK: read%s (%zu bytes), want 3 x \"ff 00 00\" then 5a", hex, got);
|
|
} else {
|
|
OK("errors with INPCK|PARMRK: break, framing and parity marked, then the data");
|
|
}
|
|
ioctl(fd, TIOCGICOUNT, &ic1);
|
|
if (ic1.brk - ic0.brk != 1 || ic1.frame - ic0.frame != 1 || ic1.parity - ic0.parity != 1 ||
|
|
ic1.overrun - ic0.overrun != 1)
|
|
FAIL("icount errors: brk +%d frame +%d parity +%d overrun +%d, want +1 each",
|
|
ic1.brk - ic0.brk, ic1.frame - ic0.frame, ic1.parity - ic0.parity, ic1.overrun - ic0.overrun);
|
|
else
|
|
OK("icount brk, frame, parity, overrun +1 each");
|
|
|
|
/* Ignored: IGNPAR and IGNBRK drop all four (overrun too, as 8250 does). */
|
|
t.c_iflag |= IGNPAR | IGNBRK;
|
|
t.c_iflag &= ~PARMRK;
|
|
tcsetattr(fd, TCSANOW, &t);
|
|
step("errors Y");
|
|
got = read_for(fd, buf, sizeof(buf), 1500);
|
|
if (got != 1 || buf[0] != 'Y')
|
|
FAIL("errors with IGNPAR|IGNBRK: read %zu bytes (first %#x), want just Y", got, got ? buf[0] : 0);
|
|
else
|
|
OK("errors with IGNPAR|IGNBRK: only the data");
|
|
ioctl(fd, TIOCGICOUNT, &ic2);
|
|
if (ic2.brk - ic1.brk != 1 || ic2.frame - ic1.frame != 1 || ic2.parity - ic1.parity != 1 ||
|
|
ic2.overrun - ic1.overrun != 1)
|
|
FAIL("icount ignored errors: brk +%d frame +%d parity +%d overrun +%d, want +1 each",
|
|
ic2.brk - ic1.brk, ic2.frame - ic1.frame, ic2.parity - ic1.parity, ic2.overrun - ic1.overrun);
|
|
else
|
|
OK("icount counts ignored errors too");
|
|
set_line(fd, B115200, 8, 'N', 1, 0);
|
|
}
|
|
|
|
static void check_hangup(int *fdp)
|
|
{
|
|
static const char again[] = "again\n";
|
|
char c, back[sizeof(again)];
|
|
int fd = *fdp;
|
|
ssize_t r;
|
|
|
|
out("hangup-wait");
|
|
/* The host unplugs the port while this read waits. */
|
|
r = read(fd, &c, 1);
|
|
if (r != 0)
|
|
FAIL("hangup: read returned %zd (%s), want 0 (EOF)", r, r < 0 ? strerror(errno) : "data");
|
|
else
|
|
OK("hangup: read gives EOF");
|
|
r = write(fd, "x", 1);
|
|
if (r != -1 || errno != EIO)
|
|
FAIL("hangup: write returned %zd (%s), want EIO", r, r < 0 ? strerror(errno) : "written");
|
|
else
|
|
OK("hangup: write fails with EIO");
|
|
close(fd);
|
|
fd = open_tty();
|
|
if (fd >= 0) {
|
|
FAIL("reopen after unplug: opened, want ENODEV");
|
|
close(fd);
|
|
} else if (errno != ENODEV) {
|
|
FAIL("reopen after unplug: %s, want ENODEV", strerror(errno));
|
|
} else {
|
|
OK("reopen after unplug: ENODEV");
|
|
}
|
|
step("reattach");
|
|
fd = open_tty();
|
|
if (fd < 0) {
|
|
FAIL("open with a new port: %s", strerror(errno));
|
|
*fdp = -1;
|
|
return;
|
|
}
|
|
set_line(fd, B115200, 8, 'N', 1, 0);
|
|
if (write(fd, again, strlen(again)) != (ssize_t)strlen(again) || tcdrain(fd) ||
|
|
read_for(fd, back, strlen(again), 5000) != strlen(again) || memcmp(back, again, strlen(again)))
|
|
FAIL("loopback on the new port");
|
|
else
|
|
OK("open with a new port, loopback");
|
|
*fdp = fd;
|
|
}
|
|
|
|
static int run(int cpu)
|
|
{
|
|
int fd;
|
|
|
|
if (cpu >= 0) {
|
|
cpu_set_t set;
|
|
|
|
CPU_ZERO(&set);
|
|
CPU_SET(cpu, &set);
|
|
if (sched_setaffinity(0, sizeof(set), &set))
|
|
FAIL("sched_setaffinity %d: %s", cpu, strerror(errno));
|
|
else
|
|
OK("on CPU %d (sched_getcpu %d)", cpu, sched_getcpu());
|
|
}
|
|
fd = open_tty();
|
|
if (fd < 0) {
|
|
FAIL("open %s: %s", dev, strerror(errno));
|
|
out("done %d failure(s)", failures);
|
|
return 1;
|
|
}
|
|
OK("open %s", dev);
|
|
check_termios(fd);
|
|
check_signals(fd);
|
|
check_break(fd);
|
|
check_data(fd);
|
|
check_inputs(fd);
|
|
check_errors(fd);
|
|
check_hangup(&fd);
|
|
if (fd >= 0)
|
|
close(fd);
|
|
out("done %d failure(s)", failures);
|
|
return failures ? 1 : 0;
|
|
}
|
|
|
|
static int open_as(int uid)
|
|
{
|
|
int fd;
|
|
|
|
if ((int)getuid() != uid && (setgroups(0, NULL) || setgid(uid) || setuid(uid))) {
|
|
out("FAIL open-as: dropping to %d: %s", uid, strerror(errno));
|
|
return 1;
|
|
}
|
|
fd = open_tty();
|
|
if (fd < 0) {
|
|
out("open-as %d: %s", uid, strerror(errno));
|
|
return 1;
|
|
}
|
|
out("open-as %d: ok", uid);
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
|
|
static int park(const char *path)
|
|
{
|
|
char c;
|
|
ssize_t r, w;
|
|
int e1, e2, fd = open_tty();
|
|
FILE *f;
|
|
|
|
if (fd < 0) {
|
|
out("FAIL park: open: %s", strerror(errno));
|
|
return 1;
|
|
}
|
|
set_line(fd, B115200, 8, 'N', 1, 0);
|
|
out("ready");
|
|
r = read(fd, &c, 1);
|
|
e1 = errno;
|
|
w = write(fd, "x", 1);
|
|
e2 = errno;
|
|
f = fopen(path, "w");
|
|
if (!f)
|
|
return 1;
|
|
fprintf(f, "read %zd %s\nwrite %zd %s\n", r, r < 0 ? strerror(e1) : "-", w, w < 0 ? strerror(e2) : "-");
|
|
fclose(f);
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
if (argc >= 3 && !strcmp(argv[1], "run")) {
|
|
dev = argv[2];
|
|
return run(argc >= 4 ? atoi(argv[3]) : -1);
|
|
}
|
|
if (argc >= 4 && !strcmp(argv[1], "open-as")) {
|
|
dev = argv[3];
|
|
return open_as(atoi(argv[2]));
|
|
}
|
|
if (argc >= 4 && !strcmp(argv[1], "park")) {
|
|
dev = argv[2];
|
|
return park(argv[3]);
|
|
}
|
|
fprintf(stderr, "usage: ttyacm run DEV [CPU] | open-as UID DEV | park DEV OUT\n");
|
|
return 2;
|
|
}
|