mirror of
https://github.com/systemd/systemd.git
synced 2025-03-24 14:50:17 +03:00
add generic string lookup macros
This commit is contained in:
parent
f3bff0eb3b
commit
1dccbe197c
112
util.c
112
util.c
@ -7,9 +7,14 @@
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <syslog.h>
|
||||
#include <sched.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
#include "macro.h"
|
||||
#include "util.h"
|
||||
#include "ioprio.h"
|
||||
#include "missing.h"
|
||||
|
||||
usec_t now(clockid_t clock) {
|
||||
struct timespec ts;
|
||||
@ -277,24 +282,6 @@ char *split_quoted(const char *c, size_t *l, char **state) {
|
||||
return (char*) current;
|
||||
}
|
||||
|
||||
const char *sigchld_code(int code) {
|
||||
|
||||
if (code == CLD_EXITED)
|
||||
return "exited";
|
||||
else if (code == CLD_KILLED)
|
||||
return "killed";
|
||||
else if (code == CLD_DUMPED)
|
||||
return "dumped";
|
||||
else if (code == CLD_TRAPPED)
|
||||
return "trapped";
|
||||
else if (code == CLD_STOPPED)
|
||||
return "stopped";
|
||||
else if (code == CLD_CONTINUED)
|
||||
return "continued";
|
||||
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
|
||||
int r;
|
||||
FILE *f;
|
||||
@ -876,3 +863,92 @@ char *ascii_strlower(char *path) {
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
static const char *const ioprio_class_table[] = {
|
||||
[IOPRIO_CLASS_NONE] = "none",
|
||||
[IOPRIO_CLASS_RT] = "realtime",
|
||||
[IOPRIO_CLASS_BE] = "best-effort",
|
||||
[IOPRIO_CLASS_IDLE] = "idle"
|
||||
};
|
||||
|
||||
DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
|
||||
|
||||
static const char *const sigchld_code_table[] = {
|
||||
[CLD_EXITED] = "exited",
|
||||
[CLD_KILLED] = "killed",
|
||||
[CLD_DUMPED] = "dumped",
|
||||
[CLD_TRAPPED] = "trapped",
|
||||
[CLD_STOPPED] = "stopped",
|
||||
[CLD_CONTINUED] = "continued",
|
||||
};
|
||||
|
||||
DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
|
||||
|
||||
static const char *const log_facility_table[LOG_NFACILITIES] = {
|
||||
[LOG_FAC(LOG_KERN)] = "kern",
|
||||
[LOG_FAC(LOG_USER)] = "user",
|
||||
[LOG_FAC(LOG_MAIL)] = "mail",
|
||||
[LOG_FAC(LOG_DAEMON)] = "daemon",
|
||||
[LOG_FAC(LOG_AUTH)] = "auth",
|
||||
[LOG_FAC(LOG_SYSLOG)] = "syslog",
|
||||
[LOG_FAC(LOG_LPR)] = "lpr",
|
||||
[LOG_FAC(LOG_NEWS)] = "news",
|
||||
[LOG_FAC(LOG_UUCP)] = "uucp",
|
||||
[LOG_FAC(LOG_CRON)] = "cron",
|
||||
[LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
|
||||
[LOG_FAC(LOG_FTP)] = "ftp",
|
||||
[LOG_FAC(LOG_LOCAL0)] = "local0",
|
||||
[LOG_FAC(LOG_LOCAL1)] = "local1",
|
||||
[LOG_FAC(LOG_LOCAL2)] = "local2",
|
||||
[LOG_FAC(LOG_LOCAL3)] = "local3",
|
||||
[LOG_FAC(LOG_LOCAL4)] = "local4",
|
||||
[LOG_FAC(LOG_LOCAL5)] = "local5",
|
||||
[LOG_FAC(LOG_LOCAL6)] = "local6",
|
||||
[LOG_FAC(LOG_LOCAL7)] = "local7"
|
||||
};
|
||||
|
||||
DEFINE_STRING_TABLE_LOOKUP(log_facility, int);
|
||||
|
||||
static const char *const log_level_table[] = {
|
||||
[LOG_EMERG] = "emerg",
|
||||
[LOG_ALERT] = "alert",
|
||||
[LOG_CRIT] = "crit",
|
||||
[LOG_ERR] = "err",
|
||||
[LOG_WARNING] = "warning",
|
||||
[LOG_NOTICE] = "notice",
|
||||
[LOG_INFO] = "info",
|
||||
[LOG_DEBUG] = "debug"
|
||||
};
|
||||
|
||||
DEFINE_STRING_TABLE_LOOKUP(log_level, int);
|
||||
|
||||
static const char* const sched_policy_table[] = {
|
||||
[SCHED_OTHER] = "other",
|
||||
[SCHED_BATCH] = "batch",
|
||||
[SCHED_IDLE] = "idle",
|
||||
[SCHED_FIFO] = "fifo",
|
||||
[SCHED_RR] = "rr"
|
||||
};
|
||||
|
||||
DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
|
||||
|
||||
static const char* const rlimit_table[] = {
|
||||
[RLIMIT_CPU] = "LimitCPU",
|
||||
[RLIMIT_FSIZE] = "LimitFSIZE",
|
||||
[RLIMIT_DATA] = "LimitDATA",
|
||||
[RLIMIT_STACK] = "LimitSTACK",
|
||||
[RLIMIT_CORE] = "LimitCORE",
|
||||
[RLIMIT_RSS] = "LimitRSS",
|
||||
[RLIMIT_NOFILE] = "LimitNOFILE",
|
||||
[RLIMIT_AS] = "LimitAS",
|
||||
[RLIMIT_NPROC] = "LimitNPROC",
|
||||
[RLIMIT_MEMLOCK] = "LimitMEMLOCK",
|
||||
[RLIMIT_LOCKS] = "LimitLOCKS",
|
||||
[RLIMIT_SIGPENDING] = "LimitSIGPENDING",
|
||||
[RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
|
||||
[RLIMIT_NICE] = "LimitNICE",
|
||||
[RLIMIT_RTPRIO] = "LimitRTPRIO",
|
||||
[RLIMIT_RTTIME] = "LimitRTTIME"
|
||||
};
|
||||
|
||||
DEFINE_STRING_TABLE_LOOKUP(rlimit, int);
|
||||
|
37
util.h
37
util.h
@ -83,8 +83,6 @@ char *split_quoted(const char *c, size_t *l, char **state);
|
||||
#define FOREACH_WORD_QUOTED(word, length, s, state) \
|
||||
for ((state) = NULL, (word) = split_quoted((s), &(l), &(state)); (word); (word) = split_quoted((s), &(l), &(state)))
|
||||
|
||||
const char *sigchld_code(int code);
|
||||
|
||||
pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
|
||||
|
||||
int write_one_line_file(const char *fn, const char *line);
|
||||
@ -121,4 +119,39 @@ char *ascii_strlower(char *path);
|
||||
|
||||
char *xescape(const char *s, const char *bad);
|
||||
|
||||
#define DEFINE_STRING_TABLE_LOOKUP(name,type) \
|
||||
const char *name##_to_string(type i) { \
|
||||
if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
|
||||
return NULL; \
|
||||
return name##_table[i]; \
|
||||
} \
|
||||
type name##_from_string(const char *s) { \
|
||||
type i; \
|
||||
assert(s); \
|
||||
for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
|
||||
if (streq(name##_table[i], s)) \
|
||||
return i; \
|
||||
return (type) -1; \
|
||||
} \
|
||||
struct __useless_struct_to_allow_trailing_semicolon__
|
||||
|
||||
|
||||
const char *ioprio_class_to_string(int i);
|
||||
int ioprio_class_from_string(const char *s);
|
||||
|
||||
const char *sigchld_code_to_string(int i);
|
||||
int sigchld_code_from_string(const char *s);
|
||||
|
||||
const char *log_facility_to_string(int i);
|
||||
int log_facility_from_string(const char *s);
|
||||
|
||||
const char *log_level_to_string(int i);
|
||||
int log_level_from_string(const char *s);
|
||||
|
||||
const char *sched_policy_to_string(int i);
|
||||
int sched_policy_from_string(const char *s);
|
||||
|
||||
const char *rlimit_to_string(int i);
|
||||
int rlimit_from_string(const char *s);
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user