util.c revision 28cf382a0afd10d0e2a71d152f0df4909e90d159
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
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 <http://www.gnu.org/licenses/>.
***/
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <syslog.h>
#include <sched.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <dirent.h>
#include <termios.h>
#include <stdarg.h>
#include <libgen.h>
#include <ctype.h>
#include <pwd.h>
#include <dlfcn.h>
#include "macro.h"
#include "util.h"
#include "ioprio.h"
#include "missing.h"
#include "log.h"
#include "strv.h"
#include "label.h"
#include "exit-status.h"
#include "hashmap.h"
bool streq_ptr(const char *a, const char *b) {
/* Like streq(), but tries to make sense of NULL pointers */
if (a && b)
return streq(a, b);
if (!a && !b)
return true;
return false;
}
return timespec_load(&ts);
}
return ts;
}
return
}
return ts;
}
return
}
return tv;
}
assert(s);
if (pl == 0)
return true;
return false;
}
bool startswith(const char *s, const char *prefix) {
assert(s);
if (pl == 0)
return true;
return false;
}
bool startswith_no_case(const char *s, const char *prefix) {
unsigned i;
assert(s);
if (pl == 0)
return true;
return false;
for(i = 0; i < pl; ++i) {
return false;
}
return true;
}
bool first_word(const char *s, const char *word) {
assert(s);
return false;
if (wl == 0)
return true;
return false;
return s[wl] == 0 ||
}
int close_nointr(int fd) {
for (;;) {
int r;
return r;
return r;
}
}
void close_nointr_nofail(int fd) {
int saved_errno = errno;
/* like close_nointr() but cannot fail, and guarantees errno
* is unchanged */
errno = saved_errno;
}
unsigned i;
for (i = 0; i < n_fd; i++)
close_nointr_nofail(fds[i]);
}
int parse_boolean(const char *v) {
assert(v);
if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
return 1;
else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
return 0;
return -EINVAL;
}
unsigned long ul = 0;
int r;
assert(s);
if ((r = safe_atolu(s, &ul)) < 0)
return r;
return -ERANGE;
if (pid <= 0)
return -ERANGE;
return 0;
}
char *x = NULL;
unsigned long l;
assert(s);
errno = 0;
l = strtoul(s, &x, 0);
if (!x || *x || errno)
if ((unsigned long) (unsigned) l != l)
return -ERANGE;
*ret_u = (unsigned) l;
return 0;
}
char *x = NULL;
long l;
assert(s);
errno = 0;
l = strtol(s, &x, 0);
if (!x || *x || errno)
if ((long) (int) l != l)
return -ERANGE;
*ret_i = (int) l;
return 0;
}
int safe_atollu(const char *s, long long unsigned *ret_llu) {
char *x = NULL;
unsigned long long l;
assert(s);
errno = 0;
l = strtoull(s, &x, 0);
if (!x || *x || errno)
*ret_llu = l;
return 0;
}
int safe_atolli(const char *s, long long int *ret_lli) {
char *x = NULL;
long long l;
assert(s);
errno = 0;
l = strtoll(s, &x, 0);
if (!x || *x || errno)
*ret_lli = l;
return 0;
}
/* Split a string into words. */
char *current;
if (!*current || *c == 0)
return NULL;
return (char*) current;
}
/* Split a string into words, but consider strings enclosed in '' and
* "" as words even if they include spaces. */
char *current, *e;
bool escaped = false;
if (!*current || *c == 0)
return NULL;
if (*current == '\'') {
current ++;
for (e = current; *e; e++) {
if (escaped)
escaped = false;
else if (*e == '\\')
escaped = true;
else if (*e == '\'')
break;
}
*l = e-current;
*state = *e == 0 ? e : e+1;
} else if (*current == '\"') {
current ++;
for (e = current; *e; e++) {
if (escaped)
escaped = false;
else if (*e == '\\')
escaped = true;
else if (*e == '\"')
break;
}
*l = e-current;
*state = *e == 0 ? e : e+1;
} else {
for (e = current; *e; e++) {
if (escaped)
escaped = false;
else if (*e == '\\')
escaped = true;
else if (strchr(WHITESPACE, *e))
break;
}
*l = e-current;
*state = e;
}
return (char*) current;
}
char **split_path_and_make_absolute(const char *p) {
char **l;
assert(p);
if (!(l = strv_split(p, ":")))
return NULL;
if (!strv_path_make_absolute_cwd(l)) {
strv_free(l);
return NULL;
}
return l;
}
int r;
FILE *f;
long unsigned ppid;
assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
return -errno;
r = -errno;
fclose(f);
return r;
}
fclose(f);
/* Let's skip the pid and comm fields. The latter is enclosed
* in () but does not escape any () in its value, so let's
* skip over it manually */
return -EIO;
p++;
if (sscanf(p, " "
"%*c " /* state */
"%lu ", /* ppid */
&ppid) != 1)
return -EIO;
return -ERANGE;
return 0;
}
FILE *f;
int r;
return -errno;
r = -errno;
goto finish;
}
fputc('\n', f);
r = 0;
fclose(f);
return r;
}
FILE *f;
int r;
char t[LINE_MAX], *c;
return -errno;
if (!(fgets(t, sizeof(t), f))) {
r = -errno;
goto finish;
}
if (!(c = strdup(t))) {
r = -ENOMEM;
goto finish;
}
*line = c;
r = 0;
fclose(f);
return r;
}
FILE *f;
int r;
size_t n, l;
return -errno;
r = -errno;
goto finish;
}
l = 0;
for (;;) {
char *t;
size_t k;
r = -ENOMEM;
goto finish;
}
buf = t;
if (k <= 0) {
if (ferror(f)) {
r = -errno;
goto finish;
}
break;
}
l += k;
n *= 2;
/* Safety check */
if (n > 4*1024*1024) {
r = -E2BIG;
goto finish;
}
}
if (buf)
buf[l] = 0;
r = -errno;
goto finish;
}
r = 0;
fclose(f);
return r;
}
int parse_env_file(
const char *fname,
const char *separator, ...) {
int r = 0;
char *contents, *p;
return r;
p = contents;
for (;;) {
p += strspn(p, WHITESPACE);
if (!*p)
break;
char **value;
size_t n;
char *v;
p[n] != '=')
continue;
p += n + 1;
if (n >= 2 &&
p[n-1] == p[0])
else
v = strndup(p, n);
if (!v) {
r = -ENOMEM;
goto fail;
}
if (v[0] == '\0') {
/* return empty value strings as NULL */
free(v);
v = NULL;
}
*value = v;
p += n;
r ++;
break;
}
}
if (!key)
}
fail:
return r;
}
int load_env_file(
const char *fname,
char ***rl) {
FILE *f;
char **m = 0;
int r;
return -errno;
while (!feof(f)) {
char l[LINE_MAX], *p, *u;
char **t;
if (!fgets(l, sizeof(l), f)) {
if (feof(f))
break;
r = -errno;
goto finish;
}
p = strstrip(l);
if (!*p)
continue;
continue;
if (!(u = normalize_env_assignment(p))) {
log_error("Out of memory");
r = -ENOMEM;
goto finish;
}
t = strv_append(m, u);
free(u);
if (!t) {
log_error("Out of memory");
r = -ENOMEM;
goto finish;
}
strv_free(m);
m = t;
}
r = 0;
*rl = m;
m = NULL;
if (f)
fclose(f);
strv_free(m);
return r;
}
char *truncate_nl(char *s) {
assert(s);
return s;
}
char *p;
int r;
return -ENOMEM;
r = read_one_line_file(p, name);
free(p);
if (r < 0)
return r;
truncate_nl(*name);
return 0;
}
char *p, *r, *k;
int c;
bool space = false;
FILE *f;
assert(max_length > 0);
return -ENOMEM;
f = fopen(p, "r");
free(p);
if (!f)
return -errno;
if (!(r = new(char, max_length))) {
fclose(f);
return -ENOMEM;
}
k = r;
left = max_length;
if (isprint(c)) {
if (space) {
if (left <= 4)
break;
*(k++) = ' ';
left--;
space = false;
}
if (left <= 4)
break;
*(k++) = (char) c;
left--;
} else
space = true;
}
if (left <= 4) {
memcpy(k, "...", n);
k[n] = 0;
} else
*k = 0;
fclose(f);
/* Kernel threads have no argv[] */
if (r[0] == 0) {
char *t;
int h;
free(r);
if ((h = get_process_name(pid, &t)) < 0)
return h;
h = asprintf(&r, "[%s]", t);
free(t);
if (h < 0)
return -ENOMEM;
}
*line = r;
return 0;
}
size_t a;
char *r;
if (!s && !suffix)
return strdup("");
if (!s)
if (!suffix)
return strdup(s);
assert(s);
a = strlen(s);
if (!(r = new(char, a+b+1)))
return NULL;
memcpy(r, s, a);
r[a+b] = 0;
return r;
}
}
int readlink_malloc(const char *p, char **r) {
size_t l = 100;
assert(p);
assert(r);
for (;;) {
char *c;
ssize_t n;
if (!(c = new(char, l)))
return -ENOMEM;
if ((n = readlink(p, c, l-1)) < 0) {
free(c);
return ret;
}
if ((size_t) n < l-1) {
c[n] = 0;
*r = c;
return 0;
}
free(c);
l *= 2;
}
}
int readlink_and_make_absolute(const char *p, char **r) {
char *target, *k;
int j;
assert(p);
assert(r);
if ((j = readlink_malloc(p, &target)) < 0)
return j;
k = file_in_same_dir(p, target);
if (!k)
return -ENOMEM;
*r = k;
return 0;
}
char *r;
bool slash = false;
if (!*path)
return -EINVAL;
for (e = path; *e; e++) {
if (!slash && *e == '/') {
a = b;
b = e;
slash = true;
} else if (slash && *e != '/')
slash = false;
}
if (*(e-1) == '/')
p = a;
else
p = b;
if (!p)
return -EINVAL;
if (p == path)
r = strdup("/");
else
if (!r)
return -ENOMEM;
*_r = r;
return 0;
}
char *file_name_from_path(const char *p) {
char *r;
assert(p);
if ((r = strrchr(p, '/')))
return r + 1;
return (char*) p;
}
bool path_is_absolute(const char *p) {
assert(p);
return p[0] == '/';
}
bool is_path(const char *p) {
return !!strchr(p, '/');
}
char *path_make_absolute(const char *p, const char *prefix) {
char *r;
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);
return NULL;
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);
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;
}
char **strv_path_canonicalize(char **l) {
char **s;
unsigned k = 0;
bool enomem = false;
if (strv_isempty(l))
return l;
/* Goes through every item in the string list and canonicalize
* the path. This works in place and won't rollback any
* changes on failure. */
STRV_FOREACH(s, l) {
char *t, *u;
t = path_make_absolute_cwd(*s);
free(*s);
if (!t) {
enomem = true;
continue;
}
errno = 0;
u = canonicalize_file_name(t);
free(t);
if (!u) {
enomem = true;
continue;
}
l[k++] = u;
}
l[k] = NULL;
if (enomem)
return NULL;
return l;
}
int reset_all_signal_handlers(void) {
int sig;
continue;
/* On Linux the first two RT signals are reserved by
* glibc, and sigaction() will return EINVAL for them. */
return -errno;
}
return 0;
}
char *strstrip(char *s) {
char *e, *l = NULL;
/* Drops trailing whitespace. Modifies the string in
* place. Returns pointer to first non-space character */
s += strspn(s, WHITESPACE);
for (e = s; *e; e++)
if (!strchr(WHITESPACE, *e))
l = e;
if (l)
*(l+1) = 0;
else
*s = 0;
return 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++) {
continue;
*(t++) = *f;
}
*t = 0;
return s;
}
char *e, *r;
size_t k;
/* This removes the last component of path and appends
* filename, unless the latter is absolute anyway or the
* former isn't */
if (path_is_absolute(filename))
return NULL;
return r;
}
return -errno;
return -errno;
return -errno;
}
return 0;
}
const char *p, *e;
/* Creates every parent directory in the path except the last
* component. */
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;
return -ENOMEM;
r = label_mkdir(t, mode);
free(t);
return -errno;
}
}
int r;
/* Like mkdir -p */
return r;
return -errno;
return 0;
}
size_t l;
int r = 0;
/* Skip trailing slashes */
l--;
while (l > 0) {
char *t;
/* Skip last component */
l--;
/* Skip trailing slashes */
l--;
if (l <= 0)
break;
return -ENOMEM;
if (path_startswith(stop, t)) {
free(t);
return 0;
}
r = rmdir(t);
free(t);
if (r < 0)
return -errno;
}
return 0;
}
char hexchar(int x) {
return table[x & 15];
}
int unhexchar(char c) {
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
char octchar(int x) {
return '0' + (x & 7);
}
int unoctchar(char c) {
if (c >= '0' && c <= '7')
return c - '0';
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;
assert(s);
/* Does C style string escaping. */
return NULL;
for (f = s, t = r; *f; f++)
switch (*f) {
case '\a':
*(t++) = '\\';
*(t++) = 'a';
break;
case '\b':
*(t++) = '\\';
*(t++) = 'b';
break;
case '\f':
*(t++) = '\\';
*(t++) = 'f';
break;
case '\n':
*(t++) = '\\';
*(t++) = 'n';
break;
case '\r':
*(t++) = '\\';
*(t++) = 'r';
break;
case '\t':
*(t++) = '\\';
*(t++) = 't';
break;
case '\v':
*(t++) = '\\';
*(t++) = 'v';
break;
case '\\':
*(t++) = '\\';
*(t++) = '\\';
break;
case '"':
*(t++) = '\\';
*(t++) = '"';
break;
case '\'':
*(t++) = '\\';
*(t++) = '\'';
break;
default:
/* For special chars we prefer octal over
* hexadecimal encoding, simply because glib's
* g_strescape() does the same */
if ((*f < ' ') || (*f >= 127)) {
*(t++) = '\\';
*(t++) = octchar((unsigned char) *f >> 6);
*(t++) = octchar((unsigned char) *f >> 3);
*(t++) = octchar((unsigned char) *f);
} else
*(t++) = *f;
break;
}
*t = 0;
return r;
}
char *r, *t;
const char *f;
assert(s);
/* Undoes C style string escaping */
return r;
for (f = s, t = r; f < s + length; f++) {
if (*f != '\\') {
*(t++) = *f;
continue;
}
f++;
switch (*f) {
case 'a':
*(t++) = '\a';
break;
case 'b':
*(t++) = '\b';
break;
case 'f':
*(t++) = '\f';
break;
case 'n':
*(t++) = '\n';
break;
case 'r':
*(t++) = '\r';
break;
case 't':
*(t++) = '\t';
break;
case 'v':
*(t++) = '\v';
break;
case '\\':
*(t++) = '\\';
break;
case '"':
*(t++) = '"';
break;
case '\'':
*(t++) = '\'';
break;
case 's':
/* This is an extension of the XDG syntax files */
*(t++) = ' ';
break;
case 'x': {
/* hexadecimal encoding */
int a, b;
if ((a = unhexchar(f[1])) < 0 ||
(b = unhexchar(f[2])) < 0) {
/* Invalid escape code, let's take it literal then */
*(t++) = '\\';
*(t++) = 'x';
} else {
*(t++) = (char) ((a << 4) | b);
f += 2;
}
break;
}
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7': {
/* octal encoding */
int a, b, c;
if ((a = unoctchar(f[0])) < 0 ||
(b = unoctchar(f[1])) < 0 ||
(c = unoctchar(f[2])) < 0) {
/* Invalid escape code, let's take it literal then */
*(t++) = '\\';
*(t++) = f[0];
} else {
*(t++) = (char) ((a << 6) | (b << 3) | c);
f += 2;
}
break;
}
case 0:
/* premature end of string.*/
*(t++) = '\\';
goto finish;
default:
/* Invalid escape code, let's take it literal then */
*(t++) = '\\';
*(t++) = *f;
break;
}
}
*t = 0;
return r;
}
char *cunescape(const char *s) {
return cunescape_length(s, strlen(s));
}
char *r, *t;
const char *f;
/* Escapes all chars in bad, in addition to \ and all special
* chars, in \xFF style escaping. May be reversed with
* cunescape. */
return NULL;
for (f = s, t = r; *f; f++) {
if ((*f < ' ') || (*f >= 127) ||
*(t++) = '\\';
*(t++) = 'x';
*(t++) = hexchar(*f >> 4);
*(t++) = hexchar(*f);
} else
*(t++) = *f;
}
*t = 0;
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() */
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 *f) {
char *r, *t;
assert(f);
if (!(r = strdup(f)))
return NULL;
for (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;
/* Removes redundant inner and trailing slashes. Modifies the
* passed string in-place.
*
*/
if (*f == '/') {
slash = true;
continue;
}
if (slash) {
slash = false;
*(t++) = '/';
}
*(t++) = *f;
}
/* Special rule, if we are talking of the root directory, a
trailing slash is good */
*(t++) = '/';
*t = 0;
return path;
}
return false;
for (;;) {
size_t a, b;
if (*prefix == 0)
return true;
if (*path == 0)
return false;
if (a != b)
return false;
return false;
path += a;
prefix += b;
}
}
bool path_equal(const char *a, const char *b) {
assert(a);
assert(b);
if ((a[0] == '/') != (b[0] == '/'))
return false;
for (;;) {
size_t j, k;
a += strspn(a, "/");
b += strspn(b, "/");
if (*a == 0 && *b == 0)
return true;
if (*a == 0 || *b == 0)
return false;
j = strcspn(a, "/");
k = strcspn(b, "/");
if (j != k)
return false;
if (memcmp(a, b, j) != 0)
return false;
a += j;
b += k;
}
}
char *ascii_strlower(char *t) {
char *p;
assert(t);
for (p = t; *p; p++)
if (*p >= 'A' && *p <= 'Z')
*p = *p - 'A' + 'a';
return t;
}
bool ignore_file(const char *filename) {
return
filename[0] == '.' ||
}
int flags;
return -errno;
if (nonblock)
flags |= O_NONBLOCK;
else
flags &= ~O_NONBLOCK;
return -errno;
return 0;
}
int flags;
return -errno;
if (cloexec)
flags |= FD_CLOEXEC;
else
flags &= ~FD_CLOEXEC;
return -errno;
return 0;
}
DIR *d;
int r = 0;
return -errno;
int fd = -1;
continue;
/* Let's better ignore this, just in case */
continue;
if (fd < 3)
continue;
continue;
if (except) {
bool found;
unsigned i;
found = false;
for (i = 0; i < n_except; i++)
found = true;
break;
}
if (found)
continue;
}
if (close_nointr(fd) < 0) {
/* Valgrind has its own FD and doesn't want to have it closed */
r = -errno;
}
}
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;
}
assert(l > 0);
if (t <= 0)
return NULL;
return NULL;
return buf;
}
usec_t n, d;
n = now(CLOCK_REALTIME);
if (t <= 0 || t > n || t + USEC_PER_DAY*7 <= t)
return NULL;
d = n - t;
if (d >= USEC_PER_YEAR)
(unsigned long long) (d / USEC_PER_YEAR),
(unsigned long long) ((d % USEC_PER_YEAR) / USEC_PER_MONTH));
else if (d >= USEC_PER_MONTH)
(unsigned long long) (d / USEC_PER_MONTH),
(unsigned long long) ((d % USEC_PER_MONTH) / USEC_PER_DAY));
else if (d >= USEC_PER_WEEK)
(unsigned long long) (d / USEC_PER_WEEK),
(unsigned long long) ((d % USEC_PER_WEEK) / USEC_PER_DAY));
else if (d >= 2*USEC_PER_DAY)
else if (d >= 25*USEC_PER_HOUR)
(unsigned long long) ((d - USEC_PER_DAY) / USEC_PER_HOUR));
else if (d >= 6*USEC_PER_HOUR)
(unsigned long long) (d / USEC_PER_HOUR));
else if (d >= USEC_PER_HOUR)
(unsigned long long) (d / USEC_PER_HOUR),
(unsigned long long) ((d % USEC_PER_HOUR) / USEC_PER_MINUTE));
else if (d >= 5*USEC_PER_MINUTE)
(unsigned long long) (d / USEC_PER_MINUTE));
else if (d >= USEC_PER_MINUTE)
(unsigned long long) (d / USEC_PER_MINUTE),
(unsigned long long) ((d % USEC_PER_MINUTE) / USEC_PER_SEC));
else if (d >= USEC_PER_SEC)
(unsigned long long) (d / USEC_PER_SEC));
else if (d >= USEC_PER_MSEC)
(unsigned long long) (d / USEC_PER_MSEC));
else if (d > 0)
(unsigned long long) d);
else
buf[l-1] = 0;
return buf;
}
static const struct {
const char *suffix;
} table[] = {
{ "w", USEC_PER_WEEK },
{ "d", USEC_PER_DAY },
{ "h", USEC_PER_HOUR },
{ "min", USEC_PER_MINUTE },
{ "s", USEC_PER_SEC },
{ "ms", USEC_PER_MSEC },
{ "us", 1 },
};
unsigned i;
char *p = buf;
assert(l > 0);
if (t == (usec_t) -1)
return NULL;
if (t == 0) {
snprintf(p, l, "0");
p[l-1] = 0;
return p;
}
/* The result of this function can be parsed with parse_usec */
for (i = 0; i < ELEMENTSOF(table); i++) {
int k;
size_t n;
continue;
if (l <= 1)
break;
k = snprintf(p, l, "%s%llu%s", p > buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix);
l -= n;
p += n;
}
*p = 0;
return buf;
}
bool fstype_is_network(const char *fstype) {
static const char * const table[] = {
"cifs",
"smbfs",
"ncpfs",
"nfs",
"nfs4",
"gfs",
"gfs2"
};
unsigned i;
for (i = 0; i < ELEMENTSOF(table); i++)
return true;
return false;
}
int fd, r = 0;
return -errno;
if (vt < 0) {
int tiocl[2] = {
0
};
return -errno;
}
r = -errno;
return r;
}
char c;
char line[1024];
assert(f);
size_t k;
if (k <= 0)
return -EIO;
if (need_nl)
*need_nl = c != '\n';
*ret = c;
return 0;
}
}
return -EIO;
return -EBADMSG;
if (need_nl)
*need_nl = false;
return 0;
}
bool on_tty;
for (;;) {
char c;
int r;
bool need_nl = true;
if (on_tty)
if (on_tty)
if (r == -EBADMSG) {
puts("Bad input, please try again.");
continue;
}
putchar('\n');
return r;
}
if (need_nl)
putchar('\n');
*ret = c;
return 0;
}
puts("Read unexpected character, please try again.");
}
}
int reset_terminal(int fd) {
int r = 0;
long arg;
/* Set terminal to some sane defaults */
/* We leave locked terminal attributes untouched, so that
* Plymouth may set whatever it wants to set, and we don't
* interfere with that. */
/* Disable exclusive mode, just in case */
/* Enable console unicode mode */
r = -errno;
goto finish;
}
/* We only reset the stuff that matters to the software. How
* hardware is set up we don't touch assuming that somebody
* else will do that for us */
r = -errno;
/* Just in case, flush all crap out */
return r;
}
int fd, r;
unsigned c = 0;
/*
* If a TTY is in the process of being closed opening it might
* cause EIO. This is horribly awful, but unlikely to be
* changed in the kernel. Hence we work around this problem by
* retrying a couple of times.
*
*/
for (;;) {
break;
return -errno;
if (c >= 20)
return -errno;
c++;
}
if (fd < 0)
return -errno;
return -errno;
}
if (!r) {
return -ENOTTY;
}
return fd;
}
for (;;) {
char buf[1024];
ssize_t l;
int r;
continue;
return -errno;
}
if (r == 0)
return 0;
continue;
return 0;
return -errno;
}
if (l <= 0)
return 0;
}
}
/* We use inotify to be notified when the tty is closed. We
* create the watch before checking if we can actually acquire
* it, so that we don't lose any event.
*
* Note: strictly speaking this actually watches for the
* device being closed, it does *not* really watch whether a
* tty loses its controlling process. However, unless some
* its tty otherwise this will not become a problem. As long
* as the administrator makes sure not configure any service
* on the same tty as an untrusted user this should not be a
* problem. (Which he probably should not do anyway.) */
r = -errno;
goto fail;
}
r = -errno;
goto fail;
}
}
for (;;) {
if (notify >= 0)
goto fail;
/* We pass here O_NOCTTY only so that we can check the return
* value TIOCSCTTY and have a reliable way to figure out if we
* successfully became the controlling process of the tty */
return -errno;
/* First, try to get the tty */
/* Sometimes it makes sense to ignore TIOCSCTTY
* returning EPERM, i.e. when very likely we already
* are have this controlling terminal. */
r = 0;
r = -errno;
goto fail;
}
if (r >= 0)
break;
for (;;) {
ssize_t l;
struct inotify_event *e;
continue;
r = -errno;
goto fail;
}
e = (struct inotify_event*) inotify_buffer;
while (l > 0) {
r = -EIO;
goto fail;
}
l -= step;
}
break;
}
/* We close the tty fd here since if the old session
* ended our handle will be dead. It's important that
* we do this after sleeping, so that we don't enter
* an endless loop. */
}
if (notify >= 0)
if ((r = reset_terminal(fd)) < 0)
return fd;
fail:
if (fd >= 0)
if (notify >= 0)
return r;
}
int release_terminal(void) {
int r = 0, fd;
return -errno;
/* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
* by our own TIOCNOTTY */
r = -errno;
return r;
}
int r = 0, sig;
r = -errno;
return r;
}
int ignore_signals(int sig, ...) {
int r = 0;
r = -errno;
r = -errno;
return r;
}
int default_signals(int sig, ...) {
int r = 0;
r = -errno;
r = -errno;
return r;
}
int close_pipe(int p[]) {
int a = 0, b = 0;
assert(p);
if (p[0] >= 0) {
a = close_nointr(p[0]);
p[0] = -1;
}
if (p[1] >= 0) {
b = close_nointr(p[1]);
p[1] = -1;
}
return a < 0 ? a : b;
}
uint8_t *p;
ssize_t n = 0;
p = buf;
while (nbytes > 0) {
ssize_t k;
continue;
continue;
return n > 0 ? n : -errno;
}
return n > 0 ? n : -EIO;
continue;
}
return n > 0 ? n : (k < 0 ? -errno : 0);
}
p += k;
nbytes -= k;
n += k;
}
return n;
}
const uint8_t *p;
ssize_t n = 0;
p = buf;
while (nbytes > 0) {
ssize_t k;
continue;
continue;
return n > 0 ? n : -errno;
}
return n > 0 ? n : -EIO;
continue;
}
return n > 0 ? n : (k < 0 ? -errno : 0);
}
p += k;
nbytes -= k;
n += k;
}
return n;
}
int path_is_mount_point(const char *t) {
struct stat a, b;
char *parent;
int r;
if (lstat(t, &a) < 0) {
return 0;
return -errno;
}
if ((r = parent_of_path(t, &parent)) < 0)
return r;
if (r < 0)
return -errno;
}
static const struct {
const char *suffix;
} table[] = {
{ "sec", USEC_PER_SEC },
{ "s", USEC_PER_SEC },
{ "min", USEC_PER_MINUTE },
{ "hr", USEC_PER_HOUR },
{ "h", USEC_PER_HOUR },
{ "d", USEC_PER_DAY },
{ "w", USEC_PER_WEEK },
{ "msec", USEC_PER_MSEC },
{ "ms", USEC_PER_MSEC },
{ "m", USEC_PER_MINUTE },
{ "usec", 1ULL },
{ "us", 1ULL },
{ "", USEC_PER_SEC },
};
const char *p;
usec_t r = 0;
assert(t);
p = t;
do {
long long l;
char *e;
unsigned i;
errno = 0;
l = strtoll(p, &e, 10);
if (errno != 0)
return -errno;
if (l < 0)
return -ERANGE;
if (e == p)
return -EINVAL;
e += strspn(e, WHITESPACE);
for (i = 0; i < ELEMENTSOF(table); i++)
break;
}
if (i >= ELEMENTSOF(table))
return -EINVAL;
} while (*p != 0);
*usec = r;
return 0;
}
int make_stdio(int fd) {
int r, s, t;
if (fd >= 3)
if (r < 0 || s < 0 || t < 0)
return -errno;
return 0;
}
int make_null_stdio(void) {
int null_fd;
return -errno;
return make_stdio(null_fd);
}
bool is_device_path(const char *path) {
/* Returns true on paths that refer to a device, either in
* sysfs or in /dev */
return
}
int dir_is_empty(const char *path) {
DIR *d;
int r;
return -errno;
for (;;) {
r = -r;
break;
}
if (!de) {
r = 1;
break;
}
r = 0;
break;
}
}
closedir(d);
return r;
}
unsigned long long random_ull(void) {
int fd;
ssize_t r;
goto fallback;
if (r != sizeof(ull))
goto fallback;
return ull;
}
/* This is a like a poor man's setproctitle(). The string
* passed should fit in 7 chars (i.e. the length of
* "systemd") */
}
int sig;
}
char* gethostname_malloc(void) {
struct utsname u;
if (u.nodename[0])
}
char* getlogname_malloc(void) {
long bufsize;
else
/* Shortcut things to avoid NSS lookups */
if (uid == 0)
return strdup("root");
bufsize = 4096;
return NULL;
return name;
}
return NULL;
return name;
}
int getttyname_malloc(int fd, char **r) {
int k;
assert(r);
return -k;
return -ENOMEM;
*r = c;
return 0;
}
int getttyname_harder(int fd, char **r) {
int k;
char *s;
if ((k = getttyname_malloc(fd, &s)) < 0)
return k;
if (streq(s, "tty")) {
free(s);
}
*r = s;
return 0;
}
int get_ctty_devnr(dev_t *d) {
int k;
char line[256], *p;
unsigned long ttynr;
FILE *f;
return -errno;
k = -errno;
fclose(f);
return k;
}
fclose(f);
return -EIO;
p++;
if (sscanf(p, " "
"%*c " /* state */
"%*d " /* ppid */
"%*d " /* pgrp */
"%*d " /* session */
"%lu ", /* ttynr */
&ttynr) != 1)
return -EIO;
return 0;
}
int k;
char fn[128], *s, *b, *p;
assert(r);
if ((k = get_ctty_devnr(&devnr)) < 0)
return k;
if ((k = readlink_malloc(fn, &s)) < 0) {
if (k != -ENOENT)
return k;
/* This is an ugly hack */
return -ENOMEM;
*r = b;
if (_devnr)
return 0;
}
/* Probably something like the ptys which have no
* vaguely useful. */
return -ENOMEM;
*r = b;
if (_devnr)
return 0;
}
if (startswith(s, "/dev/"))
p = s + 5;
else if (startswith(s, "../"))
p = s + 3;
else
p = s;
b = strdup(p);
free(s);
if (!b)
return -ENOMEM;
*r = b;
if (_devnr)
return 0;
}
DIR *d;
int ret = 0;
/* This returns the first error we run into, but nevertheless
* tries to go on */
}
for (;;) {
bool is_dir;
int r;
if (ret == 0)
ret = -r;
break;
}
if (!de)
break;
continue;
continue;
}
} else
if (is_dir) {
int subdir_fd;
continue;
}
if (ret == 0)
ret = r;
}
}
} else if (!only_dirs) {
}
}
}
closedir(d);
return ret;
}
int fd;
int r;
return -errno;
if (delete_root && !only_dirs)
return -errno;
return 0;
}
if (delete_root)
if (r == 0)
r = -errno;
}
return r;
}
/* Under the assumption that we are running privileged we
* first change the access mode and only then hand out
* ownership to avoid a window where access is too open. */
return -errno;
return -errno;
return 0;
}
cpu_set_t *r;
unsigned n = 1024;
/* Allocates the cpuset in the right size */
for (;;) {
if (!(r = CPU_ALLOC(n)))
return NULL;
if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) {
CPU_ZERO_S(CPU_ALLOC_SIZE(n), r);
if (ncpus)
*ncpus = n;
return r;
}
CPU_FREE(r);
return NULL;
n *= 2;
}
}
char *s = NULL;
int fd = -1;
/* This independent of logging, as status messages are
* optional and go exclusively to the console. */
goto finish;
goto finish;
free(s);
if (fd >= 0)
}
void status_printf(const char *format, ...) {
}
void status_welcome(void) {
int r;
"PRETTY_NAME", &pretty_name,
"ANSI_COLOR", &ansi_color,
NULL)) < 0) {
if (r != -ENOENT)
}
#if defined(TARGET_FEDORA)
if (!pretty_name) {
if (r != -ENOENT)
} else
}
if (!ansi_color && pretty_name) {
/* This tries to mimic the color magic the old Red Hat sysinit
* script did. */
}
#elif defined(TARGET_SUSE)
if (!pretty_name) {
if (r != -ENOENT)
} else
}
if (!ansi_color)
#elif defined(TARGET_GENTOO)
if (!pretty_name) {
if (r != -ENOENT)
} else
}
if (!ansi_color)
#elif defined(TARGET_ALTLINUX)
if (!pretty_name) {
if (r != -ENOENT)
} else
}
if (!ansi_color)
#elif defined(TARGET_DEBIAN)
if (!pretty_name) {
char *version;
if (r != -ENOENT)
} else {
if (!pretty_name)
log_warning("Failed to allocate Debian version string.");
}
}
if (!ansi_color)
#elif defined(TARGET_UBUNTU)
"DISTRIB_DESCRIPTION", &pretty_name,
NULL)) < 0) {
if (r != -ENOENT)
}
if (!ansi_color)
#elif defined(TARGET_MANDRIVA)
if (!pretty_name) {
char *s, *p;
if ((r = read_one_line_file("/etc/mandriva-release", &s) < 0)) {
if (r != -ENOENT)
} else {
p = strstr(s, " release ");
if (p) {
*p = '\0';
p += 9;
/* This corresponds to standard rc.sysinit */
const_color = "1;36";
else
log_warning("Failed to allocate Mandriva version string.");
} else
log_warning("Failed to parse /etc/mandriva-release");
free(s);
}
}
#endif
if (!pretty_name && !const_pretty)
const_pretty = "Linux";
if (!ansi_color && !const_color)
const_color = "1";
status_printf("\nWelcome to \x1B[%sm%s\x1B[0m!\n\n",
}
enum {
WORD,
char *r = NULL, *k;
for (e = format; *e; e ++) {
switch (state) {
case WORD:
if (*e == '$')
break;
case CURLY:
if (*e == '{') {
goto fail;
free(r);
r = k;
word = e-1;
} else if (*e == '$') {
goto fail;
free(r);
r = k;
word = e+1;
} else
break;
case VARIABLE:
if (*e == '}') {
const char *t;
t = "";
if (!(k = strappend(r, t)))
goto fail;
free(r);
r = k;
word = e+1;
}
break;
}
}
goto fail;
free(r);
return k;
fail:
free(r);
return NULL;
}
char **r, **i;
unsigned k = 0, l = 0;
l = strv_length(argv);
if (!(r = new(char*, l+1)))
return NULL;
STRV_FOREACH(i, argv) {
/* If $FOO appears as single word, replace it by the split up variable */
if ((*i)[0] == '$' && (*i)[1] != '{') {
char *e;
char **w, **m;
unsigned q;
if (!(m = strv_split_quoted(e))) {
r[k] = NULL;
strv_free(r);
return NULL;
}
} else
m = NULL;
q = strv_length(m);
l = l + q - 1;
if (!(w = realloc(r, sizeof(char*) * (l+1)))) {
r[k] = NULL;
strv_free(r);
strv_free(m);
return NULL;
}
r = w;
if (m) {
memcpy(r + k, m, q * sizeof(char*));
free(m);
}
k += q;
continue;
}
/* If ${FOO} appears as part of a word, replace it by the variable as-is */
if (!(r[k++] = replace_env(*i, env))) {
strv_free(r);
return NULL;
}
}
r[k] = NULL;
return r;
}
int columns(void) {
static __thread int parsed_columns = 0;
const char *e;
if (parsed_columns > 0)
return parsed_columns;
if ((e = getenv("COLUMNS")))
parsed_columns = atoi(e);
if (parsed_columns <= 0) {
}
if (parsed_columns <= 0)
parsed_columns = 80;
return parsed_columns;
}
int running_in_chroot(void) {
struct stat a, b;
zero(a);
zero(b);
/* Only works as root */
if (stat("/proc/1/root", &a) < 0)
return -errno;
if (stat("/", &b) < 0)
return -errno;
return
}
size_t l, x;
char *r;
assert(s);
l = strlen(s);
if (l <= 3 || l <= length)
return strdup(s);
return r;
if (x > length - 3)
x = length - 3;
memcpy(r, s, x);
r[x] = '.';
r[x+1] = '.';
r[x+2] = '.';
memcpy(r + x + 3,
s + l - (length - x - 3),
length - x - 3);
return r;
}
int fd;
return -errno;
return 0;
}
size_t l;
assert(s);
if ((l = strlen(s)) < 2)
return strdup(s);
return strdup(s);
}
char *normalize_env_assignment(const char *s) {
p = strchr(s, '=');
if (!p) {
if (!(r = strdup(s)))
return NULL;
return strstrip(r);
}
return NULL;
if (!(p = strdup(p+1))) {
return NULL;
}
free(p);
if (!value) {
free(p);
return NULL;
}
r = NULL;
return r;
}
for (;;) {
continue;
return -errno;
}
return 0;
}
}
int r;
return r;
}
}
return 0;
return -EPROTO;
}
return -EPROTO;
}
void freeze(void) {
/* Make sure nobody waits for us on a socket anymore */
close_all_fds(NULL, 0);
sync();
for (;;)
pause();
}
return true;
return true;
return false;
}
int nfd;
DIR *d;
return NULL;
return NULL;
}
return d;
}
int signal_from_string_try_harder(const char *s) {
int signo;
assert(s);
if ((signo = signal_from_string(s)) <= 0)
if (startswith(s, "SIG"))
return signal_from_string(s+3);
return signo;
}
assert(f);
assert(t);
if (!dual_timestamp_is_set(t))
return;
fprintf(f, "%s=%llu %llu\n",
name,
(unsigned long long) t->realtime,
(unsigned long long) t->monotonic);
}
unsigned long long a, b;
assert(t);
else {
t->realtime = a;
t->monotonic = b;
}
}
char *fstab_node_to_udev_node(const char *p) {
char *dn, *t, *u;
int r;
/* FIXME: to follow udev's logic 100% we need to leave valid
* UTF8 chars unescaped */
if (startswith(p, "LABEL=")) {
return NULL;
t = xescape(u, "/ ");
free(u);
if (!t)
return NULL;
free(t);
if (r < 0)
return NULL;
return dn;
}
if (startswith(p, "UUID=")) {
return NULL;
t = xescape(u, "/ ");
free(u);
if (!t)
return NULL;
free(t);
if (r < 0)
return NULL;
return dn;
}
return strdup(p);
}
void filter_environ(const char *prefix) {
int i, j;
if (!environ)
return;
for (i = 0, j = 0; environ[i]; i++) {
continue;
}
}
tty += 5;
}
const char *default_term_for_tty(const char *tty) {
const char *term;
tty += 5;
* TERM */
/* If multiple log outputs are configured the
tty++;
else
}
return term;
}
/* Returns a short identifier for the various VM implementations */
#if defined(__i386__) || defined(__x86_64__)
/* Both CPUID and DMI are x86 specific interfaces... */
static const char *const dmi_vendors[] = {
"/sys/class/dmi/id/sys_vendor",
"/sys/class/dmi/id/board_vendor",
"/sys/class/dmi/id/bios_vendor"
};
static const char dmi_vendor_table[] =
"QEMU\0" "qemu\0"
"VMware\0" "vmware\0"
"VMW\0" "vmware\0"
"Microsoft Corporation\0" "microsoft\0"
"innotek GmbH\0" "oracle\0"
"Xen\0" "xen\0"
"Bochs\0" "bochs\0";
static const char cpuid_vendor_table[] =
"XenVMMXenVMM\0" "xen\0"
"KVMKVMKVM\0" "kvm\0"
"VMwareVMware\0" "vmware\0"
"Microsoft Hv\0" "microsoft\0";
union {
char text[13];
} sig;
unsigned i;
const char *j, *k;
bool hypervisor;
#if defined (__i386__)
#define REG_a "eax"
#define REG_b "ebx"
#define REG_a "rax"
#define REG_b "rbx"
#endif
/* First detect whether there is a hypervisor */
eax = 1;
" cpuid \n\t"
: "0" (eax)
);
if (hypervisor) {
/* There is a hypervisor, see what it is */
eax = 0x40000000U;
" cpuid \n\t"
" mov %%ebx, %1 \n\t"
: "0" (eax)
);
if (id)
*id = k;
return 1;
}
}
for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
char *s;
int r;
if ((r = read_one_line_file(dmi_vendors[i], &s)) < 0) {
if (r != -ENOENT)
return r;
continue;
}
if (startswith(s, j))
found = k;
free(s);
if (found) {
if (id)
return 1;
}
}
if (hypervisor) {
if (id)
*id = "other";
return 1;
}
#endif
return 0;
}
int detect_container(const char **id) {
FILE *f;
/* Unfortunately many of these operations require root access
* in one way or another */
if (geteuid() != 0)
return -EPERM;
if (running_in_chroot() > 0) {
if (id)
*id = "chroot";
return 1;
}
if (id)
*id = "openvz";
return 1;
}
for (;;) {
break;
continue;
continue;
if (!streq(p, ":ns:/")) {
fclose(f);
if (id)
*id = "pidns";
return 1;
}
}
fclose(f);
}
return 0;
}
int detect_virtualization(const char **id) {
const char *_id;
int r;
if (cached_id) {
if (cached_id == (const char*) -1)
return 0;
if (id)
return 1;
}
if ((r = detect_container(&_id)) != 0)
goto finish;
if (r > 0) {
if (id)
} else if (r == 0)
cached_id = (const char*) -1;
return r;
}
/* Executes all binaries in a directory in parallel and waits
* until all they all finished. */
if (!d) {
return;
return;
}
d = _d;
}
log_error("Failed to allocate set.");
goto finish;
}
char *path;
int k;
continue;
continue;
log_error("Out of memory");
continue;
}
log_error("Failed to fork: %m");
continue;
}
if (pid == 0) {
char *_argv[2];
/* Child */
if (!argv) {
} else
if (!argv[0])
}
}
}
while (!hashmap_isempty(pids)) {
char *path;
continue;
log_error("waitid() failed: %m");
goto finish;
}
else
} else
}
}
if (_d)
if (pids)
}
int r;
if (r >= 0)
return r;
}
const char *i;
if (!nulstr)
return false;
NULSTR_FOREACH(i, nulstr)
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"
};
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",
};
static const char *const log_facility_table[LOG_NFACILITIES] = {
};
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"
};
static const char* const sched_policy_table[] = {
[SCHED_OTHER] = "other",
[SCHED_BATCH] = "batch",
[SCHED_IDLE] = "idle",
[SCHED_FIFO] = "fifo",
[SCHED_RR] = "rr"
};
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);
static const char* const ip_tos_table[] = {
[IPTOS_LOWDELAY] = "low-delay",
[IPTOS_THROUGHPUT] = "throughput",
[IPTOS_RELIABILITY] = "reliability",
[IPTOS_LOWCOST] = "low-cost",
};
DEFINE_STRING_TABLE_LOOKUP(ip_tos, int);
static const char *const signal_table[] = {
[SIGHUP] = "HUP",
[SIGINT] = "INT",
[SIGQUIT] = "QUIT",
[SIGILL] = "ILL",
[SIGTRAP] = "TRAP",
[SIGABRT] = "ABRT",
[SIGBUS] = "BUS",
[SIGFPE] = "FPE",
[SIGKILL] = "KILL",
[SIGUSR1] = "USR1",
[SIGSEGV] = "SEGV",
[SIGUSR2] = "USR2",
[SIGPIPE] = "PIPE",
[SIGALRM] = "ALRM",
[SIGTERM] = "TERM",
#ifdef SIGSTKFLT
#endif
[SIGCHLD] = "CHLD",
[SIGCONT] = "CONT",
[SIGSTOP] = "STOP",
[SIGTSTP] = "TSTP",
[SIGTTIN] = "TTIN",
[SIGTTOU] = "TTOU",
[SIGURG] = "URG",
[SIGXCPU] = "XCPU",
[SIGXFSZ] = "XFSZ",
[SIGVTALRM] = "VTALRM",
[SIGPROF] = "PROF",
[SIGWINCH] = "WINCH",
[SIGIO] = "IO",
[SIGPWR] = "PWR",
[SIGSYS] = "SYS"
};
DEFINE_STRING_TABLE_LOOKUP(signal, int);