Patchwork [08,of,14] chg: extract the logic of setting FD_CLOEXEC to a utility function

login
register
mail settings
Submitter Jun Wu
Date April 10, 2016, 11:57 p.m.
Message ID <95e54f1b7e4f7184f6f1.1460332645@x1c>
Download mbox | patch
Permalink /patch/14506/
State Accepted
Delegated to: Yuya Nishihara
Headers show

Comments

Jun Wu - April 10, 2016, 11:57 p.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1460330237 -3600
#      Mon Apr 11 00:17:17 2016 +0100
# Node ID 95e54f1b7e4f7184f6f1f77945b0ba9ed3cc2efb
# Parent  4616bceaa941b40b39055610ba109252dee3a20c
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.
Yuya Nishihara - April 11, 2016, 1:37 p.m.
On Mon, 11 Apr 2016 00:57:25 +0100, Jun Wu wrote:
> # HG changeset patch
> # User Jun Wu <quark@fb.com>
> # Date 1460330237 -3600
> #      Mon Apr 11 00:17:17 2016 +0100
> # Node ID 95e54f1b7e4f7184f6f1f77945b0ba9ed3cc2efb
> # Parent  4616bceaa941b40b39055610ba109252dee3a20c
> chg: extract the logic of setting FD_CLOEXEC to a utility function

Queued the patch 1 and 8, thanks.

> --- a/contrib/chg/util.c
> +++ b/contrib/chg/util.c
> @@ -92,6 +92,15 @@
>  		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);
> +}

I've added #include <fcntl.h>.

Patch

diff --git a/contrib/chg/hgclient.c b/contrib/chg/hgclient.c
--- a/contrib/chg/hgclient.c
+++ b/contrib/chg/hgclient.c
@@ -418,11 +418,7 @@ 
 
 	/* 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;
diff --git a/contrib/chg/util.c b/contrib/chg/util.c
--- a/contrib/chg/util.c
+++ b/contrib/chg/util.c
@@ -92,6 +92,15 @@ 
 		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);
diff --git a/contrib/chg/util.h b/contrib/chg/util.h
--- a/contrib/chg/util.h
+++ b/contrib/chg/util.h
@@ -26,6 +26,7 @@ 
 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);