From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/10585 Path: news.gmane.org!.POSTED!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: possible bug in getdtablesize() ? Date: Wed, 5 Oct 2016 19:00:50 -0400 Message-ID: <20161005230050.GL19318@brightrain.aerifal.cx> References: <261b4d35-9075-8b1f-ccb3-fd5654200ae4@gmail.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1475708472 16508 195.159.176.226 (5 Oct 2016 23:01:12 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 5 Oct 2016 23:01:12 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-10598-gllmg-musl=m.gmane.org@lists.openwall.com Thu Oct 06 01:01:08 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1brvBP-0003Et-IZ for gllmg-musl@m.gmane.org; Thu, 06 Oct 2016 01:01:03 +0200 Original-Received: (qmail 7186 invoked by uid 550); 5 Oct 2016 23:01:04 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 6141 invoked from network); 5 Oct 2016 23:01:03 -0000 Content-Disposition: inline In-Reply-To: <261b4d35-9075-8b1f-ccb3-fd5654200ae4@gmail.com> Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:10585 Archived-At: 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 > #include > #include > > 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