00001 #define _GNU_SOURCE 1
00002
00003 #include <errno.h>
00004 #include <fcntl.h>
00005 #include <signal.h>
00006 #include <stdarg.h>
00007 #include <stdbool.h>
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include <stropts.h>
00012 #include <sys/ioctl.h>
00013 #include <sys/stat.h>
00014 #include <sys/time.h>
00015 #include <sys/types.h>
00016 #include <sys/wait.h>
00017 #include <termios.h>
00018 #include <unistd.h>
00019
00020 static void
00021 fail_io (const char *msg, ...)
00022 __attribute__ ((noreturn))
00023 __attribute__ ((format (printf, 1, 2)));
00024
00025
00026
00027
00028 static void
00029 fail_io (const char *msg, ...)
00030 {
00031 va_list args;
00032
00033 va_start (args, msg);
00034 vfprintf (stderr, msg, args);
00035 va_end (args);
00036
00037 if (errno != 0)
00038 fprintf (stderr, ": %s", strerror (errno));
00039 putc ('\n', stderr);
00040 exit (EXIT_FAILURE);
00041 }
00042
00043
00044
00045
00046 static void
00047 make_noncanon (int fd, int vmin, int vtime)
00048 {
00049 if (isatty (fd))
00050 {
00051 struct termios termios;
00052 if (tcgetattr (fd, &termios) < 0)
00053 fail_io ("tcgetattr");
00054 termios.c_lflag &= ~(ICANON | ECHO);
00055 termios.c_cc[VMIN] = vmin;
00056 termios.c_cc[VTIME] = vtime;
00057 if (tcsetattr (fd, TCSANOW, &termios) < 0)
00058 fail_io ("tcsetattr");
00059 }
00060 }
00061
00062
00063
00064 static void
00065 make_nonblocking (int fd, bool nonblocking)
00066 {
00067 int flags = fcntl (fd, F_GETFL);
00068 if (flags < 0)
00069 fail_io ("fcntl");
00070 if (nonblocking)
00071 flags |= O_NONBLOCK;
00072 else
00073 flags &= ~O_NONBLOCK;
00074 if (fcntl (fd, F_SETFL, flags) < 0)
00075 fail_io ("fcntl");
00076 }
00077
00078
00079
00080
00081
00082
00083 static bool
00084 handle_error (ssize_t retval, int *fd, bool fd_is_pty, const char *call)
00085 {
00086 if (fd_is_pty)
00087 {
00088 if (retval < 0)
00089 {
00090 if (errno == EIO)
00091 {
00092
00093 return false;
00094 }
00095 else
00096 fail_io (call);
00097 }
00098 else
00099 return true;
00100 }
00101 else
00102 {
00103 if (retval == 0)
00104 {
00105 close (*fd);
00106 *fd = -1;
00107 return true;
00108 }
00109 else
00110 fail_io (call);
00111 }
00112 }
00113
00114
00115
00116 static void
00117 relay (int pty, int dead_child_fd)
00118 {
00119 struct pipe
00120 {
00121 int in, out;
00122 char buf[BUFSIZ];
00123 size_t size, ofs;
00124 bool active;
00125 };
00126 struct pipe pipes[2];
00127
00128
00129 make_nonblocking (pty, true);
00130 make_nonblocking (STDIN_FILENO, true);
00131 make_nonblocking (STDOUT_FILENO, true);
00132
00133
00134
00135
00136
00137 make_noncanon (pty, 16, 1);
00138 make_noncanon (STDIN_FILENO, 1, 0);
00139
00140 memset (pipes, 0, sizeof pipes);
00141 pipes[0].in = STDIN_FILENO;
00142 pipes[0].out = pty;
00143 pipes[1].in = pty;
00144 pipes[1].out = STDOUT_FILENO;
00145
00146 while (pipes[0].in != -1 || pipes[1].in != -1)
00147 {
00148 fd_set read_fds, write_fds;
00149 int retval;
00150 int i;
00151
00152 FD_ZERO (&read_fds);
00153 FD_ZERO (&write_fds);
00154 for (i = 0; i < 2; i++)
00155 {
00156 struct pipe *p = &pipes[i];
00157
00158
00159
00160
00161 if (i == 0 && !pipes[1].active)
00162 continue;
00163
00164 if (p->in != -1 && p->size + p->ofs < sizeof p->buf)
00165 FD_SET (p->in, &read_fds);
00166 if (p->out != -1 && p->size > 0)
00167 FD_SET (p->out, &write_fds);
00168 }
00169 FD_SET (dead_child_fd, &read_fds);
00170
00171 do
00172 {
00173 retval = select (FD_SETSIZE, &read_fds, &write_fds, NULL, NULL);
00174 }
00175 while (retval < 0 && errno == EINTR);
00176 if (retval < 0)
00177 fail_io ("select");
00178
00179 if (FD_ISSET (dead_child_fd, &read_fds))
00180 {
00181
00182 struct pipe *p = &pipes[1];
00183 if (p->out == -1)
00184 return;
00185 make_nonblocking (STDOUT_FILENO, false);
00186 for (;;)
00187 {
00188 ssize_t n;
00189
00190
00191 while (p->size > 0)
00192 {
00193 n = write (p->out, p->buf + p->ofs, p->size);
00194 if (n < 0)
00195 fail_io ("write");
00196 else if (n == 0)
00197 fail_io ("zero-length write");
00198 p->ofs += n;
00199 p->size -= n;
00200 }
00201 p->ofs = 0;
00202
00203 p->size = n = read (p->in, p->buf, sizeof p->buf);
00204 if (n <= 0)
00205 return;
00206 }
00207 }
00208
00209 for (i = 0; i < 2; i++)
00210 {
00211 struct pipe *p = &pipes[i];
00212 if (p->in != -1 && FD_ISSET (p->in, &read_fds))
00213 {
00214 ssize_t n = read (p->in, p->buf + p->ofs + p->size,
00215 sizeof p->buf - p->ofs - p->size);
00216 if (n > 0)
00217 {
00218 p->active = true;
00219 p->size += n;
00220 if (p->size == BUFSIZ && p->ofs != 0)
00221 {
00222 memmove (p->buf, p->buf + p->ofs, p->size);
00223 p->ofs = 0;
00224 }
00225 }
00226 else if (!handle_error (n, &p->in, p->in == pty, "read"))
00227 return;
00228 }
00229 if (p->out != -1 && FD_ISSET (p->out, &write_fds))
00230 {
00231 ssize_t n = write (p->out, p->buf + p->ofs, p->size);
00232 if (n > 0)
00233 {
00234 p->ofs += n;
00235 p->size -= n;
00236 if (p->size == 0)
00237 p->ofs = 0;
00238 }
00239 else if (!handle_error (n, &p->out, p->out == pty, "write"))
00240 return;
00241 }
00242 }
00243 }
00244 }
00245
00246 static int dead_child_fd;
00247
00248 static void
00249 sigchld_handler (int signo __attribute__ ((unused)))
00250 {
00251 if (write (dead_child_fd, "", 1) < 0)
00252 _exit (1);
00253 }
00254
00255 int
00256 main (int argc __attribute__ ((unused)), char *argv[])
00257 {
00258 int master, slave;
00259 char *name;
00260 pid_t pid;
00261 struct sigaction sa;
00262 int pipe_fds[2];
00263 struct itimerval zero_itimerval, old_itimerval;
00264
00265 if (argc < 2)
00266 {
00267 fprintf (stderr,
00268 "usage: squish-pty COMMAND [ARG]...\n"
00269 "Squishes both stdin and stdout into a single pseudoterminal,\n"
00270 "which is passed as stdout to run the specified COMMAND.\n");
00271 return EXIT_FAILURE;
00272 }
00273
00274
00275 master = open ("/dev/ptmx", O_RDWR | O_NOCTTY);
00276 if (master < 0)
00277 fail_io ("open \"/dev/ptmx\"");
00278 if (grantpt (master) < 0)
00279 fail_io ("grantpt");
00280 if (unlockpt (master) < 0)
00281 fail_io ("unlockpt");
00282
00283
00284 name = ptsname (master);
00285 if (name == NULL)
00286 fail_io ("ptsname");
00287 slave = open (name, O_RDWR);
00288 if (slave < 0)
00289 fail_io ("open \"%s\"", name);
00290
00291
00292
00293 if (isastream (slave))
00294 {
00295 if (ioctl (slave, I_PUSH, "ptem") < 0
00296 || ioctl (slave, I_PUSH, "ldterm") < 0)
00297 fail_io ("ioctl");
00298 }
00299
00300
00301
00302
00303 if (pipe (pipe_fds) < 0)
00304 fail_io ("pipe");
00305 dead_child_fd = pipe_fds[1];
00306
00307 memset (&sa, 0, sizeof sa);
00308 sa.sa_handler = sigchld_handler;
00309 sigemptyset (&sa.sa_mask);
00310 sa.sa_flags = SA_RESTART;
00311 if (sigaction (SIGCHLD, &sa, NULL) < 0)
00312 fail_io ("sigaction");
00313
00314
00315
00316
00317 memset (&zero_itimerval, 0, sizeof zero_itimerval);
00318 if (setitimer (ITIMER_VIRTUAL, &zero_itimerval, &old_itimerval) < 0)
00319 fail_io ("setitimer");
00320
00321 pid = fork ();
00322 if (pid < 0)
00323 fail_io ("fork");
00324 else if (pid != 0)
00325 {
00326
00327 int status;
00328 close (slave);
00329 relay (master, pipe_fds[0]);
00330
00331
00332
00333
00334 if (waitpid (pid, &status, WNOHANG) > 0)
00335 {
00336 if (WIFEXITED (status))
00337 return WEXITSTATUS (status);
00338 else if (WIFSIGNALED (status))
00339 raise (WTERMSIG (status));
00340 }
00341 return 0;
00342 }
00343 else
00344 {
00345
00346 if (setitimer (ITIMER_VIRTUAL, &old_itimerval, NULL) < 0)
00347 fail_io ("setitimer");
00348 if (dup2 (slave, STDOUT_FILENO) < 0)
00349 fail_io ("dup2");
00350 if (close (pipe_fds[0]) < 0 || close (pipe_fds[1]) < 0
00351 || close (slave) < 0 || close (master) < 0)
00352 fail_io ("close");
00353 execvp (argv[1], argv + 1);
00354 fail_io ("exec");
00355 }
00356 }