12 #include <sys/ioctl.h>
15 #include <sys/types.h>
17 #include <sys/socket.h>
23 fail_io (const char *msg, ...)
24 __attribute__ ((noreturn))
25 __attribute__ ((format (printf, 1, 2)));
27 /* Prints MSG, formatting as with printf(),
28 plus an error message based on errno,
31 fail_io (const char *msg, ...)
36 vfprintf (stderr, msg, args);
40 fprintf (stderr, ": %s", strerror (errno));
45 /* If FD is a terminal, configures it for noncanonical input mode
46 with VMIN and VTIME set as indicated.
47 If FD is not a terminal, has no effect. */
49 make_noncanon (int fd, int vmin, int vtime)
53 struct termios termios;
54 if (tcgetattr (fd, &termios) < 0)
55 fail_io ("tcgetattr");
56 termios.c_lflag &= ~(ICANON | ECHO);
57 termios.c_cc[VMIN] = vmin;
58 termios.c_cc[VTIME] = vtime;
59 if (tcsetattr (fd, TCSANOW, &termios) < 0)
60 fail_io ("tcsetattr");
64 /* Make FD non-blocking if NONBLOCKING is true,
65 or blocking if NONBLOCKING is false. */
67 make_nonblocking (int fd, bool nonblocking)
69 int flags = fcntl (fd, F_GETFL);
76 if (fcntl (fd, F_SETFL, flags) < 0)
80 /* Handle a read or write on *FD, which is the socket if
81 FD_IS_SOCK is true, that returned end-of-file or error
82 indication RETVAL. The system call is named CALL, for use in
83 error messages. Returns true if processing may continue,
84 false if we're all done. */
86 handle_error (ssize_t retval, int *fd, bool fd_is_sock, const char *call)
102 /* Copies data from stdin to SOCK and from SOCK to stdout until no
103 more data can be read or written. */
114 struct pipe pipes[2];
116 /* In case stdin is a file, go back to the beginning.
117 This allows replaying the input on reset. */
118 lseek (STDIN_FILENO, 0, SEEK_SET);
120 /* Make SOCK, stdin, and stdout non-blocking. */
121 make_nonblocking (sock, true);
122 make_nonblocking (STDIN_FILENO, true);
123 make_nonblocking (STDOUT_FILENO, true);
125 /* Configure noncanonical mode on stdin to avoid waiting for
127 make_noncanon (STDIN_FILENO, 1, 0);
129 memset (pipes, 0, sizeof pipes);
130 pipes[0].in = STDIN_FILENO;
133 pipes[1].out = STDOUT_FILENO;
135 while (pipes[0].in != -1 || pipes[1].in != -1
136 || (pipes[1].size && pipes[1].out != -1))
138 fd_set read_fds, write_fds;
144 FD_ZERO (&write_fds);
145 for (i = 0; i < 2; i++)
147 struct pipe *p = &pipes[i];
149 /* Don't do anything with the stdin->sock pipe until we
150 have some data for the sock->stdout pipe. If we get
151 too eager, vmplayer will throw away our input. */
152 if (i == 0 && !pipes[1].active)
155 if (p->in != -1 && p->size + p->ofs < sizeof p->buf)
156 FD_SET (p->in, &read_fds);
157 if (p->out != -1 && p->size > 0)
158 FD_SET (p->out, &write_fds);
160 sigemptyset (&empty_set);
161 retval = pselect (FD_SETSIZE, &read_fds, &write_fds, NULL, NULL,
167 /* Child died. Do final relaying. */
168 struct pipe *p = &pipes[1];
171 make_nonblocking (STDOUT_FILENO, false);
179 n = write (p->out, p->buf + p->ofs, p->size);
183 fail_io ("zero-length write");
189 p->size = n = read (p->in, p->buf, sizeof p->buf);
197 for (i = 0; i < 2; i++)
199 struct pipe *p = &pipes[i];
200 if (p->in != -1 && FD_ISSET (p->in, &read_fds))
202 ssize_t n = read (p->in, p->buf + p->ofs + p->size,
203 sizeof p->buf - p->ofs - p->size);
208 if (p->size == BUFSIZ && p->ofs != 0)
210 memmove (p->buf, p->buf + p->ofs, p->size);
214 else if (!handle_error (n, &p->in, p->in == sock, "read"))
217 if (p->out != -1 && FD_ISSET (p->out, &write_fds))
219 ssize_t n = write (p->out, p->buf + p->ofs, p->size);
227 else if (!handle_error (n, &p->out, p->out == sock, "write"))
235 sigchld_handler (int signo __attribute__ ((unused)))
241 main (int argc __attribute__ ((unused)), char *argv[])
244 struct itimerval zero_itimerval;
245 struct sockaddr_un sun;
246 sigset_t sigchld_set;
252 "usage: squish-unix SOCKET COMMAND [ARG]...\n"
253 "Squishes both stdin and stdout into a single Unix domain\n"
254 "socket named SOCKET, and runs COMMAND as a subprocess.\n");
259 sock = socket (PF_LOCAL, SOCK_STREAM, 0);
263 /* Configure socket. */
264 sun.sun_family = AF_LOCAL;
265 strncpy (sun.sun_path, argv[1], sizeof sun.sun_path);
266 sun.sun_path[sizeof sun.sun_path - 1] = '\0';
267 if (unlink (sun.sun_path) < 0 && errno != ENOENT)
269 if (bind (sock, (struct sockaddr *) &sun,
270 (offsetof (struct sockaddr_un, sun_path)
271 + strlen (sun.sun_path) + 1)) < 0)
274 /* Listen on socket. */
275 if (listen (sock, 1) < 0)
278 /* Block SIGCHLD and set up a handler for it. */
279 sigemptyset (&sigchld_set);
280 sigaddset (&sigchld_set, SIGCHLD);
281 if (sigprocmask (SIG_BLOCK, &sigchld_set, NULL) < 0)
282 fail_io ("sigprocmask");
283 if (signal (SIGCHLD, sigchld_handler) == SIG_ERR)
286 /* Save the virtual interval timer, which might have been set
287 by the process that ran us. It really should be applied to
288 our child process. */
289 memset (&zero_itimerval, 0, sizeof zero_itimerval);
290 if (setitimer (ITIMER_VIRTUAL, &zero_itimerval, NULL) < 0)
291 fail_io ("setitimer");
298 /* Running in parent process. */
299 make_nonblocking (sock, true);
307 /* Wait for connection. */
309 FD_SET (sock, &read_fds);
310 sigemptyset (&empty_set);
311 retval = pselect (sock + 1, &read_fds, NULL, NULL, NULL, &empty_set);
319 /* Accept connection. */
320 conn = accept (sock, NULL, NULL);
324 /* Relay connection. */
332 /* Running in child process. */
333 if (close (sock) < 0)
335 execvp (argv[2], argv + 2);