daemon: use an argv_array to exec children
Our struct child_process already has its own argv_array. Let's use that to avoid having to format options into separate buffers. Note that we'll need to declare the child process outside of the run_service_command() helper to do this. But that opens up a further simplification, which is that the helper can append to our argument list, saving each caller from specifying "." manually. Signed-off-by: Jeff King <peff@peff.net>
Jeff King committed
Mar 28, 2017 at 15:48 UTC
6a97da396470cb85e289a4810326fd7f50062b96
1 file changed
+17
-21
daemon.c
+17
-21
@@ -449,46 +449,42 @@ static void copy_to_log(int fd)
449
fclose(fp);
450
}
451
452
-static int run_service_command(const char **argv)
452
+static int run_service_command(struct child_process *cld)
453
{
454
- struct child_process cld = CHILD_PROCESS_INIT;
455
-
456
- cld.argv = argv;
457
- cld.git_cmd = 1;
458
- cld.err = -1;
459
- if (start_command(&cld))
454
+ argv_array_push(&cld->args, ".");
455
+ cld->git_cmd = 1;
456
+ cld->err = -1;
457
+ if (start_command(cld))
458
return -1;
459
460
close(0);
461
close(1);
462
465
- copy_to_log(cld.err);
463
+ copy_to_log(cld->err);
464
467
- return finish_command(&cld);
465
+ return finish_command(cld);
466
}
467
468
static int upload_pack(void)
469
{
472
- /* Timeout as string */
473
- char timeout_buf[64];
474
- const char *argv[] = { "upload-pack", "--strict", NULL, ".", NULL };
475
-
476
- argv[2] = timeout_buf;
477
-
478
- snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
479
- return run_service_command(argv);
470
+ struct child_process cld = CHILD_PROCESS_INIT;
471
+ argv_array_pushl(&cld.args, "upload-pack", "--strict", NULL);
472
+ argv_array_pushf(&cld.args, "--timeout=%u", timeout);
473
+ return run_service_command(&cld);
474
}
475
476
static int upload_archive(void)
477
{
484
- static const char *argv[] = { "upload-archive", ".", NULL };
485
- return run_service_command(argv);
478
+ struct child_process cld = CHILD_PROCESS_INIT;
479
+ argv_array_push(&cld.args, "upload-archive");
480
+ return run_service_command(&cld);
481
}
482
483
static int receive_pack(void)
484
{
490
- static const char *argv[] = { "receive-pack", ".", NULL };
491
- return run_service_command(argv);
485
+ struct child_process cld = CHILD_PROCESS_INIT;
486
+ argv_array_push(&cld.args, "receive-pack");
487
+ return run_service_command(&cld);
488
}
489
490
static struct daemon_service daemon_service[] = {