summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-07-28 10:19:53 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-07-29 15:54:53 +0200
commitf0d67dcddd4bcbe0a221a4ff4248114e5cf57dd8 (patch)
tree1abaa60f026d888f88145060c12f3b41b4b9bd03
parente1714f0250a9128a89f5610bd53064cc7c7ec960 (diff)
downloadsystemd-f0d67dcddd4bcbe0a221a4ff4248114e5cf57dd8.tar.gz
shared/exit-status: add exit_status_from_string()
-rw-r--r--src/shared/exit-status.c17
-rw-r--r--src/shared/exit-status.h1
-rw-r--r--src/test/test-exit-status.c15
3 files changed, 33 insertions, 0 deletions
diff --git a/src/shared/exit-status.c b/src/shared/exit-status.c
index 51f2380027..12880f805e 100644
--- a/src/shared/exit-status.c
+++ b/src/shared/exit-status.c
@@ -6,7 +6,9 @@
#include "exit-status.h"
#include "macro.h"
+#include "parse-util.h"
#include "set.h"
+#include "string-util.h"
const ExitStatusMapping exit_status_mappings[256] = {
/* Exit status ranges:
@@ -117,6 +119,21 @@ const char* exit_status_class(int code) {
}
}
+int exit_status_from_string(const char *s) {
+ uint8_t val;
+ int r;
+
+ for (size_t i = 0; i < ELEMENTSOF(exit_status_mappings); i++)
+ if (streq_ptr(s, exit_status_mappings[i].name))
+ return i;
+
+ r = safe_atou8(s, &val);
+ if (r < 0)
+ return r;
+
+ return val;
+}
+
bool is_clean_exit(int code, int status, ExitClean clean, ExitStatusSet *success_status) {
if (code == CLD_EXITED)
return status == 0 ||
diff --git a/src/shared/exit-status.h b/src/shared/exit-status.h
index 425f841523..24eba79f56 100644
--- a/src/shared/exit-status.h
+++ b/src/shared/exit-status.h
@@ -89,6 +89,7 @@ typedef struct ExitStatusSet {
const char* exit_status_to_string(int code, ExitStatusClass class) _const_;
const char* exit_status_class(int code) _const_;
+int exit_status_from_string(const char *s) _pure_;
typedef struct ExitStatusMapping {
const char *name;
diff --git a/src/test/test-exit-status.c b/src/test/test-exit-status.c
index bab0596580..3bcebd06a4 100644
--- a/src/test/test-exit-status.c
+++ b/src/test/test-exit-status.c
@@ -14,13 +14,28 @@ static void test_exit_status_to_string(void) {
log_info("%d: %s%s%s%s",
i, s ?: "-",
class ? " (" : "", class ?: "", class ? ")" : "");
+
+ if (s)
+ assert_se(exit_status_from_string(s) == i);
}
}
+static void test_exit_status_from_string(void) {
+ log_info("/* %s */", __func__);
+
+ assert_se(exit_status_from_string("11") == 11);
+ assert_se(exit_status_from_string("-1") == -ERANGE);
+ assert_se(exit_status_from_string("256") == -ERANGE);
+ assert_se(exit_status_from_string("foo") == -EINVAL);
+ assert_se(exit_status_from_string("SUCCESS") == 0);
+ assert_se(exit_status_from_string("FAILURE") == 1);
+}
+
int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG);
test_exit_status_to_string();
+ test_exit_status_from_string();
return 0;
}