diff options
author | Thomas Haller <thaller@redhat.com> | 2022-09-27 18:05:51 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2022-09-29 16:04:16 +0200 |
commit | f3f99e03f559e10bd282c5f1ddffc4415bb92bc0 (patch) | |
tree | e9637be425374db807c9cd79aad1a1c65ffdc1ff | |
parent | 51b9f0ad4c7d620e4bdee922362f1d14e36b42d2 (diff) | |
download | NetworkManager-f3f99e03f559e10bd282c5f1ddffc4415bb92bc0.tar.gz |
glib-aux: add nm_utils_get_process_exit_status_desc_buf() helper
(cherry picked from commit 95e6ebec66f173c8b958ebbbbfb745fa41d5f83c)
(cherry picked from commit 431c219ad195cc1999ac326ea410240d1c7dba69)
-rw-r--r-- | src/libnm-glib-aux/nm-shared-utils.c | 33 | ||||
-rw-r--r-- | src/libnm-glib-aux/nm-shared-utils.h | 4 |
2 files changed, 30 insertions, 7 deletions
diff --git a/src/libnm-glib-aux/nm-shared-utils.c b/src/libnm-glib-aux/nm-shared-utils.c index 70db29f2b1..d5562233c7 100644 --- a/src/libnm-glib-aux/nm-shared-utils.c +++ b/src/libnm-glib-aux/nm-shared-utils.c @@ -6574,19 +6574,38 @@ nm_crypto_md5_hash(const guint8 *salt, /*****************************************************************************/ -char * -nm_utils_get_process_exit_status_desc(int status) +const char * +nm_utils_get_process_exit_status_desc_buf(int status, char *buf, gsize buf_len) { + const char *buf0 = buf; + + nm_assert(buf_len == 0 || buf); + + /* This should give a partial sentence, it it can be combined with + * prinft("command XYZ %s.\n", desc) */ + if (WIFEXITED(status)) - return g_strdup_printf("exited with status %d", WEXITSTATUS(status)); + nm_strbuf_append(&buf, &buf_len, "exited with status %d", WEXITSTATUS(status)); else if (WIFSIGNALED(status)) - return g_strdup_printf("killed by signal %d", WTERMSIG(status)); + nm_strbuf_append(&buf, &buf_len, "killed by signal %d", WTERMSIG(status)); else if (WIFSTOPPED(status)) - return g_strdup_printf("stopped by signal %d", WSTOPSIG(status)); + nm_strbuf_append(&buf, &buf_len, "stopped by signal %d", WSTOPSIG(status)); else if (WIFCONTINUED(status)) - return g_strdup("resumed by SIGCONT)"); + nm_strbuf_append(&buf, &buf_len, "resumed by SIGCONT"); else - return g_strdup_printf("exited with unknown status 0x%x", status); + nm_strbuf_append(&buf, &buf_len, "exited with unknown status 0x%x", status); + + return buf0; +} + +char * +nm_utils_get_process_exit_status_desc(int status) +{ + char buf[NM_UTILS_GET_PROCESS_EXIT_STATUS_BUF_LEN]; + + nm_utils_get_process_exit_status_desc_buf(status, buf, sizeof(buf)); + + return g_strdup(buf); } /*****************************************************************************/ diff --git a/src/libnm-glib-aux/nm-shared-utils.h b/src/libnm-glib-aux/nm-shared-utils.h index f17b16460b..ad1d37dcd4 100644 --- a/src/libnm-glib-aux/nm-shared-utils.h +++ b/src/libnm-glib-aux/nm-shared-utils.h @@ -3299,6 +3299,10 @@ void nm_crypto_md5_hash(const guint8 *salt, /*****************************************************************************/ +#define NM_UTILS_GET_PROCESS_EXIT_STATUS_BUF_LEN 41 + +const char *nm_utils_get_process_exit_status_desc_buf(int status, char *buf, gsize buf_len); + char *nm_utils_get_process_exit_status_desc(int status); gboolean nm_utils_validate_hostname(const char *hostname); |