-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecufork.c
847 lines (745 loc) · 20.1 KB
/
ecufork.c
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
#if 0
#define FORK_DEBUG
#endif
/*+-------------------------------------------------------------------------
ecufork.c -- ecu spawning ground
Defined functions:
exec_cmd(cmdstr)
expand_wildcard_list(wild, expcmd)
extract_stdin_stdout_stderr(cmdargv,pfd_stdin,pfd_stdout,pfd_stderr)
find_executable(progname)
fork_cmd(cmdstr)
is_executable(progname)
shell(shellcmd)
smart_fork()
This boy is no longer a boy. Now he is a man. He is a small
boy, but his heart is big. His name shall be Little Big Man.
-- Chief Lodge Skins
--------------------------------------------------------------------------*/
/*+:EDITS:*/
/*:04-26-2000-11:15-wht@bob-RELEASE 4.42 */
/*:11-03-1997-02:[email protected] more fork debug for wht */
/*:01-24-1997-02:37-wht@yuriatin-SOURCE RELEASE 4.00 */
/*:10-16-1996-20:13-wht@yuriatin-redirection took a while */
/*:10-16-1996-19:05-wht@yuriatin-support redirect on fork */
/*:10-16-1996-03:30-wht@yuriatin-add fork_cmd */
/*:09-11-1996-20:[email protected] telnet,curses,structural overhaul */
/*:08-11-1996-02:10-wht@kepler-rename ecu_log_event to logevent */
/*:02-28-1996-21:30-wht@kepler-must use /bin/sh for ">" commands */
/*:01-27-1996-21:04-wht@n4hgf-drop using custom SHELL - use csh or bash */
/*:01-01-1996-19:38-wht@kepler-no endwin in shell unless needed */
/*:12-11-1995-17:41-wht@kepler-any child of ecu gets pid saved in child-pid */
/*:12-10-1995-17:43-wht@kepler-use $SHELL, pwd.pw_shell, then default */
/*:11-23-1995-11:20-wht@kepler-source control 3.37 for tsx-11 */
/*:11-14-1995-10:[email protected] control point: SOCKETS */
/*:01-12-1995-15:19-wht@n4hgf-apply Andrew Chernov 8-bit clean+FreeBSD patch */
/*:05-04-1994-04:38-wht@n4hgf-ECU release 3.30 */
/*:01-06-1994-05:52-wht@n4hgf-clean up LINUX port */
/*:12-12-1993-13:27-wht@n4hgf-differentiate MAX_EXEC_ARG only for i286 */
/*:11-12-1993-11:00-wht@n4hgf-Linux changes by [email protected] */
/*:09-10-1992-13:58-wht@n4hgf-ECU release 3.20 */
/*:08-22-1992-15:38-wht@n4hgf-ECU release 3.20 BETA */
/*:04-29-1992-13:29-wht@n4hgf-ignore SIGQUIT when in executing a child */
/*:09-25-1991-18:02-wht@n4hgf2-find_executable flunks directories now */
/*:09-06-1991-04:20-wht@n4hgf2-expand_wildcard_list minor bug */
/*:08-29-1991-01:56-wht@n4hgf2-use max esd size instead of 5120 */
/*:07-25-1991-12:55-wht@n4hgf-ECU release 3.10 */
/*:07-17-1991-07:04-wht@n4hgf-avoid SCO UNIX nap bug */
/*:09-19-1990-19:36-wht@n4hgf-logevent now gets pid for log from caller */
/*:08-14-1990-20:[email protected] old edit history */
#include "ecu.h"
#include "ecu_pwd.h"
#include "esd.h"
#include "ecufork.h"
extern int last_child_wait_status;
extern int last_child_wait_pid;
CFG_PidType child_pid = -1;
#ifdef M_I286
#define MAX_EXEC_ARG 512
#else
#define MAX_EXEC_ARG 2048
#endif /* M_I286 */
#ifdef linux
char *default_shell_path = "/bin/bash"; /* probably moot since sh==bash */
#else
char *default_shell_path = "/bin/csh";
#endif
/*+-------------------------------------------------------------------------
smart_fork()
--------------------------------------------------------------------------*/
int
smart_fork()
{
int count = 5;
int pid;
while (count--)
{
if ((pid = fork()) >= 0)
return (pid);
if (count)
Nap(40L);
}
return (-1);
} /* end of smart_fork */
/*+-----------------------------------------------------------------------
shell(shellcmd)
param 'shellcmd' is a shell command prefixed with either
a '!', '$', '>' character.
'!' causes the command to run as a normal subshell of a process.
'$' causes the communications line to be stdin and stdout
for the spawned shell
'>' causes spawned shell to receive exactly sames files as ecu
------------------------------------------------------------------------*/
void
shell(shellcmd)
char *shellcmd;
{
int itmp;
char *shell_path;
#if defined(FORK_DEBUG)
char s80[80];
#endif
int wait_status;
int restart_rcvr = need_rcvr_restart();
char *getenv();
kill_rcvr_process(SIGUSR1); /* stop receiver process gracefully */
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGUSR1, SIG_IGN);
signal(SIGUSR2, SIG_IGN);
signal(SIGCLD, SIG_DFL);
ttymode(0); /* set canonical tty mode */
if ((child_pid = smart_fork()) < 0)
{
ff(se, "Cannot fork\r\n");
if (restart_rcvr)
start_rcvr_process(1);
ttymode(1); /* control tty back to raw mode */
xmtr_signals();
return;
}
#if defined(linux) || defined(__FreeBSD__)
windows_end(0);
#endif
if (!child_pid) /* we are the spawned (going to call shell) */
{
if (*shellcmd != '>')/* '>' prefix means leave fd's alone! */
{
/*
* Hook-up our "standard output" to either the tty or the line
* as appropriate for '!' or '$'
*/
close(TTYOUT);
fcntl(((*shellcmd == '$') ? shm->Liofd : TTYERR), F_DUPFD, TTYOUT);
if (*shellcmd == '$')
{
close(TTYIN);
fcntl(shm->Liofd, F_DUPFD, TTYIN);
}
close(shm->Liofd);
}
child_signals(); /* signals for child */
shell_path = 0;
if (*shellcmd == '>')
shell_path = "/bin/sh";
else if (*shellcmd == '!')
{
if (!(shell_path = getenv("SHELL")))
{
struct passwd *pw;
if (pw = getpwuid(getuid()))
shell_path = pw->pw_shell;
endpwent();
}
}
if (!shell_path)
shell_path = default_shell_path;
shellcmd++;
child_signals();
if (!*shellcmd)
execl(shell_path, shell_path, (char *)0);
else
execl(shell_path, shell_path, "-c", shellcmd, (char *)0);
ff(se, "cannot execute %s\r\n", shell_path); /* should not get here */
_exit(255); /* end of spawned process */
} /* end of if child process */
#if defined(FORK_DEBUG)
sprintf(s80, "DEBUG fork shell pid %d", child_pid);
logevent(getpid(), s80); /* shell */
#endif
while (((itmp = wait(&wait_status)) != child_pid) && (itmp != -1))
;
last_child_wait_status = wait_status;
last_child_wait_pid = child_pid;
child_pid = -1;
xmtr_signals(); /* restore standard xmtr signals */
ttymode(1); /* control tty back to raw mode */
/* any comm program will probably doodle with the line characteristics. */
/* we want to make sure they are restored to normal */
lreset_ksr(); /* restore comm line params */
if (restart_rcvr)
start_rcvr_process(1);
} /* end of shell */
/*+-------------------------------------------------------------------------
is_executable(progname)
--------------------------------------------------------------------------*/
int
is_executable(progname)
char *progname;
{
struct stat ss;
if (stat(progname, &ss) < 0) /* if cannot stat, flunk */
return (0);
if ((ss.st_mode & 0111) == 0) /* if no --x--x--x, flunk */
return (0);
if ((ss.st_mode & S_IFMT) != S_IFREG) /* if no --x--x--x, flunk */
return (0);
return (1); /* whew, this OUGHT to work */
} /* end of is_executable */
/*+-------------------------------------------------------------------------
find_executable(progname)
PATH=':/usr/wht/bin:/bin:/usr/bin:/usr/wht/bin:/etc/tuckerware' len=56
--------------------------------------------------------------------------*/
char *
find_executable(progname)
char *progname;
{
int itmp;
static char *path_buf = (char *)0;
#define PATHNAME_QUAN 32
static char *path_name[PATHNAME_QUAN + 1];
static char rtn_path[256];
static int path_count = 0;
char *cp;
char *getenv();
if (path_buf == (char *)0)
{
if ((cp = getenv("PATH")) == (char *)0)
return (cp);
if (!(path_buf = malloc(strlen(cp) + 1)))
return ((char *)0);
strcpy(path_buf, cp);
path_name[PATHNAME_QUAN] = (char *)0;
cp = path_buf;
for (path_count = 0; path_count < PATHNAME_QUAN; path_count++)
{
if (*cp == 0)
break;
path_name[path_count] = cp;
while ((*cp != ':') && (*cp != 0))
cp++;
if (*cp == ':')
*cp++ = 0;
}
} /* end of get and process path env variable */
/* look for executable */
for (itmp = 0; itmp < path_count; itmp++)
{
if (*path_name[itmp] == 0) /* if null path (./) */
strcpy(rtn_path, "./");
else
sprintf(rtn_path, "%s/", path_name[itmp]);
strcat(rtn_path, progname);
if (is_executable(rtn_path))
return (rtn_path);
}
return ((char *)0);
} /* end of find_executable */
/*+-------------------------------------------------------------------------
extract_stdin_stdout_stderr(cmdargv,pfd_stdin,pfd_stdout,pfd_stderr)
--------------------------------------------------------------------------*/
int
extract_stdin_stdout_stderr(cmdargv, pfd_stdin, pfd_stdout, pfd_stderr)
char **cmdargv;
int *pfd_stdin;
int *pfd_stdout;
int *pfd_stderr;
{
int rtn = 0;
char *arg;
for (; *cmdargv; cmdargv++)
{
arg = *cmdargv;
/*
* if argument is a redirection directive
*/
if (strchr("><", *arg))
{
char *path;
int append_flag; /* true if append to stdout/err */
int stderr_flag; /* true if stderr as well as stdout */
char **cmdsrc; /* squash pointer temp */
char **cmddst; /* squash pointer temp */
if (!(path = *(cmdargv + 1))) /* if nothing after redirect */
{
pprintf("no path after '%s'\n", arg);
return (-1);
}
switch (*arg)
{
case '<': /* stdin */
if ((*pfd_stdin = open(path, O_RDONLY, 0)) < 0)
{
pputs("could not open for stdin: ");
pperror(path);
rtn = -1;
break;
}
if (proc_trace)
pprintf("opened '%s' for stdin\n", path);
break;
case '>': /* stdout */
append_flag = !!strchr(arg + 1, '>'); /* append? */
stderr_flag = !!strchr(arg, '&'); /* stderr too? */
if ((*pfd_stdout = open(path, O_WRONLY | O_CREAT |
((append_flag) ? O_APPEND : O_TRUNC), 0644)) < 0)
{
pputs("could not open for stdout: ");
pperror(path);
rtn = -1;
break;
}
if (stderr_flag)
*pfd_stderr = *pfd_stdout;
if (proc_trace)
{
pprintf("opened '%s' for stdout", path);
if (stderr_flag)
pputs(" and stderr");
pprintf(" (%s)\n",
(append_flag) ? "append" : "scratch");
}
break;
}
/*
* squash redirect and path from arguments
*/
cmdsrc = cmdargv + 2;
cmddst = cmdargv;
while (1)
{
*cmddst = *cmdsrc;
if (!*cmddst)
break;
cmdsrc++, cmddst++;
}
cmdargv--;
}
}
if (rtn)
{
close(*pfd_stdin);
*pfd_stdin = -1;
close(*pfd_stdout);
*pfd_stdout = -1;
close(*pfd_stderr);
*pfd_stderr = -1;
}
return (rtn);
} /* end of extract_stdin_stdout_stderr */
/*+-------------------------------------------------------------------------
fork_cmd(cmdstr) - execute an arbitrary program with arguments
--------------------------------------------------------------------------*/
int
fork_cmd(cmdstr)
char *cmdstr;
{
char *cmdpath;
char *cmdargv[MAX_EXEC_ARG];
int itmp;
int fd_stdin;
int fd_stdout;
int fd_stderr;
#if defined(FORK_DEBUG)
char s256[256];
strcpy(s256, "DEBUG exec ");
strncat(s256, cmdstr, sizeof(s256) - 12);
s256[sizeof(s256) - 12] = 0;
logevent(getpid(), s256);
#endif
build_arg_array(cmdstr, cmdargv, MAX_EXEC_ARG, &itmp);
if (itmp == MAX_EXEC_ARG)
{
pputs("Too many arguments to command\n");
return (-1);
}
else if (!itmp)
{
pputs("null command\n");
return (-1);
}
/*
* peel redirection out of arguments
*/
fd_stdin = -1;
fd_stdout = -1;
fd_stderr = -1;
itmp = extract_stdin_stdout_stderr(cmdargv,
&fd_stdin, &fd_stdout, &fd_stderr);
if(itmp)
return(-1);
if (*cmdargv[0] == '/')
{
cmdpath = cmdargv[0];
cmdargv[0] = strrchr(cmdargv[0], '/') + 1;
}
else
{
if ((cmdpath = find_executable(cmdargv[0])) == (char *)0)
{
pputs("Cannot find %s\n", cmdargv[0]);
return (-1);
}
}
if ((child_pid = smart_fork()) < 0)
{
pputs("Cannot fork\n");
return (-1);
}
if (child_pid == 0) /* we are the spawned (going to call exec) */
{
int fd;
child_signals();
/*
* redirection
*/
if (fd_stdin > -1)
{
close(TTYIN);
dup2(fd_stdin, TTYIN);
}
if (fd_stdout > -1)
{
close(TTYOUT);
dup2(fd_stdout, TTYOUT);
}
if (fd_stderr > -1)
{
close(TTYERR);
dup2(fd_stderr, TTYERR);
}
/*
* close all files except stdin/out/err and comm line
*/
for (fd = TTYERR + 1; fd < 100; fd++)
{
if (fd != shm->Liofd)
close(fd);
}
execv(cmdpath, cmdargv);
perror(cmdpath);
_exit(255); /* end of spawned process */
} /* end of if child process */
return (child_pid);
} /* end of fork_cmd */
/*+-------------------------------------------------------------------------
exec_cmd(cmdstr) - execute an arbitrary program with arguments
kills rcvr process if alive and restarts it when done if was alive
--------------------------------------------------------------------------*/
int
exec_cmd(cmdstr)
char *cmdstr;
{
char *cmdpath;
char *cmdargv[MAX_EXEC_ARG];
int itmp;
int restart_rcvr = need_rcvr_restart();
int old_ttymode = get_ttymode();
int wait_status = 0;
#if defined(FORK_DEBUG)
char s256[256];
strcpy(s256, "DEBUG exec ");
strncat(s256, cmdstr, sizeof(s256) - 12);
s256[sizeof(s256) - 12] = 0;
logevent(getpid(), s256);
#endif
build_arg_array(cmdstr, cmdargv, MAX_EXEC_ARG, &itmp);
if (itmp == MAX_EXEC_ARG)
{
pputs("Too many arguments to command\n");
return (-1);
}
else if (!itmp)
{
pputs("null command\n");
return (-1);
}
if (*cmdargv[0] == '/')
{
cmdpath = cmdargv[0];
cmdargv[0] = strrchr(cmdargv[0], '/') + 1;
}
else
{
if ((cmdpath = find_executable(cmdargv[0])) == (char *)0)
{
pputs("Cannot find %s\n", cmdargv[0]);
return (-1);
}
}
kill_rcvr_process(SIGUSR1); /* stop receiver process gracefully */
logevent(getpid(), cmdpath);
/* this code executed by the father (forking) process */
/* wait on death of child (morbid in life, but ok here) */
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGUSR1, SIG_IGN);
signal(SIGUSR2, SIG_IGN);
signal(SIGCLD, SIG_DFL);
if ((child_pid = smart_fork()) < 0)
{
pputs("Cannot fork\n");
if (restart_rcvr)
start_rcvr_process(1);
xmtr_signals();
return (-1);
}
if (child_pid == 0) /* we are the spawned (going to call exec) */
{
ttymode(0); /* set canonical tty mode */
child_signals();
execv(cmdpath, cmdargv);
perror(cmdpath);
_exit(255); /* end of spawned process */
} /* end of if child process */
wait_status = 0;
while (((itmp = wait(&wait_status)) != child_pid) && (itmp != -1))
;
last_child_wait_status = wait_status;
last_child_wait_pid = child_pid;
child_pid = -1;
/* resume our normally scheduled program */
lreset_ksr(); /* restore comm line params */
ttymode(old_ttymode); /* control tty back to original */
if (restart_rcvr)
start_rcvr_process(1);
xmtr_signals();
if (last_child_wait_status == -1)
pprintf("last child exited with status -1\n");
return (last_child_wait_status);
} /* end of exec_cmd */
/*+-------------------------------------------------------------------------
expand_wildcard_list(wild,&expcmd)
called with 'foo <wildcardlist>' for command expansion prior to exec()
or '<wildcardlist>' to expand a list of files.
If called with 'foo'-style wild, anything you want to protect from csh
globbing or other interpretation must be properly protected (quoted) --
AND quoting will be removed one level by the csh.
if return 0, wild has been expanded, expcmd must be free()'d when done
if return -1, error, expcmd has error message (static message: DO NOT FREE)
--------------------------------------------------------------------------*/
int
expand_wildcard_list(wild, expcmd)
char *wild;
char **expcmd;
{
char *cp;
#define P_READ 0
#define P_WRITE 1
int stdout_pipe[2];
int stderr_pipe[2];
int count;
int expcmd_size = 0;
int itmp;
int wait_status;
int restart_rcvr = need_rcvr_restart();
char *shell_path;
FILE *fp_pipe = (FILE *) 0;
char *echo_cmd;
static char static_s256[256]; /* MUST BE STATIC */
static char *pipe_err_msg = "system error: no pipe";
static char *mem_err_msg = "system error: no memory";
if (strchr(wild, '<') || strchr(wild, '>') || strchr(wild, '&'))
{
*expcmd = "illegal characters: '<', '>' or '&'";
return (-1);
}
if (pipe(stdout_pipe) < 0)
{
*expcmd = pipe_err_msg;
return (-1);
}
if (pipe(stderr_pipe) < 0)
{
close(stdout_pipe[P_READ]);
close(stdout_pipe[P_WRITE]);
*expcmd = pipe_err_msg;
return (-1);
}
if (!(echo_cmd = malloc(strlen(wild) + 10)))
{
close(stdout_pipe[P_READ]);
close(stdout_pipe[P_WRITE]);
close(stderr_pipe[P_READ]);
close(stderr_pipe[P_WRITE]);
*expcmd = mem_err_msg;
return (-1);
}
strcpy(echo_cmd, "echo ");
strcat(echo_cmd, wild);
kill_rcvr_process(SIGUSR1); /* stop receiver process gracefully */
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGUSR1, SIG_IGN);
signal(SIGUSR2, SIG_IGN);
signal(SIGCLD, SIG_DFL);
#if 0
/*
* does not work for /bin/sh, so kill this code until brighter days
*/
shell_path = 0;
if (!(shell_path = getenv("SHELL")))
{
struct passwd *pw;
if (pw = getpwuid(getuid()))
shell_path = pw->pw_shell;
endpwent();
}
if (!shell_path)
shell_path = default_shell_path;
#else
shell_path = default_shell_path;
#endif
#if defined(FORK_DEBUG)
sprintf(static_s256, "DEBUG expand sh=%s `%.128s'", shell_path, echo_cmd);
logevent(getpid(), static_s256); /* expand_wildcard_list */
#endif
if ((child_pid = smart_fork()) == 0)
{
int null = open("/dev/null", O_WRONLY, 0);
close(stdout_pipe[P_READ]);
close(TTYOUT);
dup(stdout_pipe[P_WRITE]);
close(stdout_pipe[P_WRITE]);
close(TTYERR);
dup(stderr_pipe[P_WRITE]);
close(stderr_pipe[P_WRITE]);
close(null);
child_signals();
execl(shell_path, shell_path,
"-e", "-c", echo_cmd, (char *)0);
_exit(255);
}
#if defined(FORK_DEBUG)
sprintf(static_s256, "DEBUG expand pid %d", child_pid);
logevent(getpid(), static_s256); /* expand_wildcard_list */
#endif
free(echo_cmd);
close(stdout_pipe[P_WRITE]);
close(stderr_pipe[P_WRITE]);
if (child_pid == -1)
{
close(stdout_pipe[P_READ]);
close(stderr_pipe[P_READ]);
*expcmd = "could not fork";
if (restart_rcvr)
start_rcvr_process(0);
xmtr_signals();
return (-1);
}
if (!(*expcmd = malloc(expcmd_size = ESD_MAXSZ)))
{
close(stdout_pipe[P_READ]);
close(stderr_pipe[P_READ]);
kill(child_pid, SIGKILL);
*expcmd = mem_err_msg;
if (restart_rcvr)
start_rcvr_process(0);
xmtr_signals();
return (-1);
}
if (!(fp_pipe = fdopen(stdout_pipe[P_READ], "r")) ||
((count = fread(*expcmd, 1, expcmd_size, fp_pipe)) < 0))
{
free(*expcmd);
kill(child_pid, SIGKILL);
close(stdout_pipe[P_READ]);
close(stderr_pipe[P_READ]);
*expcmd = "error reading wild list expansion";
if (restart_rcvr)
start_rcvr_process(0);
xmtr_signals();
return (-1);
}
/*
* make sure stdout is closed
*/
if (fp_pipe)
fclose(fp_pipe);
close(stdout_pipe[P_READ]);
/*
* place trailing null kill trailing new line
*/
if (count)
{
cp = (*expcmd) + count;
*cp-- = 0;
if (*cp == '\n')
{
*cp = 0;
count--;
}
}
/*
* if no expansion, read stderr to find out why
*/
if (!count)
{
free(*expcmd);
count = read(stderr_pipe[P_READ], static_s256, sizeof(static_s256) - 1);
if (count < 0)
strcpy(static_s256, strerror(errno));
else
static_s256[count] = 0;
if (static_s256[count - 1] == '\n')
static_s256[count - 1] = 0;
close(stderr_pipe[P_READ]);
if (strncmp(static_s256, "echo: ", 6))
*expcmd = static_s256;
else
*expcmd = static_s256 + 6;
if (restart_rcvr)
start_rcvr_process(0);
return (-1);
}
/*
* clean up zombie
*/
wait_status = 0;
while (((itmp = wait(&wait_status)) != child_pid) && (itmp != -1))
;
child_pid = -1;
xmtr_signals();
/*
* if bad termination status, read stderr
*/
if (wait_status)
{
free(*expcmd);
count = read(stderr_pipe[P_READ], static_s256, sizeof(static_s256) - 1);
if (count < 0)
strcpy(static_s256, strerror(errno));
else
static_s256[count] = 0;
if (static_s256[count - 1] == '\n')
static_s256[count - 1] = 0;
close(stderr_pipe[P_READ]);
if (strncmp(static_s256, "echo: ", 6))
*expcmd = static_s256;
else
*expcmd = static_s256 + 6;
if (restart_rcvr)
start_rcvr_process(0);
return (-1);
}
close(stderr_pipe[P_READ]);
/*
* whew: we have (I think) a file list expansion
*/
if (restart_rcvr)
start_rcvr_process(0);
return (0);
} /* end of expand_wildcard_list */
/* vi: set tabstop=4 shiftwidth=4: */
/* end of ecufork.c */