mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Szabolcs Nagy <nsz@port70.net>
To: musl@lists.openwall.com
Subject: Re: tmpfile patch
Date: Thu, 8 Jun 2017 22:15:55 +0200	[thread overview]
Message-ID: <20170608201555.GR9350@port70.net> (raw)
In-Reply-To: <1c4f252e-9c13-9d52-f966-a186216a8396@gmail.com>

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



  reply	other threads:[~2017-06-08 20:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-08 18:01 Petr Skočík
2017-06-08 20:15 ` Szabolcs Nagy [this message]
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=20170608201555.GR9350@port70.net \
    --to=nsz@port70.net \
    --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).