From c7ddc792b747fdf4fde822df0cf9c7b712be4219 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 16 Jul 2013 00:05:41 -0700 Subject: [PATCH] Fix porting bug to older POSIXish platforms. * sysdep.c (emacs_pipe): New function, that implements pipe2 (fd, O_CLOEXEC) even on hosts that lack O_CLOEXEC. This should port better to CentOS 5 and to Mac OS X 10.6. All calls to pipe2 changed. Fixes: debbugs:14862 --- src/ChangeLog | 6 ++++++ src/alloc.c | 2 +- src/callproc.c | 2 +- src/emacs.c | 2 +- src/lisp.h | 1 + src/nsterm.m | 2 +- src/process.c | 6 +++--- src/sysdep.c | 14 ++++++++++++++ 8 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index bc71a482a5..4d819413b4 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,11 @@ 2013-07-16 Paul Eggert + Fix porting bug to older POSIXish platforms (Bug#14862). + * sysdep.c (emacs_pipe): New function, that implements + pipe2 (fd, O_CLOEXEC) even on hosts that lack O_CLOEXEC. + This should port better to CentOS 5 and to Mac OS X 10.6. + All calls to pipe2 changed. + Prefer list1 (X) to Fcons (X, Qnil) when building lists. This makes the code easier to read and the executable a bit smaller. Do not replace all calls to Fcons that happen to create lists, diff --git a/src/alloc.c b/src/alloc.c index b71cdb98d7..b52e3de049 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -4741,7 +4741,7 @@ valid_pointer_p (void *p) Unfortunately, we cannot use NULL_DEVICE here, as emacs_write may not validate p in that case. */ - if (pipe2 (fd, O_CLOEXEC) == 0) + if (emacs_pipe (fd) == 0) { bool valid = emacs_write (fd[1], (char *) p, 16) == 16; emacs_close (fd[1]); diff --git a/src/callproc.c b/src/callproc.c index 86d8246680..facaac60f2 100644 --- a/src/callproc.c +++ b/src/callproc.c @@ -524,7 +524,7 @@ usage: (call-process PROGRAM &optional INFILE DESTINATION DISPLAY &rest ARGS) * { #ifndef MSDOS int fd[2]; - if (pipe2 (fd, O_CLOEXEC) != 0) + if (emacs_pipe (fd) != 0) { int pipe_errno = errno; emacs_close (filefd); diff --git a/src/emacs.c b/src/emacs.c index 7ad9739530..cf3a3c6893 100644 --- a/src/emacs.c +++ b/src/emacs.c @@ -985,7 +985,7 @@ main (int argc, char **argv) use a pipe for synchronization. The parent waits for the child to close its end of the pipe (using `daemon-initialized') before exiting. */ - if (pipe2 (daemon_pipe, O_CLOEXEC) != 0) + if (emacs_pipe (daemon_pipe) != 0) { fprintf (stderr, "Cannot pipe!\n"); exit (1); diff --git a/src/lisp.h b/src/lisp.h index a54b2e0705..6a551bb499 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -4094,6 +4094,7 @@ extern void init_random (void); extern void emacs_backtrace (int); extern _Noreturn void emacs_abort (void) NO_INLINE; extern int emacs_open (const char *, int, int); +extern int emacs_pipe (int[2]); extern int emacs_close (int); extern ptrdiff_t emacs_read (int, char *, ptrdiff_t); extern ptrdiff_t emacs_write (int, const char *, ptrdiff_t); diff --git a/src/nsterm.m b/src/nsterm.m index 5cbddd2a99..340ef3b00a 100644 --- a/src/nsterm.m +++ b/src/nsterm.m @@ -4142,7 +4142,7 @@ ns_term_init (Lisp_Object display_name) if (selfds[0] == -1) { - if (pipe2 (selfds, O_CLOEXEC) != 0) + if (emacs_pipe (selfds) != 0) { fprintf (stderr, "Failed to create pipe: %s\n", emacs_strerror (errno)); diff --git a/src/process.c b/src/process.c index 94ffc37ffe..125a938934 100644 --- a/src/process.c +++ b/src/process.c @@ -1651,11 +1651,11 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir) else #endif /* HAVE_PTYS */ { - if (pipe2 (sv, O_CLOEXEC) != 0) + if (emacs_pipe (sv) != 0) report_file_error ("Creating pipe", Qnil); inchannel = sv[0]; forkout = sv[1]; - if (pipe2 (sv, O_CLOEXEC) != 0) + if (emacs_pipe (sv) != 0) { int pipe_errno = errno; emacs_close (inchannel); @@ -1667,7 +1667,7 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir) } #ifndef WINDOWSNT - if (pipe2 (wait_child_setup, O_CLOEXEC) != 0) + if (emacs_pipe (wait_child_setup) != 0) report_file_error ("Creating pipe", Qnil); #endif diff --git a/src/sysdep.c b/src/sysdep.c index f614d8bc55..82f490e953 100644 --- a/src/sysdep.c +++ b/src/sysdep.c @@ -2201,6 +2201,20 @@ emacs_fopen (char const *file, char const *mode) return fd < 0 ? 0 : fdopen (fd, mode); } +/* Create a pipe for Emacs use. */ + +int +emacs_pipe (int fd[2]) +{ + int result = pipe2 (fd, O_CLOEXEC); + if (! O_CLOEXEC && result == 0) + { + fcntl (fd[0], F_SETFD, FD_CLOEXEC); + fcntl (fd[1], F_SETFD, FD_CLOEXEC); + } + return result; +} + /* Approximate posix_close and POSIX_CLOSE_RESTART well enough for Emacs. For the background behind this mess, please see Austin Group defect 529 . */ -- 2.20.1