chg: let procutil maintain its own pagerpid
authorJun Wu <quark@fb.com>
Mon, 02 Jan 2017 14:43:37 +0000
changeset 30692 23ddd43ba866
parent 30691 7adb60660496
child 30693 baee0f47b533
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.
contrib/chg/chg.c
contrib/chg/procutil.c
--- a/contrib/chg/chg.c	Mon Jan 02 14:10:32 2017 +0000
+++ b/contrib/chg/chg.c	Mon Jan 02 14:43:37 2017 +0000
@@ -431,15 +431,14 @@
 
 	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 */
 	int exitcode = hgc_runcommand(hgc, argv + 1, argc - 1);
 	restoresignalhandler();
 	hgc_close(hgc);
 	freecmdserveropts(&opts);
-	if (pagerpid)
-		waitpager(pagerpid);
+	waitpager();
 
 	return exitcode;
 }
--- a/contrib/chg/procutil.c	Mon Jan 02 14:10:32 2017 +0000
+++ b/contrib/chg/procutil.c	Mon Jan 02 14:43:37 2017 +0000
@@ -159,6 +159,7 @@
  * Return 0 if pager is not started, or pid of the pager */
 static pid_t setuppager(const char *pagercmd)
 {
+	assert(pagerpid == 0);
 	if (!pagercmd)
 		return 0;
 
@@ -177,6 +178,7 @@
 				goto error;
 		}
 		close(pipefds[1]);
+		pagerpid = pid;
 		return pid;
 	} else {
 		dup2(pipefds[0], fileno(stdin));
@@ -197,13 +199,16 @@
 	return 0;
 }
 
-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;
 		break;