director.c revision 1.11 1 /* $NetBSD: director.c,v 1.11 2020/10/24 04:46:17 blymn 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 withough 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 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 if (isprint((unsigned char)saved_output.data[count]))
85 fprintf(stderr, "%c", saved_output.data[count]);
86 }
87 fprintf(stderr, "\n");
88 }
89
90 if ((count = read(master, &last_words, 255)) > 0) {
91 last_words[count] = '\0';
92 fprintf(stderr, "slave exited with message \"%s\"\n",
93 last_words);
94 }
95
96 exit(2);
97 }
98
99
100 static void
101 usage(void)
102 {
103 fprintf(stderr, "Usage: %s [-vgf] [-I include-path] [-C check-path] "
104 "[-T terminfo-file] [-s pathtoslave] [-t term] "
105 "commandfile\n", getprogname());
106 fprintf(stderr, " where:\n");
107 fprintf(stderr, " -v enables verbose test output\n");
108 fprintf(stderr, " -g enables check file generation if does not exist\n");
109 fprintf(stderr, " -f forces check file generation if -g flag is set\n");
110 fprintf(stderr, " -T is a directory containing the terminfo.cdb "
111 "file, or a file holding the terminfo description n");
112 fprintf(stderr, " -s is the path to the slave executable\n");
113 fprintf(stderr, " -t is value to set TERM to for the test\n");
114 fprintf(stderr, " -I is the directory to include files\n");
115 fprintf(stderr, " -C is the directory for config files\n");
116 fprintf(stderr, " commandfile is a file of test directives\n");
117 exit(1);
118 }
119
120
121 int
122 main(int argc, char *argv[])
123 {
124 extern char *optarg;
125 extern int optind;
126 const char *termpath, *term, *slave;
127 int ch;
128 pid_t slave_pid;
129 extern FILE *yyin;
130 char *arg1, *arg2, *arg3, *arg4;
131 struct termios term_attr;
132 struct stat st;
133
134 termpath = term = slave = NULL;
135 verbose = 0;
136 check_file_flag = 0;
137
138 while ((ch = getopt(argc, argv, "vgfC:I:p:s:t:T:")) != -1) {
139 switch(ch) {
140 case 'I':
141 include_path = optarg;
142 break;
143 case 'C':
144 check_path = optarg;
145 break;
146 case 'T':
147 termpath = optarg;
148 break;
149 case 'p':
150 termpath = optarg;
151 break;
152 case 's':
153 slave = optarg;
154 break;
155 case 't':
156 term = optarg;
157 break;
158 case 'v':
159 verbose = 1;
160 break;
161 case 'g':
162 check_file_flag |= GEN_CHECK_FILE;
163 break;
164 case 'f':
165 check_file_flag |= FORCE_GEN;
166 break;
167 case '?':
168 default:
169 usage();
170 break;
171 }
172 }
173
174 argc -= optind;
175 argv += optind;
176 if (argc < 1)
177 usage();
178
179 if (termpath == NULL)
180 termpath = DEF_TERMPATH;
181
182 if (slave == NULL)
183 slave = DEF_SLAVE;
184
185 if (term == NULL)
186 term = DEF_TERM;
187
188 if (check_path == NULL)
189 check_path = getenv("CHECK_PATH");
190 if ((check_path == NULL) || (check_path[0] == '\0')) {
191 warn("$CHECK_PATH not set, defaulting to %s", def_check_path);
192 check_path = def_check_path;
193 }
194
195 if (include_path == NULL)
196 include_path = getenv("INCLUDE_PATH");
197 if ((include_path == NULL) || (include_path[0] == '\0')) {
198 warn("$INCLUDE_PATH not set, defaulting to %s",
199 def_include_path);
200 include_path = def_include_path;
201 }
202
203 signal(SIGCHLD, slave_died);
204
205 if (setenv("TERM", term, 1) != 0)
206 err(2, "Failed to set TERM variable");
207
208 if (stat(termpath, &st) == -1)
209 err(1, "Cannot stat %s", termpath);
210
211 if (S_ISDIR(st.st_mode)) {
212 char tinfo[MAXPATHLEN];
213 int l = snprintf(tinfo, sizeof(tinfo), "%s/%s", termpath,
214 "terminfo.cdb");
215 if (stat(tinfo, &st) == -1)
216 err(1, "Cannot stat `%s'", tinfo);
217 if (l >= 4)
218 tinfo[l - 4] = '\0';
219 if (setenv("TERMINFO", tinfo, 1) != 0)
220 err(1, "Failed to set TERMINFO variable");
221 } else {
222 int fd;
223 char *tinfo;
224 if ((fd = open(termpath, O_RDONLY)) == -1)
225 err(1, "Cannot open `%s'", termpath);
226 if ((tinfo = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_FILE,
227 fd, 0)) == MAP_FAILED)
228 err(1, "Cannot map `%s'", termpath);
229 if (setenv("TERMINFO", tinfo, 1) != 0)
230 err(1, "Failed to set TERMINFO variable");
231 close(fd);
232 munmap(tinfo, (size_t)st.st_size);
233 }
234
235 if (pipe(cmdpipe) < 0)
236 err(1, "Command pipe creation failed");
237
238 if (pipe(slvpipe) < 0)
239 err(1, "Slave pipe creation failed");
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", cmdpipe[0]) < 0)
259 err(1, "arg1 conversion failed");
260
261 if (asprintf(&arg2, "%d", cmdpipe[1]) < 0)
262 err(1, "arg2 conversion failed");
263
264 if (asprintf(&arg3, "%d", slvpipe[0]) < 0)
265 err(1, "arg3 conversion failed");
266
267 if (asprintf(&arg4, "%d", slvpipe[1]) < 0)
268 err(1, "arg4 conversion failed");
269
270 if (execl(slave, slave, arg1, arg2, arg3, arg4, NULL) < 0)
271 err(1, "Exec of slave %s failed", slave);
272
273 /* NOT REACHED */
274 }
275
276 fcntl(master, F_SETFL, O_NONBLOCK);
277
278 if ((yyin = fopen(argv[0], "r")) == NULL)
279 err(1, "Cannot open command file %s", argv[0]);
280
281 if ((cur_file = strdup(argv[0])) == NULL)
282 err(2, "Failed to alloc memory for test file name");
283
284 init_parse_variables(1);
285
286 yyparse();
287 fclose(yyin);
288
289 exit(0);
290 }
291