| 1 | /* Definitions for managing subprocesses in GNU Make.
|
|---|
| 2 | Copyright (C) 1992-2016 Free Software Foundation, Inc.
|
|---|
| 3 | This file is part of GNU Make.
|
|---|
| 4 |
|
|---|
| 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
|
|---|
| 7 | Foundation; either version 3 of the License, or (at your option) any later
|
|---|
| 8 | version.
|
|---|
| 9 |
|
|---|
| 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.
|
|---|
| 13 |
|
|---|
| 14 | You should have received a copy of the GNU General Public License along with
|
|---|
| 15 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
|---|
| 16 |
|
|---|
| 17 | #include "output.h"
|
|---|
| 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 |
|
|---|
| 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
|
|---|
| 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 |
|
|---|
| 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, ...);
|
|---|
| 70 | # ifdef CONFIG_NEW_WIN_CHILDREN
|
|---|
| 71 | intptr_t create_mutex (char *mtxname, size_t size);
|
|---|
| 72 | # else
|
|---|
| 73 | intptr_t create_mutex (void);
|
|---|
| 74 | # endif
|
|---|
| 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);
|
|---|
| 79 | # ifdef CONFIG_NEW_WIN_CHILDREN
|
|---|
| 80 | void prepare_mutex_handle_string (const char *mtxname);
|
|---|
| 81 | # else
|
|---|
| 82 | void prepare_mutex_handle_string (intptr_t hdl);
|
|---|
| 83 | # endif
|
|---|
| 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 |
|
|---|
| 93 | /* Structure describing a running or dead child process. */
|
|---|
| 94 |
|
|---|
| 95 | struct child
|
|---|
| 96 | {
|
|---|
| 97 | struct child *next; /* Link in the chain. */
|
|---|
| 98 |
|
|---|
| 99 | struct file *file; /* File being remade. */
|
|---|
| 100 |
|
|---|
| 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]. */
|
|---|
| 105 |
|
|---|
| 106 | #ifdef VMS
|
|---|
| 107 | char *comname; /* Temporary command file name */
|
|---|
| 108 | int efn; /* Completion event flag number */
|
|---|
| 109 | int cstatus; /* Completion status */
|
|---|
| 110 | int vms_launch_status; /* non-zero if lib$spawn, etc failed */
|
|---|
| 111 | #endif
|
|---|
| 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 |
|
|---|
| 123 | #ifdef CONFIG_WITH_KMK_BUILTIN
|
|---|
| 124 | unsigned int has_status:1; /* Nonzero if status is available. */
|
|---|
| 125 | int status; /* Status of the job. */
|
|---|
| 126 | #endif
|
|---|
| 127 | #ifdef CONFIG_WITH_PRINT_TIME_SWITCH
|
|---|
| 128 | big_int start_ts; /* nano_timestamp of the first command. */
|
|---|
| 129 | #endif
|
|---|
| 130 | };
|
|---|
| 131 |
|
|---|
| 132 | extern struct child *children;
|
|---|
| 133 |
|
|---|
| 134 | /* A signal handler for SIGCHLD, if needed. */
|
|---|
| 135 | RETSIGTYPE child_handler (int sig);
|
|---|
| 136 | int is_bourne_compatible_shell(const char *path);
|
|---|
| 137 | void new_job (struct file *file);
|
|---|
| 138 | void reap_children (int block, int err);
|
|---|
| 139 | void start_waiting_jobs (void);
|
|---|
| 140 |
|
|---|
| 141 | char **construct_command_argv (char *line, char **restp, struct file *file,
|
|---|
| 142 | int cmd_flags, char** batch_file);
|
|---|
| 143 |
|
|---|
| 144 | #ifdef VMS
|
|---|
| 145 | int child_execute_job (struct child *child, char *argv);
|
|---|
| 146 | #else
|
|---|
| 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);
|
|---|
| 151 | #endif
|
|---|
| 152 |
|
|---|
| 153 | #ifdef _AMIGA
|
|---|
| 154 | void exec_command (char **argv) __attribute__ ((noreturn));
|
|---|
| 155 | #elif defined(__EMX__)
|
|---|
| 156 | int exec_command (char **argv, char **envp);
|
|---|
| 157 | #elif !defined(WINDOWS32) || !defined(CONFIG_NEW_WIN_CHILDREN)
|
|---|
| 158 | void exec_command (char **argv, char **envp) __attribute__ ((noreturn));
|
|---|
| 159 | #endif
|
|---|
| 160 |
|
|---|
| 161 | extern unsigned int job_slots_used;
|
|---|
| 162 |
|
|---|
| 163 | void block_sigs (void);
|
|---|
| 164 | #ifdef POSIX
|
|---|
| 165 | void unblock_sigs (void);
|
|---|
| 166 | #else
|
|---|
| 167 | #ifdef HAVE_SIGSETMASK
|
|---|
| 168 | extern int fatal_signal_mask;
|
|---|
| 169 | #define unblock_sigs() sigsetmask (0)
|
|---|
| 170 | #else
|
|---|
| 171 | #define unblock_sigs()
|
|---|
| 172 | #endif
|
|---|
| 173 | #endif
|
|---|
| 174 |
|
|---|
| 175 | extern unsigned int jobserver_tokens;
|
|---|