mailing list of musl libc
 help / color / mirror / code / Atom feed
From: "Petr Skočík" <pskocik@gmail.com>
To: musl@lists.openwall.com
Subject: tmpfile patch
Date: Thu, 8 Jun 2017 20:01:53 +0200	[thread overview]
Message-ID: <1c4f252e-9c13-9d52-f966-a186216a8396@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 112 bytes --]

Hi,
I made a patch to tmpfile() to make it signal-proof and use
O_TMPFILE if it can.

Best Regards,
Petr Skocik

[-- Attachment #2: tmpfile.patch --]
[-- Type: text/x-patch, Size: 1149 bytes --]

diff --git a/src/stdio/tmpfile.c b/src/stdio/tmpfile.c
index 525090aa..34c3d487 100644
--- a/src/stdio/tmpfile.c
+++ b/src/stdio/tmpfile.c
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <fcntl.h>
 #include "stdio_impl.h"
+#include "pthread_impl.h"
 
 #define MAXTRIES 100
 
@@ -8,10 +9,23 @@ char *__randname(char *);
 
 FILE *tmpfile(void)
 {
+#ifdef O_TMPFILE
+	int fd;
+	FILE *f = 0;
+	fd = sys_open("/tmp", O_RDWR|O_TMPFILE);
+	if (fd >= 0) {
+		f = __fdopen(fd, "w+");
+		if (!f) __syscall(SYS_close, fd);
+	}
+	return f;
+#else
 	char s[] = "/tmp/tmpfile_XXXXXX";
+	FILE *f = 0;
 	int fd;
-	FILE *f;
 	int try;
+	sigset_t saved_mask;
+
+	pthread_sigmask(SIG_SETMASK, SIGALL_SET, &saved_mask);
 	for (try=0; try<MAXTRIES; try++) {
 		__randname(s+13);
 		fd = sys_open(s, O_RDWR|O_CREAT|O_EXCL, 0600);
@@ -21,12 +35,15 @@ FILE *tmpfile(void)
 #else
 			__syscall(SYS_unlinkat, AT_FDCWD, s, 0);
 #endif
+			pthread_sigmask(SIG_SETMASK, &saved_mask, 0);
 			f = __fdopen(fd, "w+");
 			if (!f) __syscall(SYS_close, fd);
 			return f;
 		}
 	}
+	pthread_sigmask(SIG_SETMASK, &saved_mask, 0);
 	return 0;
+#endif /*O_TMPFILE*/
 }
 
 LFS64(tmpfile);

             reply	other threads:[~2017-06-08 18:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-08 18:01 Petr Skočík [this message]
2017-06-08 20:15 ` Szabolcs Nagy
2017-06-09  4:19   ` Markus Wichmann
2017-06-09  0:01 ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1c4f252e-9c13-9d52-f966-a186216a8396@gmail.com \
    --to=pskocik@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).