summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authorPaul Smith <psmith@gnu.org>2013-06-28 21:57:59 -0400
committerPaul Smith <psmith@gnu.org>2013-06-28 21:57:59 -0400
commit26fe1a1e362602f333750e7a0c7a85293263ec1b (patch)
treefc76410d37ff3b464dd1da10a4ab4b90e1331354 /misc.c
parent2d1e73b8653911082b0f9670ff9460e4eb051b4f (diff)
downloadmake-26fe1a1e362602f333750e7a0c7a85293263ec1b.tar.gz
Set O_APPEND mode for stdout/stderr and output-sync temporary files.
POSIX does not guarantee that writes will be atomic if a file is opened for normal (non-append) output. That means if multiple processes are writing to the same file, output could be lost. I can't think of a real use-case where we would NOT want append for stdout/stderr, so force it if we can.
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/misc.c b/misc.c
index 1b1441b1..d3a1da52 100644
--- a/misc.c
+++ b/misc.c
@@ -22,6 +22,11 @@ this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stdarg.h>
+#ifdef HAVE_FCNTL_H
+# include <fcntl.h>
+#else
+# include <sys/file.h>
+#endif
/* Compare strings *S1 and *S2.
Return negative if the first is less, positive if it is greater,
@@ -897,6 +902,19 @@ get_path_max (void)
#endif
+/* Set a file descriptor to be in O_APPEND mode.
+ If it fails, just ignore it. */
+
+void
+set_append_mode (int fd)
+{
+#if defined(F_GETFL) && defined(F_SETFL) && defined(O_APPEND)
+ int flags = fcntl (fd, F_GETFL, 0);
+ if (flags >= 0)
+ fcntl (fd, F_SETFL, flags | O_APPEND);
+#endif
+}
+
/* Provide support for temporary files. */
#ifndef HAVE_STDLIB_H
@@ -928,6 +946,8 @@ open_tmpfd ()
fclose (tfile);
+ set_append_mode (fd);
+
return fd;
}