Subject: Re: [PATCH 1/5] run-command: convert sane_execvp to sane_execvpe

From: Brandon Williams <[email-masked]>
Date: Wed, 12 Apr 2017 12:22:10 -0700

On 04/10, Brandon Williams wrote:
> Convert 'sane_execvp()' to 'sane_execvpe()' which optionally takes a
> pointer to an array of 'char *' which should be used as the environment
> for the process being exec'd.  If no environment is provided (by passing
> NULL instead) then the already existing environment, as stored in
> 'environ', will be used.

Turns out we probably can't use execvpe since it isn't portable.  From
some of the other discussion it makes more sense to just move to using
execve instead.

> 
> Signed-off-by: Brandon Williams <[email-masked]>
> ---
>  cache.h       |  3 +--
>  exec_cmd.c    |  2 +-
>  run-command.c | 15 ++++++++++-----
>  3 files changed, 12 insertions(+), 8 deletions(-)
> 
> diff --git a/cache.h b/cache.h
> index 5c8078291..10d40ecae 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -2185,8 +2185,7 @@ int checkout_fast_forward(const unsigned char *from,
>  			  const unsigned char *to,
>  			  int overwrite_ignore);
>  
> -
> -int sane_execvp(const char *file, char *const argv[]);
> +int sane_execvpe(const char *file, char *const argv[], char *const envp[]);
>  
>  /*
>   * A struct to encapsulate the concept of whether a file has changed
> diff --git a/exec_cmd.c b/exec_cmd.c
> index fb94aeba9..c375f354d 100644
> --- a/exec_cmd.c
> +++ b/exec_cmd.c
> @@ -118,7 +118,7 @@ int execv_git_cmd(const char **argv) {
>  	trace_argv_printf(nargv.argv, "trace: exec:");
>  
>  	/* execvp() can only ever return if it fails */
> -	sane_execvp("git", (char **)nargv.argv);
> +	sane_execvpe("git", (char **)nargv.argv, NULL);
>  
>  	trace_printf("trace: exec failed: %s\n", strerror(errno));
>  
> diff --git a/run-command.c b/run-command.c
> index 574b81d3e..682bc3ca5 100644
> --- a/run-command.c
> +++ b/run-command.c
> @@ -168,10 +168,15 @@ static int exists_in_PATH(const char *file)
>  	return r != NULL;
>  }
>  
> -int sane_execvp(const char *file, char * const argv[])
> +int sane_execvpe(const char *file, char * const argv[], char *const envp[])
>  {
> -	if (!execvp(file, argv))
> -		return 0; /* cannot happen ;-) */
> +	if (envp) {
> +		if (!execvpe(file, argv, envp))
> +			return 0; /* cannot happen ;-) */
> +	} else {
> +		if (!execvp(file, argv))
> +			return 0; /* cannot happen ;-) */
> +	}
>  
>  	/*
>  	 * When a command can't be found because one of the directories
> @@ -226,7 +231,7 @@ static int execv_shell_cmd(const char **argv)
>  	struct argv_array nargv = ARGV_ARRAY_INIT;
>  	prepare_shell_cmd(&nargv, argv);
>  	trace_argv_printf(nargv.argv, "trace: exec:");
> -	sane_execvp(nargv.argv[0], (char **)nargv.argv);
> +	sane_execvpe(nargv.argv[0], (char **)nargv.argv, NULL);
>  	argv_array_clear(&nargv);
>  	return -1;
>  }
> @@ -442,7 +447,7 @@ int start_command(struct child_process *cmd)
>  		else if (cmd->use_shell)
>  			execv_shell_cmd(cmd->argv);
>  		else
> -			sane_execvp(cmd->argv[0], (char *const*) cmd->argv);
> +			sane_execvpe(cmd->argv[0], (char *const*) cmd->argv, NULL);
>  		if (errno == ENOENT) {
>  			if (!cmd->silent_exec_failure)
>  				error("cannot run %s: %s", cmd->argv[0],
> -- 
> 2.12.2.715.g7642488e1d-goog
> 

-- 
Brandon Williams