Use start_command() in git_connect() instead of explicit fork/exec.
The child process handling is delegated to start_command() and finish_command(). Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Johannes Sixt committed
Oct 19, 2007 at 21:47 UTC
f364cb88238bbb468b0e076c53a055bcb8603b60
1 file changed
+48
-55
connect.c
+48
-55
@@ -482,11 +482,12 @@ struct child_process *git_connect(int fd[2], char *url,
482
char *host, *path = url;
483
char *end;
484
int c;
485
- int pipefd[2][2];
485
struct child_process *conn;
486
enum protocol protocol = PROTO_LOCAL;
487
int free_path = 0;
488
char *port = NULL;
489
+ const char **arg;
490
+ struct strbuf cmd;
491
492
/* Without this we cannot rely on waitpid() to tell
493
* what happened to our children.
@@ -572,59 +573,52 @@ struct child_process *git_connect(int fd[2], char *url,
573
return NULL;
574
}
575
575
- if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
576
- die("unable to create pipe pair for communication");
576
conn = xcalloc(1, sizeof(*conn));
578
- conn->pid = fork();
579
- if (conn->pid < 0)
580
- die("unable to fork");
581
- if (!conn->pid) {
582
- struct strbuf cmd;
583
-
584
- strbuf_init(&cmd, MAX_CMD_LEN);
585
- strbuf_addstr(&cmd, prog);
586
- strbuf_addch(&cmd, ' ');
587
- sq_quote_buf(&cmd, path);
588
- if (cmd.len >= MAX_CMD_LEN)
589
- die("command line too long");
590
-
591
- dup2(pipefd[1][0], 0);
592
- dup2(pipefd[0][1], 1);
593
- close(pipefd[0][0]);
594
- close(pipefd[0][1]);
595
- close(pipefd[1][0]);
596
- close(pipefd[1][1]);
597
- if (protocol == PROTO_SSH) {
598
- const char *ssh, *ssh_basename;
599
- ssh = getenv("GIT_SSH");
600
- if (!ssh) ssh = "ssh";
601
- ssh_basename = strrchr(ssh, '/');
602
- if (!ssh_basename)
603
- ssh_basename = ssh;
604
- else
605
- ssh_basename++;
577
607
- if (!port)
608
- execlp(ssh, ssh_basename, host, cmd.buf, NULL);
609
- else
610
- execlp(ssh, ssh_basename, "-p", port, host,
611
- cmd.buf, NULL);
578
+ strbuf_init(&cmd, MAX_CMD_LEN);
579
+ strbuf_addstr(&cmd, prog);
580
+ strbuf_addch(&cmd, ' ');
581
+ sq_quote_buf(&cmd, path);
582
+ if (cmd.len >= MAX_CMD_LEN)
583
+ die("command line too long");
584
+
585
+ conn->in = conn->out = -1;
586
+ conn->argv = arg = xcalloc(6, sizeof(*arg));
587
+ if (protocol == PROTO_SSH) {
588
+ const char *ssh = getenv("GIT_SSH");
589
+ if (!ssh) ssh = "ssh";
590
+
591
+ *arg++ = ssh;
592
+ if (port) {
593
+ *arg++ = "-p";
594
+ *arg++ = port;
595
}
613
- else {
614
- unsetenv(ALTERNATE_DB_ENVIRONMENT);
615
- unsetenv(DB_ENVIRONMENT);
616
- unsetenv(GIT_DIR_ENVIRONMENT);
617
- unsetenv(GIT_WORK_TREE_ENVIRONMENT);
618
- unsetenv(GRAFT_ENVIRONMENT);
619
- unsetenv(INDEX_ENVIRONMENT);
620
- execlp("sh", "sh", "-c", cmd.buf, NULL);
621
- }
622
- die("exec failed");
596
+ *arg++ = host;
597
+ }
598
+ else {
599
+ /* remove these from the environment */
600
+ const char *env[] = {
601
+ ALTERNATE_DB_ENVIRONMENT,
602
+ DB_ENVIRONMENT,
603
+ GIT_DIR_ENVIRONMENT,
604
+ GIT_WORK_TREE_ENVIRONMENT,
605
+ GRAFT_ENVIRONMENT,
606
+ INDEX_ENVIRONMENT,
607
+ NULL
608
+ };
609
+ conn->env = env;
610
+ *arg++ = "sh";
611
+ *arg++ = "-c";
612
}
624
- fd[0] = pipefd[0][0];
625
- fd[1] = pipefd[1][1];
626
- close(pipefd[0][1]);
627
- close(pipefd[1][0]);
613
+ *arg++ = cmd.buf;
614
+ *arg = NULL;
615
+
616
+ if (start_command(conn))
617
+ die("unable to fork");
618
+
619
+ fd[0] = conn->out; /* read from child's stdout */
620
+ fd[1] = conn->in; /* write to child's stdin */
621
+ strbuf_release(&cmd);
622
if (free_path)
623
free(path);
624
return conn;
@@ -632,13 +626,12 @@ struct child_process *git_connect(int fd[2], char *url,
626
627
int finish_connect(struct child_process *conn)
628
{
629
+ int code;
630
if (!conn)
631
return 0;
632
638
- while (waitpid(conn->pid, NULL, 0) < 0) {
639
- if (errno != EINTR)
640
- return -1;
641
- }
633
+ code = finish_command(conn);
634
+ free(conn->argv);
635
free(conn);
643
- return 0;
636
+ return code;
637
}