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