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