1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
|
/*
* exec.c: handles exec'd process for IRCII
*
* Copyright 1990 Michael Sandrof
* Copyright 1997 EPIC Software Labs
* See the COPYRIGHT file, or do a HELP IRCII COPYRIGHT
*/
#include "irc.h"
static char cvsrevision[] = "$Id: exec.c,v 1.1.1.1 2003/04/11 01:09:07 dan Exp $";
CVS_REVISION(exec_c)
#include "struct.h"
#include "dcc.h"
#include "exec.h"
#include "vars.h"
#include "ircaux.h"
#include "commands.h"
#include "window.h"
#include "screen.h"
#include "hook.h"
#include "input.h"
#include "list.h"
#include "server.h"
#include "output.h"
#include "parse.h"
#include "newio.h"
#include "gui.h"
#define MAIN_SOURCE
#include "modval.h"
#include <unistd.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
#ifdef __EMX__
#include <process.h>
#endif
pid_t getpid(void);
/* Process: the structure that has all the info needed for each process */
typedef struct
{
int index; /* Where in the proc array it is */
char *name; /* full process name */
char *logical;
pid_t pid; /* process-id of process */
int p_stdin; /* stdin description for process */
int p_stdout; /* stdout descriptor for process */
int p_stderr; /* stderr descriptor for process */
int counter; /* output line counter for process */
char *redirect; /* redirection command (MSG, NOTICE) */
unsigned refnum; /* a window for output to go to */
int server; /* the server to use for output */
char *who; /* nickname used for redirection */
int exited; /* true if process has exited */
int termsig; /* The signal that terminated
* the process */
int retcode; /* return code of process */
List *waitcmds; /* commands queued by WAIT -CMD */
int dumb; /* 0 if input still going, 1 if not */
int disowned; /* 1 if we let it loose */
char *stdoutc;
char *stdoutpc;
char *stderrc;
char *stderrpc;
} Process;
static Process **process_list = NULL;
static int process_list_size = 0;
static void handle_filedesc (Process *, int *, int, int);
static void cleanup_dead_processes (void);
static void ignore_process (int index);
void kill_process (int, int);
static void kill_all_processes (int signo);
static int valid_process_index (int proccess);
static int is_logical_unique (char *logical);
int logical_to_index (const char *logical);
extern int dead_children_processes;
extern int in_cparse;
extern char *next_expr (char **, char);
/*
* A nice array of the possible signals. Used by the coredump trapping
* routines and in the exec.c package
*/
#if !defined(SYS_SIGLIST_DECLARED) && !defined(_SYS_SIGLIST_DECLARED) && !defined(__QNX__)
#if defined(WINNT) || defined(__EMX__)
char *sys_siglist[] = { "ZERO", "SIGINT", "SIGKILL", "SIGPIPE", "SIGFPE",
"SIGHUP", "SIGTERM", "SIGSEGV", "SIGTSTP",
"SIGQUIT", "SIGTRAP", "SIGILL", "SIGEMT", "SIGALRM",
"SIGBUS", "SIGLOST", "SIGSTOP", "SIGABRT", "SIGUSR1",
"SIGUSR2", "SIGCHLD", "SIGTTOU", "SIGTTIN", "SIGCONT" };
#elif defined(__GLIBC__)
#define _GNU_SOURCE
/* use strsignal() from <string.h> */
#include <string.h>
#else
#include "sig.inc"
#endif
#endif
/*
* exec: the /EXEC command. Does the whole shebang for ircII sub procceses
* I melded in about half a dozen external functions, since they were only
* ever used here. Perhaps at some point theyll be broken out again, but
* not until i regain control of this module.
*/
BUILT_IN_COMMAND(execcmd)
{
#if !defined(PUBLIC_ACCESS)
#if defined(EXEC_COMMAND)
char *who = NULL,
*logical = NULL,
*redirect = NULL,
*flag;
unsigned refnum = 0;
int sig,
len,
i,
refnum_flag = 0,
logical_flag = 0;
int direct = 0;
Process *proc;
char *stdoutc = NULL,
*stderrc = NULL,
*stderrpc = NULL,
*stdoutpc = NULL,
*endc = NULL;
/*
* Protect the user against themselves.
* XXX This is stupid.
*/
if (get_int_var(EXEC_PROTECTION_VAR) && (current_on_hook != -1))
{
say("Attempt to use EXEC from within an ON function!");
say("Command \"%s\" not executed!", args);
say("Please read /HELP SET EXEC_PROTECTION");
say("for important details about this!");
return;
}
if (in_cparse)
{
yell("Something very bad just happened. an $exec() was issued");
yell("from within a format. Please notify panasync about this immediately");
yell("with what version of BitchX as well the scripts loaded when this happened");
return;
}
/*
* If no args are given, list all the processes.
*/
if (!*args)
{
int i;
if (!process_list)
{
say("No processes are running");
return;
}
say("Process List:");
for (i = 0; i < process_list_size; i++)
{
Process *proc = process_list[i];
if (!proc)
continue;
if (proc->logical)
say("\t%d (%s) (pid %d): %s",
i, proc->logical, proc->pid, proc->name);
else
say("\t%d (pid %d): %s", i, proc->pid, proc->name);
}
return;
}
/*
* Walk through and parse out all the args
*/
while ((*args == '-') && (flag = next_arg(args, &args)))
{
flag++;
if (!*flag)
break;
len = strlen(flag);
/*
* /EXEC -OUT redirects all output from the /exec to the
* current channel for the current window.
*/
if (my_strnicmp(flag, "OUT", len) == 0)
{
if (doing_privmsg)
redirect = "NOTICE";
else
redirect = "PRIVMSG";
/* Now we can `/exec -o' to queries too. - djw */
if (!(who = get_target_by_refnum(0)))
{
say("No target for this window for -OUT");
return;
}
}
/*
* /EXEC -NAME gives the /exec a logical name that can be
* refered to as %name
*/
else if (my_strnicmp(flag, "NAME", len) == 0)
{
logical_flag = 1;
if (!(logical = next_arg(args, &args)))
{
say("You must specify a logical name");
return;
}
}
/*
* /EXEC -WINDOW forces all output for an /exec to go to
* the window that is current at this time
*/
else if (my_strnicmp(flag, "WINDOW", len) == 0)
{
refnum_flag = 1;
refnum = current_refnum();
}
/*
* /EXEC -MSG <target> redirects the output of an /exec
* to the given target.
*/
else if (my_strnicmp(flag, "MSG", len) == 0)
{
if (doing_privmsg)
redirect = "NOTICE";
else
redirect = "PRIVMSG";
if (!(who = next_arg(args, &args)))
{
say("No nicknames specified");
return;
}
}
/*
* /EXEC -LINE specifies the stdout callback
*/
else if (my_strnicmp(flag, "LINE", len) == 0)
{
if ((stdoutc = next_expr(&args, '{')) == NULL)
say("Need {...} argument for -LINE flag");
}
/*
* /EXEC -LINEPART specifies the stdout partial line callback
*/
else if (my_strnicmp(flag, "LINEPART", len) == 0)
{
if ((stdoutpc = next_expr(&args, '{')) == NULL)
say("Need {...} argument for -LINEPART flag");
}
/*
* /EXEC -ERROR specifies the stderr callback
*/
else if (my_strnicmp(flag, "ERROR", len) == 0)
{
if ((stderrc = next_expr(&args, '{')) == NULL)
say("Need {...} argument for -ERROR flag");
}
/*
* /EXEC -ERRORPART specifies the stderr part line callback
*/
else if (my_strnicmp(flag, "ERRORPART", len) == 0)
{
if ((stderrpc = next_expr(&args, '{')) == NULL)
say("Need {...} argument for -ERRORPART flag");
}
/*
* /EXEC -END specifies the final collection callback
*/
else if (my_strnicmp(flag, "END", len) == 0)
{
if ((endc = next_expr(&args, '{')) == NULL)
say("Need {...} argument for -END flag");
}
/*
* /EXEC -CLOSE forcibly closes all the fd's to a process,
* in the hope that it will take the hint.
*/
else if (my_strnicmp(flag, "CLOSE", len) == 0)
{
if ((i = get_process_index(&args)) == -1)
return;
ignore_process(i);
return;
}
/*
* /EXEC -NOTICE <target> redirects the output of an /exec
* to a specified target
*/
else if (my_strnicmp(flag, "NOTICE", len) == 0)
{
redirect = "NOTICE";
if (!(who = next_arg(args, &args)))
{
say("No nicknames specified");
return;
}
}
/*
* /EXEC -start <command> starts a command with no association
* to the main BitchX process.
*/
else if (my_strnicmp(flag, "START", len) == 0)
{
#ifndef __EMX__
if(fork() == 0)
{
int i;
#endif
char *name = args;
char **args, *arg;
int max, cnt;
cnt = 0;
max = 5;
args = new_malloc(sizeof(char *) * max);
while ((arg = new_next_arg(name, &name)))
{
if (!arg || !*arg)
break;
if (cnt == max)
{
max += 5;
RESIZE(args, char *, max);
}
args[cnt++] = arg;
}
args[cnt] = NULL;
#ifndef __EMX__
for (i = 3; i < 256; i++)
close(i);
setsid();
execvp(args[0], args);
_exit(-1);
}
#else
spawnvp(P_SESSION, args[0], args);
#endif
return;
}
/*
* /EXEC -IN sends a line of text to a process
*/
else if (my_strnicmp(flag, "IN", len) == 0)
{
if ((i = get_process_index(&args)) == -1)
return;
text_to_process(i, args, 1);
return;
}
/*
* /EXEC -INQUIET quietly sends a line of text to a process
*/
else if (my_strnicmp(flag, "INQUIET", len) == 0)
{
if ((i = get_process_index(&args)) == -1)
return;
text_to_process(i, args, 0);
return;
}
/*
* /EXEC -DIRECT suppresses the use of a shell
*/
else if (my_strnicmp(flag, "DIRECT", len) == 0)
direct = 1;
/*
* All other - arguments are implied KILLs
*/
else
{
if (*args != '%')
{
say("%s is not a valid process", args);
return;
}
/*
* Check for a process to kill
*/
if ((i = get_process_index(&args)) == -1)
return;
/*
* Handle /exec -<num> %<process>
*/
if ((sig = my_atol(flag)) > 0)
{
if ((sig > 0) && (sig < NSIG-1))
kill_process(i, sig);
else
say("Signal number can be from 1 to %d", NSIG-1);
return;
}
/*
* Handle /exec -<SIGNAME> %<process>
*/
for (sig = 1; sig < NSIG-1; sig++)
{
#if defined(_GNU_SOURCE)
if (strsignal(sig) && !my_strnicmp(strsignal(sig), flag, len))
#else
if (sys_siglist[sig] && !my_strnicmp(sys_siglist[sig], flag, len))
#endif
{
kill_process(i, sig);
return;
}
}
/*
* Give up! =)
*/
say("No such signal: %s", flag);
return;
}
}
/*
* This handles the form:
*
* /EXEC <flags> %process
*
* Where the user wants to redefine some options for %process.
*/
if (*args == '%')
{
int i;
/*
* Make sure the process is actually running
*/
if ((i = get_process_index(&args)) == -1)
return;
proc = process_list[i];
message_to(refnum);
/*
* Check to see if the user wants to change windows
*/
if (refnum_flag)
{
proc->refnum = refnum;
if (refnum)
say("Output from process %d (%s) now going to this window", i, proc->name);
else
say("Output from process %d (%s) not going to any window", i, proc->name);
}
/*
* Check to see if the user is changing the default target
*/
malloc_strcpy(&(proc->redirect), redirect);
malloc_strcpy(&(proc->who), who);
if (redirect)
say("Output from process %d (%s) now going to %s", i, proc->name, who);
else
say("Output from process %d (%s) now going to you", i, proc->name);
/*
* Check to see if the user changed the NAME of %proc.
*/
if (logical_flag)
{
if (is_logical_unique(logical))
{
malloc_strcpy(&proc->logical, logical);
say("Process %d (%s) is now called %s",
i, proc->name, proc->logical);
}
else
say("The name %s is not unique!", logical);
}
message_to(0);
}
/*
* The user is trying to fire up a new /exec, so pass the buck.
*/
else
{
int p0[2], p1[2], p2[2],
pid, cnt;
char *shell,
*flag,
*arg;
char *name = args;
if (!is_logical_unique(logical))
{
say("The name %s is not unique!", logical);
return;
}
p0[0] = p1[0] = p2[0] = -1;
p0[1] = p1[1] = p2[1] = -1;
/*
* Open up the communication pipes
*/
if (pipe(p0) || pipe(p1) || pipe(p2))
{
say("Unable to start new process: %s", strerror(errno));
new_close(p0[0]);
new_close(p0[1]);
new_close(p1[0]);
new_close(p1[1]);
new_close(p2[0]);
new_close(p2[1]);
return;
}
#ifndef __EMXPM__
switch ((pid = fork()))
{
case -1:
say("Couldn't start new process!");
break;
/*
* CHILD: set up and exec the process
*/
case 0:
{
int i;
/*
* Fire up a new job control session,
* Sever all ties we had with the parent ircII process
*/
#if !defined(WINNT) && !defined(__EMX__)
setsid();
#endif
setuid(getuid());
setgid(getgid());
my_signal(SIGINT, SIG_IGN, 0);
my_signal(SIGQUIT, SIG_DFL, 0);
my_signal(SIGSEGV, SIG_DFL, 0);
my_signal(SIGBUS, SIG_DFL, 0);
#endif
dup2(p0[0], 0);
dup2(p1[1], 1);
dup2(p2[1], 2);
#ifdef __EMXPM__
/* prevent inheritance */
fcntl(p0[1], F_SETFD, FD_CLOEXEC);
fcntl(p1[0], F_SETFD, FD_CLOEXEC);
fcntl(p2[0], F_SETFD, FD_CLOEXEC);
#else
close(p0[1]);
close(p1[0]);
close(p2[0]);
p0[1] = p1[0] = p2[0] = -1;
for (i = 3; i < 256; i++)
close(i);
#endif
/*
* Pretend to be just a dumb terminal
*/
/*
bsd_setenv("TERM", "tty", 1);
*/
/*
* Figure out what shell (if any) we're using
*/
shell = get_string_var(SHELL_VAR);
/*
* If we're not using a shell, doovie up the exec args
* array and pass it off to execvp
*/
if (direct || !shell)
{
char **args;
int max;
cnt = 0;
max = 5;
args = new_malloc(sizeof(char *) * max);
while ((arg = new_next_arg(name, &name)))
{
if (!arg || !*arg)
break;
if (cnt == max)
{
max += 5;
RESIZE(args, char *, max);
}
args[cnt++] = arg;
}
args[cnt] = NULL;
#ifndef __EMXPM__
execvp(args[0], args);
#else
pid = spawnvp(P_NOWAIT, args[0], args);
#endif
}
/*
* If we're using a shell, let them have all the fun
*/
else
{
if (!(flag = get_string_var(SHELL_FLAGS_VAR)))
flag = empty_string;
#ifndef __EMXPM__
execl(shell, shell, flag, name, NULL);
#else
pid = spawnl(P_NOWAIT, shell, shell, flag, name, NULL);
#endif
}
#ifndef __EMXPM__
/*
* Something really died if we got here
*/
printf("*** Error starting shell \"%s\": %s\n",
shell, strerror(errno));
_exit(-1);
break;
}
/*
* PARENT: add the new process to the process table list
*/
default:
#else
if(pid == -1)
say("Couldn't start new process!");
else
#endif
{
int i;
Process *proc = new_malloc(sizeof(Process));
new_close(p0[0]);
new_close(p1[1]);
new_close(p2[1]);
/*
* Init the proc list if neccesary
*/
if (!process_list)
{
process_list = new_malloc(sizeof(Process *));
process_list_size = 1;
process_list[0] = NULL;
}
/*
* Find the first empty proc entry
*/
for (i = 0; i < process_list_size; i++)
{
if (!process_list[i])
{
process_list[i] = proc;
break;
}
}
/*
* If there are no empty proc entries, make a new one.
*/
if (i == process_list_size)
{
process_list_size++;
RESIZE(process_list, Process *, process_list_size);
process_list[i] = proc;
}
/*
* Fill in the new proc entry
*/
proc->name = m_strdup(name);
proc->logical = logical ? m_strdup(logical) : NULL;
proc->index = i;
proc->pid = pid;
proc->p_stdin = p0[1];
proc->p_stdout = p1[0];
proc->p_stderr = p2[0];
proc->refnum = refnum;
proc->redirect = NULL;
if (redirect)
proc->redirect = m_strdup(redirect);
proc->server = current_window->server;
proc->counter = 0;
proc->exited = 0;
proc->termsig = 0;
proc->retcode = 0;
proc->waitcmds = NULL;
proc->who = NULL;
proc->disowned = 0;
if (who)
proc->who = m_strdup(who);
proc->dumb = 0;
if (stdoutc)
proc->stdoutc = m_strdup(stdoutc);
else
proc->stdoutc = NULL;
if (stdoutpc)
proc->stdoutpc = m_strdup(stdoutpc);
else
proc->stdoutpc = NULL;
if (stderrc)
proc->stderrc = m_strdup(stderrc);
else
proc->stderrc = NULL;
if (stderrpc)
proc->stderrpc = m_strdup(stderrpc);
else
proc->stderrpc = NULL;
if (endc)
add_process_wait(proc->index, endc);
new_open(proc->p_stdout);
new_open(proc->p_stderr);
#ifndef __EMXPM__
break;
}
#endif
}
}
cleanup_dead_processes();
#endif
#endif
}
/*
* do_processes: This is called from the main io() loop to handle any
* pending /exec'd events. All this does is call handle_filedesc() on
* the two reading descriptors. If an EOF is asserted on either, then they
* are closed. If EOF has been asserted on both, then we mark the process
* as being "dumb". Once it is reaped (exited), it is expunged.
*/
void do_processes (fd_set *rd)
{
int i;
int limit;
if (!process_list)
return;
limit = get_int_var(SHELL_LIMIT_VAR);
for (i = 0; i < process_list_size; i++)
{
Process *proc = process_list[i];
if (!proc)
continue;
if (proc->p_stdout != -1 && FD_ISSET(proc->p_stdout, rd))
handle_filedesc(proc, &proc->p_stdout,
EXEC_PROMPT_LIST, EXEC_LIST);
if (proc->p_stderr != -1 && FD_ISSET(proc->p_stderr, rd))
handle_filedesc(proc, &proc->p_stderr,
EXEC_PROMPT_LIST, EXEC_ERRORS_LIST);
if (limit && proc->counter >= limit)
ignore_process(proc->index);
}
/* Clean up any (now) dead processes */
cleanup_dead_processes();
}
/*
* This is the back end to do_processes, saves some repeated code
*/
static void handle_filedesc (Process *proc, int *fd, int hook_nonl, int hook_nl)
{
char exec_buffer[IO_BUFFER_SIZE + 1];
int len;
switch ((len = dgets(exec_buffer, *fd, 0, IO_BUFFER_SIZE, NULL))) /* No buffering! */
{
case -1: /* Something died */
{
*fd = new_close(*fd);
if (proc->p_stdout == -1 && proc->p_stderr == -1)
proc->dumb = 1;
break;
}
case 0: /* We didnt get a full line */
{
if (hook_nl == EXEC_LIST && proc->stdoutpc)
parse_line("EXEC", proc->stdoutpc, exec_buffer, 0, 0, 1);
else if (hook_nl == EXEC_ERRORS_LIST && proc->stderrpc)
parse_line("EXEC", proc->stderrpc, exec_buffer, 0, 0, 1);
else if (proc->logical)
do_hook(hook_nonl, "%s %s",
proc->logical, exec_buffer);
else
do_hook(hook_nonl, "%d %s",
proc->index, exec_buffer);
set_prompt_by_refnum(proc->refnum, exec_buffer);
update_input(UPDATE_ALL);
break;
}
default: /* We got a full line */
{
message_to(proc->refnum);
proc->counter++;
while (len > 0 && (exec_buffer[len] == '\n' ||
exec_buffer[len] == '\r'))
{
exec_buffer[len--] = 0;
}
if (proc->redirect)
redirect_text(proc->server, proc->who,
exec_buffer, proc->redirect, 1, 0);
if (hook_nl == EXEC_LIST && proc->stdoutc)
parse_line("EXEC", proc->stdoutc, exec_buffer, 0, 0, 1);
else if (hook_nl == EXEC_ERRORS_LIST && proc->stderrc)
parse_line("EXEC", proc->stderrc, exec_buffer, 0, 0,1);
else if (proc->logical)
{
if ((do_hook(hook_nl, "%s %s",
proc->logical, exec_buffer)))
if (!proc->redirect)
put_it("%s", exec_buffer);
}
else
{
if ((do_hook(hook_nl, "%d %s",
proc->index, exec_buffer)))
if (!proc->redirect)
put_it("%s", exec_buffer);
}
message_to(0);
}
}
}
/*
* get_child_exit: This looks for dead child processes of the client.
* There are two main sources of dead children: Either an /exec'd process
* has exited, or the client has attempted to fork() off a helper process
* (such as wserv or gzip) and that process has choked on itself.
*
* When SIGCHLD is recieved, the global variable 'dead_children_processes'
* is incremented. When this function is called, we go through and call
* waitpid() on all of the outstanding zombies, conditionally stopping when
* we reach a specific wanted sub-process.
*
* If you want to stop reaping children when a specific subprocess is
* reached, specify the process in 'wanted'. If all youre doing is cleaning
* up after zombies and /exec's, then 'wanted' should be -1.
*/
extern SIGNAL_HANDLER(child_reap);
int get_child_exit (pid_t wanted)
{
Process *proc;
pid_t pid;
int status, i;
/*
* Iterate until we've reaped all of the dead processes
* or we've found the one asked for.
*/
if (dead_children_processes)
while ((pid = waitpid(wanted, &status, WNOHANG)) > 0)
{
/*
* Ideally, this should never be < 0.
*/
dead_children_processes--;
#ifdef __EMX__
my_signal(SIGCHLD, child_reap, 0);
#endif
/*
* First thing we do is look to see if the process we're
* working on is the one that was asked for. If it is,
* then we get its exit status information and return it.
*/
if (wanted != -1 && pid == wanted)
{
if (WIFEXITED(status))
return WEXITSTATUS(status);
if (WIFSTOPPED(status))
return -(WSTOPSIG(status));
if (WIFSIGNALED(status))
return -(WTERMSIG(status));
}
/*
* If it wasnt the process asked for, then we've probably
* stumbled across a dead /exec'd process. Look for the
* corresponding child process, and mark it as being dead.
*/
else
{
for (i = 0; i < process_list_size; i++)
{
proc = process_list[i];
if (proc && proc->pid == pid)
{
proc->exited = 1;
#ifdef __EMX__
proc->disowned = 1;
#endif
proc->termsig = WTERMSIG(status);
proc->retcode = WEXITSTATUS(status);
#ifdef __EMXPM__
pm_seticon(last_input_screen);
#endif
break;
}
}
}
}
/*
* Now we may have reaped some /exec'd processes that were previously
* dumb and have now exited. So we call cleanup_dead_processes() to
* find and delete any such processes.
*/
cleanup_dead_processes();
/*
* If 'wanted' is not -1, then we didnt find that process, and
* if 'wanted' is -1, then you should ignore the retval anyhow. ;-)
*/
return -1;
}
/*
* clean_up_processes: In effect, we want to tell all of our sub processes
* that we're going away. We cant be 100% sure that theyre all dead by
* the time this function returns, but we can be 100% sure that they will
* be killed off next time they come up to run. This is the only thing that
* can be guaranteed, and is in fact all we really need to know.
*/
void clean_up_processes (void)
{
if (process_list_size)
{
say("Closing all left over exec's");
kill_all_processes(SIGTERM);
sleep(2);
kill_all_processes(SIGKILL);
}
}
/*
* text_to_process: sends the given text to the given process. If the given
* process index is not valid, an error is reported and 1 is returned.
* Otherwise 0 is returned.
* Added show, to remove some bad recursion, phone, april 1993
*/
int text_to_process (int proc_index, const char *text, int show)
{
Process * proc;
char * my_buffer;
if (valid_process_index(proc_index) == 0)
return 1;
proc = process_list[proc_index];
message_to(proc->refnum);
if (show)
put_it("%s%s", get_prompt_by_refnum(proc->refnum), text);
message_to(0);
my_buffer = alloca(strlen(text) + 2);
strcpy(my_buffer, text);
strcat(my_buffer, "\n");
write(proc->p_stdin, my_buffer, strlen(my_buffer));
set_prompt_by_refnum(proc->refnum, empty_string);
return (0);
}
/*
* When a server goes away, re-assign all of the /exec's that are
* current bound to that server.
*/
void exec_server_delete (int i)
{
int j;
for (j = 0; j < process_list_size; j++)
if (process_list[j] && process_list[j]->server >= i)
process_list[j]->server--;
}
/*
* This adds a new /wait %proc -cmd entry to a running process.
*/
void add_process_wait (int proc_index, const char *cmd)
{
Process *proc = process_list[proc_index];
List *new, *posn;
new = new_malloc(sizeof(List));
new->next = NULL;
new->name = m_strdup(cmd);
if ((posn = proc->waitcmds))
{
while (posn->next)
posn = posn->next;
posn->next = new;
}
else
proc->waitcmds = new;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*
* A process must go through two stages to be completely obliterated from
* the client. Either stage may happen first, but until both are completed
* we keep the process around.
*
* 1) We must recieve an EOF on both stdin and stderr, or we must
* have closed stdin and stderr already (handled by do_processes)
* 2) The process must have died (handled by get_child_exit)
*
* The reason why both must happen is becuase the process can die (and
* we would get an async signal) before we read all of its output on the
* pipe, and if we simply deleted the process when it dies, we could lose
* some of its output. The reason why we cant delete a process that has
* asserted EOF on its output is because it could still be running (duh! ;-)
* So we wait for both to happen.
*/
/*
* This function is called by the three places that can effect a change
* on the state of a running process:
* 1) get_child_exit, which can mark a process as exited
* 2) do_processes, which can mark a child as being done with I/O
* 3) execcmd, which cacn mark a child as being done with I/O
*
* Any processes that are found to have both exited and having completed
* their I/O will be summarily destroyed.
*/
static void cleanup_dead_processes (void)
{
int i, flag;
List *cmd,
*next;
Process *dead, *proc;
if (!process_list)
return; /* Nothing to do */
for (i = 0; i < process_list_size; i++)
{
if (!(proc = process_list[i]))
continue;
/*
* We do not parse the process if it has not
* both exited and finished its io, UNLESS
* it has been disowned.
*/
if ((!proc->exited || !proc->dumb) && !proc->disowned)
continue; /* Not really dead yet */
dead = process_list[i];
process_list[i] = NULL;
/*
* First thing we do is run any /wait %proc -cmd commands
*/
next = dead->waitcmds;
dead->waitcmds = NULL;
while ((cmd = next))
{
next = cmd->next;
parse_line(NULL, cmd->name, empty_string, 0, 0, 1);
new_free(&cmd->name);
new_free((char **)&cmd);
}
if (dead->p_stdin != -1)
close(dead->p_stdin);
dead->p_stdout = new_close(dead->p_stdout);
dead->p_stderr = new_close(dead->p_stderr);
/*
* Flag /on exec_exit
*/
if (dead->logical && *dead->logical)
flag = do_hook(EXEC_EXIT_LIST, "%s %d %d",
dead->logical, dead->termsig,
dead->retcode);
else
flag = do_hook(EXEC_EXIT_LIST, "%d %d %d",
dead->index, dead->termsig, dead->retcode);
/*
* Tell the user, if they care
*/
if (flag)
{
if (get_int_var(NOTIFY_ON_TERMINATION_VAR))
{
if (dead->termsig > 0 && dead->termsig < NSIG)
say("Process %d (%s) terminated with signal %s (%d)",
dead->index, dead->name,
#if defined(_GNU_SOURCE)
strsignal(dead->termsig),
#else
sys_siglist[dead->termsig],
#endif
dead->termsig);
else if (dead->disowned)
say("Process %d (%s) disowned", dead->index, dead->name);
else
say("Process %d (%s) terminated with return code %d",
dead->index, dead->name, dead->retcode);
}
}
/*
* Clean up after the process
*/
new_free(&dead->name);
new_free(&dead->logical);
new_free(&dead->who);
new_free(&dead->redirect);
new_free(&dead->stdoutc);
new_free(&dead->stdoutpc);
new_free(&dead->stderrc);
new_free(&dead->stderrpc);
new_free((char **)&dead);
}
/*
* Resize away any dead processes at the end
*/
for (i = process_list_size - 1; i >= 0; i--)
{
if (process_list[i])
break;
}
if (process_list_size != i + 1)
{
process_list_size = i + 1;
RESIZE(process_list, Process, process_list_size);
}
}
/*
* ignore_process: When we no longer want to communicate with the process
* any longer, we call here. It continues execution until it is done, but
* we are oblivious to any output it sends. Now, it will get an EOF
* condition on its output fd, so it will probably either take the hint, or
* its output will go the bit bucket (which we want to happen)
*/
static void ignore_process (int index)
{
Process *proc;
if (valid_process_index(index) == 0)
return;
proc = process_list[index];
if (proc->p_stdin != -1)
proc->p_stdin = new_close(proc->p_stdin);
if (proc->p_stdout != -1)
proc->p_stdout = new_close(proc->p_stdout);
if (proc->p_stderr != -1)
proc->p_stderr = new_close(proc->p_stderr);
proc->dumb = 1;
}
/*
* kill_process: sends the given signal to the specified process. It does
* not delete the process from the process table or anything like that, it
* only is for sending a signal to a sub process (most likely in an attempt
* to kill it.) The actual reaping of the children will take place async
* on the next parsing run.
*/
void kill_process (int kill_index, int sig)
{
pid_t pgid;
if (!process_list || kill_index > process_list_size ||
!process_list[kill_index])
{
say("There is no such process %d", kill_index);
return;
}
say("Sending signal %s (%d) to process %d: %s",
#if defined(_GNU_SOURCE)
strsignal(sig),
#else
sys_siglist[sig],
#endif
sig, kill_index, process_list[kill_index]->name);
#ifdef HAVE_GETPGID
pgid = getpgid(process_list[kill_index]->pid);
#else
# ifndef GETPGRP_VOID
pgid = getpgrp(process_list[kill_index]->pid);
# else
pgid = process_list[kill_index]->pid;
# endif
#endif
#ifndef HAVE_KILLPG
# define killpg(pg, sig) kill(-(pg), (sig))
#endif
/* The exec'd process shouldn't be in our process group */
if (pgid == getpid())
{
yell("--- exec'd process is in my job control session! Something is hosed ---");
return;
}
killpg(pgid, sig);
kill(process_list[kill_index]->pid, sig);
}
/*
* This kills (sends a signal, *NOT* ``make it stop running'') all of the
* currently running subprocesses with the given signal. Presumably this
* is because you want them to die.
*
* Remember that UNIX signals are asynchornous. At best, you should assume
* that they have an advisory effect. You can tell a process that it should
* die, but you cannot tell it *when* it will die -- that is up to the system.
* That means that it is pointless to assume the condition of any of the
* kill()ed processes after the kill(). They may indeed be dead, or they may
* be ``sleeping but runnable'', or they might even be waiting for a hardware
* condition (such as a swap in). You do not know when the process will
* actually die. It could be 15 ns, it could be 15 minutes, it could be
* 15 years. Its also useful to note that we, as the parent process, will not
* recieve the SIGCHLD signal until after the child dies. That means it is
* pointless to try to reap any children processes here. The main io()
* loop handles reaping children (by calling get_child_exit()).
*/
static void kill_all_processes (int signo)
{
int i;
int tmp;
tmp = window_display;
window_display = 0;
for (i = 0; i < process_list_size; i++)
{
if (process_list[i])
{
ignore_process(i);
kill_process(i, signo);
}
}
window_display = tmp;
}
/* * * * * * logical stuff * * * * * * */
/*
* valid_process_index: checks to see if index refers to a valid running
* process and returns true if this is the case. Returns false otherwise
*/
static int valid_process_index (int process)
{
if ((process < 0) || (process >= process_list_size) || !process_list[process])
{
say("No such process number %d", process);
return (0);
}
return (1);
}
static int is_logical_unique (char *logical)
{
Process *proc;
int i;
if (!logical)
return 1;
for (i = 0; i < process_list_size; i++)
{
if (!(proc = process_list[i]) || !proc->logical)
continue;
if (!my_stricmp(proc->logical, logical))
return 0;
}
return 1;
}
/*
* logical_to_index: converts a logical process name to it's approriate index
* in the process list, or -1 if not found
*/
int logical_to_index (const char *logical)
{
Process *proc;
int i;
for (i = 0; i < process_list_size; i++)
{
if (!(proc = process_list[i]) || !proc->logical)
continue;
if (!my_stricmp(proc->logical, logical))
return i;
}
return -1;
}
/*
* get_process_index: parses out a process index or logical name from the
* given string
*/
int get_process_index (char **args)
{
char *s = next_arg(*args, args);
return is_valid_process(s);
}
/*
* is_valid_process: tells me if the spec is a process that is either
* running or still has not closed its pipes, or both.
*/
int is_valid_process (const char *arg)
{
if (!arg || *arg != '%')
return -1;
arg++;
if (is_number((char *)arg) && valid_process_index(my_atol((char *)arg)))
return my_atol((char *)arg);
else
return logical_to_index(arg);
return -1;
}
int process_is_running (char *arg)
{
int idx = is_valid_process(arg);
if (idx == -1)
return 0;
else
return 1;
}
/*
* set_process_bits: This will set the bits in a fd_set map for each of the
* process descriptors.
*/
void set_process_bits(fd_set *rd)
{
int i;
Process *proc;
if (process_list)
{
for (i = 0; i < process_list_size; i++)
{
if ((proc = process_list[i]) != NULL)
{
if (proc->p_stdout != -1)
FD_SET(proc->p_stdout, rd);
if (proc->p_stderr != -1)
FD_SET(proc->p_stderr, rd);
}
}
}
}
|