git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 3/5] run-command: Elaborate execvp error checking

From
Johannes Sixt <j6t@kdbg.org>
Date
Jan 25, 2012, 19:03 UTC
Message-ID
<4F205212.5080007@kdbg.org>
In-Reply-To
<1327444346-6243-4-git-send-email-fransklaver@gmail.com>
Am 24.01.2012 23:32, schrieb Frans Klaver:
Show 13 quoted lines
> +static void inspect_failure(const char *argv0, int silent_exec_failure)
> +{
> +	int err = errno;
> +	struct strbuf sb = STRBUF_INIT;
> +
> +	/* errors not related to path */
> +	if (errno == E2BIG || errno == ENOMEM)
> +		die_file_error(argv0, err);
> +
> +	if (strchr(argv0, '/')) {
> +		if (file_exists(argv0)) {
> +			strbuf_add(&sb, argv0, strlen(argv0));
> +			inspect_file(&sb, err, argv0);

Can we end up here if errno == ENOENT? If so, silent_exec_failure must be checked. (inspect_file does not return.)

Show 59 quoted lines
> +		}
> +	} else {
> +		char *path, *next;
> +		path = getenv("PATH");
> +		while (path) {
> +			next = strchrnul(path, ':');
> +			if (path < next)
> +				strbuf_add(&sb, path, next - path);
> +			else
> +				strbuf_addch(&sb, '.');
> +
> +			if (!*next)
> +				path = NULL;
> +			else
> +				path = next + 1;
> +
> +			if (!is_searchable(sb.buf)) {
> +				strbuf_release(&sb);
> +				continue;
> +			}
> +
> +			if (sb.len && sb.buf[sb.len - 1] != '/')
> +				strbuf_addch(&sb, '/');
> +			strbuf_addstr(&sb, argv0);
> +
> +			if (file_exists(sb.buf))
> +				inspect_file(&sb, err, argv0);
> +
> +			strbuf_release(&sb);
> +		}
> +	}
> +
> +	if (err == ENOENT) {
> +		if (!silent_exec_failure)
> +			error("cannot exec '%s': %s", argv0,
> +					strerror(ENOENT));
> +		exit(127);
> +	} else {
> +		die_file_error(argv0, err);
> +	}
> +}
> +#endif
> +
>  int start_command(struct child_process *cmd)
>  {
>  	int need_in, need_out, need_err;
> @@ -280,14 +415,7 @@ fail_pipe:
>  		} else {
>  			execvp(cmd->argv[0], (char *const*) cmd->argv);
>  		}
> -		if (errno == ENOENT) {
> -			if (!cmd->silent_exec_failure)
> -				error("cannot run %s: %s", cmd->argv[0],
> -					strerror(ENOENT));
> -			exit(127);
> -		} else {
> -			die_errno("cannot exec '%s'", cmd->argv[0]);
> -		}
> +		inspect_failure(cmd->argv[0], cmd->silent_exec_failure);

Isn't it important that this function calls exit(127) if we want silent_exec_failure and errno == ENOENT? But I don't see that this guaranteed by inspect_failure; see above.

>  	}
>  	if (cmd->pid < 0)
>  		error("cannot fork() for %s: %s", cmd->argv[0],
-- Hannes
Previous: Frans KlaverNext: Frans Klaver
Message 17 of 31 in “Add execvp failure diagnostics”
  1. 0/6 Add execvp failure diagnosticsFrans Klaver, Jan 24, 2012
  2. 1/5 t0061: Fix incorrect indentationFrans Klaver, Jan 24, 2012
  3. Junio C HamanoJan 24, 2012
  4. Jonathan NiederJan 24, 2012
  5. Frans KlaverJan 25, 2012
  6. Junio C HamanoJan 25, 2012
  7. Frans KlaverJan 25, 2012
  8. Frans KlaverJan 25, 2012
  9. 2/5 t0061: Add testsFrans Klaver, Jan 24, 2012
  10. Jonathan NiederJan 24, 2012
  11. Frans KlaverJan 25, 2012
  12. 3/5 run-command: Elaborate execvp error checkingFrans Klaver, Jan 24, 2012
  13. Jonathan NiederJan 24, 2012
  14. Frans KlaverJan 25, 2012
  15. Jonathan NiederJan 25, 2012
  16. Frans KlaverJan 25, 2012
  17. Johannes SixtJan 25, 2012
  18. Frans KlaverJan 25, 2012
  19. 4/5 run-command: Warn if PATH entry cannot be searchedFrans Klaver, Jan 24, 2012
  20. 5/5 run-command: Error out if interpreter not foundFrans Klaver, Jan 24, 2012
  21. Jonathan NiederJan 24, 2012
  22. Frans KlaverJan 25, 2012
  23. Johannes SixtJan 25, 2012
  24. Frans KlaverJan 25, 2012
  25. Junio C HamanoJan 26, 2012
  26. Frans KlaverJan 27, 2012
  27. Jonathan NiederJan 27, 2012
  28. Frans KlaverJan 27, 2012
  29. Jonathan NiederJan 27, 2012
  30. Frans KlaverJan 27, 2012
  31. Frans KlaverFeb 4, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.