director.c revision 1.30 1 /* $NetBSD: director.c,v 1.30 2024/07/18 22:10:51 blymn Exp $ */
2
3 /*-
4 * Copyright 2009 Brett Lymn <blymn (at) NetBSD.org>
5 * Copyright 2021 Roland Illig <rillig (at) 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 nofail; /* don't exit on check file fail */
61 int verbose; /* control verbosity of tests */
62 int check_file_flag; /* control check-file generation */
63 const char *check_path; /* path to prepend to check files for output
64 validation */
65 char *cur_file; /* name of file currently being read */
66
67 void init_parse_variables(int); /* in testlang_parse.y */
68
69 /*
70 * Handle the slave exiting unexpectedly, try to recover the exit message
71 * and print it out.
72 *
73 * FIXME: Must not use stdio in a signal handler. This leads to incomplete
74 * output in verbose mode, truncating the useful part of the error message.
75 */
76 static void
77 slave_died(int signo)
78 {
79 char last_words[256];
80 size_t count;
81
82 fprintf(stderr, "ERROR: Slave has exited\n");
83 if (saved_output.count > 0) {
84 fprintf(stderr, "output from slave: ");
85 for (count = 0; count < saved_output.count; count ++) {
86 unsigned char b = saved_output.data[count];
87 if (isprint(b))
88 fprintf(stderr, "%c", b);
89 else
90 fprintf(stderr, "\\x%02x", b);
91 }
92 fprintf(stderr, "\n");
93 }
94
95 if ((count = read(master, &last_words, 255)) > 0) {
96 last_words[count] = '\0';
97 fprintf(stderr, "slave exited with message \"%s\"\n",
98 last_words);
99 }
100
101 exit(2);
102 }
103
104
105 static void
106 usage(void)
107 {
108 fprintf(stderr, "Usage: %s [-vgf] [-I include-path] [-C check-path] "
109 "[-T terminfo-file] [-s pathtoslave] [-t term] "
110 "commandfile\n", getprogname());
111 fprintf(stderr, " where:\n");
112 fprintf(stderr, " -v enables verbose test output\n");
113 fprintf(stderr, " -g generates check-files if they do not exist\n");
114 fprintf(stderr, " -f overwrites check-files with the actual data\n");
115 fprintf(stderr, " -T is a directory containing the terminfo.cdb "
116 "file, or a file holding the terminfo description\n");
117 fprintf(stderr, " -s is the path to the slave executable\n");
118 fprintf(stderr, " -t is value to set TERM to for the test\n");
119 fprintf(stderr, " -C is the directory for check-files\n");
120 fprintf(stderr, " commandfile is a file of test directives\n");
121 exit(1);
122 }
123
124
125 int
126 main(int argc, char *argv[])
127 {
128 extern char *optarg;
129 extern int optind;
130 const char *termpath, *term, *slave;
131 int ch;
132 pid_t slave_pid;
133 extern FILE *yyin;
134 char *arg1, *arg2;
135 struct termios term_attr;
136 struct stat st;
137 int pipe_to_slave[2], pipe_from_slave[2];
138
139 termpath = term = slave = NULL;
140 nofail = 0;
141 verbose = 0;
142 check_file_flag = 0;
143
144 while ((ch = getopt(argc, argv, "nvgfC:s:t:T:")) != -1) {
145 switch (ch) {
146 case 'C':
147 check_path = optarg;
148 break;
149 case 'T':
150 termpath = optarg;
151 break;
152 case 'n':
153 nofail = 1;
154 break;
155 case 's':
156 slave = optarg;
157 break;
158 case 't':
159 term = optarg;
160 break;
161 case 'v':
162 verbose = 1;
163 break;
164 case 'g':
165 check_file_flag |= GEN_CHECK_FILE;
166 break;
167 case 'f':
168 check_file_flag |= FORCE_GEN;
169 break;
170 case '?':
171 default:
172 usage();
173 break;
174 }
175 }
176
177 argc -= optind;
178 argv += optind;
179 if (argc != 1)
180 usage();
181
182 if (termpath == NULL)
183 termpath = DEF_TERMPATH;
184
185 if (slave == NULL)
186 slave = DEF_SLAVE;
187
188 if (term == NULL)
189 term = DEF_TERM;
190
191 if (check_path == NULL)
192 check_path = getenv("CHECK_PATH");
193 if ((check_path == NULL) || (check_path[0] == '\0')) {
194 warnx("$CHECK_PATH not set, defaulting to %s", def_check_path);
195 check_path = def_check_path;
196 }
197
198 signal(SIGCHLD, slave_died);
199
200 if (setenv("TERM", term, 1) != 0)
201 err(2, "Failed to set TERM variable");
202
203 if (unsetenv("ESCDELAY") != 0)
204 err(2, "Failed to unset ESCDELAY variable");
205
206 if (stat(termpath, &st) == -1)
207 err(1, "Cannot stat %s", termpath);
208
209 if (S_ISDIR(st.st_mode)) {
210 char tinfo[MAXPATHLEN];
211 int l = snprintf(tinfo, sizeof(tinfo), "%s/%s", termpath,
212 "terminfo.cdb");
213 if (stat(tinfo, &st) == -1)
214 err(1, "Cannot stat `%s'", tinfo);
215 if (l >= 4)
216 tinfo[l - 4] = '\0';
217 if (setenv("TERMINFO", tinfo, 1) != 0)
218 err(1, "Failed to set TERMINFO variable");
219 } else {
220 int fd;
221 char *tinfo;
222 if ((fd = open(termpath, O_RDONLY)) == -1)
223 err(1, "Cannot open `%s'", termpath);
224 if ((tinfo = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_FILE,
225 fd, 0)) == MAP_FAILED)
226 err(1, "Cannot map `%s'", termpath);
227 if (setenv("TERMINFO", tinfo, 1) != 0)
228 err(1, "Failed to set TERMINFO variable");
229 close(fd);
230 munmap(tinfo, (size_t)st.st_size);
231 }
232
233 if (pipe(pipe_to_slave) < 0)
234 err(1, "Command pipe creation failed");
235 to_slave = pipe_to_slave[1];
236
237 if (pipe(pipe_from_slave) < 0)
238 err(1, "Slave pipe creation failed");
239 from_slave = pipe_from_slave[0];
240
241 /*
242 * Create default termios settings for later use
243 */
244 memset(&term_attr, 0, sizeof(term_attr));
245 term_attr.c_iflag = TTYDEF_IFLAG;
246 term_attr.c_oflag = TTYDEF_OFLAG;
247 term_attr.c_cflag = TTYDEF_CFLAG;
248 term_attr.c_lflag = TTYDEF_LFLAG;
249 cfsetspeed(&term_attr, TTYDEF_SPEED);
250 term_attr.c_cc[VERASE] = '\b';
251 term_attr.c_cc[VKILL] = '\025'; /* ^U */
252
253 if ((slave_pid = forkpty(&master, NULL, &term_attr, NULL)) < 0)
254 err(1, "Fork of pty for slave failed\n");
255
256 if (slave_pid == 0) {
257 /* slave side, just exec the slave process */
258 if (asprintf(&arg1, "%d", pipe_to_slave[0]) < 0)
259 err(1, "arg1 conversion failed");
260 close(pipe_to_slave[1]);
261
262 close(pipe_from_slave[0]);
263 if (asprintf(&arg2, "%d", pipe_from_slave[1]) < 0)
264 err(1, "arg2 conversion failed");
265
266 if (execl(slave, slave, arg1, arg2, (char *)0) < 0)
267 err(1, "Exec of slave %s failed", slave);
268
269 /* NOT REACHED */
270 }
271
272 (void)close(pipe_to_slave[0]);
273 (void)close(pipe_from_slave[1]);
274
275 fcntl(master, F_SETFL, O_NONBLOCK);
276
277 if ((yyin = fopen(argv[0], "r")) == NULL)
278 err(1, "Cannot open command file %s", argv[0]);
279
280 if ((cur_file = strdup(argv[0])) == NULL)
281 err(2, "Failed to alloc memory for test file name");
282
283 init_parse_variables(1);
284
285 yyparse();
286 fclose(yyin);
287
288 signal(SIGCHLD, SIG_DFL);
289 (void)close(to_slave);
290 (void)close(from_slave);
291
292 int status;
293 (void)waitpid(slave_pid, &status, 0);
294
295 exit(0);
296 }
297