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