1#include "cache.h"
2#include "exec_cmd.h"
3#include "pkt-line.h"
4#include "sideband.h"
5#include <sys/wait.h>
6
7static pid_t setup_sideband(int sideband, const char *me, int fd[2], int xd[2])
8{
9 pid_t side_pid;
10
11 if (!sideband) {
12 fd[0] = xd[0];
13 fd[1] = xd[1];
14 return 0;
15 }
16 /* xd[] is talking with upload-pack; subprocess reads from
17 * xd[0], spits out band#2 to stderr, and feeds us band#1
18 * through our fd[0].
19 */
20 if (pipe(fd) < 0)
21 die("%s: unable to set up pipe", me);
22 side_pid = fork();
23 if (side_pid < 0)
24 die("%s: unable to fork off sideband demultiplexer", me);
25 if (!side_pid) {
26 /* subprocess */
27 close(fd[0]);
28 if (xd[0] != xd[1])
29 close(xd[1]);
30 if (recv_sideband(me, xd[0], fd[1], 2))
31 exit(1);
32 exit(0);
33 }
34 close(xd[0]);
35 close(fd[1]);
36 fd[1] = xd[1];
37 return side_pid;
38}
39
40static int get_pack(int xd[2], const char *me, int sideband, const char **argv)
41{
42 int status;
43 pid_t pid, side_pid;
44 int fd[2];
45
46 side_pid = setup_sideband(sideband, me, fd, xd);
47 pid = fork();
48 if (pid < 0)
49 die("%s: unable to fork off git-unpack-objects", me);
50 if (!pid) {
51 dup2(fd[0], 0);
52 close(fd[0]);
53 close(fd[1]);
54 execv_git_cmd(argv);
55 die("%s exec failed", argv[0]);
56 }
57 close(fd[0]);
58 close(fd[1]);
59 while (waitpid(pid, &status, 0) < 0) {
60 if (errno != EINTR)
61 die("waiting for %s: %s", argv[0], strerror(errno));
62 }
63 if (WIFEXITED(status)) {
64 int code = WEXITSTATUS(status);
65 if (code)
66 die("%s died with error code %d", argv[0], code);
67 return 0;
68 }
69 if (WIFSIGNALED(status)) {
70 int sig = WTERMSIG(status);
71 die("%s died of signal %d", argv[0], sig);
72 }
73 die("%s died of unnatural causes %d", argv[0], status);
74}
75
76int receive_unpack_pack(int xd[2], const char *me, int quiet, int sideband)
77{
78 const char *argv[3] = { "unpack-objects", quiet ? "-q" : NULL, NULL };
79 return get_pack(xd, me, sideband, argv);
80}
81
82int receive_keep_pack(int xd[2], const char *me, int quiet, int sideband)
83{
84 const char *argv[5] = { "index-pack", "--stdin", "--fix-thin",
85 quiet ? NULL : "-v", NULL };
86 return get_pack(xd, me, sideband, argv);
87}