Can someone please explain the reason on the following code (in asterisk.c, function ast_safe_system()): /* Close file descriptors and launch system command */ for (x = STDERR_FILENO + 1; x < 4096; x++) close(x); Why to close so many descriptors? Thanks in advance ?ric
On Saturday 19 July 2008 06:41:04 Eric Dantie wrote:> Can someone please explain the reason on the following code (in > asterisk.c, function ast_safe_system()): > > /* Close file descriptors and launch system command */ > for (x = STDERR_FILENO + 1; x < 4096; x++) > close(x); > > > Why to close so many descriptors?There's no way to know how many file descriptors are actually open, and every file descriptor that is open when a process is forked is duplicated in the new process. Also, if the process being forked is long running (such as an AGI), then there could be side effects to not closing all descriptors in a child process (such as receiving a SIGPIPE when the other end closes). Eventually, we could probably start registering the highest file descriptor to a central function, ensuring that we close all of them. It is admittedly a hack to pick an arbitrary number like this. -- Tilghman
Never noticed the c is next to the n, however thinking about it, the c is to the f what the n is to the j. which might make for an easy mistake. I guess if you put only your right hand on the keyboard, and mistake the f for the j on your right index finger that it could happen easyly. On Sat, Jul 19, 2008 at 7:41 AM, Eric Dantie <edantie at gmail.com> wrote:> Can someone please explain the reason on the following code (in > asterisk.c, function ast_safe_system()): > > /* Close file descriptors and launch system command */ > for (x = STDERR_FILENO + 1; x < 4096; x++) > close(x); > > > Why to close so many descriptors? > > Thanks in advance > ?ric > > _______________________________________________ > -- Bandwidth and Colocation Provided by http://www.api-digital.com -- > > AstriCon 2008 - September 22 - 25 Phoenix, Arizona > Register Now: http://www.astricon.net > > asterisk-users mailing list > To UNSUBSCRIBE or update options visit: > http://lists.digium.com/mailman/listinfo/asterisk-users
Maybe Matching Threads
- [PATCH nbdkit] sh: Remove assert and replace with smarter file descriptor duplication. (was: Re: [nbdkit PATCH v2 14/17] sh: Use pipe2 with CLOEXEC when possible)
- [libnbd PATCH v3 07/29] lib/utils: add async-signal-safe assert()
- Re: [PATCH nbdkit] sh: Remove assert and replace with smarter file descriptor duplication. (was: Re: [nbdkit PATCH v2 14/17] sh: Use pipe2 with CLOEXEC when possible)
- [libnbd PATCH v3 07/29] lib/utils: add async-signal-safe assert()
- [PATCH] kinit minor checkpatch cleanup