Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/tests/lib/libcurses tests/libcurses: fix pipe handling in ch...
details: https://anonhg.NetBSD.org/src/rev/1999cdc52f9e
branches: trunk
changeset: 959404:1999cdc52f9e
user: rillig <rillig%NetBSD.org@localhost>
date: Sat Feb 13 06:45:42 2021 +0000
description:
tests/libcurses: fix pipe handling in child process
The child process only ever needs 2 ends of the pipes: one for reading
the commands, one for writing back the results.
diffstat:
tests/lib/libcurses/director/director.c | 16 +++++---------
tests/lib/libcurses/slave/commands.c | 34 +++++++++++++++-----------------
tests/lib/libcurses/slave/slave.c | 29 ++++++++++++---------------
tests/lib/libcurses/slave/slave.h | 6 ++--
4 files changed, 38 insertions(+), 47 deletions(-)
diffs (260 lines):
diff -r 8155f17f716a -r 1999cdc52f9e tests/lib/libcurses/director/director.c
--- a/tests/lib/libcurses/director/director.c Sat Feb 13 06:29:45 2021 +0000
+++ b/tests/lib/libcurses/director/director.c Sat Feb 13 06:45:42 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: director.c,v 1.19 2021/02/13 05:38:16 rillig Exp $ */
+/* $NetBSD: director.c,v 1.20 2021/02/13 06:45:42 rillig Exp $ */
/*-
* Copyright 2009 Brett Lymn <blymn%NetBSD.org@localhost>
@@ -133,7 +133,7 @@
int ch;
pid_t slave_pid;
extern FILE *yyin;
- char *arg1, *arg2, *arg3, *arg4;
+ char *arg1, *arg2;
struct termios term_attr;
struct stat st;
@@ -263,17 +263,13 @@
/* slave side, just exec the slave process */
if (asprintf(&arg1, "%d", cmdpipe[0]) < 0)
err(1, "arg1 conversion failed");
+ close(cmdpipe[1]);
- if (asprintf(&arg2, "%d", cmdpipe[1]) < 0)
+ close(slvpipe[0]);
+ if (asprintf(&arg2, "%d", slvpipe[1]) < 0)
err(1, "arg2 conversion failed");
- if (asprintf(&arg3, "%d", slvpipe[0]) < 0)
- err(1, "arg3 conversion failed");
-
- if (asprintf(&arg4, "%d", slvpipe[1]) < 0)
- err(1, "arg4 conversion failed");
-
- if (execl(slave, slave, arg1, arg2, arg3, arg4, (char *)0) < 0)
+ if (execl(slave, slave, arg1, arg2, (char *)0) < 0)
err(1, "Exec of slave %s failed", slave);
/* NOT REACHED */
diff -r 8155f17f716a -r 1999cdc52f9e tests/lib/libcurses/slave/commands.c
--- a/tests/lib/libcurses/slave/commands.c Sat Feb 13 06:29:45 2021 +0000
+++ b/tests/lib/libcurses/slave/commands.c Sat Feb 13 06:45:42 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: commands.c,v 1.10 2021/02/09 20:42:31 rillig Exp $ */
+/* $NetBSD: commands.c,v 1.11 2021/02/13 06:45:42 rillig Exp $ */
/*-
* Copyright 2009 Brett Lymn <blymn%NetBSD.org@localhost>
@@ -40,8 +40,6 @@
#include "slave.h"
#include "command_table.h"
-extern int cmdpipe[2];
-extern int slvpipe[2];
extern int initdone;
static void report_type(data_enum_t);
@@ -141,7 +139,7 @@
int type;
type = return_type;
- if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0)
+ if (write(to_director, &type, sizeof(int)) < 0)
err(1, "command pipe write for status type failed");
}
@@ -155,10 +153,10 @@
int type;
type = data_count;
- if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0)
+ if (write(to_director, &type, sizeof(int)) < 0)
err(1, "command pipe write for count type failed");
- if (write(slvpipe[WRITE_PIPE], &count, sizeof(int)) < 0)
+ if (write(to_director, &count, sizeof(int)) < 0)
err(1, "command pipe write for count");
}
@@ -191,13 +189,13 @@
len = strlen(status);
- if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0)
+ if (write(to_director, &type, sizeof(int)) < 0)
err(1, "command pipe write for message type failed");
- if (write(slvpipe[WRITE_PIPE], &len, sizeof(int)) < 0)
+ if (write(to_director, &len, sizeof(int)) < 0)
err(1, "command pipe write for message length failed");
- if (write(slvpipe[WRITE_PIPE], status, len) < 0)
+ if (write(to_director, status, len) < 0)
err(1, "command pipe write of message data failed");
}
@@ -234,15 +232,15 @@
len *= sizeof(chtype);
type = data_byte;
- if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0)
+ if (write(to_director, &type, sizeof(int)) < 0)
err(1, "%s: command pipe write for status type failed",
__func__);
- if (write(slvpipe[WRITE_PIPE], &len, sizeof(int)) < 0)
+ if (write(to_director, &len, sizeof(int)) < 0)
err(1, "%s: command pipe write for status length failed",
__func__);
- if (write(slvpipe[WRITE_PIPE], string, len) < 0)
+ if (write(to_director, string, len) < 0)
err(1, "%s: command pipe write of status data failed",
__func__);
}
@@ -257,15 +255,15 @@
len = sizeof(cchar_t);
type = data_cchar;
- if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0)
+ if (write(to_director, &type, sizeof(int)) < 0)
err(1, "%s: command pipe write for status type failed",
__func__);
- if (write(slvpipe[WRITE_PIPE], &len, sizeof(int)) < 0)
+ if (write(to_director, &len, sizeof(int)) < 0)
err(1, "%s: command pipe write for status length failed",
__func__);
- if (write(slvpipe[WRITE_PIPE], &c, len) < 0)
+ if (write(to_director, &c, len) < 0)
err(1, "%s: command pipe write of status data failed",
__func__);
}
@@ -303,15 +301,15 @@
len *= sizeof(wchar_t);
type = data_wchar;
- if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0)
+ if (write(to_director, &type, sizeof(int)) < 0)
err(1, "%s: command pipe write for status type failed",
__func__);
- if (write(slvpipe[WRITE_PIPE], &len, sizeof(int)) < 0)
+ if (write(to_director, &len, sizeof(int)) < 0)
err(1, "%s: command pipe write for status length failed",
__func__);
- if (write(slvpipe[WRITE_PIPE], wstr, len) < 0)
+ if (write(to_director, wstr, len) < 0)
err(1, "%s: command pipe write of status data failed",
__func__);
}
diff -r 8155f17f716a -r 1999cdc52f9e tests/lib/libcurses/slave/slave.c
--- a/tests/lib/libcurses/slave/slave.c Sat Feb 13 06:29:45 2021 +0000
+++ b/tests/lib/libcurses/slave/slave.c Sat Feb 13 06:45:42 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: slave.c,v 1.11 2021/02/12 21:29:54 rillig Exp $ */
+/* $NetBSD: slave.c,v 1.12 2021/02/13 06:45:42 rillig Exp $ */
/*-
* Copyright 2009 Brett Lymn <blymn%NetBSD.org@localhost>
@@ -39,8 +39,8 @@
#include "returns.h"
#include "slave.h"
-int cmdpipe[2];
-int slvpipe[2];
+int from_director;
+int to_director;
int initdone = 0;
#if 0
@@ -65,13 +65,13 @@
err(1, "slave cmdbuf malloc failed");
for (;;) {
- if (read(cmdpipe[READ_PIPE], &type, sizeof(int)) < 0)
+ if (read(from_director, &type, sizeof(int)) < 0)
err(1, "slave command type read failed");
if (type != data_string)
errx(1, "Unexpected type for command, got %d", type);
- if (read(cmdpipe[READ_PIPE], &len, sizeof(int)) < 0)
+ if (read(from_director, &len, sizeof(int)) < 0)
err(1, "slave command len read failed");
if ((len + 1) > maxlen) {
@@ -82,17 +82,17 @@
cmdbuf = tmpbuf;
}
- if (read(cmdpipe[READ_PIPE], cmdbuf, len) < 0)
+ if (read(from_director, cmdbuf, len) < 0)
err(1, "slave command read failed");
cmdbuf[len] = '\0';
argslen = 0;
args = NULL;
do {
- if (read(cmdpipe[READ_PIPE], &type, sizeof(int)) < 0)
+ if (read(from_director, &type, sizeof(int)) < 0)
err(1, "slave arg type read failed");
- if (read(cmdpipe[READ_PIPE], &len, sizeof(int)) < 0)
+ if (read(from_director, &len, sizeof(int)) < 0)
err(1, "slave arg len read failed");
if (len >= 0) {
@@ -117,7 +117,7 @@
else
args[argslen][0] = '\0';
} else {
- read(cmdpipe[READ_PIPE], args[argslen],
+ read(from_director, args[argslen],
len);
if (type != data_byte)
args[argslen][len] = '\0';
@@ -155,15 +155,12 @@
int
main(int argc, char *argv[])
{
- if (argc != 5) {
- fprintf(stderr, "Usage: %s <cmdin> <cmdout> <slvin> <slvout>\n",
- getprogname());
+ if (argc != 3) {
+ fprintf(stderr, "usage: %s <in_fd> <out_fd>\n", getprogname());
return 0;
}
- sscanf(argv[1], "%d", &cmdpipe[0]);
- sscanf(argv[2], "%d", &cmdpipe[1]);
- sscanf(argv[3], "%d", &slvpipe[0]);
- sscanf(argv[4], "%d", &slvpipe[1]);
+ sscanf(argv[1], "%d", &from_director);
+ sscanf(argv[2], "%d", &to_director);
process_commands();
diff -r 8155f17f716a -r 1999cdc52f9e tests/lib/libcurses/slave/slave.h
--- a/tests/lib/libcurses/slave/slave.h Sat Feb 13 06:29:45 2021 +0000
+++ b/tests/lib/libcurses/slave/slave.h Sat Feb 13 06:45:42 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: slave.h,v 1.5 2021/02/08 19:15:21 rillig Exp $ */
+/* $NetBSD: slave.h,v 1.6 2021/02/13 06:45:42 rillig Exp $ */
/*-
* Copyright 2009 Brett Lymn <blymn%NetBSD.org@localhost>
@@ -33,8 +33,8 @@
#include <curses.h>
-#define READ_PIPE 0
-#define WRITE_PIPE 1
+extern int from_director;
+extern int to_director;
void command_execute(char *, int, char **);
void report_count(int);
Home |
Main Index |
Thread Index |
Old Index