util.c revision 57f3067825d9361d7487f272bfaff3b36c684c62
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering This file is part of systemd.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Copyright 2010 Lennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemd is free software; you can redistribute it and/or modify it
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering under the terms of the GNU Lesser General Public License as published by
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering (at your option) any later version.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemd is distributed in the hope that it will be useful, but
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Lesser General Public License for more details.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering You should have received a copy of the GNU Lesser General Public License
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringbool streq_ptr(const char *a, const char *b) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* Like streq(), but tries to make sense of NULL pointers */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(clock_gettime(clock_id, &ts) == 0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversdual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sieversdual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringusec_t timespec_load(const struct timespec *ts) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstruct timespec *timespec_store(struct timespec *ts, usec_t u) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poetteringusec_t timeval_load(const struct timeval *tv) {
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poetteringstruct timeval *timeval_store(struct timeval *tv, usec_t u) {
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringbool endswith(const char *s, const char *postfix) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return memcmp(s + sl - pl, postfix, pl) == 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringbool startswith(const char *s, const char *prefix) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringbool startswith_no_case(const char *s, const char *prefix) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for(i = 0; i < pl; ++i)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringbool first_word(const char *s, const char *word) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return s[wl] == 0 ||
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* like close_nointr() but cannot fail, and guarantees errno
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * is unchanged */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringvoid close_many(const int fds[], unsigned n_fd) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for (i = 0; i < n_fd; i++)
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagnaint parse_boolean(const char *v) {
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagnaint parse_pid(const char *s, pid_t* ret_pid) {
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna unsigned long ul = 0;
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmekint parse_uid(const char *s, uid_t* ret_uid) {
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna unsigned long ul = 0;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagnaint safe_atou(const char *s, unsigned *ret_u) {
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna unsigned long l;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!x || *x || errno)
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek return errno ? -errno : -EINVAL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if ((unsigned long) (unsigned) l != l)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *ret_u = (unsigned) l;
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmekint safe_atoi(const char *s, int *ret_i) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering l = strtol(s, &x, 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!x || *x || errno)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if ((long) (int) l != l)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint safe_atollu(const char *s, long long unsigned *ret_llu) {
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering unsigned long long l;
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering if (!x || *x || errno)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint safe_atolli(const char *s, long long int *ret_lli) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!x || *x || errno)
2b6bf07dd23bb467099d213c97b3875c5e453491Zbigniew Jędrzejewski-Szmek/* Split a string into words. */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringchar *split(const char *c, size_t *l, const char *separator, char **state) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!*current || *c == 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/* Split a string into words, but consider strings enclosed in '' and
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * "" as words even if they include spaces. */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringchar *split_quoted(const char *c, size_t *l, char **state) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!*current || *c == 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for (e = current; *e; e++) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else if (*e == '\\')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else if (*e == '\'')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for (e = current; *e; e++) {
7c2d80944afb4196f2eff614e8da1450dffcbeaaThomas Hindoe Paaboel Andersen else if (*e == '\\')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else if (*e == '\"')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for (e = current; *e; e++) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else if (*e == '\\')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return (char*) current;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint get_parent_of_pid(pid_t pid, pid_t *_ppid) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering long unsigned ppid;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering /* Let's skip the pid and comm fields. The latter is enclosed
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering * in () but does not escape any () in its value, so let's
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering * skip over it manually */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering if ((long unsigned) (pid_t) ppid != ppid)
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poetteringint get_starttime_of_pid(pid_t pid, unsigned long long *st) {
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering /* Let's skip the pid and comm fields. The latter is enclosed
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering * in () but does not escape any () in its value, so let's
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering * skip over it manually */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poetteringint write_one_line_file(const char *fn, const char *line) {
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering r = fchmod(fd, m & (~u)) < 0 ? -errno : 0;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poetteringint write_one_line_file_atomic(const char *fn, const char *line) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint read_one_line_file(const char *fn, char **line) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (!fgets(t, sizeof(t), f)) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint read_full_file(const char *fn, char **contents, size_t *size) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* Safety check */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering n = st.st_size > 0 ? st.st_size : LINE_MAX;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* Safety check */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *separator, ...) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if ((r = read_full_file(fname, &contents, NULL)) < 0)
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner p[n] != '=')
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers p[n-1] == p[0])
84f6181c2ac99a0514ca5e0c8fc8c8e284caf789Lennart Poettering if (v[0] == '\0') {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* return empty value strings as NULL */
free(v);
v = NULL;
*value = v;
if (!key)
fail:
int load_env_file(
const char *fname,
char ***rl) {
FILE *f;
char **m = NULL;
return -errno;
while (!feof(f)) {
char l[LINE_MAX], *p, *u;
if (!fgets(l, sizeof(l), f)) {
if (feof(f))
r = -errno;
goto finish;
p = strstrip(l);
if (!(u = normalize_env_assignment(p))) {
r = log_oom();
goto finish;
t = strv_append(m, u);
free(u);
r = log_oom();
goto finish;
strv_free(m);
*rl = m;
m = NULL;
fclose(f);
strv_free(m);
FILE *f;
errno = 0;
STRV_FOREACH(i, l) {
fputs(*i, f);
fflush(f);
if (ferror(f)) {
if (errno != 0)
r = -errno;
r = -EIO;
r = -errno;
unlink(p);
fclose(f);
free(p);
char *truncate_nl(char *s) {
assert(s);
if (pid == 0)
return -ENOMEM;
free(p);
bool space = false;
FILE *f;
if (pid == 0)
return -ENOMEM;
free(p);
return -errno;
fclose(f);
return -ENOMEM;
if (isprint(c)) {
if (space) {
left--;
space = false;
left--;
space = true;
fclose(f);
free(r);
if (!comm_fallback)
return -ENOENT;
free(t);
return -ENOMEM;
*line = r;
bool eof;
FILE *f;
if (pid == 0)
return -ENOMEM;
free(p);
return -errno;
fclose(f);
if (count <= 0)
if (pid == 0)
return -ENOMEM;
free(p);
FILE *f;
if (pid == 0)
return getuid();
return -ENOMEM;
free(p);
return -errno;
while (!feof(f)) {
if (feof(f))
r = -errno;
goto finish;
goto finish;
r = -EIO;
fclose(f);
size_t a;
if (!s && !suffix)
if (!suffix)
return strdup(s);
assert(s);
a = strlen(s);
return NULL;
memcpy(r, s, a);
int readlink_malloc(const char *p, char **r) {
assert(p);
assert(r);
ssize_t n;
if (!(c = new(char, l)))
return -ENOMEM;
free(c);
return ret;
free(c);
int readlink_and_make_absolute(const char *p, char **r) {
char *target, *k;
assert(p);
assert(r);
return -ENOMEM;
int readlink_and_canonicalize(const char *p, char **r) {
assert(p);
assert(r);
j = readlink_and_make_absolute(p, &t);
s = canonicalize_file_name(t);
free(t);
path_kill_slashes(*r);
int reset_all_signal_handlers(void) {
int sig;
return -errno;
char *strstrip(char *s) {
for (e = strchr(s, 0); e > s; e --)
assert(s);
size_t k;
return NULL;
size_t l;
return -ENOMEM;
free(t);
r = rmdir(t);
free(t);
return -errno;
char hexchar(int x) {
int unhexchar(char c) {
char octchar(int x) {
int unoctchar(char c) {
char decchar(int x) {
int undecchar(char c) {
char *cescape(const char *s) {
assert(s);
return NULL;
*(t++) = octchar((unsigned char) *f);
assert(s);
if (prefix)
a = unoctchar(f[0]);
goto finish;
char *cunescape(const char *s) {
assert(s);
return NULL;
*(t++) = hexchar(*f);
char *bus_path_escape(const char *s) {
assert(s);
return NULL;
*(t++) = hexchar(*f);
char *bus_path_unescape(const char *f) {
assert(f);
if (!(r = strdup(f)))
return NULL;
char *ascii_strlower(char *t) {
assert(t);
int flags;
return -errno;
if (nonblock)
return -errno;
int flags;
return -errno;
if (cloexec)
return -errno;
for (i = 0; i < n_fdset; i++)
DIR *d;
int fd;
r = -errno;
r = -errno;
closedir(d);
bool chars_intersect(const char *a, const char *b) {
if (strchr(b, *p))
assert(l > 0);
return NULL;
return NULL;
return buf;
usec_t n, d;
return NULL;
if (d >= USEC_PER_YEAR)
(unsigned long long) (d / USEC_PER_YEAR),
else if (d >= USEC_PER_MONTH)
(unsigned long long) (d / USEC_PER_MONTH),
else if (d >= USEC_PER_WEEK)
(unsigned long long) (d / USEC_PER_WEEK),
(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_MINUTE));
else if (d >= USEC_PER_MINUTE)
(unsigned long long) (d / USEC_PER_MINUTE),
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));
return buf;
const char *suffix;
} table[] = {
char *p = buf;
assert(l > 0);
return NULL;
size_t n;
k = snprintf(p, l, "%s%llu%s", p > buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix);
return buf;
static const char table[] =
if (fd < 0)
return -errno;
if (vt < 0) {
return -errno;
return -errno;
assert(f);
size_t k;
return -ETIMEDOUT;
return -EIO;
if (need_nl)
*ret = c;
return -ETIMEDOUT;
return -EIO;
return -EBADMSG;
if (need_nl)
*need_nl = false;
bool on_tty;
bool need_nl = true;
if (on_tty)
if (on_tty)
if (r == -EBADMSG) {
if (need_nl)
*ret = c;
if (switch_to_text)
r = -errno;
goto finish;
r = -errno;
int fd, r;
if (fd < 0)
return fd;
int fd, r;
if (fd >= 0)
return -errno;
return -errno;
if (fd < 0)
return -errno;
return -errno;
return -ENOTTY;
return fd;
ssize_t l;
return -errno;
return -errno;
int acquire_terminal(
const char *name,
bool fail,
bool force,
bool ignore_tiocstty_eperm,
if (notify < 0) {
r = -errno;
goto fail;
if (wd < 0) {
r = -errno;
goto fail;
if (notify >= 0) {
goto fail;
if (fd < 0)
return fd;
r = -errno;
goto fail;
ssize_t l;
struct inotify_event *e;
usec_t n;
r = -ETIMEDOUT;
goto fail;
goto fail;
r = -ETIMEDOUT;
goto fail;
r = -errno;
goto fail;
r = -EIO;
goto fail;
l -= step;
if (notify >= 0)
return fd;
fail:
if (fd >= 0)
if (notify >= 0)
int release_terminal(void) {
int r = 0, fd;
return -errno;
r = -errno;
int r = 0, sig;
r = -errno;
r = -errno;
r = -errno;
r = -errno;
r = -errno;
int close_pipe(int p[]) {
assert(p);
a = close_nointr(p[0]);
uint8_t *p;
ssize_t n = 0;
p = buf;
while (nbytes > 0) {
ssize_t k;
return n > 0 ? n : -errno;
return n > 0 ? n : -EIO;
return n > 0 ? n : (k < 0 ? -errno : 0);
nbytes -= k;
const uint8_t *p;
ssize_t n = 0;
p = buf;
while (nbytes > 0) {
ssize_t k;
return n > 0 ? n : -errno;
return n > 0 ? n : -EIO;
return n > 0 ? n : (k < 0 ? -errno : 0);
nbytes -= k;
const char *suffix;
} table[] = {
usec_t r = 0;
assert(t);
errno = 0;
if (errno != 0)
return -errno;
return -ERANGE;
return -EINVAL;
return -EINVAL;
*usec = r;
const char *suffix;
} table[] = {
nsec_t r = 0;
assert(t);
errno = 0;
if (errno != 0)
return -errno;
return -ERANGE;
return -EINVAL;
return -EINVAL;
*nsec = r;
const char *suffix;
} table[] = {
off_t r = 0;
assert(t);
errno = 0;
if (errno != 0)
return -errno;
return -ERANGE;
return -EINVAL;
return -EINVAL;
*bytes = r;
return -errno;
int make_null_stdio(void) {
int null_fd;
if (null_fd < 0)
return -errno;
return -errno;
if (!de)
unsigned long long random_ull(void) {
ssize_t r;
if (fd < 0)
goto fallback;
if (r != sizeof(ull))
goto fallback;
return ull;
if (saved_argc > 0) {
if (saved_argv[0])
if (!saved_argv[i])
int sig;
char* gethostname_malloc(void) {
struct utsname u;
bool hostname_is_set(void) {
struct utsname u;
long bufsize;
char *name;
if (uid == 0)
if (bufsize <= 0)
if (!buf)
return NULL;
return NULL;
return name;
char* getlogname_malloc(void) {
char *getusername_malloc(void) {
return strdup(e);
assert(r);
return -ENOMEM;
free(s);
unsigned long ttynr;
FILE *f;
return -ENOMEM;
return -errno;
fclose(f);
fclose(f);
return -EIO;
return -EIO;
assert(r);
if (k != -ENOENT)
return -ENOMEM;
if (_devnr)
return -ENOMEM;
if (_devnr)
b = strdup(p);
free(s);
return -ENOMEM;
if (_devnr)
DIR *d;
int ret = 0;
if (r != 0 && ret == 0) {
ret = -r;
if (!de)
keep_around = false;
if (is_dir) {
int subdir_fd;
if (subdir_fd < 0) {
if (r < 0 && ret == 0)
ret = r;
if (!keep_around)
closedir(d);
return ret;
struct statfs s;
return -errno;
return -EPERM;
static int rm_rf_internal(const char *path, bool only_dirs, bool delete_root, bool honour_sticky, bool dangerous) {
int fd, r;
struct statfs s;
return -EPERM;
if (fd < 0) {
return -errno;
if (!dangerous) {
return -errno;
return -EPERM;
return -errno;
if (!dangerous) {
return -errno;
return -EPERM;
if (delete_root) {
r = -errno;
return -errno;
return -errno;
return -errno;
return -errno;
cpu_set_t *r;
if (!(r = CPU_ALLOC(n)))
return NULL;
if (ncpus)
*ncpus = n;
CPU_FREE(r);
return NULL;
return log_oom();
if (fd < 0)
return fd;
if (ellipse) {
free(s);
if (status) {
return -errno;
int status_welcome(void) {
NULL);
if (r < 0 && r != -ENOENT)
WORD,
char *r = NULL, *k;
for (e = format; *e; e ++) {
switch (state) {
case WORD:
case CURLY:
goto fail;
free(r);
goto fail;
free(r);
case VARIABLE:
if (!(k = strappend(r, t)))
goto fail;
free(r);
goto fail;
free(r);
fail:
free(r);
return NULL;
return NULL;
if (!(m = strv_split_quoted(e))) {
r[k] = NULL;
strv_free(r);
return NULL;
m = NULL;
q = strv_length(m);
r[k] = NULL;
strv_free(r);
strv_free(m);
return NULL;
memcpy(r + k, m, q * sizeof(char*));
free(m);
strv_free(r);
return NULL;
r[k] = NULL;
return -errno;
return -EIO;
return parsed_columns;
env_columns = 0;
if (env_columns > 0) {
return parsed_columns;
if (parsed_columns <= 0)
return parsed_columns;
unsigned columns(void) {
return columns_cached(true);
unsigned columns_uncached(void) {
return columns_cached(false);
return -errno;
return -EIO;
unsigned lines(void) {
return parsed_lines;
if (parsed_lines <= 0)
if (parsed_lines <= 0)
return parsed_lines;
int running_in_chroot(void) {
struct stat a, b;
zero(a);
zero(b);
return -errno;
return -errno;
size_t x;
assert(s);
memcpy(r, s, x);
int fd;
if (fd < 0)
return -errno;
size_t l;
assert(s);
l = strlen(s);
return strdup(s);
return strdup(s);
char *normalize_env_assignment(const char *s) {
char *eq, *r;
if (!eq) {
r = strdup(s);
return NULL;
t = strstrip(r);
if (!name)
return NULL;
return NULL;
if (!value)
return NULL;
r = NULL;
if (!status)
return -errno;
return -EPROTO;
return -EPROTO;
sync();
pause();
return -errno;
int nfd;
DIR *d;
if (nfd < 0)
return NULL;
return NULL;
int signal_from_string_try_harder(const char *s) {
int signo;
assert(s);
if (signo <= 0)
return signo;
assert(f);
assert(t);
if (!dual_timestamp_is_set(t))
name,
(unsigned long long) t->realtime,
(unsigned long long) t->monotonic);
assert(t);
t->realtime = a;
t->monotonic = b;
char *dn, *t, *u;
if (u == NULL)
return NULL;
free(u);
if (t == NULL)
return NULL;
free(t);
return NULL;
return dn;
char *fstab_node_to_udev_node(const char *p) {
assert(p);
return strdup(p);
return -EINVAL;
return -EINVAL;
return -EINVAL;
if (tty)
tty++;
d = _d;
goto finish;
char *path;
log_oom();
if (pid == 0) {
if (!argv) {
char *path;
goto finish;
if (_d)
if (pids)
if (!nulstr)
bool plymouth_running(void) {
assert(s);
if (l < strlen(s))
static bool hostname_valid_char(char c) {
bool hostname_is_valid(const char *s) {
if (isempty(s))
if (!hostname_valid_char(*p))
if (p-s > HOST_NAME_MAX)
char* hostname_cleanup(char *s) {
return -errno;
return -errno;
FILE *f;
const char *fn;
size_t k;
int fd;
return -ENOMEM;
if (fd < 0) {
free(t);
return -errno;
unlink(t);
free(t);
return -errno;
*_f = f;
*_temp_path = t;
return -errno;
int fd, r;
if (fd < 0)
return fd;
int fd, r;
return -EINVAL;
if (fd < 0)
return fd;
return -EINVAL;
return -EINVAL;
if (fd < 0)
return fd;
return -errno;
if (fd < 0)
return fd;
if (fdf < 0)
return -errno;
if (fdt < 0) {
return -errno;
ssize_t n, k;
r = -errno;
errno = 0;
_cleanup_free_ char *t;
const char *fn;
size_t k;
unsigned long long ull;
return -ENOMEM;
return -errno;
r = -errno;
unlink(t);
size_t k;
return -EINVAL;
return -ENOMEM;
*path = f;
int get_user_creds(
const char **username,
const char **home,
const char **shell) {
struct passwd *p;
uid_t u;
if (uid)
*uid = 0;
if (gid)
*gid = 0;
if (home)
if (shell)
errno = 0;
p = getpwuid(u);
errno = 0;
if (uid)
if (gid)
if (home)
if (shell)
struct group *g;
if (gid)
*gid = 0;
errno = 0;
errno = 0;
if (gid)
int ngroups_max, r, i;
return -errno;
glob_t g;
zero(g);
errno = 0;
if (k == GLOB_NOMATCH)
else if (k == GLOB_NOSPACE)
r = -ENOMEM;
globfree(&g);
assert(d);
return -errno;
char **i, *parent;
DIR *d;
char **l = NULL;
return -errno;
goto finish;
if (!de)
if (list) {
t = realloc(l, sizeof(char*) * n);
r = -ENOMEM;
goto finish;
assert((unsigned) r < n);
r = -ENOMEM;
goto finish;
l[++r] = NULL;
closedir(d);
if (list)
*list = l;
strv_free(l);
char *strjoin(const char *x, ...) {
size_t l;
l = strlen(x);
l += strlen(t);
return NULL;
p = stpcpy(r, x);
p = stpcpy(p, t);
bool is_main_thread(void) {
return cached > 0;
return -ENOMEM;
free(p);
*ret = d;
return -ENOMEM;
free(p);
return -ENOENT;
return -ENOMEM;
r = read_one_line_file(p, &s);
free(p);
free(s);
return -EINVAL;
return -ENOMEM;
free(p);
return -ENOENT;
int file_is_priv_sticky(const char *p) {
assert(p);
return -errno;
static const char *const ioprio_class_table[] = {
static const char *const sigchld_code_table[] = {
static const char *const log_level_table[] = {
static const char* const sched_policy_table[] = {
static const char* const rlimit_table[] = {
static const char* const ip_tos_table[] = {
static const char *const __signal_table[] = {
#ifdef SIGSTKFLT
const char *name;
if (name)
return name;
return buf;
int signal_from_string(const char *s) {
int signo;
int offset = 0;
if (signo > 0)
return signo;
if (safe_atou(s, &u) >= 0) {
return signo;
bool kexec_loaded(void) {
bool loaded = false;
loaded = true;
free(s);
return loaded;
int strdup_or_null(const char *a, char **b) {
assert(b);
*b = NULL;
c = strdup(a);
return -ENOMEM;
case O_RDONLY:
return PROT_READ;
case O_WRONLY:
return PROT_WRITE;
case O_RDWR:
return -EINVAL;
const char *suffix;
} table[] = {
goto finish;
return buf;
assert(p);
r = malloc(l);
return NULL;
memcpy(r, p, l);
int r, value;
l == sizeof(value) &&
value = (int) n;
return -errno;
int r, value;
l == sizeof(value) &&
value = (int) n;
return -errno;
int fd;
if (agent_pid < 0)
return -errno;
if (agent_pid != 0) {
if (fd < 0) {
if (!stdout_is_tty)
if (!stderr_is_tty)
return -errno;
return -errno;
FILE *f;
bool done = false;
size_t l;
if (pid == 0)
return -errno;
c = getc(f);
done = true;
line[i] = c;
line[i] = 0;
if (!value) {
r = -ENOMEM;
} while (!done);
fclose(f);
char *w, *state;
size_t l, k;
return r == -ENOENT ? 0 : r;
bool in_initrd(void) {
struct statfs s;
if (saved >= 0)
return saved;
* 1. the flag file /etc/initrd-release must exist
return saved;
void warn_melody(void) {
if (fd < 0)
int make_console_stdio(void) {
int fd, r;
if (fd < 0) {
return fd;
uid_t u;
struct passwd *p;
h = strdup(e);
return -ENOMEM;
*_h = h;
u = getuid();
return -ENOMEM;
*_h = h;
errno = 0;
p = getpwuid(u);
return -EINVAL;
return -ENOMEM;
*_h = h;
char *sh;
uid_t u;
struct passwd *p;
if (!sh)
return -ENOMEM;
u = getuid();
if (!sh)
return -ENOMEM;
errno = 0;
p = getpwuid(u);
return -EINVAL;
if (!sh)
return -ENOMEM;
void freep(void *p) {
free(*(void**) p);
fclose(*f);
if (*fd >= 0)
closedir(*d);
umask(*u);