This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: PATCH: linux-generic dup2 behaviour
Linas, you might want to grab the version we have up on our website and
diff it with the sources you have, because this bug was in fact fixed a
while ago (Nov 2010) in our code. The current body of dup2() looks like this:
int
__dup2 (int fd, int fd2)
{
/* For the degenerate case, check if the fd is valid (by trying to
get the file status flags) and return it, or else return EBADF. */
if (fd == fd2)
return __libc_fcntl(fd, F_GETFL, 0) < 0 ? -1 : fd;
return INLINE_SYSCALL(dup3, 3, fd, fd2, 0);
}
As I've mentioned before, our website has:
http://www.tilera.com/scm/glibc-2.11.2.tar.bz2
In addition, I also just uploaded the much smaller (100 KB) bzipped patch
file we are using relative to RHEL 6's libc for our next release:
http://www.tilera.com/scm/glibc-2.12.get.bz2
This is what we would be starting with for a community return -- though not
yet worth going through with a fine-tooth comb, as (in addition to being
behind the git head) there are a couple of known minor bugs I'd like to
fix, and it needs a style review, copyright fixes, and changelog work.
On 9/13/2011 5:43 PM, Linas Vepstas wrote:
> Argh, patch below is bad; here's why:
>
> There is another m4 test case which closes stderr, on purpose,
> then does a dup2(2,2) and expects to get back EBADF
> Should I emulate this kind of behavior in glibc? If not, then
> the question arises: what is the correct way of mapping
> expected dup2() behavior to non-deprecated syscalls?
>
> --linas
>
>
> On 13 September 2011 16:10, Linas Vepstas <linasvepstas@gmail.com> wrote:
>> Chris, here's another:
>>
>> man dup2 states that:
>> If oldfd is a valid file descriptor, and newfd has the same value as
>> oldfd, then dup2() does nothing, and returns newfd.
>>
>> while the linux kernel does this in fs/fcntl.c:
>> if (unlikely(oldfd == newfd)) return -EINVAL;
>>
>> Which I presume is correct for dup3.
>> Thus, dup2 needs fixing. Note that m4 test suite catches this.
>>
>>
>> ---
>> sysdeps/unix/sysv/linux/generic/dup2.c | 2 ++
>> 1 file changed, 2 insertions(+)
>>
>> Index: glibc-2.14/sysdeps/unix/sysv/linux/generic/dup2.c
>> ===================================================================
>> --- glibc-2.14.orig/sysdeps/unix/sysv/linux/generic/dup2.c 2011-09-13
>> 15:39:03.000000000 -0500
>> +++ glibc-2.14/sysdeps/unix/sysv/linux/generic/dup2.c 2011-09-13
>> 15:40:09.000000000 -0500
>> @@ -26,6 +26,8 @@
>> int
>> __dup2 (int fd, int fd2)
>> {
>> + /* if fd's are equal, preserve documented dup2 response */
>> + if (fd == fd2) return fd;
>> return INLINE_SYSCALL(dup3, 3, fd, fd2, 0);
>> }
>> libc_hidden_def (__dup2)
>>
--
Chris Metcalf, Tilera Corp.
http://www.tilera.com