1 /* POSIX-based operating system interface for GNU Make.
2 Copyright (C) 2016-2022 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 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
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.
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <https://www.gnu.org/licenses/>. */
23 # define FD_OK(_f) (fcntl ((_f), F_GETFD) != -1)
24 #elif defined(HAVE_SYS_FILE_H)
25 # include <sys/file.h>
32 #if defined(HAVE_PSELECT) && defined(HAVE_SYS_SELECT_H)
33 # include <sys/select.h>
40 #define STREAM_OK(_s) ((fcntl (fileno (_s), F_GETFD) != -1) || (errno != EBADF))
45 static unsigned int state = IO_UNKNOWN;
47 /* We only need to compute this once per process. */
48 if (state != IO_UNKNOWN)
51 if (STREAM_OK (stdin))
53 if (STREAM_OK (stdout))
54 state |= IO_STDOUT_OK;
55 if (STREAM_OK (stderr))
56 state |= IO_STDERR_OK;
58 if (ALL_SET (state, IO_STDOUT_OK|IO_STDERR_OK))
60 struct stat stbuf_o, stbuf_e;
62 if (fstat (fileno (stdout), &stbuf_o) == 0
63 && fstat (fileno (stderr), &stbuf_e) == 0
64 && stbuf_o.st_dev == stbuf_e.st_dev
65 && stbuf_o.st_ino == stbuf_e.st_ino)
66 state |= IO_COMBINED_OUTERR;
72 #if defined(MAKE_JOBSERVER)
74 #define FIFO_PREFIX "fifo:"
76 /* This section provides OS-specific functions to support the jobserver. */
78 /* True if this is the root make instance. */
79 static unsigned char job_root = 0;
81 /* These track the state of the jobserver pipe. Passed to child instances. */
82 static int job_fds[2] = { -1, -1 };
84 /* Used to signal read() that a SIGCHLD happened. Always CLOEXEC.
85 If we use pselect() this will never be created and always -1.
87 static int job_rfd = -1;
89 /* Token written to the pipe (could be any character...) */
90 static char token = '+';
92 /* The type of jobserver we're using. */
95 js_none = 0, /* No jobserver. */
96 js_pipe, /* Use a simple pipe as the jobserver. */
97 js_fifo /* Use a named pipe as the jobserver. */
100 static enum js_type js_type = js_none;
102 /* The name of the named pipe (if used). */
103 static char *fifo_name = NULL;
109 /* Pretend we succeeded. */
112 EINTRLOOP (job_rfd, dup (job_fds[0]));
114 fd_noinherit (job_rfd);
121 set_blocking (int fd, int blocking)
123 /* If we're not using pselect() don't change the blocking. */
126 EINTRLOOP (flags, fcntl (fd, F_GETFL));
130 flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
131 EINTRLOOP (r, fcntl (fd, F_SETFL, flags));
133 pfatal_with_name ("fcntl(O_NONBLOCK)");
139 jobserver_setup (int slots, const char *style)
144 if (style == NULL || strcmp (style, "fifo") == 0)
146 /* Unfortunately glibc warns about uses of mktemp even though we aren't
147 using it in dangerous way here. So avoid this by generating our own
148 temporary file name. */
149 # define FNAME_PREFIX "GMfifo"
150 const char *tmpdir = get_tmpdir ();
152 fifo_name = xmalloc (strlen (tmpdir) + CSTRLEN (FNAME_PREFIX)
153 + INTSTR_LENGTH + 2);
154 sprintf (fifo_name, "%s/" FNAME_PREFIX "%" MK_PRI64_PREFIX "d",
155 tmpdir, (long long)make_pid ());
157 EINTRLOOP (r, mkfifo (fifo_name, 0600));
160 perror_with_name("jobserver mkfifo: ", fifo_name);
166 /* We have to open the read side in non-blocking mode, else it will
167 hang until the write side is open. */
168 EINTRLOOP (job_fds[0], open (fifo_name, O_RDONLY|O_NONBLOCK));
170 OSS (fatal, NILF, _("Cannot open jobserver %s: %s"),
171 fifo_name, strerror (errno));
173 EINTRLOOP (job_fds[1], open (fifo_name, O_WRONLY));
175 OSS (fatal, NILF, _("Cannot open jobserver %s: %s"),
176 fifo_name, strerror (errno));
183 if (js_type == js_none)
185 if (style && strcmp (style, "pipe") != 0)
186 OS (fatal, NILF, _("Unknown jobserver auth style '%s'"), style);
188 EINTRLOOP (r, pipe (job_fds));
190 pfatal_with_name (_("creating jobs pipe"));
195 /* By default we don't send the job pipe FDs to our children.
196 See jobserver_pre_child() and jobserver_post_child(). */
197 fd_noinherit (job_fds[0]);
198 fd_noinherit (job_fds[1]);
200 if (make_job_rfd () < 0)
201 pfatal_with_name (_("duping jobs pipe"));
205 EINTRLOOP (r, write (job_fds[1], &token, 1));
207 pfatal_with_name (_("init jobserver pipe"));
210 /* When using pselect() we want the read to be non-blocking. */
211 set_blocking (job_fds[0], 0);
219 jobserver_parse_auth (const char *auth)
223 /* Given the command-line parameter, parse it. */
225 /* First see if we're using a named pipe. */
226 if (strncmp (auth, FIFO_PREFIX, CSTRLEN (FIFO_PREFIX)) == 0)
228 fifo_name = xstrdup (auth + CSTRLEN (FIFO_PREFIX));
230 EINTRLOOP (job_fds[0], open (fifo_name, O_RDONLY));
233 _("Cannot open jobserver %s: %s"), fifo_name, strerror (errno));
235 EINTRLOOP (job_fds[1], open (fifo_name, O_WRONLY));
238 _("Cannot open jobserver %s: %s"), fifo_name, strerror (errno));
242 /* If not, it must be a simple pipe. */
243 else if (sscanf (auth, "%d,%d", &rfd, &wfd) == 2)
245 /* The parent overrode our FDs because we aren't a recursive make. */
246 if (rfd == -2 || wfd == -2)
249 /* Make sure our pipeline is valid. */
250 if (!FD_OK (rfd) || !FD_OK (wfd))
258 /* Who knows what it is? */
261 OS (error, NILF, _("invalid --jobserver-auth string '%s'"), auth);
265 /* Create a duplicate pipe, if needed, that will be closed in the SIGCHLD
266 handler. If this fails with EBADF, the parent closed the pipe on us as
267 it didn't think we were a submake. If so, warn and default to -j1. */
269 if (make_job_rfd () < 0)
272 pfatal_with_name ("jobserver readfd");
279 /* When using pselect() we want the read to be non-blocking. */
280 set_blocking (job_fds[0], 0);
282 /* By default we don't send the job pipe FDs to our children.
283 See jobserver_pre_child() and jobserver_post_child(). */
284 fd_noinherit (job_fds[0]);
285 fd_noinherit (job_fds[1]);
291 jobserver_get_auth ()
295 if (js_type == js_fifo) {
296 auth = xmalloc (strlen (fifo_name) + CSTRLEN (FIFO_PREFIX) + 1);
297 sprintf (auth, FIFO_PREFIX "%s", fifo_name);
299 auth = xmalloc ((INTSTR_LENGTH * 2) + 2);
300 sprintf (auth, "%d,%d", job_fds[0], job_fds[1]);
307 jobserver_get_invalid_auth ()
309 /* If we're using a named pipe we don't need to invalidate the jobserver. */
310 if (js_type == js_fifo) {
314 /* It's not really great that we are assuming the command line option
315 here but other alternatives are also gross. */
316 return " --" JOBSERVER_AUTH_OPT "=-2,-2";
322 return js_type != js_none;
335 job_fds[0] = job_fds[1] = job_rfd = -1;
342 EINTRLOOP (r, unlink (fifo_name));
345 if (!handling_fatal_signal)
356 jobserver_release (int is_fatal)
359 EINTRLOOP (r, write (job_fds[1], &token, 1));
363 pfatal_with_name (_("write jobserver"));
364 perror_with_name ("write", "");
369 jobserver_acquire_all ()
372 unsigned int tokens = 0;
374 /* Use blocking reads to wait for all outstanding jobs. */
375 set_blocking (job_fds[0], 1);
377 /* Close the write side, so the read() won't hang forever. */
384 EINTRLOOP (r, read (job_fds[0], &intake, 1));
390 DB (DB_JOBS, ("Acquired all %u jobserver tokens.\n", tokens));
397 /* Prepare the jobserver to start a child process. */
399 jobserver_pre_child (int recursive)
401 if (recursive && js_type == js_pipe)
403 fd_inherit (job_fds[0]);
404 fd_inherit (job_fds[1]);
408 /* Reconfigure the jobserver after starting a child process. */
410 jobserver_post_child (int recursive)
412 if (recursive && js_type == js_pipe)
414 fd_noinherit (job_fds[0]);
415 fd_noinherit (job_fds[1]);
430 jobserver_pre_acquire ()
432 /* Make sure we have a dup'd FD. */
433 if (job_rfd < 0 && job_fds[0] >= 0 && make_job_rfd () < 0)
434 pfatal_with_name (_("duping jobs pipe"));
439 /* Use pselect() to atomically wait for both a signal and a file descriptor.
440 It also provides a timeout facility so we don't need to use SIGALRM.
442 This method relies on the fact that SIGCHLD will be blocked everywhere,
443 and only unblocked (atomically) within the pselect() call, so we can
444 never miss a SIGCHLD.
447 jobserver_acquire (int timeout)
449 struct timespec spec;
450 struct timespec *specp = NULL;
453 sigemptyset (&empty);
457 /* Alarm after one second (is this too granular?) */
470 FD_SET (job_fds[0], &readfds);
472 r = pselect (job_fds[0]+1, &readfds, NULL, NULL, specp, &empty);
477 /* SIGCHLD will show up as an EINTR. */
481 /* Someone closed the jobs pipe.
482 That shouldn't happen but if it does we're done. */
483 O (fatal, NILF, _("job server shut down"));
486 pfatal_with_name (_("pselect jobs pipe"));
493 /* The read FD is ready: read it! This is non-blocking. */
494 EINTRLOOP (r, read (job_fds[0], &intake, 1));
498 /* Someone sniped our token! Try again. */
502 pfatal_with_name (_("read jobs pipe"));
505 /* read() should never return 0: only the parent make can reap all the
506 tokens and close the write side...?? */
513 /* This method uses a "traditional" UNIX model for waiting on both a signal
514 and a file descriptor. However, it's complex and since we have a SIGCHLD
515 handler installed we need to check ALL system calls for EINTR: painful!
517 Read a token. As long as there's no token available we'll block. We
518 enable interruptible system calls before the read(2) so that if we get a
519 SIGCHLD while we're waiting, we'll return with EINTR and we can process the
520 death(s) and return tokens to the free pool.
522 Once we return from the read, we immediately reinstate restartable system
523 calls. This allows us to not worry about checking for EINTR on all the
524 other system calls in the program.
526 There is one other twist: there is a span between the time reap_children()
527 does its last check for dead children and the time the read(2) call is
528 entered, below, where if a child dies we won't notice. This is extremely
529 serious as it could cause us to deadlock, given the right set of events.
531 To avoid this, we do the following: before we reap_children(), we dup(2)
532 the read FD on the jobserver pipe. The read(2) call below uses that new
533 FD. In the signal handler, we close that FD. That way, if a child dies
534 during the section mentioned above, the read(2) will be invoked with an
535 invalid FD and will return immediately with EBADF. */
538 job_noop (int sig UNUSED)
542 /* Set the child handler action flags to FLAGS. */
544 set_child_handler_action_flags (int set_handler, int set_alarm)
549 /* The child handler must be turned off here. */
550 signal (SIGCHLD, SIG_DFL);
553 memset (&sa, '\0', sizeof sa);
554 sa.sa_handler = child_handler;
555 sa.sa_flags = set_handler ? 0 : SA_RESTART;
558 if (sigaction (SIGCHLD, &sa, NULL) < 0)
559 pfatal_with_name ("sigaction: SIGCHLD");
562 #if defined SIGCLD && SIGCLD != SIGCHLD
563 if (sigaction (SIGCLD, &sa, NULL) < 0)
564 pfatal_with_name ("sigaction: SIGCLD");
570 /* If we're about to enter the read(), set an alarm to wake up in a
571 second so we can check if the load has dropped and we can start more
572 work. On the way out, turn off the alarm and set SIG_DFL. */
575 sa.sa_handler = job_noop;
577 if (sigaction (SIGALRM, &sa, NULL) < 0)
578 pfatal_with_name ("sigaction: SIGALRM");
584 sa.sa_handler = SIG_DFL;
586 if (sigaction (SIGALRM, &sa, NULL) < 0)
587 pfatal_with_name ("sigaction: SIGALRM");
594 jobserver_acquire (int timeout)
600 /* Set interruptible system calls, and read() for a job token. */
601 set_child_handler_action_flags (1, timeout);
603 EINTRLOOP (got_token, read (job_rfd, &intake, 1));
606 set_child_handler_action_flags (0, timeout);
611 /* If the error _wasn't_ expected (EINTR or EBADF), fatal. Otherwise,
612 go back and reap_children(), and try again. */
615 if (errno != EINTR && errno != EBADF)
616 pfatal_with_name (_("read jobs pipe"));
619 DB (DB_JOBS, ("Read returned EBADF.\n"));
624 #endif /* HAVE_PSELECT */
626 #endif /* MAKE_JOBSERVER */
628 #if !defined(NO_OUTPUT_SYNC)
630 #define MUTEX_PREFIX "fnm:"
632 static int osync_handle = -1;
634 static char *osync_tmpfile = NULL;
636 static unsigned int sync_root = 0;
641 return osync_handle >= 0;
647 osync_handle = get_tmpfd (&osync_tmpfile);
648 fd_noinherit (osync_handle);
657 if (osync_enabled ())
659 /* Prepare the mutex handle string for our children. */
660 mutex = xmalloc (strlen (osync_tmpfile) + CSTRLEN (MUTEX_PREFIX) + 1);
661 sprintf (mutex, MUTEX_PREFIX "%s", osync_tmpfile);
668 osync_parse_mutex (const char *mutex)
670 if (strncmp (mutex, MUTEX_PREFIX, CSTRLEN (MUTEX_PREFIX)) != 0)
672 OS (error, NILF, _("invalid --sync-mutex string '%s'"), mutex);
676 free (osync_tmpfile);
677 osync_tmpfile = xstrdup (mutex + CSTRLEN (MUTEX_PREFIX));
679 EINTRLOOP (osync_handle, open (osync_tmpfile, O_WRONLY));
680 if (osync_handle < 0)
681 OSS (fatal, NILF, _("cannot open output sync mutex %s: %s"),
682 osync_tmpfile, strerror (errno));
684 fd_noinherit (osync_handle);
692 if (osync_handle >= 0)
694 close (osync_handle);
698 if (sync_root && osync_tmpfile)
702 EINTRLOOP (r, unlink (osync_tmpfile));
703 free (osync_tmpfile);
704 osync_tmpfile = NULL;
716 fl.l_whence = SEEK_SET;
719 /* We don't want to keep waiting on EINTR. */
720 if (fcntl (osync_handle, F_SETLKW, &fl) == -1)
738 fl.l_whence = SEEK_SET;
741 /* We don't want to keep waiting on EINTR. */
742 if (fcntl (osync_handle, F_SETLKW, &fl) == -1)
749 /* Create a "bad" file descriptor for stdin when parallel jobs are run. */
753 static int bad_stdin = -1;
755 /* Set up a bad standard input that reads from a broken pipe. */
759 /* Make a file descriptor that is the read end of a broken pipe.
760 This will be used for some children's standard inputs. */
764 /* Close the write side. */
766 /* Save the read side. */
769 /* Set the descriptor to close on exec, so it does not litter any
770 child's descriptor table. When it is dup2'd onto descriptor 0,
771 that descriptor will not close on exec. */
772 fd_noinherit (bad_stdin);
779 /* Set file descriptors to be inherited / not inherited by subprocesses. */
781 #if !defined(F_SETFD) || !defined(F_GETFD)
782 void fd_inherit (int fd) {}
783 void fd_noinherit (int fd) {}
788 # define FD_CLOEXEC 1
795 EINTRLOOP (flags, fcntl (fd, F_GETFD));
799 flags &= ~FD_CLOEXEC;
800 EINTRLOOP (r, fcntl (fd, F_SETFD, flags));
805 fd_noinherit (int fd)
808 EINTRLOOP (flags, fcntl(fd, F_GETFD));
813 EINTRLOOP (r, fcntl(fd, F_SETFD, flags));
818 /* Set a file descriptor referring to a regular file to be in O_APPEND mode.
819 If it fails, just ignore it. */
822 fd_set_append (int fd)
824 #if defined(F_GETFL) && defined(F_SETFL) && defined(O_APPEND)
827 if (fstat (fd, &stbuf) == 0 && S_ISREG (stbuf.st_mode))
829 flags = fcntl (fd, F_GETFL, 0);
833 EINTRLOOP(r, fcntl (fd, F_SETFL, flags | O_APPEND));
839 /* Return a file descriptor for a new anonymous temp file, or -1. */
843 const char *tdir = get_tmpdir ();
847 static unsigned int tmpfile_works = 1;
851 EINTRLOOP (fd, open (tdir, O_RDWR | O_TMPFILE | O_EXCL, 0600));
855 DB (DB_BASIC, (_("Cannot open '%s' with O_TMPFILE: %s.\n"),
856 tdir, strerror (errno)));
862 /* If we can dup and we are creating temp files in the default location then
863 try tmpfile() + dup() + fclose() to avoid ever having a named file. */
864 if (streq (tdir, DEFAULT_TMPDIR))
866 mode_t mask = umask (0077);
868 ENULLLOOP (tfile, tmpfile ());
870 pfatal_with_name ("tmpfile");
873 EINTRLOOP (fd, dup (fileno (tfile)));
875 pfatal_with_name ("dup");