[53] | 1 | /* Definitions for managing subprocesses in GNU Make.
|
---|
[3140] | 2 | Copyright (C) 1992-2016 Free Software Foundation, Inc.
|
---|
[53] | 3 | This file is part of GNU Make.
|
---|
| 4 |
|
---|
[503] | 5 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
| 6 | terms of the GNU General Public License as published by the Free Software
|
---|
[1993] | 7 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
| 8 | version.
|
---|
[53] | 9 |
|
---|
[503] | 10 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
| 11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
| 12 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
[53] | 13 |
|
---|
[503] | 14 | You should have received a copy of the GNU General Public License along with
|
---|
[1993] | 15 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
[53] | 16 |
|
---|
[3140] | 17 | #include "output.h"
|
---|
[53] | 18 |
|
---|
| 19 | #ifdef HAVE_FCNTL_H
|
---|
| 20 | # include <fcntl.h>
|
---|
| 21 | #else
|
---|
| 22 | # include <sys/file.h>
|
---|
| 23 | #endif
|
---|
| 24 |
|
---|
| 25 | /* How to set close-on-exec for a file descriptor. */
|
---|
| 26 |
|
---|
[3140] | 27 | #if !defined(F_SETFD) || !defined(F_GETFD)
|
---|
| 28 | # ifdef WINDOWS32
|
---|
| 29 | # define CLOSE_ON_EXEC(_d) process_noinherit(_d)
|
---|
| 30 | # else
|
---|
| 31 | # define CLOSE_ON_EXEC(_d)
|
---|
| 32 | # endif
|
---|
[53] | 33 | #else
|
---|
| 34 | # ifndef FD_CLOEXEC
|
---|
| 35 | # define FD_CLOEXEC 1
|
---|
| 36 | # endif
|
---|
| 37 | # define CLOSE_ON_EXEC(_d) (void) fcntl ((_d), F_SETFD, FD_CLOEXEC)
|
---|
| 38 | #endif
|
---|
| 39 |
|
---|
[3140] | 40 | #ifdef NO_OUTPUT_SYNC
|
---|
| 41 | # define RECORD_SYNC_MUTEX(m) \
|
---|
| 42 | O (error, NILF, \
|
---|
| 43 | _("-O[TYPE] (--output-sync[=TYPE]) is not configured for this build."));
|
---|
| 44 | #else
|
---|
| 45 | # ifdef WINDOWS32
|
---|
| 46 | /* For emulations in w32/compat/posixfcn.c. */
|
---|
| 47 | # define F_GETFD 1
|
---|
| 48 | # define F_SETLKW 2
|
---|
| 49 | /* Implementation note: None of the values of l_type below can be zero
|
---|
| 50 | -- they are compared with a static instance of the struct, so zero
|
---|
| 51 | means unknown/invalid, see w32/compat/posixfcn.c. */
|
---|
| 52 | # define F_WRLCK 1
|
---|
| 53 | # define F_UNLCK 2
|
---|
| 54 |
|
---|
| 55 | struct flock
|
---|
| 56 | {
|
---|
| 57 | short l_type;
|
---|
| 58 | short l_whence;
|
---|
| 59 | off_t l_start;
|
---|
| 60 | off_t l_len;
|
---|
| 61 | pid_t l_pid;
|
---|
| 62 | };
|
---|
| 63 |
|
---|
| 64 | /* This type is actually a HANDLE, but we want to avoid including
|
---|
| 65 | windows.h as much as possible. */
|
---|
| 66 | typedef intptr_t sync_handle_t;
|
---|
| 67 |
|
---|
| 68 | /* Public functions emulated/provided in posixfcn.c. */
|
---|
| 69 | int fcntl (intptr_t fd, int cmd, ...);
|
---|
[3194] | 70 | # ifdef CONFIG_NEW_WIN_CHILDREN
|
---|
| 71 | intptr_t create_mutex (char *mtxname, size_t size);
|
---|
| 72 | # else
|
---|
[3140] | 73 | intptr_t create_mutex (void);
|
---|
[3194] | 74 | # endif
|
---|
[3140] | 75 | int same_stream (FILE *f1, FILE *f2);
|
---|
| 76 |
|
---|
| 77 | # define RECORD_SYNC_MUTEX(m) record_sync_mutex(m)
|
---|
| 78 | void record_sync_mutex (const char *str);
|
---|
[3194] | 79 | # ifdef CONFIG_NEW_WIN_CHILDREN
|
---|
| 80 | void prepare_mutex_handle_string (const char *mtxname);
|
---|
| 81 | # else
|
---|
[3140] | 82 | void prepare_mutex_handle_string (intptr_t hdl);
|
---|
[3194] | 83 | # endif
|
---|
[3140] | 84 | # else /* !WINDOWS32 */
|
---|
| 85 |
|
---|
| 86 | typedef int sync_handle_t; /* file descriptor */
|
---|
| 87 |
|
---|
| 88 | # define RECORD_SYNC_MUTEX(m) (void)(m)
|
---|
| 89 |
|
---|
| 90 | # endif
|
---|
| 91 | #endif /* !NO_OUTPUT_SYNC */
|
---|
| 92 |
|
---|
[53] | 93 | /* Structure describing a running or dead child process. */
|
---|
| 94 |
|
---|
| 95 | struct child
|
---|
| 96 | {
|
---|
[3140] | 97 | struct child *next; /* Link in the chain. */
|
---|
[53] | 98 |
|
---|
[3140] | 99 | struct file *file; /* File being remade. */
|
---|
[53] | 100 |
|
---|
[3140] | 101 | char **environment; /* Environment for commands. */
|
---|
| 102 | char *sh_batch_file; /* Script file for shell commands */
|
---|
| 103 | char **command_lines; /* Array of variable-expanded cmd lines. */
|
---|
| 104 | char *command_ptr; /* Ptr into command_lines[command_line]. */
|
---|
[53] | 105 |
|
---|
| 106 | #ifdef VMS
|
---|
[2591] | 107 | char *comname; /* Temporary command file name */
|
---|
[3140] | 108 | int efn; /* Completion event flag number */
|
---|
| 109 | int cstatus; /* Completion status */
|
---|
| 110 | int vms_launch_status; /* non-zero if lib$spawn, etc failed */
|
---|
[53] | 111 | #endif
|
---|
[3140] | 112 |
|
---|
| 113 | unsigned int command_line; /* Index into command_lines. */
|
---|
| 114 | struct output output; /* Output for this child. */
|
---|
| 115 | pid_t pid; /* Child process's ID number. */
|
---|
| 116 | unsigned int remote:1; /* Nonzero if executing remotely. */
|
---|
| 117 | unsigned int noerror:1; /* Nonzero if commands contained a '-'. */
|
---|
| 118 | unsigned int good_stdin:1; /* Nonzero if this child has a good stdin. */
|
---|
| 119 | unsigned int deleted:1; /* Nonzero if targets have been deleted. */
|
---|
| 120 | unsigned int recursive:1; /* Nonzero for recursive command ('+' etc.) */
|
---|
| 121 | unsigned int dontcare:1; /* Saved dontcare flag. */
|
---|
| 122 |
|
---|
[520] | 123 | #ifdef CONFIG_WITH_KMK_BUILTIN
|
---|
[3140] | 124 | unsigned int has_status:1; /* Nonzero if status is available. */
|
---|
[227] | 125 | int status; /* Status of the job. */
|
---|
| 126 | #endif
|
---|
[2101] | 127 | #ifdef CONFIG_WITH_PRINT_TIME_SWITCH
|
---|
| 128 | big_int start_ts; /* nano_timestamp of the first command. */
|
---|
| 129 | #endif
|
---|
[53] | 130 | };
|
---|
| 131 |
|
---|
| 132 | extern struct child *children;
|
---|
| 133 |
|
---|
[3140] | 134 | /* A signal handler for SIGCHLD, if needed. */
|
---|
| 135 | RETSIGTYPE child_handler (int sig);
|
---|
[2591] | 136 | int is_bourne_compatible_shell(const char *path);
|
---|
[903] | 137 | void new_job (struct file *file);
|
---|
| 138 | void reap_children (int block, int err);
|
---|
| 139 | void start_waiting_jobs (void);
|
---|
[53] | 140 |
|
---|
[1993] | 141 | char **construct_command_argv (char *line, char **restp, struct file *file,
|
---|
| 142 | int cmd_flags, char** batch_file);
|
---|
[3140] | 143 |
|
---|
[53] | 144 | #ifdef VMS
|
---|
[3140] | 145 | int child_execute_job (struct child *child, char *argv);
|
---|
[53] | 146 | #else
|
---|
[3140] | 147 | # define FD_STDIN (fileno (stdin))
|
---|
| 148 | # define FD_STDOUT (fileno (stdout))
|
---|
| 149 | # define FD_STDERR (fileno (stderr))
|
---|
| 150 | int child_execute_job (struct output *out, int good_stdin, char **argv, char **envp);
|
---|
[53] | 151 | #endif
|
---|
[3140] | 152 |
|
---|
[53] | 153 | #ifdef _AMIGA
|
---|
[3140] | 154 | void exec_command (char **argv) __attribute__ ((noreturn));
|
---|
[53] | 155 | #elif defined(__EMX__)
|
---|
[903] | 156 | int exec_command (char **argv, char **envp);
|
---|
[3156] | 157 | #elif !defined(WINDOWS32) || !defined(CONFIG_NEW_WIN_CHILDREN)
|
---|
[3140] | 158 | void exec_command (char **argv, char **envp) __attribute__ ((noreturn));
|
---|
[53] | 159 | #endif
|
---|
| 160 |
|
---|
| 161 | extern unsigned int job_slots_used;
|
---|
| 162 |
|
---|
[903] | 163 | void block_sigs (void);
|
---|
[53] | 164 | #ifdef POSIX
|
---|
[903] | 165 | void unblock_sigs (void);
|
---|
[53] | 166 | #else
|
---|
[3140] | 167 | #ifdef HAVE_SIGSETMASK
|
---|
[53] | 168 | extern int fatal_signal_mask;
|
---|
[3140] | 169 | #define unblock_sigs() sigsetmask (0)
|
---|
[53] | 170 | #else
|
---|
[3140] | 171 | #define unblock_sigs()
|
---|
[53] | 172 | #endif
|
---|
| 173 | #endif
|
---|
| 174 |
|
---|
[287] | 175 | extern unsigned int jobserver_tokens;
|
---|