| 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 |
#include "jove.h" |
| 9 |
|
| 10 |
#ifdef IPROCS /* the body is the rest of this file */ |
| 11 |
|
| 12 |
#define MAX_BUSY_WAIT 3 /* seconds to ignore errors on pty */ |
| 13 |
|
| 14 |
#include <signal.h> |
| 15 |
|
| 16 |
#include "re.h" |
| 17 |
#include "jctype.h" |
| 18 |
#include "disp.h" |
| 19 |
#include "fp.h" |
| 20 |
#include "iproc.h" |
| 21 |
#include "ask.h" |
| 22 |
#include "extend.h" |
| 23 |
#include "fmt.h" |
| 24 |
#include "insert.h" |
| 25 |
#include "marks.h" |
| 26 |
#include "move.h" |
| 27 |
#include "proc.h" |
| 28 |
#include "wind.h" |
| 29 |
|
| 30 |
struct process { |
| 31 |
Process p_next; |
| 32 |
#ifdef PIPEPROCS |
| 33 |
int p_toproc; /* write end of pipe to process */ |
| 34 |
pid_t p_portpid, /* pid of direct child (the portsrv) */ |
| 35 |
p_pid; /* pid of real child i.e. not portsrv */ |
| 36 |
jbool p_portlive; /* is portsrv still live? */ |
| 37 |
#else |
| 38 |
int p_fd; /* file descriptor of pty? opened r/w */ |
| 39 |
# define p_portpid p_pid /* pid of direct child (the shell) */ |
| 40 |
pid_t p_pid; /* pid of child (the shell) */ |
| 41 |
time_t p_start; /* time we started this child */ |
| 42 |
#endif |
| 43 |
Buffer *p_buffer; /* add output to end of this buffer */ |
| 44 |
char *p_name; /* ... */ |
| 45 |
int p_io_state; /* state of communication with child and descendents */ |
| 46 |
# define IO_NEW 0 /* brand new, process has not yet sent output */ |
| 47 |
# define IO_RUNNING 1 /* just running */ |
| 48 |
# define IO_EOFED 2 /* we have sent EOF, or have received it */ |
| 49 |
int p_child_state; /* state of child process, as disclosed by SIGCHLD/wait */ |
| 50 |
# define C_LIVE 0 /* no news is good news */ |
| 51 |
# define C_STOPPED 1 |
| 52 |
# define C_EXITED 2 |
| 53 |
# define C_KILLED 3 |
| 54 |
int p_reason; /* If killed, p_reason is the signal; |
| 55 |
if exited, it is the the exit code */ |
| 56 |
Mark *p_mark; /* where output left us */ |
| 57 |
}; |
| 58 |
|
| 59 |
private void |
| 60 |
proc_rec(Process, char *, size_t), |
| 61 |
proc_close(Process), |
| 62 |
SendData(jbool), |
| 63 |
obituary(Process child, wait_status_t w); |
| 64 |
|
| 65 |
private jbool |
| 66 |
proc_kill(Process, int); |
| 67 |
|
| 68 |
#define child_dead(p) ((p)->p_child_state >= C_EXITED) |
| 69 |
#define io_eofed(p) ((p)->p_io_state == IO_EOFED) |
| 70 |
|
| 71 |
private jbool |
| 72 |
dead(Process p) |
| 73 |
{ |
| 74 |
return p == NULL || (io_eofed(p) && child_dead(p)); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
#define proc_cmd(p) ((p)->p_name) |
| 79 |
|
| 80 |
private Process procs = NULL; |
| 81 |
|
| 82 |
private Process |
| 83 |
proc_pid(pid_t pid) |
| 84 |
{ |
| 85 |
Process p; |
| 86 |
|
| 87 |
for (p = procs; ; p = p->p_next) |
| 88 |
if (p == NULL || p->p_portpid == pid) |
| 89 |
return p; |
| 90 |
} |
| 91 |
|
| 92 |
private const char * |
| 93 |
proc_bufname(Process p) |
| 94 |
{ |
| 95 |
Buffer *b = p->p_buffer; |
| 96 |
|
| 97 |
return (b != NULL) ? b->b_name : "<Deleted>"; |
| 98 |
} |
| 99 |
|
| 100 |
void |
| 101 |
ProcKill(void) |
| 102 |
{ |
| 103 |
(void) proc_kill( |
| 104 |
buf_exists(ask_buf(curbuf, ALLOW_OLD | ALLOW_INDEX))->b_process, |
| 105 |
SIGKILL); |
| 106 |
} |
| 107 |
|
| 108 |
private void |
| 109 |
make_argv(char *argv[], va_list ap) |
| 110 |
{ |
| 111 |
int i = 0; |
| 112 |
|
| 113 |
argv[i++] = va_arg(ap, char *); |
| 114 |
argv[i++] = (char *)jbasename(argv[0]); /* lose const (but it's safe) */ |
| 115 |
do {} while ((argv[i++] = va_arg(ap, char *)) != NULL); |
| 116 |
} |
| 117 |
|
| 118 |
/* environment manipulation for interactive processes */ |
| 119 |
|
| 120 |
private Env iproc_env; |
| 121 |
private char IEnvExpBuf[LBSIZE]; |
| 122 |
private char IEnvUnsetBuf[LBSIZE]; |
| 123 |
|
| 124 |
/* Erase misleading knowledge of the terminal type from the environment. |
| 125 |
* The value of variable TERMCAP has two interpretations: |
| 126 |
* If it starts with /, it is a path to the database. |
| 127 |
* If it does not, it is the database entry. |
| 128 |
* Only happens in child. |
| 129 |
*/ |
| 130 |
private void |
| 131 |
set_process_env(void) |
| 132 |
{ |
| 133 |
static const char tcn[] = "TERMCAP"; |
| 134 |
const char *tc = getenv(tcn); |
| 135 |
|
| 136 |
if (tc != NULL && tc[0] != '/') |
| 137 |
junsetenv(&iproc_env, tcn); |
| 138 |
environ = (char **) jenvdata(&iproc_env); /* avoid gcc warning */ |
| 139 |
} |
| 140 |
|
| 141 |
void |
| 142 |
IprocEnvExport(void) |
| 143 |
{ |
| 144 |
jamstr(IEnvExpBuf, ask(IEnvExpBuf, ProcFmt)); |
| 145 |
jputenv(&iproc_env, IEnvExpBuf); |
| 146 |
} |
| 147 |
|
| 148 |
void |
| 149 |
IprocEnvShow(void) |
| 150 |
{ |
| 151 |
const char **p; |
| 152 |
TOstart("i-shell environment"); |
| 153 |
for (p = jenvdata(&iproc_env); *p; p++) { |
| 154 |
Typeout("%s", *p); |
| 155 |
} |
| 156 |
TOstop(); |
| 157 |
} |
| 158 |
|
| 159 |
void |
| 160 |
IprocEnvUnset(void) |
| 161 |
{ |
| 162 |
jamstr(IEnvUnsetBuf, ask(IEnvUnsetBuf, ProcFmt)); |
| 163 |
junsetenv(&iproc_env, IEnvUnsetBuf); |
| 164 |
} |
| 165 |
|
| 166 |
/* There are two very different implementation techniques: pipes and ptys. |
| 167 |
* The following two chunks of code implement the same operations using |
| 168 |
* the two techniques: the first uses pipes and the second uses ptys. |
| 169 |
*/ |
| 170 |
|
| 171 |
|
| 172 |
#include "ttystate.h" |
| 173 |
#ifdef PIPEPROCS |
| 174 |
|
| 175 |
char Portsrv[FILESIZE]; /* path to portsrv program (in LibDir) */ |
| 176 |
const char *procarg0 = "jove-portsrv"; /* helpful identifying it in ps */ |
| 177 |
|
| 178 |
int NumProcs = 0; |
| 179 |
|
| 180 |
File *ProcInput; |
| 181 |
private int ProcOutput = -1; |
| 182 |
|
| 183 |
pid_t kbd_pid = -1; |
| 184 |
|
| 185 |
void |
| 186 |
read_pipe_proc(pid_t pid, int nbytes) |
| 187 |
{ |
| 188 |
Process p = proc_pid(pid); |
| 189 |
|
| 190 |
jdbg("read_pipe_proc pid %D %d\n", (long) pid, nbytes); |
| 191 |
if (p == NULL) { |
| 192 |
writef("\riproc: unknown pid (%D)", (long) pid); |
| 193 |
} else if (p->p_io_state == IO_NEW) { |
| 194 |
/* first message: pid of real child, not of portsrv */ |
| 195 |
pid_t rpid; |
| 196 |
|
| 197 |
(void) f_readn(ProcInput, (char *) &rpid, sizeof(pid_t)); |
| 198 |
p->p_pid = rpid; |
| 199 |
p->p_io_state = IO_RUNNING; |
| 200 |
UpdModLine = YES; |
| 201 |
} else if (nbytes == -1) { |
| 202 |
/* okay to clean up this process */ |
| 203 |
wait_status_t status; |
| 204 |
|
| 205 |
(void) f_readn(ProcInput, (char *) &status, sizeof(status)); |
| 206 |
/* Reap portsrv process, if it still remains. |
| 207 |
* Note that any status for this process is uninteresting: |
| 208 |
* the interesting status came (just now) by pipe. |
| 209 |
*/ |
| 210 |
while (p->p_portlive) { |
| 211 |
wait_status_t w; |
| 212 |
pid_t rpid = wait(&w); |
| 213 |
|
| 214 |
if (rpid == -1) { |
| 215 |
if (errno == ECHILD) { |
| 216 |
/* oops: no children, not even portsrv */ |
| 217 |
p->p_portlive = NO; |
| 218 |
} |
| 219 |
} else { |
| 220 |
kill_off(rpid, w); |
| 221 |
} |
| 222 |
} |
| 223 |
proc_close(p); |
| 224 |
obituary(p, status); |
| 225 |
} else { |
| 226 |
/* regular data */ |
| 227 |
while (nbytes > 0) { |
| 228 |
char ibuf[512+1]; /* NOTE: room for added NUL */ |
| 229 |
size_t n = f_readn(ProcInput, ibuf, |
| 230 |
(size_t)jmin((int)(sizeof ibuf) - 1, nbytes)); |
| 231 |
|
| 232 |
nbytes -= n; |
| 233 |
proc_rec(p, ibuf, n); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
void |
| 239 |
ProcInt(void) |
| 240 |
{ |
| 241 |
(void) proc_kill(curbuf->b_process, SIGINT); |
| 242 |
} |
| 243 |
|
| 244 |
void |
| 245 |
ProcQuit(void) |
| 246 |
{ |
| 247 |
(void) proc_kill(curbuf->b_process, SIGQUIT); |
| 248 |
} |
| 249 |
|
| 250 |
private void |
| 251 |
proc_close(Process p) |
| 252 |
{ |
| 253 |
if (p->p_toproc >= 0) { |
| 254 |
jdbg("closing proc fd %d pid %D NumProcs %d\n", |
| 255 |
p->p_toproc, (long) (p->p_pid), NumProcs); |
| 256 |
(void) close(p->p_toproc); |
| 257 |
p->p_toproc = -1; /* writes will fail */ |
| 258 |
NumProcs -= 1; |
| 259 |
p->p_io_state = IO_EOFED; /* process output EOF is tied to reaping */ |
| 260 |
UpdModLine = YES; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
private void |
| 265 |
proc_write(Process p, char *buf, size_t nbytes) |
| 266 |
{ |
| 267 |
if (p->p_toproc >= 0) { |
| 268 |
while (nbytes != 0) { |
| 269 |
JSSIZE_T wr = write(p->p_toproc, buf, nbytes); |
| 270 |
|
| 271 |
if (wr >= 0) { |
| 272 |
nbytes -= wr; |
| 273 |
buf += wr; |
| 274 |
} else if (errno != EINTR) { |
| 275 |
complain("[error writing to iproc: %d %s]", errno, strerror(errno)); |
| 276 |
/* NOTREACHED */ |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
private void |
| 284 |
proc_strt(char *bufname, jbool clobber, char *procname, ...) |
| 285 |
{ |
| 286 |
Window *owind = curwind; |
| 287 |
int toproc[2]; |
| 288 |
pid_t pid; |
| 289 |
Process newp; |
| 290 |
Buffer *newbuf; |
| 291 |
char *argv[32]; |
| 292 |
va_list ap; |
| 293 |
|
| 294 |
untieDeadProcess(buf_exists(bufname)); |
| 295 |
isprocbuf(bufname); /* make sure BUFNAME is either nonexistant |
| 296 |
or is of type B_PROCESS */ |
| 297 |
if (access(Portsrv, J_X_OK) < 0) { |
| 298 |
complain("[Couldn't access %s: %s]", Portsrv, strerror(errno)); |
| 299 |
/* NOTREACHED */ |
| 300 |
} |
| 301 |
|
| 302 |
dopipe(toproc); |
| 303 |
|
| 304 |
jdbg("proc_strt NumProcs=%d buf \"%s\" proc \"%s\"\n", |
| 305 |
NumProcs, bufname, procname); |
| 306 |
if (NumProcs++ == 0) |
| 307 |
kbd_strt(); /* may create kbd process: must be done before fork */ |
| 308 |
switch (pid = fork()) { |
| 309 |
case -1: |
| 310 |
pipeclose(toproc); |
| 311 |
if (--NumProcs == 0) |
| 312 |
kbd_stop(); |
| 313 |
complain("[Fork failed: %s]", strerror(errno)); |
| 314 |
/* NOTREACHED */ |
| 315 |
|
| 316 |
case 0: |
| 317 |
argv[0] = (char*)procarg0; |
| 318 |
va_start(ap, procname); |
| 319 |
make_argv(&argv[1], ap); |
| 320 |
va_end(ap); |
| 321 |
(void) dup2(toproc[0], 0); |
| 322 |
(void) dup2(ProcOutput, 1); |
| 323 |
(void) dup2(ProcOutput, 2); |
| 324 |
pipeclose(toproc); |
| 325 |
jcloseall(); |
| 326 |
set_process_env(); |
| 327 |
jdbg("ready to exec %s %s %s %s\n", Portsrv, argv[0], |
| 328 |
argv[1]? argv[1] : "(null)", argv[2]? argv[2] : "(null)"); |
| 329 |
execv(Portsrv, argv); |
| 330 |
raw_complain("execl failed: %s", strerror(errno)); |
| 331 |
_exit(1); |
| 332 |
} |
| 333 |
|
| 334 |
newp = (Process) emalloc(sizeof *newp); |
| 335 |
newp->p_next = procs; |
| 336 |
newp->p_io_state = IO_NEW; |
| 337 |
newp->p_child_state = C_LIVE; |
| 338 |
newp->p_name = copystr(procname); |
| 339 |
procs = newp; |
| 340 |
newp->p_portpid = pid; |
| 341 |
newp->p_pid = -1; |
| 342 |
newp->p_portlive = YES; |
| 343 |
|
| 344 |
newbuf = do_select((Window *)NULL, bufname); |
| 345 |
newbuf->b_type = B_PROCESS; |
| 346 |
newp->p_buffer = newbuf; |
| 347 |
newbuf->b_process = newp; /* sorta circular, eh? */ |
| 348 |
pop_wind(bufname, clobber, B_PROCESS); |
| 349 |
ToLast(); |
| 350 |
if (!bolp()) |
| 351 |
LineInsert(1); |
| 352 |
/* Pop_wind() after everything is set up; important! |
| 353 |
* Bindings won't work right unless newbuf->b_process is already |
| 354 |
* set up BEFORE NEWBUF is first SetBuf()'d. |
| 355 |
*/ |
| 356 |
newp->p_mark = MakeMark(curline, curchar); |
| 357 |
|
| 358 |
newp->p_toproc = toproc[1]; |
| 359 |
(void) close(toproc[0]); |
| 360 |
SetWind(owind); |
| 361 |
} |
| 362 |
|
| 363 |
void |
| 364 |
closeiprocs(void) |
| 365 |
{ |
| 366 |
Process p; |
| 367 |
|
| 368 |
if (ProcOutput != -1) |
| 369 |
close(ProcOutput); |
| 370 |
for (p=procs; p!=NULL; p=p->p_next) |
| 371 |
if (p->p_toproc >= 0) |
| 372 |
close(p->p_toproc); |
| 373 |
} |
| 374 |
|
| 375 |
private void |
| 376 |
kbd_init(void) |
| 377 |
{ |
| 378 |
/* Initiate the keyboard process. |
| 379 |
* We only get here after a portsrv process has been started |
| 380 |
* so we know that the portsrv program must exist -- no need to test. |
| 381 |
*/ |
| 382 |
int p[2]; |
| 383 |
|
| 384 |
if (pipe(p) < 0) { |
| 385 |
complain("Cannot create pipe to kbd process! %s\n", strerror(errno)); |
| 386 |
/* NOTREACHED */ |
| 387 |
} |
| 388 |
ProcInput = fd_open("process-input", F_READ|F_LOCKED, p[0], |
| 389 |
(char *)NULL, 512); |
| 390 |
ProcOutput = p[1]; |
| 391 |
switch (kbd_pid = fork()) { |
| 392 |
case -1: |
| 393 |
complain("Cannot fork kbd process! %s\n", strerror(errno)); |
| 394 |
/* NOTREACHED */ |
| 395 |
|
| 396 |
case 0: |
| 397 |
(void) setsighandler(SIGINT, SIG_IGN); |
| 398 |
(void) setsighandler(SIGALRM, SIG_IGN); |
| 399 |
close(1); |
| 400 |
if (dup(ProcOutput) < 0) { |
| 401 |
raw_complain("Cannot dup output fd: %s", strerror(errno)); |
| 402 |
EXIT(-1); |
| 403 |
} |
| 404 |
jcloseall(); |
| 405 |
jdbg("ready to exec %s as %s --kbd\n", Portsrv, procarg0); |
| 406 |
execl(Portsrv, procarg0, "--kbd", (char *)NULL); |
| 407 |
raw_complain("kbd exec failed: %s", strerror(errno)); |
| 408 |
EXIT(-1); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
/* kbd_stop() returns true if it changes the state of (i.e. stops) |
| 413 |
* the keyboard process. This is so kbd stopping and starting in |
| 414 |
* pairs works - see finish() in jove.c. |
| 415 |
*/ |
| 416 |
private jbool |
| 417 |
kbd_state = NO; |
| 418 |
|
| 419 |
void |
| 420 |
kbd_strt(void) |
| 421 |
{ |
| 422 |
jdbg("kbd_strt state %d pid %D NumProcs %d\n", |
| 423 |
kbd_state, (long) kbd_pid, NumProcs); |
| 424 |
if (!kbd_state) { |
| 425 |
if (kbd_pid == -1) |
| 426 |
kbd_init(); |
| 427 |
else |
| 428 |
kill(kbd_pid, KBDSIG); |
| 429 |
kbd_state = YES; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
jbool |
| 434 |
kbd_stop(void) |
| 435 |
{ |
| 436 |
jdbg("kbd_stop state %d pid %D NumProcs %d\n", |
| 437 |
kbd_state, (long) kbd_pid, NumProcs); |
| 438 |
if (kbd_state) { |
| 439 |
kbd_state = NO; |
| 440 |
kill(kbd_pid, KBDSIG); |
| 441 |
return YES; |
| 442 |
} |
| 443 |
return NO; |
| 444 |
} |
| 445 |
|
| 446 |
void |
| 447 |
kbd_kill(void) |
| 448 |
{ |
| 449 |
jdbg("kbd_kill state %d pid %D Numprocs %d\n", |
| 450 |
kbd_state, (long) kbd_pid, NumProcs); |
| 451 |
if (kbd_pid != -1) { |
| 452 |
kill(kbd_pid, SIGKILL); |
| 453 |
kbd_pid = -1; |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
#else /* !PIPEPROCS */ |
| 458 |
|
| 459 |
#include <sys/time.h> |
| 460 |
#include "select.h" |
| 461 |
|
| 462 |
# ifdef USE_OPENPTY /* modern BSDs have openpty(3) */ |
| 463 |
# ifdef HAVE_LIBUTIL_H /* but disagree about header! */ |
| 464 |
# include <libutil.h> |
| 465 |
# else |
| 466 |
# ifdef HAVE_PTY_H |
| 467 |
# include <pty.h> |
| 468 |
# else |
| 469 |
# include <util.h> |
| 470 |
# endif |
| 471 |
# endif |
| 472 |
# endif |
| 473 |
|
| 474 |
# ifdef SVR4_PTYS |
| 475 |
# include <stdlib.h> /* for grantpt and unlockpt, at least in Solaris 2.3 */ |
| 476 |
# if _XOPEN_SOURCE >= 500 |
| 477 |
/* Linux/glibc no longer pretends to support STREAMS (XSR) (2008) */ |
| 478 |
# if defined(_XOPEN_STREAMS) && _XOPEN_STREAMS != -1 |
| 479 |
# include <stropts.h> |
| 480 |
# endif |
| 481 |
# else |
| 482 |
# include <sys/stropts.h> |
| 483 |
# endif |
| 484 |
extern char *ptsname(int /*filedes*/); /* get name of slave */ |
| 485 |
# endif |
| 486 |
|
| 487 |
# ifdef IRIX_PTYS |
| 488 |
# include <sys/stat.h> |
| 489 |
# endif |
| 490 |
|
| 491 |
void |
| 492 |
read_pty_proc(int fd) |
| 493 |
{ |
| 494 |
Process p; |
| 495 |
int n; |
| 496 |
char ibuf[1024+1]; /* NOTE: room for added NUL */ |
| 497 |
|
| 498 |
for (p = procs; ; p = p->p_next) { |
| 499 |
if (p == NULL) { |
| 500 |
writef("\riproc: unknown fd %d", fd); |
| 501 |
return; |
| 502 |
} |
| 503 |
if (p->p_fd == fd) |
| 504 |
break; |
| 505 |
} |
| 506 |
|
| 507 |
n = read(fd, ibuf, sizeof ibuf - 1); |
| 508 |
jdbg("pty read %d returned %d errno %d from %s\n", fd, n, errno, |
| 509 |
p->p_name); |
| 510 |
if (n <= 0) { |
| 511 |
if (n < 0) { |
| 512 |
if (errno == EIO || RETRY_ERRNO(errno)) { |
| 513 |
/* |
| 514 |
* ??? On some systems, pty reads fail |
| 515 |
* initially, for reasons that are not clear to |
| 516 |
* me (DHR). This code forgives these specific |
| 517 |
* kinds of failure until the process leaves the |
| 518 |
* NEW state. We hope that this does not cause |
| 519 |
* a long busy wait. |
| 520 |
*/ |
| 521 |
if (p->p_io_state == IO_NEW) { |
| 522 |
time_t now = time(NULL); |
| 523 |
jdbg("IO_NEW err %d start %D now %D\n", |
| 524 |
errno, (long)p->p_start,(long)now); |
| 525 |
if (now < p->p_start+MAX_BUSY_WAIT) |
| 526 |
return; |
| 527 |
} |
| 528 |
jdbg("treating error as EOF\n"); |
| 529 |
|
| 530 |
/* We get here if the i-proc closes stdout |
| 531 |
* before exiting (eg. Bourne Shell), |
| 532 |
* so we treat it as a simple EOF. |
| 533 |
*/ |
| 534 |
} else { |
| 535 |
/* true I/O error */ |
| 536 |
swritef(ibuf, sizeof(ibuf), |
| 537 |
"\n[pty read error: %s]\n", strerror(errno)); |
| 538 |
proc_rec(p, ibuf, strlen(ibuf)); |
| 539 |
/* now treat as EOF... */ |
| 540 |
} |
| 541 |
} /* else, EOF received */ |
| 542 |
proc_close(p); |
| 543 |
} else { |
| 544 |
if (p->p_io_state == IO_NEW) { |
| 545 |
p->p_io_state = IO_RUNNING; |
| 546 |
UpdModLine = YES; |
| 547 |
} |
| 548 |
proc_rec(p, ibuf, (size_t)n); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
void |
| 553 |
ProcCont(void) |
| 554 |
{ |
| 555 |
Process p = curbuf->b_process; |
| 556 |
|
| 557 |
jdbg("ProcCont pid %D %s\n", (long)(p->p_pid), p->p_name); |
| 558 |
if (proc_kill(p, SIGCONT) && p->p_child_state == C_STOPPED) { |
| 559 |
p->p_child_state = C_LIVE; |
| 560 |
UpdModLine = YES; |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
/* Sending non-character info through "keyboard" |
| 565 |
* |
| 566 |
* There are two kinds of "out of band" signals we wish to send |
| 567 |
* through the pty: signals (such as SIGINT) and EOF. On top |
| 568 |
* of that, there is a kind of out of band signal we wish not to |
| 569 |
* send: ERASE and KILL. Unfortunately, there are a number of ways |
| 570 |
* in which our environment may vary. Two ioctls are useful: |
| 571 |
* TIOCREMOTE and TIOCSIGNAL/TIOCSIG. |
| 572 |
* |
| 573 |
* |
| 574 |
* TIOCREMOTE: |
| 575 |
* |
| 576 |
* Some systems support the TIOCREMOTE ioctl. With this ioctl, |
| 577 |
* the "input editing" is suppressed on the characters sent |
| 578 |
* to the slave. Input editing involves interpreting ERASE, |
| 579 |
* KILL, INTERRUPT, QUIT, EOF, etc. characters. It is best |
| 580 |
* to run this way since JOVE already does input editing. |
| 581 |
* |
| 582 |
* The scant documentation on TIOCREMOTE in Solaris2.3 suggests |
| 583 |
* that the third argument to the TIOCREMOTE ioctl ought to be |
| 584 |
* an integer (but a pointer to the integer works). The SunOS4 |
| 585 |
* ldterm(4m) manpage says that the third argument is to be a |
| 586 |
* pointer to an integer. At the moment, we only use the pointer |
| 587 |
* form. |
| 588 |
* |
| 589 |
* Although IRIX defines TIOCREMOTE, it does not seem to work. |
| 590 |
* This may well be true of other systems, so as a safety, |
| 591 |
* we support "NO_TIOCREMOTE" as a feature deselect macro |
| 592 |
* for systems that define TIOCREMOTE but are broken. |
| 593 |
* |
| 594 |
* Under BSDI's BSD/386 v1.[01], the TIOCREMOTE ioctls appear to fail |
| 595 |
* for send_xc (without an error indication), but work otherwise. This |
| 596 |
* only affects dstop-process, so it is probably best not to define |
| 597 |
* NO_TIOCREMOTE. Even if we do define NO_TIOCREMOTE, the dstop-process |
| 598 |
* may require the user to do a process-newline (apparently, if the tty |
| 599 |
* input is being canonicalized). How odd. |
| 600 |
* |
| 601 |
* |
| 602 |
* TIOCSIGNAL/TIOCSIG: |
| 603 |
* |
| 604 |
* Some systems support the TIOCSIGNAL ioctl. With this ioctl, |
| 605 |
* a signal can be sent through the pty. Recent (4.3 or later?) |
| 606 |
* BSD systems seem to provide a TIOCSIG ioctl which is similar. |
| 607 |
* |
| 608 |
* The TIOCSIGNAL ioctl is not well documented and seems to be |
| 609 |
* broken on at least IRIX 5.3. Again, we provide "NO_TIOCSIGNAL" |
| 610 |
* to prevent using it on systems that define it but are broken. |
| 611 |
* |
| 612 |
* Another mystery: systems vary on how the signal number should be |
| 613 |
* passed in the TIOCSIGNAL ioctl. SunOS4 requires that the third |
| 614 |
* argument be a pointer to an integer, the signal number. Vanilla |
| 615 |
* SVR4 requires that the third argument be a an integer, the signal |
| 616 |
* number. Solaris2.3 accepts either. The SunOS 5.3 STREAMS |
| 617 |
* Programmer's Guide says that the third argument is an int. We have |
| 618 |
* not figured out, for an arbitrary system with TIOCSIGNAL, how to |
| 619 |
* tell which form the third argument should take. For now, it is |
| 620 |
* conditionalized on SVR4_PTYS. |
| 621 |
* |
| 622 |
* If TIOCSIGNAL/TIOCSIG isn't supplied (perhaps from termios.h), we |
| 623 |
* cannot send signals to the child when TIOCREMOTE is on, so we just |
| 624 |
* turn it off for a moment. This is pretty dubious because the user |
| 625 |
* might have done an stty to change or disable the characters. |
| 626 |
* |
| 627 |
* We don't know how to implement dstop using TIOCSIGNAL/TIOCSIG, so |
| 628 |
* we use the dubious trick. |
| 629 |
*/ |
| 630 |
|
| 631 |
# if !defined(NO_TIOCREMOTE) && !defined(TIOCREMOTE) |
| 632 |
# define NO_TIOCREMOTE 1 |
| 633 |
# endif |
| 634 |
|
| 635 |
# if !defined(NO_TIOCSIGNAL) && !defined(TIOCSIGNAL) && !defined(TIOCSIG) |
| 636 |
# define NO_TIOCSIGNAL 1 |
| 637 |
# endif |
| 638 |
|
| 639 |
# ifdef NO_TIOCSIGNAL |
| 640 |
|
| 641 |
# define kbd_sig(sig, tch, sch) send_oxc(tch, sch) |
| 642 |
# define kbd_sig_ls(sig, tch, sch) send_oxc_ls(tch, sch) |
| 643 |
|
| 644 |
# else /* !NO_TIOCSIGNAL */ |
| 645 |
|
| 646 |
# define kbd_sig(sig, tch, sch) send_sig(sig) |
| 647 |
# define kbd_sig_ls(sig, tch, sch) kbd_sig(sig, tch, sch) |
| 648 |
|
| 649 |
private void |
| 650 |
send_sig(int sig) |
| 651 |
{ |
| 652 |
Process p; |
| 653 |
|
| 654 |
jdbg("send_sig %d\n", sig); |
| 655 |
if ((p = curbuf->b_process) == NULL || p->p_fd < 0) { |
| 656 |
complain("[No process]"); |
| 657 |
/* NOTREACHED */ |
| 658 |
} |
| 659 |
ToLast(); |
| 660 |
# ifdef TIOCSIG |
| 661 |
jdbg("TIOCSIG pid %d %s\n", p->p_pid, p->p_name); |
| 662 |
if (ioctl(p->p_fd, TIOCSIG, sig) < 0) { |
| 663 |
complain("TIOCSIG failed: %d %s", errno, strerror(errno)); |
| 664 |
/* NOTREACHED */ |
| 665 |
} |
| 666 |
# else /* !TIOCSIG */ |
| 667 |
# ifdef SVR4_PTYS |
| 668 |
jdbg("SVR4 TIOCSIGNAL pid %d %s\n", p->p_pid, p->p_name); |
| 669 |
if (ioctl(p->p_fd, TIOCSIGNAL, sig) < 0) { |
| 670 |
complain("TIOCSIGNAL failed: %d %s", errno, strerror(errno)); |
| 671 |
/* NOTREACHED */ |
| 672 |
} |
| 673 |
# else /* !SVR4_PTYS */ |
| 674 |
jdbg("TIOCSIGNAL pid %d %s\n", p->p_pid, p->p_name); |
| 675 |
if (ioctl(p->p_fd, TIOCSIGNAL, (void *) &sig) < 0) { |
| 676 |
complain("TIOCSIGNAL failed: %d %s", errno, strerror(errno)); |
| 677 |
/* NOTREACHED */ |
| 678 |
} |
| 679 |
# endif /* !SVR4_PTYS */ |
| 680 |
# endif /* !TIOCSIG */ |
| 681 |
} |
| 682 |
|
| 683 |
# endif /* !NO_TIOCSIGNAL */ |
| 684 |
|
| 685 |
# if defined(NO_TIOCREMOTE) || defined(NO_TIOCSIGNAL) || defined(VDSUSP) |
| 686 |
|
| 687 |
# define send_oxc(tch, sch) send_xc(sg[NO].c_cc[tch]) |
| 688 |
# define send_oxc_ls(tch, sch) send_oxc(tch, sch) |
| 689 |
|
| 690 |
private void |
| 691 |
send_xc(char c) |
| 692 |
{ |
| 693 |
Process p; |
| 694 |
|
| 695 |
if ((p = curbuf->b_process) == NULL || p->p_fd < 0) { |
| 696 |
complain("[No process]"); |
| 697 |
/* NOTREACHED */ |
| 698 |
} |
| 699 |
jdbg("send_xc 0x%x pid %d %s \n", c, p->p_pid, p->p_name); |
| 700 |
ToLast(); |
| 701 |
{ |
| 702 |
char buf[1+1]; /* NOTE: room for added NUL */ |
| 703 |
|
| 704 |
buf[0] = c; |
| 705 |
proc_rec(p, buf, (size_t)1); |
| 706 |
|
| 707 |
{ |
| 708 |
# ifndef NO_TIOCREMOTE |
| 709 |
int |
| 710 |
off = 0, |
| 711 |
on = 1; |
| 712 |
|
| 713 |
jdbg("TIOCREMOTE off pid %d %s\n", p->p_pid, p->p_name); |
| 714 |
while (ioctl(p->p_fd, TIOCREMOTE, &off) < 0) |
| 715 |
if (errno != EINTR) { |
| 716 |
complain("TIOCREMOTE OFF failed: %d %s", errno, strerror(errno)); |
| 717 |
/* NOTREACHED */ |
| 718 |
} |
| 719 |
# endif /* !NO_TIOCREMOTE */ |
| 720 |
for (;;) { |
| 721 |
switch (write(p->p_fd, &c, sizeof c)) { |
| 722 |
case -1: /* error: consider ERRNO */ |
| 723 |
if (errno == EINTR) |
| 724 |
continue; /* interrupted: try again */ |
| 725 |
|
| 726 |
complain("pty write of control failed: %d %s", errno, strerror(errno)); |
| 727 |
/* NOTREACHED */ |
| 728 |
case 0: /* nothing happened: try again */ |
| 729 |
continue; |
| 730 |
case 1: /* done */ |
| 731 |
break; |
| 732 |
} |
| 733 |
break; |
| 734 |
} |
| 735 |
# ifndef NO_TIOCREMOTE |
| 736 |
jdbg("TIOCREMOTE on pid %d %s\n", p->p_pid, p->p_name); |
| 737 |
while (ioctl(p->p_fd, TIOCREMOTE, &on) < 0) { |
| 738 |
if (errno != EINTR) { |
| 739 |
complain("TIOCREMOTE ON failed: %d %s", errno, strerror(errno)); |
| 740 |
/* NOTREACHED */ |
| 741 |
} |
| 742 |
} |
| 743 |
# endif /* !NO_TIOCREMOTE */ |
| 744 |
} |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
# endif /* defined(NO_TIOCREMOTE) || defined(NO_TIOCSIGNAL) */ |
| 749 |
|
| 750 |
void |
| 751 |
ProcEof(void) |
| 752 |
{ |
| 753 |
# ifdef NO_TIOCREMOTE |
| 754 |
/* we have to write a char */ |
| 755 |
send_oxc(VEOF, t_eofc); |
| 756 |
# else /* !NO_TIOCREMOTE */ |
| 757 |
/* write a zero-length record to signify EOF */ |
| 758 |
static char mess[] = "<EOF> "; /* NOTE: NUL will be overwritten */ |
| 759 |
Process p; |
| 760 |
|
| 761 |
if ((p = curbuf->b_process) == NULL || p->p_fd < 0) { |
| 762 |
complain("[No process]"); |
| 763 |
/* NOTREACHED */ |
| 764 |
} |
| 765 |
ToLast(); |
| 766 |
proc_rec(p, mess, sizeof(mess)-1); |
| 767 |
jdbg("ProcEof pid %d %s\n", p->p_pid, p->p_name); |
| 768 |
while (write(p->p_fd, mess, (size_t)0) < 0) { |
| 769 |
if (errno != EINTR) { |
| 770 |
complain("[error writing EOF to iproc: %d %s]", errno, strerror(errno)); |
| 771 |
/* NOTREACHED */ |
| 772 |
} |
| 773 |
} |
| 774 |
# endif /* !NO_TIOCREMOTE */ |
| 775 |
} |
| 776 |
|
| 777 |
void |
| 778 |
ProcInt(void) |
| 779 |
{ |
| 780 |
kbd_sig(SIGINT, VINTR, t_intrc); |
| 781 |
} |
| 782 |
|
| 783 |
void |
| 784 |
ProcQuit(void) |
| 785 |
{ |
| 786 |
kbd_sig(SIGQUIT, VQUIT, t_quitc); |
| 787 |
} |
| 788 |
|
| 789 |
void |
| 790 |
ProcStop(void) |
| 791 |
{ |
| 792 |
# if defined(VSUSP) |
| 793 |
kbd_sig_ls(SIGTSTP, VSUSP, t_suspc); |
| 794 |
# else |
| 795 |
complain("[stop-process not supported]"); |
| 796 |
/* NOTREACHED */ |
| 797 |
# endif |
| 798 |
} |
| 799 |
|
| 800 |
void |
| 801 |
ProcDStop(void) |
| 802 |
{ |
| 803 |
/* we don't know how to send a dstop via TIOCSIGNAL/TIOCSIG */ |
| 804 |
# if defined(VDSUSP) |
| 805 |
send_oxc_ls(VDSUSP, t_dsuspc); |
| 806 |
# else |
| 807 |
complain("[dstop-process not supported]"); |
| 808 |
/* NOTREACHED */ |
| 809 |
# endif |
| 810 |
} |
| 811 |
|
| 812 |
private void |
| 813 |
proc_close(Process p) |
| 814 |
{ |
| 815 |
jdbg("proc_close %d %s\n", p->p_pid, p->p_name); |
| 816 |
if (p->p_fd >= 0) { |
| 817 |
(void) close(p->p_fd); |
| 818 |
FD_CLR(p->p_fd, &global_fd); |
| 819 |
p->p_fd = -1; |
| 820 |
p->p_io_state = IO_EOFED; /* I/O is finished */ |
| 821 |
UpdModLine = YES; |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
private void |
| 826 |
proc_write(Process p, char *buf, size_t nbytes) |
| 827 |
{ |
| 828 |
jdbg("proc_write %d bytes to pid %d %s\n", nbytes, p->p_pid, p->p_name); |
| 829 |
if (p->p_fd >= 0) { |
| 830 |
fd_set mask; |
| 831 |
|
| 832 |
FD_ZERO(&mask); |
| 833 |
FD_SET(p->p_fd, &mask); |
| 834 |
while (write(p->p_fd, buf, nbytes) < 0) |
| 835 |
(void) select(p->p_fd + 1, (fd_set *)NULL, &mask, (fd_set *)NULL, |
| 836 |
(struct timeval *)NULL); |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
private void |
| 841 |
proc_strt(char *bufname, jbool clobber, const char *procname, ...) |
| 842 |
{ |
| 843 |
va_list ap; |
| 844 |
char *argv[32]; |
| 845 |
Window *owind = curwind; |
| 846 |
pid_t pid; |
| 847 |
Process newp; |
| 848 |
Buffer *newbuf; |
| 849 |
int ptyfd = -1; |
| 850 |
int slvptyfd = -1; |
| 851 |
char ttybuf[32]; |
| 852 |
struct termios sgt; |
| 853 |
|
| 854 |
# ifdef TIOCGWINSZ |
| 855 |
struct winsize win; |
| 856 |
# else |
| 857 |
# ifdef BTL_BLIT |
| 858 |
# include <sys/jioctl.h> |
| 859 |
struct jwinsize jwin; |
| 860 |
# endif |
| 861 |
# endif |
| 862 |
|
| 863 |
strcpy(ttybuf, "?"); /* to safely print when debugging */ |
| 864 |
untieDeadProcess(buf_exists(bufname)); |
| 865 |
isprocbuf(bufname); /* make sure BUFNAME is either nonexistant |
| 866 |
or is of type B_PROCESS */ |
| 867 |
va_start(ap, procname); |
| 868 |
make_argv(argv, ap); |
| 869 |
va_end(ap); |
| 870 |
if (access(argv[0], J_X_OK) != 0) { |
| 871 |
complain("[Couldn't access shell %s: %s]", argv[0], strerror(errno)); |
| 872 |
/* NOTREACHED */ |
| 873 |
} |
| 874 |
|
| 875 |
# ifdef IRIX_PTYS |
| 876 |
/* _getpty may fork off a child to execute mkpts to make a slave pty |
| 877 |
* in /dev (if we need more) and set the correct ownership and |
| 878 |
* modes on the slave pty. Since _getpty uses waitpid this works |
| 879 |
* fine with regard to our other children, but we do have to be |
| 880 |
* prepared to catch a SIGCHLD for an unknown child and ignore it ... |
| 881 |
*/ |
| 882 |
{ |
| 883 |
char *s = _getpty(&ptyfd, O_RDWR | O_NDELAY, 0600, 0); |
| 884 |
|
| 885 |
if (s == NULL) { |
| 886 |
message("[No pty from getpty!]"); |
| 887 |
goto fail; |
| 888 |
} |
| 889 |
(void)strcpy(ttybuf, s); |
| 890 |
jdbg("IRIX_PTYS _getpty ptyfd %d %s\n", ptyfd, ttybuf); |
| 891 |
} |
| 892 |
# endif /* IRIX_PTYS */ |
| 893 |
# ifdef SVR4_PTYS |
| 894 |
if ((ptyfd = open("/dev/ptmx", O_RDWR | O_BINARY)) < 0) { |
| 895 |
message("[No pty from /dev/ptmx!]"); |
| 896 |
goto fail; |
| 897 |
} |
| 898 |
jdbg("SVR4_PTYS ptmx ptyfd %d\n", ptyfd); |
| 899 |
# ifndef GRANTPT_BUG |
| 900 |
/* grantpt() seems to be implemented using a fork/exec. |
| 901 |
* This is done to allow grantpt do do priviledged things |
| 902 |
* via a setuid program. One consequence is that JOVE's |
| 903 |
* SIGCLD/SIGCHLD handler must not do a wait that would |
| 904 |
* reap the grantpt process. So much for library routines |
| 905 |
* being black boxes. Worse, this restriction is not documented. |
| 906 |
*/ |
| 907 |
if (grantpt(ptyfd) < 0) { |
| 908 |
message("[grantpt failed]"); |
| 909 |
goto fail; |
| 910 |
} |
| 911 |
if (unlockpt(ptyfd) < 0) { |
| 912 |
message("[unlockpt failed]"); |
| 913 |
goto fail; |
| 914 |
} |
| 915 |
jdbg("SVR4_PTYS grantpt and unlockpt ptyfd %d\n", ptyfd); |
| 916 |
# endif /* !GRANTPT_BUG */ |
| 917 |
#ifdef TIOCGPTPEER |
| 918 |
/* if we have TIOCGPTPEER (Linux post-2017), much better to use it! */ |
| 919 |
slvptyfd = ioctl(ptyfd, TIOCGPTPEER, O_RDWR | O_NOCTTY); |
| 920 |
if (slvptyfd == -1 && errno != EINVAL && errno != ENOTTY) { |
| 921 |
message("[ioctl TIOCGPTPEER failed]"); |
| 922 |
goto fail; |
| 923 |
} |
| 924 |
jdbg("SVR4_PTYS TIOCGPTPEER ptyfd %d slv %d\n", ptyfd, slvptyfd); |
| 925 |
#endif |
| 926 |
{ |
| 927 |
char *s = ptsname(ptyfd); |
| 928 |
|
| 929 |
if (s == NULL) { |
| 930 |
message("[ptsname failed]"); |
| 931 |
goto fail; |
| 932 |
} |
| 933 |
strcpy(ttybuf, s); |
| 934 |
jdbg("SVR4_PTYS ptyfd %d %s\n", ptyfd, ttybuf); |
| 935 |
} |
| 936 |
# ifdef TIOCFLUSH |
| 937 |
jdbg("TIOCFLUSH ptyfd %d\n", ptyfd); |
| 938 |
(void) ioctl(ptyfd, TIOCFLUSH, NULL); /* ??? why? */ |
| 939 |
# endif |
| 940 |
# endif /* SVR4_PTYS */ |
| 941 |
# ifdef BSD_PTYS |
| 942 |
# ifdef USE_OPENPTY |
| 943 |
if (openpty(&ptyfd, &slvptyfd, ttybuf, NULL, NULL) < 0) |
| 944 |
{ |
| 945 |
message("[No pty from openpty!]"); |
| 946 |
goto fail; |
| 947 |
} |
| 948 |
jdbg("openpty ptyfd %d slv %d %s\n", ptyfd, slvptyfd, ttybuf); |
| 949 |
# else /* !USE_OPENPTY */ |
| 950 |
{ |
| 951 |
const char *s; |
| 952 |
|
| 953 |
for (s = "pqrs"; ptyfd<0; s++) { |
| 954 |
const char *t; |
| 955 |
|
| 956 |
if (*s == '\0') { |
| 957 |
message("[Out of ptys in /dev/pty*!]"); |
| 958 |
goto fail; |
| 959 |
} |
| 960 |
for (t = "0123456789abcdef"; *t; t++) { |
| 961 |
swritef(ttybuf, sizeof(ttybuf), "/dev/pty%c%c", *s, *t); |
| 962 |
jdbg("trying pty %s\n", ttybuf); |
| 963 |
if ((ptyfd = open(ttybuf, O_RDWR | O_BINARY)) >= 0) { |
| 964 |
ttybuf[5] = 't'; /* pty => tty */ |
| 965 |
/* Make sure other end is available too */ |
| 966 |
slvptyfd = open(ttybuf, O_RDWR | O_BINARY); |
| 967 |
if (slvptyfd > 0) |
| 968 |
break; /* it worked: use this one */ |
| 969 |
|
| 970 |
/* can't open, so give up on this pty */ |
| 971 |
(void) close(ptyfd); |
| 972 |
ptyfd = slvptyfd = -1; |
| 973 |
} |
| 974 |
} |
| 975 |
} |
| 976 |
} |
| 977 |
# endif /* !USE_OPENPTY */ |
| 978 |
# endif /* BSD_PTYS */ |
| 979 |
/* Check that we can write to the pty, else things will fail in the |
| 980 |
* child, where they're harder to detect. This will not work with |
| 981 |
* GRANTPT_BUG because the grantpt and unlockpt have not been done yet. |
| 982 |
*/ |
| 983 |
# ifndef GRANTPT_BUG |
| 984 |
if (access(ttybuf, R_OK | W_OK) != 0) { |
| 985 |
s_mess("[Couldn't access %s: %s]", ttybuf, strerror(errno)); |
| 986 |
goto fail; |
| 987 |
} |
| 988 |
jdbg("can access pty %s\n", ttybuf); |
| 989 |
# endif /* !GRANTPT_BUG */ |
| 990 |
|
| 991 |
# ifdef TIOCGWINSZ |
| 992 |
jdbg("TIOCGWINSZ %s\n", ttybuf); |
| 993 |
(void) ioctl(0, TIOCGWINSZ, &win); |
| 994 |
# else |
| 995 |
# ifdef BTL_BLIT |
| 996 |
jdbg("JWINSIZE %s\n", ttybuf); |
| 997 |
(void) ioctl(0, JWINSIZE, &jwin); |
| 998 |
# endif /* BTL_BLIT */ |
| 999 |
# endif |
| 1000 |
|
| 1001 |
jdbg("before fork, ptyfd %d slv %d buf %s\n", ptyfd, slvptyfd, ttybuf); |
| 1002 |
switch (pid = fork()) { |
| 1003 |
case -1: |
| 1004 |
/* fork failed */ |
| 1005 |
s_mess("[Fork failed! %s]", strerror(errno)); |
| 1006 |
goto fail; |
| 1007 |
|
| 1008 |
case 0: |
| 1009 |
/* child process */ |
| 1010 |
|
| 1011 |
# ifdef GRANTPT_BUG |
| 1012 |
/* grantpt() seems to be implemented using a fork/exec. |
| 1013 |
* This is done to allow grantpt do do priviledged things |
| 1014 |
* via a setuid program. One consequence is that JOVE's |
| 1015 |
* SIGCLD/SIGCHLD handler must not do a wait that would |
| 1016 |
* reap the grantpt process. So much for library routines |
| 1017 |
* being black boxes. Worse, this restriction is not documented. |
| 1018 |
* |
| 1019 |
* Worse still, at least Solaris 2.0 and SVR4.0 appear to have |
| 1020 |
* a bug: the wait is not restarted once interrupted. If the wait |
| 1021 |
* is interrupted, (1) the grantpt will return a failure status |
| 1022 |
* and (2) the process will not be reaped -- it will remain a |
| 1023 |
* zombie until JOVE exits or happens to reap it accidentally. |
| 1024 |
* Any interrupt could cause the premature end of the wait, |
| 1025 |
* but SIGCHLD (actually, SIGCLD) is the most probable. |
| 1026 |
* |
| 1027 |
* To dodge these bullets, we moved the grantpt into the child |
| 1028 |
* where we can turn off signal handling. Too bad this prevents |
| 1029 |
* us giving good diagnostics for grantpt and unlockpt. |
| 1030 |
* I hope I've found all the signals caught everywhere else. |
| 1031 |
*/ |
| 1032 |
jdbg("child grantpt bug, ptyfd %d slv %d buf %s\n", ptyfd, slvptyfd, ttybuf); |
| 1033 |
(void) setsighandler(SIGCHLD, SIG_DFL); |
| 1034 |
(void) setsighandler(SIGWINCH, SIG_DFL); |
| 1035 |
(void) setsighandler(SIGALRM, SIG_DFL); |
| 1036 |
(void) setsighandler(SIGINT, SIG_DFL); |
| 1037 |
/* (void) setsighandler(SIGQUIT, SIG_DFL); */ /* dead anyway */ |
| 1038 |
/* (void) setsighandler(SIGHUP, SIG_DFL); */ /* dead anyway */ |
| 1039 |
/* (void) setsighandler(SIGTERM, SIG_DFL); */ /* no longer used */ |
| 1040 |
/* (void) setsighandler(SIGBUS, SIG_DFL); */ /* dead anyway */ |
| 1041 |
/* (void) setsighandler(SIGSEGV, SIG_DFL); */ /* dead anyway */ |
| 1042 |
/* (void) setsighandler(SIGPIPE, SIG_DFL); */ /* dead anyway */ |
| 1043 |
|
| 1044 |
if (grantpt(ptyfd) < 0) { |
| 1045 |
_exit(errno + 1); |
| 1046 |
} |
| 1047 |
|
| 1048 |
if (unlockpt(ptyfd) < 0) { |
| 1049 |
_exit(errno + 1); |
| 1050 |
} |
| 1051 |
# endif /* GRANTPT_BUG */ |
| 1052 |
jcloseall(); |
| 1053 |
(void) close(0); |
| 1054 |
(void) close(1); |
| 1055 |
(void) close(2); |
| 1056 |
|
| 1057 |
jdbg("child setsid %s\n", ttybuf); |
| 1058 |
setsid(); |
| 1059 |
jdbg("child ptyfd %d slv %d buf %s\n", ptyfd, slvptyfd, ttybuf); |
| 1060 |
if (slvptyfd < 0) { |
| 1061 |
if ((slvptyfd = open(ttybuf, O_RDWR | O_BINARY)) != 0) |
| 1062 |
_exit(errno+1); |
| 1063 |
jdbg("child slv slv %d\n", slvptyfd); |
| 1064 |
} /* else use what we got from openpty */ |
| 1065 |
(void) dup2(slvptyfd, 1); |
| 1066 |
(void) dup2(slvptyfd, 2); |
| 1067 |
if (slvptyfd != 0) { |
| 1068 |
(void) dup2(slvptyfd, 0); |
| 1069 |
jdbg("child closing slv %d %s\n", slvptyfd, ttybuf); |
| 1070 |
(void) close(slvptyfd); /* safe to close now that std* are open */ |
| 1071 |
} |
| 1072 |
jdbg("child std 0, 1, 2 duped\n"); |
| 1073 |
# ifdef SVR4_PTYS |
| 1074 |
# ifdef I_PUSH /* LINUX/glibc no longer even pretends to support this (2008) */ |
| 1075 |
jdbg("child SVR4_PTYS I_PUSH ptem ldterm ttcompat %s\n", ttybuf); |
| 1076 |
(void) ioctl(0, I_PUSH, "ptem"); |
| 1077 |
(void) ioctl(0, I_PUSH, "ldterm"); |
| 1078 |
(void) ioctl(0, I_PUSH, "ttcompat"); |
| 1079 |
# endif |
| 1080 |
# endif |
| 1081 |
|
| 1082 |
# ifdef TIOCSCTTY |
| 1083 |
/* Make this controling tty. |
| 1084 |
* This is needed by OSF. It may be needed by BSDPOSIX systems. |
| 1085 |
* It should not hurt on any system that defines TIOCSCTTY. |
| 1086 |
* On Solaris (OpenIndiana 2019.11), the ioctl fails with errno 25 (ENOTTY) |
| 1087 |
* but that seems harmless, so we just log but ignore error returns |
| 1088 |
* (as used to be the case before 4.16.0.31!) |
| 1089 |
*/ |
| 1090 |
jdbg("child TIOCSTTY %s\n", ttybuf); |
| 1091 |
if (ioctl(0, TIOCSCTTY) < 0) { |
| 1092 |
jdbg("TIOCSCTTY failed errno %d %s\n", errno, strerror(errno)); |
| 1093 |
} |
| 1094 |
# endif |
| 1095 |
|
| 1096 |
# ifndef NO_TIOCREMOTE |
| 1097 |
/* The TIOCREMOTE ioctl prevents the pty code from doing |
| 1098 |
* input editing on characters from the master. In particular |
| 1099 |
* we don't want ERASE, KILL, INTERRUPT, QUIT, etc. characters |
| 1100 |
* interpreted. |
| 1101 |
* On SVR4, this must be done *after* the slave side is set up. |
| 1102 |
* The easiest way to ensure this is to put it here, in the child |
| 1103 |
* after the slave is opened and has the STREAMS modules pushed, |
| 1104 |
* and before the master is closed. |
| 1105 |
*/ |
| 1106 |
{ |
| 1107 |
int on = 1; |
| 1108 |
|
| 1109 |
jdbg("child TIOCREMOTE on %d %s\n", ptyfd, ttybuf); |
| 1110 |
if (ioctl(ptyfd, TIOCREMOTE, &on) < 0) |
| 1111 |
_exit(errno+1); /* no good way to signal user */ |
| 1112 |
} |
| 1113 |
# endif |
| 1114 |
jdbg("child closing %d %s\n", ptyfd, ttybuf); |
| 1115 |
close(ptyfd); |
| 1116 |
|
| 1117 |
# ifdef TIOCGWINSZ |
| 1118 |
jdbg("child TIOCSWINSZ %s\n", ttybuf); |
| 1119 |
win.ws_row = curwind->w_height; |
| 1120 |
(void) ioctl(0, TIOCSWINSZ, &win); |
| 1121 |
# else /* !TIOCGWINSZ */ |
| 1122 |
# ifdef BTL_BLIT |
| 1123 |
jdbg("child JSWINSIZE %s\n", ttybuf); |
| 1124 |
jwin.bytesy = curwind->w_height; |
| 1125 |
(void) ioctl(0, JSWINSIZE, &jwin); |
| 1126 |
# endif |
| 1127 |
# endif /* !TIOCGWINSZ */ |
| 1128 |
|
| 1129 |
jdbg("child set termio(s)%s\n", ttybuf); |
| 1130 |
sgt = sg[NO]; |
| 1131 |
sgt.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | ICRNL |
| 1132 |
# ifdef IXANY /* not in QNX */ |
| 1133 |
| IXANY |
| 1134 |
# endif |
| 1135 |
| IXON | IXOFF); |
| 1136 |
sgt.c_lflag &= ~(ECHO); |
| 1137 |
sgt.c_oflag &= ~(ONLCR | TABDLY); |
| 1138 |
jdbg("child TCSADRAIN %s\n", ttybuf); |
| 1139 |
do {} while (tcsetattr(0, TCSADRAIN, &sgt) < 0 && errno == EINTR); |
| 1140 |
jdbg("child newpg %s\n", ttybuf); |
| 1141 |
NEWPG(); |
| 1142 |
{ |
| 1143 |
int i = getpid(); |
| 1144 |
|
| 1145 |
# ifdef POSIX_PROCS |
| 1146 |
jdbg("child tcsetpgrp %d %s\n", i, ttybuf); |
| 1147 |
tcsetpgrp(0, getpid()); |
| 1148 |
# else /* !POSIX_PROCS */ |
| 1149 |
jdbg("child TIOCSPGRP %d %s\n", i, ttybuf); |
| 1150 |
(void) ioctl(0, TIOCSPGRP, &i); |
| 1151 |
# endif /* POSIX_PROCS */ |
| 1152 |
} |
| 1153 |
|
| 1154 |
set_process_env(); |
| 1155 |
jdbg("child ready to exec %s %s\n", argv[0], argv[1]); |
| 1156 |
execvp(argv[0], &argv[1]); |
| 1157 |
raw_complain("execvp failed! %s", strerror(errno)); |
| 1158 |
_exit(errno + 1); |
| 1159 |
} |
| 1160 |
|
| 1161 |
# ifdef BSD_PTYS |
| 1162 |
jdbg("closing slv %d %s\n", slvptyfd, ttybuf); |
| 1163 |
close(slvptyfd); |
| 1164 |
# endif /* BSD_PTYS */ |
| 1165 |
|
| 1166 |
newp = (Process) emalloc(sizeof *newp); |
| 1167 |
|
| 1168 |
# ifdef O_NDELAY |
| 1169 |
jdbg("O_NDELAY %d %s\n", ptyfd, ttybuf); |
| 1170 |
fcntl(ptyfd, F_SETFL, O_NDELAY); |
| 1171 |
# endif |
| 1172 |
# ifdef O_NONBLOCK |
| 1173 |
jdbg("O_NONBLOCK %d %s\n", ptyfd, ttybuf); |
| 1174 |
fcntl(ptyfd, F_SETFL, O_NONBLOCK); |
| 1175 |
# endif |
| 1176 |
(void) time(&(newp->p_start)); |
| 1177 |
jdbg("start pid %d fd %d %s %D\n", pid, ptyfd, ttybuf, |
| 1178 |
(long) newp->p_start); |
| 1179 |
newp->p_fd = ptyfd; |
| 1180 |
newp->p_pid = pid; |
| 1181 |
|
| 1182 |
newbuf = do_select((Window *)NULL, bufname); |
| 1183 |
newbuf->b_type = B_PROCESS; |
| 1184 |
newp->p_buffer = newbuf; |
| 1185 |
newbuf->b_process = newp; /* sorta circular, eh? */ |
| 1186 |
pop_wind(bufname, clobber, B_PROCESS); |
| 1187 |
/* Pop_wind() after everything is set up; important! |
| 1188 |
* Bindings won't work right unless newbuf->b_process is already |
| 1189 |
* set up BEFORE NEWBUF is first SetBuf()'d. |
| 1190 |
*/ |
| 1191 |
ToLast(); |
| 1192 |
if (!bolp()) |
| 1193 |
LineInsert(1); |
| 1194 |
|
| 1195 |
newp->p_name = copystr(procname); |
| 1196 |
newp->p_io_state = IO_NEW; |
| 1197 |
newp->p_child_state = C_LIVE; |
| 1198 |
newp->p_mark = MakeMark(curline, curchar); |
| 1199 |
|
| 1200 |
newp->p_next = procs; |
| 1201 |
procs = newp; |
| 1202 |
FD_SET(newp->p_fd, &global_fd); |
| 1203 |
if (global_maxfd <= newp->p_fd) |
| 1204 |
global_maxfd = newp->p_fd + 1; |
| 1205 |
SetWind(owind); |
| 1206 |
return; |
| 1207 |
|
| 1208 |
fail: |
| 1209 |
jdbg("pty open fail %d %d %s\n", ptyfd, slvptyfd, ttybuf); |
| 1210 |
if (ptyfd >= 0) |
| 1211 |
close(ptyfd); |
| 1212 |
# ifdef BSD_PTYS |
| 1213 |
if (slvptyfd >= 0) |
| 1214 |
close(slvptyfd); |
| 1215 |
# endif /* BSD_PTYS */ |
| 1216 |
} |
| 1217 |
|
| 1218 |
/* NOTE 1: SIGCHLD is an asynchronous signal. To safely handle it, |
| 1219 |
* the sigchld_handler simply sets a flag requesting later processing. |
| 1220 |
* reap_procs is called synchronously to do the actual work. |
| 1221 |
* |
| 1222 |
* NOTE 2: SIGCHLD is "level triggered" on some systems (HPUX, IRIX, ...). |
| 1223 |
* If the signal signal handler is re-established before the child is reaped, |
| 1224 |
* another signal will be generated immediately. For this reason, we |
| 1225 |
* defer the re-establishment until reap_procs is done. |
| 1226 |
*/ |
| 1227 |
|
| 1228 |
volatile jbool |
| 1229 |
procs_to_reap = NO; |
| 1230 |
|
| 1231 |
/*ARGSUSED*/ |
| 1232 |
SIGRESTYPE |
| 1233 |
sigchld_handler(int UNUSED(junk)) |
| 1234 |
{ |
| 1235 |
procs_to_reap = YES; |
| 1236 |
return SIGRESVALUE; |
| 1237 |
} |
| 1238 |
|
| 1239 |
void |
| 1240 |
reap_procs(void) |
| 1241 |
{ |
| 1242 |
wait_status_t |
| 1243 |
w; |
| 1244 |
pid_t pid; |
| 1245 |
|
| 1246 |
for (;;) { |
| 1247 |
pid = wait_opt(&w, (WNOHANG | WUNTRACED)); |
| 1248 |
if (pid <= 0) { |
| 1249 |
if (procs_to_reap) { |
| 1250 |
resetsighandler(SIGCHLD, sigchld_handler); |
| 1251 |
procs_to_reap = NO; |
| 1252 |
/* go around once more to avoid window of vulnerability */ |
| 1253 |
} else { |
| 1254 |
break; |
| 1255 |
} |
| 1256 |
} else { |
| 1257 |
kill_off(pid, w); |
| 1258 |
} |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
void |
| 1263 |
closeiprocs(void) |
| 1264 |
{ |
| 1265 |
Process p; |
| 1266 |
|
| 1267 |
for (p=procs; p!=NULL; p=p->p_next) |
| 1268 |
if (p->p_fd >= 0) |
| 1269 |
close(p->p_fd); |
| 1270 |
} |
| 1271 |
|
| 1272 |
#endif /* !PIPEPROCS */ |
| 1273 |
|
| 1274 |
/* This is for the shell window. Supports sh, csh and ksh. */ |
| 1275 |
char proc_prompt[128] = "^[^%$#]*[%$#] "; /* VAR: process prompt */ |
| 1276 |
|
| 1277 |
const char * |
| 1278 |
pstate(Process p) |
| 1279 |
{ |
| 1280 |
static const char *const ios_name[] = { "New", "Running", "EOFed" }; |
| 1281 |
|
| 1282 |
if (p == NULL) |
| 1283 |
return "No process"; |
| 1284 |
|
| 1285 |
/* Try *not* to report the whole cross-product of child and IO states. */ |
| 1286 |
switch (p->p_child_state) { |
| 1287 |
case C_LIVE: |
| 1288 |
return ios_name[p->p_io_state]; |
| 1289 |
case C_STOPPED: |
| 1290 |
return io_eofed(p)? "Stopped; EOFed" : "Stopped"; |
| 1291 |
case C_EXITED: |
| 1292 |
if (io_eofed(p) && p->p_reason == 0) |
| 1293 |
return "Done"; |
| 1294 |
/* FALLTHROUGH */ |
| 1295 |
default: |
| 1296 |
return sprint("%s %d%s", |
| 1297 |
(p->p_child_state == C_EXITED? "Exited" : "Killed"), |
| 1298 |
p->p_reason, |
| 1299 |
(io_eofed(p)? "" : "; not EOFed")); |
| 1300 |
} |
| 1301 |
} |
| 1302 |
|
| 1303 |
jbool |
| 1304 |
KillProcs(void) |
| 1305 |
{ |
| 1306 |
Process p; |
| 1307 |
jbool asked = NO; |
| 1308 |
|
| 1309 |
for (p = procs; p != NULL; p = p->p_next) { |
| 1310 |
if (!dead(p)) { |
| 1311 |
if (!asked) { |
| 1312 |
if (!yes_or_no_p("Some interactive processes are still running; leave anyway? ")) |
| 1313 |
return NO; /* processes not killed */ |
| 1314 |
asked = YES; |
| 1315 |
} |
| 1316 |
if (!child_dead(p)) |
| 1317 |
(void) proc_kill(p, SIGKILL); |
| 1318 |
proc_close(p); |
| 1319 |
} |
| 1320 |
} |
| 1321 |
return YES; /* processes killed */ |
| 1322 |
} |
| 1323 |
|
| 1324 |
/* VAR: dbx-mode parse string */ |
| 1325 |
char dbx_parse_fmt[128] = "line \\([0-9]*\\) in \\{file\\|\\} *\"\\([^\"]*\\)\""; |
| 1326 |
|
| 1327 |
private void |
| 1328 |
watch_input(Mark *m) |
| 1329 |
{ |
| 1330 |
Bufpos save; |
| 1331 |
char fname[FILESIZE], |
| 1332 |
lineno[FILESIZE]; |
| 1333 |
long lnum; |
| 1334 |
Window *savew = curwind; |
| 1335 |
Buffer *buf; |
| 1336 |
|
| 1337 |
DOTsave(&save); |
| 1338 |
ToMark(m); |
| 1339 |
if (dosearch(dbx_parse_fmt, FORWARD, YES) != NULL) { |
| 1340 |
get_FL_info(fname, lineno); |
| 1341 |
buf = do_find((Window *)NULL, fname, YES, YES); |
| 1342 |
pop_wind(buf->b_name, NO, -1); |
| 1343 |
lnum = atol(lineno); |
| 1344 |
SetLine(next_line(buf->b_first, lnum - 1)); |
| 1345 |
SetWind(savew); |
| 1346 |
} |
| 1347 |
SetDot(&save); |
| 1348 |
} |
| 1349 |
|
| 1350 |
/* Process receive: receives the characters in buf, and appends them to |
| 1351 |
* the buffer associated with p. |
| 1352 |
*/ |
| 1353 |
private void |
| 1354 |
proc_rec(Process p, char *buf, size_t len) |
| 1355 |
{ |
| 1356 |
Buffer *saveb = curbuf; |
| 1357 |
Window *w; |
| 1358 |
Mark *savepoint; |
| 1359 |
jbool sameplace, |
| 1360 |
do_disp; |
| 1361 |
|
| 1362 |
if (curwind->w_bufp == p->p_buffer) |
| 1363 |
w = curwind; |
| 1364 |
else |
| 1365 |
w = windbp(p->p_buffer); /* Is this window visible? */ |
| 1366 |
do_disp = w != NULL && in_window(w, p->p_mark->m_line) != -1; |
| 1367 |
SetBuf(p->p_buffer); |
| 1368 |
savepoint = MakeMark(curline, curchar); |
| 1369 |
ToMark(p->p_mark); /* where output last stopped */ |
| 1370 |
sameplace = savepoint->m_line == curline && savepoint->m_char == curchar; |
| 1371 |
|
| 1372 |
/* insert contents of buffer, carefully translating NUL to ^@ */ |
| 1373 |
buf[len] = '\0'; /* NUL-terminate buffer */ |
| 1374 |
while (len != 0) { |
| 1375 |
if (*buf == '\0') { |
| 1376 |
ins_str_wrap("^@", YES, WrapProcessLines ? CO-1 : LBSIZE-1); |
| 1377 |
buf++; |
| 1378 |
len--; |
| 1379 |
} else { |
| 1380 |
size_t sublen = strlen(buf); |
| 1381 |
|
| 1382 |
ins_str_wrap(buf, YES, WrapProcessLines ? CO-1 : LBSIZE-1); |
| 1383 |
buf += sublen; |
| 1384 |
len -= sublen; |
| 1385 |
} |
| 1386 |
} |
| 1387 |
|
| 1388 |
if (do_disp && BufMinorMode(p->p_buffer, DbxMode)) |
| 1389 |
watch_input(p->p_mark); |
| 1390 |
MarkSet(p->p_mark, curline, curchar); |
| 1391 |
if (!sameplace) |
| 1392 |
ToMark(savepoint); /* back to where we were */ |
| 1393 |
DelMark(savepoint); |
| 1394 |
/* redisplay now, instead of right after the ins_str, so that |
| 1395 |
* we don't get a bouncing effect if point is not the same as |
| 1396 |
* the process output position |
| 1397 |
*/ |
| 1398 |
if (do_disp) { |
| 1399 |
w->w_line = curline; |
| 1400 |
w->w_char = curchar; |
| 1401 |
redisplay(); |
| 1402 |
} |
| 1403 |
SetBuf(saveb); |
| 1404 |
} |
| 1405 |
|
| 1406 |
private jbool |
| 1407 |
proc_kill(Process p, int sig) |
| 1408 |
{ |
| 1409 |
if (p == NULL) { |
| 1410 |
complain("[no process]"); |
| 1411 |
/* NOTREACHED */ |
| 1412 |
} |
| 1413 |
if (!child_dead(p)) { |
| 1414 |
if (killpg(p->p_pid, sig) != -1) |
| 1415 |
return YES; |
| 1416 |
|
| 1417 |
s_mess("Cannot kill %s!", proc_bufname(p)); |
| 1418 |
} |
| 1419 |
return NO; |
| 1420 |
} |
| 1421 |
|
| 1422 |
/* Free process CHILD. Do all the necessary cleaning up (closing fd's, |
| 1423 |
* etc.). |
| 1424 |
*/ |
| 1425 |
private void |
| 1426 |
free_proc(Process child) |
| 1427 |
{ |
| 1428 |
Process p, |
| 1429 |
prev = NULL; |
| 1430 |
|
| 1431 |
if (!dead(child)) |
| 1432 |
return; |
| 1433 |
|
| 1434 |
untieDeadProcess(child->p_buffer); |
| 1435 |
|
| 1436 |
for (p = procs; p != child; prev = p, p = p->p_next) |
| 1437 |
; |
| 1438 |
|
| 1439 |
if (prev == NULL) |
| 1440 |
procs = child->p_next; |
| 1441 |
else |
| 1442 |
prev->p_next = child->p_next; |
| 1443 |
proc_close(child); /* if not already closed */ |
| 1444 |
|
| 1445 |
free(child->p_name); |
| 1446 |
free(child); |
| 1447 |
} |
| 1448 |
|
| 1449 |
void |
| 1450 |
untieDeadProcess(Buffer *b) |
| 1451 |
{ |
| 1452 |
if (b != NULL) { |
| 1453 |
Process p = b->b_process; |
| 1454 |
|
| 1455 |
if (p != NULL) { |
| 1456 |
Buffer *old = curbuf; |
| 1457 |
|
| 1458 |
if (!dead(p)) { |
| 1459 |
complain("Process %s, attached to %b, is %s.", |
| 1460 |
proc_cmd(p), b, pstate(p)); |
| 1461 |
/* NOTREACHED */ |
| 1462 |
} |
| 1463 |
SetBuf(p->p_buffer); |
| 1464 |
DelMark(p->p_mark); |
| 1465 |
SetBuf(old); |
| 1466 |
p->p_buffer = NULL; |
| 1467 |
b->b_process = NULL; |
| 1468 |
} |
| 1469 |
} |
| 1470 |
} |
| 1471 |
|
| 1472 |
void |
| 1473 |
ProcList(void) |
| 1474 |
{ |
| 1475 |
Process p, |
| 1476 |
next; |
| 1477 |
const char |
| 1478 |
fmt[] = "%-15s %-15s %-8s %s"; |
| 1479 |
char |
| 1480 |
pidstr[16]; |
| 1481 |
|
| 1482 |
if (procs == NULL) { |
| 1483 |
message("[No subprocesses]"); |
| 1484 |
return; |
| 1485 |
} |
| 1486 |
TOstart("Process list"); |
| 1487 |
|
| 1488 |
Typeout(fmt, "Buffer", "Status", "Pid ", "Command"); |
| 1489 |
Typeout(fmt, "------", "------", "--- ", "-------"); |
| 1490 |
for (p = procs; p != NULL; p = next) { |
| 1491 |
next = p->p_next; |
| 1492 |
swritef(pidstr, sizeof(pidstr), "%d", (int) p->p_pid); |
| 1493 |
Typeout(fmt, proc_bufname(p), pstate(p), pidstr, p->p_name); |
| 1494 |
if (dead(p)) { |
| 1495 |
free_proc(p); |
| 1496 |
UpdModLine = YES; |
| 1497 |
} |
| 1498 |
} |
| 1499 |
TOstop(); |
| 1500 |
} |
| 1501 |
|
| 1502 |
private void |
| 1503 |
do_rtp(Mark *mp) |
| 1504 |
{ |
| 1505 |
Process p = curbuf->b_process; |
| 1506 |
LinePtr line1 = curline, |
| 1507 |
line2 = mp->m_line; |
| 1508 |
int char1 = curchar, |
| 1509 |
char2 = mp->m_char; |
| 1510 |
char *gp; |
| 1511 |
size_t nbytes; |
| 1512 |
|
| 1513 |
if (dead(p) || p->p_buffer != curbuf) |
| 1514 |
return; |
| 1515 |
|
| 1516 |
(void) fixorder(&line1, &char1, &line2, &char2); |
| 1517 |
while (line1 != line2->l_next) { |
| 1518 |
gp = ltobuf(line1, genbuf) + char1; |
| 1519 |
if (line1 == line2) { |
| 1520 |
nbytes = char2 - char1; |
| 1521 |
} else { |
| 1522 |
genbuf[Jr_Len] = EOL; /* replace NUL with EOL */ |
| 1523 |
nbytes = Jr_Len - char1 + 1; /* and include it */ |
| 1524 |
} |
| 1525 |
if (nbytes != 0) |
| 1526 |
proc_write(p, gp, nbytes); |
| 1527 |
line1 = line1->l_next; |
| 1528 |
char1 = 0; |
| 1529 |
} |
| 1530 |
} |
| 1531 |
|
| 1532 |
void |
| 1533 |
ProcNewline(void) |
| 1534 |
{ |
| 1535 |
#ifdef ABBREV |
| 1536 |
MaybeAbbrevExpand(); |
| 1537 |
#endif |
| 1538 |
SendData(YES); |
| 1539 |
} |
| 1540 |
|
| 1541 |
void |
| 1542 |
ProcSendData(void) |
| 1543 |
{ |
| 1544 |
#ifdef ABBREV |
| 1545 |
MaybeAbbrevExpand(); |
| 1546 |
#endif |
| 1547 |
SendData(NO); |
| 1548 |
} |
| 1549 |
|
| 1550 |
private void |
| 1551 |
SendData(jbool newlinep) |
| 1552 |
{ |
| 1553 |
Process p = curbuf->b_process; |
| 1554 |
char *lp, |
| 1555 |
*gp; /* JF fix for better prompt handling */ |
| 1556 |
|
| 1557 |
if (dead(p)) |
| 1558 |
return; |
| 1559 |
|
| 1560 |
/* If the process mark was involved in a big deletion, because |
| 1561 |
* the user hit ^W or something, then let's do some magic with |
| 1562 |
* the process mark. Problem is that if the user yanks back the |
| 1563 |
* text he deleted, the mark stays at the beginning of the region, |
| 1564 |
* and so the next time SendData() is called the entire region |
| 1565 |
* will be sent. That's not good. So, to deal with that we reset |
| 1566 |
* the mark to the last line, after skipping over the prompt, etc. |
| 1567 |
*/ |
| 1568 |
if (p->p_mark->m_big_delete) { |
| 1569 |
Bufpos bp; |
| 1570 |
|
| 1571 |
p->p_mark->m_big_delete = NO; |
| 1572 |
|
| 1573 |
DOTsave(&bp); |
| 1574 |
ToLast(); |
| 1575 |
Bol(); |
| 1576 |
/* While we're looking at a prompt, and while we're |
| 1577 |
* moving forward. This is for people who accidently |
| 1578 |
* set their process-prompt to ">*" which will always |
| 1579 |
* match! |
| 1580 |
*/ |
| 1581 |
while (LookingAt(proc_prompt, linebuf, curchar) |
| 1582 |
&& (REeom > curchar)) |
| 1583 |
curchar = REeom; |
| 1584 |
MarkSet(p->p_mark, curline, curchar); |
| 1585 |
SetDot(&bp); |
| 1586 |
} |
| 1587 |
|
| 1588 |
if (lastp(curline)) { |
| 1589 |
Eol(); |
| 1590 |
if (newlinep) |
| 1591 |
LineInsert(1); |
| 1592 |
do_rtp(p->p_mark); |
| 1593 |
MarkSet(p->p_mark, curline, curchar); |
| 1594 |
} else { |
| 1595 |
/* Either we're looking at a prompt, or we're not, in |
| 1596 |
* which case we want to strip off the beginning of the |
| 1597 |
* line anything that looks like what the prompt at the |
| 1598 |
* end of the file is. In other words, if "(dbx) stop in |
| 1599 |
* ProcessNewline" is the line we're on, and the last |
| 1600 |
* line in the buffer is "(dbx) ", then we strip off the |
| 1601 |
* leading "(dbx) " from this line, because we know it's |
| 1602 |
* part of the prompt. But this only happens if "(dbx) " |
| 1603 |
* isn't one of the process prompts ... follow what I'm |
| 1604 |
* saying? |
| 1605 |
*/ |
| 1606 |
Bol(); |
| 1607 |
if (LookingAt(proc_prompt, linebuf, curchar)) { |
| 1608 |
do { |
| 1609 |
curchar = REeom; |
| 1610 |
} while (LookingAt(proc_prompt, linebuf, curchar) |
| 1611 |
&& (REeom > curchar)); |
| 1612 |
strcpy(genbuf, linebuf + curchar); |
| 1613 |
Eof(); |
| 1614 |
ins_str(genbuf); |
| 1615 |
} else { |
| 1616 |
strcpy(genbuf, linebuf + curchar); |
| 1617 |
Eof(); |
| 1618 |
gp = genbuf; |
| 1619 |
lp = linebuf; |
| 1620 |
while (*lp == *gp && *lp != '\0') { |
| 1621 |
lp += 1; |
| 1622 |
gp += 1; |
| 1623 |
} |
| 1624 |
ins_str(gp); |
| 1625 |
} |
| 1626 |
} |
| 1627 |
} |
| 1628 |
|
| 1629 |
void |
| 1630 |
ShellProc(void) |
| 1631 |
{ |
| 1632 |
char shbuf[20]; |
| 1633 |
Buffer *b; |
| 1634 |
|
| 1635 |
swritef(shbuf, sizeof(shbuf), "[shell-%d]", arg_value()); |
| 1636 |
b = buf_exists(shbuf); |
| 1637 |
if (b == NULL || dead(b->b_process)) { |
| 1638 |
char cbnspace[FILESIZE]; |
| 1639 |
|
| 1640 |
proc_strt(shbuf, NO, "i-shell", Shell, "-is", |
| 1641 |
curbuf->b_fname == NULL? (char *)NULL : strcpy(cbnspace, pr_name(curbuf->b_fname, NO)), |
| 1642 |
(char *)NULL); |
| 1643 |
} |
| 1644 |
pop_wind(shbuf, NO, -1); |
| 1645 |
} |
| 1646 |
|
| 1647 |
void |
| 1648 |
Iprocess(void) |
| 1649 |
{ |
| 1650 |
char scratch[64], |
| 1651 |
*bnm; |
| 1652 |
int cnt = 1; |
| 1653 |
Buffer *bp; |
| 1654 |
char fnspace[FILESIZE]; |
| 1655 |
char *fn = curbuf->b_fname == NULL |
| 1656 |
? (char *) NULL : strcpy(fnspace, pr_name(curbuf->b_fname, NO)); |
| 1657 |
|
| 1658 |
jamstr(ShcomBuf, ask(ShcomBuf, ProcFmt)); |
| 1659 |
bnm = MakeName(ShcomBuf); |
| 1660 |
truncstr(scratch, bnm); |
| 1661 |
while ((bp = buf_exists(scratch)) != NULL && !dead(bp->b_process)) |
| 1662 |
swritef(scratch, sizeof(scratch), "%s.%d", bnm, cnt++); |
| 1663 |
proc_strt(scratch, YES, ShcomBuf, Shell, ShFlags, ShcomBuf, fn, fn, (char *)NULL); |
| 1664 |
} |
| 1665 |
|
| 1666 |
pid_t DeadPid; /* info about ChildPid, if reaped by kill_off */ |
| 1667 |
wait_status_t DeadStatus; |
| 1668 |
|
| 1669 |
void |
| 1670 |
kill_off(pid_t pid, wait_status_t w) |
| 1671 |
{ |
| 1672 |
Process child; |
| 1673 |
|
| 1674 |
if (pid == ChildPid) { |
| 1675 |
/* we are reaping the non-iproc process: record info */ |
| 1676 |
DeadPid = pid; |
| 1677 |
DeadStatus = w; |
| 1678 |
return; |
| 1679 |
} |
| 1680 |
|
| 1681 |
if ((child = proc_pid(pid)) == NULL) |
| 1682 |
return; |
| 1683 |
|
| 1684 |
UpdModLine = YES; /* we're changing state ... */ |
| 1685 |
if (WIFSTOPPED(w)) |
| 1686 |
child->p_child_state = C_STOPPED; |
| 1687 |
else { |
| 1688 |
#ifdef PIPEPROCS |
| 1689 |
/* the true process status comes by pipe, not from wait(). |
| 1690 |
* However, we must note the death of portsrv processes. |
| 1691 |
*/ |
| 1692 |
child->p_portlive = NO; |
| 1693 |
#else /* !PIPEPROCS */ |
| 1694 |
/* record the details of the death of the process. |
| 1695 |
* Kludge: see if we can get any queued EOF from pty first. |
| 1696 |
* We wish to do this to make our message less confusing. |
| 1697 |
*/ |
| 1698 |
while (!io_eofed(child)) { |
| 1699 |
int nfds; |
| 1700 |
fd_set reads; |
| 1701 |
struct timeval notime; |
| 1702 |
|
| 1703 |
FD_ZERO(&reads); |
| 1704 |
FD_SET(child->p_fd, &reads); |
| 1705 |
notime.tv_sec = 0; |
| 1706 |
notime.tv_usec = 0; |
| 1707 |
nfds = select(global_maxfd, &reads, |
| 1708 |
(fd_set *)NULL, (fd_set *)NULL, ¬ime); |
| 1709 |
if (nfds == 1) |
| 1710 |
read_pty_proc(child->p_fd); |
| 1711 |
else if (nfds >= 0 || errno != EINTR) { |
| 1712 |
# ifdef NO_EOF_FROM_PTY |
| 1713 |
/* Certain systems never indicate EOF from a slave. |
| 1714 |
* On those systems, we force a close when the direct |
| 1715 |
* child terminates. |
| 1716 |
*/ |
| 1717 |
proc_close(child); |
| 1718 |
# endif |
| 1719 |
break; |
| 1720 |
} |
| 1721 |
} |
| 1722 |
obituary(child, w); |
| 1723 |
#endif /* !PIPEPROCS */ |
| 1724 |
} |
| 1725 |
} |
| 1726 |
|
| 1727 |
private void |
| 1728 |
obituary(Process child, wait_status_t w) |
| 1729 |
{ |
| 1730 |
UpdModLine = YES; /* we're changing state ... */ |
| 1731 |
if (WIFEXITED(w)) { |
| 1732 |
child->p_child_state = C_EXITED; |
| 1733 |
child->p_reason = WEXITSTATUS(w); |
| 1734 |
} else if (WIFSIGNALED(w)) { |
| 1735 |
child->p_child_state = C_KILLED; |
| 1736 |
child->p_reason = WTERMSIG(w); |
| 1737 |
} else { |
| 1738 |
/* presume it died peacefully! */ |
| 1739 |
child->p_child_state = C_EXITED; |
| 1740 |
child->p_reason = 0; |
| 1741 |
} |
| 1742 |
|
| 1743 |
{ |
| 1744 |
Buffer *save = curbuf; |
| 1745 |
Bufpos bp; |
| 1746 |
char mesg[128]; |
| 1747 |
|
| 1748 |
/* insert status message now */ |
| 1749 |
swritef(mesg, sizeof(mesg), "[Process %s: %s]\n", |
| 1750 |
proc_cmd(child), pstate(child)); |
| 1751 |
SetBuf(child->p_buffer); |
| 1752 |
DOTsave(&bp); |
| 1753 |
ToLast(); |
| 1754 |
ins_str(mesg); |
| 1755 |
SetDot(&bp); |
| 1756 |
SetBuf(save); |
| 1757 |
redisplay(); |
| 1758 |
} |
| 1759 |
} |
| 1760 |
|
| 1761 |
#endif /* IPROCS */ |