glib-aux: add nm_utils_get_process_exit_status_desc_buf() helper

This commit is contained in:
Thomas Haller 2022-09-27 18:05:51 +02:00
parent cd2f8945c9
commit 95e6ebec66
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 33 additions and 10 deletions

View file

@ -6100,19 +6100,38 @@ nm_crypto_md5_hash(const guint8 *salt,
/*****************************************************************************/
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))
nm_strbuf_append(&buf, &buf_len, "exited with status %d", WEXITSTATUS(status));
else if (WIFSIGNALED(status))
nm_strbuf_append(&buf, &buf_len, "killed by signal %d", WTERMSIG(status));
else if (WIFSTOPPED(status))
nm_strbuf_append(&buf, &buf_len, "stopped by signal %d", WSTOPSIG(status));
else if (WIFCONTINUED(status))
nm_strbuf_append(&buf, &buf_len, "resumed by SIGCONT");
else
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)
{
if (WIFEXITED(status))
return g_strdup_printf("exited with status %d", WEXITSTATUS(status));
else if (WIFSIGNALED(status))
return g_strdup_printf("killed by signal %d", WTERMSIG(status));
else if (WIFSTOPPED(status))
return g_strdup_printf("stopped by signal %d", WSTOPSIG(status));
else if (WIFCONTINUED(status))
return g_strdup("resumed by SIGCONT)");
else
return g_strdup_printf("exited with unknown status 0x%x", 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);
}
/*****************************************************************************/

View file

@ -3067,6 +3067,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);