upload-archive: monitor child communication more carefully.
Franck noticed that the code around polling and relaying messages from the child process was quite bogus. Here is an attempt to clean it up a bit, based on his patch: - When POLLHUP is set, it goes ahead and reads the file descriptor. Worse yet, it does not check the return value of read() for errors when it does. - When we processed one POLLIN, we should just go back and see if any more data is available. We can check if the child is still there when poll gave control back at us but without any actual input. [jc: with simplification suggested by Franck. ] Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 12, 2006 at 00:26 UTC
d3788e19e20cd14aeac99d1f294d9a368437284f
1 file changed
+38
-22
builtin-upload-archive.c
+38
-22
@@ -16,6 +16,9 @@ static const char upload_archive_usage[] =
16
static const char deadchild[] =
17
"git-upload-archive: archiver died with error";
18
19
+static const char lostchild[] =
20
+"git-upload-archive: archiver process was lost";
21
+
22
23
static int run_upload_archive(int argc, const char **argv, const char *prefix)
24
{
@@ -73,6 +76,31 @@ static int run_upload_archive(int argc, const char **argv, const char *prefix)
76
return ar.write_archive(&ar.args);
77
}
78
79
+static void error_clnt(const char *fmt, ...)
80
+{
81
+ char buf[1024];
82
+ va_list params;
83
+ int len;
84
+
85
+ va_start(params, fmt);
86
+ len = vsprintf(buf, fmt, params);
87
+ va_end(params);
88
+ send_sideband(1, 3, buf, len, LARGE_PACKET_MAX);
89
+ die("sent error to the client: %s", buf);
90
+}
91
+
92
+static void process_input(int child_fd, int band)
93
+{
94
+ char buf[16384];
95
+ ssize_t sz = read(child_fd, buf, sizeof(buf));
96
+ if (sz < 0) {
97
+ if (errno != EINTR)
98
+ error_clnt("read error: %s\n", strerror(errno));
99
+ return;
100
+ }
101
+ send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
102
+}
103
+
104
int cmd_upload_archive(int argc, const char **argv, const char *prefix)
105
{
106
pid_t writer;
@@ -112,9 +140,6 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)
140
141
while (1) {
142
struct pollfd pfd[2];
115
- char buf[16384];
116
- ssize_t sz;
117
- pid_t pid;
143
int status;
144
145
pfd[0].fd = fd1[0];
@@ -129,28 +154,19 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)
154
}
155
continue;
156
}
132
- if (pfd[0].revents & (POLLIN|POLLHUP)) {
157
+ if (pfd[0].revents & POLLIN)
158
/* Data stream ready */
134
- sz = read(pfd[0].fd, buf, sizeof(buf));
135
- send_sideband(1, 1, buf, sz, LARGE_PACKET_MAX);
136
- }
137
- if (pfd[1].revents & (POLLIN|POLLHUP)) {
159
+ process_input(pfd[0].fd, 1);
160
+ if (pfd[1].revents & POLLIN)
161
/* Status stream ready */
139
- sz = read(pfd[1].fd, buf, sizeof(buf));
140
- send_sideband(1, 2, buf, sz, LARGE_PACKET_MAX);
141
- }
142
-
143
- if (((pfd[0].revents | pfd[1].revents) & POLLHUP) == 0)
144
- continue;
145
- /* did it die? */
146
- pid = waitpid(writer, &status, WNOHANG);
147
- if (!pid) {
148
- fprintf(stderr, "Hmph, HUP?\n");
162
+ process_input(pfd[1].fd, 2);
163
+ if ((pfd[0].revents | pfd[1].revents) == POLLIN)
164
continue;
150
- }
151
- if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
152
- send_sideband(1, 3, deadchild, strlen(deadchild),
153
- LARGE_PACKET_MAX);
165
+
166
+ if (waitpid(writer, &status, 0) < 0)
167
+ error_clnt("%s", lostchild);
168
+ else if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
169
+ error_clnt("%s", deadchild);
170
packet_flush(1);
171
break;
172
}