mirror of
https://github.com/facebook/sapling.git
synced 2024-10-07 07:17:55 +03:00
chg: extract the logic of setting FD_CLOEXEC to a utility function
Setting FD_CLOEXEC is useful for other fds such like lockfd and sockdirfd, move the logic from hgc_open to util.
This commit is contained in:
parent
b8937b0692
commit
f8e2eeaa6b
@ -418,11 +418,7 @@ hgclient_t *hgc_open(const char *sockname)
|
||||
|
||||
/* don't keep fd on fork(), so that it can be closed when the parent
|
||||
* process get terminated. */
|
||||
int flags = fcntl(fd, F_GETFD);
|
||||
if (flags < 0)
|
||||
abortmsgerrno("cannot get flags of socket");
|
||||
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
|
||||
abortmsgerrno("cannot set flags of socket");
|
||||
fsetcloexec(fd);
|
||||
|
||||
struct sockaddr_un addr;
|
||||
addr.sun_family = AF_UNIX;
|
||||
|
@ -8,6 +8,7 @@
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
@ -91,6 +92,15 @@ void fchdirx(int dirfd)
|
||||
abortmsgerrno("failed to fchdir");
|
||||
}
|
||||
|
||||
void fsetcloexec(int fd)
|
||||
{
|
||||
int flags = fcntl(fd, F_GETFD);
|
||||
if (flags < 0)
|
||||
abortmsgerrno("cannot get flags of fd %d", fd);
|
||||
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
|
||||
abortmsgerrno("cannot set flags of fd %d", fd);
|
||||
}
|
||||
|
||||
void *mallocx(size_t size)
|
||||
{
|
||||
void *result = malloc(size);
|
||||
|
@ -24,6 +24,7 @@ void enabledebugmsg(void);
|
||||
void debugmsg(const char *fmt, ...) PRINTF_FORMAT_;
|
||||
|
||||
void fchdirx(int dirfd);
|
||||
void fsetcloexec(int fd);
|
||||
void *mallocx(size_t size);
|
||||
void *reallocx(void *ptr, size_t size);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user