Home | History | Annotate | Line # | Download | only in director
director.c revision 1.1
      1 /*	$NetBSD: director.c,v 1.1 2011/04/10 09:55:09 blymn 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, ch;
    114 	pid_t slave_pid;
    115 	extern FILE *yyin;
    116 	char *arg1, *arg2, *arg3, *arg4;
    117 	struct termios term_attr;
    118 	int slavefd, on;
    119 
    120 	termpath = term = slave = NULL;
    121 	verbose = 0;
    122 
    123 	while ((ch = getopt(argc, argv, "vp:s:t:")) != -1) {
    124 		switch(ch) {
    125 		case 'p':
    126 			asprintf(&termpath, "%s", optarg);
    127 			break;
    128 		case 's':
    129 			asprintf(&slave, "%s", optarg);
    130 			break;
    131 		case 't':
    132 			asprintf(&term, "%s", optarg);
    133 			break;
    134 		case 'v':
    135 			verbose = 1;
    136 			break;
    137 		case '?':
    138 		default:
    139 			usage(argv[0]);
    140 			break;
    141 		}
    142 	}
    143 
    144 	if (termpath == NULL)
    145 		asprintf(&termpath, "%s", DEF_TERMPATH);
    146 
    147 	if (slave == NULL)
    148 		asprintf(&slave, "%s", DEF_SLAVE);
    149 
    150 	if (term == NULL)
    151 		asprintf(&term, "%s", DEF_TERM);
    152 
    153 	argc -= optind;
    154 	if (argc < 1)
    155 		usage(argv[0]);
    156 
    157 	signal(SIGCHLD, slave_died);
    158 
    159 	argv += optind;
    160 
    161 	if (setenv("TERM", term, 1) != 0)
    162 		err(2, "Failed to set TERM variable");
    163 
    164 	check_path = getenv("CHECK_PATH");
    165 	if ((check_path == NULL) || (check_path[0] == '\0')) {
    166 		fprintf(stderr,
    167 			"WARNING: CHECK_PATH not set, defaulting to %s\n",
    168 			def_check_path);
    169 		check_path = def_check_path;
    170 	}
    171 
    172 	include_path = getenv("INCLUDE_PATH");
    173 	if ((include_path == NULL) || (include_path[0] == '\0')) {
    174 		fprintf(stderr,
    175 			"WARNING: INCLUDE_PATH not set, defaulting to %s\n",
    176 			def_include_path);
    177 		include_path = def_include_path;
    178 	}
    179 
    180 	if (pipe(cmdpipe) < 0) {
    181 		fprintf(stderr, "Command pipe creation failed: ");
    182 		perror(NULL);
    183 		exit(2);
    184 	}
    185 
    186 	if (pipe(slvpipe) < 0) {
    187 		fprintf(stderr, "Slave pipe creation failed: ");
    188 		perror(NULL);
    189 		exit(2);
    190 	}
    191 
    192 	if (tcgetattr(0, &term_attr) < 0)
    193 		err(2, "Failed to get term attributes");
    194 
    195 	if ((slave_pid = forkpty(&master, NULL, &term_attr, NULL)) < 0) {
    196 		fprintf(stderr, "Fork of pty for slave failed\n");
    197 		exit(2);
    198 	}
    199 
    200 	if (slave_pid == 0) {
    201 		/* slave side, just exec the slave process */
    202 		if (asprintf(&arg1, "%d", cmdpipe[0]) < 0)
    203 			err(1, "arg1 conversion failed");
    204 
    205 		if (asprintf(&arg2, "%d", cmdpipe[1]) < 0)
    206 			err(1, "arg2 conversion failed");
    207 
    208 		if (asprintf(&arg3, "%d", slvpipe[0]) < 0)
    209 			err(1, "arg3 conversion failed");
    210 
    211 		if (asprintf(&arg4, "%d", slvpipe[1]) < 0)
    212 			err(1, "arg4 conversion failed");
    213 
    214 		if (execl(slave, slave, arg1, arg2, arg3, arg4, NULL) < 0) {
    215 			fprintf(stderr, "Exec of slave %s failed: ", slave);
    216 			perror(NULL);
    217 			exit(2);
    218 		}
    219 
    220 		/* NOT REACHED */
    221 	}
    222 
    223 	fcntl(master, F_SETFL, O_NONBLOCK);
    224 
    225 	if ((yyin = fopen(argv[0], "r")) == NULL) {
    226 		fprintf(stderr, "Cannot open command file %s: ", argv[0]);
    227 		perror(NULL);
    228 		exit(2);
    229 	}
    230 
    231 	if ((cur_file = malloc(strlen(argv[0]) + 1)) == NULL)
    232 		err(2, "Failed to alloc memory for test file name");
    233 
    234 	strlcpy(cur_file, argv[0], strlen(argv[0]) + 1);
    235 
    236 	init_parse_variables(1);
    237 
    238 	yyparse();
    239 	fclose(yyin);
    240 
    241 	exit(0);
    242 }
    243