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