* [PATCH] Cygwin: Have tmpfile(3) use O_TMPFILE @ 2021-02-09 10:50 Mark Geisert 2021-02-09 15:25 ` Corinna Vinschen 0 siblings, 1 reply; 4+ messages in thread From: Mark Geisert @ 2021-02-09 10:50 UTC (permalink / raw) To: cygwin-patches Per discussion on cygwin-developers, a Cygwin tmpfile(3) implementation has been added to syscalls.cc. This overrides the one supplied by newlib. Then the open(2) flag O_TMPFILE was added to the open call that tmpfile internally makes. --- winsup/cygwin/release/3.2.0 | 4 ++++ winsup/cygwin/syscalls.cc | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/winsup/cygwin/release/3.2.0 b/winsup/cygwin/release/3.2.0 index f748a9bc8..d02d16863 100644 --- a/winsup/cygwin/release/3.2.0 +++ b/winsup/cygwin/release/3.2.0 @@ -19,6 +19,10 @@ What changed: - A few FAQ updates. +- Have tmpfile(3) make use of Win32 FILE_ATTRIBUTE_TEMPORARY via open(2) + flag O_TMPFILE. + Addresses: https://cygwin.com/pipermail/cygwin/2021-January/247304.html + Bug Fixes --------- diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc index 52a020f07..b79c1c7cd 100644 --- a/winsup/cygwin/syscalls.cc +++ b/winsup/cygwin/syscalls.cc @@ -5225,3 +5225,23 @@ pipe2 (int filedes[2], int mode) syscall_printf ("%R = pipe2([%d, %d], %y)", res, read, write, mode); return res; } + +extern "C" FILE * +tmpfile (void) +{ + char *dir = getenv ("TMPDIR"); + if (!dir) + dir = P_tmpdir; + int fd = open (dir, O_RDWR | O_CREAT | O_BINARY | O_TMPFILE, + S_IRUSR | S_IWUSR); + if (fd < 0) + return NULL; + FILE *fp = fdopen (fd, "wb+"); + int e = errno; + if (!fp) + close (fd); // ..will remove file + set_errno (e); + return fp; +} + +EXPORT_ALIAS (tmpfile, tmpfile64); -- 2.30.0 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Cygwin: Have tmpfile(3) use O_TMPFILE 2021-02-09 10:50 [PATCH] Cygwin: Have tmpfile(3) use O_TMPFILE Mark Geisert @ 2021-02-09 15:25 ` Corinna Vinschen 2021-02-10 9:07 ` Mark Geisert 0 siblings, 1 reply; 4+ messages in thread From: Corinna Vinschen @ 2021-02-09 15:25 UTC (permalink / raw) To: cygwin-patches Hi Mark, On Feb 9 02:50, Mark Geisert wrote: > Per discussion on cygwin-developers, a Cygwin tmpfile(3) implementation > has been added to syscalls.cc. This overrides the one supplied by > newlib. Then the open(2) flag O_TMPFILE was added to the open call that > tmpfile internally makes. > --- > winsup/cygwin/release/3.2.0 | 4 ++++ > winsup/cygwin/syscalls.cc | 20 ++++++++++++++++++++ > 2 files changed, 24 insertions(+) > > diff --git a/winsup/cygwin/release/3.2.0 b/winsup/cygwin/release/3.2.0 > index f748a9bc8..d02d16863 100644 > --- a/winsup/cygwin/release/3.2.0 > +++ b/winsup/cygwin/release/3.2.0 > @@ -19,6 +19,10 @@ What changed: > > - A few FAQ updates. > > +- Have tmpfile(3) make use of Win32 FILE_ATTRIBUTE_TEMPORARY via open(2) > + flag O_TMPFILE. > + Addresses: https://cygwin.com/pipermail/cygwin/2021-January/247304.html > + > > Bug Fixes > --------- > diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc > index 52a020f07..b79c1c7cd 100644 > --- a/winsup/cygwin/syscalls.cc > +++ b/winsup/cygwin/syscalls.cc > @@ -5225,3 +5225,23 @@ pipe2 (int filedes[2], int mode) > syscall_printf ("%R = pipe2([%d, %d], %y)", res, read, write, mode); > return res; > } > + > +extern "C" FILE * > +tmpfile (void) > +{ > + char *dir = getenv ("TMPDIR"); This isn't what Linux tmpfile does. Per the man page, it tries to create the file in P_tmpdir first, and if that fails, it tries "/tmp". > + if (!dir) > + dir = P_tmpdir; > + int fd = open (dir, O_RDWR | O_CREAT | O_BINARY | O_TMPFILE, You have to specify O_EXCL here. The idea is that this file cannot be made permanent, and missing the O_EXCL flag allows exactly that. See https://man7.org/linux/man-pages/man2/open.2.html, the lengthy description in terms of O_TMPFILE. Thanks, Corinna ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Cygwin: Have tmpfile(3) use O_TMPFILE 2021-02-09 15:25 ` Corinna Vinschen @ 2021-02-10 9:07 ` Mark Geisert 2021-02-10 10:12 ` Corinna Vinschen 0 siblings, 1 reply; 4+ messages in thread From: Mark Geisert @ 2021-02-10 9:07 UTC (permalink / raw) To: cygwin-patches Hi Corinna, Corinna Vinschen via Cygwin-patches wrote: > Hi Mark, > > On Feb 9 02:50, Mark Geisert wrote: >> Per discussion on cygwin-developers, a Cygwin tmpfile(3) implementation >> has been added to syscalls.cc. This overrides the one supplied by >> newlib. Then the open(2) flag O_TMPFILE was added to the open call that >> tmpfile internally makes. >> --- >> winsup/cygwin/release/3.2.0 | 4 ++++ >> winsup/cygwin/syscalls.cc | 20 ++++++++++++++++++++ >> 2 files changed, 24 insertions(+) >> >> diff --git a/winsup/cygwin/release/3.2.0 b/winsup/cygwin/release/3.2.0 >> index f748a9bc8..d02d16863 100644 >> --- a/winsup/cygwin/release/3.2.0 >> +++ b/winsup/cygwin/release/3.2.0 >> @@ -19,6 +19,10 @@ What changed: >> >> - A few FAQ updates. >> >> +- Have tmpfile(3) make use of Win32 FILE_ATTRIBUTE_TEMPORARY via open(2) >> + flag O_TMPFILE. >> + Addresses: https://cygwin.com/pipermail/cygwin/2021-January/247304.html >> + >> >> Bug Fixes >> --------- >> diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc >> index 52a020f07..b79c1c7cd 100644 >> --- a/winsup/cygwin/syscalls.cc >> +++ b/winsup/cygwin/syscalls.cc >> @@ -5225,3 +5225,23 @@ pipe2 (int filedes[2], int mode) >> syscall_printf ("%R = pipe2([%d, %d], %y)", res, read, write, mode); >> return res; >> } >> + >> +extern "C" FILE * >> +tmpfile (void) >> +{ >> + char *dir = getenv ("TMPDIR"); > > This isn't what Linux tmpfile does. Per the man page, it tries to > create the file in P_tmpdir first, and if that fails, it tries > "/tmp". Oops, I was following newlib's code here. I'll adjust this. >> + if (!dir) >> + dir = P_tmpdir; >> + int fd = open (dir, O_RDWR | O_CREAT | O_BINARY | O_TMPFILE, > > You have to specify O_EXCL here. The idea is that this file cannot be > made permanent, and missing the O_EXCL flag allows exactly that. See > https://man7.org/linux/man-pages/man2/open.2.html, the lengthy > description in terms of O_TMPFILE. I started out with O_EXCL as you suggested, but found syscalls.cc:1504 reporting EEXIST. Is there some clash there between fh->exists() and O_TMPFILE? Hmm. ..mark ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Cygwin: Have tmpfile(3) use O_TMPFILE 2021-02-10 9:07 ` Mark Geisert @ 2021-02-10 10:12 ` Corinna Vinschen 0 siblings, 0 replies; 4+ messages in thread From: Corinna Vinschen @ 2021-02-10 10:12 UTC (permalink / raw) To: cygwin-patches On Feb 10 01:07, Mark Geisert wrote: > Hi Corinna, > > Corinna Vinschen via Cygwin-patches wrote: > > Hi Mark, > > > > On Feb 9 02:50, Mark Geisert wrote: > > > Per discussion on cygwin-developers, a Cygwin tmpfile(3) implementation > > > has been added to syscalls.cc. This overrides the one supplied by > > > newlib. Then the open(2) flag O_TMPFILE was added to the open call that > > > tmpfile internally makes. > > > --- > > > winsup/cygwin/release/3.2.0 | 4 ++++ > > > winsup/cygwin/syscalls.cc | 20 ++++++++++++++++++++ > > > 2 files changed, 24 insertions(+) > > > > > > diff --git a/winsup/cygwin/release/3.2.0 b/winsup/cygwin/release/3.2.0 > > > index f748a9bc8..d02d16863 100644 > > > --- a/winsup/cygwin/release/3.2.0 > > > +++ b/winsup/cygwin/release/3.2.0 > > > @@ -19,6 +19,10 @@ What changed: > > > - A few FAQ updates. > > > +- Have tmpfile(3) make use of Win32 FILE_ATTRIBUTE_TEMPORARY via open(2) > > > + flag O_TMPFILE. > > > + Addresses: https://cygwin.com/pipermail/cygwin/2021-January/247304.html > > > + > > > Bug Fixes > > > --------- > > > diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc > > > index 52a020f07..b79c1c7cd 100644 > > > --- a/winsup/cygwin/syscalls.cc > > > +++ b/winsup/cygwin/syscalls.cc > > > @@ -5225,3 +5225,23 @@ pipe2 (int filedes[2], int mode) > > > syscall_printf ("%R = pipe2([%d, %d], %y)", res, read, write, mode); > > > return res; > > > } > > > + > > > +extern "C" FILE * > > > +tmpfile (void) > > > +{ > > > + char *dir = getenv ("TMPDIR"); > > > > This isn't what Linux tmpfile does. Per the man page, it tries to > > create the file in P_tmpdir first, and if that fails, it tries > > "/tmp". > > Oops, I was following newlib's code here. I'll adjust this. Oops, I didn't check with newlib, sorry. Hmm, so we're stuck with either Linux-compat or backward-compat but not both. Decisions, decisions... Let's stick to backward-compat then, so no changes here. > > > + if (!dir) > > > + dir = P_tmpdir; > > > + int fd = open (dir, O_RDWR | O_CREAT | O_BINARY | O_TMPFILE, > > > > You have to specify O_EXCL here. The idea is that this file cannot be > > made permanent, and missing the O_EXCL flag allows exactly that. See > > https://man7.org/linux/man-pages/man2/open.2.html, the lengthy > > description in terms of O_TMPFILE. > > I started out with O_EXCL as you suggested, but found syscalls.cc:1504 > reporting EEXIST. Is there some clash there between fh->exists() and > O_TMPFILE? Hmm. You specify O_CREAT as well. O_TMPFILE replaces O_CREAT. The combination O_CREAT|O_EXCL still checks for existence of the file, in that case, the dir. Thanks, Corinna ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-02-10 10:12 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2021-02-09 10:50 [PATCH] Cygwin: Have tmpfile(3) use O_TMPFILE Mark Geisert 2021-02-09 15:25 ` Corinna Vinschen 2021-02-10 9:07 ` Mark Geisert 2021-02-10 10:12 ` Corinna Vinschen
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 read-only IMAP folder(s) and NNTP newsgroup(s).