1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 95, 96
3 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 /* Default shell to use. */
29 char default_shell
[] = "/bin/sh";
31 char default_shell
[] = "";
32 extern int MyExecute (char **);
37 static int dos_pid
= 123;
38 static int dos_status
;
39 static char *dos_bname
;
40 static char *dos_bename
;
41 static int dos_batch_file
;
44 #include <proto/dos.h>
45 static int amiga_pid
= 123;
46 static int amiga_status
;
47 static char amiga_bname
[32];
48 static int amiga_batch_file
;
53 #include <processes.h>
55 #include <lib$routines.h>
64 #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
69 #define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
70 #else /* Don't have waitpid. */
75 #define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
76 #endif /* Have wait3. */
77 #endif /* Have waitpid. */
79 #if !defined (wait) && !defined (POSIX)
83 #ifndef HAVE_UNION_WAIT
88 #define WTERMSIG(x) ((x) & 0x7f)
91 #define WCOREDUMP(x) ((x) & 0x80)
94 #define WEXITSTATUS(x) (((x) >> 8) & 0xff)
97 #define WIFSIGNALED(x) (WTERMSIG (x) != 0)
100 #define WIFEXITED(x) (WTERMSIG (x) == 0)
103 #else /* Have `union wait'. */
105 #define WAIT_T union wait
107 #define WTERMSIG(x) ((x).w_termsig)
110 #define WCOREDUMP(x) ((x).w_coredump)
113 #define WEXITSTATUS(x) ((x).w_retcode)
116 #define WIFSIGNALED(x) (WTERMSIG(x) != 0)
119 #define WIFEXITED(x) (WTERMSIG(x) == 0)
122 #endif /* Don't have `union wait'. */
125 static int vms_jobsefnmask
=0;
128 #ifndef HAVE_UNISTD_H
130 extern int execve ();
131 extern void _exit ();
133 extern int geteuid ();
134 extern int getegid ();
135 extern int setgid ();
136 extern int getgid ();
140 extern char *allocated_variable_expand_for_file
PARAMS ((char *line
, struct file
*file
));
142 extern int getloadavg
PARAMS ((double loadavg
[], int nelem
));
143 extern int start_remote_job
PARAMS ((char **argv
, char **envp
, int stdin_fd
,
144 int *is_remote
, int *id_ptr
, int *used_stdin
));
145 extern int start_remote_job_p
PARAMS ((void));
146 extern int remote_status
PARAMS ((int *exit_code_ptr
, int *signal_ptr
,
147 int *coredump_ptr
, int block
));
149 RETSIGTYPE child_handler
PARAMS ((int));
150 static void free_child
PARAMS ((struct child
*));
151 static void start_job_command
PARAMS ((struct child
*child
));
152 static int load_too_high
PARAMS ((void));
153 static int job_next_command
PARAMS ((struct child
*));
154 static int start_waiting_job
PARAMS ((struct child
*));
156 static void vmsWaitForChildren
PARAMS ((int *));
159 /* Chain of all live (or recently deceased) children. */
161 struct child
*children
= 0;
163 /* Number of children currently running. */
165 unsigned int job_slots_used
= 0;
167 /* Nonzero if the `good' standard input is in use. */
169 static int good_stdin_used
= 0;
171 /* Chain of children waiting to run until the load average goes down. */
173 static struct child
*waiting_jobs
= 0;
175 /* Write an error message describing the exit status given in
176 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
177 Append "(ignored)" if IGNORED is nonzero. */
180 child_error (target_name
, exit_code
, exit_sig
, coredump
, ignored
)
182 int exit_code
, exit_sig
, coredump
;
185 if (ignored
&& silent_flag
)
189 if (!(exit_code
& 1))
190 error("*** [%s] Error 0x%x%s", target_name
, exit_code
, ((ignored
)? " (ignored)" : ""));
193 error (ignored
? "[%s] Error %d (ignored)" :
195 target_name
, exit_code
);
197 error ("*** [%s] %s%s",
198 target_name
, strsignal (exit_sig
),
199 coredump
? " (core dumped)" : "");
203 static unsigned int dead_children
= 0;
206 /* Wait for nchildren children to terminate */
208 vmsWaitForChildren(int *status
)
212 if (!vms_jobsefnmask
)
218 *status
= sys$
wflor (32, vms_jobsefnmask
);
225 /* Notice that a child died.
226 reap_children should be called when convenient. */
234 printf ("Got a SIGCHLD; %d unreaped children.\n", dead_children
);
237 extern int shell_function_pid
, shell_function_completed
;
239 /* Reap dead children, storing the returned status and the new command
240 state (`cs_finished') in the `file' member of the `struct child' for the
241 dead child, and removing the child from the chain. If BLOCK nonzero,
242 reap at least one child, waiting for it to die if necessary. If ERR is
243 nonzero, print an error message first. */
246 reap_children (block
, err
)
251 while ((children
!= 0 || shell_function_pid
!= 0) &&
252 (block
|| dead_children
> 0))
256 int exit_code
, exit_sig
, coredump
;
257 register struct child
*lastc
, *c
;
259 int any_remote
, any_local
;
261 if (err
&& dead_children
== 0)
263 /* We might block for a while, so let the user know why. */
265 error ("*** Waiting for unfinished jobs....");
268 /* We have one less dead child to reap.
269 The test and decrement are not atomic; if it is compiled into:
270 register = dead_children - 1;
271 dead_children = register;
272 a SIGCHLD could come between the two instructions.
273 child_handler increments dead_children.
274 The second instruction here would lose that increment. But the
275 only effect of dead_children being wrong is that we might wait
276 longer than necessary to reap a child, and lose some parallelism;
277 and we might print the "Waiting for unfinished jobs" message above
278 when not necessary. */
280 if (dead_children
> 0)
284 any_local
= shell_function_pid
!= -1;
285 for (c
= children
; c
!= 0; c
= c
->next
)
287 any_remote
|= c
->remote
;
288 any_local
|= ! c
->remote
;
290 printf ("Live child 0x%08lx PID %d%s\n",
291 (unsigned long int) c
,
292 c
->pid
, c
->remote
? " (remote)" : "");
298 /* First, check for remote children. */
300 pid
= remote_status (&exit_code
, &exit_sig
, &coredump
, 0);
311 pfatal_with_name ("remote_status");
315 #if !defined(__MSDOS__) && !defined(_AMIGA)
316 /* No remote children. Check for local children. */
321 vmsWaitForChildren (&status
);
326 pid
= WAIT_NOHANG (&status
);
329 pid
= wait (&status
);
341 pfatal_with_name ("wait");
345 /* No local children. */
346 if (block
&& any_remote
)
348 /* Now try a blocking wait for a remote child. */
349 pid
= remote_status (&exit_code
, &exit_sig
, &coredump
, 1);
351 goto remote_status_lose
;
353 /* No remote children either. Finally give up. */
356 /* We got a remote child. */
364 /* Chop the status word up. */
365 exit_code
= WEXITSTATUS (status
);
366 exit_sig
= WIFSIGNALED (status
) ? WTERMSIG (status
) : 0;
367 coredump
= WCOREDUMP (status
);
371 /* Life is very different on MSDOS. */
374 exit_code
= dos_status
;
380 status
= amiga_status
;
381 exit_code
= amiga_status
;
385 #endif /* Not MSDOS. */
388 /* We got a remote child. */
391 /* Check if this is the child of the `shell' function. */
392 if (!remote
&& pid
== shell_function_pid
)
394 /* It is. Leave an indicator for the `shell' function. */
395 if (exit_sig
== 0 && exit_code
== 127)
396 shell_function_completed
= -1;
398 shell_function_completed
= 1;
402 child_failed
= exit_sig
!= 0 || exit_code
!= 0;
404 /* Search for a child matching the deceased one. */
406 for (c
= children
; c
!= 0; lastc
= c
, c
= c
->next
)
407 if (c
->remote
== remote
&& c
->pid
== pid
)
412 /* An unknown child died. */
414 sprintf (buf
, "Unknown%s job %d", remote
? " remote" : "", pid
);
416 child_error (buf
, exit_code
, exit_sig
, coredump
,
419 error ("%s finished.", buf
);
424 printf ("Reaping %s child 0x%08lx PID %d%s\n",
425 child_failed
? "losing" : "winning",
426 (unsigned long int) c
,
427 c
->pid
, c
->remote
? " (remote)" : "");
429 /* If this child had the good stdin, say it is now free. */
433 if (child_failed
&& !c
->noerror
&& !ignore_errors_flag
)
435 /* The commands failed. Write an error message,
436 delete non-precious targets, and abort. */
437 static int delete_on_error
= -1;
438 child_error (c
->file
->name
, exit_code
, exit_sig
, coredump
, 0);
439 c
->file
->update_status
= 2;
440 if (delete_on_error
== -1)
442 struct file
*f
= lookup_file (".DELETE_ON_ERROR");
443 delete_on_error
= f
!= 0 && f
->is_target
;
445 if (exit_sig
!= 0 || delete_on_error
)
446 delete_child_targets (c
);
452 /* The commands failed, but we don't care. */
453 child_error (c
->file
->name
,
454 exit_code
, exit_sig
, coredump
, 1);
458 /* If there are more commands to run, try to start them. */
459 if (job_next_command (c
))
461 if (handling_fatal_signal
)
463 /* Never start new commands while we are dying.
464 Since there are more commands that wanted to be run,
465 the target was not completely remade. So we treat
466 this as if a command had failed. */
467 c
->file
->update_status
= 2;
471 /* Check again whether to start remotely.
472 Whether or not we want to changes over time.
473 Also, start_remote_job may need state set up
474 by start_remote_job_p. */
475 c
->remote
= start_remote_job_p ();
476 start_job_command (c
);
477 /* Fatal signals are left blocked in case we were
478 about to put that child on the chain. But it is
479 already there, so it is safe for a fatal signal to
480 arrive now; it will clean up this child's targets. */
482 if (c
->file
->command_state
== cs_running
)
483 /* We successfully started the new command.
484 Loop to reap more children. */
488 if (c
->file
->update_status
!= 0)
489 /* We failed to start the commands. */
490 delete_child_targets (c
);
493 /* There are no more commands. We got through them all
494 without an unignored error. Now the target has been
495 successfully updated. */
496 c
->file
->update_status
= 0;
499 /* When we get here, all the commands for C->file are finished
500 (or aborted) and C->file->update_status contains 0 or 2. But
501 C->file->command_state is still cs_running if all the commands
502 ran; notice_finish_file looks for cs_running to tell it that
503 it's interesting to check the file's modtime again now. */
505 if (! handling_fatal_signal
)
506 /* Notice if the target of the commands has been changed.
507 This also propagates its values for command_state and
508 update_status to its also_make files. */
509 notice_finished_file (c
->file
);
512 printf ("Removing child 0x%08lx PID %d%s from chain.\n",
513 (unsigned long int) c
,
514 c
->pid
, c
->remote
? " (remote)" : "");
516 /* Remove the child from the chain and free it. */
520 lastc
->next
= c
->next
;
521 if (! handling_fatal_signal
) /* Avoid nonreentrancy. */
524 /* There is now another slot open. */
525 if (job_slots_used
> 0)
528 /* If the job failed, and the -k flag was not given, die,
529 unless we are already in the process of dying. */
530 if (!err
&& child_failed
&& !keep_going_flag
)
534 /* Only block for one child. */
540 /* Free the storage allocated for CHILD. */
544 register struct child
*child
;
546 if (child
->command_lines
!= 0)
548 register unsigned int i
;
549 for (i
= 0; i
< child
->file
->cmds
->ncommand_lines
; ++i
)
550 free (child
->command_lines
[i
]);
551 free ((char *) child
->command_lines
);
554 if (child
->environment
!= 0)
556 register char **ep
= child
->environment
;
559 free ((char *) child
->environment
);
562 free ((char *) child
);
573 extern sigset_t fatal_signal_set
;
579 sigemptyset (&empty
);
580 sigprocmask (SIG_SETMASK
, &empty
, (sigset_t
*) 0);
585 /* Start a job to run the commands specified in CHILD.
586 CHILD is updated to reflect the commands and ID of the child process.
588 NOTE: On return fatal signals are blocked! The caller is responsible
589 for calling `unblock_sigs', once the new child is safely on the chain so
590 it can be cleaned up in the event of a fatal signal. */
593 start_job_command (child
)
594 register struct child
*child
;
597 static int bad_stdin
= -1;
607 /* Combine the flags parsed for the line itself with
608 the flags specified globally for this target. */
609 flags
= (child
->file
->command_flags
610 | child
->file
->cmds
->lines_flags
[child
->command_line
- 1]);
612 p
= child
->command_ptr
;
613 child
->noerror
= flags
& COMMANDS_NOERROR
;
618 flags
|= COMMANDS_SILENT
;
620 flags
|= COMMANDS_RECURSE
;
623 else if (!isblank (*p
) && *p
!= '+')
628 /* If -q was given, just say that updating `failed'. The exit status of
629 1 tells the user that -q is saying `something to do'; the exit status
630 for a random error is 2. */
631 if (question_flag
&& !(flags
& COMMANDS_RECURSE
))
633 child
->file
->update_status
= 1;
634 notice_finished_file (child
->file
);
638 /* There may be some preceding whitespace left if there
639 was nothing but a backslash on the first line. */
642 /* Figure out an argument list from this command line. */
649 argv
= construct_command_argv (p
, &end
, child
->file
);
652 child
->command_ptr
= NULL
;
656 child
->command_ptr
= end
;
660 if (touch_flag
&& !(flags
& COMMANDS_RECURSE
))
662 /* Go on to the next command. It might be the recursive one.
663 We construct ARGV only to find the end of the command line. */
666 free ((char *) argv
);
674 /* This line has no commands. Go to the next. */
675 if (job_next_command (child
))
676 start_job_command (child
);
679 /* No more commands. All done. */
680 child
->file
->update_status
= 0;
681 notice_finished_file (child
->file
);
686 /* Print out the command. If silent, we call `message' with null so it
687 can log the working directory before the command's own error messages
690 message (0, (just_print_flag
|| (!(flags
& COMMANDS_SILENT
) && !silent_flag
))
691 ? "%s" : (char *) 0, p
);
693 /* Tell update_goal_chain that a command has been started on behalf of
694 this target. It is important that this happens here and not in
695 reap_children (where we used to do it), because reap_children might be
696 reaping children from a different target. We want this increment to
697 guaranteedly indicate that a command was started for the dependency
698 chain (i.e., update_file recursion chain) we are processing. */
702 /* If -n was given, recurse to get the next line in the sequence. */
704 if (just_print_flag
&& !(flags
& COMMANDS_RECURSE
))
708 free ((char *) argv
);
713 /* Flush the output streams so they won't have things written twice. */
721 /* Set up a bad standard input that reads from a broken pipe. */
725 /* Make a file descriptor that is the read end of a broken pipe.
726 This will be used for some children's standard inputs. */
730 /* Close the write side. */
731 (void) close (pd
[1]);
732 /* Save the read side. */
735 /* Set the descriptor to close on exec, so it does not litter any
736 child's descriptor table. When it is dup2'd onto descriptor 0,
737 that descriptor will not close on exec. */
742 (void) fcntl (bad_stdin
, F_SETFD
, FD_CLOEXEC
);
749 /* Decide whether to give this child the `good' standard input
750 (one that points to the terminal or whatever), or the `bad' one
751 that points to the read side of a broken pipe. */
753 child
->good_stdin
= !good_stdin_used
;
754 if (child
->good_stdin
)
762 /* Set up the environment for the child. */
763 if (child
->environment
== 0)
764 child
->environment
= target_environment (child
->file
);
767 #if !defined(__MSDOS__) && !defined(_AMIGA)
770 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
773 int is_remote
, id
, used_stdin
;
774 if (start_remote_job (argv
, child
->environment
,
775 child
->good_stdin
? 0 : bad_stdin
,
776 &is_remote
, &id
, &used_stdin
))
780 if (child
->good_stdin
&& !used_stdin
)
782 child
->good_stdin
= 0;
785 child
->remote
= is_remote
;
792 /* Fork the child process. */
794 char **parent_environ
;
797 (void) sigprocmask (SIG_BLOCK
, &fatal_signal_set
, (sigset_t
*) 0);
799 #ifdef HAVE_SIGSETMASK
800 (void) sigblock (fatal_signal_mask
);
808 if (!child_execute_job (argv
, child
)) {
810 perror_with_name ("vfork", "");
816 parent_environ
= environ
;
817 child
->pid
= vfork ();
818 environ
= parent_environ
; /* Restore value child may have clobbered. */
821 /* We are the child side. */
823 child_execute_job (child
->good_stdin
? 0 : bad_stdin
, 1,
824 argv
, child
->environment
);
826 else if (child
->pid
< 0)
830 perror_with_name ("vfork", "");
836 #else /* MSDOS or Amiga. */
838 dos_status
= spawnvpe (P_WAIT
, argv
[0], argv
, child
->environment
);
840 child
->pid
= dos_pid
++;
844 remove (dos_bname
); /* Ignore errors. */
845 if (access (dos_bename
, 0))
852 amiga_status
= MyExecute (argv
);
855 child
->pid
= amiga_pid
++;
856 if (amiga_batch_file
)
858 amiga_batch_file
= 0;
859 DeleteFile (amiga_bname
); /* Ignore errors. */
861 #endif /* Not Amiga */
862 #endif /* Not MSDOS. */
864 /* We are the parent side. Set the state to
865 say the commands are running and return. */
867 set_command_state (child
->file
, cs_running
);
869 /* Free the storage used by the child's argument list. */
872 free ((char *) argv
);
878 child
->file
->update_status
= 2;
879 notice_finished_file (child
->file
);
883 /* Try to start a child running.
884 Returns nonzero if the child was started (and maybe finished), or zero if
885 the load was too high and the child was put on the `waiting_jobs' chain. */
888 start_waiting_job (c
)
891 /* If we can start a job remotely, we always want to, and don't care about
892 the local load average. We record that the job should be started
893 remotely in C->remote for start_job_command to test. */
895 c
->remote
= start_remote_job_p ();
897 /* If this job is to be started locally, and we are already running
898 some jobs, make this one wait if the load average is too high. */
899 if (!c
->remote
&& job_slots_used
> 0 && load_too_high ())
901 /* Put this child on the chain of children waiting
902 for the load average to go down. */
903 set_command_state (c
->file
, cs_running
);
904 c
->next
= waiting_jobs
;
909 /* Start the first command; reap_children will run later command lines. */
910 start_job_command (c
);
912 switch (c
->file
->command_state
)
917 printf ("Putting child 0x%08lx PID %05d%s on the chain.\n",
918 (unsigned long int) c
,
919 c
->pid
, c
->remote
? " (remote)" : "");
921 /* One more job slot is in use. */
927 /* All the command lines turned out to be empty. */
928 c
->file
->update_status
= 0;
932 notice_finished_file (c
->file
);
937 assert (c
->file
->command_state
== cs_finished
);
944 /* Create a `struct child' for FILE and start its commands running. */
948 register struct file
*file
;
950 register struct commands
*cmds
= file
->cmds
;
951 register struct child
*c
;
953 register unsigned int i
;
955 /* Let any previously decided-upon jobs that are waiting
956 for the load to go down start before this new one. */
957 start_waiting_jobs ();
959 /* Reap any children that might have finished recently. */
960 reap_children (0, 0);
962 /* Chop the commands up into lines if they aren't already. */
963 chop_commands (cmds
);
966 /* Wait for a job slot to be freed up. */
967 while (job_slots_used
== job_slots
)
968 reap_children (1, 0);
970 /* Expand the command lines and store the results in LINES. */
971 lines
= (char **) xmalloc (cmds
->ncommand_lines
* sizeof (char *));
972 for (i
= 0; i
< cmds
->ncommand_lines
; ++i
)
974 /* Collapse backslash-newline combinations that are inside variable
975 or function references. These are left alone by the parser so
976 that they will appear in the echoing of commands (where they look
977 nice); and collapsed by construct_command_argv when it tokenizes.
978 But letting them survive inside function invocations loses because
979 we don't want the functions to see them as part of the text. */
981 char *in
, *out
, *ref
;
983 /* IN points to where in the line we are scanning.
984 OUT points to where in the line we are writing.
985 When we collapse a backslash-newline combination,
986 IN gets ahead out OUT. */
988 in
= out
= cmds
->command_lines
[i
];
989 while ((ref
= index (in
, '$')) != 0)
991 ++ref
; /* Move past the $. */
994 /* Copy the text between the end of the last chunk
995 we processed (where IN points) and the new chunk
996 we are about to process (where REF points). */
997 bcopy (in
, out
, ref
- in
);
999 /* Move both pointers past the boring stuff. */
1003 if (*ref
== '(' || *ref
== '{')
1005 char openparen
= *ref
;
1006 char closeparen
= openparen
== '(' ? ')' : '}';
1010 *out
++ = *in
++; /* Copy OPENPAREN. */
1011 /* IN now points past the opening paren or brace.
1012 Count parens or braces until it is matched. */
1016 if (*in
== closeparen
&& --count
< 0)
1018 else if (*in
== '\\' && in
[1] == '\n')
1020 /* We have found a backslash-newline inside a
1021 variable or function reference. Eat it and
1022 any following whitespace. */
1025 for (p
= in
- 1; p
> ref
&& *p
== '\\'; --p
)
1029 /* There were two or more backslashes, so this is
1030 not really a continuation line. We don't collapse
1031 the quoting backslashes here as is done in
1032 collapse_continuations, because the line will
1033 be collapsed again after expansion. */
1037 /* Skip the backslash, newline and
1038 any following whitespace. */
1039 in
= next_token (in
+ 2);
1041 /* Discard any preceding whitespace that has
1042 already been written to the output. */
1043 while (out
> ref
&& isblank (out
[-1]))
1046 /* Replace it all with a single space. */
1052 if (*in
== openparen
)
1061 /* There are no more references in this line to worry about.
1062 Copy the remaining uninteresting text to the output. */
1066 /* Finally, expand the line. */
1067 lines
[i
] = allocated_variable_expand_for_file (cmds
->command_lines
[i
],
1071 /* Start the command sequence, record it in a new
1072 `struct child', and add that to the chain. */
1074 c
= (struct child
*) xmalloc (sizeof (struct child
));
1076 c
->command_lines
= lines
;
1077 c
->command_line
= 0;
1081 /* Fetch the first command line to be run. */
1082 job_next_command (c
);
1084 /* The job is now primed. Start it running.
1085 (This will notice if there are in fact no commands.) */
1086 (void)start_waiting_job (c
);
1089 /* Since there is only one job slot, make things run linearly.
1090 Wait for the child to die, setting the state to `cs_finished'. */
1091 while (file
->command_state
== cs_running
)
1092 reap_children (1, 0);
1097 /* Move CHILD's pointers to the next command for it to execute.
1098 Returns nonzero if there is another command. */
1101 job_next_command (child
)
1102 struct child
*child
;
1104 while (child
->command_ptr
== 0 || *child
->command_ptr
== '\0')
1106 /* There are no more lines in the expansion of this line. */
1107 if (child
->command_line
== child
->file
->cmds
->ncommand_lines
)
1109 /* There are no more lines to be expanded. */
1110 child
->command_ptr
= 0;
1114 /* Get the next line to run. */
1115 child
->command_ptr
= child
->command_lines
[child
->command_line
++];
1123 #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA)
1128 if (max_load_average
< 0)
1132 if (getloadavg (&load
, 1) != 1)
1134 static int lossage
= -1;
1135 /* Complain only once for the same error. */
1136 if (lossage
== -1 || errno
!= lossage
)
1139 /* An errno value of zero means getloadavg is just unsupported. */
1140 error ("cannot enforce load limits on this operating system");
1142 perror_with_name ("cannot enforce load limit: ", "getloadavg");
1149 return load
>= max_load_average
;
1153 /* Start jobs that are waiting for the load to be lower. */
1156 start_waiting_jobs ()
1160 if (waiting_jobs
== 0)
1165 /* Check for recently deceased descendants. */
1166 reap_children (0, 0);
1168 /* Take a job off the waiting list. */
1170 waiting_jobs
= job
->next
;
1172 /* Try to start that job. We break out of the loop as soon
1173 as start_waiting_job puts one back on the waiting list. */
1175 while (start_waiting_job (job
) && waiting_jobs
!= 0);
1181 #include <descrip.h>
1184 /* This is called as an AST when a child process dies (it won't get
1185 interrupted by anything except a higher level AST).
1187 int vmsHandleChildTerm(struct child
*child
)
1190 register struct child
*lastc
, *c
;
1193 vms_jobsefnmask
&= ~(1 << (child
->efn
- 32));
1195 lib$
free_ef(&child
->efn
);
1197 (void) sigblock (fatal_signal_mask
);
1199 child_failed
= !(child
->cstatus
& 1 || ((child
->cstatus
& 7) == 0));
1201 /* Search for a child matching the deceased one. */
1203 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
1204 for (c
= children
; c
!= 0 && c
!= child
; lastc
= c
, c
= c
->next
);
1209 if (child_failed
&& !c
->noerror
&& !ignore_errors_flag
)
1211 /* The commands failed. Write an error message,
1212 delete non-precious targets, and abort. */
1213 child_error (c
->file
->name
, c
->cstatus
, 0, 0, 0);
1214 c
->file
->update_status
= 1;
1215 delete_child_targets (c
);
1221 /* The commands failed, but we don't care. */
1222 child_error (c
->file
->name
, c
->cstatus
, 0, 0, 1);
1226 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
1227 /* If there are more commands to run, try to start them. */
1230 switch (c
->file
->command_state
)
1233 /* Successfully started. */
1237 if (c
->file
->update_status
!= 0) {
1238 /* We failed to start the commands. */
1239 delete_child_targets (c
);
1244 error ("internal error: `%s' command_state \
1245 %d in child_handler", c
->file
->name
);
1249 #endif /* RECURSIVEJOBS */
1252 /* Set the state flag to say the commands have finished. */
1253 c
->file
->command_state
= cs_finished
;
1254 notice_finished_file (c
->file
);
1256 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
1257 /* Remove the child from the chain and free it. */
1261 lastc
->next
= c
->next
;
1263 #endif /* RECURSIVEJOBS */
1265 /* There is now another slot open. */
1266 if (job_slots_used
> 0)
1269 /* If the job failed, and the -k flag was not given, die. */
1270 if (child_failed
&& !keep_going_flag
)
1273 (void) sigsetmask (sigblock (0) & ~(fatal_signal_mask
));
1279 Spawn a process executing the command in ARGV and return its pid. */
1281 #define MAXCMDLEN 200
1284 child_execute_job (argv
, child
)
1286 struct child
*child
;
1289 static struct dsc$descriptor_s cmddsc
;
1290 #ifndef DONTWAITFORCHILD
1293 int spflags
= CLI$M_NOWAIT
;
1296 char cmd
[4096],*p
,*c
;
1299 /* Remove backslashes */
1300 for (p
= argv
, c
= cmd
; *p
; p
++,c
++)
1302 if (*p
== '\\') p
++;
1307 /* check for maximum dcl length and create *.com file if neccesary */
1311 if (strlen (cmd
) > MAXCMDLEN
)
1316 strcpy (comname
, "sys$scratch:CMDXXXXXX.COM");
1317 (void) mktemp (comname
);
1319 outfile
= fopen (comname
, "w");
1321 pfatal_with_name (comname
);
1323 fprintf (outfile
, "$ ");
1328 p
= strchr (c
, ',');
1329 if ((p
== NULL
) || (p
-c
> MAXCMDLEN
))
1330 p
= strchr (c
, ' ');
1339 fprintf (outfile
, "%s%s\n", c
, (tmp
== '\0')?"":" -");
1347 sprintf (cmd
, "$ @%s", comname
);
1350 printf ("Executing %s instead\n", cmd
);
1353 cmddsc
.dsc$w_length
= strlen(cmd
);
1354 cmddsc
.dsc$a_pointer
= cmd
;
1355 cmddsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
1356 cmddsc
.dsc$b_class
= DSC$K_CLASS_S
;
1359 while (child
->efn
< 32 || child
->efn
> 63)
1361 status
= lib$
get_ef(&child
->efn
);
1366 sys$
clref(child
->efn
);
1368 vms_jobsefnmask
|= (1 << (child
->efn
- 32));
1370 #ifndef DONTWAITFORCHILD
1371 status
= lib$
spawn(&cmddsc
,0,0,&spflags
,0,&child
->pid
,&child
->cstatus
,
1373 vmsHandleChildTerm(child
);
1375 status
= lib$
spawn(&cmddsc
,0,0,&spflags
,0,&child
->pid
,&child
->cstatus
,
1376 &child
->efn
,vmsHandleChildTerm
,child
);
1381 printf("Error spawning, %d\n",status
);
1387 return (status
& 1);
1394 Replace the current process with one executing the command in ARGV.
1395 STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
1396 the environment of the new program. This function does not return. */
1399 child_execute_job (stdin_fd
, stdout_fd
, argv
, envp
)
1400 int stdin_fd
, stdout_fd
;
1401 char **argv
, **envp
;
1404 (void) dup2 (stdin_fd
, 0);
1406 (void) dup2 (stdout_fd
, 1);
1408 (void) close (stdin_fd
);
1410 (void) close (stdout_fd
);
1412 /* Run the command. */
1413 exec_command (argv
, envp
);
1419 /* Replace the current process with one running the command in ARGV,
1420 with environment ENVP. This function does not return. */
1423 exec_command (argv
, envp
)
1424 char **argv
, **envp
;
1427 /* Run the program. */
1428 execve (argv
[0], argv
, envp
);
1429 perror_with_name ("execve: ", argv
[0]);
1430 _exit (EXIT_FAILURE
);
1432 /* Be the user, permanently. */
1435 /* Run the program. */
1437 execvp (argv
[0], argv
);
1442 error ("%s: Command not found", argv
[0]);
1446 /* The file is not executable. Try it as a shell script. */
1447 extern char *getenv ();
1452 shell
= getenv ("SHELL");
1454 shell
= default_shell
;
1457 while (argv
[argc
] != 0)
1460 new_argv
= (char **) alloca ((1 + argc
+ 1) * sizeof (char *));
1461 new_argv
[0] = shell
;
1462 new_argv
[1] = argv
[0];
1465 new_argv
[1 + argc
] = argv
[argc
];
1469 execvp (shell
, new_argv
);
1470 if (errno
== ENOENT
)
1471 error ("%s: Shell program not found", shell
);
1473 perror_with_name ("execvp: ", shell
);
1478 perror_with_name ("execvp: ", argv
[0]);
1485 #else /* On Amiga */
1486 void exec_command (argv
)
1492 void clean_tmp (void)
1494 DeleteFile (amiga_bname
);
1497 #endif /* An Amiga */
1500 /* Figure out the argument list necessary to run LINE as a command. Try to
1501 avoid using a shell. This routine handles only ' quoting, and " quoting
1502 when no backslash, $ or ` characters are seen in the quotes. Starting
1503 quotes may be escaped with a backslash. If any of the characters in
1504 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
1505 is the first word of a line, the shell is used.
1507 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
1508 If *RESTP is NULL, newlines will be ignored.
1510 SHELL is the shell to use, or nil to use the default shell.
1511 IFS is the value of $IFS, or nil (meaning the default). */
1514 construct_command_argv_internal (line
, restp
, shell
, ifs
)
1515 char *line
, **restp
;
1519 static char sh_chars
[] = "\"|<>";
1520 static char *sh_cmds
[] = { "break", "call", "cd", "chcp", "chdir", "cls",
1521 "copy", "ctty", "date", "del", "dir", "echo",
1522 "erase", "exit", "for", "goto", "if", "if", "md",
1523 "mkdir", "path", "pause", "prompt", "rem", "ren",
1524 "rename", "set", "shift", "time", "type",
1525 "ver", "verify", "vol", ":", 0 };
1528 static char sh_chars
[] = "#;\"|<>()?*$`";
1529 static char *sh_cmds
[] = { "cd", "eval", "if", "delete", "echo", "copy",
1530 "rename", "set", "setenv", "date", "makedir",
1531 "skip", "else", "endif", "path", "prompt",
1532 "unset", "unsetenv", "version",
1535 static char sh_chars
[] = "#;\"*?[]&|<>(){}$`^";
1536 static char *sh_cmds
[] = { "cd", "eval", "exec", "exit", "login",
1537 "logout", "set", "umask", "wait", "while", "for",
1538 "case", "if", ":", ".", "break", "continue",
1539 "export", "read", "readonly", "shift", "times",
1540 "trap", "switch", 0 };
1546 int instring
, word_has_equals
, seen_nonequals
;
1547 char **new_argv
= 0;
1552 /* Make sure not to bother processing an empty line. */
1553 while (isblank (*line
))
1558 /* See if it is safe to parse commands internally. */
1560 shell
= default_shell
;
1561 else if (strcmp (shell
, default_shell
))
1565 for (ap
= ifs
; *ap
!= '\0'; ++ap
)
1566 if (*ap
!= ' ' && *ap
!= '\t' && *ap
!= '\n')
1569 i
= strlen (line
) + 1;
1571 /* More than 1 arg per character is impossible. */
1572 new_argv
= (char **) xmalloc (i
* sizeof (char *));
1574 /* All the args can fit in a buffer as big as LINE is. */
1575 ap
= new_argv
[0] = (char *) xmalloc (i
);
1578 /* I is how many complete arguments have been found. */
1580 instring
= word_has_equals
= seen_nonequals
= 0;
1581 for (p
= line
; *p
!= '\0'; ++p
)
1589 /* Inside a string, just copy any char except a closing quote
1590 or a backslash-newline combination. */
1593 else if (*p
== '\\' && p
[1] == '\n')
1594 goto swallow_escaped_newline
;
1595 else if (*p
== '\n' && restp
!= NULL
)
1597 /* End of the command line. */
1601 /* Backslash, $, and ` are special inside double quotes.
1602 If we see any of those, punt. */
1603 else if (instring
== '"' && index ("\\$`", *p
) != 0)
1608 else if (index (sh_chars
, *p
) != 0)
1609 /* Not inside a string, but it's a special char. */
1612 /* Not a special char. */
1616 /* Equals is a special character in leading words before the
1617 first word with no equals sign in it. This is not the case
1618 with sh -k, but we never get here when using nonstandard
1620 if (! seen_nonequals
)
1622 word_has_equals
= 1;
1627 /* Backslash-newline combinations are eaten. */
1630 swallow_escaped_newline
:
1632 /* Eat the backslash, the newline, and following whitespace,
1633 replacing it all with a single space. */
1636 /* If there is a tab after a backslash-newline,
1637 remove it from the source line which will be echoed,
1638 since it was most likely used to line
1639 up the continued line with the previous one. */
1647 if (ap
!= new_argv
[i
])
1648 /* Treat this as a space, ending the arg.
1649 But if it's at the beginning of the arg, it should
1650 just get eaten, rather than becoming an empty arg. */
1653 p
= next_token (p
) - 1;
1656 else if (p
[1] != '\0')
1657 /* Copy and skip the following char. */
1669 /* End of the command line. */
1674 /* Newlines are not special. */
1681 /* We have the end of an argument.
1682 Terminate the text of the argument. */
1686 /* Update SEEN_NONEQUALS, which tells us if every word
1687 heretofore has contained an `='. */
1688 seen_nonequals
|= ! word_has_equals
;
1689 if (word_has_equals
&& ! seen_nonequals
)
1690 /* An `=' in a word before the first
1691 word without one is magical. */
1693 word_has_equals
= 0; /* Prepare for the next word. */
1695 /* If this argument is the command name,
1696 see if it is a built-in shell command.
1697 If so, have the shell handle it. */
1701 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
1702 if (streq (sh_cmds
[j
], new_argv
[0]))
1706 /* Ignore multiple whitespace chars. */
1708 /* Next iteration should examine the first nonwhite char. */
1720 /* Let the shell deal with an unterminated quote. */
1723 /* Terminate the last argument and the argument list. */
1726 if (new_argv
[i
][0] != '\0')
1733 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
1734 if (streq (sh_cmds
[j
], new_argv
[0]))
1738 if (new_argv
[0] == 0)
1739 /* Line was empty. */
1745 /* We must use the shell. */
1749 /* Free the old argument list we were working on. */
1751 free ((void *)new_argv
);
1760 dos_bname
= tempnam (".", "mk");
1761 for (i
= 0; dos_bname
[i
] != '\0'; ++i
)
1762 if (dos_bname
[i
] == '/')
1763 dos_bname
[i
] = '\\';
1764 dos_bename
= (char *) xmalloc (strlen (dos_bname
) + 5);
1765 strcpy (dos_bename
, dos_bname
);
1766 strcat (dos_bname
, ".bat");
1767 strcat (dos_bename
, ".err");
1769 batch
= fopen (dos_bename
, "w"); /* Create a file. */
1772 batch
= fopen (dos_bname
, "w");
1773 fputs ("@echo off\n", batch
);
1774 fputs (line
, batch
);
1775 fprintf (batch
, "\nif errorlevel 1 del %s\n", dos_bename
);
1777 new_argv
= (char **) xmalloc(2 * sizeof(char *));
1778 new_argv
[0] = strdup (dos_bname
);
1788 buffer
= (char *)xmalloc (strlen (line
)+1);
1791 for (dptr
=buffer
; *ptr
; )
1793 if (*ptr
== '\\' && ptr
[1] == '\n')
1795 else if (*ptr
== '@') /* Kludge: multiline commands */
1805 new_argv
= (char **) xmalloc(2 * sizeof(char *));
1806 new_argv
[0] = buffer
;
1809 #else /* Not MSDOS or Amiga */
1811 /* SHELL may be a multi-word command. Construct a command line
1812 "SHELL -c LINE", with all special chars in LINE escaped.
1813 Then recurse, expanding this command line to get the final
1816 unsigned int shell_len
= strlen (shell
);
1817 static char minus_c
[] = " -c ";
1818 unsigned int line_len
= strlen (line
);
1820 char *new_line
= (char *) alloca (shell_len
+ (sizeof (minus_c
) - 1)
1821 + (line_len
* 2) + 1);
1824 bcopy (shell
, ap
, shell_len
);
1826 bcopy (minus_c
, ap
, sizeof (minus_c
) - 1);
1827 ap
+= sizeof (minus_c
) - 1;
1828 for (p
= line
; *p
!= '\0'; ++p
)
1830 if (restp
!= NULL
&& *p
== '\n')
1835 else if (*p
== '\\' && p
[1] == '\n')
1837 /* Eat the backslash, the newline, and following whitespace,
1838 replacing it all with a single space (which is escaped
1842 /* If there is a tab after a backslash-newline,
1843 remove it from the source line which will be echoed,
1844 since it was most likely used to line
1845 up the continued line with the previous one. */
1856 if (*p
== '\\' || *p
== '\'' || *p
== '"'
1858 || index (sh_chars
, *p
) != 0)
1864 new_argv
= construct_command_argv_internal (new_line
, (char **) NULL
,
1865 (char *) 0, (char *) 0);
1867 #endif /* Not MSDOS nor Amiga. */
1872 /* Figure out the argument list necessary to run LINE as a command. Try to
1873 avoid using a shell. This routine handles only ' quoting, and " quoting
1874 when no backslash, $ or ` characters are seen in the quotes. Starting
1875 quotes may be escaped with a backslash. If any of the characters in
1876 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
1877 is the first word of a line, the shell is used.
1879 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
1880 If *RESTP is NULL, newlines will be ignored.
1882 FILE is the target whose commands these are. It is used for
1883 variable expansion for $(SHELL) and $(IFS). */
1886 construct_command_argv (line
, restp
, file
)
1887 char *line
, **restp
;
1894 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
1895 int save
= warn_undefined_variables_flag
;
1896 warn_undefined_variables_flag
= 0;
1898 shell
= allocated_variable_expand_for_file ("$(SHELL)", file
);
1899 ifs
= allocated_variable_expand_for_file ("$(IFS)", file
);
1901 warn_undefined_variables_flag
= save
;
1904 argv
= construct_command_argv_internal (line
, restp
, shell
, ifs
);
1913 #if !defined(HAVE_DUP2) && !defined(_AMIGA)
1931 #endif /* !HAPE_DUP2 && !_AMIGA */