]> diplodocus.org Git - nmh/blob - test/runpty.c
ef5cf822f0da6932dd55401ee2e7dd542342fb6a
[nmh] / test / runpty.c
1 /*
2 * runpty.c - Run a process under a pseudo-tty
3 *
4 * This code is Copyright (c) 2017, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdarg.h>
13 #include <fcntl.h>
14 #include <termios.h>
15 #include <unistd.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <sys/select.h>
19 #include <time.h>
20 #include <errno.h>
21
22 #define COMMAND_TIMEOUT 30
23
24 static int open_master_pty(const char *desc);
25 static void die(const char *fmt, ...);
26
27 int
28 main(int argc, char *argv[])
29 {
30 int master_in, master_out, slave, cc, status;
31 time_t starttime, now;
32 const char *slavename;
33 pid_t child;
34 unsigned char readbuf[1024];
35 FILE *output;
36
37 if (argc < 3) {
38 die("%s: too few arguments\n"
39 "usage: %s output-filename command [arguments...]\n",
40 *argv, *argv);
41 }
42
43 master_in = open_master_pty("input");
44 master_out = open_master_pty("output");
45
46 child = fork();
47
48 /*
49 * Start the child process if we are in the child; open the two
50 * slave pseudo-ttys and close the masters after we are done with them.
51 */
52
53 if (child == 0) {
54 if (!(slavename = ptsname(master_in))) {
55 die("Unable to determine name of slave pty: %s\n",
56 strerror(errno));
57 }
58
59 if ((slave = open(slavename, O_RDWR)) < 0) {
60 die("Unable to open slave pty \"%s\": %s\n", slavename,
61 strerror(errno));
62 }
63
64 dup2(slave, STDIN_FILENO);
65 close(slave);
66 close(master_in);
67
68 if (!(slavename = ptsname(master_out))) {
69 die("Unable to determine name of slave pty: %s\n",
70 strerror(errno));
71 }
72
73 if ((slave = open(slavename, O_RDWR | O_NOCTTY)) < 0) {
74 die("Unable to open slave pty \"%s\": %s\n", slavename,
75 strerror(errno));
76 }
77
78 dup2(slave, STDOUT_FILENO);
79 dup2(slave, STDERR_FILENO);
80 close(slave);
81 close(master_out);
82
83 execvp(argv[2], argv + 2);
84
85 die("execvp(%s) failed: %s\n", argv[2], strerror(errno));
86
87 } else if (child < 0) {
88 die("fork() failed: %s\n", strerror(errno));
89 }
90
91 if (!(output = fopen(argv[1], "w"))) {
92 die("Unable to open \"%s\" for output: %s\n", argv[1],
93 strerror(errno));
94 }
95
96 starttime = time(NULL);
97
98 for (;;) {
99 fd_set readfds;
100 struct timeval tv;
101
102 FD_ZERO(&readfds);
103
104 FD_SET(master_out, &readfds);
105
106 /*
107 * After we get our first bit of data, close the master pty
108 * connected to standard input on our slave; that will generate
109 * an EOF.
110 */
111
112 tv.tv_sec = starttime + COMMAND_TIMEOUT - time(NULL);
113 if (tv.tv_sec < 0)
114 tv.tv_sec = 0;
115 tv.tv_usec = 0;
116
117 cc = select(master_out + 1, &readfds, NULL, NULL, &tv);
118
119 if (cc < 0) {
120 die("select() failed: %s\n", strerror(errno));
121 }
122
123 if (cc > 0 && FD_ISSET(master_out, &readfds)) {
124 cc = read(master_out, readbuf, sizeof(readbuf));
125
126 if (cc <= 0)
127 break;
128
129 fwrite(readbuf, 1, cc, output);
130
131 if (master_in != -1) {
132 close(master_in);
133 master_in = -1;
134 }
135 }
136
137 now = time(NULL);
138 if (now >= starttime + COMMAND_TIMEOUT) {
139 fprintf(stderr, "Command execution timed out: %ld to %ld: %d\n",
140 starttime, now, cc);
141 break;
142 }
143 }
144
145 if (master_in != -1)
146 close(master_in);
147 close(master_out);
148 fclose(output);
149
150 waitpid(child, &status, 0);
151
152 exit(0);
153 }
154
155 static int
156 open_master_pty(const char *desc)
157 {
158 int fd;
159
160 if ((fd = posix_openpt(O_RDWR | O_NOCTTY)) == -1) {
161 die("Unable to open master %s pseudo-tty: %s\n", desc, strerror(errno));
162 }
163 if (grantpt(fd) == -1) {
164 die("Unable to grant permissions to master %s pty: %s\n", desc,
165 strerror(errno));
166 }
167 if (unlockpt(fd) == -1) {
168 die("Unable to unlock master %s pty: %s\n", desc, strerror(errno));
169 }
170
171 return fd;
172 }
173
174 static void
175 die(const char *fmt, ...)
176 {
177 va_list args;
178
179 va_start(args, fmt);
180 vfprintf(stderr, fmt, args);
181 va_end(args);
182
183 exit(1);
184 }