From: Ben Pfaff Subject: Re: [PATCH] nextfd(2) Date: Sat, 07 Apr 2012 14:21:24 -0700 Message-ID: <87r4vzjl0r.fsf@blp.benpfaff.org> References: <20120401125741.GA7484@p183.telecom.by> <4F78D0BA.9040709@zytor.com> <4F7F1864.8090606@zytor.com> Reply-To: Ben Pfaff Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: Alexey Dobriyan , akpm@linux-foundation.org, viro@zeniv.linux.org.uk, torvalds@linux-foundation.org, drepper@gmail.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org To: "H. Peter Anvin" Return-path: Received: from outmail148093.authsmtp.net ([62.13.148.93]:24433 "EHLO outmail148093.authsmtp.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751707Ab2DGVvp (ORCPT ); Sat, 7 Apr 2012 17:51:45 -0400 In-Reply-To: <4F7F1864.8090606@zytor.com> (H. Peter Anvin's message of "Fri, 06 Apr 2012 09:23:00 -0700") Sender: linux-fsdevel-owner@vger.kernel.org List-ID: "H. Peter Anvin" writes: > On 04/06/2012 02:54 AM, Alexey Dobriyan wrote: >> >> Without proc knowledge about fdtable is gathered linearly and still unreliable. >> With nextfd(2), even procful environments could lose several failure branches. >> And they can keep old dumb fd++ or smart /proc/self/fd loops for a change. >> > > Incidentally, if we were to create a system call for this -- which I so > far see no reason for -- I would make it return a select-style bitmask > of file descriptors in use, not a "next fd" which would require a system > call per iteration. It's already possible to do something a little like that with the existing "poll" system call: #include #include int main(void) { enum { N_FDS = 1024 }; struct pollfd fds[N_FDS]; int error; int i; for (i = 0; i < N_FDS; i++) { fds[i].fd = i; fds[i].events = 0; } error = poll(fds, N_FDS, 0); if (error < 0) perror("poll"); else { printf("valid fds:"); for (i = 0; i < N_FDS; i++) if (!(fds[i].revents & POLLNVAL)) printf(" %d", fds[i].fd); printf("\n"); } return 0; } blp@blp:~/tmp(0)$ gcc tmp.c -Wall blp@blp:~/tmp(0)$ ./a.out valid fds: 0 1 2 blp@blp:~/tmp(0)$ ./a.out 5>/dev/null valid fds: 0 1 2 5 blp@blp:~/tmp(0)$ ./a.out 5>/dev/null 3