[pacman-dev] alpm: Add better logging when a command fails to execute correctly

Message ID 20200523115537.73230-1-daan.j.demeyer@gmail.com
State Superseded, archived
Headers show
Series [pacman-dev] alpm: Add better logging when a command fails to execute correctly | expand

Commit Message

Daan De Meyer May 23, 2020, 11:55 a.m. UTC
This makes debugging hook failures when running pacman less of a
hassle. Instead of a generic "command failed to execute correctly",
we now get the exact command and its arguments, as well as the exit
code of the command logged.

Signed-off-by: Daan De Meyer <daan.j.demeyer@gmail.com>
---
 lib/libalpm/util.c | 46 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)

Patch

diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index ead03004..299e1954 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -568,6 +568,42 @@  static void _alpm_reset_signals(void)
 	}
 }
 
+static char *strv_join(char *const *strv, const char *separator)
+{
+	char *const *i;
+	char *r, *p;
+	size_t size = 0;
+
+	if (!separator) {
+		separator = " ";
+	}
+
+	for (i = strv; *i; i++) {
+		if (i != strv) {
+			size += strlen(separator);
+		}
+
+		size += strlen(*i);
+	}
+
+	r = malloc(size + 1);
+	if (r == NULL) {
+		return NULL;
+	}
+
+	p = r;
+
+	for (i = strv; *i; i++) {
+		if (i != strv) {
+			p = stpcpy(p, separator);
+		}
+
+		p = stpcpy(p, *i);
+	}
+
+	return r;
+}
+
 /** Execute a command with arguments in a chroot.
  * @param handle the context handle
  * @param cmd command to execute
@@ -745,7 +781,15 @@  int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[],
 		if(WIFEXITED(status)) {
 			_alpm_log(handle, ALPM_LOG_DEBUG, "call to waitpid succeeded\n");
 			if(WEXITSTATUS(status) != 0) {
-				_alpm_log(handle, ALPM_LOG_ERROR, _("command failed to execute correctly\n"));
+				char *argv_str = strv_join(argv, NULL);
+				if (argv_str == NULL) {
+					goto cleanup;
+				}
+
+				_alpm_log(handle, ALPM_LOG_ERROR,
+							_("command \"%s\" failed to execute correctly: %i\n"),
+							argv_str, WEXITSTATUS(status));
+				free(argv_str);
 				retval = 1;
 			}
 		} else if(WIFSIGNALED(status) != 0) {