/*-*- 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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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"
#ifdef HAVE_SELINUX
#include
#include
static struct selabel_handle *label_hnd = NULL;
static inline int use_selinux(void) {
static int use_selinux_ind = -1;
if (use_selinux_ind == -1)
use_selinux_ind = (is_selinux_enabled() == 1);
return use_selinux_ind;
}
static int label_get_file_label_from_path(
const char *label,
const char *path,
const char *class,
security_context_t *fcon) {
security_context_t dir_con = NULL;
security_class_t sclass;
int r = 0;
r = getfilecon(path, &dir_con);
if (r >= 0) {
r = -1;
if ((sclass = string_to_security_class(class)) != 0)
r = security_compute_create((security_context_t) label, dir_con, sclass, fcon);
}
if (r < 0)
r = -errno;
freecon(dir_con);
return r;
}
#endif
int label_init(void) {
int r = 0;
#ifdef HAVE_SELINUX
if (!use_selinux())
return 0;
label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
if (!label_hnd) {
log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
"Failed to initialize SELinux context: %m");
r = (security_getenforce() == 1) ? -errno : 0;
}
#endif
return r;
}
int label_fix(const char *path) {
int r = 0;
#ifdef HAVE_SELINUX
struct stat st;
security_context_t fcon;
if (!use_selinux() || !label_hnd)
return 0;
r = lstat(path, &st);
if (r == 0) {
r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
if (r == 0) {
r = setfilecon(path, fcon);
freecon(fcon);
}
}
if (r < 0) {
log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
"Unable to fix label of %s: %m", path);
r = (security_getenforce() == 1) ? -errno : 0;
}
#endif
return r;
}
void label_finish(void) {
#ifdef HAVE_SELINUX
if (use_selinux() && label_hnd)
selabel_close(label_hnd);
#endif
}
int label_get_socket_label_from_exe(const char *exe, char **label) {
int r = 0;
#ifdef HAVE_SELINUX
security_context_t mycon = NULL, fcon = NULL;
security_class_t sclass;
if (!use_selinux()) {
*label = NULL;
return 0;
}
r = getcon(&mycon);
if (r < 0)
goto fail;
r = getfilecon(exe, &fcon);
if (r < 0)
goto fail;
sclass = string_to_security_class("process");
r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
if (r == 0)
log_debug("SELinux Socket context for %s will be set to %s", exe, *label);
fail:
if (r < 0 && security_getenforce() == 1)
r = -errno;
freecon(mycon);
freecon(fcon);
#endif
return r;
}
int label_fifofile_set(const char *label, const char *path) {
int r = 0;
#ifdef HAVE_SELINUX
security_context_t filecon = NULL;
if (!use_selinux() || !label)
return 0;
if (((r = label_get_file_label_from_path(label, path, "fifo_file", &filecon)) == 0)) {
if ((r = setfscreatecon(filecon)) < 0) {
log_error("Failed to set SELinux file context (%s) on %s: %m", label, path);
r = -errno;
}
freecon(filecon);
}
if (r < 0 && security_getenforce() == 0)
r = 0;
#endif
return r;
}
int label_socket_set(const char *label) {
#ifdef HAVE_SELINUX
if (!use_selinux())
return 0;
if (setsockcreatecon((security_context_t) label) < 0) {
log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
"Failed to set SELinux context (%s) on socket: %m", label);
if (security_getenforce() == 1)
return -errno;
}
#endif
return 0;
}
void label_file_clear(void) {
#ifdef HAVE_SELINUX
if (!use_selinux())
return;
setfscreatecon(NULL);
#endif
}
void label_socket_clear(void) {
#ifdef HAVE_SELINUX
if (!use_selinux())
return;
setsockcreatecon(NULL);
#endif
}
void label_free(const char *label) {
#ifdef HAVE_SELINUX
if (!use_selinux())
return;
freecon((security_context_t) label);
#endif
}
static int label_mkdir(
const char *path,
mode_t mode) {
#ifdef HAVE_SELINUX
int r;
security_context_t fcon = NULL;
if (use_selinux() && label_hnd) {
if (path[0] == '/') {
r = selabel_lookup_raw(label_hnd, &fcon, path, mode);
}
else {
char *cwd = NULL;
char *newpath = NULL;
cwd = getcwd(NULL,0);
if ((! cwd) || (asprintf(&newpath, "%s/%s",cwd,path) < 0)) {
free(cwd);
return -errno;
}
r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode);
free(cwd);
free(newpath);
}
if (r == 0)
r = setfscreatecon(fcon);
if ((r < 0) && (errno != ENOENT)) {
log_error("Failed to set security context %s for %s", fcon, path);
if (security_getenforce() == 1)
goto finish;
}
}
r = mkdir(path, mode);
finish:
if (use_selinux() && label_hnd) {
setfscreatecon(NULL);
freecon(fcon);
}
return r;
#else
return mkdir(path, mode);
#endif
}
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;
}
usec_t now(clockid_t clock_id) {
struct timespec ts;
assert_se(clock_gettime(clock_id, &ts) == 0);
return timespec_load(&ts);
}
dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
assert(ts);
ts->realtime = now(CLOCK_REALTIME);
ts->monotonic = now(CLOCK_MONOTONIC);
return ts;
}
usec_t timespec_load(const struct timespec *ts) {
assert(ts);
return
(usec_t) ts->tv_sec * USEC_PER_SEC +
(usec_t) ts->tv_nsec / NSEC_PER_USEC;
}
struct timespec *timespec_store(struct timespec *ts, usec_t u) {
assert(ts);
ts->tv_sec = (time_t) (u / USEC_PER_SEC);
ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
return ts;
}
usec_t timeval_load(const struct timeval *tv) {
assert(tv);
return
(usec_t) tv->tv_sec * USEC_PER_SEC +
(usec_t) tv->tv_usec;
}
struct timeval *timeval_store(struct timeval *tv, usec_t u) {
assert(tv);
tv->tv_sec = (time_t) (u / USEC_PER_SEC);
tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
return tv;
}
bool endswith(const char *s, const char *postfix) {
size_t sl, pl;
assert(s);
assert(postfix);
sl = strlen(s);
pl = strlen(postfix);
if (pl == 0)
return true;
if (sl < pl)
return false;
return memcmp(s + sl - pl, postfix, pl) == 0;
}
bool startswith(const char *s, const char *prefix) {
size_t sl, pl;
assert(s);
assert(prefix);
sl = strlen(s);
pl = strlen(prefix);
if (pl == 0)
return true;
if (sl < pl)
return false;
return memcmp(s, prefix, pl) == 0;
}
bool startswith_no_case(const char *s, const char *prefix) {
size_t sl, pl;
unsigned i;
assert(s);
assert(prefix);
sl = strlen(s);
pl = strlen(prefix);
if (pl == 0)
return true;
if (sl < pl)
return false;
for(i = 0; i < pl; ++i) {
if (tolower(s[i]) != tolower(prefix[i]))
return false;
}
return true;
}
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 (wl == 0)
return true;
if (memcmp(s, word, wl) != 0)
return false;
return s[wl] == 0 ||
strchr(WHITESPACE, s[wl]);
}
int close_nointr(int fd) {
assert(fd >= 0);
for (;;) {
int r;
if ((r = close(fd)) >= 0)
return r;
if (errno != EINTR)
return r;
}
}
void close_nointr_nofail(int fd) {
int saved_errno = errno;
/* like close_nointr() but cannot fail, and guarantees errno
* is unchanged */
assert_se(close_nointr(fd) == 0);
errno = saved_errno;
}
void close_many(const int fds[], unsigned n_fd) {
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;
}
int parse_pid(const char *s, pid_t* ret_pid) {
unsigned long ul;
pid_t pid;
int r;
assert(s);
assert(ret_pid);
if ((r = safe_atolu(s, &ul)) < 0)
return r;
pid = (pid_t) ul;
if ((unsigned long) pid != ul)
return -ERANGE;
if (pid <= 0)
return -ERANGE;
*ret_pid = pid;
return 0;
}
int safe_atou(const char *s, unsigned *ret_u) {
char *x = NULL;
unsigned long l;
assert(s);
assert(ret_u);
errno = 0;
l = strtoul(s, &x, 0);
if (!x || *x || errno)
return errno ? -errno : -EINVAL;
if ((unsigned long) (unsigned) l != l)
return -ERANGE;
*ret_u = (unsigned) l;
return 0;
}
int safe_atoi(const char *s, int *ret_i) {
char *x = NULL;
long l;
assert(s);
assert(ret_i);
errno = 0;
l = strtol(s, &x, 0);
if (!x || *x || errno)
return errno ? -errno : -EINVAL;
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);
assert(ret_llu);
errno = 0;
l = strtoull(s, &x, 0);
if (!x || *x || errno)
return errno ? -errno : -EINVAL;
*ret_llu = l;
return 0;
}
int safe_atolli(const char *s, long long int *ret_lli) {
char *x = NULL;
long long l;
assert(s);
assert(ret_lli);
errno = 0;
l = strtoll(s, &x, 0);
if (!x || *x || errno)
return errno ? -errno : -EINVAL;
*ret_lli = l;
return 0;
}
/* Split a string into words. */
char *split(const char *c, size_t *l, const char *separator, char **state) {
char *current;
current = *state ? *state : (char*) c;
if (!*current || *c == 0)
return NULL;
current += strspn(current, separator);
*l = strcspn(current, separator);
*state = current+*l;
return (char*) current;
}
/* Split a string into words, but consider strings enclosed in '' and
* "" as words even if they include spaces. */
char *split_quoted(const char *c, size_t *l, char **state) {
char *current, *e;
bool escaped = false;
current = *state ? *state : (char*) c;
if (!*current || *c == 0)
return NULL;
current += strspn(current, WHITESPACE);
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 get_parent_of_pid(pid_t pid, pid_t *_ppid) {
int r;
FILE *f;
char fn[132], line[256], *p;
long unsigned ppid;
assert(pid >= 0);
assert(_ppid);
assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
fn[sizeof(fn)-1] = 0;
if (!(f = fopen(fn, "r")))
return -errno;
if (!(fgets(line, sizeof(line), f))) {
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 */
if (!(p = strrchr(line, ')')))
return -EIO;
p++;
if (sscanf(p, " "
"%*c " /* state */
"%lu ", /* ppid */
&ppid) != 1)
return -EIO;
if ((long unsigned) (pid_t) ppid != ppid)
return -ERANGE;
*_ppid = (pid_t) ppid;
return 0;
}
int write_one_line_file(const char *fn, const char *line) {
FILE *f;
int r;
assert(fn);
assert(line);
if (!(f = fopen(fn, "we")))
return -errno;
if (fputs(line, f) < 0) {
r = -errno;
goto finish;
}
r = 0;
finish:
fclose(f);
return r;
}
int read_one_line_file(const char *fn, char **line) {
FILE *f;
int r;
char t[2048], *c;
assert(fn);
assert(line);
if (!(f = fopen(fn, "re")))
return -errno;
if (!(fgets(t, sizeof(t), f))) {
r = -errno;
goto finish;
}
if (!(c = strdup(t))) {
r = -ENOMEM;
goto finish;
}
*line = c;
r = 0;
finish:
fclose(f);
return r;
}
char *truncate_nl(char *s) {
assert(s);
s[strcspn(s, NEWLINE)] = 0;
return s;
}
int get_process_name(pid_t pid, char **name) {
char *p;
int r;
assert(pid >= 1);
assert(name);
if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0)
return -ENOMEM;
r = read_one_line_file(p, name);
free(p);
if (r < 0)
return r;
truncate_nl(*name);
return 0;
}
int get_process_cmdline(pid_t pid, size_t max_length, char **line) {
char *p, *r, *k;
int c;
bool space = false;
size_t left;
FILE *f;
assert(pid >= 1);
assert(max_length > 0);
assert(line);
if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 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;
while ((c = getc(f)) != EOF) {
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) {
size_t n = MIN(left-1, 3U);
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;
}
char *strnappend(const char *s, const char *suffix, size_t b) {
size_t a;
char *r;
if (!s && !suffix)
return strdup("");
if (!s)
return strndup(suffix, b);
if (!suffix)
return strdup(s);
assert(s);
assert(suffix);
a = strlen(s);
if (!(r = new(char, a+b+1)))
return NULL;
memcpy(r, s, a);
memcpy(r+a, suffix, b);
r[a+b] = 0;
return r;
}
char *strappend(const char *s, const char *suffix) {
return strnappend(s, suffix, suffix ? strlen(suffix) : 0);
}
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) {
int ret = -errno;
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);
free(target);
if (!k)
return -ENOMEM;
*r = k;
return 0;
}
int parent_of_path(const char *path, char **_r) {
const char *e, *a = NULL, *b = NULL, *p;
char *r;
bool slash = false;
assert(path);
assert(_r);
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
r = strndup(path, p-path);
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);
if (asprintf(&r, "%s/%s", prefix, p) < 0)
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);
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;
}
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) {
if (errno == ENOMEM || !errno)
enomem = true;
continue;
}
l[k++] = u;
}
l[k] = NULL;
if (enomem)
return NULL;
return l;
}
int reset_all_signal_handlers(void) {
int sig;
for (sig = 1; sig < _NSIG; sig++) {
struct sigaction sa;
if (sig == SIGKILL || sig == SIGSTOP)
continue;
zero(sa);
sa.sa_handler = SIG_DFL;
sa.sa_flags = SA_RESTART;
/* On Linux the first two RT signals are reserved by
* glibc, and sigaction() will return EINVAL for them. */
if ((sigaction(sig, &sa, NULL) < 0))
if (errno != EINVAL)
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++) {
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;
assert(path);
assert(filename);
/* 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 strdup(filename);
if (!(e = strrchr(path, '/')))
return strdup(filename);
k = strlen(filename);
if (!(r = new(char, e-path+1+k+1)))
return NULL;
memcpy(r, path, e-path+1);
memcpy(r+(e-path)+1, filename, k+1);
return r;
}
int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) {
struct stat st;
if (label_mkdir(path, mode) >= 0)
if (chmod_and_chown(path, mode, uid, gid) < 0)
return -errno;
if (lstat(path, &st) < 0)
return -errno;
if ((st.st_mode & 0777) != mode ||
st.st_uid != uid ||
st.st_gid != gid ||
!S_ISDIR(st.st_mode)) {
errno = EEXIST;
return -errno;
}
return 0;
}
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 = label_mkdir(t, mode);
free(t);
if (r < 0 && errno != EEXIST)
return -errno;
}
}
int mkdir_p(const char *path, mode_t mode) {
int r;
/* Like mkdir -p */
if ((r = mkdir_parents(path, mode)) < 0)
return r;
if (label_mkdir(path, mode) < 0 && errno != EEXIST)
return -errno;
return 0;
}
int rmdir_parents(const char *path, const char *stop) {
size_t l;
int r = 0;
assert(path);
assert(stop);
l = strlen(path);
/* Skip trailing slashes */
while (l > 0 && path[l-1] == '/')
l--;
while (l > 0) {
char *t;
/* Skip last component */
while (l > 0 && path[l-1] != '/')
l--;
/* Skip trailing slashes */
while (l > 0 && path[l-1] == '/')
l--;
if (l <= 0)
break;
if (!(t = strndup(path, l)))
return -ENOMEM;
if (path_startswith(stop, t)) {
free(t);
return 0;
}
r = rmdir(t);
free(t);
if (r < 0)
if (errno != ENOENT)
return -errno;
}
return 0;
}
char hexchar(int x) {
static const char table[16] = "0123456789abcdef";
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. */
if (!(r = new(char, strlen(s)*4 + 1)))
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 *cunescape_length(const char *s, size_t length) {
char *r, *t;
const char *f;
assert(s);
/* Undoes C style string escaping */
if (!(r = new(char, length+1)))
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;
}
}
finish:
*t = 0;
return r;
}
char *cunescape(const char *s) {
return cunescape_length(s, strlen(s));
}
char *xescape(const char *s, const char *bad) {
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. */
if (!(r = new(char, strlen(s)*4+1)))
return NULL;
for (f = s, t = r; *f; f++) {
if ((*f < ' ') || (*f >= 127) ||
(*f == '\\') || strchr(bad, *f)) {
*(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() */
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 *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.
*
* ///foo///bar/ becomes /foo/bar
*/
for (f = path, t = path; *f; f++) {
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 */
if (t == path && slash)
*(t++) = '/';
*t = 0;
return path;
}
bool path_startswith(const char *path, const char *prefix) {
assert(path);
assert(prefix);
if ((path[0] == '/') != (prefix[0] == '/'))
return false;
for (;;) {
size_t a, b;
path += strspn(path, "/");
prefix += strspn(prefix, "/");
if (*prefix == 0)
return true;
if (*path == 0)
return false;
a = strcspn(path, "/");
b = strcspn(prefix, "/");
if (a != b)
return false;
if (memcmp(path, prefix, a) != 0)
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) {
assert(filename);
return
filename[0] == '.' ||
streq(filename, "lost+found") ||
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 (ignore_file(de->d_name))
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;
}
char *format_timestamp(char *buf, size_t l, usec_t t) {
struct tm tm;
time_t sec;
assert(buf);
assert(l > 0);
if (t <= 0)
return NULL;
sec = (time_t) (t / USEC_PER_SEC);
if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0)
return NULL;
return buf;
}
char *format_timespan(char *buf, size_t l, usec_t t) {
static const struct {
const char *suffix;
usec_t usec;
} 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(buf);
assert(l > 0);
if (t == (usec_t) -1)
return NULL;
/* The result of this function can be parsed with parse_usec */
for (i = 0; i < ELEMENTSOF(table); i++) {
int k;
size_t n;
if (t < table[i].usec)
continue;
if (l <= 1)
break;
k = snprintf(p, l, "%s%llu%s", p > buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix);
n = MIN((size_t) k, l);
l -= n;
p += n;
t %= table[i].usec;
}
*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++)
if (streq(table[i], fstype))
return true;
return false;
}
int chvt(int vt) {
int fd, r = 0;
if ((fd = open("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
return -errno;
if (vt < 0) {
int tiocl[2] = {
TIOCL_GETKMSGREDIRECT,
0
};
if (ioctl(fd, TIOCLINUX, tiocl) < 0)
return -errno;
vt = tiocl[0] <= 0 ? 1 : tiocl[0];
}
if (ioctl(fd, VT_ACTIVATE, vt) < 0)
r = -errno;
close_nointr_nofail(r);
return r;
}
int read_one_char(FILE *f, char *ret, bool *need_nl) {
struct termios old_termios, new_termios;
char c;
char line[1024];
assert(f);
assert(ret);
if (tcgetattr(fileno(f), &old_termios) >= 0) {
new_termios = old_termios;
new_termios.c_lflag &= ~ICANON;
new_termios.c_cc[VMIN] = 1;
new_termios.c_cc[VTIME] = 0;
if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
size_t k;
k = fread(&c, 1, 1, f);
tcsetattr(fileno(f), TCSADRAIN, &old_termios);
if (k <= 0)
return -EIO;
if (need_nl)
*need_nl = c != '\n';
*ret = c;
return 0;
}
}
if (!(fgets(line, sizeof(line), f)))
return -EIO;
truncate_nl(line);
if (strlen(line) != 1)
return -EBADMSG;
if (need_nl)
*need_nl = false;
*ret = line[0];
return 0;
}
int ask(char *ret, const char *replies, const char *text, ...) {
assert(ret);
assert(replies);
assert(text);
for (;;) {
va_list ap;
char c;
int r;
bool need_nl = true;
fputs("\x1B[1m", stdout);
va_start(ap, text);
vprintf(text, ap);
va_end(ap);
fputs("\x1B[0m", stdout);
fflush(stdout);
if ((r = read_one_char(stdin, &c, &need_nl)) < 0) {
if (r == -EBADMSG) {
puts("Bad input, please try again.");
continue;
}
putchar('\n');
return r;
}
if (need_nl)
putchar('\n');
if (strchr(replies, c)) {
*ret = c;
return 0;
}
puts("Read unexpected character, please try again.");
}
}
int reset_terminal(int fd) {
struct termios termios;
int r = 0;
long arg;
/* Set terminal to some sane defaults */
assert(fd >= 0);
/* First, unlock termios */
zero(termios);
ioctl(fd, TIOCSLCKTRMIOS, &termios);
/* Disable exclusive mode, just in case */
ioctl(fd, TIOCNXCL);
/* Enable console unicode mode */
arg = K_UNICODE;
ioctl(fd, KDSKBMODE, &arg);
if (tcgetattr(fd, &termios) < 0) {
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 */
termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC);
termios.c_iflag |= ICRNL | IMAXBEL | IUTF8;
termios.c_oflag |= ONLCR;
termios.c_cflag |= CREAD;
termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE;
termios.c_cc[VINTR] = 03; /* ^C */
termios.c_cc[VQUIT] = 034; /* ^\ */
termios.c_cc[VERASE] = 0177;
termios.c_cc[VKILL] = 025; /* ^X */
termios.c_cc[VEOF] = 04; /* ^D */
termios.c_cc[VSTART] = 021; /* ^Q */
termios.c_cc[VSTOP] = 023; /* ^S */
termios.c_cc[VSUSP] = 032; /* ^Z */
termios.c_cc[VLNEXT] = 026; /* ^V */
termios.c_cc[VWERASE] = 027; /* ^W */
termios.c_cc[VREPRINT] = 022; /* ^R */
termios.c_cc[VEOL] = 0;
termios.c_cc[VEOL2] = 0;
termios.c_cc[VTIME] = 0;
termios.c_cc[VMIN] = 1;
if (tcsetattr(fd, TCSANOW, &termios) < 0)
r = -errno;
finish:
/* Just in case, flush all crap out */
tcflush(fd, TCIOFLUSH);
return r;
}
int open_terminal(const char *name, int mode) {
int fd, r;
if ((fd = open(name, mode)) < 0)
return -errno;
if ((r = isatty(fd)) < 0) {
close_nointr_nofail(fd);
return -errno;
}
if (!r) {
close_nointr_nofail(fd);
return -ENOTTY;
}
return fd;
}
int flush_fd(int fd) {
struct pollfd pollfd;
zero(pollfd);
pollfd.fd = fd;
pollfd.events = POLLIN;
for (;;) {
char buf[1024];
ssize_t l;
int r;
if ((r = poll(&pollfd, 1, 0)) < 0) {
if (errno == EINTR)
continue;
return -errno;
}
if (r == 0)
return 0;
if ((l = read(fd, buf, sizeof(buf))) < 0) {
if (errno == EINTR)
continue;
if (errno == EAGAIN)
return 0;
return -errno;
}
if (l <= 0)
return 0;
}
}
int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm) {
int fd = -1, notify = -1, r, wd = -1;
assert(name);
/* 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
* rogue process uses TIOCNOTTY on /dev/tty *after* closing
* 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.) */
if (!fail && !force) {
if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
r = -errno;
goto fail;
}
if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) {
r = -errno;
goto fail;
}
}
for (;;) {
if (notify >= 0)
if ((r = flush_fd(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 */
if ((fd = open_terminal(name, O_RDWR|O_NOCTTY)) < 0)
return -errno;
/* First, try to get the tty */
r = ioctl(fd, TIOCSCTTY, force);
/* Sometimes it makes sense to ignore TIOCSCTTY
* returning EPERM, i.e. when very likely we already
* are have this controlling terminal. */
if (r < 0 && errno == EPERM && ignore_tiocstty_eperm)
r = 0;
if (r < 0 && (force || fail || errno != EPERM)) {
r = -errno;
goto fail;
}
if (r >= 0)
break;
assert(!fail);
assert(!force);
assert(notify >= 0);
for (;;) {
struct inotify_event e;
ssize_t l;
if ((l = read(notify, &e, sizeof(e))) != sizeof(e)) {
if (l < 0) {
if (errno == EINTR)
continue;
r = -errno;
} else
r = -EIO;
goto fail;
}
if (e.wd != wd || !(e.mask & IN_CLOSE)) {
r = -EIO;
goto fail;
}
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. */
close_nointr_nofail(fd);
}
if (notify >= 0)
close_nointr_nofail(notify);
if ((r = reset_terminal(fd)) < 0)
log_warning("Failed to reset terminal: %s", strerror(-r));
return fd;
fail:
if (fd >= 0)
close_nointr_nofail(fd);
if (notify >= 0)
close_nointr_nofail(notify);
return r;
}
int release_terminal(void) {
int r = 0, fd;
struct sigaction sa_old, sa_new;
if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY)) < 0)
return -errno;
/* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
* by our own TIOCNOTTY */
zero(sa_new);
sa_new.sa_handler = SIG_IGN;
sa_new.sa_flags = SA_RESTART;
assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
if (ioctl(fd, TIOCNOTTY) < 0)
r = -errno;
assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
close_nointr_nofail(fd);
return r;
}
int sigaction_many(const struct sigaction *sa, ...) {
va_list ap;
int r = 0, sig;
va_start(ap, sa);
while ((sig = va_arg(ap, int)) > 0)
if (sigaction(sig, sa, NULL) < 0)
r = -errno;
va_end(ap);
return r;
}
int ignore_signals(int sig, ...) {
struct sigaction sa;
va_list ap;
int r = 0;
zero(sa);
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_RESTART;
if (sigaction(sig, &sa, NULL) < 0)
r = -errno;
va_start(ap, sig);
while ((sig = va_arg(ap, int)) > 0)
if (sigaction(sig, &sa, NULL) < 0)
r = -errno;
va_end(ap);
return r;
}
int default_signals(int sig, ...) {
struct sigaction sa;
va_list ap;
int r = 0;
zero(sa);
sa.sa_handler = SIG_DFL;
sa.sa_flags = SA_RESTART;
if (sigaction(sig, &sa, NULL) < 0)
r = -errno;
va_start(ap, sig);
while ((sig = va_arg(ap, int)) > 0)
if (sigaction(sig, &sa, NULL) < 0)
r = -errno;
va_end(ap);
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;
}
ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
uint8_t *p;
ssize_t n = 0;
assert(fd >= 0);
assert(buf);
p = buf;
while (nbytes > 0) {
ssize_t k;
if ((k = read(fd, p, nbytes)) <= 0) {
if (k < 0 && errno == EINTR)
continue;
if (k < 0 && errno == EAGAIN && do_poll) {
struct pollfd pollfd;
zero(pollfd);
pollfd.fd = fd;
pollfd.events = POLLIN;
if (poll(&pollfd, 1, -1) < 0) {
if (errno == EINTR)
continue;
return n > 0 ? n : -errno;
}
if (pollfd.revents != POLLIN)
return n > 0 ? n : -EIO;
continue;
}
return n > 0 ? n : (k < 0 ? -errno : 0);
}
p += k;
nbytes -= k;
n += k;
}
return n;
}
ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
const uint8_t *p;
ssize_t n = 0;
assert(fd >= 0);
assert(buf);
p = buf;
while (nbytes > 0) {
ssize_t k;
if ((k = write(fd, p, nbytes)) <= 0) {
if (k < 0 && errno == EINTR)
continue;
if (k < 0 && errno == EAGAIN && do_poll) {
struct pollfd pollfd;
zero(pollfd);
pollfd.fd = fd;
pollfd.events = POLLOUT;
if (poll(&pollfd, 1, -1) < 0) {
if (errno == EINTR)
continue;
return n > 0 ? n : -errno;
}
if (pollfd.revents != POLLOUT)
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) {
if (errno == ENOENT)
return 0;
return -errno;
}
if ((r = parent_of_path(t, &parent)) < 0)
return r;
r = lstat(parent, &b);
free(parent);
if (r < 0)
return -errno;
return a.st_dev != b.st_dev;
}
int parse_usec(const char *t, usec_t *usec) {
static const struct {
const char *suffix;
usec_t usec;
} 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);
assert(usec);
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++)
if (startswith(e, table[i].suffix)) {
r += (usec_t) l * table[i].usec;
p = e + strlen(table[i].suffix);
break;
}
if (i >= ELEMENTSOF(table))
return -EINVAL;
} while (*p != 0);
*usec = r;
return 0;
}
int make_stdio(int fd) {
int r, s, t;
assert(fd >= 0);
r = dup2(fd, STDIN_FILENO);
s = dup2(fd, STDOUT_FILENO);
t = dup2(fd, STDERR_FILENO);
if (fd >= 3)
close_nointr_nofail(fd);
if (r < 0 || s < 0 || t < 0)
return -errno;
return 0;
}
bool is_clean_exit(int code, int status) {
if (code == CLD_EXITED)
return status == 0;
/* If a daemon does not implement handlers for some of the
* signals that's not considered an unclean shutdown */
if (code == CLD_KILLED)
return
status == SIGHUP ||
status == SIGINT ||
status == SIGTERM ||
status == SIGPIPE;
return false;
}
bool is_device_path(const char *path) {
/* Returns true on paths that refer to a device, either in
* sysfs or in /dev */
return
path_startswith(path, "/dev/") ||
path_startswith(path, "/sys/");
}
int dir_is_empty(const char *path) {
DIR *d;
int r;
struct dirent buf, *de;
if (!(d = opendir(path)))
return -errno;
for (;;) {
if ((r = readdir_r(d, &buf, &de)) > 0) {
r = -r;
break;
}
if (!de) {
r = 1;
break;
}
if (!ignore_file(de->d_name)) {
r = 0;
break;
}
}
closedir(d);
return r;
}
unsigned long long random_ull(void) {
int fd;
uint64_t ull;
ssize_t r;
if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0)
goto fallback;
r = loop_read(fd, &ull, sizeof(ull), true);
close_nointr_nofail(fd);
if (r != sizeof(ull))
goto fallback;
return ull;
fallback:
return random() * RAND_MAX + random();
}
void rename_process(const char name[8]) {
assert(name);
prctl(PR_SET_NAME, name);
/* This is a like a poor man's setproctitle(). The string
* passed should fit in 7 chars (i.e. the length of
* "systemd") */
if (program_invocation_name)
strncpy(program_invocation_name, name, strlen(program_invocation_name));
}
void sigset_add_many(sigset_t *ss, ...) {
va_list ap;
int sig;
assert(ss);
va_start(ap, ss);
while ((sig = va_arg(ap, int)) > 0)
assert_se(sigaddset(ss, sig) == 0);
va_end(ap);
}
char* gethostname_malloc(void) {
struct utsname u;
assert_se(uname(&u) >= 0);
if (u.nodename[0])
return strdup(u.nodename);
return strdup(u.sysname);
}
int getmachineid_malloc(char **b) {
int r;
assert(b);
if ((r = read_one_line_file("/var/lib/dbus/machine-id", b)) < 0)
return r;
strstrip(*b);
return 0;
}
char* getlogname_malloc(void) {
uid_t uid;
long bufsize;
char *buf, *name;
struct passwd pwbuf, *pw = NULL;
struct stat st;
if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
uid = st.st_uid;
else
uid = getuid();
/* Shortcut things to avoid NSS lookups */
if (uid == 0)
return strdup("root");
if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) <= 0)
bufsize = 4096;
if (!(buf = malloc(bufsize)))
return NULL;
if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) {
name = strdup(pw->pw_name);
free(buf);
return name;
}
free(buf);
if (asprintf(&name, "%lu", (unsigned long) uid) < 0)
return NULL;
return name;
}
int getttyname_malloc(char **r) {
char path[PATH_MAX], *p, *c;
assert(r);
if (ttyname_r(STDIN_FILENO, path, sizeof(path)) < 0)
return -errno;
char_array_0(path);
p = path;
if (startswith(path, "/dev/"))
p += 5;
if (!(c = strdup(p)))
return -ENOMEM;
*r = c;
return 0;
}
static int rm_rf_children(int fd, bool only_dirs) {
DIR *d;
int ret = 0;
assert(fd >= 0);
/* This returns the first error we run into, but nevertheless
* tries to go on */
if (!(d = fdopendir(fd))) {
close_nointr_nofail(fd);
return errno == ENOENT ? 0 : -errno;
}
for (;;) {
struct dirent buf, *de;
bool is_dir;
int r;
if ((r = readdir_r(d, &buf, &de)) != 0) {
if (ret == 0)
ret = -r;
break;
}
if (!de)
break;
if (streq(de->d_name, ".") || streq(de->d_name, ".."))
continue;
if (de->d_type == DT_UNKNOWN) {
struct stat st;
if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
if (ret == 0 && errno != ENOENT)
ret = -errno;
continue;
}
is_dir = S_ISDIR(st.st_mode);
} else
is_dir = de->d_type == DT_DIR;
if (is_dir) {
int subdir_fd;
if ((subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
if (ret == 0 && errno != ENOENT)
ret = -errno;
continue;
}
if ((r = rm_rf_children(subdir_fd, only_dirs)) < 0) {
if (ret == 0)
ret = r;
}
if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
if (ret == 0 && errno != ENOENT)
ret = -errno;
}
} else if (!only_dirs) {
if (unlinkat(fd, de->d_name, 0) < 0) {
if (ret == 0 && errno != ENOENT)
ret = -errno;
}
}
}
closedir(d);
return ret;
}
int rm_rf(const char *path, bool only_dirs, bool delete_root) {
int fd;
int r;
assert(path);
if ((fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
if (errno != ENOTDIR)
return -errno;
if (delete_root && !only_dirs)
if (unlink(path) < 0)
return -errno;
return 0;
}
r = rm_rf_children(fd, only_dirs);
if (delete_root)
if (rmdir(path) < 0) {
if (r == 0)
r = -errno;
}
return r;
}
int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
assert(path);
/* 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. */
if (chmod(path, mode) < 0)
return -errno;
if (chown(path, uid, gid) < 0)
return -errno;
return 0;
}
cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
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);
if (errno != EINVAL)
return NULL;
n *= 2;
}
}
void status_vprintf(const char *format, va_list ap) {
char *s = NULL;
int fd = -1;
assert(format);
/* This independent of logging, as status messages are
* optional and go exclusively to the console. */
if (vasprintf(&s, format, ap) < 0)
goto finish;
if ((fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0)
goto finish;
write(fd, s, strlen(s));
finish:
free(s);
if (fd >= 0)
close_nointr_nofail(fd);
}
void status_printf(const char *format, ...) {
va_list ap;
assert(format);
va_start(ap, format);
status_vprintf(format, ap);
va_end(ap);
}
void status_welcome(void) {
#if defined(TARGET_FEDORA)
char *r;
if (read_one_line_file("/etc/system-release", &r) < 0)
return;
truncate_nl(r);
/* This tries to mimic the color magic the old Red Hat sysinit
* script did. */
if (startswith(r, "Red Hat"))
status_printf("Welcome to \x1B[0;31m%s\x1B[0m!\n", r); /* Red for RHEL */
else if (startswith(r, "Fedora"))
status_printf("Welcome to \x1B[0;34m%s\x1B[0m!\n", r); /* Blue for Fedora */
else
status_printf("Welcome to %s!\n", r);
free(r);
#elif defined(TARGET_SUSE)
char *r;
if (read_one_line_file("/etc/SuSE-release", &r) < 0)
return;
truncate_nl(r);
status_printf("Welcome to \x1B[0;32m%s\x1B[0m!\n", r); /* Green for SUSE */
free(r);
#else
#warning "You probably should add a welcome text logic here."
#endif
}
char *replace_env(const char *format, char **env) {
enum {
WORD,
CURLY,
VARIABLE
} state = WORD;
const char *e, *word = format;
char *r = NULL, *k;
assert(format);
for (e = format; *e; e ++) {
switch (state) {
case WORD:
if (*e == '$')
state = CURLY;
break;
case CURLY:
if (*e == '{') {
if (!(k = strnappend(r, word, e-word-1)))
goto fail;
free(r);
r = k;
word = e-1;
state = VARIABLE;
} else if (*e == '$') {
if (!(k = strnappend(r, word, e-word)))
goto fail;
free(r);
r = k;
word = e+1;
state = WORD;
} else
state = WORD;
break;
case VARIABLE:
if (*e == '}') {
char *t;
if ((t = strv_env_get_with_length(env, word+2, e-word-2))) {
if (!(k = strappend(r, t)))
goto fail;
free(r);
r = k;
word = e+1;
}
state = WORD;
}
break;
}
}
if (!(k = strnappend(r, word, e-word)))
goto fail;
free(r);
return k;
fail:
free(r);
return NULL;
}
char **replace_env_argv(char **argv, char **env) {
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] == '$') {
char *e = strv_env_get(env, *i+1);
if (e) {
char **w, **m;
unsigned q;
if (!(m = strv_split_quoted(e))) {
r[k] = NULL;
strv_free(r);
return 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;
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) {
struct winsize ws;
zero(ws);
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
parsed_columns = ws.ws_col;
}
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
a.st_dev != b.st_dev ||
a.st_ino != b.st_ino;
}
char *ellipsize(const char *s, unsigned length, unsigned percent) {
size_t l, x;
char *r;
assert(s);
assert(percent <= 100);
assert(length >= 3);
l = strlen(s);
if (l <= 3 || l <= length)
return strdup(s);
if (!(r = new0(char, length+1)))
return r;
x = (length * percent) / 100;
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;
}
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);
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",
[SIGSTKFLT] = "STKFLT",
[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);