2 * Copyright (c) 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #ifndef HAVE_SYS_TYPES_H
37 #include <sys/types.h>
39 #ifdef HAVE_SYS_WAIT_H
59 #endif /* STREAMPTY */
65 enum { CMD_EXPECT = 0, CMD_SEND, CMD_PASSWORD } type;
75 static struct command *commands, **next = &commands;
77 static sig_atomic_t alarmset = 0;
79 static int timeout = 10;
82 static int version_flag;
86 static char line[256] = { 0 };
89 caught_signal(int signo)
99 printf("implement open_pty\n");
102 #if defined(HAVE_OPENPTY) || defined(__linux) || defined(__osf__) /* XXX */
103 if(openpty(&master, &slave, line, 0, 0) == 0)
105 #endif /* HAVE_OPENPTY .... */
117 for(q = clone; *q; q++){
118 master = open(*q, O_RDWR);
126 strlcpy(line, ptsname(master), sizeof(line));
127 slave = open(line, O_RDWR);
129 errx(1, "failed to open slave when using %s", *q);
130 ioctl(slave, I_PUSH, "ptem");
131 ioctl(slave, I_PUSH, "ldterm");
137 #endif /* STREAMSPTY */
139 /* more cases, like open /dev/ptmx, etc */
149 iscmd(const char *buf, const char *s)
151 size_t len = strlen(s);
152 if (strncmp(buf, s, len) != 0)
154 return estrdup(buf + len);
158 parse_configuration(const char *fn)
163 unsigned int lineno = 0;
166 cmd = fopen(fn, "r");
168 err(1, "open: %s", fn);
170 while (fgets(s, sizeof(s), cmd) != NULL) {
172 s[strcspn(s, "#\n")] = '\0';
175 c = calloc(1, sizeof(*c));
183 if ((str = iscmd(s, "expect ")) != NULL) {
184 c->type = CMD_EXPECT;
186 } else if ((str = iscmd(s, "send ")) != NULL) {
189 } else if ((str = iscmd(s, "password ")) != NULL) {
190 c->type = CMD_PASSWORD;
193 errx(1, "Invalid command on line %d: %s", lineno, s);
205 eval_parent(pid_t pid)
212 for (c = commands; c != NULL; c = c->next) {
216 printf("[expecting %s]", c->str);
219 while((sret = read(master, &in, sizeof(in))) > 0) {
222 if (c->str[len] != in) {
227 if (c->str[len] == '\0')
231 if (alarmset == SIGALRM)
232 errx(1, "timeout waiting for %s (line %u)",
235 errx(1, "got a signal %d waiting for %s (line %u)",
236 alarmset, c->str, c->lineno);
238 errx(1, "end command while waiting for %s (line %u)",
244 const char *msg = (c->type == CMD_PASSWORD) ? "****" : c->str;
247 printf("[send %s]", msg);
249 len = strlen(c->str);
252 if (c->str[i] == '\\' && i < len - 1) {
256 case 'n': ctrl = '\n'; break;
257 case 'r': ctrl = '\r'; break;
258 case 't': ctrl = '\t'; break;
260 errx(1, "unknown control char %c (line %u)",
261 c->str[i], c->lineno);
263 if (net_write(master, &ctrl, 1) != 1)
264 errx(1, "command refused input (line %u)", c->lineno);
266 if (net_write(master, &c->str[i], 1) != 1)
267 errx(1, "command refused input (line %u)", c->lineno);
277 while(read(master, &in, sizeof(in)) > 0)
281 printf("[end of program]\n");
284 * Fetch status from child
289 ret = waitpid(pid, &status, 0);
292 if (WIFEXITED(status) && WEXITSTATUS(status))
293 return WEXITSTATUS(status);
294 else if (WIFSIGNALED(status)) {
295 printf("killed by signal: %d\n", WTERMSIG(status));
306 static struct getargs args[] = {
307 { "timeout", 't', arg_integer, &timeout, "timout", "seconds" },
308 { "verbose", 'v', arg_counter, &verbose, "verbose debugging" },
309 { "version", 0, arg_flag, &version_flag, "print version" },
310 { "help", 0, arg_flag, &help_flag, NULL }
316 arg_printusage (args, sizeof(args)/sizeof(*args), NULL, "infile command..");
321 main(int argc, char **argv)
326 setprogname(argv[0]);
328 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
335 fprintf (stderr, "%s from %s-%s\n", getprogname(), PACKAGE, VERSION);
345 parse_configuration(argv[0]);
354 err(1, "Failed to fork");
360 dup2(slave, STDIN_FILENO);
361 dup2(slave, STDOUT_FILENO);
362 dup2(slave, STDERR_FILENO);
363 closefrom(STDERR_FILENO + 1);
365 execvp(argv[0], argv); /* add NULL to end of array ? */
366 err(1, "Failed to exec: %s", argv[0]);
372 sa.sa_handler = caught_signal;
374 sigemptyset (&sa.sa_mask);
376 sigaction(SIGALRM, &sa, NULL);
379 return eval_parent(pid);