Module Name:    src
Committed By:   rillig
Date:           Sat Feb 13 06:45:42 UTC 2021

Modified Files:
        src/tests/lib/libcurses/director: director.c
        src/tests/lib/libcurses/slave: commands.c slave.c slave.h

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/tests/lib/libcurses/director/director.c
cvs rdiff -u -r1.10 -r1.11 src/tests/lib/libcurses/slave/commands.c
cvs rdiff -u -r1.11 -r1.12 src/tests/lib/libcurses/slave/slave.c
cvs rdiff -u -r1.5 -r1.6 src/tests/lib/libcurses/slave/slave.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/lib/libcurses/director/director.c
diff -u src/tests/lib/libcurses/director/director.c:1.19 src/tests/lib/libcurses/director/director.c:1.20
--- src/tests/lib/libcurses/director/director.c:1.19	Sat Feb 13 05:38:16 2021
+++ src/tests/lib/libcurses/director/director.c	Sat Feb 13 06:45:42 2021
@@ -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 <[email protected]>
@@ -133,7 +133,7 @@ main(int argc, char *argv[])
 	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 @@ main(int argc, char *argv[])
 		/* 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 */

Index: src/tests/lib/libcurses/slave/commands.c
diff -u src/tests/lib/libcurses/slave/commands.c:1.10 src/tests/lib/libcurses/slave/commands.c:1.11
--- src/tests/lib/libcurses/slave/commands.c:1.10	Tue Feb  9 20:42:31 2021
+++ src/tests/lib/libcurses/slave/commands.c	Sat Feb 13 06:45:42 2021
@@ -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 <[email protected]>
@@ -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 @@ report_type(data_enum_t return_type)
 	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 @@ report_count(int count)
 	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 @@ report_message(int type, const char *sta
 
 	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 @@ report_nstr(chtype *string)
 	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 @@ report_cchar(cchar_t c)
 	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 @@ report_wstr(wchar_t *wstr)
 	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__);
 }

Index: src/tests/lib/libcurses/slave/slave.c
diff -u src/tests/lib/libcurses/slave/slave.c:1.11 src/tests/lib/libcurses/slave/slave.c:1.12
--- src/tests/lib/libcurses/slave/slave.c:1.11	Fri Feb 12 21:29:54 2021
+++ src/tests/lib/libcurses/slave/slave.c	Sat Feb 13 06:45:42 2021
@@ -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 <[email protected]>
@@ -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 @@ process_commands(void)
 		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 @@ process_commands(void)
 			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 @@ process_commands(void)
 					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 @@ process_commands(void)
 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();
 

Index: src/tests/lib/libcurses/slave/slave.h
diff -u src/tests/lib/libcurses/slave/slave.h:1.5 src/tests/lib/libcurses/slave/slave.h:1.6
--- src/tests/lib/libcurses/slave/slave.h:1.5	Mon Feb  8 19:15:21 2021
+++ src/tests/lib/libcurses/slave/slave.h	Sat Feb 13 06:45:42 2021
@@ -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 <[email protected]>
@@ -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);

Reply via email to