[PATCH 4 of 5] chg: let procutil maintain its own pagerpid

Jun Wu quark at fb.com
Mon Jan 2 10:09:26 EST 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1483368217 0
#      Mon Jan 02 14:43:37 2017 +0000
# Node ID 73378fbef0fe54376ecfcb0009f0b7924d3a245b
# Parent  b2a4d87a091c271868720be8cc51bb2b5988d1c2
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 73378fbef0fe
chg: let procutil maintain its own pagerpid

Previously, chg.c maintains the pagerpid. Let's move it to procutil.c.

Note: chg.c still have a pagerpid to decide whether to call attachio or not.
In the future, attachio may be moved from hgc_open to hgc_runcommand, and
hgc_runcommand handles both pager and attachio so we don't need to run
attachio twice. And chg.c will be free of pagerpid.

diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c
--- a/contrib/chg/chg.c
+++ b/contrib/chg/chg.c
@@ -432,5 +432,5 @@ int main(int argc, const char *argv[], c
 	setupsignalhandler(hgc_peerpid(hgc), hgc_peerpgid(hgc));
 	const char *pagercmd = hgc_getpager(hgc, argv + 1, argc - 1);
-	pagerpid = setuppager(pagercmd);
+	pid_t pagerpid = setuppager(pagercmd);
 	if (pagerpid)
 		hgc_attachio(hgc);  /* reattach to pager */
@@ -439,6 +439,5 @@ int main(int argc, const char *argv[], c
 	hgc_close(hgc);
 	freecmdserveropts(&opts);
-	if (pagerpid)
-		waitpager(pagerpid);
+	waitpager();
 
 	return exitcode;
diff --git a/contrib/chg/procutil.c b/contrib/chg/procutil.c
--- a/contrib/chg/procutil.c
+++ b/contrib/chg/procutil.c
@@ -160,4 +160,5 @@ error:
 static pid_t setuppager(const char *pagercmd)
 {
+	assert(pagerpid == 0);
 	if (!pagercmd)
 		return 0;
@@ -178,4 +179,5 @@ static pid_t setuppager(const char *page
 		}
 		close(pipefds[1]);
+		pagerpid = pid;
 		return pid;
 	} else {
@@ -198,11 +200,14 @@ error:
 }
 
-static void waitpager(pid_t pid)
+static void waitpager(void)
 {
+	if (pagerpid == 0)
+		return;
+
 	/* close output streams to notify the pager its input ends */
 	fclose(stdout);
 	fclose(stderr);
 	while (1) {
-		pid_t ret = waitpid(pid, NULL, 0);
+		pid_t ret = waitpid(pagerpid, NULL, 0);
 		if (ret == -1 && errno == EINTR)
 			continue;


More information about the Mercurial-devel mailing list