1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 /* Default path to search for executables. */
27 static char default_path
[] = ":/bin:/usr/bin";
29 /* Default shell to use. */
30 char default_shell
[] = "/bin/sh";
34 static int dos_pid
= 123;
35 static int dos_status
;
36 static char *dos_bname
;
37 static char *dos_bename
;
38 static int dos_batch_file
;
42 /* If NGROUPS_MAX == 0 then try other methods for finding a real value. */
43 #if defined (NGROUPS_MAX) && NGROUPS_MAX == 0
45 #endif /* NGROUPS_MAX == 0 */
49 #define GET_NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
50 #else /* Not POSIX. */
51 #define NGROUPS_MAX NGROUPS
55 #ifdef HAVE_SYS_WAIT_H
60 #define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
61 #else /* Don't have waitpid. */
66 #define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
67 #endif /* Have wait3. */
68 #endif /* Have waitpid. */
70 #if !defined (wait) && !defined (POSIX)
74 #ifndef HAVE_UNION_WAIT
79 #define WTERMSIG(x) ((x) & 0x7f)
82 #define WCOREDUMP(x) ((x) & 0x80)
85 #define WEXITSTATUS(x) (((x) >> 8) & 0xff)
88 #define WIFSIGNALED(x) (WTERMSIG (x) != 0)
91 #define WIFEXITED(x) (WTERMSIG (x) == 0)
94 #else /* Have `union wait'. */
96 #define WAIT_T union wait
98 #define WTERMSIG(x) ((x).w_termsig)
101 #define WCOREDUMP(x) ((x).w_coredump)
104 #define WEXITSTATUS(x) ((x).w_retcode)
107 #define WIFSIGNALED(x) (WTERMSIG(x) != 0)
110 #define WIFEXITED(x) (WTERMSIG(x) == 0)
113 #endif /* Don't have `union wait'. */
116 #ifndef HAVE_UNISTD_H
118 extern int execve ();
119 extern void _exit ();
120 extern int geteuid (), getegid ();
121 extern int setgid (), getgid ();
124 #ifndef getdtablesize
125 #ifdef HAVE_GETDTABLESIZE
126 extern int getdtablesize ();
128 #include <sys/param.h>
129 #define getdtablesize() NOFILE
130 #if !defined (NOFILE) && defined (NOFILES_MAX)
131 /* SCO 3.2 "devsys 4.2" defines NOFILES_{MIN,MAX} in lieu of NOFILE. */
132 #define NOFILE NOFILES_MAX
137 extern int getloadavg ();
138 extern int start_remote_job_p ();
139 extern int start_remote_job (), remote_status ();
141 RETSIGTYPE
child_handler ();
142 static void free_child (), start_job_command ();
143 static int load_too_high (), job_next_command ();
145 /* Chain of all live (or recently deceased) children. */
147 struct child
*children
= 0;
149 /* Number of children currently running. */
151 unsigned int job_slots_used
= 0;
153 /* Nonzero if the `good' standard input is in use. */
155 static int good_stdin_used
= 0;
157 /* Chain of children waiting to run until the load average goes down. */
159 static struct child
*waiting_jobs
= 0;
161 /* Write an error message describing the exit status given in
162 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
163 Append "(ignored)" if IGNORED is nonzero. */
166 child_error (target_name
, exit_code
, exit_sig
, coredump
, ignored
)
168 int exit_code
, exit_sig
, coredump
;
172 error (ignored
? "[%s] Error %d (ignored)" :
174 target_name
, exit_code
);
177 char *coredump_string
= coredump
? " (core dumped)" : "";
178 if (exit_sig
> 0 && exit_sig
< NSIG
)
179 error ("*** [%s] %s%s",
180 target_name
, sys_siglist
[exit_sig
], coredump_string
);
182 error ("*** [%s] Signal %d%s", target_name
, exit_sig
, coredump_string
);
186 static unsigned int dead_children
= 0;
188 /* Notice that a child died.
189 reap_children should be called when convenient. */
197 printf ("Got a SIGCHLD; %d unreaped children.\n", dead_children
);
200 extern int shell_function_pid
, shell_function_completed
;
202 /* Reap dead children, storing the returned status and the new command
203 state (`cs_finished') in the `file' member of the `struct child' for the
204 dead child, and removing the child from the chain. If BLOCK nonzero,
205 reap at least one child, waiting for it to die if necessary. If ERR is
206 nonzero, print an error message first. */
209 reap_children (block
, err
)
214 while ((children
!= 0 || shell_function_pid
!= 0) &&
215 (block
|| dead_children
> 0))
219 int exit_code
, exit_sig
, coredump
;
220 register struct child
*lastc
, *c
;
222 int any_remote
, any_local
;
224 if (err
&& dead_children
== 0)
226 /* We might block for a while, so let the user know why. */
228 error ("*** Waiting for unfinished jobs....");
231 /* We have one less dead child to reap.
232 The test and decrement are not atomic; if it is compiled into:
233 register = dead_children - 1;
234 dead_children = register;
235 a SIGCHLD could come between the two instructions.
236 child_handler increments dead_children.
237 The second instruction here would lose that increment. But the
238 only effect of dead_children being wrong is that we might wait
239 longer than necessary to reap a child, and lose some parallelism;
240 and we might print the "Waiting for unfinished jobs" message above
241 when not necessary. */
243 if (dead_children
!= 0)
247 any_local
= shell_function_pid
!= -1;
248 for (c
= children
; c
!= 0; c
= c
->next
)
250 any_remote
|= c
->remote
;
251 any_local
|= ! c
->remote
;
253 printf ("Live child 0x%08lx PID %d%s\n",
254 (unsigned long int) c
,
255 c
->pid
, c
->remote
? " (remote)" : "");
258 /* First, check for remote children. */
260 pid
= remote_status (&exit_code
, &exit_sig
, &coredump
, 0);
270 pfatal_with_name ("remote_status");
275 /* No remote children. Check for local children. */
281 pid
= WAIT_NOHANG (&status
);
284 pid
= wait (&status
);
295 pfatal_with_name ("wait");
299 /* No local children. */
300 if (block
&& any_remote
)
302 /* Now try a blocking wait for a remote child. */
303 pid
= remote_status (&exit_code
, &exit_sig
, &coredump
, 1);
305 goto remote_status_lose
;
307 /* No remote children either. Finally give up. */
310 /* We got a remote child. */
318 /* Chop the status word up. */
319 exit_code
= WEXITSTATUS (status
);
320 exit_sig
= WIFSIGNALED (status
) ? WTERMSIG (status
) : 0;
321 coredump
= WCOREDUMP (status
);
324 /* Life is very different on MSDOS. */
327 exit_code
= dos_status
;
330 #endif /* Not MSDOS. */
333 /* We got a remote child. */
336 /* Check if this is the child of the `shell' function. */
337 if (!remote
&& pid
== shell_function_pid
)
339 /* It is. Leave an indicator for the `shell' function. */
340 if (exit_sig
== 0 && exit_code
== 127)
341 shell_function_completed
= -1;
343 shell_function_completed
= 1;
347 child_failed
= exit_sig
!= 0 || exit_code
!= 0;
349 /* Search for a child matching the deceased one. */
351 for (c
= children
; c
!= 0; lastc
= c
, c
= c
->next
)
352 if (c
->remote
== remote
&& c
->pid
== pid
)
357 /* An unknown child died. */
359 sprintf (buf
, "Unknown%s job %d", remote
? " remote" : "", pid
);
361 child_error (buf
, exit_code
, exit_sig
, coredump
,
364 error ("%s finished.", buf
);
369 printf ("Reaping %s child 0x%08lx PID %d%s\n",
370 child_failed
? "losing" : "winning",
371 (unsigned long int) c
,
372 c
->pid
, c
->remote
? " (remote)" : "");
374 /* If this child had the good stdin, say it is now free. */
378 if (child_failed
&& !c
->noerror
&& !ignore_errors_flag
)
380 /* The commands failed. Write an error message,
381 delete non-precious targets, and abort. */
382 static int delete_on_error
= -1;
383 child_error (c
->file
->name
, exit_code
, exit_sig
, coredump
, 0);
384 c
->file
->update_status
= 1;
385 if (delete_on_error
== -1)
387 struct file
*f
= lookup_file (".DELETE_ON_ERROR");
388 delete_on_error
= f
!= 0 && f
->is_target
;
390 if (exit_sig
!= 0 || delete_on_error
)
391 delete_child_targets (c
);
397 /* The commands failed, but we don't care. */
398 child_error (c
->file
->name
,
399 exit_code
, exit_sig
, coredump
, 1);
403 /* If there are more commands to run, try to start them. */
404 if (job_next_command (c
))
406 if (handling_fatal_signal
)
408 /* Never start new commands while we are dying.
409 Since there are more commands that wanted to be run,
410 the target was not completely remade. So we treat
411 this as if a command had failed. */
412 c
->file
->update_status
= 1;
416 /* Check again whether to start remotely.
417 Whether or not we want to changes over time.
418 Also, start_remote_job may need state set up
419 by start_remote_job_p. */
420 c
->remote
= start_remote_job_p ();
421 start_job_command (c
);
422 if (c
->file
->command_state
== cs_running
)
423 /* We successfully started the new command.
424 Loop to reap more children. */
428 if (c
->file
->update_status
!= 0)
429 /* We failed to start the commands. */
430 delete_child_targets (c
);
433 /* There are no more commands. We got through them all
434 without an unignored error. Now the target has been
435 successfully updated. */
436 c
->file
->update_status
= 0;
439 /* When we get here, all the commands for C->file are finished
440 (or aborted) and C->file->update_status contains 0 or 1. But
441 C->file->command_state is still cs_running if all the commands
442 ran; notice_finish_file looks for cs_running to tell it that
443 it's interesting to check the file's modtime again now. */
445 if (! handling_fatal_signal
)
446 /* Notice if the target of the commands has been changed.
447 This also propagates its values for command_state and
448 update_status to its also_make files. */
449 notice_finished_file (c
->file
);
452 printf ("Removing child 0x%08lx PID %d%s from chain.\n",
453 (unsigned long int) c
,
454 c
->pid
, c
->remote
? " (remote)" : "");
456 /* Remove the child from the chain and free it. */
460 lastc
->next
= c
->next
;
461 if (! handling_fatal_signal
) /* Avoid nonreentrancy. */
464 /* There is now another slot open. */
467 /* If the job failed, and the -k flag was not given, die,
468 unless we are already in the process of dying. */
469 if (!err
&& child_failed
&& !keep_going_flag
)
473 /* Only block for one child. */
478 /* Free the storage allocated for CHILD. */
482 register struct child
*child
;
484 if (child
->command_lines
!= 0)
486 register unsigned int i
;
487 for (i
= 0; i
< child
->file
->cmds
->ncommand_lines
; ++i
)
488 free (child
->command_lines
[i
]);
489 free ((char *) child
->command_lines
);
492 if (child
->environment
!= 0)
494 register char **ep
= child
->environment
;
497 free ((char *) child
->environment
);
500 free ((char *) child
);
511 extern sigset_t fatal_signal_set
;
517 sigemptyset (&empty
);
518 sigprocmask (SIG_SETMASK
, &empty
, (sigset_t
*) 0);
523 /* Start a job to run the commands specified in CHILD.
524 CHILD is updated to reflect the commands and ID of the child process. */
527 start_job_command (child
)
528 register struct child
*child
;
530 static int bad_stdin
= -1;
535 /* Combine the flags parsed for the line itself with
536 the flags specified globally for this target. */
537 flags
= (child
->file
->command_flags
538 | child
->file
->cmds
->lines_flags
[child
->command_line
- 1]);
540 p
= child
->command_ptr
;
541 child
->noerror
= flags
& COMMANDS_NOERROR
;
545 flags
|= COMMANDS_SILENT
;
547 flags
|= COMMANDS_RECURSE
;
550 else if (!isblank (*p
) && *p
!= '+')
555 /* If -q was given, just say that updating `failed'. The exit status of
556 1 tells the user that -q is saying `something to do'; the exit status
557 for a random error is 2. */
558 if (question_flag
&& !(flags
& COMMANDS_RECURSE
))
560 child
->file
->update_status
= 1;
561 notice_finished_file (child
->file
);
565 /* There may be some preceding whitespace left if there
566 was nothing but a backslash on the first line. */
569 /* Figure out an argument list from this command line. */
573 argv
= construct_command_argv (p
, &end
, child
->file
);
575 child
->command_ptr
= NULL
;
579 child
->command_ptr
= end
;
583 if (touch_flag
&& !(flags
& COMMANDS_RECURSE
))
585 /* Go on to the next command. It might be the recursive one.
586 We construct ARGV only to find the end of the command line. */
588 free ((char *) argv
);
594 /* This line has no commands. Go to the next. */
595 if (job_next_command (child
))
596 start_job_command (child
);
599 /* No more commands. All done. */
600 child
->file
->update_status
= 0;
601 notice_finished_file (child
->file
);
606 /* Print out the command. */
608 if (just_print_flag
|| (!(flags
& COMMANDS_SILENT
) && !silent_flag
))
611 /* Tell update_goal_chain that a command has been started on behalf of
612 this target. It is important that this happens here and not in
613 reap_children (where we used to do it), because reap_children might be
614 reaping children from a different target. We want this increment to
615 guaranteedly indicate that a command was started for the dependency
616 chain (i.e., update_file recursion chain) we are processing. */
620 /* If -n was given, recurse to get the next line in the sequence. */
622 if (just_print_flag
&& !(flags
& COMMANDS_RECURSE
))
625 free ((char *) argv
);
626 if (job_next_command (child
))
627 start_job_command (child
);
628 child
->file
->update_status
= 0;
632 /* Flush the output streams so they won't have things written twice. */
637 /* Set up a bad standard input that reads from a broken pipe. */
641 /* Make a file descriptor that is the read end of a broken pipe.
642 This will be used for some children's standard inputs. */
646 /* Close the write side. */
647 (void) close (pd
[1]);
648 /* Save the read side. */
653 /* Decide whether to give this child the `good' standard input
654 (one that points to the terminal or whatever), or the `bad' one
655 that points to the read side of a broken pipe. */
657 child
->good_stdin
= !good_stdin_used
;
658 if (child
->good_stdin
)
663 /* Set up the environment for the child. */
664 if (child
->environment
== 0)
665 child
->environment
= target_environment (child
->file
);
669 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
672 int is_remote
, id
, used_stdin
;
673 if (start_remote_job (argv
, child
->environment
,
674 child
->good_stdin
? 0 : bad_stdin
,
675 &is_remote
, &id
, &used_stdin
))
679 if (child
->good_stdin
&& !used_stdin
)
681 child
->good_stdin
= 0;
684 child
->remote
= is_remote
;
690 /* Fork the child process. */
693 (void) sigprocmask (SIG_BLOCK
, &fatal_signal_set
, (sigset_t
*) 0);
695 #ifdef HAVE_SIGSETMASK
696 (void) sigblock (fatal_signal_mask
);
701 child
->pid
= vfork ();
704 /* We are the child side. */
706 child_execute_job (child
->good_stdin
? 0 : bad_stdin
, 1,
707 argv
, child
->environment
);
709 else if (child
->pid
< 0)
713 perror_with_name ("vfork", "");
719 dos_status
= spawnvpe (P_WAIT
, argv
[0], argv
, child
->environment
);
721 child
->pid
= dos_pid
++;
725 remove (dos_bname
); /* Ignore errors. */
726 if (access (dos_bename
, 0))
732 #endif /* Not MSDOS. */
734 /* We are the parent side. Set the state to
735 say the commands are running and return. */
737 set_command_state (child
->file
, cs_running
);
739 /* Free the storage used by the child's argument list. */
742 free ((char *) argv
);
747 child
->file
->update_status
= 2;
748 notice_finished_file (child
->file
);
751 /* Try to start a child running.
752 Returns nonzero if the child was started (and maybe finished), or zero if
753 the load was too high and the child was put on the `waiting_jobs' chain. */
756 start_waiting_job (c
)
759 /* If we can start a job remotely, we always want to, and don't care about
760 the local load average. We record that the job should be started
761 remotely in C->remote for start_job_command to test. */
763 c
->remote
= start_remote_job_p ();
765 /* If this job is to be started locally, and we are already running
766 some jobs, make this one wait if the load average is too high. */
767 if (!c
->remote
&& job_slots_used
> 0 && load_too_high ())
769 /* Put this child on the chain of children waiting
770 for the load average to go down. */
771 c
->file
->command_state
= cs_running
;
772 c
->next
= waiting_jobs
;
777 /* Start the first command; reap_children will run later command lines. */
778 start_job_command (c
);
780 switch (c
->file
->command_state
)
785 printf ("Putting child 0x%08lx PID %05d%s on the chain.\n",
786 (unsigned long int) c
,
787 c
->pid
, c
->remote
? " (remote)" : "");
789 /* One more job slot is in use. */
795 /* All the command lines turned out to be empty. */
796 c
->file
->update_status
= 0;
800 notice_finished_file (c
->file
);
805 assert (c
->file
->command_state
== cs_finished
);
812 /* Create a `struct child' for FILE and start its commands running. */
816 register struct file
*file
;
818 register struct commands
*cmds
= file
->cmds
;
819 register struct child
*c
;
821 register unsigned int i
;
823 /* Let any previously decided-upon jobs that are waiting
824 for the load to go down start before this new one. */
825 start_waiting_jobs ();
827 /* Reap any children that might have finished recently. */
828 reap_children (0, 0);
830 /* Chop the commands up into lines if they aren't already. */
831 chop_commands (cmds
);
834 /* Wait for a job slot to be freed up. */
835 while (job_slots_used
== job_slots
)
836 reap_children (1, 0);
838 /* Expand the command lines and store the results in LINES. */
839 lines
= (char **) xmalloc (cmds
->ncommand_lines
* sizeof (char *));
840 for (i
= 0; i
< cmds
->ncommand_lines
; ++i
)
842 /* Collapse backslash-newline combinations that are inside variable
843 or function references. These are left alone by the parser so
844 that they will appear in the echoing of commands (where they look
845 nice); and collapsed by construct_command_argv when it tokenizes.
846 But letting them survive inside function invocations loses because
847 we don't want the functions to see them as part of the text. */
849 char *in
, *out
, *ref
;
851 /* IN points to where in the line we are scanning.
852 OUT points to where in the line we are writing.
853 When we collapse a backslash-newline combination,
854 IN gets ahead out OUT. */
856 in
= out
= cmds
->command_lines
[i
];
857 while ((ref
= index (in
, '$')) != 0)
859 ++ref
; /* Move past the $. */
862 /* Copy the text between the end of the last chunk
863 we processed (where IN points) and the new chunk
864 we are about to process (where REF points). */
865 bcopy (in
, out
, ref
- in
);
867 /* Move both pointers past the boring stuff. */
871 if (*ref
== '(' || *ref
== '{')
873 char openparen
= *ref
;
874 char closeparen
= openparen
== '(' ? ')' : '}';
878 *out
++ = *in
++; /* Copy OPENPAREN. */
879 /* IN now points past the opening paren or brace.
880 Count parens or braces until it is matched. */
884 if (*in
== closeparen
&& --count
< 0)
886 else if (*in
== '\\' && in
[1] == '\n')
888 /* We have found a backslash-newline inside a
889 variable or function reference. Eat it and
890 any following whitespace. */
893 for (p
= in
- 1; p
> ref
&& *p
== '\\'; --p
)
897 /* There were two or more backslashes, so this is
898 not really a continuation line. We don't collapse
899 the quoting backslashes here as is done in
900 collapse_continuations, because the line will
901 be collapsed again after expansion. */
905 /* Skip the backslash, newline and
906 any following whitespace. */
907 in
= next_token (in
+ 2);
909 /* Discard any preceding whitespace that has
910 already been written to the output. */
911 while (out
> ref
&& isblank (out
[-1]))
914 /* Replace it all with a single space. */
920 if (*in
== openparen
)
929 /* There are no more references in this line to worry about.
930 Copy the remaining uninteresting text to the output. */
934 /* Finally, expand the line. */
935 lines
[i
] = allocated_variable_expand_for_file (cmds
->command_lines
[i
],
939 /* Start the command sequence, record it in a new
940 `struct child', and add that to the chain. */
942 c
= (struct child
*) xmalloc (sizeof (struct child
));
944 c
->command_lines
= lines
;
949 /* Fetch the first command line to be run. */
950 if (! job_next_command (c
))
952 /* There were no commands! */
954 c
->file
->update_status
= 0;
958 /* The job is now primed. Start it running. */
959 start_waiting_job (c
);
962 /* Since there is only one job slot, make things run linearly.
963 Wait for the child to die, setting the state to `cs_finished'. */
964 while (file
->command_state
== cs_running
)
965 reap_children (1, 0);
969 /* Move CHILD's pointers to the next command for it to execute.
970 Returns nonzero if there is another command. */
973 job_next_command (child
)
976 if (child
->command_ptr
== 0 || *child
->command_ptr
== '\0')
978 /* There are no more lines in the expansion of this line. */
979 if (child
->command_line
== child
->file
->cmds
->ncommand_lines
)
981 /* There are no more lines to be expanded. */
982 child
->command_ptr
= 0;
986 /* Get the next line to run. */
987 child
->command_ptr
= child
->command_lines
[child
->command_line
++];
998 extern int getloadavg ();
1001 if (max_load_average
< 0)
1005 if (getloadavg (&load
, 1) != 1)
1007 static int lossage
= -1;
1008 /* Complain only once for the same error. */
1009 if (lossage
== -1 || errno
!= lossage
)
1012 /* An errno value of zero means getloadavg is just unsupported. */
1013 error ("cannot enforce load limits on this operating system");
1015 perror_with_name ("cannot enforce load limit: ", "getloadavg");
1022 return load
>= max_load_average
;
1026 /* Start jobs that are waiting for the load to be lower. */
1029 start_waiting_jobs ()
1033 if (waiting_jobs
== 0)
1038 /* Check for recently deceased descendants. */
1039 reap_children (0, 0);
1041 /* Take a job off the waiting list. */
1043 waiting_jobs
= job
->next
;
1045 /* Try to start that job. We break out of the loop as soon
1046 as start_waiting_job puts one back on the waiting list. */
1047 } while (start_waiting_job (job
) && waiting_jobs
!= 0);
1050 /* Replace the current process with one executing the command in ARGV.
1051 STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
1052 the environment of the new program. This function does not return. */
1055 child_execute_job (stdin_fd
, stdout_fd
, argv
, envp
)
1056 int stdin_fd
, stdout_fd
;
1057 char **argv
, **envp
;
1060 (void) dup2 (stdin_fd
, 0);
1062 (void) dup2 (stdout_fd
, 1);
1064 /* Free up file descriptors. */
1067 int max
= getdtablesize ();
1068 for (d
= 3; d
< max
; ++d
)
1072 /* Run the command. */
1073 exec_command (argv
, envp
);
1076 /* Search PATH for FILE.
1077 If successful, store the full pathname in PROGRAM and return 1.
1078 If not sucessful, return zero. */
1081 search_path (file
, path
, program
)
1082 char *file
, *path
, *program
;
1084 if (path
== 0 || path
[0] == '\0')
1085 path
= default_path
;
1089 strpbrk (file
, "/\\:")
1095 strcpy (program
, file
);
1102 #ifdef HAVE_GETGROUPS
1103 #ifndef HAVE_UNISTD_H
1104 extern int getgroups ();
1106 static int ngroups
= -1;
1108 static GETGROUPS_T groups
[NGROUPS_MAX
];
1109 #define ngroups_max NGROUPS_MAX
1111 static GETGROUPS_T
*groups
= 0;
1112 static int ngroups_max
;
1115 ngroups_max
= GET_NGROUPS_MAX
;
1116 groups
= (GETGROUPS_T
*) malloc (ngroups_max
* sizeof (GETGROUPS_T
));
1119 if (groups
!= 0 && ngroups
== -1)
1120 ngroups
= getgroups (ngroups_max
, groups
);
1121 #endif /* Have getgroups. */
1123 len
= strlen (file
) + 1;
1130 p
= index (path
, PATH_SEPARATOR_CHAR
);
1132 p
= path
+ strlen (path
);
1135 bcopy (file
, program
, len
);
1138 bcopy (path
, program
, p
- path
);
1139 program
[p
- path
] = '/';
1140 bcopy (file
, program
+ (p
- path
) + 1, len
);
1143 if (safe_stat (program
, &st
) == 0
1144 && S_ISREG (st
.st_mode
))
1146 if (st
.st_uid
== geteuid ())
1147 perm
= (st
.st_mode
& 0100);
1148 else if (st
.st_gid
== getegid ())
1149 perm
= (st
.st_mode
& 0010);
1152 #ifdef HAVE_GETGROUPS
1154 for (i
= 0; i
< ngroups
; ++i
)
1155 if (groups
[i
] == st
.st_gid
)
1158 perm
= (st
.st_mode
& 0010);
1160 #endif /* Have getgroups. */
1161 perm
= (st
.st_mode
& 0001);
1169 } while (*path
!= '\0');
1175 /* Replace the current process with one running the command in ARGV,
1176 with environment ENVP. This function does not return. */
1179 exec_command (argv
, envp
)
1180 char **argv
, **envp
;
1187 for (ep
= envp
; *ep
!= 0; ++ep
)
1189 if (shell
== 0 && !strncmp(*ep
, "SHELL=", 6))
1191 else if (path
== 0 && !strncmp(*ep
, "PATH=", 5))
1193 else if (path
!= 0 && shell
!= 0)
1197 /* Be the user, permanently. */
1200 if (!search_path (argv
[0], path
, program
))
1201 error ("%s: Command not found", argv
[0]);
1204 /* Run the program. */
1205 execve (program
, argv
, envp
);
1207 if (errno
== ENOEXEC
)
1209 PATH_VAR (shell_program
);
1212 shell_path
= default_shell
;
1215 if (search_path (shell
, path
, shell_program
))
1216 shell_path
= shell_program
;
1220 error ("%s: Shell program not found", shell
);
1224 if (shell_path
!= 0)
1230 while (argv
[argc
] != 0)
1233 new_argv
= (char **) alloca ((1 + argc
+ 1) * sizeof (char *));
1234 new_argv
[0] = shell_path
;
1235 new_argv
[1] = program
;
1238 new_argv
[1 + argc
] = argv
[argc
];
1242 execve (shell_path
, new_argv
, envp
);
1243 perror_with_name ("execve: ", shell_path
);
1247 perror_with_name ("execve: ", program
);
1253 /* Figure out the argument list necessary to run LINE as a command.
1254 Try to avoid using a shell. This routine handles only ' quoting.
1255 Starting quotes may be escaped with a backslash. If any of the
1256 characters in sh_chars[] is seen, or any of the builtin commands
1257 listed in sh_cmds[] is the first word of a line, the shell is used.
1259 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
1260 If *RESTP is NULL, newlines will be ignored.
1262 SHELL is the shell to use, or nil to use the default shell.
1263 IFS is the value of $IFS, or nil (meaning the default). */
1266 construct_command_argv_internal (line
, restp
, shell
, ifs
)
1267 char *line
, **restp
;
1271 static char sh_chars
[] = "\"|<>";
1272 static char *sh_cmds
[] = { "break", "call", "cd", "chcp", "chdir", "cls",
1273 "copy", "ctty", "date", "del", "dir", "echo",
1274 "erase", "exit", "for", "goto", "if", "if", "md",
1275 "mkdir", "path", "pause", "prompt", "rem", "ren",
1276 "rename", "set", "shift", "time", "type",
1277 "ver", "verify", "vol", ":", 0 };
1279 static char sh_chars
[] = "#;\"*?[]&|<>(){}$`^";
1280 static char *sh_cmds
[] = { "cd", "eval", "exec", "exit", "login",
1281 "logout", "set", "umask", "wait", "while", "for",
1282 "case", "if", ":", ".", "break", "continue",
1283 "export", "read", "readonly", "shift", "times",
1284 "trap", "switch", 0 };
1290 int instring
, word_has_equals
, seen_nonequals
;
1291 char **new_argv
= 0;
1296 /* Make sure not to bother processing an empty line. */
1297 while (isblank (*line
))
1302 /* See if it is safe to parse commands internally. */
1304 shell
= default_shell
;
1305 else if (strcmp (shell
, default_shell
))
1309 for (ap
= ifs
; *ap
!= '\0'; ++ap
)
1310 if (*ap
!= ' ' && *ap
!= '\t' && *ap
!= '\n')
1313 i
= strlen (line
) + 1;
1315 /* More than 1 arg per character is impossible. */
1316 new_argv
= (char **) xmalloc (i
* sizeof (char *));
1318 /* All the args can fit in a buffer as big as LINE is. */
1319 ap
= new_argv
[0] = (char *) xmalloc (i
);
1322 /* I is how many complete arguments have been found. */
1324 instring
= word_has_equals
= seen_nonequals
= 0;
1325 for (p
= line
; *p
!= '\0'; ++p
)
1333 /* Inside a string, just copy any char except a closing quote
1334 or a backslash-newline combination. */
1337 else if (*p
== '\\' && p
[1] == '\n')
1338 goto swallow_escaped_newline
;
1339 else if (*p
== '\n' && restp
!= NULL
)
1341 /* End of the command line. */
1348 else if (index (sh_chars
, *p
) != 0)
1349 /* Not inside a string, but it's a special char. */
1352 /* Not a special char. */
1356 /* Equals is a special character in leading words before the
1357 first word with no equals sign in it. This is not the case
1358 with sh -k, but we never get here when using nonstandard
1360 if (! seen_nonequals
)
1362 word_has_equals
= 1;
1367 /* Backslash-newline combinations are eaten. */
1370 swallow_escaped_newline
:
1372 /* Eat the backslash, the newline, and following whitespace,
1373 replacing it all with a single space. */
1376 /* If there is a tab after a backslash-newline,
1377 remove it from the source line which will be echoed,
1378 since it was most likely used to line
1379 up the continued line with the previous one. */
1387 if (ap
!= new_argv
[i
])
1388 /* Treat this as a space, ending the arg.
1389 But if it's at the beginning of the arg, it should
1390 just get eaten, rather than becoming an empty arg. */
1393 p
= next_token (p
) - 1;
1396 else if (p
[1] != '\0')
1397 /* Copy and skip the following char. */
1408 /* End of the command line. */
1413 /* Newlines are not special. */
1420 /* We have the end of an argument.
1421 Terminate the text of the argument. */
1425 /* Update SEEN_NONEQUALS, which tells us if every word
1426 heretofore has contained an `='. */
1427 seen_nonequals
|= ! word_has_equals
;
1428 if (word_has_equals
&& ! seen_nonequals
)
1429 /* An `=' in a word before the first
1430 word without one is magical. */
1432 word_has_equals
= 0; /* Prepare for the next word. */
1434 /* If this argument is the command name,
1435 see if it is a built-in shell command.
1436 If so, have the shell handle it. */
1440 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
1441 if (streq (sh_cmds
[j
], new_argv
[0]))
1445 /* Ignore multiple whitespace chars. */
1447 /* Next iteration should examine the first nonwhite char. */
1459 /* Let the shell deal with an unterminated quote. */
1462 /* Terminate the last argument and the argument list. */
1465 if (new_argv
[i
][0] != '\0')
1472 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
1473 if (streq (sh_cmds
[j
], new_argv
[0]))
1477 if (new_argv
[0] == 0)
1478 /* Line was empty. */
1484 /* We must use the shell. */
1488 /* Free the old argument list we were working on. */
1499 dos_bname
= tempnam (".", "mk");
1500 for (i
= 0; dos_bname
[i
] != '\0'; ++i
)
1501 if (dos_bname
[i
] == '/')
1502 dos_bname
[i
] = '\\';
1503 dos_bename
= (char *) xmalloc (strlen (dos_bname
) + 5);
1504 strcpy (dos_bename
, dos_bname
);
1505 strcat (dos_bname
, ".bat");
1506 strcat (dos_bename
, ".err");
1508 batch
= fopen(bename
, "w"); /* Create a file. */
1511 batch
= fopen (dos_bname
, "w");
1512 fputs ("@echo off\n", batch
);
1513 fputs (line
, batch
);
1514 fprintf (batch
, "\nif errorlevel 1 del %s\n", dos_bename
);
1516 new_argv
= (char **) xmalloc(2 * sizeof(char *));
1517 new_argv
[0] = strdup (bname
);
1520 #else /* Not MSDOS. */
1522 /* SHELL may be a multi-word command. Construct a command line
1523 "SHELL -c LINE", with all special chars in LINE escaped.
1524 Then recurse, expanding this command line to get the final
1527 unsigned int shell_len
= strlen (shell
);
1528 static char minus_c
[] = " -c ";
1529 unsigned int line_len
= strlen (line
);
1531 char *new_line
= (char *) alloca (shell_len
+ (sizeof (minus_c
) - 1)
1532 + (line_len
* 2) + 1);
1535 bcopy (shell
, ap
, shell_len
);
1537 bcopy (minus_c
, ap
, sizeof (minus_c
) - 1);
1538 ap
+= sizeof (minus_c
) - 1;
1539 for (p
= line
; *p
!= '\0'; ++p
)
1541 if (restp
!= NULL
&& *p
== '\n')
1546 else if (*p
== '\\' && p
[1] == '\n')
1548 /* Eat the backslash, the newline, and following whitespace,
1549 replacing it all with a single space (which is escaped
1553 /* If there is a tab after a backslash-newline,
1554 remove it from the source line which will be echoed,
1555 since it was most likely used to line
1556 up the continued line with the previous one. */
1567 if (*p
== '\\' || *p
== '\''
1569 || index (sh_chars
, *p
) != 0)
1575 new_argv
= construct_command_argv_internal (new_line
, (char **) NULL
,
1576 (char *) 0, (char *) 0);
1583 /* Figure out the argument list necessary to run LINE as a command.
1584 Try to avoid using a shell. This routine handles only ' quoting.
1585 Starting quotes may be escaped with a backslash. If any of the
1586 characters in sh_chars[] is seen, or any of the builtin commands
1587 listed in sh_cmds[] is the first word of a line, the shell is used.
1589 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
1590 If *RESTP is NULL, newlines will be ignored.
1592 FILE is the target whose commands these are. It is used for
1593 variable expansion for $(SHELL) and $(IFS). */
1596 construct_command_argv (line
, restp
, file
)
1597 char *line
, **restp
;
1604 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
1605 int save
= warn_undefined_variables_flag
;
1606 warn_undefined_variables_flag
= 0;
1608 shell
= allocated_variable_expand_for_file ("$(SHELL)", file
);
1609 ifs
= allocated_variable_expand_for_file ("$(IFS)", file
);
1611 warn_undefined_variables_flag
= save
;
1614 argv
= construct_command_argv_internal (line
, restp
, shell
, ifs
);