director.c revision 1.18 1 /* $NetBSD: director.c,v 1.18 2021/02/09 20:42:31 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 cmdpipe[2]; /* command pipe between director and slave */
59 int slvpipe[2]; /* reply pipe back from slave */
60 int master; /* pty to the slave */
61 int verbose; /* control verbosity of tests */
62 int check_file_flag; /* control checkfile 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 enables check file generation if does not exist\n");
115 fprintf(stderr, " -f forces check file generation if -g flag is set\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 config 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, *arg3, *arg4;
137 struct termios term_attr;
138 struct stat st;
139
140 termpath = term = slave = NULL;
141 verbose = 0;
142 check_file_flag = 0;
143
144 while ((ch = getopt(argc, argv, "vgfC:I:p:s:t:T:")) != -1) {
145 switch (ch) {
146 case 'I':
147 include_path = optarg;
148 break;
149 case 'C':
150 check_path = optarg;
151 break;
152 case 'T':
153 termpath = optarg;
154 break;
155 case 'p':
156 termpath = optarg;
157 break;
158 case 's':
159 slave = optarg;
160 break;
161 case 't':
162 term = optarg;
163 break;
164 case 'v':
165 verbose = 1;
166 break;
167 case 'g':
168 check_file_flag |= GEN_CHECK_FILE;
169 break;
170 case 'f':
171 check_file_flag |= FORCE_GEN;
172 break;
173 case '?':
174 default:
175 usage();
176 break;
177 }
178 }
179
180 argc -= optind;
181 argv += optind;
182 if (argc != 1)
183 usage();
184
185 if (termpath == NULL)
186 termpath = DEF_TERMPATH;
187
188 if (slave == NULL)
189 slave = DEF_SLAVE;
190
191 if (term == NULL)
192 term = DEF_TERM;
193
194 if (check_path == NULL)
195 check_path = getenv("CHECK_PATH");
196 if ((check_path == NULL) || (check_path[0] == '\0')) {
197 warn("$CHECK_PATH not set, defaulting to %s", def_check_path);
198 check_path = def_check_path;
199 }
200
201 if (include_path == NULL)
202 include_path = getenv("INCLUDE_PATH");
203 if ((include_path == NULL) || (include_path[0] == '\0')) {
204 warn("$INCLUDE_PATH not set, defaulting to %s",
205 def_include_path);
206 include_path = def_include_path;
207 }
208
209 signal(SIGCHLD, slave_died);
210
211 if (setenv("TERM", term, 1) != 0)
212 err(2, "Failed to set TERM variable");
213
214 if (stat(termpath, &st) == -1)
215 err(1, "Cannot stat %s", termpath);
216
217 if (S_ISDIR(st.st_mode)) {
218 char tinfo[MAXPATHLEN];
219 int l = snprintf(tinfo, sizeof(tinfo), "%s/%s", termpath,
220 "terminfo.cdb");
221 if (stat(tinfo, &st) == -1)
222 err(1, "Cannot stat `%s'", tinfo);
223 if (l >= 4)
224 tinfo[l - 4] = '\0';
225 if (setenv("TERMINFO", tinfo, 1) != 0)
226 err(1, "Failed to set TERMINFO variable");
227 } else {
228 int fd;
229 char *tinfo;
230 if ((fd = open(termpath, O_RDONLY)) == -1)
231 err(1, "Cannot open `%s'", termpath);
232 if ((tinfo = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_FILE,
233 fd, 0)) == MAP_FAILED)
234 err(1, "Cannot map `%s'", termpath);
235 if (setenv("TERMINFO", tinfo, 1) != 0)
236 err(1, "Failed to set TERMINFO variable");
237 close(fd);
238 munmap(tinfo, (size_t)st.st_size);
239 }
240
241 if (pipe(cmdpipe) < 0)
242 err(1, "Command pipe creation failed");
243
244 if (pipe(slvpipe) < 0)
245 err(1, "Slave pipe creation failed");
246
247 /*
248 * Create default termios settings for later use
249 */
250 memset(&term_attr, 0, sizeof(term_attr));
251 term_attr.c_iflag = TTYDEF_IFLAG;
252 term_attr.c_oflag = TTYDEF_OFLAG;
253 term_attr.c_cflag = TTYDEF_CFLAG;
254 term_attr.c_lflag = TTYDEF_LFLAG;
255 cfsetspeed(&term_attr, TTYDEF_SPEED);
256 term_attr.c_cc[VERASE] = '\b';
257 term_attr.c_cc[VKILL] = '\025'; /* ^U */
258
259 if ((slave_pid = forkpty(&master, NULL, &term_attr, NULL)) < 0)
260 err(1, "Fork of pty for slave failed\n");
261
262 if (slave_pid == 0) {
263 /* slave side, just exec the slave process */
264 if (asprintf(&arg1, "%d", cmdpipe[0]) < 0)
265 err(1, "arg1 conversion failed");
266
267 if (asprintf(&arg2, "%d", cmdpipe[1]) < 0)
268 err(1, "arg2 conversion failed");
269
270 if (asprintf(&arg3, "%d", slvpipe[0]) < 0)
271 err(1, "arg3 conversion failed");
272
273 if (asprintf(&arg4, "%d", slvpipe[1]) < 0)
274 err(1, "arg4 conversion failed");
275
276 if (execl(slave, slave, arg1, arg2, arg3, arg4, (char *)0) < 0)
277 err(1, "Exec of slave %s failed", slave);
278
279 /* NOT REACHED */
280 }
281
282 fcntl(master, F_SETFL, O_NONBLOCK);
283
284 if ((yyin = fopen(argv[0], "r")) == NULL)
285 err(1, "Cannot open command file %s", argv[0]);
286
287 if ((cur_file = strdup(argv[0])) == NULL)
288 err(2, "Failed to alloc memory for test file name");
289
290 init_parse_variables(1);
291
292 yyparse();
293 fclose(yyin);
294
295 exit(0);
296 }
297