Patchwork [05,of,14] chg: add a dirfd parameter to hgc_open

login
register
mail settings
Submitter Jun Wu
Date April 10, 2016, 11:57 p.m.
Message ID <8dcda8964985a6e1826e.1460332642@x1c>
Download mbox | patch
Permalink /patch/14503/
State Changes Requested
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 1460325563 -3600
#      Sun Apr 10 22:59:23 2016 +0100
# Node ID 8dcda8964985a6e1826e6ceb8e7f476a66b8a3f9
# Parent  f2400efd3bd61aef35de18b462d50611be4d77d1
chg: add a dirfd parameter to hgc_open

This is a part of the series to support long socket path. Since we have
connectat and sockdirfd in cmdserveropts, it's time to add a dirfd parameter
to hgc_open.
Yuya Nishihara - April 11, 2016, 12:58 p.m.
On Mon, 11 Apr 2016 00:57:22 +0100, Jun Wu wrote:
> # HG changeset patch
> # User Jun Wu <quark@fb.com>
> # Date 1460325563 -3600
> #      Sun Apr 10 22:59:23 2016 +0100
> # Node ID 8dcda8964985a6e1826e6ceb8e7f476a66b8a3f9
> # Parent  f2400efd3bd61aef35de18b462d50611be4d77d1
> chg: add a dirfd parameter to hgc_open

> +hgclient_t *hgc_open(const char *sockname, int dirfd);

My two cents, let's name it hgc_openat.

Patch

diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c
--- a/contrib/chg/chg.c
+++ b/contrib/chg/chg.c
@@ -250,7 +250,7 @@ 
 
 	debugmsg("try connect to %s repeatedly", opts->sockname);
 	for (unsigned int i = 0; i < 10 * 100; i++) {
-		hgclient_t *hgc = hgc_open(opts->sockname);
+		hgclient_t *hgc = hgc_open(opts->sockname, opts->sockdirfd);
 		if (hgc)
 			return hgc;
 
@@ -285,12 +285,12 @@ 
 	const char *sockname = opts->redirectsockname[0] ?
 		opts->redirectsockname : opts->sockname;
 	debugmsg("try connect to %s", sockname);
-	hgclient_t *hgc = hgc_open(sockname);
+	hgclient_t *hgc = hgc_open(sockname, opts->sockdirfd);
 	if (hgc)
 		return hgc;
 
 	lockcmdserver(opts);
-	hgc = hgc_open(sockname);
+	hgc = hgc_open(sockname, opts->sockdirfd);
 	if (hgc) {
 		unlockcmdserver(opts);
 		debugmsg("cmdserver is started by another process");
diff --git a/contrib/chg/hgclient.c b/contrib/chg/hgclient.c
--- a/contrib/chg/hgclient.c
+++ b/contrib/chg/hgclient.c
@@ -410,7 +410,7 @@ 
  *
  * If no background server running, returns NULL.
  */
-hgclient_t *hgc_open(const char *sockname)
+hgclient_t *hgc_open(const char *sockname, int dirfd)
 {
 	int fd = socket(AF_UNIX, SOCK_STREAM, 0);
 	if (fd < 0)
@@ -429,7 +429,7 @@ 
 	strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
 	addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
 
-	int r = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
+	int r = connectat(dirfd, fd, (struct sockaddr *)&addr, sizeof(addr));
 	if (r < 0) {
 		close(fd);
 		if (errno == ENOENT || errno == ECONNREFUSED)
diff --git a/contrib/chg/hgclient.h b/contrib/chg/hgclient.h
--- a/contrib/chg/hgclient.h
+++ b/contrib/chg/hgclient.h
@@ -15,7 +15,7 @@ 
 struct hgclient_tag_;
 typedef struct hgclient_tag_ hgclient_t;
 
-hgclient_t *hgc_open(const char *sockname);
+hgclient_t *hgc_open(const char *sockname, int dirfd);
 void hgc_close(hgclient_t *hgc);
 
 pid_t hgc_peerpid(const hgclient_t *hgc);