util.c revision cddf148028f525be8176e7f1cbbf4f862bd287f6
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2010 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic volatile unsigned cached_columns = 0;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic volatile unsigned cached_lines = 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _r_ = alloca(sizeof("/proc/") -1 + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sprintf(_r_, "/proc/%lu/" field, (unsigned long) _pid_); \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringbool streq_ptr(const char *a, const char *b) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Like streq(), but tries to make sense of NULL pointers */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (!a && !b)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringchar* endswith(const char *s, const char *postfix) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return (char*) s + sl;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering if (memcmp(s + sl - pl, postfix, pl) != 0)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenchar* startswith(const char *s, const char *prefix) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering const char *a, *b;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering return (char*) a;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringchar* startswith_no_case(const char *s, const char *prefix) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering const char *a, *b;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return (char*) a;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sieversbool first_word(const char *s, const char *word) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return s[wl] == 0 ||
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Just ignore EINTR; a retry loop is the wrong
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * thing to do on Linux.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * https://bugzilla.gnome.org/show_bug.cgi?id=682819
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * http://utcc.utoronto.ca/~cks/space/blog/unix/CloseEINTR
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * https://sites.google.com/site/michaelsafyan/software-engineering/checkforeintrwheninvokingclosethinkagain
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else if (r >= 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* like close_nointr() but cannot fail, and guarantees errno
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * is unchanged */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringvoid close_many(const int fds[], unsigned n_fd) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers for (i = 0; i < n_fd; i++)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint parse_boolean(const char *v) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || strcaseeq(v, "on"))
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || strcaseeq(v, "off"))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint parse_pid(const char *s, pid_t* ret_pid) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned long ul = 0;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers unsigned long ul = 0;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenint safe_atou(const char *s, unsigned *ret_u) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned long l;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!x || x == s || *x || errno)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if ((unsigned long) (unsigned) l != l)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen *ret_u = (unsigned) l;
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering l = strtol(s, &x, 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!x || x == s || *x || errno)
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if ((long) (int) l != l)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenint safe_atollu(const char *s, long long unsigned *ret_llu) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned long long l;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!x || x == s || *x || errno)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenint safe_atolli(const char *s, long long int *ret_lli) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!x || x == s || *x || errno)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint safe_atod(const char *s, double *ret_d) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (!x || x == s || *x || errno)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen *ret_d = (double) d;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen/* Split a string into words. */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringchar *split(const char *c, size_t *l, const char *separator, char **state) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!*current || *c == 0)
34a6778fb9d1065f3fbb8e2243b9f0f25d1d18f1Zbigniew Jędrzejewski-Szmek/* Split a string into words, but consider strings enclosed in '' and
34a6778fb9d1065f3fbb8e2243b9f0f25d1d18f1Zbigniew Jędrzejewski-Szmek * "" as words even if they include spaces. */
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringchar *split_quoted(const char *c, size_t *l, char **state) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!*current || *c == 0)
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering for (e = current; *e; e++) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering else if (*e == '\\')
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering else if (*e == '\'')
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering for (e = current; *e; e++) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering else if (*e == '\\')
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering else if (*e == '\"')
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering for (e = current; *e; e++) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering else if (*e == '\\')
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return (char*) current;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringint get_parent_of_pid(pid_t pid, pid_t *_ppid) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering long unsigned ppid;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering const char *p;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering /* Let's skip the pid and comm fields. The latter is enclosed
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering * in () but does not escape any () in its value, so let's
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering * skip over it manually */
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if ((long unsigned) (pid_t) ppid != ppid)
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringint get_starttime_of_pid(pid_t pid, unsigned long long *st) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering const char *p;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering /* Let's skip the pid and comm fields. The latter is enclosed
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering * in () but does not escape any () in its value, so let's
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering * skip over it manually */
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = fchmod(fd, m & (~u)) < 0 ? -errno : 0;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringint get_process_comm(pid_t pid, char **name) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering const char *p;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringint get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering char *r = NULL, *k;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering const char *p;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!GREEDY_REALLOC(r, allocated, len+2)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *(k++) = (char) c;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Kernel threads have no argv[] */
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering if (r == NULL || r[0] == 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *p;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Kernel threads have an empty cmdline */
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poetteringint get_process_exe(pid_t pid, char **name) {
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering const char *p;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int get_process_id(pid_t pid, const char *field, uid_t *uid) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *p;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint get_process_uid(pid_t pid, uid_t *uid) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint get_process_gid(pid_t pid, gid_t *gid) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_cc(sizeof(uid_t) == sizeof(gid_t));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringchar *strnappend(const char *s, const char *suffix, size_t b) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringchar *strappend(const char *s, const char *suffix) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return strnappend(s, suffix, suffix ? strlen(suffix) : 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint readlink_malloc(const char *p, char **r) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!(c = new(char, l)))
84f6181c2ac99a0514ca5e0c8fc8c8e284caf789Lennart Poetteringint readlink_and_make_absolute(const char *p, char **r) {
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) {
const uint8_t *x;
return NULL;
for (x = p; x < (const uint8_t*) p + l; x++) {
uint8_t *r, *z;
assert(p);
return NULL;
a = unhexchar(x[0]);
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);
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))
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 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) {
if (fd < 0)
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[] = {
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)
return NULL;
if (dir != d) {
free(d);
return dir2;
return dir;
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;
const char *fn;
assert(d);
if (pid == 0)
return -errno;
return -EIO;
return -EIO;
return -ENOENT;
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;
assert(s);
struct statfs s;
return -errno;
if (!is_temporary_fs(&s)) {
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;
if (!is_temporary_fs(&s)) {
return -EPERM;
return -errno;
if (!dangerous) {
return -errno;
if (!is_temporary_fs(&s)) {
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;
int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) {
static bool prev_ephemeral;
return log_oom();
if (fd < 0)
return fd;
if (ellipse) {
free(s);
if (prev_ephemeral)
if (status) {
if (!ephemeral)
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:
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;
unsigned columns(void) {
return cached_columns;
safe_atoi(e, &c);
cached_columns = c;
return -errno;
return -EIO;
unsigned lines(void) {
return cached_lines;
safe_atou(e, &l);
cached_lines = l;
return cached_lines;
cached_columns = 0;
cached_lines = 0;
bool on_tty(void) {
return cached_on_tty;
int running_in_chroot(void) {
struct stat a = {}, 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;
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;
char *tty;
return NULL;
return NULL;
if (tty)
tty++;
return tty;
if (!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) {
bool dot;
if (isempty(s))
for (p = s, dot = true; *p; p++) {
if (dot)
dot = true;
if (!hostname_valid_char(*p))
dot = false;
if (dot)
if (p-s > HOST_NAME_MAX)
char* hostname_cleanup(char *s) {
bool dot;
for (p = s, d = s, dot = true; *p; p++) {
dot = true;
dot = false;
if (hostname_valid_char(*p))
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 passwd *p;
if (uid == 0)
return NULL;
struct group *p;
if (gid == 0)
return NULL;
struct group *g;
if (gid)
*gid = 0;
errno = 0;
errno = 0;
if (gid)
int ngroups_max, r, i;
return -errno;
errno = 0;
if (k == GLOB_NOMATCH)
else if (k == GLOB_NOSPACE)
r = -ENOMEM;
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);
size_t n;
n = strlen(t);
return NULL;
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;
bool done = false;
size_t l;
const char *path;
if (pid == 0)
return -errno;
c = getc(f);
done = true;
line[i] = c;
line[i] = 0;
if (!value)
return -ENOMEM;
} while (!done);
char *w, *state;
size_t l, k;
char *w, *state;
size_t l, k;
bool in_initrd(void) {
struct statfs s;
if (saved >= 0)
return saved;
* 1. the flag file /etc/initrd-release must exist
is_temporary_fs(&s);
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;
bool filename_is_safe(const char *p) {
if (isempty(p))
bool string_is_safe(const char *p) {
assert(p);
bool string_has_cc(const char *p) {
assert(p);
bool path_is_safe(const char *p) {
if (isempty(p))
int comparison;
u = nmemb;
if (comparison < 0)
u = idx;
else if (comparison > 0)
return NULL;
bool is_locale_utf8(void) {
const char *set;
if (cached_answer >= 0)
goto out;
cached_answer = true;
goto out;
if (!set) {
cached_answer = true;
goto out;
cached_answer = true;
goto out;
if (!set) {
cached_answer = true;
goto out;
out:
return (bool)cached_answer;
return NULL;
f = text;
goto oom;
l = nl;
f += old_len;
oom:
free(r);
return NULL;
FILE *f;
return NULL;
switch (state) {
case STATE_OTHER:
fputc(*i, f);
case STATE_ESCAPE:
fputc(*i, f);
case STATE_BRACKET:
if (ferror(f)) {
fclose(f);
return NULL;
fclose(f);
if (_isz)
return obuf;
int on_ac_power(void) {
return -errno;
ssize_t n;
if (!de)
if (device < 0) {
return -errno;
if (fd < 0) {
return -errno;
return -errno;
if (fd < 0) {
return -errno;
return -errno;
return -EIO;
found_online = true;
found_offline = true;
return -EIO;
static int search_and_fopen_internal(const char *path, const char *mode, char **search, FILE **_f) {
return -ENOMEM;
FILE *f;
return -ENOMEM;
*_f = f;
return -errno;
return -ENOENT;
FILE *f;
*_f = f;
return -errno;
if (!copy)
return -ENOMEM;
FILE *f;
*_f = f;
return -errno;
return -ENOMEM;
char *d, *dt;
return -errno;
if (!dt) {
r = log_oom();
goto fail3;
RUN_WITH_UMASK(0000) {
r = -errno;
goto fail2;
r = -errno;
goto fail1;
char *strextend(char **x, ...) {
size_t f, l;
assert(x);
l = f = *x ? strlen(*x) : 0;
size_t n;
n = strlen(t);
return NULL;
return NULL;
p = stpcpy(p, t);
char *strrep(const char *s, unsigned n) {
size_t l;
assert(s);
l = strlen(s);
return NULL;
p = stpcpy(p, s);
size_t a;
q = realloc(*p, a);
return NULL;
*allocated = a;