X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=util.c;h=b54c24a3dcd9efaecbe70ddb39efe8129b3eabd7;hp=19375aebca7aec07ace039d182239c7ce74d026a;hb=ef886c6ab86436f0d8b5128799a8f801c62d859a;hpb=4fe88d28a4cfa504c1f2362d4a7030fae4c4af29 diff --git a/util.c b/util.c index 19375aebc..b54c24a3d 100644 --- a/util.c +++ b/util.c @@ -7,9 +7,15 @@ #include #include #include +#include +#include +#include +#include #include "macro.h" #include "util.h" +#include "ioprio.h" +#include "missing.h" usec_t now(clockid_t clock) { struct timespec ts; @@ -83,6 +89,25 @@ bool startswith(const char *s, const char *prefix) { return memcmp(s, prefix, pl) == 0; } +bool first_word(const char *s, const char *word) { + size_t sl, wl; + + assert(s); + assert(word); + + sl = strlen(s); + wl = strlen(word); + + if (sl < wl) + return false; + + if (memcmp(s, word, wl) != 0) + return false; + + return (s[wl] == 0 || + strchr(WHITESPACE, s[wl])); +} + int close_nointr(int fd) { assert(fd >= 0); @@ -277,24 +302,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; @@ -560,10 +567,10 @@ int unhexchar(char c) { return c - '0'; if (c >= 'a' && c <= 'f') - return c - 'a'; + return c - 'a' + 10; if (c >= 'A' && c <= 'F') - return c - 'A'; + return c - 'A' + 10; return -1; } @@ -797,6 +804,66 @@ char *xescape(const char *s, const char *bad) { return r; } +char *bus_path_escape(const char *s) { + assert(s); + + char *r, *t; + const char *f; + + /* Escapes all chars that D-Bus' object path cannot deal + * with. Can be reverse with bus_path_unescape() */ + + if (!(r = new(char, strlen(s)*3+1))) + return NULL; + + for (f = s, t = r; *f; f++) { + + if (!(*f >= 'A' && *f <= 'Z') && + !(*f >= 'a' && *f <= 'z') && + !(*f >= '0' && *f <= '9')) { + *(t++) = '_'; + *(t++) = hexchar(*f >> 4); + *(t++) = hexchar(*f); + } else + *(t++) = *f; + } + + *t = 0; + + return r; +} + +char *bus_path_unescape(const char *s) { + assert(s); + + char *r, *t; + const char *f; + + if (!(r = new(char, strlen(s)+1))) + return NULL; + + for (f = s, t = r; *f; f++) { + + if (*f == '_') { + int a, b; + + if ((a = unhexchar(f[1])) < 0 || + (b = unhexchar(f[2])) < 0) { + /* Invalid escape code, let's take it literal then */ + *(t++) = '_'; + } else { + *(t++) = (char) ((a << 4) | b); + f += 2; + } + } else + *(t++) = *f; + } + + *t = 0; + + return r; +} + char *path_kill_slashes(char *path) { char *f, *t; bool slash = false; @@ -876,3 +943,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);