From d259dacf5bcf2bc8be64d53ad01a88afb478a00a Mon Sep 17 00:00:00 2001 From: Julio Montes Date: Tue, 24 Nov 2015 10:34:58 -0600 Subject: [PATCH] reimplement exec_task function --- src/lib.c | 44 +++++++++++++++++++++++++++++++++++++------- src/lib.h | 2 +- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/lib.c b/src/lib.c index 1e2582e..670de13 100644 --- a/src/lib.c +++ b/src/lib.c @@ -62,13 +62,43 @@ void LOG(const char *fmt, ...) { va_end(args); } -void exec_task(const gchar* task) { - /* - * execute a process externally in a secured and limited enviroment, - * handle return values gracefully - */ - LOG(MOD "Executing: %s\n", task); - system(task); +bool exec_task(const gchar* command_line) { + gchar* standard_output = NULL; + gchar* standard_error = NULL; + GError* error = NULL; + gint exit_status = 0; + gboolean result; + + LOG(MOD "Executing: %s\n", command_line); + result = g_spawn_command_line_sync(command_line, + &standard_output, + &standard_error, + &exit_status, + &error); + + if (!result || exit_status != 0) { + LOG(MOD "Command failed\n"); + if (error) { + LOG(MOD "Error: %s\n", (char*)error->message); + } + if (standard_error) { + LOG(MOD "STD Error: %s\n", (char*)standard_error); + } + } + + if (standard_output) { + g_free(standard_output); + } + + if (standard_error) { + g_free(standard_error); + } + + if (error) { + g_error_free(error); + } + + return result; } int make_dir(const char* pathname, mode_t mode) { diff --git a/src/lib.h b/src/lib.h index 7100498..6f43df1 100644 --- a/src/lib.h +++ b/src/lib.h @@ -48,7 +48,7 @@ #define cloud_config_dump(...) #endif /* DEBUG */ -void exec_task(const gchar* task); +gboolean exec_task(const gchar* command_line); void LOG(const char *fmt, ...) __attribute__((format(printf, 1, 2))); int make_dir(const char* pathname, mode_t mode); int chown_path(const char* pathname, const char* ownername, const char* groupname);