unix: open "/dev/null" instead of "/" for emfile_fd

We may not have permission to open "/". This fix allows libuv to be
used in a OS X app that has sandboxing enabled, without the need for
a temporary entitlement to allow reading of "/" (Which would never
pass app review).

In some rare cases "/dev" isn't mounted, so we open "/" as a
fallback.

PR-URL: https://github.com/libuv/libuv/pull/328
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
This commit is contained in:
Alan Rogers 2015-04-21 15:39:02 +10:00 committed by Saúl Ibarra Corretgé
parent 21bcacebba
commit 8fac49d0fc

View File

@ -88,7 +88,12 @@ void uv__stream_init(uv_loop_t* loop,
stream->write_queue_size = 0;
if (loop->emfile_fd == -1) {
err = uv__open_cloexec("/", O_RDONLY);
err = uv__open_cloexec("/dev/null", O_RDONLY);
if (err < 0)
/* In the rare case that "/dev/null" isn't mounted open "/"
* instead.
*/
err = uv__open_cloexec("/", O_RDONLY);
if (err >= 0)
loop->emfile_fd = err;
}