unix: add fork-safe open file function

PR-URL: https://github.com/libuv/libuv/pull/743
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
This commit is contained in:
Kári Tristan Helgason 2016-03-01 22:42:37 +00:00 committed by Saúl Ibarra Corretgé
parent 4972f7411c
commit d7910e42d0
2 changed files with 19 additions and 0 deletions

View File

@ -427,6 +427,22 @@ int uv__socket(int domain, int type, int protocol) {
return sockfd;
}
/* get a file pointer to a file in read-only and close-on-exec mode */
FILE* uv__open_file(const char* path) {
int fd;
FILE* fp;
fd = uv__open_cloexec(path, O_RDONLY);
if (fd == -1)
return NULL;
fp = fdopen(fd, "r");
if (fp == NULL)
uv__close(fd);
return fp;
}
int uv__accept(int sockfd) {
int peerfd;

View File

@ -28,6 +28,7 @@
#include <stdlib.h> /* abort */
#include <string.h> /* strrchr */
#include <fcntl.h> /* O_CLOEXEC, may be */
#include <stdio.h>
#if defined(__STRICT_ANSI__)
# define inline __inline
@ -246,6 +247,8 @@ void uv__timer_close(uv_timer_t* handle);
void uv__udp_close(uv_udp_t* handle);
void uv__udp_finish_close(uv_udp_t* handle);
uv_handle_type uv__handle_type(int fd);
FILE* uv__open_file(const char* path);
#if defined(__APPLE__)
int uv___stream_fd(const uv_stream_t* handle);