1 /* $NetBSD: director.c,v 1.10 2012/06/03 23:19:11 joerg Exp $ */
4 * Copyright 2009 Brett Lymn <blymn@NetBSD.org>
8 * This code has been donated to The NetBSD Foundation by the Author.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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.
32 #include <sys/param.h>
48 #define DEF_TERMPATH "."
49 #define DEF_TERM "atf"
50 #define DEF_SLAVE "./slave"
52 const char *def_check_path = "./"; /* default check path */
53 const char *def_include_path = "./"; /* default include path */
55 extern size_t nvars; /* In testlang_conf.y */
56 saved_data_t saved_output; /* In testlang_conf.y */
57 int cmdpipe[2]; /* command pipe between director and slave */
58 int slvpipe[2]; /* reply pipe back from slave */
59 int master; /* pty to the slave */
60 int verbose; /* control verbosity of tests */
61 const char *check_path; /* path to prepend to check files for output
63 const char *include_path; /* path to prepend to include files */
64 char *cur_file; /* name of file currently being read */
66 void init_parse_variables(int); /* in testlang_parse.y */
69 * Handle the slave exiting unexpectedly, try to recover the exit message
78 fprintf(stderr, "ERROR: Slave has exited\n");
79 if (saved_output.count > 0) {
80 fprintf(stderr, "output from slave: ");
81 for (count = 0; count < saved_output.count; count ++) {
82 if (isprint((unsigned char)saved_output.data[count]))
83 fprintf(stderr, "%c", saved_output.data[count]);
85 fprintf(stderr, "\n");
88 if ((count = read(master, &last_words, 255)) > 0) {
89 last_words[count] = '\0';
90 fprintf(stderr, "slave exited with message \"%s\"\n",
101 fprintf(stderr, "Usage: %s [-v] [-I include-path] [-C check-path] "
102 "[-T terminfo-file] [-s pathtoslave] [-t term] "
103 "commandfile\n", getprogname());
104 fprintf(stderr, " where:\n");
105 fprintf(stderr, " -v enables verbose test output\n");
106 fprintf(stderr, " -T is a directory containing the terminfo.cdb "
107 "file, or a file holding the terminfo description n");
108 fprintf(stderr, " -s is the path to the slave executable\n");
109 fprintf(stderr, " -t is value to set TERM to for the test\n");
110 fprintf(stderr, " -I is the directory to include files\n");
111 fprintf(stderr, " -C is the directory for config files\n");
112 fprintf(stderr, " commandfile is a file of test directives\n");
118 main(int argc, char *argv[])
122 const char *termpath, *term, *slave;
126 char *arg1, *arg2, *arg3, *arg4;
127 struct termios term_attr;
130 termpath = term = slave = NULL;
133 while ((ch = getopt(argc, argv, "vC:I:p:s:t:T:")) != -1) {
136 include_path = optarg;
168 if (termpath == NULL)
169 termpath = DEF_TERMPATH;
177 if (check_path == NULL)
178 check_path = getenv("CHECK_PATH");
179 if ((check_path == NULL) || (check_path[0] == '\0')) {
180 warn("$CHECK_PATH not set, defaulting to %s", def_check_path);
181 check_path = def_check_path;
184 if (include_path == NULL)
185 include_path = getenv("INCLUDE_PATH");
186 if ((include_path == NULL) || (include_path[0] == '\0')) {
187 warn("$INCLUDE_PATH not set, defaulting to %s",
189 include_path = def_include_path;
192 signal(SIGCHLD, slave_died);
194 if (setenv("TERM", term, 1) != 0)
195 err(2, "Failed to set TERM variable");
197 if (stat(termpath, &st) == -1)
198 err(1, "Cannot stat %s", termpath);
200 if (S_ISDIR(st.st_mode)) {
201 char tinfo[MAXPATHLEN];
202 int l = snprintf(tinfo, sizeof(tinfo), "%s/%s", termpath,
204 if (stat(tinfo, &st) == -1)
205 err(1, "Cannot stat `%s'", tinfo);
208 if (setenv("TERMINFO", tinfo, 1) != 0)
209 err(1, "Failed to set TERMINFO variable");
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");
221 munmap(tinfo, (size_t)st.st_size);
224 if (pipe(cmdpipe) < 0)
225 err(1, "Command pipe creation failed");
227 if (pipe(slvpipe) < 0)
228 err(1, "Slave pipe creation failed");
231 * Create default termios settings for later use
233 memset(&term_attr, 0, sizeof(term_attr));
234 term_attr.c_iflag = TTYDEF_IFLAG;
235 term_attr.c_oflag = TTYDEF_OFLAG;
236 term_attr.c_cflag = TTYDEF_CFLAG;
237 term_attr.c_lflag = TTYDEF_LFLAG;
238 cfsetspeed(&term_attr, TTYDEF_SPEED);
239 term_attr.c_cc[VERASE] = '\b';
240 term_attr.c_cc[VKILL] = '\025'; /* ^U */
242 if ((slave_pid = forkpty(&master, NULL, &term_attr, NULL)) < 0)
243 err(1, "Fork of pty for slave failed\n");
245 if (slave_pid == 0) {
246 /* slave side, just exec the slave process */
247 if (asprintf(&arg1, "%d", cmdpipe[0]) < 0)
248 err(1, "arg1 conversion failed");
250 if (asprintf(&arg2, "%d", cmdpipe[1]) < 0)
251 err(1, "arg2 conversion failed");
253 if (asprintf(&arg3, "%d", slvpipe[0]) < 0)
254 err(1, "arg3 conversion failed");
256 if (asprintf(&arg4, "%d", slvpipe[1]) < 0)
257 err(1, "arg4 conversion failed");
259 if (execl(slave, slave, arg1, arg2, arg3, arg4, NULL) < 0)
260 err(1, "Exec of slave %s failed", slave);
265 fcntl(master, F_SETFL, O_NONBLOCK);
267 if ((yyin = fopen(argv[0], "r")) == NULL)
268 err(1, "Cannot open command file %s", argv[0]);
270 if ((cur_file = strdup(argv[0])) == NULL)
271 err(2, "Failed to alloc memory for test file name");
273 init_parse_variables(1);