From 2d11a35ee1975f015c1c4d7696d71a408178e5be Mon Sep 17 00:00:00 2001 From: Marouane L Date: Mon, 26 Sep 2022 12:29:14 +0100 Subject: [PATCH] Spawn external programs asynchronously Drawback is that we can't check the exitcode anymore. But we were unsure why/when we need this, see: https://github.com/profanity-im/profanity/pull/1760/files#r980868708 Fixes https://github.com/profanity-im/profanity/issues/1759 --- src/command/cmd_funcs.c | 2 +- src/common.c | 39 ++++++++++--------------------------- src/common.h | 2 +- src/tools/aesgcm_download.c | 2 +- src/tools/http_download.c | 2 +- src/xmpp/avatar.c | 2 +- 6 files changed, 15 insertions(+), 34 deletions(-) diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index e8322214..9978e889 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -9424,7 +9424,7 @@ _url_external_method(const char* cmd_template, const char* url, const char* file { gchar** argv = format_call_external_argv(cmd_template, url, filename); - if (!call_external(argv, NULL, NULL)) { + if (!call_external(argv)) { cons_show_error("Unable to call external executable for url: check the logs for more information."); } else { cons_show("URL '%s' has been called with '%s'.", url, cmd_template); diff --git a/src/common.c b/src/common.c index de35ea2d..14f87da5 100644 --- a/src/common.c +++ b/src/common.c @@ -443,44 +443,25 @@ get_mentions(gboolean whole_word, gboolean case_sensitive, const char* const mes } gboolean -call_external(gchar** argv, gchar** std_out, gchar** std_err) +call_external(gchar** argv) { - GError* spawn_error = NULL; - GError* exit_error = NULL; + GError* spawn_error; gboolean is_successful; - gint wait_status; - GSpawnFlags flags = G_SPAWN_SEARCH_PATH; - if (std_out == NULL) - flags |= G_SPAWN_STDOUT_TO_DEV_NULL; - if (std_err == NULL) - flags |= G_SPAWN_STDERR_TO_DEV_NULL; - - is_successful = g_spawn_sync(NULL, // Inherit the parent PWD. - argv, - NULL, // Inherit the parent environment. - flags, - NULL, NULL, // No func. before exec() in child. - std_out, std_err, - &wait_status, &spawn_error); + GSpawnFlags flags = G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL; + is_successful = g_spawn_async(NULL, // Inherit the parent PWD + argv, + NULL, // Inherit the parent environment + flags, + NULL, NULL, NULL, + &spawn_error); if (!is_successful) { gchar* cmd = g_strjoinv(" ", argv); - log_error("Spawning '%s' failed with with error '%s'", cmd, spawn_error ? spawn_error->message : "Unknown, spawn_error is NULL"); - ; + log_error("Spawning '%s' failed with error '%s'", cmd, spawn_error ? spawn_error->message : "Unknown, spawn_error is NULL"); g_error_free(spawn_error); g_free(cmd); - } else { - is_successful = g_spawn_check_exit_status(wait_status, &exit_error); - - if (!is_successful) { - gchar* cmd = g_strjoinv(" ", argv); - log_error("'%s' exited with error '%s'", cmd, exit_error->message); - - g_error_free(exit_error); - g_free(cmd); - } } return is_successful; diff --git a/src/common.h b/src/common.h index b97ef401..27d1684d 100644 --- a/src/common.h +++ b/src/common.h @@ -103,7 +103,7 @@ void get_file_paths_recursive(const char* directory, GSList** contents); char* get_random_string(int length); -gboolean call_external(gchar** argv, gchar** std_out, gchar** std_err); +gboolean call_external(gchar** argv); gchar** format_call_external_argv(const char* template, const char* url, const char* filename); gchar* unique_filename_from_url(const char* url, const char* path); diff --git a/src/tools/aesgcm_download.c b/src/tools/aesgcm_download.c index 45aa7b66..e7b5b42c 100644 --- a/src/tools/aesgcm_download.c +++ b/src/tools/aesgcm_download.c @@ -155,7 +155,7 @@ aesgcm_file_get(void* userdata) aesgcm_dl->filename); // TODO: Log the error. - if (!call_external(argv, NULL, NULL)) { + if (!call_external(argv)) { http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->url, "Downloading '%s' failed: Unable to call " "command '%s' with file at '%s' (%s).", diff --git a/src/tools/http_download.c b/src/tools/http_download.c index 9920caa1..5a5b8ef8 100644 --- a/src/tools/http_download.c +++ b/src/tools/http_download.c @@ -202,7 +202,7 @@ http_file_get(void* userdata) download->filename); // TODO: Log the error. - if (!call_external(argv, NULL, NULL)) { + if (!call_external(argv)) { http_print_transfer_update(download->window, download->url, "Downloading '%s' failed: Unable to call " "command '%s' with file at '%s' (%s).", diff --git a/src/xmpp/avatar.c b/src/xmpp/avatar.c index fb3b01ea..5ba2cb3b 100644 --- a/src/xmpp/avatar.c +++ b/src/xmpp/avatar.c @@ -346,7 +346,7 @@ _avatar_request_item_result_handler(xmpp_stanza_t* const stanza, void* const use } else { gchar** argv = format_call_external_argv(cmdtemplate, NULL, filename->str); - if (!call_external(argv, NULL, NULL)) { + if (!call_external(argv)) { cons_show_error("Unable to display avatar: check the logs for more information."); }