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