X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=util.c;h=a0f26762dc39b87b3f6cb3c24acb50db14849512;hp=19375aebca7aec07ace039d182239c7ce74d026a;hb=db12775d59ab709f8afc361eaa30c9d54a0a8d7c;hpb=4fe88d28a4cfa504c1f2362d4a7030fae4c4af29 diff --git a/util.c b/util.c index 19375aebc..a0f26762d 100644 --- a/util.c +++ b/util.c @@ -1,5 +1,24 @@ /*-*- Mode: C; c-basic-offset: 8 -*-*/ +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see . +***/ + #include #include #include @@ -7,14 +26,26 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include #include "macro.h" #include "util.h" +#include "ioprio.h" +#include "missing.h" +#include "log.h" +#include "strv.h" -usec_t now(clockid_t clock) { +usec_t now(clockid_t clock_id) { struct timespec ts; - assert_se(clock_gettime(clock, &ts) == 0); + assert_se(clock_gettime(clock_id, &ts) == 0); return timespec_load(&ts); } @@ -83,6 +114,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); @@ -225,7 +275,7 @@ int safe_atolli(const char *s, long long int *ret_lli) { } /* Split a string into words. */ -char *split_spaces(const char *c, size_t *l, char **state) { +char *split(const char *c, size_t *l, const char *separator, char **state) { char *current; current = *state ? *state : (char*) c; @@ -233,8 +283,8 @@ char *split_spaces(const char *c, size_t *l, char **state) { if (!*current || *c == 0) return NULL; - current += strspn(current, WHITESPACE); - *l = strcspn(current, WHITESPACE); + current += strspn(current, separator); + *l = strcspn(current, separator); *state = current+*l; return (char*) current; @@ -277,22 +327,19 @@ char *split_quoted(const char *c, size_t *l, char **state) { return (char*) current; } -const char *sigchld_code(int code) { +char **split_path_and_make_absolute(const char *p) { + char **l; + assert(p); + + if (!(l = strv_split(p, ":"))) + return NULL; - 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"; + if (!strv_path_make_absolute_cwd(l)) { + strv_free(l); + return NULL; + } - return "unknown"; + return l; } int get_parent_of_pid(pid_t pid, pid_t *_ppid) { @@ -468,6 +515,9 @@ char *path_make_absolute(const char *p, const char *prefix) { assert(p); + /* Makes every item in the list an absolute path by prepending + * the prefix, if specified and necessary */ + if (path_is_absolute(p) || !prefix) return strdup(p); @@ -477,6 +527,46 @@ char *path_make_absolute(const char *p, const char *prefix) { return r; } +char *path_make_absolute_cwd(const char *p) { + char *cwd, *r; + + assert(p); + + /* Similar to path_make_absolute(), but prefixes with the + * current working directory. */ + + if (path_is_absolute(p)) + return strdup(p); + + if (!(cwd = get_current_dir_name())) + return NULL; + + r = path_make_absolute(p, cwd); + free(cwd); + + return r; +} + +char **strv_path_make_absolute_cwd(char **l) { + char **s; + + /* Goes through every item in the string list and makes it + * absolute. This works in place and won't rollback any + * changes on failure. */ + + STRV_FOREACH(s, l) { + char *t; + + if (!(t = path_make_absolute_cwd(*s))) + return NULL; + + free(*s); + *s = t; + } + + return l; +} + int reset_all_signal_handlers(void) { int sig; @@ -497,7 +587,7 @@ int reset_all_signal_handlers(void) { return -errno; } - return 0; + return 0; } char *strstrip(char *s) { @@ -521,6 +611,23 @@ char *strstrip(char *s) { } +char *delete_chars(char *s, const char *bad) { + char *f, *t; + + /* Drops all whitespace, regardless where in the string */ + + for (f = s, t = s; *f; f++) { + if (strchr(bad, *f)) + continue; + + *(t++) = *f; + } + + *t = 0; + + return s; +} + char *file_in_same_dir(const char *path, const char *filename) { char *e, *r; size_t k; @@ -548,6 +655,39 @@ char *file_in_same_dir(const char *path, const char *filename) { return r; } +int mkdir_parents(const char *path, mode_t mode) { + const char *p, *e; + + assert(path); + + /* Creates every parent directory in the path except the last + * component. */ + + p = path + strspn(path, "/"); + for (;;) { + int r; + char *t; + + e = p + strcspn(p, "/"); + p = e + strspn(e, "/"); + + /* Is this the last component? If so, then we're + * done */ + if (*p == 0) + return 0; + + if (!(t = strndup(path, e - path))) + return -ENOMEM; + + r = mkdir(t, mode); + + free(t); + + if (r < 0 && errno != EEXIST) + return -errno; + } +} + char hexchar(int x) { static const char table[16] = "0123456789abcdef"; @@ -560,10 +700,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; } @@ -580,6 +720,18 @@ int unoctchar(char c) { return -1; } +char decchar(int x) { + return '0' + (x % 10); +} + +int undecchar(char c) { + + if (c >= '0' && c <= '9') + return c - '0'; + + return -1; +} + char *cescape(const char *s) { char *r, *t; const char *f; @@ -782,8 +934,8 @@ char *xescape(const char *s, const char *bad) { for (f = s, t = r; *f; f++) { - if (*f < ' ' || *f >= 127 || - *f == '\\' || strchr(bad, *f)) { + if ((*f < ' ') || (*f >= 127) || + (*f == '\\') || strchr(bad, *f)) { *(t++) = '\\'; *(t++) = 'x'; *(t++) = hexchar(*f >> 4); @@ -797,6 +949,66 @@ char *xescape(const char *s, const char *bad) { return r; } +char *bus_path_escape(const char *s) { + char *r, *t; + const char *f; + + assert(s); + + /* 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) { + char *r, *t; + const char *f; + + assert(s); + + 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; @@ -865,14 +1077,218 @@ bool path_startswith(const char *path, const char *prefix) { } } -char *ascii_strlower(char *path) { +char *ascii_strlower(char *t) { char *p; - assert(path); + assert(t); - for (p = path; *p; p++) + for (p = t; *p; p++) if (*p >= 'A' && *p <= 'Z') *p = *p - 'A' + 'a'; - return p; + return t; +} + +bool ignore_file(const char *filename) { + assert(filename); + + return + filename[0] == '.' || + endswith(filename, "~") || + endswith(filename, ".rpmnew") || + endswith(filename, ".rpmsave") || + endswith(filename, ".rpmorig") || + endswith(filename, ".dpkg-old") || + endswith(filename, ".dpkg-new") || + endswith(filename, ".swp"); +} + +int fd_nonblock(int fd, bool nonblock) { + int flags; + + assert(fd >= 0); + + if ((flags = fcntl(fd, F_GETFL, 0)) < 0) + return -errno; + + if (nonblock) + flags |= O_NONBLOCK; + else + flags &= ~O_NONBLOCK; + + if (fcntl(fd, F_SETFL, flags) < 0) + return -errno; + + return 0; +} + +int fd_cloexec(int fd, bool cloexec) { + int flags; + + assert(fd >= 0); + + if ((flags = fcntl(fd, F_GETFD, 0)) < 0) + return -errno; + + if (cloexec) + flags |= FD_CLOEXEC; + else + flags &= ~FD_CLOEXEC; + + if (fcntl(fd, F_SETFD, flags) < 0) + return -errno; + + return 0; +} + +int close_all_fds(const int except[], unsigned n_except) { + DIR *d; + struct dirent *de; + int r = 0; + + if (!(d = opendir("/proc/self/fd"))) + return -errno; + + while ((de = readdir(d))) { + int fd = -1; + + if (de->d_name[0] == '.') + continue; + + if ((r = safe_atoi(de->d_name, &fd)) < 0) + goto finish; + + if (fd < 3) + continue; + + if (fd == dirfd(d)) + continue; + + if (except) { + bool found; + unsigned i; + + found = false; + for (i = 0; i < n_except; i++) + if (except[i] == fd) { + found = true; + break; + } + + if (found) + continue; + } + + if ((r = close_nointr(fd)) < 0) { + /* Valgrind has its own FD and doesn't want to have it closed */ + if (errno != EBADF) + goto finish; + } + } + + r = 0; + +finish: + closedir(d); + return r; } + +bool chars_intersect(const char *a, const char *b) { + const char *p; + + /* Returns true if any of the chars in a are in b. */ + for (p = a; *p; p++) + if (strchr(b, *p)) + return true; + + return false; +} + +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);