mailing list of musl libc
 help / color / mirror / code / Atom feed
* possible bug in getdtablesize() ?
@ 2016-10-05 21:56 Assaf Gordon
  2016-10-05 23:00 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Assaf Gordon @ 2016-10-05 21:56 UTC (permalink / raw)
  To: musl

Hello,

It seems that musl's getdtablesize() returns incorrect value,
possibly using the "rlim_max" instead of "rlim_cur" value (just a guess, not sure about the actual reason).

The following code demonstrates: trying to use dup2(2) on a file-descriptor value that "should work".

   #include <unistd.h>
   #include <stdio.h>
   #include <err.h>

   int main(void)
   {
     int fd = STDIN_FILENO;
     int maxfd = getdtablesize();
     printf("maxfd = %d\n", maxfd);

     int newfd = maxfd-1;

     int i = dup2(fd,newfd);
     if (i == -1)
       err(1,"dup2(%d, %d) failed", fd, newfd);

     if (i != newfd)
       errx(1,"dup2(%d, %d) returned %d", fd, newfd, i);

     return 0;
   }


With glibc, it works:

     $ gcc -o dup2-glibc dup2-test.c
     $ strace -e prlimit64,getrlimit,dup2 ./dup2-glibc
     getrlimit(RLIMIT_NOFILE, {rlim_cur=1024, rlim_max=4*1024}) = 0
     maxfd = 1024
     dup2(0, 1023)                           = 1023
     +++ exited with 0 +++

With musl-1.1.15, it failsdue to wrong 'max-fd' value:

     $ musl-gcc -o dup2-musl dup2-test.c
     $ strace -e prlimit64,getrlimit,dup2 ./dup2-musl
     prlimit64(0, RLIMIT_NOFILE, NULL, {rlim_cur=1024, rlim_max=4*1024}) = 0
     maxfd = 4096
     dup2(0, 4095)                           = -1 EBADF (Bad file descriptor)
     dup2-musl: dup2(0, 4095) failed: Bad file descriptor
     +++ exited with 1 +++

Using:

     $ uname -svr
     Linux 3.13.0-88-generic #135-Ubuntu SMP Wed Jun 8 21:10:42 UTC 2016

     $ gcc --version
     gcc (GCC) 5.2.0

     $ ulimit -a
     [...]
     open files                      (-n) 1024

and glibc-2.19.



regards,
  - assaf



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

* Re: possible bug in getdtablesize() ?
  2016-10-05 21:56 possible bug in getdtablesize() ? Assaf Gordon
@ 2016-10-05 23:00 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2016-10-05 23:00 UTC (permalink / raw)
  To: musl

On Wed, Oct 05, 2016 at 05:56:40PM -0400, Assaf Gordon wrote:
> Hello,
> 
> It seems that musl's getdtablesize() returns incorrect value,
> possibly using the "rlim_max" instead of "rlim_cur" value (just a guess, not sure about the actual reason).
> 
> The following code demonstrates: trying to use dup2(2) on a file-descriptor value that "should work".
> 
>   #include <unistd.h>
>   #include <stdio.h>
>   #include <err.h>
> 
>   int main(void)
>   {
>     int fd = STDIN_FILENO;
>     int maxfd = getdtablesize();
>     printf("maxfd = %d\n", maxfd);
> 
>     int newfd = maxfd-1;
> 
>     int i = dup2(fd,newfd);
>     if (i == -1)
>       err(1,"dup2(%d, %d) failed", fd, newfd);
> 
>     if (i != newfd)
>       errx(1,"dup2(%d, %d) returned %d", fd, newfd, i);
> 
>     return 0;
>   }
> 
> 
> With glibc, it works:
> 
>     $ gcc -o dup2-glibc dup2-test.c
>     $ strace -e prlimit64,getrlimit,dup2 ./dup2-glibc
>     getrlimit(RLIMIT_NOFILE, {rlim_cur=1024, rlim_max=4*1024}) = 0
>     maxfd = 1024
>     dup2(0, 1023)                           = 1023
>     +++ exited with 0 +++
> 
> With musl-1.1.15, it failsdue to wrong 'max-fd' value:
> 
>     $ musl-gcc -o dup2-musl dup2-test.c
>     $ strace -e prlimit64,getrlimit,dup2 ./dup2-musl
>     prlimit64(0, RLIMIT_NOFILE, NULL, {rlim_cur=1024, rlim_max=4*1024}) = 0
>     maxfd = 4096
>     dup2(0, 4095)                           = -1 EBADF (Bad file descriptor)
>     dup2-musl: dup2(0, 4095) failed: Bad file descriptor
>     +++ exited with 1 +++
> 
> Using:
> 
>     $ uname -svr
>     Linux 3.13.0-88-generic #135-Ubuntu SMP Wed Jun 8 21:10:42 UTC 2016
> 
>     $ gcc --version
>     gcc (GCC) 5.2.0
> 
>     $ ulimit -a
>     [...]
>     open files                      (-n) 1024
> 
> and glibc-2.19.

This is already fixed in git master, as of:

https://git.musl-libc.org/cgit/musl/commit/?id=397586625e71d166f493f16bfe04f3005ae464c3

I'll try to roll a new release soon with this and other fixes.

Rich


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

end of thread, other threads:[~2016-10-05 23:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-05 21:56 possible bug in getdtablesize() ? Assaf Gordon
2016-10-05 23:00 ` 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).