| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tclInt.h" |
| 17 |
#include "tclPort.h" |
| 18 |
|
| 19 |
#ifdef USE_VFORK |
| 20 |
#define fork vfork |
| 21 |
#endif |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#define MakeFile(fd) ((TclFile)(((int)fd)+1)) |
| 30 |
#define GetFd(file) (((int)file)-1) |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
typedef struct PipeState { |
| 37 |
Tcl_Channel channel; |
| 38 |
TclFile inFile; |
| 39 |
TclFile outFile; |
| 40 |
TclFile errorFile; |
| 41 |
int numPids; |
| 42 |
Tcl_Pid *pidPtr; |
| 43 |
|
| 44 |
int isNonBlocking; |
| 45 |
|
| 46 |
|
| 47 |
} PipeState; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
static int PipeBlockModeProc _ANSI_ARGS_((ClientData instanceData, |
| 54 |
int mode)); |
| 55 |
static int PipeCloseProc _ANSI_ARGS_((ClientData instanceData, |
| 56 |
Tcl_Interp *interp)); |
| 57 |
static int PipeGetHandleProc _ANSI_ARGS_((ClientData instanceData, |
| 58 |
int direction, ClientData *handlePtr)); |
| 59 |
static int PipeInputProc _ANSI_ARGS_((ClientData instanceData, |
| 60 |
char *buf, int toRead, int *errorCode)); |
| 61 |
static int PipeOutputProc _ANSI_ARGS_(( |
| 62 |
ClientData instanceData, CONST char *buf, int toWrite, |
| 63 |
int *errorCode)); |
| 64 |
static void PipeWatchProc _ANSI_ARGS_((ClientData instanceData, int mask)); |
| 65 |
static void RestoreSignals _ANSI_ARGS_((void)); |
| 66 |
static int SetupStdFile _ANSI_ARGS_((TclFile file, int type)); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
static Tcl_ChannelType pipeChannelType = { |
| 74 |
"pipe", |
| 75 |
TCL_CHANNEL_VERSION_2, |
| 76 |
PipeCloseProc, |
| 77 |
PipeInputProc, |
| 78 |
PipeOutputProc, |
| 79 |
NULL, |
| 80 |
NULL, |
| 81 |
NULL, |
| 82 |
PipeWatchProc, |
| 83 |
PipeGetHandleProc, |
| 84 |
NULL, |
| 85 |
PipeBlockModeProc, |
| 86 |
NULL, |
| 87 |
NULL, |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
TclFile |
| 107 |
TclpMakeFile(channel, direction) |
| 108 |
Tcl_Channel channel; |
| 109 |
int direction; |
| 110 |
{ |
| 111 |
ClientData data; |
| 112 |
|
| 113 |
if (Tcl_GetChannelHandle(channel, direction, (ClientData *) &data) |
| 114 |
== TCL_OK) { |
| 115 |
return MakeFile((int)data); |
| 116 |
} else { |
| 117 |
return (TclFile) NULL; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
TclFile |
| 138 |
TclpOpenFile(fname, mode) |
| 139 |
CONST char *fname; |
| 140 |
int mode; |
| 141 |
{ |
| 142 |
int fd; |
| 143 |
CONST char *native; |
| 144 |
Tcl_DString ds; |
| 145 |
|
| 146 |
native = Tcl_UtfToExternalDString(NULL, fname, -1, &ds); |
| 147 |
fd = TclOSopen(native, mode, 0666); |
| 148 |
Tcl_DStringFree(&ds); |
| 149 |
if (fd != -1) { |
| 150 |
fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
if (mode & O_WRONLY) { |
| 158 |
TclOSseek(fd, (Tcl_SeekOffset) 0, SEEK_END); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
return MakeFile(fd); |
| 167 |
} |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
TclFile |
| 190 |
TclpCreateTempFile(contents) |
| 191 |
CONST char *contents; |
| 192 |
{ |
| 193 |
char fileName[L_tmpnam + 9]; |
| 194 |
CONST char *native; |
| 195 |
Tcl_DString dstring; |
| 196 |
int fd; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
strcpy(fileName, P_tmpdir); |
| 203 |
if (fileName[strlen(fileName) - 1] != '/') { |
| 204 |
strcat(fileName, "/"); |
| 205 |
} |
| 206 |
strcat(fileName, "tclXXXXXX"); |
| 207 |
fd = mkstemp(fileName); |
| 208 |
if (fd == -1) { |
| 209 |
return NULL; |
| 210 |
} |
| 211 |
fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 212 |
unlink(fileName); |
| 213 |
|
| 214 |
if (contents != NULL) { |
| 215 |
native = Tcl_UtfToExternalDString(NULL, contents, -1, &dstring); |
| 216 |
if (write(fd, native, strlen(native)) == -1) { |
| 217 |
close(fd); |
| 218 |
Tcl_DStringFree(&dstring); |
| 219 |
return NULL; |
| 220 |
} |
| 221 |
Tcl_DStringFree(&dstring); |
| 222 |
TclOSseek(fd, (Tcl_SeekOffset) 0, SEEK_SET); |
| 223 |
} |
| 224 |
return MakeFile(fd); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
Tcl_Obj* |
| 244 |
TclpTempFileName() |
| 245 |
{ |
| 246 |
char fileName[L_tmpnam + 9]; |
| 247 |
Tcl_Obj *result = NULL; |
| 248 |
int fd; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
strcpy(fileName, P_tmpdir); |
| 255 |
if (fileName[strlen(fileName) - 1] != '/') { |
| 256 |
strcat(fileName, "/"); |
| 257 |
} |
| 258 |
strcat(fileName, "tclXXXXXX"); |
| 259 |
fd = mkstemp(fileName); |
| 260 |
if (fd == -1) { |
| 261 |
return NULL; |
| 262 |
} |
| 263 |
fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 264 |
unlink(fileName); |
| 265 |
|
| 266 |
result = TclpNativeToNormalized((ClientData) fileName); |
| 267 |
close (fd); |
| 268 |
return result; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
int |
| 288 |
TclpCreatePipe(readPipe, writePipe) |
| 289 |
TclFile *readPipe; |
| 290 |
|
| 291 |
TclFile *writePipe; |
| 292 |
|
| 293 |
{ |
| 294 |
int pipeIds[2]; |
| 295 |
|
| 296 |
if (pipe(pipeIds) != 0) { |
| 297 |
return 0; |
| 298 |
} |
| 299 |
|
| 300 |
fcntl(pipeIds[0], F_SETFD, FD_CLOEXEC); |
| 301 |
fcntl(pipeIds[1], F_SETFD, FD_CLOEXEC); |
| 302 |
|
| 303 |
*readPipe = MakeFile(pipeIds[0]); |
| 304 |
*writePipe = MakeFile(pipeIds[1]); |
| 305 |
return 1; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
int |
| 325 |
TclpCloseFile(file) |
| 326 |
TclFile file; |
| 327 |
{ |
| 328 |
int fd = GetFd(file); |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
if ((fd == 0) || (fd == 1) || (fd == 2)) { |
| 335 |
return 0; |
| 336 |
} |
| 337 |
|
| 338 |
Tcl_DeleteFileHandler(fd); |
| 339 |
return close(fd); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
int |
| 368 |
TclpCreateProcess(interp, argc, argv, inputFile, outputFile, errorFile, |
| 369 |
pidPtr) |
| 370 |
Tcl_Interp *interp; |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
int argc; |
| 375 |
CONST char **argv; |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
TclFile inputFile; |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
TclFile outputFile; |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
TclFile errorFile; |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
Tcl_Pid *pidPtr; |
| 395 |
|
| 396 |
|
| 397 |
{ |
| 398 |
TclFile errPipeIn, errPipeOut; |
| 399 |
int joinThisError, count, status, fd; |
| 400 |
char errSpace[200 + TCL_INTEGER_SPACE]; |
| 401 |
Tcl_DString *dsArray; |
| 402 |
char **newArgv; |
| 403 |
int pid, i; |
| 404 |
|
| 405 |
errPipeIn = NULL; |
| 406 |
errPipeOut = NULL; |
| 407 |
pid = -1; |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
if (TclpCreatePipe(&errPipeIn, &errPipeOut) == 0) { |
| 415 |
Tcl_AppendResult(interp, "couldn't create pipe: ", |
| 416 |
Tcl_PosixError(interp), (char *) NULL); |
| 417 |
goto error; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
dsArray = (Tcl_DString *) ckalloc(argc * sizeof(Tcl_DString)); |
| 425 |
newArgv = (char **) ckalloc((argc+1) * sizeof(char *)); |
| 426 |
newArgv[argc] = NULL; |
| 427 |
for (i = 0; i < argc; i++) { |
| 428 |
newArgv[i] = Tcl_UtfToExternalDString(NULL, argv[i], -1, &dsArray[i]); |
| 429 |
} |
| 430 |
|
| 431 |
joinThisError = errorFile && (errorFile == outputFile); |
| 432 |
pid = fork(); |
| 433 |
if (pid == 0) { |
| 434 |
fd = GetFd(errPipeOut); |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
if (!SetupStdFile(inputFile, TCL_STDIN) |
| 441 |
|| !SetupStdFile(outputFile, TCL_STDOUT) |
| 442 |
|| (!joinThisError && !SetupStdFile(errorFile, TCL_STDERR)) |
| 443 |
|| (joinThisError && |
| 444 |
((dup2(1,2) == -1) || |
| 445 |
(fcntl(2, F_SETFD, 0) != 0)))) { |
| 446 |
sprintf(errSpace, |
| 447 |
"%dforked process couldn't set up input/output: ", errno); |
| 448 |
write(fd, errSpace, (size_t) strlen(errSpace)); |
| 449 |
_exit(1); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
RestoreSignals(); |
| 457 |
execvp(newArgv[0], newArgv); |
| 458 |
sprintf(errSpace, "%dcouldn't execute \"%.150s\": ", errno, argv[0]); |
| 459 |
write(fd, errSpace, (size_t) strlen(errSpace)); |
| 460 |
_exit(1); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
for (i = 0; i < argc; i++) { |
| 467 |
Tcl_DStringFree(&dsArray[i]); |
| 468 |
} |
| 469 |
ckfree((char *) dsArray); |
| 470 |
ckfree((char *) newArgv); |
| 471 |
|
| 472 |
if (pid == -1) { |
| 473 |
Tcl_AppendResult(interp, "couldn't fork child process: ", |
| 474 |
Tcl_PosixError(interp), (char *) NULL); |
| 475 |
goto error; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
TclpCloseFile(errPipeOut); |
| 485 |
errPipeOut = NULL; |
| 486 |
|
| 487 |
fd = GetFd(errPipeIn); |
| 488 |
count = read(fd, errSpace, (size_t) (sizeof(errSpace) - 1)); |
| 489 |
if (count > 0) { |
| 490 |
char *end; |
| 491 |
errSpace[count] = 0; |
| 492 |
errno = strtol(errSpace, &end, 10); |
| 493 |
Tcl_AppendResult(interp, end, Tcl_PosixError(interp), |
| 494 |
(char *) NULL); |
| 495 |
goto error; |
| 496 |
} |
| 497 |
|
| 498 |
TclpCloseFile(errPipeIn); |
| 499 |
*pidPtr = (Tcl_Pid) pid; |
| 500 |
return TCL_OK; |
| 501 |
|
| 502 |
error: |
| 503 |
if (pid != -1) { |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
Tcl_WaitPid((Tcl_Pid) pid, &status, 0); |
| 512 |
} |
| 513 |
|
| 514 |
if (errPipeIn) { |
| 515 |
TclpCloseFile(errPipeIn); |
| 516 |
} |
| 517 |
if (errPipeOut) { |
| 518 |
TclpCloseFile(errPipeOut); |
| 519 |
} |
| 520 |
return TCL_ERROR; |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
static void |
| 542 |
RestoreSignals() |
| 543 |
{ |
| 544 |
#ifdef SIGABRT |
| 545 |
signal(SIGABRT, SIG_DFL); |
| 546 |
#endif |
| 547 |
#ifdef SIGALRM |
| 548 |
signal(SIGALRM, SIG_DFL); |
| 549 |
#endif |
| 550 |
#ifdef SIGFPE |
| 551 |
signal(SIGFPE, SIG_DFL); |
| 552 |
#endif |
| 553 |
#ifdef SIGHUP |
| 554 |
signal(SIGHUP, SIG_DFL); |
| 555 |
#endif |
| 556 |
#ifdef SIGILL |
| 557 |
signal(SIGILL, SIG_DFL); |
| 558 |
#endif |
| 559 |
#ifdef SIGINT |
| 560 |
signal(SIGINT, SIG_DFL); |
| 561 |
#endif |
| 562 |
#ifdef SIGPIPE |
| 563 |
signal(SIGPIPE, SIG_DFL); |
| 564 |
#endif |
| 565 |
#ifdef SIGQUIT |
| 566 |
signal(SIGQUIT, SIG_DFL); |
| 567 |
#endif |
| 568 |
#ifdef SIGSEGV |
| 569 |
signal(SIGSEGV, SIG_DFL); |
| 570 |
#endif |
| 571 |
#ifdef SIGTERM |
| 572 |
signal(SIGTERM, SIG_DFL); |
| 573 |
#endif |
| 574 |
#ifdef SIGUSR1 |
| 575 |
signal(SIGUSR1, SIG_DFL); |
| 576 |
#endif |
| 577 |
#ifdef SIGUSR2 |
| 578 |
signal(SIGUSR2, SIG_DFL); |
| 579 |
#endif |
| 580 |
#ifdef SIGCHLD |
| 581 |
signal(SIGCHLD, SIG_DFL); |
| 582 |
#endif |
| 583 |
#ifdef SIGCONT |
| 584 |
signal(SIGCONT, SIG_DFL); |
| 585 |
#endif |
| 586 |
#ifdef SIGTSTP |
| 587 |
signal(SIGTSTP, SIG_DFL); |
| 588 |
#endif |
| 589 |
#ifdef SIGTTIN |
| 590 |
signal(SIGTTIN, SIG_DFL); |
| 591 |
#endif |
| 592 |
#ifdef SIGTTOU |
| 593 |
signal(SIGTTOU, SIG_DFL); |
| 594 |
#endif |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
static int |
| 617 |
SetupStdFile(file, type) |
| 618 |
TclFile file; |
| 619 |
int type; |
| 620 |
{ |
| 621 |
Tcl_Channel channel; |
| 622 |
int fd; |
| 623 |
int targetFd = 0; |
| 624 |
int direction = 0; |
| 625 |
|
| 626 |
|
| 627 |
switch (type) { |
| 628 |
case TCL_STDIN: |
| 629 |
targetFd = 0; |
| 630 |
direction = TCL_READABLE; |
| 631 |
break; |
| 632 |
case TCL_STDOUT: |
| 633 |
targetFd = 1; |
| 634 |
direction = TCL_WRITABLE; |
| 635 |
break; |
| 636 |
case TCL_STDERR: |
| 637 |
targetFd = 2; |
| 638 |
direction = TCL_WRITABLE; |
| 639 |
break; |
| 640 |
} |
| 641 |
|
| 642 |
if (!file) { |
| 643 |
channel = Tcl_GetStdChannel(type); |
| 644 |
if (channel) { |
| 645 |
file = TclpMakeFile(channel, direction); |
| 646 |
} |
| 647 |
} |
| 648 |
if (file) { |
| 649 |
fd = GetFd(file); |
| 650 |
if (fd != targetFd) { |
| 651 |
if (dup2(fd, targetFd) == -1) { |
| 652 |
return 0; |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
fcntl(targetFd, F_SETFD, 0); |
| 662 |
} else { |
| 663 |
int result; |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
result = fcntl(fd, F_SETFD, 0); |
| 671 |
} |
| 672 |
} else { |
| 673 |
close(targetFd); |
| 674 |
} |
| 675 |
return 1; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
Tcl_Channel |
| 697 |
TclpCreateCommandChannel(readFile, writeFile, errorFile, numPids, pidPtr) |
| 698 |
TclFile readFile; |
| 699 |
TclFile writeFile; |
| 700 |
TclFile errorFile; |
| 701 |
|
| 702 |
int numPids; |
| 703 |
Tcl_Pid *pidPtr; |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
{ |
| 708 |
char channelName[16 + TCL_INTEGER_SPACE]; |
| 709 |
int channelId; |
| 710 |
PipeState *statePtr = (PipeState *) ckalloc((unsigned) sizeof(PipeState)); |
| 711 |
int mode; |
| 712 |
|
| 713 |
statePtr->inFile = readFile; |
| 714 |
statePtr->outFile = writeFile; |
| 715 |
statePtr->errorFile = errorFile; |
| 716 |
statePtr->numPids = numPids; |
| 717 |
statePtr->pidPtr = pidPtr; |
| 718 |
statePtr->isNonBlocking = 0; |
| 719 |
|
| 720 |
mode = 0; |
| 721 |
if (readFile) { |
| 722 |
mode |= TCL_READABLE; |
| 723 |
} |
| 724 |
if (writeFile) { |
| 725 |
mode |= TCL_WRITABLE; |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
if (readFile) { |
| 734 |
channelId = GetFd(readFile); |
| 735 |
} else if (writeFile) { |
| 736 |
channelId = GetFd(writeFile); |
| 737 |
} else if (errorFile) { |
| 738 |
channelId = GetFd(errorFile); |
| 739 |
} else { |
| 740 |
channelId = 0; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
sprintf(channelName, "file%d", channelId); |
| 750 |
statePtr->channel = Tcl_CreateChannel(&pipeChannelType, channelName, |
| 751 |
(ClientData) statePtr, mode); |
| 752 |
return statePtr->channel; |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
void |
| 775 |
TclGetAndDetachPids(interp, chan) |
| 776 |
Tcl_Interp *interp; |
| 777 |
Tcl_Channel chan; |
| 778 |
{ |
| 779 |
PipeState *pipePtr; |
| 780 |
Tcl_ChannelType *chanTypePtr; |
| 781 |
int i; |
| 782 |
char buf[TCL_INTEGER_SPACE]; |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
chanTypePtr = Tcl_GetChannelType(chan); |
| 789 |
if (chanTypePtr != &pipeChannelType) { |
| 790 |
return; |
| 791 |
} |
| 792 |
|
| 793 |
pipePtr = (PipeState *) Tcl_GetChannelInstanceData(chan); |
| 794 |
for (i = 0; i < pipePtr->numPids; i++) { |
| 795 |
TclFormatInt(buf, (long) TclpGetPid(pipePtr->pidPtr[i])); |
| 796 |
Tcl_AppendElement(interp, buf); |
| 797 |
Tcl_DetachPids(1, &(pipePtr->pidPtr[i])); |
| 798 |
} |
| 799 |
if (pipePtr->numPids > 0) { |
| 800 |
ckfree((char *) pipePtr->pidPtr); |
| 801 |
pipePtr->numPids = 0; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
static int |
| 824 |
PipeBlockModeProc(instanceData, mode) |
| 825 |
ClientData instanceData; |
| 826 |
int mode; |
| 827 |
|
| 828 |
|
| 829 |
{ |
| 830 |
PipeState *psPtr = (PipeState *) instanceData; |
| 831 |
int curStatus; |
| 832 |
int fd; |
| 833 |
|
| 834 |
#ifndef USE_FIONBIO |
| 835 |
if (psPtr->inFile) { |
| 836 |
fd = GetFd(psPtr->inFile); |
| 837 |
curStatus = fcntl(fd, F_GETFL); |
| 838 |
if (mode == TCL_MODE_BLOCKING) { |
| 839 |
curStatus &= (~(O_NONBLOCK)); |
| 840 |
} else { |
| 841 |
curStatus |= O_NONBLOCK; |
| 842 |
} |
| 843 |
if (fcntl(fd, F_SETFL, curStatus) < 0) { |
| 844 |
return errno; |
| 845 |
} |
| 846 |
} |
| 847 |
if (psPtr->outFile) { |
| 848 |
fd = GetFd(psPtr->outFile); |
| 849 |
curStatus = fcntl(fd, F_GETFL); |
| 850 |
if (mode == TCL_MODE_BLOCKING) { |
| 851 |
curStatus &= (~(O_NONBLOCK)); |
| 852 |
} else { |
| 853 |
curStatus |= O_NONBLOCK; |
| 854 |
} |
| 855 |
if (fcntl(fd, F_SETFL, curStatus) < 0) { |
| 856 |
return errno; |
| 857 |
} |
| 858 |
} |
| 859 |
#endif |
| 860 |
|
| 861 |
#ifdef USE_FIONBIO |
| 862 |
if (psPtr->inFile) { |
| 863 |
fd = GetFd(psPtr->inFile); |
| 864 |
if (mode == TCL_MODE_BLOCKING) { |
| 865 |
curStatus = 0; |
| 866 |
} else { |
| 867 |
curStatus = 1; |
| 868 |
} |
| 869 |
if (ioctl(fd, (int) FIONBIO, &curStatus) < 0) { |
| 870 |
return errno; |
| 871 |
} |
| 872 |
} |
| 873 |
if (psPtr->outFile != NULL) { |
| 874 |
fd = GetFd(psPtr->outFile); |
| 875 |
if (mode == TCL_MODE_BLOCKING) { |
| 876 |
curStatus = 0; |
| 877 |
} else { |
| 878 |
curStatus = 1; |
| 879 |
} |
| 880 |
if (ioctl(fd, (int) FIONBIO, &curStatus) < 0) { |
| 881 |
return errno; |
| 882 |
} |
| 883 |
} |
| 884 |
#endif |
| 885 |
|
| 886 |
psPtr->isNonBlocking = (mode == TCL_MODE_NONBLOCKING); |
| 887 |
|
| 888 |
return 0; |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
static int |
| 911 |
PipeCloseProc(instanceData, interp) |
| 912 |
ClientData instanceData; |
| 913 |
Tcl_Interp *interp; |
| 914 |
{ |
| 915 |
PipeState *pipePtr; |
| 916 |
Tcl_Channel errChan; |
| 917 |
int errorCode, result; |
| 918 |
|
| 919 |
errorCode = 0; |
| 920 |
result = 0; |
| 921 |
pipePtr = (PipeState *) instanceData; |
| 922 |
if (pipePtr->inFile) { |
| 923 |
if (TclpCloseFile(pipePtr->inFile) < 0) { |
| 924 |
errorCode = errno; |
| 925 |
} |
| 926 |
} |
| 927 |
if (pipePtr->outFile) { |
| 928 |
if ((TclpCloseFile(pipePtr->outFile) < 0) && (errorCode == 0)) { |
| 929 |
errorCode = errno; |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
if (pipePtr->isNonBlocking || TclInExit()) { |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
Tcl_DetachPids(pipePtr->numPids, pipePtr->pidPtr); |
| 942 |
Tcl_ReapDetachedProcs(); |
| 943 |
|
| 944 |
if (pipePtr->errorFile) { |
| 945 |
TclpCloseFile(pipePtr->errorFile); |
| 946 |
} |
| 947 |
} else { |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
if (pipePtr->errorFile) { |
| 955 |
errChan = Tcl_MakeFileChannel( |
| 956 |
(ClientData) GetFd(pipePtr->errorFile), TCL_READABLE); |
| 957 |
} else { |
| 958 |
errChan = NULL; |
| 959 |
} |
| 960 |
result = TclCleanupChildren(interp, pipePtr->numPids, pipePtr->pidPtr, |
| 961 |
errChan); |
| 962 |
} |
| 963 |
|
| 964 |
if (pipePtr->numPids != 0) { |
| 965 |
ckfree((char *) pipePtr->pidPtr); |
| 966 |
} |
| 967 |
ckfree((char *) pipePtr); |
| 968 |
if (errorCode == 0) { |
| 969 |
return result; |
| 970 |
} |
| 971 |
return errorCode; |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
static int |
| 993 |
PipeInputProc(instanceData, buf, toRead, errorCodePtr) |
| 994 |
ClientData instanceData; |
| 995 |
char *buf; |
| 996 |
int toRead; |
| 997 |
|
| 998 |
int *errorCodePtr; |
| 999 |
{ |
| 1000 |
PipeState *psPtr = (PipeState *) instanceData; |
| 1001 |
int bytesRead; |
| 1002 |
|
| 1003 |
|
| 1004 |
*errorCodePtr = 0; |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
do { |
| 1016 |
bytesRead = read (GetFd(psPtr->inFile), buf, (size_t) toRead); |
| 1017 |
} while ((bytesRead < 0) && (errno == EINTR)); |
| 1018 |
|
| 1019 |
if (bytesRead < 0) { |
| 1020 |
*errorCodePtr = errno; |
| 1021 |
return -1; |
| 1022 |
} else { |
| 1023 |
return bytesRead; |
| 1024 |
} |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
static int |
| 1047 |
PipeOutputProc(instanceData, buf, toWrite, errorCodePtr) |
| 1048 |
ClientData instanceData; |
| 1049 |
CONST char *buf; |
| 1050 |
int toWrite; |
| 1051 |
int *errorCodePtr; |
| 1052 |
{ |
| 1053 |
PipeState *psPtr = (PipeState *) instanceData; |
| 1054 |
int written; |
| 1055 |
|
| 1056 |
*errorCodePtr = 0; |
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
do { |
| 1064 |
written = write(GetFd(psPtr->outFile), buf, (size_t) toWrite); |
| 1065 |
} while ((written < 0) && (errno == EINTR)); |
| 1066 |
|
| 1067 |
if (written < 0) { |
| 1068 |
*errorCodePtr = errno; |
| 1069 |
return -1; |
| 1070 |
} else { |
| 1071 |
return written; |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
static void |
| 1093 |
PipeWatchProc(instanceData, mask) |
| 1094 |
ClientData instanceData; |
| 1095 |
int mask; |
| 1096 |
|
| 1097 |
|
| 1098 |
{ |
| 1099 |
PipeState *psPtr = (PipeState *) instanceData; |
| 1100 |
int newmask; |
| 1101 |
|
| 1102 |
if (psPtr->inFile) { |
| 1103 |
newmask = mask & (TCL_READABLE | TCL_EXCEPTION); |
| 1104 |
if (newmask) { |
| 1105 |
Tcl_CreateFileHandler(GetFd(psPtr->inFile), mask, |
| 1106 |
(Tcl_FileProc *) Tcl_NotifyChannel, |
| 1107 |
(ClientData) psPtr->channel); |
| 1108 |
} else { |
| 1109 |
Tcl_DeleteFileHandler(GetFd(psPtr->inFile)); |
| 1110 |
} |
| 1111 |
} |
| 1112 |
if (psPtr->outFile) { |
| 1113 |
newmask = mask & (TCL_WRITABLE | TCL_EXCEPTION); |
| 1114 |
if (newmask) { |
| 1115 |
Tcl_CreateFileHandler(GetFd(psPtr->outFile), mask, |
| 1116 |
(Tcl_FileProc *) Tcl_NotifyChannel, |
| 1117 |
(ClientData) psPtr->channel); |
| 1118 |
} else { |
| 1119 |
Tcl_DeleteFileHandler(GetFd(psPtr->outFile)); |
| 1120 |
} |
| 1121 |
} |
| 1122 |
} |
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
static int |
| 1143 |
PipeGetHandleProc(instanceData, direction, handlePtr) |
| 1144 |
ClientData instanceData; |
| 1145 |
int direction; |
| 1146 |
ClientData *handlePtr; |
| 1147 |
{ |
| 1148 |
PipeState *psPtr = (PipeState *) instanceData; |
| 1149 |
|
| 1150 |
if (direction == TCL_READABLE && psPtr->inFile) { |
| 1151 |
*handlePtr = (ClientData) GetFd(psPtr->inFile); |
| 1152 |
return TCL_OK; |
| 1153 |
} |
| 1154 |
if (direction == TCL_WRITABLE && psPtr->outFile) { |
| 1155 |
*handlePtr = (ClientData) GetFd(psPtr->outFile); |
| 1156 |
return TCL_OK; |
| 1157 |
} |
| 1158 |
return TCL_ERROR; |
| 1159 |
} |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
Tcl_Pid |
| 1178 |
Tcl_WaitPid(pid, statPtr, options) |
| 1179 |
Tcl_Pid pid; |
| 1180 |
int *statPtr; |
| 1181 |
int options; |
| 1182 |
{ |
| 1183 |
int result; |
| 1184 |
pid_t real_pid; |
| 1185 |
|
| 1186 |
real_pid = (pid_t) pid; |
| 1187 |
while (1) { |
| 1188 |
result = (int) waitpid(real_pid, statPtr, options); |
| 1189 |
if ((result != -1) || (errno != EINTR)) { |
| 1190 |
return (Tcl_Pid) result; |
| 1191 |
} |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
int |
| 1214 |
Tcl_PidObjCmd(dummy, interp, objc, objv) |
| 1215 |
ClientData dummy; |
| 1216 |
Tcl_Interp *interp; |
| 1217 |
int objc; |
| 1218 |
Tcl_Obj *CONST *objv; |
| 1219 |
{ |
| 1220 |
Tcl_Channel chan; |
| 1221 |
Tcl_ChannelType *chanTypePtr; |
| 1222 |
PipeState *pipePtr; |
| 1223 |
int i; |
| 1224 |
Tcl_Obj *resultPtr, *longObjPtr; |
| 1225 |
|
| 1226 |
if (objc > 2) { |
| 1227 |
Tcl_WrongNumArgs(interp, 1, objv, "?channelId?"); |
| 1228 |
return TCL_ERROR; |
| 1229 |
} |
| 1230 |
if (objc == 1) { |
| 1231 |
Tcl_SetLongObj(Tcl_GetObjResult(interp), (long) getpid()); |
| 1232 |
} else { |
| 1233 |
chan = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), NULL); |
| 1234 |
if (chan == (Tcl_Channel) NULL) { |
| 1235 |
return TCL_ERROR; |
| 1236 |
} |
| 1237 |
chanTypePtr = Tcl_GetChannelType(chan); |
| 1238 |
if (chanTypePtr != &pipeChannelType) { |
| 1239 |
return TCL_OK; |
| 1240 |
} |
| 1241 |
pipePtr = (PipeState *) Tcl_GetChannelInstanceData(chan); |
| 1242 |
resultPtr = Tcl_GetObjResult(interp); |
| 1243 |
for (i = 0; i < pipePtr->numPids; i++) { |
| 1244 |
longObjPtr = Tcl_NewLongObj((long) TclpGetPid(pipePtr->pidPtr[i])); |
| 1245 |
Tcl_ListObjAppendElement(NULL, resultPtr, longObjPtr); |
| 1246 |
} |
| 1247 |
} |
| 1248 |
return TCL_OK; |
| 1249 |
} |