Home | History | Annotate | Line # | Download | only in director
director.c revision 1.13
      1 /*	$NetBSD: director.c,v 1.13 2021/02/07 13:44:22 rillig 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  * FIXME: Must not use stdio in a signal handler.  This leads to incomplete
     75  * output in verbose mode, truncating the useful part of the error message.
     76  */
     77 static void
     78 slave_died(int param)
     79 {
     80 	char last_words[256];
     81 	size_t count;
     82 
     83 	fprintf(stderr, "ERROR: Slave has exited\n");
     84 	if (saved_output.count > 0) {
     85 		fprintf(stderr, "output from slave: ");
     86 		for (count = 0; count < saved_output.count; count ++) {
     87 			if (isprint((unsigned char)saved_output.data[count]))
     88 			    fprintf(stderr, "%c", saved_output.data[count]);
     89 		}
     90 		fprintf(stderr, "\n");
     91 	}
     92 
     93 	if ((count = read(master, &last_words, 255)) > 0) {
     94 		last_words[count] = '\0';
     95 		fprintf(stderr, "slave exited with message \"%s\"\n",
     96 			last_words);
     97 	}
     98 
     99 	exit(2);
    100 }
    101 
    102 
    103 static void
    104 usage(void)
    105 {
    106 	fprintf(stderr, "Usage: %s [-vgf] [-I include-path] [-C check-path] "
    107 	    "[-T terminfo-file] [-s pathtoslave] [-t term] "
    108 	    "commandfile\n", getprogname());
    109 	fprintf(stderr, " where:\n");
    110 	fprintf(stderr, "    -v enables verbose test output\n");
    111 	fprintf(stderr, "    -g enables check file generation if does not exist\n");
    112 	fprintf(stderr, "    -f forces check file generation if -g flag is set\n");
    113 	fprintf(stderr, "    -T is a directory containing the terminfo.cdb "
    114 	    "file, or a file holding the terminfo description\n");
    115 	fprintf(stderr, "    -s is the path to the slave executable\n");
    116 	fprintf(stderr, "    -t is value to set TERM to for the test\n");
    117 	fprintf(stderr, "    -I is the directory to include files\n");
    118 	fprintf(stderr, "    -C is the directory for config files\n");
    119 	fprintf(stderr, "    commandfile is a file of test directives\n");
    120 	exit(1);
    121 }
    122 
    123 
    124 int
    125 main(int argc, char *argv[])
    126 {
    127 	extern char *optarg;
    128 	extern int optind;
    129 	const char *termpath, *term, *slave;
    130 	int ch;
    131 	pid_t slave_pid;
    132 	extern FILE *yyin;
    133 	char *arg1, *arg2, *arg3, *arg4;
    134 	struct termios term_attr;
    135 	struct stat st;
    136 
    137 	termpath = term = slave = NULL;
    138 	verbose = 0;
    139 	check_file_flag = 0;
    140 
    141 	while ((ch = getopt(argc, argv, "vgfC:I:p:s:t:T:")) != -1) {
    142 		switch(ch) {
    143 		case 'I':
    144 			include_path = optarg;
    145 			break;
    146 		case 'C':
    147 			check_path = optarg;
    148 			break;
    149 		case 'T':
    150 			termpath = optarg;
    151 			break;
    152 		case 'p':
    153 			termpath = optarg;
    154 			break;
    155 		case 's':
    156 			slave = optarg;
    157 			break;
    158 		case 't':
    159 			term = optarg;
    160 			break;
    161 		case 'v':
    162 			verbose = 1;
    163 			break;
    164 		case 'g':
    165 			check_file_flag |= GEN_CHECK_FILE;
    166 			break;
    167 		case 'f':
    168 			check_file_flag |= FORCE_GEN;
    169 			break;
    170 		case '?':
    171 		default:
    172 			usage();
    173 			break;
    174 		}
    175 	}
    176 
    177 	argc -= optind;
    178 	argv += optind;
    179 	if (argc < 1)
    180 		usage();
    181 
    182 	if (termpath == NULL)
    183 		termpath = DEF_TERMPATH;
    184 
    185 	if (slave == NULL)
    186 		slave = DEF_SLAVE;
    187 
    188 	if (term == NULL)
    189 		term = DEF_TERM;
    190 
    191 	if (check_path == NULL)
    192 		check_path = getenv("CHECK_PATH");
    193 	if ((check_path == NULL) || (check_path[0] == '\0')) {
    194 		warn("$CHECK_PATH not set, defaulting to %s", def_check_path);
    195 		check_path = def_check_path;
    196 	}
    197 
    198 	if (include_path == NULL)
    199 		include_path = getenv("INCLUDE_PATH");
    200 	if ((include_path == NULL) || (include_path[0] == '\0')) {
    201 		warn("$INCLUDE_PATH not set, defaulting to %s",
    202 			def_include_path);
    203 		include_path = def_include_path;
    204 	}
    205 
    206 	signal(SIGCHLD, slave_died);
    207 
    208 	if (setenv("TERM", term, 1) != 0)
    209 		err(2, "Failed to set TERM variable");
    210 
    211 	if (stat(termpath, &st) == -1)
    212 		err(1, "Cannot stat %s", termpath);
    213 
    214 	if (S_ISDIR(st.st_mode)) {
    215 		char tinfo[MAXPATHLEN];
    216 		int l = snprintf(tinfo, sizeof(tinfo), "%s/%s", termpath,
    217 		    "terminfo.cdb");
    218 		if (stat(tinfo, &st) == -1)
    219 			err(1, "Cannot stat `%s'", tinfo);
    220 		if (l >= 4)
    221 			tinfo[l - 4] = '\0';
    222 		if (setenv("TERMINFO", tinfo, 1) != 0)
    223 			err(1, "Failed to set TERMINFO variable");
    224 	} else {
    225 		int fd;
    226 		char *tinfo;
    227 		if ((fd = open(termpath, O_RDONLY)) == -1)
    228 			err(1, "Cannot open `%s'", termpath);
    229 		if ((tinfo = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_FILE,
    230 			fd, 0)) == MAP_FAILED)
    231 			err(1, "Cannot map `%s'", termpath);
    232 		if (setenv("TERMINFO", tinfo, 1) != 0)
    233 			err(1, "Failed to set TERMINFO variable");
    234 		close(fd);
    235 		munmap(tinfo, (size_t)st.st_size);
    236 	}
    237 
    238 	if (pipe(cmdpipe) < 0)
    239 		err(1, "Command pipe creation failed");
    240 
    241 	if (pipe(slvpipe) < 0)
    242 		err(1, "Slave pipe creation failed");
    243 
    244 	/*
    245 	 * Create default termios settings for later use
    246 	 */
    247 	memset(&term_attr, 0, sizeof(term_attr));
    248 	term_attr.c_iflag = TTYDEF_IFLAG;
    249 	term_attr.c_oflag = TTYDEF_OFLAG;
    250 	term_attr.c_cflag = TTYDEF_CFLAG;
    251 	term_attr.c_lflag = TTYDEF_LFLAG;
    252 	cfsetspeed(&term_attr, TTYDEF_SPEED);
    253 	term_attr.c_cc[VERASE] = '\b';
    254 	term_attr.c_cc[VKILL] = '\025'; /* ^U */
    255 
    256 	if ((slave_pid = forkpty(&master, NULL, &term_attr, NULL)) < 0)
    257 		err(1, "Fork of pty for slave failed\n");
    258 
    259 	if (slave_pid == 0) {
    260 		/* slave side, just exec the slave process */
    261 		if (asprintf(&arg1, "%d", cmdpipe[0]) < 0)
    262 			err(1, "arg1 conversion failed");
    263 
    264 		if (asprintf(&arg2, "%d", cmdpipe[1]) < 0)
    265 			err(1, "arg2 conversion failed");
    266 
    267 		if (asprintf(&arg3, "%d", slvpipe[0]) < 0)
    268 			err(1, "arg3 conversion failed");
    269 
    270 		if (asprintf(&arg4, "%d", slvpipe[1]) < 0)
    271 			err(1, "arg4 conversion failed");
    272 
    273 		if (execl(slave, slave, arg1, arg2, arg3, arg4, NULL) < 0)
    274 			err(1, "Exec of slave %s failed", slave);
    275 
    276 		/* NOT REACHED */
    277 	}
    278 
    279 	fcntl(master, F_SETFL, O_NONBLOCK);
    280 
    281 	if ((yyin = fopen(argv[0], "r")) == NULL)
    282 		err(1, "Cannot open command file %s", argv[0]);
    283 
    284 	if ((cur_file = strdup(argv[0])) == NULL)
    285 		err(2, "Failed to alloc memory for test file name");
    286 
    287 	init_parse_variables(1);
    288 
    289 	yyparse();
    290 	fclose(yyin);
    291 
    292 	exit(0);
    293 }
    294