Cleanup file descriptors

This commit is contained in:
topjohnwu
2017-05-08 03:11:14 +08:00
parent 58849f28a8
commit ea8cd98361
9 changed files with 51 additions and 22 deletions

View File

@@ -221,6 +221,8 @@ int run_command(int *fd, const char *path, char *const argv[]) {
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) == -1)
return -1;
// We use sv[0], give them sv[1] for communication
if (fcntl(sv[1], F_SETFD, FD_CLOEXEC))
PLOGE("fcntl FD_CLOEXEC");
*fd = sv[1];
}

View File

@@ -23,6 +23,7 @@ extern int quit_signals[];
// xwrap.c
FILE *xfopen(const char *pathname, const char *mode);
FILE *xfdopen(int fd, const char *mode);
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define xopen(...) GET_MACRO(__VA_ARGS__, xopen3, xopen2)(__VA_ARGS__)
int xopen2(const char *pathname, int flags);

View File

@@ -32,6 +32,14 @@ FILE *xfopen(const char *pathname, const char *mode) {
return fp;
}
FILE *xfdopen(int fd, const char *mode) {
FILE *fp = fdopen(fd, mode);
if (fp == NULL) {
PLOGE("fopen");
}
return fp;
}
int xopen2(const char *pathname, int flags) {
int fd = open(pathname, flags);
if (fd < 0) {