xref: /netbsd/tests/lib/libcurses/director/director.c (revision 2b2ffcbc)
1 /*	$NetBSD: director.c,v 1.29 2021/06/10 07:21:07 mcf Exp $	*/
2 
3 /*-
4  * Copyright 2009 Brett Lymn <blymn@NetBSD.org>
5  * Copyright 2021 Roland Illig <rillig@NetBSD.org>
6  *
7  * All rights reserved.
8  *
9  * This code has been donated to The NetBSD Foundation by the Author.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #include <sys/mman.h>
34 #include <sys/wait.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <ctype.h>
38 #include <termios.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <util.h>
44 #include <err.h>
45 #include "returns.h"
46 #include "director.h"
47 
48 void yyparse(void);
49 #define DEF_TERMPATH "."
50 #define DEF_TERM "atf"
51 #define DEF_SLAVE "./slave"
52 
53 const char *def_check_path = "./"; /* default check path */
54 
55 extern size_t nvars;		/* In testlang_conf.y */
56 saved_data_t  saved_output;	/* In testlang_conf.y */
57 int to_slave;
58 int from_slave;
59 int master;			/* pty to the slave */
60 int verbose;			/* control verbosity of tests */
61 int check_file_flag;		/* control check-file generation */
62 const char *check_path;		/* path to prepend to check files for output
63 				   validation */
64 char *cur_file;			/* name of file currently being read */
65 
66 void init_parse_variables(int);	/* in testlang_parse.y */
67 
68 /*
69  * Handle the slave exiting unexpectedly, try to recover the exit message
70  * and print it out.
71  *
72  * FIXME: Must not use stdio in a signal handler.  This leads to incomplete
73  * output in verbose mode, truncating the useful part of the error message.
74  */
75 static void
slave_died(int signo)76 slave_died(int signo)
77 {
78 	char last_words[256];
79 	size_t count;
80 
81 	fprintf(stderr, "ERROR: Slave has exited\n");
82 	if (saved_output.count > 0) {
83 		fprintf(stderr, "output from slave: ");
84 		for (count = 0; count < saved_output.count; count ++) {
85 			unsigned char b = saved_output.data[count];
86 			if (isprint(b))
87 				fprintf(stderr, "%c", b);
88 			else
89 				fprintf(stderr, "\\x%02x", b);
90 		}
91 		fprintf(stderr, "\n");
92 	}
93 
94 	if ((count = read(master, &last_words, 255)) > 0) {
95 		last_words[count] = '\0';
96 		fprintf(stderr, "slave exited with message \"%s\"\n",
97 			last_words);
98 	}
99 
100 	exit(2);
101 }
102 
103 
104 static void
usage(void)105 usage(void)
106 {
107 	fprintf(stderr, "Usage: %s [-vgf] [-I include-path] [-C check-path] "
108 	    "[-T terminfo-file] [-s pathtoslave] [-t term] "
109 	    "commandfile\n", getprogname());
110 	fprintf(stderr, " where:\n");
111 	fprintf(stderr, "    -v enables verbose test output\n");
112 	fprintf(stderr, "    -g generates check-files if they do not exist\n");
113 	fprintf(stderr, "    -f overwrites check-files with the actual data\n");
114 	fprintf(stderr, "    -T is a directory containing the terminfo.cdb "
115 	    "file, or a file holding the terminfo description\n");
116 	fprintf(stderr, "    -s is the path to the slave executable\n");
117 	fprintf(stderr, "    -t is value to set TERM to for the test\n");
118 	fprintf(stderr, "    -C is the directory for check-files\n");
119 	fprintf(stderr, "    commandfile is a file of test directives\n");
120 	exit(1);
121 }
122 
123 
124 int
main(int argc,char * argv[])125 main(int argc, char *argv[])
126 {
127 	extern char *optarg;
128 	extern int optind;
129 	const char *termpath, *term, *slave;
130 	int ch;
131 	pid_t slave_pid;
132 	extern FILE *yyin;
133 	char *arg1, *arg2;
134 	struct termios term_attr;
135 	struct stat st;
136 	int pipe_to_slave[2], pipe_from_slave[2];
137 
138 	termpath = term = slave = NULL;
139 	verbose = 0;
140 	check_file_flag = 0;
141 
142 	while ((ch = getopt(argc, argv, "vgfC:s:t:T:")) != -1) {
143 		switch (ch) {
144 		case 'C':
145 			check_path = optarg;
146 			break;
147 		case 'T':
148 			termpath = optarg;
149 			break;
150 		case 's':
151 			slave = optarg;
152 			break;
153 		case 't':
154 			term = optarg;
155 			break;
156 		case 'v':
157 			verbose = 1;
158 			break;
159 		case 'g':
160 			check_file_flag |= GEN_CHECK_FILE;
161 			break;
162 		case 'f':
163 			check_file_flag |= FORCE_GEN;
164 			break;
165 		case '?':
166 		default:
167 			usage();
168 			break;
169 		}
170 	}
171 
172 	argc -= optind;
173 	argv += optind;
174 	if (argc != 1)
175 		usage();
176 
177 	if (termpath == NULL)
178 		termpath = DEF_TERMPATH;
179 
180 	if (slave == NULL)
181 		slave = DEF_SLAVE;
182 
183 	if (term == NULL)
184 		term = DEF_TERM;
185 
186 	if (check_path == NULL)
187 		check_path = getenv("CHECK_PATH");
188 	if ((check_path == NULL) || (check_path[0] == '\0')) {
189 		warnx("$CHECK_PATH not set, defaulting to %s", def_check_path);
190 		check_path = def_check_path;
191 	}
192 
193 	signal(SIGCHLD, slave_died);
194 
195 	if (setenv("TERM", term, 1) != 0)
196 		err(2, "Failed to set TERM variable");
197 
198 	if (unsetenv("ESCDELAY") != 0)
199 		err(2, "Failed to unset ESCDELAY variable");
200 
201 	if (stat(termpath, &st) == -1)
202 		err(1, "Cannot stat %s", termpath);
203 
204 	if (S_ISDIR(st.st_mode)) {
205 		char tinfo[MAXPATHLEN];
206 		int l = snprintf(tinfo, sizeof(tinfo), "%s/%s", termpath,
207 		    "terminfo.cdb");
208 		if (stat(tinfo, &st) == -1)
209 			err(1, "Cannot stat `%s'", tinfo);
210 		if (l >= 4)
211 			tinfo[l - 4] = '\0';
212 		if (setenv("TERMINFO", tinfo, 1) != 0)
213 			err(1, "Failed to set TERMINFO variable");
214 	} else {
215 		int fd;
216 		char *tinfo;
217 		if ((fd = open(termpath, O_RDONLY)) == -1)
218 			err(1, "Cannot open `%s'", termpath);
219 		if ((tinfo = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_FILE,
220 			fd, 0)) == MAP_FAILED)
221 			err(1, "Cannot map `%s'", termpath);
222 		if (setenv("TERMINFO", tinfo, 1) != 0)
223 			err(1, "Failed to set TERMINFO variable");
224 		close(fd);
225 		munmap(tinfo, (size_t)st.st_size);
226 	}
227 
228 	if (pipe(pipe_to_slave) < 0)
229 		err(1, "Command pipe creation failed");
230 	to_slave = pipe_to_slave[1];
231 
232 	if (pipe(pipe_from_slave) < 0)
233 		err(1, "Slave pipe creation failed");
234 	from_slave = pipe_from_slave[0];
235 
236 	/*
237 	 * Create default termios settings for later use
238 	 */
239 	memset(&term_attr, 0, sizeof(term_attr));
240 	term_attr.c_iflag = TTYDEF_IFLAG;
241 	term_attr.c_oflag = TTYDEF_OFLAG;
242 	term_attr.c_cflag = TTYDEF_CFLAG;
243 	term_attr.c_lflag = TTYDEF_LFLAG;
244 	cfsetspeed(&term_attr, TTYDEF_SPEED);
245 	term_attr.c_cc[VERASE] = '\b';
246 	term_attr.c_cc[VKILL] = '\025'; /* ^U */
247 
248 	if ((slave_pid = forkpty(&master, NULL, &term_attr, NULL)) < 0)
249 		err(1, "Fork of pty for slave failed\n");
250 
251 	if (slave_pid == 0) {
252 		/* slave side, just exec the slave process */
253 		if (asprintf(&arg1, "%d", pipe_to_slave[0]) < 0)
254 			err(1, "arg1 conversion failed");
255 		close(pipe_to_slave[1]);
256 
257 		close(pipe_from_slave[0]);
258 		if (asprintf(&arg2, "%d", pipe_from_slave[1]) < 0)
259 			err(1, "arg2 conversion failed");
260 
261 		if (execl(slave, slave, arg1, arg2, (char *)0) < 0)
262 			err(1, "Exec of slave %s failed", slave);
263 
264 		/* NOT REACHED */
265 	}
266 
267 	(void)close(pipe_to_slave[0]);
268 	(void)close(pipe_from_slave[1]);
269 
270 	fcntl(master, F_SETFL, O_NONBLOCK);
271 
272 	if ((yyin = fopen(argv[0], "r")) == NULL)
273 		err(1, "Cannot open command file %s", argv[0]);
274 
275 	if ((cur_file = strdup(argv[0])) == NULL)
276 		err(2, "Failed to alloc memory for test file name");
277 
278 	init_parse_variables(1);
279 
280 	yyparse();
281 	fclose(yyin);
282 
283 	signal(SIGCHLD, SIG_DFL);
284 	(void)close(to_slave);
285 	(void)close(from_slave);
286 
287 	int status;
288 	(void)waitpid(slave_pid, &status, 0);
289 
290 	exit(0);
291 }
292