]> diplodocus.org Git - nmh/blob - test/runpty.c
e5de14e0c05f31aaa774de75f20e569154347c86
[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 <fcntl.h>
13 #include <termios.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/select.h>
18 #include <time.h>
19 #include <errno.h>
20
21 #define COMMAND_TIMEOUT 30
22
23 int
24 main(int argc, char *argv[])
25 {
26 int master, slave, cc, sendeof = 0, status;
27 time_t starttime;
28 const char *slavename;
29 pid_t child;
30 unsigned char readbuf[1024];
31 FILE *output;
32
33 if (argc < 3) {
34 fprintf(stderr, "Usage: %s output-filename command [arguments ...]\n",
35 argv[0]);
36 exit(1);
37 }
38
39 if ((master = posix_openpt(O_RDWR | O_NOCTTY)) < 0) {
40 fprintf(stderr, "Unable to open master pseudo-tty: %s\n",
41 strerror(errno));
42 exit(1);
43 }
44
45 if (grantpt(master) < 0) {
46 fprintf(stderr, "Unable to grant permissions to master pty: %s\n",
47 strerror(errno));
48 exit(1);
49 }
50
51 if (unlockpt(master) < 0) {
52 fprintf(stderr, "Unable to unlock master pty: %s\n", strerror(errno));
53 exit(1);
54 }
55
56 if (!(slavename = ptsname(master))) {
57 fprintf(stderr, "Unable to determine name of slave pty: %s\n",
58 strerror(errno));
59 exit(1);
60 }
61
62 if ((slave = open(slavename, O_RDWR | O_NOCTTY)) < 0) {
63 fprintf(stderr, "Unable to open slave pty \"%s\": %s\n", slavename,
64 strerror(errno));
65 exit(1);
66 }
67
68 child = fork();
69
70 /*
71 * Start the child process if we are in the child; close the master
72 * as we are supposed to only use the slave ptys here.
73 */
74
75 if (child == 0) {
76 close(master);
77 dup2(slave, STDIN_FILENO);
78 dup2(slave, STDOUT_FILENO);
79 dup2(slave, STDERR_FILENO);
80 close(slave);
81
82 execvp(argv[2], argv + 2);
83
84 fprintf(stderr, "execvp(%s) failed: %s\n", argv[2], strerror(errno));
85 } else if (child < 0) {
86 fprintf(stderr, "fork() failed: %s\n", strerror(errno));
87 exit(1);
88 }
89
90 close(slave);
91
92 if (!(output = fopen(argv[1], "w"))) {
93 fprintf(stderr, "Unable to open \"%s\" for output: %s\n", argv[1],
94 strerror(errno));
95 exit(1);
96 }
97
98 starttime = time(NULL);
99
100 for (;;) {
101 fd_set readfds;
102 struct timeval tv;
103
104 FD_ZERO(&readfds);
105
106 FD_SET(master, &readfds);
107
108 /*
109 * Okay, what's going on here?
110 *
111 * We want to send the EOF character (to simulate a "end of file",
112 * as if we redirected stdin to /dev/null). We can't just close
113 * master, because we won't be able to get any data back. So,
114 * after we get SOME data, set sendeof, and that will cause us to
115 * send the EOF character after the first select call. If we are
116 * doing sendeof, set the timeout to 1 second; otherwise, set the
117 * timeout to COMMAND_TIMEOUT seconds remaining.
118 */
119
120 if (sendeof) {
121 tv.tv_sec = 1;
122 } else {
123 tv.tv_sec = starttime + COMMAND_TIMEOUT - time(NULL);
124 if (tv.tv_sec < 0)
125 tv.tv_sec = 0;
126 }
127 tv.tv_usec = 0;
128
129 cc = select(master + 1, &readfds, NULL, NULL, &tv);
130
131 if (cc < 0) {
132 fprintf(stderr, "select() failed: %s\n", strerror(errno));
133 exit(1);
134 }
135
136 if (cc > 0 && FD_ISSET(master, &readfds)) {
137 cc = read(master, readbuf, sizeof(readbuf));
138
139 if (cc <= 0)
140 break;
141
142 fwrite(readbuf, 1, cc, output);
143 }
144
145 if (cc == 0 && sendeof) {
146 struct termios rtty;
147
148 if (tcgetattr(master, &rtty) < 0) {
149 fprintf(stderr, "tcgetattr() failed: %s\n", strerror(errno));
150 exit(1);
151 }
152
153 if (rtty.c_lflag & ICANON)
154 write(master, &rtty.c_cc[VEOF], 1);
155 } else if (!sendeof)
156 sendeof = 1;
157
158 if (time(NULL) > starttime + COMMAND_TIMEOUT) {
159 fprintf(stderr, "Command execution timed out\n");
160 break;
161 }
162 }
163
164 close(master);
165 fclose(output);
166
167 waitpid(child, &status, 0);
168
169 exit(0);
170 }