Skip to content

Commit e72ae28

Browse files
Johannes Sixtgitster
authored andcommitted
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>
1 parent 923d44a commit e72ae28

File tree

8 files changed

+9
-11
lines changed

8 files changed

+9
-11
lines changed

builtin-fetch-pack.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,10 @@ static int get_pack(int xd[2], char **pack_lockfile)
538538
cmd.git_cmd = 1;
539539
if (start_command(&cmd))
540540
die("fetch-pack: unable to fork off %s", argv[0]);
541-
if (do_keep && pack_lockfile)
541+
if (do_keep && pack_lockfile) {
542542
*pack_lockfile = index_pack_lockfile(cmd.out);
543+
close(cmd.out);
544+
}
543545

544546
if (finish_command(&cmd))
545547
die("%s failed", argv[0]);

builtin-send-pack.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static int pack_objects(int fd, struct ref *refs)
7171
refs = refs->next;
7272
}
7373

74+
close(po.in);
7475
if (finish_command(&po))
7576
return error("pack-objects died with strange error");
7677
return 0;

builtin-tag.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,13 @@ static int do_sign(struct strbuf *buffer)
226226

227227
if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
228228
close(gpg.in);
229+
close(gpg.out);
229230
finish_command(&gpg);
230231
return error("gpg did not accept the tag data");
231232
}
232233
close(gpg.in);
233-
gpg.close_in = 0;
234234
len = strbuf_read(buffer, gpg.out, 1024);
235+
close(gpg.out);
235236

236237
if (finish_command(&gpg) || !len || len < 0)
237238
return error("gpg failed to sign the tag");

builtin-verify-tag.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ static int run_gpg_verify(const char *buf, unsigned long size, int verbose)
5252

5353
write_in_full(gpg.in, buf, len);
5454
close(gpg.in);
55-
gpg.close_in = 0;
5655
ret = finish_command(&gpg);
5756

5857
unlink(path);

bundle.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ int create_bundle(struct bundle_header *header, const char *path,
333333
write_or_die(rls.in, sha1_to_hex(object->sha1), 40);
334334
write_or_die(rls.in, "\n", 1);
335335
}
336+
close(rls.in);
336337
if (finish_command(&rls))
337338
return error ("pack-objects died");
338339

receive-pack.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ static int run_hook(const char *hook_name)
132132
break;
133133
}
134134
}
135+
close(proc.in);
135136
return hook_status(finish_command(&proc), hook_name);
136137
}
137138

@@ -414,6 +415,7 @@ static const char *unpack(void)
414415
if (start_command(&ip))
415416
return "index-pack fork failed";
416417
pack_lockfile = index_pack_lockfile(ip.out);
418+
close(ip.out);
417419
status = finish_command(&ip);
418420
if (!status) {
419421
reprepare_packed_git();

run-command.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ int start_command(struct child_process *cmd)
2525
if (pipe(fdin) < 0)
2626
return -ERR_RUN_COMMAND_PIPE;
2727
cmd->in = fdin[1];
28-
cmd->close_in = 1;
2928
}
3029

3130
need_out = !cmd->no_stdout
@@ -38,7 +37,6 @@ int start_command(struct child_process *cmd)
3837
return -ERR_RUN_COMMAND_PIPE;
3938
}
4039
cmd->out = fdout[0];
41-
cmd->close_out = 1;
4240
}
4341

4442
need_err = !cmd->no_stderr && cmd->err < 0;
@@ -157,10 +155,6 @@ static int wait_or_whine(pid_t pid)
157155

158156
int finish_command(struct child_process *cmd)
159157
{
160-
if (cmd->close_in)
161-
close(cmd->in);
162-
if (cmd->close_out)
163-
close(cmd->out);
164158
return wait_or_whine(cmd->pid);
165159
}
166160

run-command.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ struct child_process {
1919
int err;
2020
const char *dir;
2121
const char *const *env;
22-
unsigned close_in:1;
23-
unsigned close_out:1;
2422
unsigned no_stdin:1;
2523
unsigned no_stdout:1;
2624
unsigned no_stderr:1;

0 commit comments

Comments
 (0)