From: Florian Weimer <fweimer@bfk.de>
To: Eric Blake <ebb9@byu.net>
Cc: Davide Libenzi <davidel@xmailserver.org>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
bug-coreutils@gnu.org, bug-gnulib@gnu.org,
Ulrich Drepper <drepper@redhat.com>, Ingo Molnar <mingo@elte.hu>
Subject: Re: [PATCH] open: introduce O_NOSTD
Date: Thu, 27 Aug 2009 14:35:47 +0000 [thread overview]
Message-ID: <82k50puxx8.fsf@mid.bfk.de> (raw)
In-Reply-To: <4A968FF8.8050109@byu.net> (Eric Blake's message of "Thu\, 27 Aug 2009 07\:54\:00 -0600")
* Eric Blake:
> int open_safer (const char *name, int flags, int mode)
> {
> int fd = open (name, flags | O_CLOEXEC, mode);
> if (0 <= fd && fd <= 2)
> {
> int dup = fcntl (fd, ((flags & O_CLOEXEC)
> ? F_DUPFD_CLOEXEC : F_DUPFD), 3);
> int saved_errno = errno;
> close (fd);
> errno = saved_errno;
> fd = dup;
> }
> else if (!(flags & O_CLOEXEC))
> {
> if ((flags = fcntl (fd, F_GETFD)) < 0
> || fcntl (fd, F_SETFD, flags & ~FD_CLOEXEC) == -1)
> {
> int saved_errno = errno;
> close (fd);
> fd = -1;
> errno = saved_errno;
> }
> }
> return fd;
> }
> This solves the fd leak,
It's still buggy. You need something like this:
int open_safer(const char *name, int flags, int mode)
{
int opened_fd[3] = {0, 0, 0};
int fd, i, errno_saved;
while (1) {
fd = open(name, flags | O_CLOEXEC, mode);
if (fd < 0 || fd > 2) {
break;
}
opened_fd[fd] = 1;
}
for (int i = 0; i <= 2; ++i) {
if (opened_fd[i]) {
errno_saved = errno;
close(i);
errno = errno_saved;
}
}
return fd;
}
It's untested, so it's probably still buggy.
(O_CLOEXEC should have been a thread attribute, like the base path in
the *_at functions. *sigh*)
--
Florian Weimer <fweimer@bfk.de>
BFK edv-consulting GmbH http://www.bfk.de/
Kriegsstraße 100 tel: +49-721-96201-1
D-76133 Karlsruhe fax: +49-721-96201-99
next prev parent reply other threads:[~2009-08-27 14:35 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-24 12:22 [RFC] new open flag O_NOSTD Eric Blake
2009-08-25 12:16 ` [PATCH] open: introduce O_NOSTD Eric Blake
2009-08-25 21:53 ` Davide Libenzi
2009-08-27 13:54 ` Eric Blake
2009-08-27 14:22 ` Ulrich Drepper
2009-08-28 12:28 ` Eric Blake
2009-08-27 14:35 ` Florian Weimer [this message]
2009-08-28 12:45 ` Eric Blake
2009-08-28 12:52 ` Florian Weimer
2009-08-28 12:58 ` Eric Blake
2009-08-28 13:04 ` Eric Blake
2009-08-27 22:55 ` Davide Libenzi
2009-08-27 23:11 ` Ulrich Drepper
2009-08-30 11:12 ` [RFC] new open flag O_NOSTD James Youngman
2009-08-30 11:18 ` Jim Meyering
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=82k50puxx8.fsf@mid.bfk.de \
--to=fweimer@bfk.de \
--cc=bug-coreutils@gnu.org \
--cc=bug-gnulib@gnu.org \
--cc=davidel@xmailserver.org \
--cc=drepper@redhat.com \
--cc=ebb9@byu.net \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
/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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox