mailing list of musl libc
 help / color / mirror / code / Atom feed
* tmpfile patch
@ 2017-06-08 18:01 Petr Skočík
  2017-06-08 20:15 ` Szabolcs Nagy
  2017-06-09  0:01 ` Rich Felker
  0 siblings, 2 replies; 4+ messages in thread
From: Petr Skočík @ 2017-06-08 18:01 UTC (permalink / raw)
  To: musl

[-- 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);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: tmpfile patch
  2017-06-08 18:01 tmpfile patch Petr Skočík
@ 2017-06-08 20:15 ` Szabolcs Nagy
  2017-06-09  4:19   ` Markus Wichmann
  2017-06-09  0:01 ` Rich Felker
  1 sibling, 1 reply; 4+ messages in thread
From: Szabolcs Nagy @ 2017-06-08 20:15 UTC (permalink / raw)
  To: musl

* Petr Skočík <pskocik@gmail.com> [2017-06-08 20:01:53 +0200]:
> Hi,
> I made a patch to tmpfile() to make it signal-proof and use
> O_TMPFILE if it can.
> 
> Best Regards,
> Petr Skocik

> 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

i don't think this ifdef helps,
kernel support for O_TMPFILE can
only be detected at runtime,
you will get ENOSYS on an old kernel.

> +	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);



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: tmpfile patch
  2017-06-08 18:01 tmpfile patch Petr Skočík
  2017-06-08 20:15 ` Szabolcs Nagy
@ 2017-06-09  0:01 ` Rich Felker
  1 sibling, 0 replies; 4+ messages in thread
From: Rich Felker @ 2017-06-09  0:01 UTC (permalink / raw)
  To: musl

On Thu, Jun 08, 2017 at 08:01:53PM +0200, Petr Skočík wrote:
> Hi,
> I made a patch to tmpfile() to make it signal-proof and use
> O_TMPFILE if it can.

Can you clarify what you mean by "signal-proof"?

Rich


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: tmpfile patch
  2017-06-08 20:15 ` Szabolcs Nagy
@ 2017-06-09  4:19   ` Markus Wichmann
  0 siblings, 0 replies; 4+ messages in thread
From: Markus Wichmann @ 2017-06-09  4:19 UTC (permalink / raw)
  To: musl

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

Hi,

I reformatted the OP's patch. Now it uses O_TMPFILE if available, and if
that fails for any reason it falls back to the current code. Enjoy.

Note: This is entirely untested. I don't see any obvious bugs, however.

Ciao,
Markus

[-- Attachment #2: tmp.patch --]
[-- Type: text/x-diff, Size: 1332 bytes --]

diff --git a/src/stdio/tmpfile.c b/src/stdio/tmpfile.c
index 525090aa..ccfd0128 100644
--- a/src/stdio/tmpfile.c
+++ b/src/stdio/tmpfile.c
@@ -8,25 +8,33 @@ char *__randname(char *);
 
 FILE *tmpfile(void)
 {
-	char s[] = "/tmp/tmpfile_XXXXXX";
 	int fd;
-	FILE *f;
-	int try;
-	for (try=0; try<MAXTRIES; try++) {
-		__randname(s+13);
-		fd = sys_open(s, O_RDWR|O_CREAT|O_EXCL, 0600);
-		if (fd >= 0) {
+        FILE *f = 0;
+#ifdef O_TMPFILE
+        fd = sys_open("/tmp", O_RDWR|O_TMPFILE);
+        if (fd == -1)
+#endif
+        {
+            char s[] = "/tmp/tmpfile_XXXXXX";
+            int try;
+            fd = -1;
+            for (try=0; try<MAXTRIES && fd < 0; try++) {
+                __randname(s+13);
+                fd = sys_open(s, O_RDWR|O_CREAT|O_EXCL, 0600);
+                if (fd >= 0) {
 #ifdef SYS_unlink
-			__syscall(SYS_unlink, s);
+                    __syscall(SYS_unlink, s);
 #else
-			__syscall(SYS_unlinkat, AT_FDCWD, s, 0);
+                    __syscall(SYS_unlinkat, AT_FDCWD, s, 0);
 #endif
-			f = __fdopen(fd, "w+");
-			if (!f) __syscall(SYS_close, fd);
-			return f;
-		}
-	}
-	return 0;
+                }
+            }
+        }
+        if (fd >= 0) {
+            f = __fdopen(fd, "w+");
+            if (!f) __syscall(SYS_close, fd);
+        }
+	return f;
 }
 
 LFS64(tmpfile);

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-06-09  4:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-08 18:01 tmpfile patch Petr Skočík
2017-06-08 20:15 ` Szabolcs Nagy
2017-06-09  4:19   ` Markus Wichmann
2017-06-09  0:01 ` Rich Felker

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).