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