mirror of
https://github.com/Dasharo/systemd.git
synced 2026-03-06 15:02:31 -08:00
shared/exit-status: add exit_status_from_string()
This commit is contained in:
@@ -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 ||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user