-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.c
2992 lines (2727 loc) · 79.6 KB
/
job.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
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
/* $NetBSD: job.c,v 1.155 2010/09/13 15:36:57 sjg Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Adam de Boor.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 1988, 1989 by Adam de Boor
* Copyright (c) 1989 by Berkeley Softworks
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Adam de Boor.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: job.c,v 1.155 2010/09/13 15:36:57 sjg Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)job.c 8.2 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: job.c,v 1.155 2010/09/13 15:36:57 sjg Exp $");
#endif
#endif /* not lint */
#endif
/*-
* job.c --
* handle the creation etc. of our child processes.
*
* Interface:
* Job_Make Start the creation of the given target.
*
* Job_CatchChildren Check for and handle the termination of any
* children. This must be called reasonably
* frequently to keep the whole make going at
* a decent clip, since job table entries aren't
* removed until their process is caught this way.
*
* Job_CatchOutput Print any output our children have produced.
* Should also be called fairly frequently to
* keep the user informed of what's going on.
* If no output is waiting, it will block for
* a time given by the SEL_* constants, below,
* or until output is ready.
*
* Job_Init Called to intialize this module. in addition,
* any commands attached to the .BEGIN target
* are executed before this function returns.
* Hence, the makefile must have been parsed
* before this function is called.
*
* Job_End Cleanup any memory used.
*
* Job_ParseShell Given the line following a .SHELL target, parse
* the line as a shell specification. Returns
* FAILURE if the spec was incorrect.
*
* Job_Finish Perform any final processing which needs doing.
* This includes the execution of any commands
* which have been/were attached to the .END
* target. It should only be called when the
* job table is empty.
*
* Job_AbortAll Abort all currently running jobs. It doesn't
* handle output or do anything for the jobs,
* just kills them. It should only be called in
* an emergency, as it were.
*
* Job_CheckCommands Verify that the commands for a target are
* ok. Provide them if necessary and possible.
*
* Job_Touch Update a target without really updating it.
*
* Job_Wait Wait for all currently-running jobs to finish.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/time.h>
#include "wait.h"
#include <errno.h>
#include <fcntl.h>
#if !defined(USE_SELECT) && defined(HAVE_POLL_H)
#include <poll.h>
#else
#ifndef USE_SELECT /* no poll.h */
# define USE_SELECT
#endif
#if defined(HAVE_SYS_SELECT_H)
# include <sys/select.h>
#endif
#endif
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <utime.h>
#if defined(HAVE_SYS_SOCKET_H)
# include <sys/socket.h>
#endif
#include "make.h"
#include "hash.h"
#include "dir.h"
#include "job.h"
#include "pathnames.h"
#include "trace.h"
# define STATIC static
/*
* error handling variables
*/
static int errors = 0; /* number of errors reported */
static int aborting = 0; /* why is the make aborting? */
#define ABORT_ERROR 1 /* Because of an error */
#define ABORT_INTERRUPT 2 /* Because it was interrupted */
#define ABORT_WAIT 3 /* Waiting for jobs to finish */
#define JOB_TOKENS "+EI+" /* Token to requeue for each abort state */
/*
* this tracks the number of tokens currently "out" to build jobs.
*/
int jobTokensRunning = 0;
int not_parallel = 0; /* set if .NOT_PARALLEL */
/*
* XXX: Avoid SunOS bug... FILENO() is fp->_file, and file
* is a char! So when we go above 127 we turn negative!
*/
#define FILENO(a) ((unsigned) fileno(a))
/*
* post-make command processing. The node postCommands is really just the
* .END target but we keep it around to avoid having to search for it
* all the time.
*/
static GNode *postCommands = NULL;
/* node containing commands to execute when
* everything else is done */
static int numCommands; /* The number of commands actually printed
* for a target. Should this number be
* 0, no shell will be executed. */
/*
* Return values from JobStart.
*/
#define JOB_RUNNING 0 /* Job is running */
#define JOB_ERROR 1 /* Error in starting the job */
#define JOB_FINISHED 2 /* The job is already finished */
/*
* Descriptions for various shells.
*
* The build environment may set DEFSHELL_INDEX to one of
* DEFSHELL_INDEX_SH, DEFSHELL_INDEX_KSH, or DEFSHELL_INDEX_CSH, to
* select one of the prefedined shells as the default shell.
*
* Alternatively, the build environment may set DEFSHELL_CUSTOM to the
* name or the full path of a sh-compatible shell, which will be used as
* the default shell.
*
* ".SHELL" lines in Makefiles can choose the default shell from the
# set defined here, or add additional shells.
*/
#ifdef DEFSHELL_CUSTOM
#define DEFSHELL_INDEX_CUSTOM 0
#define DEFSHELL_INDEX_SH 1
#define DEFSHELL_INDEX_KSH 2
#define DEFSHELL_INDEX_CSH 3
#else /* !DEFSHELL_CUSTOM */
#define DEFSHELL_INDEX_SH 0
#define DEFSHELL_INDEX_KSH 1
#define DEFSHELL_INDEX_CSH 2
#endif /* !DEFSHELL_CUSTOM */
#ifndef DEFSHELL_INDEX
#define DEFSHELL_INDEX 0 /* DEFSHELL_INDEX_CUSTOM or DEFSHELL_INDEX_SH */
#endif /* !DEFSHELL_INDEX */
static Shell shells[] = {
#ifdef DEFSHELL_CUSTOM
/*
* An sh-compatible shell with a non-standard name.
*
* Keep this in sync with the "sh" description below, but avoid
* non-portable features that might not be supplied by all
* sh-compatible shells.
*/
{
DEFSHELL_CUSTOM,
FALSE, "", "", "", 0,
FALSE, "echo \"%s\"\n", "%s\n", "{ %s \n} || exit $?\n", "'\n'", '#',
"",
"",
},
#endif /* DEFSHELL_CUSTOM */
/*
* SH description. Echo control is also possible and, under
* sun UNIX anyway, one can even control error checking.
*/
{
"sh",
FALSE, "", "", "", 0,
FALSE, "echo \"%s\"\n", "%s\n", "{ %s \n} || exit $?\n", "'\n'", '#',
#if defined(MAKE_NATIVE) && defined(__NetBSD__)
"q",
#else
"",
#endif
"",
},
/*
* KSH description.
*/
{
"ksh",
TRUE, "set +v", "set -v", "set +v", 6,
FALSE, "echo \"%s\"\n", "%s\n", "{ %s \n} || exit $?\n", "'\n'", '#',
"v",
"",
},
/*
* CSH description. The csh can do echo control by playing
* with the setting of the 'echo' shell variable. Sadly,
* however, it is unable to do error control nicely.
*/
{
"csh",
TRUE, "unset verbose", "set verbose", "unset verbose", 10,
FALSE, "echo \"%s\"\n", "csh -c \"%s || exit 0\"\n", "", "'\\\n'", '#',
"v", "e",
},
/*
* UNKNOWN.
*/
{
NULL,
FALSE, NULL, NULL, NULL, 0,
FALSE, NULL, NULL, NULL, NULL, 0,
NULL, NULL,
}
};
static Shell *commandShell = &shells[DEFSHELL_INDEX]; /* this is the shell to
* which we pass all
* commands in the Makefile.
* It is set by the
* Job_ParseShell function */
const char *shellPath = NULL, /* full pathname of
* executable image */
*shellName = NULL; /* last component of shell */
static const char *shellArgv = NULL; /* Custom shell args */
STATIC Job *job_table; /* The structures that describe them */
STATIC Job *job_table_end; /* job_table + maxJobs */
static int wantToken; /* we want a token */
static int lurking_children = 0;
static int make_suspended = 0; /* non-zero if we've seen a SIGTSTP (etc) */
/*
* Set of descriptors of pipes connected to
* the output channels of children
*/
static struct pollfd *fds = NULL;
static Job **jobfds = NULL;
static int nfds = 0;
static void watchfd(Job *);
static void clearfd(Job *);
static int readyfd(Job *);
STATIC GNode *lastNode; /* The node for which output was most recently
* produced. */
STATIC const char *targFmt; /* Format string to use to head output from a
* job when it's not the most-recent job heard
* from */
static char *targPrefix = NULL; /* What we print at the start of targFmt */
static Job tokenWaitJob; /* token wait pseudo-job */
static Job childExitJob; /* child exit pseudo-job */
#define CHILD_EXIT "."
#define DO_JOB_RESUME "R"
#define TARG_FMT "%s %s ---\n" /* Default format */
#define MESSAGE(fp, gn) \
(void)fprintf(fp, targFmt, targPrefix, gn->name)
static sigset_t caught_signals; /* Set of signals we handle */
#if defined(SYSV)
#define KILLPG(pid, sig) kill(-(pid), (sig))
#else
#define KILLPG(pid, sig) killpg((pid), (sig))
#endif
static void JobChildSig(int);
static void JobContinueSig(int);
static Job *JobFindPid(int, int, Boolean);
static int JobPrintCommand(void *, void *);
static int JobSaveCommand(void *, void *);
static void JobClose(Job *);
static void JobExec(Job *, char **);
static void JobMakeArgv(Job *, char **);
static int JobStart(GNode *, int);
static char *JobOutput(Job *, char *, char *, int);
static void JobDoOutput(Job *, Boolean);
static Shell *JobMatchShell(const char *);
static void JobInterrupt(int, int);
static void JobRestartJobs(void);
static void JobTokenAdd(void);
static void JobSigLock(sigset_t *);
static void JobSigUnlock(sigset_t *);
static void JobSigReset(void);
const char *malloc_options="A";
static void
job_table_dump(const char *where)
{
Job *job;
fprintf(debug_file, "job table @ %s\n", where);
for (job = job_table; job < job_table_end; job++) {
fprintf(debug_file, "job %d, status %d, flags %d, pid %d\n",
(int)(job - job_table), job->job_state, job->flags, job->pid);
}
}
/*
* JobSigLock/JobSigUnlock
*
* Signal lock routines to get exclusive access. Currently used to
* protect `jobs' and `stoppedJobs' list manipulations.
*/
static void JobSigLock(sigset_t *omaskp)
{
if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) {
Punt("JobSigLock: sigprocmask: %s", strerror(errno));
sigemptyset(omaskp);
}
}
static void JobSigUnlock(sigset_t *omaskp)
{
(void)sigprocmask(SIG_SETMASK, omaskp, NULL);
}
static void
JobCreatePipe(Job *job, int minfd)
{
int i, fd;
if (pipe(job->jobPipe) == -1)
Punt("Cannot create pipe: %s", strerror(errno));
/* Set close-on-exec flag for both */
(void)fcntl(job->jobPipe[0], F_SETFD, 1);
(void)fcntl(job->jobPipe[1], F_SETFD, 1);
/*
* We mark the input side of the pipe non-blocking; we poll(2) the
* pipe when we're waiting for a job token, but we might lose the
* race for the token when a new one becomes available, so the read
* from the pipe should not block.
*/
fcntl(job->jobPipe[0], F_SETFL,
fcntl(job->jobPipe[0], F_GETFL, 0) | O_NONBLOCK);
for (i = 0; i < 2; i++) {
/* Avoid using low numbered fds */
fd = fcntl(job->jobPipe[i], F_DUPFD, minfd);
if (fd != -1) {
close(job->jobPipe[i]);
job->jobPipe[i] = fd;
}
}
}
/*-
*-----------------------------------------------------------------------
* JobCondPassSig --
* Pass a signal to a job
*
* Input:
* signop Signal to send it
*
* Side Effects:
* None, except the job may bite it.
*
*-----------------------------------------------------------------------
*/
static void
JobCondPassSig(int signo)
{
Job *job;
if (DEBUG(JOB)) {
(void)fprintf(debug_file, "JobCondPassSig(%d) called.\n", signo);
}
for (job = job_table; job < job_table_end; job++) {
if (job->job_state != JOB_ST_RUNNING)
continue;
if (DEBUG(JOB)) {
(void)fprintf(debug_file,
"JobCondPassSig passing signal %d to child %d.\n",
signo, job->pid);
}
KILLPG(job->pid, signo);
}
}
/*-
*-----------------------------------------------------------------------
* JobChldSig --
* SIGCHLD handler.
*
* Input:
* signo The signal number we've received
*
* Results:
* None.
*
* Side Effects:
* Sends a token on the child exit pipe to wake us up from
* select()/poll().
*
*-----------------------------------------------------------------------
*/
static void
JobChildSig(int signo __unused)
{
write(childExitJob.outPipe, CHILD_EXIT, 1);
}
/*-
*-----------------------------------------------------------------------
* JobContinueSig --
* Resume all stopped jobs.
*
* Input:
* signo The signal number we've received
*
* Results:
* None.
*
* Side Effects:
* Jobs start running again.
*
*-----------------------------------------------------------------------
*/
static void
JobContinueSig(int signo __unused)
{
/*
* Defer sending to SIGCONT to our stopped children until we return
* from the signal handler.
*/
write(childExitJob.outPipe, DO_JOB_RESUME, 1);
}
/*-
*-----------------------------------------------------------------------
* JobPassSig --
* Pass a signal on to all jobs, then resend to ourselves.
*
* Input:
* signo The signal number we've received
*
* Results:
* None.
*
* Side Effects:
* We die by the same signal.
*
*-----------------------------------------------------------------------
*/
static void
JobPassSig_int(int signo)
{
/* Run .INTERRUPT target then exit */
JobInterrupt(TRUE, signo);
}
static void
JobPassSig_term(int signo)
{
/* Dont run .INTERRUPT target then exit */
JobInterrupt(FALSE, signo);
}
static void
JobPassSig_suspend(int signo)
{
sigset_t nmask, omask;
struct sigaction act;
/* Suppress job started/continued messages */
make_suspended = 1;
/* Pass the signal onto every job */
JobCondPassSig(signo);
/*
* Send ourselves the signal now we've given the message to everyone else.
* Note we block everything else possible while we're getting the signal.
* This ensures that all our jobs get continued when we wake up before
* we take any other signal.
*/
sigfillset(&nmask);
sigdelset(&nmask, signo);
(void)sigprocmask(SIG_SETMASK, &nmask, &omask);
act.sa_handler = SIG_DFL;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
(void)sigaction(signo, &act, NULL);
if (DEBUG(JOB)) {
(void)fprintf(debug_file,
"JobPassSig passing signal %d to self.\n", signo);
}
(void)kill(getpid(), signo);
/*
* We've been continued.
*
* A whole host of signals continue to happen!
* SIGCHLD for any processes that actually suspended themselves.
* SIGCHLD for any processes that exited while we were alseep.
* The SIGCONT that actually caused us to wakeup.
*
* Since we defer passing the SIGCONT on to our children until
* the main processing loop, we can be sure that all the SIGCHLD
* events will have happened by then - and that the waitpid() will
* collect the child 'suspended' events.
* For correct sequencing we just need to ensure we process the
* waitpid() before passign on the SIGCONT.
*
* In any case nothing else is needed here.
*/
/* Restore handler and signal mask */
act.sa_handler = JobPassSig_suspend;
(void)sigaction(signo, &act, NULL);
(void)sigprocmask(SIG_SETMASK, &omask, NULL);
}
/*-
*-----------------------------------------------------------------------
* JobFindPid --
* Compare the pid of the job with the given pid and return 0 if they
* are equal. This function is called from Job_CatchChildren
* to find the job descriptor of the finished job.
*
* Input:
* job job to examine
* pid process id desired
*
* Results:
* Job with matching pid
*
* Side Effects:
* None
*-----------------------------------------------------------------------
*/
static Job *
JobFindPid(int pid, int status, Boolean isJobs)
{
Job *job;
for (job = job_table; job < job_table_end; job++) {
if ((job->job_state == status) && job->pid == pid)
return job;
}
if (DEBUG(JOB) && isJobs)
job_table_dump("no pid");
return NULL;
}
/*-
*-----------------------------------------------------------------------
* JobPrintCommand --
* Put out another command for the given job. If the command starts
* with an @ or a - we process it specially. In the former case,
* so long as the -s and -n flags weren't given to make, we stick
* a shell-specific echoOff command in the script. In the latter,
* we ignore errors for the entire job, unless the shell has error
* control.
* If the command is just "..." we take all future commands for this
* job to be commands to be executed once the entire graph has been
* made and return non-zero to signal that the end of the commands
* was reached. These commands are later attached to the postCommands
* node and executed by Job_End when all things are done.
* This function is called from JobStart via Lst_ForEach.
*
* Input:
* cmdp command string to print
* jobp job for which to print it
*
* Results:
* Always 0, unless the command was "..."
*
* Side Effects:
* If the command begins with a '-' and the shell has no error control,
* the JOB_IGNERR flag is set in the job descriptor.
* If the command is "..." and we're not ignoring such things,
* tailCmds is set to the successor node of the cmd.
* numCommands is incremented if the command is actually printed.
*-----------------------------------------------------------------------
*/
static int
JobPrintCommand(void *cmdp, void *jobp)
{
Boolean noSpecials; /* true if we shouldn't worry about
* inserting special commands into
* the input stream. */
Boolean shutUp = FALSE; /* true if we put a no echo command
* into the command file */
Boolean errOff = FALSE; /* true if we turned error checking
* off before printing the command
* and need to turn it back on */
const char *cmdTemplate; /* Template to use when printing the
* command */
char *cmdStart; /* Start of expanded command */
char *escCmd = NULL; /* Command with quotes/backticks escaped */
char *cmd = (char *)cmdp;
Job *job = (Job *)jobp;
char *cp, *tmp;
int i, j;
noSpecials = NoExecute(job->node);
if (strcmp(cmd, "...") == 0) {
job->node->type |= OP_SAVE_CMDS;
if ((job->flags & JOB_IGNDOTS) == 0) {
job->tailCmds = Lst_Succ(Lst_Member(job->node->commands,
cmd));
return 1;
}
return 0;
}
#define DBPRINTF(fmt, arg) if (DEBUG(JOB)) { \
(void)fprintf(debug_file, fmt, arg); \
} \
(void)fprintf(job->cmdFILE, fmt, arg); \
(void)fflush(job->cmdFILE);
numCommands += 1;
cmdStart = cmd = Var_Subst(NULL, cmd, job->node, FALSE);
cmdTemplate = "%s\n";
/*
* Check for leading @' and -'s to control echoing and error checking.
*/
while (*cmd == '@' || *cmd == '-' || (*cmd == '+')) {
switch (*cmd) {
case '@':
shutUp = DEBUG(LOUD) ? FALSE : TRUE;
break;
case '-':
job->flags |= JOB_IGNERR;
errOff = TRUE;
break;
case '+':
if (noSpecials) {
/*
* We're not actually executing anything...
* but this one needs to be - use compat mode just for it.
*/
CompatRunCommand(cmdp, job->node);
return 0;
}
break;
}
cmd++;
}
while (isspace((unsigned char) *cmd))
cmd++;
/*
* If the shell doesn't have error control the alternate echo'ing will
* be done (to avoid showing additional error checking code)
* and this will need the characters '$ ` \ "' escaped
*/
if (!commandShell->hasErrCtl) {
/* Worst that could happen is every char needs escaping. */
escCmd = bmake_malloc((strlen(cmd) * 2) + 1);
for (i = 0, j= 0; cmd[i] != '\0'; i++, j++) {
if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' ||
cmd[i] == '"')
escCmd[j++] = '\\';
escCmd[j] = cmd[i];
}
escCmd[j] = 0;
}
if (shutUp) {
if (!(job->flags & JOB_SILENT) && !noSpecials &&
commandShell->hasEchoCtl) {
DBPRINTF("%s\n", commandShell->echoOff);
} else {
if (commandShell->hasErrCtl)
shutUp = FALSE;
}
}
if (errOff) {
if (!noSpecials) {
if (commandShell->hasErrCtl) {
/*
* we don't want the error-control commands showing
* up either, so we turn off echoing while executing
* them. We could put another field in the shell
* structure to tell JobDoOutput to look for this
* string too, but why make it any more complex than
* it already is?
*/
if (!(job->flags & JOB_SILENT) && !shutUp &&
commandShell->hasEchoCtl) {
DBPRINTF("%s\n", commandShell->echoOff);
DBPRINTF("%s\n", commandShell->ignErr);
DBPRINTF("%s\n", commandShell->echoOn);
} else {
DBPRINTF("%s\n", commandShell->ignErr);
}
} else if (commandShell->ignErr &&
(*commandShell->ignErr != '\0'))
{
/*
* The shell has no error control, so we need to be
* weird to get it to ignore any errors from the command.
* If echoing is turned on, we turn it off and use the
* errCheck template to echo the command. Leave echoing
* off so the user doesn't see the weirdness we go through
* to ignore errors. Set cmdTemplate to use the weirdness
* instead of the simple "%s\n" template.
*/
if (!(job->flags & JOB_SILENT) && !shutUp) {
if (commandShell->hasEchoCtl) {
DBPRINTF("%s\n", commandShell->echoOff);
}
DBPRINTF(commandShell->errCheck, escCmd);
shutUp = TRUE;
} else {
if (!shutUp) {
DBPRINTF(commandShell->errCheck, escCmd);
}
}
cmdTemplate = commandShell->ignErr;
/*
* The error ignoration (hee hee) is already taken care
* of by the ignErr template, so pretend error checking
* is still on.
*/
errOff = FALSE;
} else {
errOff = FALSE;
}
} else {
errOff = FALSE;
}
} else {
/*
* If errors are being checked and the shell doesn't have error control
* but does supply an errOut template, then setup commands to run
* through it.
*/
if (!commandShell->hasErrCtl && commandShell->errOut &&
(*commandShell->errOut != '\0')) {
if (!(job->flags & JOB_SILENT) && !shutUp) {
if (commandShell->hasEchoCtl) {
DBPRINTF("%s\n", commandShell->echoOff);
}
DBPRINTF(commandShell->errCheck, escCmd);
shutUp = TRUE;
}
/* If it's a comment line or blank, treat as an ignored error */
if ((escCmd[0] == commandShell->commentChar) ||
(escCmd[0] == 0))
cmdTemplate = commandShell->ignErr;
else
cmdTemplate = commandShell->errOut;
errOff = FALSE;
}
}
if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0 &&
(job->flags & JOB_TRACED) == 0) {
DBPRINTF("set -%s\n", "x");
job->flags |= JOB_TRACED;
}
if ((cp = Check_Cwd_Cmd(cmd)) != NULL) {
DBPRINTF("test -d %s && ", cp);
DBPRINTF("cd %s\n", cp);
}
DBPRINTF(cmdTemplate, cmd);
free(cmdStart);
if (escCmd)
free(escCmd);
if (errOff) {
/*
* If echoing is already off, there's no point in issuing the
* echoOff command. Otherwise we issue it and pretend it was on
* for the whole command...
*/
if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl){
DBPRINTF("%s\n", commandShell->echoOff);
shutUp = TRUE;
}
DBPRINTF("%s\n", commandShell->errCheck);
}
if (shutUp && commandShell->hasEchoCtl) {
DBPRINTF("%s\n", commandShell->echoOn);
}
if (cp != NULL) {
DBPRINTF("test -d %s && ", cp);
DBPRINTF("cd %s\n", Var_Value(".OBJDIR", VAR_GLOBAL, &tmp));
}
return 0;
}
/*-
*-----------------------------------------------------------------------
* JobSaveCommand --
* Save a command to be executed when everything else is done.
* Callback function for JobFinish...
*
* Results:
* Always returns 0
*
* Side Effects:
* The command is tacked onto the end of postCommands's commands list.
*
*-----------------------------------------------------------------------
*/
static int
JobSaveCommand(void *cmd, void *gn)
{
cmd = Var_Subst(NULL, (char *)cmd, (GNode *)gn, FALSE);
(void)Lst_AtEnd(postCommands->commands, cmd);
return(0);
}
/*-
*-----------------------------------------------------------------------
* JobClose --
* Called to close both input and output pipes when a job is finished.
*
* Results:
* Nada
*
* Side Effects:
* The file descriptors associated with the job are closed.
*
*-----------------------------------------------------------------------
*/
static void
JobClose(Job *job)
{
clearfd(job);
(void)close(job->outPipe);
job->outPipe = -1;
JobDoOutput(job, TRUE);
(void)close(job->inPipe);
job->inPipe = -1;
}
/*-
*-----------------------------------------------------------------------
* JobFinish --
* Do final processing for the given job including updating
* parents and starting new jobs as available/necessary. Note
* that we pay no attention to the JOB_IGNERR flag here.
* This is because when we're called because of a noexecute flag
* or something, jstat.w_status is 0 and when called from
* Job_CatchChildren, the status is zeroed if it s/b ignored.
*
* Input:
* job job to finish
* status sub-why job went away
*
* Results:
* None
*
* Side Effects:
* Final commands for the job are placed on postCommands.
*
* If we got an error and are aborting (aborting == ABORT_ERROR) and
* the job list is now empty, we are done for the day.
* If we recognized an error (errors !=0), we set the aborting flag
* to ABORT_ERROR so no more jobs will be started.
*-----------------------------------------------------------------------
*/
/*ARGSUSED*/
static void
JobFinish (Job *job, WAIT_T status)
{
Boolean done, return_job_token;
#ifdef USE_META
if (useMeta) {
meta_job_finish(job);
}
#endif
if (DEBUG(JOB)) {
fprintf(debug_file, "Jobfinish: %d [%s], status %d\n",
job->pid, job->node->name, status);
}
if ((WIFEXITED(status) &&
(((WEXITSTATUS(status) != 0) && !(job->flags & JOB_IGNERR)))) ||
WIFSIGNALED(status))
{
/*
* If it exited non-zero and either we're doing things our
* way or we're not ignoring errors, the job is finished.
* Similarly, if the shell died because of a signal
* the job is also finished. In these
* cases, finish out the job's output before printing the exit
* status...
*/
JobClose(job);
if (job->cmdFILE != NULL && job->cmdFILE != stdout) {