All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/14] fork/exec removal series
@ 2007-10-13 20:06 Johannes Sixt
  2007-10-13 20:06 ` [PATCH 01/14] Change git_connect() to return a struct child_process instead of a pid_t Johannes Sixt
  2007-10-14  2:11 ` [PATCH 0/14] fork/exec removal series Shawn O. Pearce
  0 siblings, 2 replies; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

Junio,

here is a series of patches that removes a number fork/exec pairs.

The series consists of 2 parts:

- The first half replaces a number of fork/exec pairs by start_command/
  finish_command or run_command.

- The second half introduces a new framework that runs a function
  asynchronously. New functions start_async and finish_async are implemented
  similarly to start_command and run_command. They are used to replace
  occurrences of fork() that does not exec() in the child. Such code
  could in principle be run in a thread, and on MinGW port we will go this
  route, but on Posix we stay with fork().

The series can be applied on top of 2b5a06e (Restore default verbosity for
http fetches), as you reqested, but that commit does not compile, so I
developed it on 90446a00 (bundle transport: fix an alloc_ref() call),
which is a few commits earlier.

There will be some strbuf related merge conflicts once you merge this into
master.

 builtin-archive.c     |    8 +-
 builtin-fetch-pack.c  |  101 +++++++++----------------
 cache.h               |    4 +-
 connect.c             |  131 ++++++++++++++++-----------------
 convert.c             |   87 ++++++++--------------
 diff.c                |   38 +---------
 peek-remote.c         |    8 +-
 run-command.c         |   79 +++++++++++++++++---
 run-command.h         |   24 ++++++
 send-pack.c           |    8 +-
 t/t0021-conversion.sh |    7 ++-
 transport.c           |    9 +--
 upload-pack.c         |  199 ++++++++++++++++++++++---------------------------
 13 files changed, 334 insertions(+), 369 deletions(-)

-- Hannes

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 01/14] Change git_connect() to return a struct child_process instead of a pid_t.
  2007-10-13 20:06 [PATCH 0/14] fork/exec removal series Johannes Sixt
@ 2007-10-13 20:06 ` Johannes Sixt
  2007-10-13 20:06   ` [PATCH 02/14] Use start_command() in git_connect() instead of explicit fork/exec Johannes Sixt
  2007-10-14  0:57   ` [PATCH 01/14] Change git_connect() to return a struct child_process instead of a pid_t Johannes Schindelin
  2007-10-14  2:11 ` [PATCH 0/14] fork/exec removal series Shawn O. Pearce
  1 sibling, 2 replies; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

This prepares the API of git_connect() and finish_connect() to operate on
a struct child_process. Currently, we just use that object as a placeholder
for the pid that we used to return. A follow-up patch will change the
implementation of git_connect() and finish_connect() to make full use
of the object.

Old code had early-return-on-error checks at the calling sites of
git_connect(), but since git_connect() dies on errors anyway, these checks
were removed.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 builtin-archive.c    |    8 +++-----
 builtin-fetch-pack.c |    8 +++-----
 cache.h              |    4 ++--
 connect.c            |   31 +++++++++++++++++--------------
 peek-remote.c        |    8 +++-----
 send-pack.c          |    8 +++-----
 transport.c          |    9 ++-------
 7 files changed, 33 insertions(+), 43 deletions(-)

diff --git a/builtin-archive.c b/builtin-archive.c
index a90c65c..346444b 100644
--- a/builtin-archive.c
+++ b/builtin-archive.c
@@ -30,7 +30,7 @@ static int run_remote_archiver(const char *remote, int argc,
 {
 	char *url, buf[LARGE_PACKET_MAX];
 	int fd[2], i, len, rv;
-	pid_t pid;
+	struct child_process *conn;
 	const char *exec = "git-upload-archive";
 	int exec_at = 0;
 
@@ -46,9 +46,7 @@ static int run_remote_archiver(const char *remote, int argc,
 	}
 
 	url = xstrdup(remote);
-	pid = git_connect(fd, url, exec, 0);
-	if (pid < 0)
-		return pid;
+	conn = git_connect(fd, url, exec, 0);
 
 	for (i = 1; i < argc; i++) {
 		if (i == exec_at)
@@ -76,7 +74,7 @@ static int run_remote_archiver(const char *remote, int argc,
 	rv = recv_sideband("archive", fd[0], 1, 2);
 	close(fd[0]);
 	close(fd[1]);
-	rv |= finish_connect(pid);
+	rv |= finish_connect(conn);
 
 	return !!rv;
 }
diff --git a/builtin-fetch-pack.c b/builtin-fetch-pack.c
index 8f25d50..f56592f 100644
--- a/builtin-fetch-pack.c
+++ b/builtin-fetch-pack.c
@@ -762,7 +762,7 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
 {
 	int i, ret;
 	int fd[2];
-	pid_t pid;
+	struct child_process *conn;
 	struct ref *ref;
 	struct stat st;
 
@@ -773,16 +773,14 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
 			st.st_mtime = 0;
 	}
 
-	pid = git_connect(fd, (char *)dest, uploadpack,
+	conn = git_connect(fd, (char *)dest, uploadpack,
                           args.verbose ? CONNECT_VERBOSE : 0);
-	if (pid < 0)
-		return NULL;
 	if (heads && nr_heads)
 		nr_heads = remove_duplicates(nr_heads, heads);
 	ref = do_fetch_pack(fd, nr_heads, heads, pack_lockfile);
 	close(fd[0]);
 	close(fd[1]);
-	ret = finish_connect(pid);
+	ret = finish_connect(conn);
 
 	if (!ret && nr_heads) {
 		/* If the heads to pull were given, we should have
diff --git a/cache.h b/cache.h
index 16bdbb2..c428f87 100644
--- a/cache.h
+++ b/cache.h
@@ -502,8 +502,8 @@ struct ref {
 #define REF_TAGS	(1u << 2)
 
 #define CONNECT_VERBOSE       (1u << 0)
-extern pid_t git_connect(int fd[2], char *url, const char *prog, int flags);
-extern int finish_connect(pid_t pid);
+extern struct child_process *git_connect(int fd[2], char *url, const char *prog, int flags);
+extern int finish_connect(struct child_process *conn);
 extern int path_match(const char *path, int nr, char **match);
 extern int get_ack(int fd, unsigned char *result_sha1);
 extern struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match, unsigned int flags);
diff --git a/connect.c b/connect.c
index aee78ff..b156f92 100644
--- a/connect.c
+++ b/connect.c
@@ -470,21 +470,22 @@ char *get_port(char *host)
 }
 
 /*
- * This returns 0 if the transport protocol does not need fork(2),
- * or a process id if it does.  Once done, finish the connection
+ * This returns NULL if the transport protocol does not need fork(2), or a
+ * struct child_process object if it does.  Once done, finish the connection
  * with finish_connect() with the value returned from this function
- * (it is safe to call finish_connect() with 0 to support the former
+ * (it is safe to call finish_connect() with NULL to support the former
  * case).
  *
- * Does not return a negative value on error; it just dies.
+ * If it returns, the connect is successful; it just dies on errors.
  */
-pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
+struct child_process *git_connect(int fd[2], char *url,
+				  const char *prog, int flags)
 {
 	char *host, *path = url;
 	char *end;
 	int c;
 	int pipefd[2][2];
-	pid_t pid;
+	struct child_process *conn;
 	enum protocol protocol = PROTO_LOCAL;
 	int free_path = 0;
 	char *port = NULL;
@@ -570,15 +571,16 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
 		free(target_host);
 		if (free_path)
 			free(path);
-		return 0;
+		return NULL;
 	}
 
 	if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
 		die("unable to create pipe pair for communication");
-	pid = fork();
-	if (pid < 0)
+	conn = xcalloc(1, sizeof(*conn));
+	conn->pid = fork();
+	if (conn->pid < 0)
 		die("unable to fork");
-	if (!pid) {
+	if (!conn->pid) {
 		char command[MAX_CMD_LEN];
 		char *posn = command;
 		int size = MAX_CMD_LEN;
@@ -630,17 +632,18 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
 	close(pipefd[1][0]);
 	if (free_path)
 		free(path);
-	return pid;
+	return conn;
 }
 
-int finish_connect(pid_t pid)
+int finish_connect(struct child_process *conn)
 {
-	if (pid == 0)
+	if (conn == NULL)
 		return 0;
 
-	while (waitpid(pid, NULL, 0) < 0) {
+	while (waitpid(conn->pid, NULL, 0) < 0) {
 		if (errno != EINTR)
 			return -1;
 	}
+	free(conn);
 	return 0;
 }
diff --git a/peek-remote.c b/peek-remote.c
index ceb7871..8d20f7c 100644
--- a/peek-remote.c
+++ b/peek-remote.c
@@ -25,7 +25,7 @@ int main(int argc, char **argv)
 	int i, ret;
 	char *dest = NULL;
 	int fd[2];
-	pid_t pid;
+	struct child_process *conn;
 	int nongit = 0;
 	unsigned flags = 0;
 
@@ -64,12 +64,10 @@ int main(int argc, char **argv)
 	if (!dest || i != argc - 1)
 		usage(peek_remote_usage);
 
-	pid = git_connect(fd, dest, uploadpack, 0);
-	if (pid < 0)
-		return 1;
+	conn = git_connect(fd, dest, uploadpack, 0);
 	ret = peek_remote(fd, flags);
 	close(fd[0]);
 	close(fd[1]);
-	ret |= finish_connect(pid);
+	ret |= finish_connect(conn);
 	return !!ret;
 }
diff --git a/send-pack.c b/send-pack.c
index 4533d1b..b562953 100644
--- a/send-pack.c
+++ b/send-pack.c
@@ -362,7 +362,7 @@ int main(int argc, char **argv)
 	char *dest = NULL;
 	char **heads = NULL;
 	int fd[2], ret;
-	pid_t pid;
+	struct child_process *conn;
 	char *remote_name = NULL;
 	struct remote *remote = NULL;
 
@@ -426,12 +426,10 @@ int main(int argc, char **argv)
 		}
 	}
 
-	pid = git_connect(fd, dest, receivepack, verbose ? CONNECT_VERBOSE : 0);
-	if (pid < 0)
-		return 1;
+	conn = git_connect(fd, dest, receivepack, verbose ? CONNECT_VERBOSE : 0);
 	ret = send_pack(fd[0], fd[1], remote, nr_heads, heads);
 	close(fd[0]);
 	close(fd[1]);
-	ret |= finish_connect(pid);
+	ret |= finish_connect(conn);
 	return !!ret;
 }
diff --git a/transport.c b/transport.c
index 3475cca..5d723b0 100644
--- a/transport.c
+++ b/transport.c
@@ -279,18 +279,13 @@ static struct ref *get_refs_via_connect(const struct transport *transport)
 	struct git_transport_data *data = transport->data;
 	struct ref *refs;
 	int fd[2];
-	pid_t pid;
 	char *dest = xstrdup(transport->url);
-
-	pid = git_connect(fd, dest, data->uploadpack, 0);
-
-	if (pid < 0)
-		die("Failed to connect to \"%s\"", transport->url);
+	struct child_process *conn = git_connect(fd, dest, data->uploadpack, 0);
 
 	get_remote_heads(fd[0], &refs, 0, NULL, 0);
 	packet_flush(fd[1]);
 
-	finish_connect(pid);
+	finish_connect(conn);
 
 	free(dest);
 
-- 
1.5.3.3.1134.gee562

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 02/14] Use start_command() in git_connect() instead of explicit fork/exec.
  2007-10-13 20:06 ` [PATCH 01/14] Change git_connect() to return a struct child_process instead of a pid_t Johannes Sixt
@ 2007-10-13 20:06   ` Johannes Sixt
  2007-10-13 20:06     ` [PATCH 03/14] Use start_command() to run content filters " Johannes Sixt
  2007-10-14  0:57   ` [PATCH 01/14] Change git_connect() to return a struct child_process instead of a pid_t Johannes Schindelin
  1 sibling, 1 reply; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

The child process handling is delegated to start_command() and
finish_command().

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 connect.c |  108 ++++++++++++++++++++++++++++--------------------------------
 1 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/connect.c b/connect.c
index b156f92..21597bf 100644
--- a/connect.c
+++ b/connect.c
@@ -484,11 +484,15 @@ struct child_process *git_connect(int fd[2], char *url,
 	char *host, *path = url;
 	char *end;
 	int c;
-	int pipefd[2][2];
 	struct child_process *conn;
 	enum protocol protocol = PROTO_LOCAL;
 	int free_path = 0;
 	char *port = NULL;
+	const char **arg;
+	char command[MAX_CMD_LEN];
+	char *posn = command;
+	int size = MAX_CMD_LEN;
+	int of = 0;
 
 	/* Without this we cannot rely on waitpid() to tell
 	 * what happened to our children.
@@ -574,62 +578,51 @@ struct child_process *git_connect(int fd[2], char *url,
 		return NULL;
 	}
 
-	if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
-		die("unable to create pipe pair for communication");
 	conn = xcalloc(1, sizeof(*conn));
-	conn->pid = fork();
-	if (conn->pid < 0)
-		die("unable to fork");
-	if (!conn->pid) {
-		char command[MAX_CMD_LEN];
-		char *posn = command;
-		int size = MAX_CMD_LEN;
-		int of = 0;
-
-		of |= add_to_string(&posn, &size, prog, 0);
-		of |= add_to_string(&posn, &size, " ", 0);
-		of |= add_to_string(&posn, &size, path, 1);
-
-		if (of)
-			die("command line too long");
-
-		dup2(pipefd[1][0], 0);
-		dup2(pipefd[0][1], 1);
-		close(pipefd[0][0]);
-		close(pipefd[0][1]);
-		close(pipefd[1][0]);
-		close(pipefd[1][1]);
-		if (protocol == PROTO_SSH) {
-			const char *ssh, *ssh_basename;
-			ssh = getenv("GIT_SSH");
-			if (!ssh) ssh = "ssh";
-			ssh_basename = strrchr(ssh, '/');
-			if (!ssh_basename)
-				ssh_basename = ssh;
-			else
-				ssh_basename++;
 
-			if (!port)
-				execlp(ssh, ssh_basename, host, command, NULL);
-			else
-				execlp(ssh, ssh_basename, "-p", port, host,
-				       command, NULL);
-		}
-		else {
-			unsetenv(ALTERNATE_DB_ENVIRONMENT);
-			unsetenv(DB_ENVIRONMENT);
-			unsetenv(GIT_DIR_ENVIRONMENT);
-			unsetenv(GIT_WORK_TREE_ENVIRONMENT);
-			unsetenv(GRAFT_ENVIRONMENT);
-			unsetenv(INDEX_ENVIRONMENT);
-			execlp("sh", "sh", "-c", command, NULL);
+	of |= add_to_string(&posn, &size, prog, 0);
+	of |= add_to_string(&posn, &size, " ", 0);
+	of |= add_to_string(&posn, &size, path, 1);
+
+	if (of)
+		die("command line too long");
+
+	conn->in = conn->out = -1;
+	conn->argv = arg = xcalloc(6, sizeof(*arg));
+	if (protocol == PROTO_SSH) {
+		const char *ssh = getenv("GIT_SSH");
+		if (!ssh) ssh = "ssh";
+
+		*arg++ = ssh;
+		if (port) {
+			*arg++ = "-p";
+			*arg++ = port;
 		}
-		die("exec failed");
+		*arg++ = host;
+	}
+	else {
+		/* remove these from the environment */
+		const char *env[] = {
+			ALTERNATE_DB_ENVIRONMENT,
+			DB_ENVIRONMENT,
+			GIT_DIR_ENVIRONMENT,
+			GIT_WORK_TREE_ENVIRONMENT,
+			GRAFT_ENVIRONMENT,
+			INDEX_ENVIRONMENT,
+			NULL
+		};
+		conn->env = env;
+		*arg++ = "sh";
+		*arg++ = "-c";
 	}
-	fd[0] = pipefd[0][0];
-	fd[1] = pipefd[1][1];
-	close(pipefd[0][1]);
-	close(pipefd[1][0]);
+	*arg++ = command;
+	*arg = NULL;
+
+	if (start_command(conn))
+		die("unable to fork");
+
+	fd[0] = conn->out; /* read from child's stdout */
+	fd[1] = conn->in;  /* write to child's stdin */
 	if (free_path)
 		free(path);
 	return conn;
@@ -637,13 +630,12 @@ struct child_process *git_connect(int fd[2], char *url,
 
 int finish_connect(struct child_process *conn)
 {
+	int code;
 	if (conn == NULL)
 		return 0;
 
-	while (waitpid(conn->pid, NULL, 0) < 0) {
-		if (errno != EINTR)
-			return -1;
-	}
+	code = finish_command(conn);
+	free(conn->argv);
 	free(conn);
-	return 0;
+	return code;
 }
-- 
1.5.3.3.1134.gee562

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 03/14] Use start_command() to run content filters instead of explicit fork/exec.
  2007-10-13 20:06   ` [PATCH 02/14] Use start_command() in git_connect() instead of explicit fork/exec Johannes Sixt
@ 2007-10-13 20:06     ` Johannes Sixt
  2007-10-13 20:06       ` [PATCH 04/14] Use run_command() to spawn external diff programs instead of fork/exec Johannes Sixt
  0 siblings, 1 reply; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

The previous code already used finish_command() to wait for the process
to terminate, but did not use start_command() to run it.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 convert.c |   30 +++++++-----------------------
 1 files changed, 7 insertions(+), 23 deletions(-)

diff --git a/convert.c b/convert.c
index d77c8eb..6d64994 100644
--- a/convert.c
+++ b/convert.c
@@ -208,34 +208,18 @@ static int filter_buffer(const char *path, const char *src,
 	 * Spawn cmd and feed the buffer contents through its stdin.
 	 */
 	struct child_process child_process;
-	int pipe_feed[2];
 	int write_err, status;
+	const char *argv[] = { "sh", "-c", cmd, NULL };
 
 	memset(&child_process, 0, sizeof(child_process));
+	child_process.argv = argv;
+	child_process.in = -1;
 
-	if (pipe(pipe_feed) < 0) {
-		error("cannot create pipe to run external filter %s", cmd);
-		return 1;
-	}
-
-	child_process.pid = fork();
-	if (child_process.pid < 0) {
-		error("cannot fork to run external filter %s", cmd);
-		close(pipe_feed[0]);
-		close(pipe_feed[1]);
-		return 1;
-	}
-	if (!child_process.pid) {
-		dup2(pipe_feed[0], 0);
-		close(pipe_feed[0]);
-		close(pipe_feed[1]);
-		execlp("sh", "sh", "-c", cmd, NULL);
-		return 1;
-	}
-	close(pipe_feed[0]);
+	if (start_command(&child_process))
+		return error("cannot fork to run external filter %s", cmd);
 
-	write_err = (write_in_full(pipe_feed[1], src, size) < 0);
-	if (close(pipe_feed[1]))
+	write_err = (write_in_full(child_process.in, src, size) < 0);
+	if (close(child_process.in))
 		write_err = 1;
 	if (write_err)
 		error("cannot feed the input to external filter %s", cmd);
-- 
1.5.3.3.1134.gee562

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 04/14] Use run_command() to spawn external diff programs instead of fork/exec.
  2007-10-13 20:06     ` [PATCH 03/14] Use start_command() to run content filters " Johannes Sixt
@ 2007-10-13 20:06       ` Johannes Sixt
  2007-10-13 20:06         ` [PATCH 05/14] Use start_comand() in builtin-fetch-pack.c instead of explicit fork/exec Johannes Sixt
  0 siblings, 1 reply; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 diff.c |   38 +++-----------------------------------
 1 files changed, 3 insertions(+), 35 deletions(-)

diff --git a/diff.c b/diff.c
index 0ee9ea1..d03dc6a 100644
--- a/diff.c
+++ b/diff.c
@@ -9,6 +9,7 @@
 #include "xdiff-interface.h"
 #include "color.h"
 #include "attr.h"
+#include "run-command.h"
 
 #ifdef NO_FAST_WORKING_DIRECTORY
 #define FAST_WORKING_DIRECTORY 0
@@ -1791,40 +1792,6 @@ static void remove_tempfile_on_signal(int signo)
 	raise(signo);
 }
 
-static int spawn_prog(const char *pgm, const char **arg)
-{
-	pid_t pid;
-	int status;
-
-	fflush(NULL);
-	pid = fork();
-	if (pid < 0)
-		die("unable to fork");
-	if (!pid) {
-		execvp(pgm, (char *const*) arg);
-		exit(255);
-	}
-
-	while (waitpid(pid, &status, 0) < 0) {
-		if (errno == EINTR)
-			continue;
-		return -1;
-	}
-
-	/* Earlier we did not check the exit status because
-	 * diff exits non-zero if files are different, and
-	 * we are not interested in knowing that.  It was a
-	 * mistake which made it harder to quit a diff-*
-	 * session that uses the git-apply-patch-script as
-	 * the GIT_EXTERNAL_DIFF.  A custom GIT_EXTERNAL_DIFF
-	 * should also exit non-zero only when it wants to
-	 * abort the entire diff-* session.
-	 */
-	if (WIFEXITED(status) && !WEXITSTATUS(status))
-		return 0;
-	return -1;
-}
-
 /* An external diff command takes:
  *
  * diff-cmd name infile1 infile1-sha1 infile1-mode \
@@ -1877,7 +1844,8 @@ static void run_external_diff(const char *pgm,
 		*arg++ = name;
 	}
 	*arg = NULL;
-	retval = spawn_prog(pgm, spawn_arg);
+	fflush(NULL);
+	retval = run_command_v_opt(spawn_arg, 0);
 	remove_tempfile();
 	if (retval) {
 		fprintf(stderr, "external diff died, stopping at %s.\n", name);
-- 
1.5.3.3.1134.gee562

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 05/14] Use start_comand() in builtin-fetch-pack.c instead of explicit fork/exec.
  2007-10-13 20:06       ` [PATCH 04/14] Use run_command() to spawn external diff programs instead of fork/exec Johannes Sixt
@ 2007-10-13 20:06         ` Johannes Sixt
  2007-10-13 20:06           ` [PATCH 06/14] Have start_command() create a pipe to read the stderr of the child Johannes Sixt
  0 siblings, 1 reply; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 builtin-fetch-pack.c |   56 ++++++++++++++-----------------------------------
 1 files changed, 16 insertions(+), 40 deletions(-)

diff --git a/builtin-fetch-pack.c b/builtin-fetch-pack.c
index f56592f..871b704 100644
--- a/builtin-fetch-pack.c
+++ b/builtin-fetch-pack.c
@@ -7,6 +7,7 @@
 #include "pack.h"
 #include "sideband.h"
 #include "fetch-pack.h"
+#include "run-command.h"
 
 static int transfer_unpack_limit = -1;
 static int fetch_unpack_limit = -1;
@@ -491,18 +492,19 @@ static pid_t setup_sideband(int fd[2], int xd[2])
 
 static int get_pack(int xd[2], char **pack_lockfile)
 {
-	int status;
-	pid_t pid, side_pid;
+	pid_t side_pid;
 	int fd[2];
 	const char *argv[20];
 	char keep_arg[256];
 	char hdr_arg[256];
 	const char **av;
 	int do_keep = args.keep_pack;
-	int keep_pipe[2];
+	struct child_process cmd;
 
 	side_pid = setup_sideband(fd, xd);
 
+	memset(&cmd, 0, sizeof(cmd));
+	cmd.argv = argv;
 	av = argv;
 	*hdr_arg = 0;
 	if (!args.keep_pack && unpack_limit) {
@@ -519,8 +521,8 @@ static int get_pack(int xd[2], char **pack_lockfile)
 	}
 
 	if (do_keep) {
-		if (pack_lockfile && pipe(keep_pipe))
-			die("fetch-pack: pipe setup failure: %s", strerror(errno));
+		if (pack_lockfile)
+			cmd.out = -1;
 		*av++ = "index-pack";
 		*av++ = "--stdin";
 		if (!args.quiet && !args.no_progress)
@@ -544,43 +546,17 @@ static int get_pack(int xd[2], char **pack_lockfile)
 		*av++ = hdr_arg;
 	*av++ = NULL;
 
-	pid = fork();
-	if (pid < 0)
+	cmd.in = fd[0];
+	cmd.git_cmd = 1;
+	if (start_command(&cmd))
 		die("fetch-pack: unable to fork off %s", argv[0]);
-	if (!pid) {
-		dup2(fd[0], 0);
-		if (do_keep && pack_lockfile) {
-			dup2(keep_pipe[1], 1);
-			close(keep_pipe[0]);
-			close(keep_pipe[1]);
-		}
-		close(fd[0]);
-		close(fd[1]);
-		execv_git_cmd(argv);
-		die("%s exec failed", argv[0]);
-	}
-	close(fd[0]);
 	close(fd[1]);
-	if (do_keep && pack_lockfile) {
-		close(keep_pipe[1]);
-		*pack_lockfile = index_pack_lockfile(keep_pipe[0]);
-		close(keep_pipe[0]);
-	}
-	while (waitpid(pid, &status, 0) < 0) {
-		if (errno != EINTR)
-			die("waiting for %s: %s", argv[0], strerror(errno));
-	}
-	if (WIFEXITED(status)) {
-		int code = WEXITSTATUS(status);
-		if (code)
-			die("%s died with error code %d", argv[0], code);
-		return 0;
-	}
-	if (WIFSIGNALED(status)) {
-		int sig = WTERMSIG(status);
-		die("%s died of signal %d", argv[0], sig);
-	}
-	die("%s died of unnatural causes %d", argv[0], status);
+	if (do_keep && pack_lockfile)
+		*pack_lockfile = index_pack_lockfile(cmd.out);
+
+	if (finish_command(&cmd))
+		die("%s failed", argv[0]);
+	return 0;
 }
 
 static struct ref *do_fetch_pack(int fd[2],
-- 
1.5.3.3.1134.gee562

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 06/14] Have start_command() create a pipe to read the stderr of the child.
  2007-10-13 20:06         ` [PATCH 05/14] Use start_comand() in builtin-fetch-pack.c instead of explicit fork/exec Johannes Sixt
@ 2007-10-13 20:06           ` Johannes Sixt
  2007-10-13 20:06             ` [PATCH 07/14] upload-pack: Use start_command() to run pack-objects in create_pack_file() Johannes Sixt
  0 siblings, 1 reply; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

This adds another stanza that allocates a pipe that is connected to the
child's stderr and that the caller can read from. In order to request this
pipe, the caller sets cmd->err to -1.

The implementation is not exactly modeled after the stdout case: For stdout
the caller can supply an existing file descriptor, but this facility is
nowhere needed in the stderr case. Additionally, the caller is required to
close cmd->err.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 run-command.c |   26 ++++++++++++++++++++++++--
 run-command.h |    1 +
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/run-command.c b/run-command.c
index 7e779d3..d00c03b 100644
--- a/run-command.c
+++ b/run-command.c
@@ -17,8 +17,8 @@ static inline void dup_devnull(int to)
 
 int start_command(struct child_process *cmd)
 {
-	int need_in, need_out;
-	int fdin[2], fdout[2];
+	int need_in, need_out, need_err;
+	int fdin[2], fdout[2], fderr[2];
 
 	need_in = !cmd->no_stdin && cmd->in < 0;
 	if (need_in) {
@@ -41,12 +41,26 @@ int start_command(struct child_process *cmd)
 		cmd->close_out = 1;
 	}
 
+	need_err = cmd->err < 0;
+	if (need_err) {
+		if (pipe(fderr) < 0) {
+			if (need_in)
+				close_pair(fdin);
+			if (need_out)
+				close_pair(fdout);
+			return -ERR_RUN_COMMAND_PIPE;
+		}
+		cmd->err = fderr[0];
+	}
+
 	cmd->pid = fork();
 	if (cmd->pid < 0) {
 		if (need_in)
 			close_pair(fdin);
 		if (need_out)
 			close_pair(fdout);
+		if (need_err)
+			close_pair(fderr);
 		return -ERR_RUN_COMMAND_FORK;
 	}
 
@@ -73,6 +87,11 @@ int start_command(struct child_process *cmd)
 			close(cmd->out);
 		}
 
+		if (need_err) {
+			dup2(fderr[1], 2);
+			close_pair(fderr);
+		}
+
 		if (cmd->dir && chdir(cmd->dir))
 			die("exec %s: cd to %s failed (%s)", cmd->argv[0],
 			    cmd->dir, strerror(errno));
@@ -102,6 +121,9 @@ int start_command(struct child_process *cmd)
 	else if (cmd->out > 1)
 		close(cmd->out);
 
+	if (need_err)
+		close(fderr[1]);
+
 	return 0;
 }
 
diff --git a/run-command.h b/run-command.h
index 7958eb1..35b9fb6 100644
--- a/run-command.h
+++ b/run-command.h
@@ -16,6 +16,7 @@ struct child_process {
 	pid_t pid;
 	int in;
 	int out;
+	int err;
 	const char *dir;
 	const char *const *env;
 	unsigned close_in:1;
-- 
1.5.3.3.1134.gee562

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 07/14] upload-pack: Use start_command() to run pack-objects in create_pack_file().
  2007-10-13 20:06           ` [PATCH 06/14] Have start_command() create a pipe to read the stderr of the child Johannes Sixt
@ 2007-10-13 20:06             ` Johannes Sixt
  2007-10-13 20:06               ` [PATCH 08/14] Add infrastructure to run a function asynchronously Johannes Sixt
  0 siblings, 1 reply; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

This gets rid of an explicit fork/exec.

Since upload-pack has to coordinate two processes (rev-list and
pack-objects), we cannot use the normal finish_command(), but have to
monitor the processes explicitly. Hence, the waitpid() call remains.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 upload-pack.c |  105 ++++++++++++++++++++++++---------------------------------
 1 files changed, 44 insertions(+), 61 deletions(-)

diff --git a/upload-pack.c b/upload-pack.c
index fe96ef1..ef2894a 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -9,6 +9,7 @@
 #include "diff.h"
 #include "revision.h"
 #include "list-objects.h"
+#include "run-command.h"
 
 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
 
@@ -98,16 +99,18 @@ static void show_edge(struct commit *commit)
 
 static void create_pack_file(void)
 {
-	/* Pipes between rev-list to pack-objects, pack-objects to us
-	 * and pack-objects error stream for progress bar.
+	/* Pipe from rev-list to pack-objects
 	 */
-	int lp_pipe[2], pu_pipe[2], pe_pipe[2];
-	pid_t pid_rev_list, pid_pack_objects;
+	int lp_pipe[2];
+	pid_t pid_rev_list;
+	struct child_process pack_objects;
 	int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
 	char data[8193], progress[128];
 	char abort_msg[] = "aborting due to possible repository "
 		"corruption on the remote side.";
 	int buffered = -1;
+	const char *argv[10];
+	int arg = 0;
 
 	if (pipe(lp_pipe) < 0)
 		die("git-upload-pack: unable to create pipe");
@@ -154,52 +157,32 @@ static void create_pack_file(void)
 		exit(0);
 	}
 
-	if (pipe(pu_pipe) < 0)
-		die("git-upload-pack: unable to create pipe");
-	if (pipe(pe_pipe) < 0)
-		die("git-upload-pack: unable to create pipe");
-	pid_pack_objects = fork();
-	if (pid_pack_objects < 0) {
+	/* close this so that it is not inherited by pack_objects */
+	close(lp_pipe[1]);
+
+	argv[arg++] = "pack-objects";
+	argv[arg++] = "--stdout";
+	if (!no_progress)
+		argv[arg++] = "--progress";
+	if (use_ofs_delta)
+		argv[arg++] = "--delta-base-offset";
+	argv[arg++] = NULL;
+
+	memset(&pack_objects, 0, sizeof(pack_objects));
+	pack_objects.in = lp_pipe[0];	/* start_command closes it */
+	pack_objects.out = -1;
+	pack_objects.err = -1;
+	pack_objects.git_cmd = 1;
+	pack_objects.argv = argv;
+	if (start_command(&pack_objects)) {
 		/* daemon sets things up to ignore TERM */
 		kill(pid_rev_list, SIGKILL);
 		die("git-upload-pack: unable to fork git-pack-objects");
 	}
-	if (!pid_pack_objects) {
-		const char *argv[10];
-		int i = 0;
-
-		dup2(lp_pipe[0], 0);
-		dup2(pu_pipe[1], 1);
-		dup2(pe_pipe[1], 2);
-
-		close(lp_pipe[0]);
-		close(lp_pipe[1]);
-		close(pu_pipe[0]);
-		close(pu_pipe[1]);
-		close(pe_pipe[0]);
-		close(pe_pipe[1]);
-
-		argv[i++] = "pack-objects";
-		argv[i++] = "--stdout";
-		if (!no_progress)
-			argv[i++] = "--progress";
-		if (use_ofs_delta)
-			argv[i++] = "--delta-base-offset";
-		argv[i++] = NULL;
-
-		execv_git_cmd(argv);
-		kill(pid_rev_list, SIGKILL);
-		die("git-upload-pack: unable to exec git-pack-objects");
-	}
-
-	close(lp_pipe[0]);
-	close(lp_pipe[1]);
 
-	/* We read from pe_pipe[0] to capture stderr output for
-	 * progress bar, and pu_pipe[0] to capture the pack data.
+	/* We read from pack_objects.err to capture stderr output for
+	 * progress bar, and pack_objects.out to capture the pack data.
 	 */
-	close(pe_pipe[1]);
-	close(pu_pipe[1]);
 
 	while (1) {
 		const char *who;
@@ -214,14 +197,14 @@ static void create_pack_file(void)
 		pollsize = 0;
 		pe = pu = -1;
 
-		if (0 <= pu_pipe[0]) {
-			pfd[pollsize].fd = pu_pipe[0];
+		if (0 <= pack_objects.out) {
+			pfd[pollsize].fd = pack_objects.out;
 			pfd[pollsize].events = POLLIN;
 			pu = pollsize;
 			pollsize++;
 		}
-		if (0 <= pe_pipe[0]) {
-			pfd[pollsize].fd = pe_pipe[0];
+		if (0 <= pack_objects.err) {
+			pfd[pollsize].fd = pack_objects.err;
 			pfd[pollsize].events = POLLIN;
 			pe = pollsize;
 			pollsize++;
@@ -254,13 +237,13 @@ static void create_pack_file(void)
 					*cp++ = buffered;
 					outsz++;
 				}
-				sz = xread(pu_pipe[0], cp,
+				sz = xread(pack_objects.out, cp,
 					  sizeof(data) - outsz);
 				if (0 < sz)
 						;
 				else if (sz == 0) {
-					close(pu_pipe[0]);
-					pu_pipe[0] = -1;
+					close(pack_objects.out);
+					pack_objects.out = -1;
 				}
 				else
 					goto fail;
@@ -279,13 +262,13 @@ static void create_pack_file(void)
 				/* Status ready; we ship that in the side-band
 				 * or dump to the standard error.
 				 */
-				sz = xread(pe_pipe[0], progress,
+				sz = xread(pack_objects.err, progress,
 					  sizeof(progress));
 				if (0 < sz)
 					send_client_data(2, progress, sz);
 				else if (sz == 0) {
-					close(pe_pipe[0]);
-					pe_pipe[0] = -1;
+					close(pack_objects.err);
+					pack_objects.err = -1;
 				}
 				else
 					goto fail;
@@ -293,12 +276,12 @@ static void create_pack_file(void)
 		}
 
 		/* See if the children are still there */
-		if (pid_rev_list || pid_pack_objects) {
+		if (pid_rev_list || pack_objects.pid) {
 			pid = waitpid(-1, &status, WNOHANG);
 			if (!pid)
 				continue;
 			who = ((pid == pid_rev_list) ? "git-rev-list" :
-			       (pid == pid_pack_objects) ? "git-pack-objects" :
+			       (pid == pack_objects.pid) ? "git-pack-objects" :
 			       NULL);
 			if (!who) {
 				if (pid < 0) {
@@ -317,9 +300,9 @@ static void create_pack_file(void)
 			}
 			if (pid == pid_rev_list)
 				pid_rev_list = 0;
-			if (pid == pid_pack_objects)
-				pid_pack_objects = 0;
-			if (pid_rev_list || pid_pack_objects)
+			if (pid == pack_objects.pid)
+				pack_objects.pid = 0;
+			if (pid_rev_list || pack_objects.pid)
 				continue;
 		}
 
@@ -340,8 +323,8 @@ static void create_pack_file(void)
 		return;
 	}
  fail:
-	if (pid_pack_objects)
-		kill(pid_pack_objects, SIGKILL);
+	if (pack_objects.pid)
+		kill(pack_objects.pid, SIGKILL);
 	if (pid_rev_list)
 		kill(pid_rev_list, SIGKILL);
 	send_client_data(3, abort_msg, sizeof(abort_msg));
-- 
1.5.3.3.1134.gee562

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 08/14] Add infrastructure to run a function asynchronously.
  2007-10-13 20:06             ` [PATCH 07/14] upload-pack: Use start_command() to run pack-objects in create_pack_file() Johannes Sixt
@ 2007-10-13 20:06               ` Johannes Sixt
  2007-10-13 20:06                 ` [PATCH 09/14] Use the asyncronous function infrastructure in builtin-fetch-pack.c Johannes Sixt
  2007-10-14 17:08                 ` [PATCH amend 08/14] Add infrastructure to run a function asynchronously Johannes Sixt
  0 siblings, 2 replies; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

This adds start_async() and finish_async(), which runs a function
asynchronously. Communication with the caller happens only via pipes.
For this reason, this implementation forks off a child process that runs
the function.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 run-command.c |   53 +++++++++++++++++++++++++++++++++++++++++++++--------
 run-command.h |   23 +++++++++++++++++++++++
 2 files changed, 68 insertions(+), 8 deletions(-)

diff --git a/run-command.c b/run-command.c
index d00c03b..111d584 100644
--- a/run-command.c
+++ b/run-command.c
@@ -127,16 +127,11 @@ int start_command(struct child_process *cmd)
 	return 0;
 }
 
-int finish_command(struct child_process *cmd)
+static int wait_or_whine(pid_t pid)
 {
-	if (cmd->close_in)
-		close(cmd->in);
-	if (cmd->close_out)
-		close(cmd->out);
-
 	for (;;) {
 		int status, code;
-		pid_t waiting = waitpid(cmd->pid, &status, 0);
+		pid_t waiting = waitpid(pid, &status, 0);
 
 		if (waiting < 0) {
 			if (errno == EINTR)
@@ -144,7 +139,7 @@ int finish_command(struct child_process *cmd)
 			error("waitpid failed (%s)", strerror(errno));
 			return -ERR_RUN_COMMAND_WAITPID;
 		}
-		if (waiting != cmd->pid)
+		if (waiting != pid)
 			return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
 		if (WIFSIGNALED(status))
 			return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
@@ -158,6 +153,15 @@ int finish_command(struct child_process *cmd)
 	}
 }
 
+int finish_command(struct child_process *cmd)
+{
+	if (cmd->close_in)
+		close(cmd->in);
+	if (cmd->close_out)
+		close(cmd->out);
+	return wait_or_whine(cmd->pid);
+}
+
 int run_command(struct child_process *cmd)
 {
 	int code = start_command(cmd);
@@ -200,3 +204,36 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const
 	cmd.env = env;
 	return run_command(&cmd);
 }
+
+int start_async(struct async *async)
+{
+	int pipe_out[2];
+
+	if (pipe(pipe_out) < 0) {
+		return error("cannot create pipe: %s", strerror(errno));
+	}
+
+	async->pid = fork();
+	if (async->pid < 0) {
+		error("fork (async) failed: %s", strerror(errno));
+		close(pipe_out[0]);
+		close(pipe_out[1]);
+		return -1;
+	}
+	if (!async->pid) {
+		close(pipe_out[0]);
+		exit(!!async->proc(pipe_out[1], async->data));
+	}
+	async->out = pipe_out[0];
+	close(pipe_out[1]);
+	return 0;
+}
+
+int finish_async(struct async *async)
+{
+	int ret = 0;
+
+	if (wait_or_whine(async->pid))
+		ret = error("waitpid (async) failed");
+	return ret;
+}
diff --git a/run-command.h b/run-command.h
index 35b9fb6..c5fd2fc 100644
--- a/run-command.h
+++ b/run-command.h
@@ -43,4 +43,27 @@ int run_command_v_opt_cd(const char **argv, int opt, const char *dir);
  */
 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env);
 
+/*
+ * The purpose of the following functions is to feed a pipe by running
+ * a function asynchronously and providing output that the call reads
+ * in a different pipe.
+ *
+ * It is expected that no synchronization and mutual exclusion between
+ * the caller and the feed function is necessary so that the function
+ * can run in a thread without interfering with the caller.
+ */
+struct async {
+	/*
+	 * proc writes to fd and closes it;
+	 * returns 0 on success, non-zero on failure
+	 */
+	int (*proc)(int fd, void *data);
+	void *data;
+	int out;	/* caller reads from this */
+	pid_t pid;
+};
+
+int start_async(struct async *async);
+int finish_async(struct async *async);
+
 #endif
-- 
1.5.3.3.1134.gee562

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 09/14] Use the asyncronous function infrastructure in builtin-fetch-pack.c.
  2007-10-13 20:06               ` [PATCH 08/14] Add infrastructure to run a function asynchronously Johannes Sixt
@ 2007-10-13 20:06                 ` Johannes Sixt
  2007-10-13 20:06                   ` [PATCH 10/14] upload-pack: Move the revision walker into a separate function Johannes Sixt
  2007-10-14 17:08                 ` [PATCH amend 08/14] Add infrastructure to run a function asynchronously Johannes Sixt
  1 sibling, 1 reply; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

We run the sideband demultiplexer in an asynchronous function.

Note that earlier there was a check in the child process that closed
xd[1] only if it was different from xd[0]; this test is no longer needed
because git_connect() always returns two different file descriptors
(see ec587fde0a76780931c7ac32474c8c000aa45134).

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 builtin-fetch-pack.c |   39 ++++++++++++++++++---------------------
 1 files changed, 18 insertions(+), 21 deletions(-)

diff --git a/builtin-fetch-pack.c b/builtin-fetch-pack.c
index 871b704..51d8a32 100644
--- a/builtin-fetch-pack.c
+++ b/builtin-fetch-pack.c
@@ -457,42 +457,37 @@ static int everything_local(struct ref **refs, int nr_match, char **match)
 	return retval;
 }
 
-static pid_t setup_sideband(int fd[2], int xd[2])
+static int sideband_demux(int fd, void *data)
 {
-	pid_t side_pid;
+	int *xd = data;
 
+	close(xd[1]);
+	return recv_sideband("fetch-pack", xd[0], fd, 2);
+}
+
+static void setup_sideband(int fd[2], int xd[2], struct async *demux)
+{
 	if (!use_sideband) {
 		fd[0] = xd[0];
 		fd[1] = xd[1];
-		return 0;
+		return;
 	}
 	/* xd[] is talking with upload-pack; subprocess reads from
 	 * xd[0], spits out band#2 to stderr, and feeds us band#1
-	 * through our fd[0].
+	 * through demux->out.
 	 */
-	if (pipe(fd) < 0)
-		die("fetch-pack: unable to set up pipe");
-	side_pid = fork();
-	if (side_pid < 0)
+	demux->proc = sideband_demux;
+	demux->data = xd;
+	if (start_async(demux))
 		die("fetch-pack: unable to fork off sideband demultiplexer");
-	if (!side_pid) {
-		/* subprocess */
-		close(fd[0]);
-		if (xd[0] != xd[1])
-			close(xd[1]);
-		if (recv_sideband("fetch-pack", xd[0], fd[1], 2))
-			exit(1);
-		exit(0);
-	}
 	close(xd[0]);
-	close(fd[1]);
+	fd[0] = demux->out;
 	fd[1] = xd[1];
-	return side_pid;
 }
 
 static int get_pack(int xd[2], char **pack_lockfile)
 {
-	pid_t side_pid;
+	struct async demux;
 	int fd[2];
 	const char *argv[20];
 	char keep_arg[256];
@@ -501,7 +496,7 @@ static int get_pack(int xd[2], char **pack_lockfile)
 	int do_keep = args.keep_pack;
 	struct child_process cmd;
 
-	side_pid = setup_sideband(fd, xd);
+	setup_sideband(fd, xd, &demux);
 
 	memset(&cmd, 0, sizeof(cmd));
 	cmd.argv = argv;
@@ -556,6 +551,8 @@ static int get_pack(int xd[2], char **pack_lockfile)
 
 	if (finish_command(&cmd))
 		die("%s failed", argv[0]);
+	if (use_sideband && finish_async(&demux))
+		die("error in sideband demultiplexer");
 	return 0;
 }
 
-- 
1.5.3.3.1134.gee562

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 10/14] upload-pack: Move the revision walker into a separate function.
  2007-10-13 20:06                 ` [PATCH 09/14] Use the asyncronous function infrastructure in builtin-fetch-pack.c Johannes Sixt
@ 2007-10-13 20:06                   ` Johannes Sixt
  2007-10-13 20:06                     ` [PATCH 11/14] upload-pack: Run rev-list in an asynchronous function Johannes Sixt
  0 siblings, 1 reply; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

While this is mostly a cleanup and makes a long function shorter, it later
also allows us to use start_async() with this function.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 upload-pack.c |   70 ++++++++++++++++++++++++++++++--------------------------
 1 files changed, 37 insertions(+), 33 deletions(-)

diff --git a/upload-pack.c b/upload-pack.c
index ef2894a..c5aa0ea 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -97,6 +97,42 @@ static void show_edge(struct commit *commit)
 	fprintf(pack_pipe, "-%s\n", sha1_to_hex(commit->object.sha1));
 }
 
+static void do_rev_list(int create_full_pack)
+{
+	int i;
+	struct rev_info revs;
+
+	if (create_full_pack)
+		use_thin_pack = 0; /* no point doing it */
+	init_revisions(&revs, NULL);
+	revs.tag_objects = 1;
+	revs.tree_objects = 1;
+	revs.blob_objects = 1;
+	if (use_thin_pack)
+		revs.edge_hint = 1;
+
+	if (create_full_pack) {
+		const char *args[] = {"rev-list", "--all", NULL};
+		setup_revisions(2, args, &revs, NULL);
+	} else {
+		for (i = 0; i < want_obj.nr; i++) {
+			struct object *o = want_obj.objects[i].item;
+			/* why??? */
+			o->flags &= ~UNINTERESTING;
+			add_pending_object(&revs, o, NULL);
+		}
+		for (i = 0; i < have_obj.nr; i++) {
+			struct object *o = have_obj.objects[i].item;
+			o->flags |= UNINTERESTING;
+			add_pending_object(&revs, o, NULL);
+		}
+		setup_revisions(0, NULL, &revs, NULL);
+	}
+	prepare_revision_walk(&revs);
+	mark_edges_uninteresting(revs.commits, &revs, show_edge);
+	traverse_commit_list(&revs, show_commit, show_object);
+}
+
 static void create_pack_file(void)
 {
 	/* Pipe from rev-list to pack-objects
@@ -119,41 +155,9 @@ static void create_pack_file(void)
 		die("git-upload-pack: unable to fork git-rev-list");
 
 	if (!pid_rev_list) {
-		int i;
-		struct rev_info revs;
-
 		close(lp_pipe[0]);
 		pack_pipe = fdopen(lp_pipe[1], "w");
-
-		if (create_full_pack)
-			use_thin_pack = 0; /* no point doing it */
-		init_revisions(&revs, NULL);
-		revs.tag_objects = 1;
-		revs.tree_objects = 1;
-		revs.blob_objects = 1;
-		if (use_thin_pack)
-			revs.edge_hint = 1;
-
-		if (create_full_pack) {
-			const char *args[] = {"rev-list", "--all", NULL};
-			setup_revisions(2, args, &revs, NULL);
-		} else {
-			for (i = 0; i < want_obj.nr; i++) {
-				struct object *o = want_obj.objects[i].item;
-				/* why??? */
-				o->flags &= ~UNINTERESTING;
-				add_pending_object(&revs, o, NULL);
-			}
-			for (i = 0; i < have_obj.nr; i++) {
-				struct object *o = have_obj.objects[i].item;
-				o->flags |= UNINTERESTING;
-				add_pending_object(&revs, o, NULL);
-			}
-			setup_revisions(0, NULL, &revs, NULL);
-		}
-		prepare_revision_walk(&revs);
-		mark_edges_uninteresting(revs.commits, &revs, show_edge);
-		traverse_commit_list(&revs, show_commit, show_object);
+		do_rev_list(create_full_pack);
 		exit(0);
 	}
 
-- 
1.5.3.3.1134.gee562

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 11/14] upload-pack: Run rev-list in an asynchronous function.
  2007-10-13 20:06                   ` [PATCH 10/14] upload-pack: Move the revision walker into a separate function Johannes Sixt
@ 2007-10-13 20:06                     ` Johannes Sixt
  2007-10-13 20:06                       ` [PATCH 12/14] t0021-conversion.sh: Test that the clean filter really cleans content Johannes Sixt
  0 siblings, 1 reply; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

This gets rid of an explicit fork().

Since upload-pack has to coordinate two processes (rev-list and
pack-objects), we cannot use the normal finish_async(), but have to monitor
the process explicitly. Hence, there are no changes at this front.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 upload-pack.c |   46 ++++++++++++++++++----------------------------
 1 files changed, 18 insertions(+), 28 deletions(-)

diff --git a/upload-pack.c b/upload-pack.c
index c5aa0ea..6799468 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -97,11 +97,12 @@ static void show_edge(struct commit *commit)
 	fprintf(pack_pipe, "-%s\n", sha1_to_hex(commit->object.sha1));
 }
 
-static void do_rev_list(int create_full_pack)
+static int do_rev_list(int fd, void *create_full_pack)
 {
 	int i;
 	struct rev_info revs;
 
+	pack_pipe = fdopen(fd, "w");
 	if (create_full_pack)
 		use_thin_pack = 0; /* no point doing it */
 	init_revisions(&revs, NULL);
@@ -131,14 +132,12 @@ static void do_rev_list(int create_full_pack)
 	prepare_revision_walk(&revs);
 	mark_edges_uninteresting(revs.commits, &revs, show_edge);
 	traverse_commit_list(&revs, show_commit, show_object);
+	return 0;
 }
 
 static void create_pack_file(void)
 {
-	/* Pipe from rev-list to pack-objects
-	 */
-	int lp_pipe[2];
-	pid_t pid_rev_list;
+	struct async rev_list;
 	struct child_process pack_objects;
 	int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
 	char data[8193], progress[128];
@@ -148,22 +147,12 @@ static void create_pack_file(void)
 	const char *argv[10];
 	int arg = 0;
 
-	if (pipe(lp_pipe) < 0)
-		die("git-upload-pack: unable to create pipe");
-	pid_rev_list = fork();
-	if (pid_rev_list < 0)
+	rev_list.proc = do_rev_list;
+	/* .data is just a boolean: any non-NULL value will do */
+	rev_list.data = create_full_pack ? &rev_list : NULL;
+	if (start_async(&rev_list))
 		die("git-upload-pack: unable to fork git-rev-list");
 
-	if (!pid_rev_list) {
-		close(lp_pipe[0]);
-		pack_pipe = fdopen(lp_pipe[1], "w");
-		do_rev_list(create_full_pack);
-		exit(0);
-	}
-
-	/* close this so that it is not inherited by pack_objects */
-	close(lp_pipe[1]);
-
 	argv[arg++] = "pack-objects";
 	argv[arg++] = "--stdout";
 	if (!no_progress)
@@ -173,14 +162,15 @@ static void create_pack_file(void)
 	argv[arg++] = NULL;
 
 	memset(&pack_objects, 0, sizeof(pack_objects));
-	pack_objects.in = lp_pipe[0];	/* start_command closes it */
+	pack_objects.in = rev_list.out;	/* start_command closes it */
 	pack_objects.out = -1;
 	pack_objects.err = -1;
 	pack_objects.git_cmd = 1;
 	pack_objects.argv = argv;
+
 	if (start_command(&pack_objects)) {
 		/* daemon sets things up to ignore TERM */
-		kill(pid_rev_list, SIGKILL);
+		kill(rev_list.pid, SIGKILL);
 		die("git-upload-pack: unable to fork git-pack-objects");
 	}
 
@@ -280,11 +270,11 @@ static void create_pack_file(void)
 		}
 
 		/* See if the children are still there */
-		if (pid_rev_list || pack_objects.pid) {
+		if (rev_list.pid || pack_objects.pid) {
 			pid = waitpid(-1, &status, WNOHANG);
 			if (!pid)
 				continue;
-			who = ((pid == pid_rev_list) ? "git-rev-list" :
+			who = ((pid == rev_list.pid) ? "git-rev-list" :
 			       (pid == pack_objects.pid) ? "git-pack-objects" :
 			       NULL);
 			if (!who) {
@@ -302,11 +292,11 @@ static void create_pack_file(void)
 				      who);
 				goto fail;
 			}
-			if (pid == pid_rev_list)
-				pid_rev_list = 0;
+			if (pid == rev_list.pid)
+				rev_list.pid = 0;
 			if (pid == pack_objects.pid)
 				pack_objects.pid = 0;
-			if (pid_rev_list || pack_objects.pid)
+			if (rev_list.pid || pack_objects.pid)
 				continue;
 		}
 
@@ -329,8 +319,8 @@ static void create_pack_file(void)
  fail:
 	if (pack_objects.pid)
 		kill(pack_objects.pid, SIGKILL);
-	if (pid_rev_list)
-		kill(pid_rev_list, SIGKILL);
+	if (rev_list.pid)
+		kill(rev_list.pid, SIGKILL);
 	send_client_data(3, abort_msg, sizeof(abort_msg));
 	die("git-upload-pack: %s", abort_msg);
 }
-- 
1.5.3.3.1134.gee562

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 12/14] t0021-conversion.sh: Test that the clean filter really cleans content.
  2007-10-13 20:06                     ` [PATCH 11/14] upload-pack: Run rev-list in an asynchronous function Johannes Sixt
@ 2007-10-13 20:06                       ` Johannes Sixt
  2007-10-13 20:06                         ` [PATCH 13/14] Avoid a dup2(2) in apply_filter() - start_command() can do it for us Johannes Sixt
  0 siblings, 1 reply; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

This test uses a rot13 filter, which is its own inverse. It tested only
that the content was the same as the original after both the 'clean' and
the 'smudge' filter were applied. This way it would not detect whether
any filter was run at all. Hence, here we add another test that checks
that the repository contained content that was processed by the 'clean'
filter.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 t/t0021-conversion.sh |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)

diff --git a/t/t0021-conversion.sh b/t/t0021-conversion.sh
index a839f4e..cb86029 100755
--- a/t/t0021-conversion.sh
+++ b/t/t0021-conversion.sh
@@ -42,7 +42,12 @@ test_expect_success check '
 	git diff --raw --exit-code :test :test.i &&
 	id=$(git rev-parse --verify :test) &&
 	embedded=$(sed -ne "$script" test.i) &&
-	test "z$id" = "z$embedded"
+	test "z$id" = "z$embedded" &&
+
+	git cat-file blob :test.t > test.r &&
+
+	./rot13.sh < test.o > test.t &&
+	cmp test.r test.t
 '
 
 # If an expanded ident ever gets into the repository, we want to make sure that
-- 
1.5.3.3.1134.gee562

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 13/14] Avoid a dup2(2) in apply_filter() - start_command() can do it for us.
  2007-10-13 20:06                       ` [PATCH 12/14] t0021-conversion.sh: Test that the clean filter really cleans content Johannes Sixt
@ 2007-10-13 20:06                         ` Johannes Sixt
  2007-10-13 20:06                           ` [PATCH 14/14] Use the asyncronous function infrastructure to run the content filter Johannes Sixt
  0 siblings, 1 reply; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

When apply_filter() runs the external (clean or smudge) filter program, it
needs to pass the writable end of a pipe as its stdout. For this purpose,
it used to dup2(2) the file descriptor explicitly to stdout. Now we use
the facilities of start_command() to do it for us.

Furthermore, the path argument of a subordinate function, filter_buffer(),
was not used, so here we replace it to pass the fd instead.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 convert.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/convert.c b/convert.c
index 6d64994..c870817 100644
--- a/convert.c
+++ b/convert.c
@@ -201,7 +201,7 @@ static char *crlf_to_worktree(const char *path, const char *src, unsigned long *
 	return buffer;
 }
 
-static int filter_buffer(const char *path, const char *src,
+static int filter_buffer(int fd, const char *src,
 			 unsigned long size, const char *cmd)
 {
 	/*
@@ -214,6 +214,7 @@ static int filter_buffer(const char *path, const char *src,
 	memset(&child_process, 0, sizeof(child_process));
 	child_process.argv = argv;
 	child_process.in = -1;
+	child_process.out = fd;
 
 	if (start_command(&child_process))
 		return error("cannot fork to run external filter %s", cmd);
@@ -265,10 +266,8 @@ static char *apply_filter(const char *path, const char *src,
 		return NULL;
 	}
 	if (!child_process.pid) {
-		dup2(pipe_feed[1], 1);
 		close(pipe_feed[0]);
-		close(pipe_feed[1]);
-		exit(filter_buffer(path, src, *sizep, cmd));
+		exit(filter_buffer(pipe_feed[1], src, *sizep, cmd));
 	}
 	close(pipe_feed[1]);
 
-- 
1.5.3.3.1134.gee562

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 14/14] Use the asyncronous function infrastructure to run the content filter.
  2007-10-13 20:06                         ` [PATCH 13/14] Avoid a dup2(2) in apply_filter() - start_command() can do it for us Johannes Sixt
@ 2007-10-13 20:06                           ` Johannes Sixt
  2007-10-14  3:07                             ` Johannes Schindelin
  0 siblings, 1 reply; 31+ messages in thread
From: Johannes Sixt @ 2007-10-13 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes Sixt

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 convert.c |   60 +++++++++++++++++++++++++++---------------------------------
 1 files changed, 27 insertions(+), 33 deletions(-)

diff --git a/convert.c b/convert.c
index c870817..10161a0 100644
--- a/convert.c
+++ b/convert.c
@@ -201,15 +201,21 @@ static char *crlf_to_worktree(const char *path, const char *src, unsigned long *
 	return buffer;
 }
 
-static int filter_buffer(int fd, const char *src,
-			 unsigned long size, const char *cmd)
+struct filter_params {
+	const char *src;
+	unsigned long size;
+	const char *cmd;
+};
+
+static int filter_buffer(int fd, void *data)
 {
 	/*
 	 * Spawn cmd and feed the buffer contents through its stdin.
 	 */
 	struct child_process child_process;
+	struct filter_params *params = (struct filter_params *)data;
 	int write_err, status;
-	const char *argv[] = { "sh", "-c", cmd, NULL };
+	const char *argv[] = { "sh", "-c", params->cmd, NULL };
 
 	memset(&child_process, 0, sizeof(child_process));
 	child_process.argv = argv;
@@ -217,17 +223,17 @@ static int filter_buffer(int fd, const char *src,
 	child_process.out = fd;
 
 	if (start_command(&child_process))
-		return error("cannot fork to run external filter %s", cmd);
+		return error("cannot fork to run external filter %s", params->cmd);
 
-	write_err = (write_in_full(child_process.in, src, size) < 0);
+	write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
 	if (close(child_process.in))
 		write_err = 1;
 	if (write_err)
-		error("cannot feed the input to external filter %s", cmd);
+		error("cannot feed the input to external filter %s", params->cmd);
 
 	status = finish_command(&child_process);
 	if (status)
-		error("external filter %s failed %d", cmd, -status);
+		error("external filter %s failed", params->cmd);
 	return (write_err || status);
 }
 
@@ -241,42 +247,31 @@ static char *apply_filter(const char *path, const char *src,
 	 * (child --> cmd) --> us
 	 */
 	const int SLOP = 4096;
-	int pipe_feed[2];
-	int status;
 	char *dst;
 	unsigned long dstsize, dstalloc;
-	struct child_process child_process;
+	struct async async;
+	struct filter_params params;
 
 	if (!cmd)
 		return NULL;
 
-	memset(&child_process, 0, sizeof(child_process));
-
-	if (pipe(pipe_feed) < 0) {
-		error("cannot create pipe to run external filter %s", cmd);
-		return NULL;
-	}
+	memset(&async, 0, sizeof(async));
+	async.proc = filter_buffer;
+	async.data = &params;
+	params.src = src;
+	params.size = *sizep;
+	params.cmd = cmd;
 
 	fflush(NULL);
-	child_process.pid = fork();
-	if (child_process.pid < 0) {
-		error("cannot fork to run external filter %s", cmd);
-		close(pipe_feed[0]);
-		close(pipe_feed[1]);
-		return NULL;
-	}
-	if (!child_process.pid) {
-		close(pipe_feed[0]);
-		exit(filter_buffer(pipe_feed[1], src, *sizep, cmd));
-	}
-	close(pipe_feed[1]);
+	if (start_async(&async))
+		return 0;	/* error was already reported */
 
 	dstalloc = *sizep;
 	dst = xmalloc(dstalloc);
 	dstsize = 0;
 
 	while (1) {
-		ssize_t numread = xread(pipe_feed[0], dst + dstsize,
+		ssize_t numread = xread(async.out, dst + dstsize,
 					dstalloc - dstsize);
 
 		if (numread <= 0) {
@@ -293,15 +288,14 @@ static char *apply_filter(const char *path, const char *src,
 			dst = xrealloc(dst, dstalloc);
 		}
 	}
-	if (close(pipe_feed[0])) {
+	if (close(async.out)) {
 		error("read from external filter %s failed", cmd);
 		free(dst);
 		dst = NULL;
 	}
 
-	status = finish_command(&child_process);
-	if (status) {
-		error("external filter %s failed %d", cmd, -status);
+	if (finish_async(&async)) {
+		error("external filter %s failed", cmd);
 		free(dst);
 		dst = NULL;
 	}
-- 
1.5.3.3.1134.gee562

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* Re: [PATCH 01/14] Change git_connect() to return a struct child_process instead of a pid_t.
  2007-10-13 20:06 ` [PATCH 01/14] Change git_connect() to return a struct child_process instead of a pid_t Johannes Sixt
  2007-10-13 20:06   ` [PATCH 02/14] Use start_command() in git_connect() instead of explicit fork/exec Johannes Sixt
@ 2007-10-14  0:57   ` Johannes Schindelin
  2007-10-14  9:40     ` Johannes Sixt
  1 sibling, 1 reply; 31+ messages in thread
From: Johannes Schindelin @ 2007-10-14  0:57 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: gitster, git

Hi,

On Sat, 13 Oct 2007, Johannes Sixt wrote:

> -int finish_connect(pid_t pid)
> +int finish_connect(struct child_process *conn)
>  {
> -	if (pid == 0)
> +	if (conn == NULL)
>  		return 0;
>  
> -	while (waitpid(pid, NULL, 0) < 0) {
> +	while (waitpid(conn->pid, NULL, 0) < 0) {
>  		if (errno != EINTR)
>  			return -1;

Just for completeness' sake: could you do a free(conn); before return -1;?

Thanks,
Dscho

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 0/14] fork/exec removal series
  2007-10-13 20:06 [PATCH 0/14] fork/exec removal series Johannes Sixt
  2007-10-13 20:06 ` [PATCH 01/14] Change git_connect() to return a struct child_process instead of a pid_t Johannes Sixt
@ 2007-10-14  2:11 ` Shawn O. Pearce
  2007-10-14  2:50   ` Johannes Schindelin
  1 sibling, 1 reply; 31+ messages in thread
From: Shawn O. Pearce @ 2007-10-14  2:11 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: gitster, git

Johannes Sixt <johannes.sixt@telecom.at> wrote:
> here is a series of patches that removes a number fork/exec pairs.
...
> The series consists of 2 parts:
> 
> - The first half replaces a number of fork/exec pairs by start_command/
>   finish_command or run_command.
> 
> - The second half introduces a new framework that runs a function
>   asynchronously. New functions start_async and finish_async are implemented
>   similarly to start_command and run_command. They are used to replace
>   occurrences of fork() that does not exec() in the child. Such code
>   could in principle be run in a thread, and on MinGW port we will go this
>   route, but on Posix we stay with fork().

This series looks pretty good to me.  I like seeing huge blocks
go away only to be replaced with the simple API offered by
run-command.h.  Makes the result much easier to follow.

The async interface is also quite simple.  Unfortunately there
is some risk with the canonical fork() implementation in that the
async routine might attempt to alter global data that the parent
is also using, and folks on a good UNIX that is using the fork()
implementation will not even notice as they are in totally separated
address spaces.  But you'll see it in MSYS Git.

Since builtin-pack-objects now accepts (limited) pthread support,
perhaps this should be implemented in terms of pthread support
when pthreads are available?  Most Linux/BSD/Mac OS X systems do
have pthreads these days and that's the majority of git users and
developers.  This would make it more likely that bugs in this sort
of code would be detected early.  Just a thought.
 
>  13 files changed, 334 insertions(+), 369 deletions(-)

Hard to argue with that final state.  You killed 35 lines and
also made Git easier to port to "that OS unfortunately named after
transparent glass thingies".

-- 
Shawn.

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 0/14] fork/exec removal series
  2007-10-14  2:11 ` [PATCH 0/14] fork/exec removal series Shawn O. Pearce
@ 2007-10-14  2:50   ` Johannes Schindelin
  2007-10-14  2:58     ` Shawn O. Pearce
  0 siblings, 1 reply; 31+ messages in thread
From: Johannes Schindelin @ 2007-10-14  2:50 UTC (permalink / raw)
  To: Shawn O. Pearce; +Cc: Johannes Sixt, gitster, git

Hi,

On Sat, 13 Oct 2007, Shawn O. Pearce wrote:

> Since builtin-pack-objects now accepts (limited) pthread support, 
> perhaps this should be implemented in terms of pthread support when 
> pthreads are available?

Falling back to fork() when no pthreads are available?  Yes, that makes 
sense.

It might also (marginally) speed up operations, since the switches between 
threads are cheaper than those between processes, right?

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 0/14] fork/exec removal series
  2007-10-14  2:50   ` Johannes Schindelin
@ 2007-10-14  2:58     ` Shawn O. Pearce
  2007-10-14  7:12       ` Pierre Habouzit
  0 siblings, 1 reply; 31+ messages in thread
From: Shawn O. Pearce @ 2007-10-14  2:58 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Johannes Sixt, gitster, git

Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> On Sat, 13 Oct 2007, Shawn O. Pearce wrote:
> 
> > Since builtin-pack-objects now accepts (limited) pthread support, 
> > perhaps this should be implemented in terms of pthread support when 
> > pthreads are available?
> 
> Falling back to fork() when no pthreads are available?  Yes, that makes 
> sense.
> 
> It might also (marginally) speed up operations, since the switches between 
> threads are cheaper than those between processes, right?

Usually.  If we have a large virtual address space (say due to
opening a bunch of packfiles and reading commits out of them into
struct commit* thingies) and the OS does a giant copy of the page
tables during fork() then the pthread creation should be a heck of
a lot cheaper.

But we most definately *must* continue to support fork() for the
async functions.  Its the most common interface available on one
of our biggest platforms (UNIX).

-- 
Shawn.

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 14/14] Use the asyncronous function infrastructure to run the content filter.
  2007-10-13 20:06                           ` [PATCH 14/14] Use the asyncronous function infrastructure to run the content filter Johannes Sixt
@ 2007-10-14  3:07                             ` Johannes Schindelin
  2007-10-14  9:39                               ` Johannes Sixt
  2007-10-14 17:14                               ` [PATCH amend " Johannes Sixt
  0 siblings, 2 replies; 31+ messages in thread
From: Johannes Schindelin @ 2007-10-14  3:07 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: gitster, git

Hi,

On Sat, 13 Oct 2007, Johannes Sixt wrote:

>  	status = finish_command(&child_process);
>  	if (status)
> -		error("external filter %s failed %d", cmd, -status);
> +		error("external filter %s failed", params->cmd);

Did you mean to remove the status from the output (it should probably read 
"(exit status %d)" instead of just "%d", but an exit status can help 
identify problems, right?


> -	child_process.pid = fork();
> -	if (child_process.pid < 0) {
> -		error("cannot fork to run external filter %s", cmd);
> -		close(pipe_feed[0]);
> -		close(pipe_feed[1]);
> -		return NULL;
> -	}
> -	if (!child_process.pid) {
> -		close(pipe_feed[0]);
> -		exit(filter_buffer(pipe_feed[1], src, *sizep, cmd));
> -	}
> -	close(pipe_feed[1]);
> +	if (start_async(&async))
> +		return 0;	/* error was already reported */

Please write "return NULL;"

Thanks,
Dscho

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 0/14] fork/exec removal series
  2007-10-14  2:58     ` Shawn O. Pearce
@ 2007-10-14  7:12       ` Pierre Habouzit
  2007-10-14  7:17         ` Pierre Habouzit
  2007-10-14 17:09         ` Johannes Schindelin
  0 siblings, 2 replies; 31+ messages in thread
From: Pierre Habouzit @ 2007-10-14  7:12 UTC (permalink / raw)
  To: Shawn O. Pearce; +Cc: Johannes Schindelin, Johannes Sixt, gitster, git

[-- Attachment #1: Type: text/plain, Size: 1945 bytes --]

On Sun, Oct 14, 2007 at 02:58:57AM +0000, Shawn O. Pearce wrote:
> Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> > On Sat, 13 Oct 2007, Shawn O. Pearce wrote:
> > 
> > > Since builtin-pack-objects now accepts (limited) pthread support, 
> > > perhaps this should be implemented in terms of pthread support when 
> > > pthreads are available?
> > 
> > Falling back to fork() when no pthreads are available?  Yes, that makes 
> > sense.
> > 
> > It might also (marginally) speed up operations, since the switches between 
> > threads are cheaper than those between processes, right?
> 
> Usually.  If we have a large virtual address space (say due to
> opening a bunch of packfiles and reading commits out of them into
> struct commit* thingies) and the OS does a giant copy of the page
> tables during fork() then the pthread creation should be a heck of
> a lot cheaper.
> 
> But we most definately *must* continue to support fork() for the
> async functions.  Its the most common interface available on one
> of our biggest platforms (UNIX).

  Yeah that, and the fact that many of the git modules aren't
thread-safe (some modules have static buffers strbuf's or caching
variables) and should be used with care.

  The trivial way is to add a __thread keyword to make them TLS
variables, though, it's not really a step in the direction of
portability, and last time I looked at it, mingw didn't had TLS support,
not sure if msys has. Though, if Msys has, it's worth using, and we
could require that targets using the fancy pthread thingy should also
have some fancy TLS, or use fork().

  Portability for such issues, would be to use pthread_key_* and
pthread_{get,set}specific, and those are a hell of a sucky API.

-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 0/14] fork/exec removal series
  2007-10-14  7:12       ` Pierre Habouzit
@ 2007-10-14  7:17         ` Pierre Habouzit
  2007-10-14  7:28           ` Pierre Habouzit
  2007-10-14 17:09         ` Johannes Schindelin
  1 sibling, 1 reply; 31+ messages in thread
From: Pierre Habouzit @ 2007-10-14  7:17 UTC (permalink / raw)
  To: Shawn O. Pearce, Johannes Schindelin, Johannes Sixt, gitster, git

[-- Attachment #1: Type: text/plain, Size: 642 bytes --]

On dim, oct 14, 2007 at 07:12:39 +0000, Pierre Habouzit wrote:
>   The trivial way is to add a __thread keyword to make them TLS
> variables, though, it's not really a step in the direction of
> portability, and last time I looked at it, mingw didn't had TLS support,
> not sure if msys has. Though, if Msys has, it's worth using, and we

  Okay forget it, mingw and msys are one and the same *g*.
  So well, maybe threading isn't such a so great idea :/

-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 0/14] fork/exec removal series
  2007-10-14  7:17         ` Pierre Habouzit
@ 2007-10-14  7:28           ` Pierre Habouzit
  2007-10-14  9:10             ` Andreas Ericsson
  0 siblings, 1 reply; 31+ messages in thread
From: Pierre Habouzit @ 2007-10-14  7:28 UTC (permalink / raw)
  To: Shawn O. Pearce, Johannes Schindelin, Johannes Sixt, gitster, git

[-- Attachment #1: Type: text/plain, Size: 1196 bytes --]

On dim, oct 14, 2007 at 07:17:51 +0000, Pierre Habouzit wrote:
> On dim, oct 14, 2007 at 07:12:39 +0000, Pierre Habouzit wrote:
> >   The trivial way is to add a __thread keyword to make them TLS
> > variables, though, it's not really a step in the direction of
> > portability, and last time I looked at it, mingw didn't had TLS support,
> > not sure if msys has. Though, if Msys has, it's worth using, and we
> 
>   Okay forget it, mingw and msys are one and the same *g*.
>   So well, maybe threading isn't such a so great idea :/

  And again last time I checked it was still a mingw 3.x in debian, now
that it's 4.2.1 it seems to support __thread (but not
__declspec(thread)) and their changelog seems to confirm that fact [0].

  So the question holds again, do we require pthread-using targets to
support TLS ? It feels sane and right to me, but …


  [0] http://sourceforge.net/project/shownotes.php?release_id=532062
      [...]
      * The  __thread keyword is honoured.
      [...]

-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 0/14] fork/exec removal series
  2007-10-14  7:28           ` Pierre Habouzit
@ 2007-10-14  9:10             ` Andreas Ericsson
  0 siblings, 0 replies; 31+ messages in thread
From: Andreas Ericsson @ 2007-10-14  9:10 UTC (permalink / raw)
  To: Pierre Habouzit, Shawn O. Pearce, Johannes Schindelin,
	Johannes Sixt, gitster

Pierre Habouzit wrote:
> On dim, oct 14, 2007 at 07:17:51 +0000, Pierre Habouzit wrote:
>> On dim, oct 14, 2007 at 07:12:39 +0000, Pierre Habouzit wrote:
>>>   The trivial way is to add a __thread keyword to make them TLS
>>> variables, though, it's not really a step in the direction of
>>> portability, and last time I looked at it, mingw didn't had TLS support,
>>> not sure if msys has. Though, if Msys has, it's worth using, and we
>>   Okay forget it, mingw and msys are one and the same *g*.
>>   So well, maybe threading isn't such a so great idea :/
> 
>   And again last time I checked it was still a mingw 3.x in debian, now
> that it's 4.2.1 it seems to support __thread (but not
> __declspec(thread)) and their changelog seems to confirm that fact [0].
> 
>   So the question holds again, do we require pthread-using targets to
> support TLS ? It feels sane and right to me, but …
> 

To me it's a sane place to start. As time goes by and people on non-TLS
capable systems come along that need the functionality (or the speedup;
fork() is expensive on some systems), they can probably implement it
themselves or at least give voice to the fact that they need it.

-- 
Andreas Ericsson                   andreas.ericsson@op5.se
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 14/14] Use the asyncronous function infrastructure to run the content filter.
  2007-10-14  3:07                             ` Johannes Schindelin
@ 2007-10-14  9:39                               ` Johannes Sixt
  2007-10-14 17:14                               ` [PATCH amend " Johannes Sixt
  1 sibling, 0 replies; 31+ messages in thread
From: Johannes Sixt @ 2007-10-14  9:39 UTC (permalink / raw)
  To: git; +Cc: Johannes Schindelin, gitster

On Sunday 14 October 2007 05:07, Johannes Schindelin wrote:
> Hi,
>
> On Sat, 13 Oct 2007, Johannes Sixt wrote:
> >  	status = finish_command(&child_process);
> >  	if (status)
> > -		error("external filter %s failed %d", cmd, -status);
> > +		error("external filter %s failed", params->cmd);
>
> Did you mean to remove the status from the output (it should probably read
> "(exit status %d)" instead of just "%d", but an exit status can help
> identify problems, right?

Oops, that looks like an artefact. Will correct.

> > +	if (start_async(&async))
> > +		return 0;	/* error was already reported */
>
> Please write "return NULL;"

Will do.

-- Hannes

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 01/14] Change git_connect() to return a struct child_process instead of a pid_t.
  2007-10-14  0:57   ` [PATCH 01/14] Change git_connect() to return a struct child_process instead of a pid_t Johannes Schindelin
@ 2007-10-14  9:40     ` Johannes Sixt
  2007-10-14 17:10       ` Johannes Schindelin
  0 siblings, 1 reply; 31+ messages in thread
From: Johannes Sixt @ 2007-10-14  9:40 UTC (permalink / raw)
  To: git; +Cc: Johannes Schindelin, gitster

On Sunday 14 October 2007 02:57, Johannes Schindelin wrote:
> Hi,
>
> On Sat, 13 Oct 2007, Johannes Sixt wrote:
> > -int finish_connect(pid_t pid)
> > +int finish_connect(struct child_process *conn)
> >  {
> > -	if (pid == 0)
> > +	if (conn == NULL)
> >  		return 0;
> >
> > -	while (waitpid(pid, NULL, 0) < 0) {
> > +	while (waitpid(conn->pid, NULL, 0) < 0) {
> >  		if (errno != EINTR)
> >  			return -1;
>
> Just for completeness' sake: could you do a free(conn); before return -1;?

I know. But the loop is going away with the next patch, so I didn't bother. 
Can you live with that?

-- Hannes

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH amend 08/14] Add infrastructure to run a function asynchronously.
  2007-10-13 20:06               ` [PATCH 08/14] Add infrastructure to run a function asynchronously Johannes Sixt
  2007-10-13 20:06                 ` [PATCH 09/14] Use the asyncronous function infrastructure in builtin-fetch-pack.c Johannes Sixt
@ 2007-10-14 17:08                 ` Johannes Sixt
  1 sibling, 0 replies; 31+ messages in thread
From: Johannes Sixt @ 2007-10-14 17:08 UTC (permalink / raw)
  To: gitster; +Cc: git

This adds start_async() and finish_async(), which runs a function
asynchronously. Communication with the caller happens only via pipes.
For this reason, this implementation forks off a child process that runs
the function.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---

This is the same as submitted previously except it uses close_pair()
to close the pipe in an error patch and improves the comments in
run-command.h.

-- Hannes

 run-command.c |   52 ++++++++++++++++++++++++++++++++++++++++++++--------
 run-command.h |   22 ++++++++++++++++++++++
 2 files changed, 66 insertions(+), 8 deletions(-)

diff --git a/run-command.c b/run-command.c
index d00c03b..db02f75 100644
--- a/run-command.c
+++ b/run-command.c
@@ -127,16 +127,11 @@ int start_command(struct child_process *cmd)
 	return 0;
 }
 
-int finish_command(struct child_process *cmd)
+static int wait_or_whine(pid_t pid)
 {
-	if (cmd->close_in)
-		close(cmd->in);
-	if (cmd->close_out)
-		close(cmd->out);
-
 	for (;;) {
 		int status, code;
-		pid_t waiting = waitpid(cmd->pid, &status, 0);
+		pid_t waiting = waitpid(pid, &status, 0);
 
 		if (waiting < 0) {
 			if (errno == EINTR)
@@ -144,7 +139,7 @@ int finish_command(struct child_process *cmd)
 			error("waitpid failed (%s)", strerror(errno));
 			return -ERR_RUN_COMMAND_WAITPID;
 		}
-		if (waiting != cmd->pid)
+		if (waiting != pid)
 			return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
 		if (WIFSIGNALED(status))
 			return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
@@ -158,6 +153,15 @@ int finish_command(struct child_process *cmd)
 	}
 }
 
+int finish_command(struct child_process *cmd)
+{
+	if (cmd->close_in)
+		close(cmd->in);
+	if (cmd->close_out)
+		close(cmd->out);
+	return wait_or_whine(cmd->pid);
+}
+
 int run_command(struct child_process *cmd)
 {
 	int code = start_command(cmd);
@@ -200,3 +204,35 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const
 	cmd.env = env;
 	return run_command(&cmd);
 }
+
+int start_async(struct async *async)
+{
+	int pipe_out[2];
+
+	if (pipe(pipe_out) < 0) {
+		return error("cannot create pipe: %s", strerror(errno));
+	}
+
+	async->pid = fork();
+	if (async->pid < 0) {
+		error("fork (async) failed: %s", strerror(errno));
+		close_pair(pipe_out);
+		return -1;
+	}
+	if (!async->pid) {
+		close(pipe_out[0]);
+		exit(!!async->proc(pipe_out[1], async->data));
+	}
+	async->out = pipe_out[0];
+	close(pipe_out[1]);
+	return 0;
+}
+
+int finish_async(struct async *async)
+{
+	int ret = 0;
+
+	if (wait_or_whine(async->pid))
+		ret = error("waitpid (async) failed");
+	return ret;
+}
diff --git a/run-command.h b/run-command.h
index 35b9fb6..94e1e9d 100644
--- a/run-command.h
+++ b/run-command.h
@@ -43,4 +43,26 @@ int run_command_v_opt_cd(const char **argv, int opt, const char *dir);
  */
 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env);
 
+/*
+ * The purpose of the following functions is to feed a pipe by running
+ * a function asynchronously and providing output that the caller reads.
+ *
+ * It is expected that no synchronization and mutual exclusion between
+ * the caller and the feed function is necessary so that the function
+ * can run in a thread without interfering with the caller.
+ */
+struct async {
+	/*
+	 * proc writes to fd and closes it;
+	 * returns 0 on success, non-zero on failure
+	 */
+	int (*proc)(int fd, void *data);
+	void *data;
+	int out;	/* caller reads from here and closes it */
+	pid_t pid;
+};
+
+int start_async(struct async *async);
+int finish_async(struct async *async);
+
 #endif
-- 
1.5.3.2.141.g48f10

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* Re: [PATCH 0/14] fork/exec removal series
  2007-10-14  7:12       ` Pierre Habouzit
  2007-10-14  7:17         ` Pierre Habouzit
@ 2007-10-14 17:09         ` Johannes Schindelin
  1 sibling, 0 replies; 31+ messages in thread
From: Johannes Schindelin @ 2007-10-14 17:09 UTC (permalink / raw)
  To: Pierre Habouzit; +Cc: Shawn O. Pearce, Johannes Sixt, gitster, git

Hi,

On Sun, 14 Oct 2007, Pierre Habouzit wrote:

> On Sun, Oct 14, 2007 at 02:58:57AM +0000, Shawn O. Pearce wrote:
>
> > But we most definately *must* continue to support fork() for the async 
> > functions.  Its the most common interface available on one of our 
> > biggest platforms (UNIX).
> 
> Yeah that, and the fact that many of the git modules aren't thread-safe 
> (some modules have static buffers strbuf's or caching variables) and 
> should be used with care.

I think that was exactly the point of Shawn: expose bugs on Unix that 
would otherwise only show in msysGit.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 01/14] Change git_connect() to return a struct child_process instead of a pid_t.
  2007-10-14  9:40     ` Johannes Sixt
@ 2007-10-14 17:10       ` Johannes Schindelin
  0 siblings, 0 replies; 31+ messages in thread
From: Johannes Schindelin @ 2007-10-14 17:10 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: git, gitster

Hi,

On Sun, 14 Oct 2007, Johannes Sixt wrote:

> On Sunday 14 October 2007 02:57, Johannes Schindelin wrote:
> >
> > On Sat, 13 Oct 2007, Johannes Sixt wrote:
> > > -int finish_connect(pid_t pid)
> > > +int finish_connect(struct child_process *conn)
> > >  {
> > > -	if (pid == 0)
> > > +	if (conn == NULL)
> > >  		return 0;
> > >
> > > -	while (waitpid(pid, NULL, 0) < 0) {
> > > +	while (waitpid(conn->pid, NULL, 0) < 0) {
> > >  		if (errno != EINTR)
> > >  			return -1;
> >
> > Just for completeness' sake: could you do a free(conn); before return 
> > -1;?
> 
> I know. But the loop is going away with the next patch, so I didn't 
> bother. Can you live with that?

It'll be hard, but I'll try ;-)

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH amend 14/14] Use the asyncronous function infrastructure to run the content filter.
  2007-10-14  3:07                             ` Johannes Schindelin
  2007-10-14  9:39                               ` Johannes Sixt
@ 2007-10-14 17:14                               ` Johannes Sixt
  1 sibling, 0 replies; 31+ messages in thread
From: Johannes Sixt @ 2007-10-14 17:14 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: git, gitster

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
On Sunday 14 October 2007 05:07, Johannes Schindelin wrote:
> On Sat, 13 Oct 2007, Johannes Sixt wrote:
> >  	status = finish_command(&child_process);
> >  	if (status)
> > -		error("external filter %s failed %d", cmd, -status);
> > +		error("external filter %s failed", params->cmd);
>
> Did you mean to remove the status from the output (it should probably read
> "(exit status %d)" instead of just "%d", but an exit status can help
> identify problems, right?

I didn't mean to change the error message here.

> > +	if (start_async(&async))
> > +		return 0;	/* error was already reported */
>
> Please write "return NULL;"

This patch now does just that.

-- Hannes

 convert.c |   60 +++++++++++++++++++++++++++---------------------------------
 1 files changed, 27 insertions(+), 33 deletions(-)

diff --git a/convert.c b/convert.c
index c870817..ac04157 100644
--- a/convert.c
+++ b/convert.c
@@ -201,15 +201,21 @@ static char *crlf_to_worktree(const char *path, const char *src, unsigned long *
 	return buffer;
 }
 
-static int filter_buffer(int fd, const char *src,
-			 unsigned long size, const char *cmd)
+struct filter_params {
+	const char *src;
+	unsigned long size;
+	const char *cmd;
+};
+
+static int filter_buffer(int fd, void *data)
 {
 	/*
 	 * Spawn cmd and feed the buffer contents through its stdin.
 	 */
 	struct child_process child_process;
+	struct filter_params *params = (struct filter_params *)data;
 	int write_err, status;
-	const char *argv[] = { "sh", "-c", cmd, NULL };
+	const char *argv[] = { "sh", "-c", params->cmd, NULL };
 
 	memset(&child_process, 0, sizeof(child_process));
 	child_process.argv = argv;
@@ -217,17 +223,17 @@ static int filter_buffer(int fd, const char *src,
 	child_process.out = fd;
 
 	if (start_command(&child_process))
-		return error("cannot fork to run external filter %s", cmd);
+		return error("cannot fork to run external filter %s", params->cmd);
 
-	write_err = (write_in_full(child_process.in, src, size) < 0);
+	write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
 	if (close(child_process.in))
 		write_err = 1;
 	if (write_err)
-		error("cannot feed the input to external filter %s", cmd);
+		error("cannot feed the input to external filter %s", params->cmd);
 
 	status = finish_command(&child_process);
 	if (status)
-		error("external filter %s failed %d", cmd, -status);
+		error("external filter %s failed %d", params->cmd, -status);
 	return (write_err || status);
 }
 
@@ -241,42 +247,31 @@ static char *apply_filter(const char *path, const char *src,
 	 * (child --> cmd) --> us
 	 */
 	const int SLOP = 4096;
-	int pipe_feed[2];
-	int status;
 	char *dst;
 	unsigned long dstsize, dstalloc;
-	struct child_process child_process;
+	struct async async;
+	struct filter_params params;
 
 	if (!cmd)
 		return NULL;
 
-	memset(&child_process, 0, sizeof(child_process));
-
-	if (pipe(pipe_feed) < 0) {
-		error("cannot create pipe to run external filter %s", cmd);
-		return NULL;
-	}
+	memset(&async, 0, sizeof(async));
+	async.proc = filter_buffer;
+	async.data = &params;
+	params.src = src;
+	params.size = *sizep;
+	params.cmd = cmd;
 
 	fflush(NULL);
-	child_process.pid = fork();
-	if (child_process.pid < 0) {
-		error("cannot fork to run external filter %s", cmd);
-		close(pipe_feed[0]);
-		close(pipe_feed[1]);
-		return NULL;
-	}
-	if (!child_process.pid) {
-		close(pipe_feed[0]);
-		exit(filter_buffer(pipe_feed[1], src, *sizep, cmd));
-	}
-	close(pipe_feed[1]);
+	if (start_async(&async))
+		return NULL;	/* error was already reported */
 
 	dstalloc = *sizep;
 	dst = xmalloc(dstalloc);
 	dstsize = 0;
 
 	while (1) {
-		ssize_t numread = xread(pipe_feed[0], dst + dstsize,
+		ssize_t numread = xread(async.out, dst + dstsize,
 					dstalloc - dstsize);
 
 		if (numread <= 0) {
@@ -293,15 +288,14 @@ static char *apply_filter(const char *path, const char *src,
 			dst = xrealloc(dst, dstalloc);
 		}
 	}
-	if (close(pipe_feed[0])) {
+	if (close(async.out)) {
 		error("read from external filter %s failed", cmd);
 		free(dst);
 		dst = NULL;
 	}
 
-	status = finish_command(&child_process);
-	if (status) {
-		error("external filter %s failed %d", cmd, -status);
+	if (finish_async(&async)) {
+		error("external filter %s failed", cmd);
 		free(dst);
 		dst = NULL;
 	}
-- 
1.5.3.2.141.g48f10

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 14/14] Use the asyncronous function infrastructure to run the content filter.
  2007-10-19 19:48                         ` [PATCH 13/14] Avoid a dup2(2) in apply_filter() - start_command() can do it for us Johannes Sixt
@ 2007-10-19 19:48                           ` Johannes Sixt
  0 siblings, 0 replies; 31+ messages in thread
From: Johannes Sixt @ 2007-10-19 19:48 UTC (permalink / raw)
  To: Shawn O. Pearce; +Cc: git, Johannes Sixt

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 convert.c |   61 ++++++++++++++++++++++++++++---------------------------------
 1 files changed, 28 insertions(+), 33 deletions(-)

diff --git a/convert.c b/convert.c
index 8dc9965..4df7559 100644
--- a/convert.c
+++ b/convert.c
@@ -192,15 +192,21 @@ static int crlf_to_worktree(const char *path, const char *src, size_t len,
 	return 1;
 }
 
-static int filter_buffer(int fd, const char *src,
-			 unsigned long size, const char *cmd)
+struct filter_params {
+	const char *src;
+	unsigned long size;
+	const char *cmd;
+};
+
+static int filter_buffer(int fd, void *data)
 {
 	/*
 	 * Spawn cmd and feed the buffer contents through its stdin.
 	 */
 	struct child_process child_process;
+	struct filter_params *params = (struct filter_params *)data;
 	int write_err, status;
-	const char *argv[] = { "sh", "-c", cmd, NULL };
+	const char *argv[] = { "sh", "-c", params->cmd, NULL };
 
 	memset(&child_process, 0, sizeof(child_process));
 	child_process.argv = argv;
@@ -208,17 +214,17 @@ static int filter_buffer(int fd, const char *src,
 	child_process.out = fd;
 
 	if (start_command(&child_process))
-		return error("cannot fork to run external filter %s", cmd);
+		return error("cannot fork to run external filter %s", params->cmd);
 
-	write_err = (write_in_full(child_process.in, src, size) < 0);
+	write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
 	if (close(child_process.in))
 		write_err = 1;
 	if (write_err)
-		error("cannot feed the input to external filter %s", cmd);
+		error("cannot feed the input to external filter %s", params->cmd);
 
 	status = finish_command(&child_process);
 	if (status)
-		error("external filter %s failed %d", cmd, -status);
+		error("external filter %s failed %d", params->cmd, -status);
 	return (write_err || status);
 }
 
@@ -231,47 +237,36 @@ static int apply_filter(const char *path, const char *src, size_t len,
 	 *
 	 * (child --> cmd) --> us
 	 */
-	int pipe_feed[2];
-	int status, ret = 1;
-	struct child_process child_process;
+	int ret = 1;
 	struct strbuf nbuf;
+	struct async async;
+	struct filter_params params;
 
 	if (!cmd)
 		return 0;
 
-	memset(&child_process, 0, sizeof(child_process));
-
-	if (pipe(pipe_feed) < 0) {
-		error("cannot create pipe to run external filter %s", cmd);
-		return 0;
-	}
+	memset(&async, 0, sizeof(async));
+	async.proc = filter_buffer;
+	async.data = &params;
+	params.src = src;
+	params.size = len;
+	params.cmd = cmd;
 
 	fflush(NULL);
-	child_process.pid = fork();
-	if (child_process.pid < 0) {
-		error("cannot fork to run external filter %s", cmd);
-		close(pipe_feed[0]);
-		close(pipe_feed[1]);
-		return 0;
-	}
-	if (!child_process.pid) {
-		close(pipe_feed[0]);
-		exit(filter_buffer(pipe_feed[1], src, len, cmd));
-	}
-	close(pipe_feed[1]);
+	if (start_async(&async))
+		return 0;	/* error was already reported */
 
 	strbuf_init(&nbuf, 0);
-	if (strbuf_read(&nbuf, pipe_feed[0], len) < 0) {
+	if (strbuf_read(&nbuf, async.out, len) < 0) {
 		error("read from external filter %s failed", cmd);
 		ret = 0;
 	}
-	if (close(pipe_feed[0])) {
+	if (close(async.out)) {
 		error("read from external filter %s failed", cmd);
 		ret = 0;
 	}
-	status = finish_command(&child_process);
-	if (status) {
-		error("external filter %s failed %d", cmd, -status);
+	if (finish_async(&async)) {
+		error("external filter %s failed", cmd);
 		ret = 0;
 	}
 
-- 
1.5.3.4.315.g2ce38

^ permalink raw reply related	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2007-10-19 19:49 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-13 20:06 [PATCH 0/14] fork/exec removal series Johannes Sixt
2007-10-13 20:06 ` [PATCH 01/14] Change git_connect() to return a struct child_process instead of a pid_t Johannes Sixt
2007-10-13 20:06   ` [PATCH 02/14] Use start_command() in git_connect() instead of explicit fork/exec Johannes Sixt
2007-10-13 20:06     ` [PATCH 03/14] Use start_command() to run content filters " Johannes Sixt
2007-10-13 20:06       ` [PATCH 04/14] Use run_command() to spawn external diff programs instead of fork/exec Johannes Sixt
2007-10-13 20:06         ` [PATCH 05/14] Use start_comand() in builtin-fetch-pack.c instead of explicit fork/exec Johannes Sixt
2007-10-13 20:06           ` [PATCH 06/14] Have start_command() create a pipe to read the stderr of the child Johannes Sixt
2007-10-13 20:06             ` [PATCH 07/14] upload-pack: Use start_command() to run pack-objects in create_pack_file() Johannes Sixt
2007-10-13 20:06               ` [PATCH 08/14] Add infrastructure to run a function asynchronously Johannes Sixt
2007-10-13 20:06                 ` [PATCH 09/14] Use the asyncronous function infrastructure in builtin-fetch-pack.c Johannes Sixt
2007-10-13 20:06                   ` [PATCH 10/14] upload-pack: Move the revision walker into a separate function Johannes Sixt
2007-10-13 20:06                     ` [PATCH 11/14] upload-pack: Run rev-list in an asynchronous function Johannes Sixt
2007-10-13 20:06                       ` [PATCH 12/14] t0021-conversion.sh: Test that the clean filter really cleans content Johannes Sixt
2007-10-13 20:06                         ` [PATCH 13/14] Avoid a dup2(2) in apply_filter() - start_command() can do it for us Johannes Sixt
2007-10-13 20:06                           ` [PATCH 14/14] Use the asyncronous function infrastructure to run the content filter Johannes Sixt
2007-10-14  3:07                             ` Johannes Schindelin
2007-10-14  9:39                               ` Johannes Sixt
2007-10-14 17:14                               ` [PATCH amend " Johannes Sixt
2007-10-14 17:08                 ` [PATCH amend 08/14] Add infrastructure to run a function asynchronously Johannes Sixt
2007-10-14  0:57   ` [PATCH 01/14] Change git_connect() to return a struct child_process instead of a pid_t Johannes Schindelin
2007-10-14  9:40     ` Johannes Sixt
2007-10-14 17:10       ` Johannes Schindelin
2007-10-14  2:11 ` [PATCH 0/14] fork/exec removal series Shawn O. Pearce
2007-10-14  2:50   ` Johannes Schindelin
2007-10-14  2:58     ` Shawn O. Pearce
2007-10-14  7:12       ` Pierre Habouzit
2007-10-14  7:17         ` Pierre Habouzit
2007-10-14  7:28           ` Pierre Habouzit
2007-10-14  9:10             ` Andreas Ericsson
2007-10-14 17:09         ` Johannes Schindelin
2007-10-19 19:47 [PATCH 0/14 resend] " Johannes Sixt
2007-10-19 19:47 ` [PATCH 01/14] Change git_connect() to return a struct child_process instead of a pid_t Johannes Sixt
2007-10-19 19:47   ` [PATCH 02/14] Use start_command() in git_connect() instead of explicit fork/exec Johannes Sixt
2007-10-19 19:47     ` [PATCH 03/14] Use start_command() to run content filters " Johannes Sixt
2007-10-19 19:47       ` [PATCH 04/14] Use run_command() to spawn external diff programs instead of fork/exec Johannes Sixt
2007-10-19 19:47         ` [PATCH 05/14] Use start_comand() in builtin-fetch-pack.c instead of explicit fork/exec Johannes Sixt
2007-10-19 19:47           ` [PATCH 06/14] Have start_command() create a pipe to read the stderr of the child Johannes Sixt
2007-10-19 19:47             ` [PATCH 07/14] upload-pack: Use start_command() to run pack-objects in create_pack_file() Johannes Sixt
2007-10-19 19:48               ` [PATCH 08/14] Add infrastructure to run a function asynchronously Johannes Sixt
2007-10-19 19:48                 ` [PATCH 09/14] Use the asyncronous function infrastructure in builtin-fetch-pack.c Johannes Sixt
2007-10-19 19:48                   ` [PATCH 10/14] upload-pack: Move the revision walker into a separate function Johannes Sixt
2007-10-19 19:48                     ` [PATCH 11/14] upload-pack: Run rev-list in an asynchronous function Johannes Sixt
2007-10-19 19:48                       ` [PATCH 12/14] t0021-conversion.sh: Test that the clean filter really cleans content Johannes Sixt
2007-10-19 19:48                         ` [PATCH 13/14] Avoid a dup2(2) in apply_filter() - start_command() can do it for us Johannes Sixt
2007-10-19 19:48                           ` [PATCH 14/14] Use the asyncronous function infrastructure to run the content filter Johannes Sixt

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.