| 1 |
/************************************************************************** |
| 2 |
* This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is * |
| 3 |
* provided by Jonathan and Jovehacks without charge and without * |
| 4 |
* warranty. You may copy, modify, and/or distribute JOVE, provided that * |
| 5 |
* this notice is included in all the source files and documentation. * |
| 6 |
**************************************************************************/ |
| 7 |
|
| 8 |
/* This program is invoked by JOVE for two purposes related to PIPEPROCS: |
| 9 |
* - "kbd": gather tty input into lumps and send them to JOVE proper. |
| 10 |
* - "portsrv": gather process output into lumps and send them to JOVE |
| 11 |
* These functions are gathered into one program to reduce the number |
| 12 |
* of misc. programs to install. The resulting program is small anyway. |
| 13 |
*/ |
| 14 |
|
| 15 |
#define USE_STDIO_H 1 |
| 16 |
#include "jove.h" |
| 17 |
|
| 18 |
#ifdef PIPEPROCS /* almost the whole file! */ |
| 19 |
|
| 20 |
#include <signal.h> |
| 21 |
#include <sys/ioctl.h> |
| 22 |
#include "iproc.h" |
| 23 |
extern int pause(void); |
| 24 |
|
| 25 |
private struct lump lump; |
| 26 |
|
| 27 |
/* JOVE sends KBDSIG whenever it wants the kbd process (this program) |
| 28 |
* to stop competing for input from the keyboard. JOVE does this when |
| 29 |
* JOVE realizes that there are no more interactive processes running. |
| 30 |
* The reason we go through all this trouble is that JOVE slows down |
| 31 |
* a lot when it's getting its keyboard input via a pipe. |
| 32 |
*/ |
| 33 |
|
| 34 |
#ifdef POSIX_SIGS |
| 35 |
SIGHANDLERTYPE |
| 36 |
setsighandler(int signo, SIGHANDLERTYPE handler) |
| 37 |
{ |
| 38 |
static struct sigaction act; /* static so unspecified fields are 0 */ |
| 39 |
struct sigaction oact; |
| 40 |
|
| 41 |
act.sa_handler = handler; |
| 42 |
act.sa_flags = SA_RESTART | SA_NODEFER; |
| 43 |
sigaction(signo, &act, NULL); |
| 44 |
return oact.sa_handler; |
| 45 |
} |
| 46 |
#else |
| 47 |
# define setsighandler(signo, handler) signal((signo), (handler)) |
| 48 |
#endif |
| 49 |
|
| 50 |
private SIGRESTYPE |
| 51 |
strt_read(int); |
| 52 |
private volatile jbool |
| 53 |
wait_for_sig = NO; |
| 54 |
|
| 55 |
private SIGRESTYPE |
| 56 |
hold_read(int UNUSED(junk)) |
| 57 |
{ |
| 58 |
setsighandler(KBDSIG, strt_read); |
| 59 |
#if defined(BSD_SIGS)||defined(POSIX_SIGS) |
| 60 |
/* |
| 61 |
* with restartable signals, the read() in kbd_process() might never |
| 62 |
* return EINTR, so need to wait here till we receive KBDSIG again. |
| 63 |
*/ |
| 64 |
pause(); |
| 65 |
#else |
| 66 |
/* For old non-BSD Unix, without restartable signals */ |
| 67 |
wait_for_sig = YES; |
| 68 |
#endif |
| 69 |
return SIGRESVALUE; |
| 70 |
} |
| 71 |
|
| 72 |
private SIGRESTYPE |
| 73 |
strt_read(int UNUSED(junk)) |
| 74 |
{ |
| 75 |
setsighandler(KBDSIG, hold_read); |
| 76 |
wait_for_sig = NO; |
| 77 |
return SIGRESVALUE; |
| 78 |
} |
| 79 |
|
| 80 |
private void |
| 81 |
detach(void) |
| 82 |
{ |
| 83 |
#ifdef POSIX_PROCS |
| 84 |
setsid(); |
| 85 |
#ifdef TIOCNOTTY |
| 86 |
{ |
| 87 |
int fd = open("/dev/tty", O_WRONLY | O_BINARY | O_CLOEXEC); |
| 88 |
/* |
| 89 |
* if one tries to use portsrv on modern *n*x, |
| 90 |
* shells (bash, dash) seem to hang, because |
| 91 |
* they seem to try to open /dev/tty, even if |
| 92 |
* given the -s option. portsrv should not be |
| 93 |
* needed on such machines, since they should |
| 94 |
* have and use ptys, but for testing on such |
| 95 |
* machines, need to detach from the |
| 96 |
* controlling terminal. |
| 97 |
*/ |
| 98 |
if (fd >= 0) { |
| 99 |
(void) ioctl(fd, TIOCNOTTY, NULL); |
| 100 |
(void) close(fd); |
| 101 |
} |
| 102 |
} |
| 103 |
#endif |
| 104 |
NEWPG(); |
| 105 |
#endif |
| 106 |
} |
| 107 |
|
| 108 |
private void |
| 109 |
kbd_process(void) |
| 110 |
{ |
| 111 |
int pid, |
| 112 |
n = -1; |
| 113 |
|
| 114 |
detach(); |
| 115 |
signal(SIGINT, SIG_IGN); |
| 116 |
pid = getpid(); |
| 117 |
lump.header.pid = pid; |
| 118 |
|
| 119 |
strt_read(0); |
| 120 |
for (;;) { |
| 121 |
if (wait_for_sig) { |
| 122 |
pause(); |
| 123 |
} |
| 124 |
n = read(0, lump.data, sizeof(lump.data)); |
| 125 |
if (n < 0) { |
| 126 |
if (!RETRY_ERRNO(errno)) |
| 127 |
break; /* something unfortunate hapened?! */ |
| 128 |
/* most likely KBDSIG */ |
| 129 |
continue; |
| 130 |
} |
| 131 |
lump.header.nbytes = n; |
| 132 |
/* It is not clear what we can do if this write fails */ |
| 133 |
do {} while (write(1, &lump, sizeof(struct header) + n) < 0 |
| 134 |
&& RETRY_ERRNO(errno)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/* This is a server for jove sub processes. By the time we get here, our |
| 139 |
* standard output goes to jove's process input. |
| 140 |
*/ |
| 141 |
|
| 142 |
private void |
| 143 |
proc_write(const void *ptr, size_t n) |
| 144 |
{ |
| 145 |
/* It is not clear what we can do if this write fails */ |
| 146 |
do {} while (write(1, ptr, n) < 0 && RETRY_ERRNO(errno)); |
| 147 |
} |
| 148 |
|
| 149 |
private void |
| 150 |
read_pipe(int fd) |
| 151 |
{ |
| 152 |
size_t n; |
| 153 |
|
| 154 |
while ((lump.header.nbytes = read(fd, lump.data, sizeof lump.data)) > 0) { |
| 155 |
n = sizeof(struct header) + lump.header.nbytes; |
| 156 |
proc_write(&lump, n); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
private void proc_error(const char * /* str */) NEVER_RETURNS; |
| 161 |
|
| 162 |
private void |
| 163 |
proc_error(const char *str) |
| 164 |
{ |
| 165 |
lump.header.pid = getpid(); |
| 166 |
lump.header.nbytes = strlen(str); |
| 167 |
strcpy(lump.data, str); |
| 168 |
proc_write(&lump, sizeof(struct header) + lump.header.nbytes); |
| 169 |
/* It is not clear what we can do if this write fails */ |
| 170 |
#ifndef TIOCNOTTY |
| 171 |
{ |
| 172 |
/* |
| 173 |
* for last-ditch error-reporting on old machines. On new |
| 174 |
* machines, we are either detached from tty or trying to open |
| 175 |
* it might hang. |
| 176 |
*/ |
| 177 |
int tfd = open("/dev/tty", O_WRONLY | O_BINARY); |
| 178 |
if (tfd >= 0) |
| 179 |
do {} while (write(tfd, str, strlen(str)) < 0 && |
| 180 |
RETRY_ERRNO(errno)); |
| 181 |
} |
| 182 |
#endif |
| 183 |
_exit(-2); |
| 184 |
} |
| 185 |
|
| 186 |
private void |
| 187 |
portsrv_process(int argc, char **argv) |
| 188 |
{ |
| 189 |
int p[2]; |
| 190 |
pid_t pid; |
| 191 |
|
| 192 |
if (pipe(p) == -1) { |
| 193 |
proc_error("Cannot pipe jove portsrv.\n"); |
| 194 |
/* NOTREACHED */ |
| 195 |
} |
| 196 |
switch (pid = fork()) { |
| 197 |
case -1: |
| 198 |
proc_error("portsrv: cannot fork.\n"); |
| 199 |
/*NOTREACHED*/ |
| 200 |
|
| 201 |
case 0: |
| 202 |
/* We'll intercept child's output in p[0] */ |
| 203 |
(void) dup2(p[1], 1); |
| 204 |
(void) dup2(p[1], 2); |
| 205 |
(void) close(p[0]); |
| 206 |
(void) close(p[1]); |
| 207 |
detach(); |
| 208 |
execv(argv[1], &argv[2]); |
| 209 |
_exit(-4); |
| 210 |
/*NOTREACHED*/ |
| 211 |
|
| 212 |
default: |
| 213 |
(void) close(0); |
| 214 |
(void) signal(SIGINT, SIG_IGN); |
| 215 |
(void) signal(SIGQUIT, SIG_IGN); |
| 216 |
(void) close(p[1]); |
| 217 |
|
| 218 |
/* tell jove the pid of the real child as opposed to us */ |
| 219 |
lump.header.pid = getpid(); |
| 220 |
lump.header.nbytes = sizeof (pid_t); |
| 221 |
byte_copy(&pid, lump.data, sizeof(pid_t)); |
| 222 |
/* It is not clear what we can do if this write fails */ |
| 223 |
do {} while (write(1, &lump, sizeof(struct header) + sizeof(pid_t)) < 0 |
| 224 |
&& RETRY_ERRNO(errno)); |
| 225 |
|
| 226 |
/* read proc's output and send it to jove */ |
| 227 |
read_pipe(p[0]); |
| 228 |
|
| 229 |
/* received EOF - wait for child to die and then write the |
| 230 |
* child's status to JOVE. |
| 231 |
* |
| 232 |
* Notice that we use a byte count of -1 (an otherwise |
| 233 |
* impossible value) as a marker. JOVE "knows" the real |
| 234 |
* length is sizeof(wait_status_t). |
| 235 |
*/ |
| 236 |
|
| 237 |
(void) close(p[0]); |
| 238 |
lump.header.pid = getpid(); |
| 239 |
lump.header.nbytes = -1; /* tell jove we are finished */ |
| 240 |
/* try to exit like our child did ... */ |
| 241 |
{ |
| 242 |
wait_status_t status; |
| 243 |
|
| 244 |
do {} while (wait(&status) != pid); |
| 245 |
byte_copy(&status, lump.data, sizeof(status)); |
| 246 |
} |
| 247 |
/* It is not clear what we can do if this write fails */ |
| 248 |
do {} while (write(1, &lump, |
| 249 |
sizeof(struct header) + sizeof(wait_status_t)) < 0 |
| 250 |
&& errno == EINTR); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
int |
| 255 |
main(int argc, char **argv) |
| 256 |
{ |
| 257 |
if (argc == 2 && strcmp(argv[1], "--kbd") == 0) { |
| 258 |
kbd_process(); |
| 259 |
} else if (argc > 2) { |
| 260 |
portsrv_process(argc, argv); |
| 261 |
} else { |
| 262 |
fprintf(stderr, "Usage: %s --kbd\nor\n%s EXECUTABLE ARGV...\n", |
| 263 |
argv[0], argv[0]); |
| 264 |
exit(1); |
| 265 |
} |
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
#else /* !PIPEPROCS */ |
| 270 |
/* |
| 271 |
* Without PIPEPROCS (ptyprocs or NO_IPROCS), this program |
| 272 |
* should neither be installed nor called, but just in case, |
| 273 |
* some verbosity to help with the bug report! |
| 274 |
*/ |
| 275 |
int |
| 276 |
main(int argc, char *argv[]) |
| 277 |
{ |
| 278 |
int i; |
| 279 |
fprintf(stderr, "%s not compiled for PIPEPROCS: argc=%d\n", argv[0], |
| 280 |
argc); |
| 281 |
for (i = 0; i < argc; i++) |
| 282 |
fprintf(stderr, "argv[%d] = \"%s\"\n", i, argv[i]); |
| 283 |
return 1; |
| 284 |
} |
| 285 |
#endif /* !PIPEPROCS */ |