director.c revision 1.4 1 1.4 christos /* $NetBSD: director.c,v 1.4 2011/05/15 23:56: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.1 blymn #include <fcntl.h>
33 1.1 blymn #include <unistd.h>
34 1.4 christos #include <ctype.h>
35 1.1 blymn #include <termios.h>
36 1.1 blymn #include <signal.h>
37 1.1 blymn #include <stdio.h>
38 1.1 blymn #include <stdlib.h>
39 1.1 blymn #include <string.h>
40 1.4 christos #include <util.h>
41 1.4 christos #include <err.h>
42 1.1 blymn #include "returns.h"
43 1.1 blymn
44 1.4 christos void yyparse(void);
45 1.1 blymn #define DEF_TERMPATH "."
46 1.1 blymn #define DEF_TERM "atf"
47 1.1 blymn #define DEF_SLAVE "./slave"
48 1.1 blymn
49 1.1 blymn char *def_check_path = "./"; /* default check path */
50 1.1 blymn char *def_include_path = "./"; /* default include path */
51 1.1 blymn
52 1.1 blymn extern size_t nvars; /* In testlang_conf.y */
53 1.1 blymn saved_data_t saved_output; /* In testlang_conf.y */
54 1.1 blymn int cmdpipe[2]; /* command pipe between director and slave */
55 1.1 blymn int slvpipe[2]; /* reply pipe back from slave */
56 1.1 blymn int master; /* pty to the slave */
57 1.1 blymn int verbose; /* control verbosity of tests */
58 1.1 blymn char *check_path; /* path to prepend to check files for output
59 1.1 blymn validation */
60 1.1 blymn char *include_path; /* path to prepend to include files */
61 1.1 blymn char *cur_file; /* name of file currently being read */
62 1.1 blymn
63 1.1 blymn void init_parse_variables(int); /* in testlang_parse.y */
64 1.1 blymn
65 1.1 blymn /*
66 1.1 blymn * Handle the slave exiting unexpectedly, try to recover the exit message
67 1.1 blymn * and print it out.
68 1.1 blymn */
69 1.1 blymn void
70 1.1 blymn slave_died(int param)
71 1.1 blymn {
72 1.1 blymn char last_words[256];
73 1.1 blymn int count;
74 1.1 blymn
75 1.1 blymn fprintf(stderr, "ERROR: Slave has exited\n");
76 1.1 blymn if (saved_output.count > 0) {
77 1.1 blymn fprintf(stderr, "output from slave: ");
78 1.1 blymn for (count = 0; count < saved_output.count; count ++) {
79 1.1 blymn if (isprint(saved_output.data[count]))
80 1.1 blymn fprintf(stderr, "%c", saved_output.data[count]);
81 1.1 blymn }
82 1.1 blymn fprintf(stderr, "\n");
83 1.1 blymn }
84 1.1 blymn
85 1.1 blymn if ((count = read(master, &last_words, 255)) > 0) {
86 1.1 blymn last_words[count] = '\0';
87 1.1 blymn fprintf(stderr, "slave exited with message \"%s\"\n",
88 1.1 blymn last_words);
89 1.1 blymn }
90 1.1 blymn
91 1.1 blymn exit(2);
92 1.1 blymn }
93 1.1 blymn
94 1.1 blymn
95 1.1 blymn static void
96 1.1 blymn usage(char *name)
97 1.1 blymn {
98 1.1 blymn fprintf(stderr, "Curses automated test director\n");
99 1.1 blymn fprintf(stderr, "%s [-v] [-p termcappath] [-s pathtoslave] [-t term]"
100 1.1 blymn " commandfile\n", name);
101 1.1 blymn fprintf(stderr, " where:\n");
102 1.1 blymn fprintf(stderr, " -v enables verbose test output\n");
103 1.1 blymn fprintf(stderr, " termcappath is the path to the directory"
104 1.1 blymn "holding the termpcap file\n");
105 1.1 blymn fprintf(stderr, " pathtoslave is the path to the slave exectuable\n");
106 1.1 blymn fprintf(stderr, " term is value to set TERM to for the test\n");
107 1.1 blymn fprintf(stderr, " commandfile is a file of test directives\n");
108 1.1 blymn exit(2);
109 1.1 blymn }
110 1.1 blymn
111 1.1 blymn
112 1.1 blymn int
113 1.1 blymn main(int argc, char *argv[])
114 1.1 blymn {
115 1.1 blymn extern char *optarg;
116 1.1 blymn extern int optind;
117 1.2 joerg char *termpath, *term, *slave;
118 1.2 joerg int ch;
119 1.1 blymn pid_t slave_pid;
120 1.1 blymn extern FILE *yyin;
121 1.1 blymn char *arg1, *arg2, *arg3, *arg4;
122 1.1 blymn struct termios term_attr;
123 1.1 blymn int slavefd, on;
124 1.1 blymn
125 1.1 blymn termpath = term = slave = NULL;
126 1.1 blymn verbose = 0;
127 1.1 blymn
128 1.1 blymn while ((ch = getopt(argc, argv, "vp:s:t:")) != -1) {
129 1.1 blymn switch(ch) {
130 1.1 blymn case 'p':
131 1.1 blymn asprintf(&termpath, "%s", optarg);
132 1.1 blymn break;
133 1.1 blymn case 's':
134 1.1 blymn asprintf(&slave, "%s", optarg);
135 1.1 blymn break;
136 1.1 blymn case 't':
137 1.1 blymn asprintf(&term, "%s", optarg);
138 1.1 blymn break;
139 1.1 blymn case 'v':
140 1.1 blymn verbose = 1;
141 1.1 blymn break;
142 1.1 blymn case '?':
143 1.1 blymn default:
144 1.1 blymn usage(argv[0]);
145 1.1 blymn break;
146 1.1 blymn }
147 1.1 blymn }
148 1.1 blymn
149 1.1 blymn if (termpath == NULL)
150 1.1 blymn asprintf(&termpath, "%s", DEF_TERMPATH);
151 1.1 blymn
152 1.1 blymn if (slave == NULL)
153 1.1 blymn asprintf(&slave, "%s", DEF_SLAVE);
154 1.1 blymn
155 1.1 blymn if (term == NULL)
156 1.1 blymn asprintf(&term, "%s", DEF_TERM);
157 1.1 blymn
158 1.1 blymn argc -= optind;
159 1.1 blymn if (argc < 1)
160 1.1 blymn usage(argv[0]);
161 1.1 blymn
162 1.1 blymn signal(SIGCHLD, slave_died);
163 1.1 blymn
164 1.1 blymn argv += optind;
165 1.1 blymn
166 1.1 blymn if (setenv("TERM", term, 1) != 0)
167 1.1 blymn err(2, "Failed to set TERM variable");
168 1.1 blymn
169 1.1 blymn check_path = getenv("CHECK_PATH");
170 1.1 blymn if ((check_path == NULL) || (check_path[0] == '\0')) {
171 1.1 blymn fprintf(stderr,
172 1.1 blymn "WARNING: CHECK_PATH not set, defaulting to %s\n",
173 1.1 blymn def_check_path);
174 1.1 blymn check_path = def_check_path;
175 1.1 blymn }
176 1.1 blymn
177 1.1 blymn include_path = getenv("INCLUDE_PATH");
178 1.1 blymn if ((include_path == NULL) || (include_path[0] == '\0')) {
179 1.1 blymn fprintf(stderr,
180 1.1 blymn "WARNING: INCLUDE_PATH not set, defaulting to %s\n",
181 1.1 blymn def_include_path);
182 1.1 blymn include_path = def_include_path;
183 1.1 blymn }
184 1.1 blymn
185 1.1 blymn if (pipe(cmdpipe) < 0) {
186 1.1 blymn fprintf(stderr, "Command pipe creation failed: ");
187 1.1 blymn perror(NULL);
188 1.1 blymn exit(2);
189 1.1 blymn }
190 1.1 blymn
191 1.1 blymn if (pipe(slvpipe) < 0) {
192 1.1 blymn fprintf(stderr, "Slave pipe creation failed: ");
193 1.1 blymn perror(NULL);
194 1.1 blymn exit(2);
195 1.1 blymn }
196 1.1 blymn
197 1.3 martin /*
198 1.3 martin * Create default termios settings for later use
199 1.3 martin */
200 1.3 martin memset(&term_attr, 0, sizeof(term_attr));
201 1.3 martin term_attr.c_iflag = TTYDEF_IFLAG;
202 1.3 martin term_attr.c_oflag = TTYDEF_OFLAG;
203 1.3 martin term_attr.c_cflag = TTYDEF_CFLAG;
204 1.3 martin term_attr.c_lflag = TTYDEF_LFLAG;
205 1.3 martin cfsetspeed(&term_attr, TTYDEF_SPEED);
206 1.1 blymn
207 1.1 blymn if ((slave_pid = forkpty(&master, NULL, &term_attr, NULL)) < 0) {
208 1.1 blymn fprintf(stderr, "Fork of pty for slave failed\n");
209 1.1 blymn exit(2);
210 1.1 blymn }
211 1.1 blymn
212 1.1 blymn if (slave_pid == 0) {
213 1.1 blymn /* slave side, just exec the slave process */
214 1.1 blymn if (asprintf(&arg1, "%d", cmdpipe[0]) < 0)
215 1.1 blymn err(1, "arg1 conversion failed");
216 1.1 blymn
217 1.1 blymn if (asprintf(&arg2, "%d", cmdpipe[1]) < 0)
218 1.1 blymn err(1, "arg2 conversion failed");
219 1.1 blymn
220 1.1 blymn if (asprintf(&arg3, "%d", slvpipe[0]) < 0)
221 1.1 blymn err(1, "arg3 conversion failed");
222 1.1 blymn
223 1.1 blymn if (asprintf(&arg4, "%d", slvpipe[1]) < 0)
224 1.1 blymn err(1, "arg4 conversion failed");
225 1.1 blymn
226 1.1 blymn if (execl(slave, slave, arg1, arg2, arg3, arg4, NULL) < 0) {
227 1.1 blymn fprintf(stderr, "Exec of slave %s failed: ", slave);
228 1.1 blymn perror(NULL);
229 1.1 blymn exit(2);
230 1.1 blymn }
231 1.1 blymn
232 1.1 blymn /* NOT REACHED */
233 1.1 blymn }
234 1.1 blymn
235 1.1 blymn fcntl(master, F_SETFL, O_NONBLOCK);
236 1.1 blymn
237 1.1 blymn if ((yyin = fopen(argv[0], "r")) == NULL) {
238 1.1 blymn fprintf(stderr, "Cannot open command file %s: ", argv[0]);
239 1.1 blymn perror(NULL);
240 1.1 blymn exit(2);
241 1.1 blymn }
242 1.1 blymn
243 1.1 blymn if ((cur_file = malloc(strlen(argv[0]) + 1)) == NULL)
244 1.1 blymn err(2, "Failed to alloc memory for test file name");
245 1.1 blymn
246 1.1 blymn strlcpy(cur_file, argv[0], strlen(argv[0]) + 1);
247 1.1 blymn
248 1.1 blymn init_parse_variables(1);
249 1.1 blymn
250 1.1 blymn yyparse();
251 1.1 blymn fclose(yyin);
252 1.1 blymn
253 1.1 blymn exit(0);
254 1.1 blymn }
255