@matheusbsbs / matheus-bagliano / commits / e72ae28895
Fork 0

start_command(), .in/.out/.err = -1: Callers must close the file descriptor

By setting .in, .out, or .err members of struct child_process to -1, the callers of start_command() can request that a pipe is allocated that talks to the child process and one end is returned by replacing -1 with the file descriptor. Previously, a flag was set (for .in and .out, but not .err) to signal finish_command() to close the pipe end that start_command() had handed out, so it was optional for callers to close the pipe, and many already do so. Now we make it mandatory to close the pipe. Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Sixt committed Feb 16, 2008 at 18:36 UTC e72ae28895b22052b7ca2eef36c039ac62671f7d
8 files changed +9 -11
builtin-fetch-pack.c
+3 -1
@@ -538,8 +538,10 @@ static int get_pack(int xd[2], char **pack_lockfile)
538 cmd.git_cmd = 1;
539 if (start_command(&cmd))
540 die("fetch-pack: unable to fork off %s", argv[0]);
541 - if (do_keep && pack_lockfile)
541 + if (do_keep && pack_lockfile) {
542 *pack_lockfile = index_pack_lockfile(cmd.out);
543 + close(cmd.out);
544 + }
545
546 if (finish_command(&cmd))
547 die("%s failed", argv[0]);
builtin-send-pack.c
+1
@@ -71,6 +71,7 @@ static int pack_objects(int fd, struct ref *refs)
71 refs = refs->next;
72 }
73
74 + close(po.in);
75 if (finish_command(&po))
76 return error("pack-objects died with strange error");
77 return 0;
builtin-tag.c
+2 -1
@@ -226,12 +226,13 @@ static int do_sign(struct strbuf *buffer)
226
227 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
228 close(gpg.in);
229 + close(gpg.out);
230 finish_command(&gpg);
231 return error("gpg did not accept the tag data");
232 }
233 close(gpg.in);
233 - gpg.close_in = 0;
234 len = strbuf_read(buffer, gpg.out, 1024);
235 + close(gpg.out);
236
237 if (finish_command(&gpg) || !len || len < 0)
238 return error("gpg failed to sign the tag");
builtin-verify-tag.c
-1
@@ -52,7 +52,6 @@ static int run_gpg_verify(const char *buf, unsigned long size, int verbose)
52
53 write_in_full(gpg.in, buf, len);
54 close(gpg.in);
55 - gpg.close_in = 0;
55 ret = finish_command(&gpg);
56
57 unlink(path);
bundle.c
+1
@@ -333,6 +333,7 @@ int create_bundle(struct bundle_header *header, const char *path,
333 write_or_die(rls.in, sha1_to_hex(object->sha1), 40);
334 write_or_die(rls.in, "\n", 1);
335 }
336 + close(rls.in);
337 if (finish_command(&rls))
338 return error ("pack-objects died");
339
receive-pack.c
+2
@@ -132,6 +132,7 @@ static int run_hook(const char *hook_name)
132 break;
133 }
134 }
135 + close(proc.in);
136 return hook_status(finish_command(&proc), hook_name);
137 }
138
@@ -414,6 +415,7 @@ static const char *unpack(void)
415 if (start_command(&ip))
416 return "index-pack fork failed";
417 pack_lockfile = index_pack_lockfile(ip.out);
418 + close(ip.out);
419 status = finish_command(&ip);
420 if (!status) {
421 reprepare_packed_git();
run-command.c
-6
@@ -25,7 +25,6 @@ int start_command(struct child_process *cmd)
25 if (pipe(fdin) < 0)
26 return -ERR_RUN_COMMAND_PIPE;
27 cmd->in = fdin[1];
28 - cmd->close_in = 1;
28 }
29
30 need_out = !cmd->no_stdout
@@ -38,7 +37,6 @@ int start_command(struct child_process *cmd)
37 return -ERR_RUN_COMMAND_PIPE;
38 }
39 cmd->out = fdout[0];
41 - cmd->close_out = 1;
40 }
41
42 need_err = !cmd->no_stderr && cmd->err < 0;
@@ -157,10 +155,6 @@ static int wait_or_whine(pid_t pid)
155
156 int finish_command(struct child_process *cmd)
157 {
160 - if (cmd->close_in)
161 - close(cmd->in);
162 - if (cmd->close_out)
163 - close(cmd->out);
158 return wait_or_whine(cmd->pid);
159 }
160
run-command.h
-2
@@ -19,8 +19,6 @@ struct child_process {
19 int err;
20 const char *dir;
21 const char *const *env;
22 - unsigned close_in:1;
23 - unsigned close_out:1;
22 unsigned no_stdin:1;
23 unsigned no_stdout:1;
24 unsigned no_stderr:1;