util.c revision 8c6db8336536916d0476ff8233e0abf40a2f6aab
0N/A along with systemd; If not, see <http://www.gnu.org/licenses/>.
#include <pwd.h>
#include "macro.h"
#include "util.h"
#include "ioprio.h"
#include "missing.h"
#include "log.h"
#include "strv.h"
bool streq_ptr(const char *a, const char *b) {
return streq(a, b);
return ts;
return ts;
return tv;
assert(s);
if (pl == 0)
assert(s);
if (pl == 0)
assert(s);
if (pl == 0)
for(i = 0; i < pl; ++i) {
assert(s);
if (wl == 0)
return s[wl] == 0 ||
for (i = 0; i < n_fd; 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"))
else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
return -EINVAL;
unsigned long ul;
assert(s);
return -ERANGE;
if (pid <= 0)
return -ERANGE;
char *x = NULL;
assert(s);
errno = 0;
l = strtoul(s, &x, 0);
if (!x || *x || errno)
return -ERANGE;
*ret_u = (unsigned) l;
char *x = NULL;
assert(s);
errno = 0;
l = strtol(s, &x, 0);
if (!x || *x || errno)
return -ERANGE;
*ret_i = (int) l;
char *x = NULL;
assert(s);
errno = 0;
l = strtoul(s, &x, 0);
if (!x || *x || errno)
*ret_lu = l;
char *x = NULL;
assert(s);
errno = 0;
l = strtol(s, &x, 0);
if (!x || *x || errno)
*ret_li = l;
char *x = NULL;
assert(s);
errno = 0;
l = strtoull(s, &x, 0);
if (!x || *x || errno)
*ret_llu = l;
char *x = NULL;
assert(s);
errno = 0;
l = strtoll(s, &x, 0);
if (!x || *x || errno)
*ret_lli = l;
char *current;
if (!*current || *c == 0)
return NULL;
return (char*) current;
char *current;
if (!*current || *c == 0)
return NULL;
current ++;
(*state)++;
current ++;
(*state)++;
return (char*) current;
char **split_path_and_make_absolute(const char *p) {
assert(p);
return NULL;
if (!strv_path_make_absolute_cwd(l)) {
strv_free(l);
return NULL;
FILE *f;
long unsigned ppid;
assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
return -errno;
r = -errno;
fclose(f);
fclose(f);
return -EIO;
return -EIO;
return -ERANGE;
FILE *f;
return -errno;
r = -errno;
goto finish;
fclose(f);
FILE *f;
return -errno;
if (!(fgets(t, sizeof(t), f))) {
r = -errno;
goto finish;
if (!(c = strdup(t))) {
r = -ENOMEM;
goto finish;
*line = c;
fclose(f);
char *truncate_nl(char *s) {
assert(s);
return -ENOMEM;
free(p);
size_t a, b;
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;
char *file_name_from_path(const char *p) {
assert(p);
bool path_is_absolute(const char *p) {
assert(p);
bool is_path(const char *p) {
assert(p);
return strdup(p);
return NULL;
char *path_make_absolute_cwd(const char *p) {
char *cwd, *r;
assert(p);
if (path_is_absolute(p))
return strdup(p);
return NULL;
char **strv_path_make_absolute_cwd(char **l) {
STRV_FOREACH(s, l) {
if (!(t = path_make_absolute_cwd(*s)))
return NULL;
free(*s);
char **strv_path_canonicalize(char **l) {
bool enomem = false;
if (strv_isempty(l))
STRV_FOREACH(s, l) {
t = path_make_absolute_cwd(*s);
free(*s);
enomem = true;
errno = 0;
u = canonicalize_file_name(t);
free(t);
enomem = true;
l[k] = NULL;
if (enomem)
return NULL;
int reset_all_signal_handlers(void) {
int sig;
return -errno;
char *strstrip(char *s) {
char *e, *l = NULL;
size_t k;
return NULL;
return -errno;
return -errno;
return -errno;
return -ENOMEM;
free(t);
return -errno;
return -errno;
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);
char *cunescape(const char *s) {
assert(s);
if ((a = unoctchar(f[0])) < 0 ||
goto finish;
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;
bool slash = false;
slash = true;
if (slash) {
slash = false;
return path;
size_t a, b;
if (*prefix == 0)
if (*path == 0)
path += a;
prefix += b;
bool path_equal(const char *a, const char *b) {
assert(a);
assert(b);
size_t j, k;
if (memcmp(a, b, j) != 0)
char *ascii_strlower(char *t) {
assert(t);
int flags;
return -errno;
if (nonblock)
return -errno;
int flags;
return -errno;
if (cloexec)
return -errno;
DIR *d;
return -errno;
goto finish;
if (except) {
bool found;
found = false;
for (i = 0; i < n_except; i++)
found = true;
if (found)
goto finish;
closedir(d);
bool chars_intersect(const char *a, const char *b) {
if (strchr(b, *p))
assert(l > 0);
return NULL;
return NULL;
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 * const table[] = {
int fd, r = 0;
return -errno;
if (vt < 0) {
return -errno;
r = -errno;
assert(f);
size_t k;
return -EIO;
if (need_nl)
*ret = c;
return -EIO;
return -EBADMSG;
if (need_nl)
*need_nl = false;
bool need_nl = true;
if (r == -EBADMSG) {
if (need_nl)
*ret = c;
r = -errno;
goto finish;
r = -errno;
int fd, r;
return -errno;
return -errno;
return -ENOTTY;
return fd;
ssize_t l;
return -errno;
return -errno;
r = -errno;
goto fail;
r = -errno;
goto fail;
if (notify >= 0)
goto fail;
return -errno;
r = -errno;
goto fail;
struct inotify_event e;
ssize_t l;
r = -errno;
r = -EIO;
goto fail;
r = -errno;
goto fail;
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;
int path_is_mount_point(const char *t) {
struct stat a, b;
char *copy;
if (lstat(t, &a) < 0) {
return -errno;
return -ENOMEM;
return -errno;
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;
return -errno;
return status == 0;
DIR *d;
return -errno;
if (!de) {
closedir(d);
unsigned long long random_ull(void) {
int fd;
ssize_t r;
goto fallback;
if (r != sizeof(ull))
goto fallback;
return ull;
int sig;
char* gethostname_malloc(void) {
struct utsname u;
if (u.nodename[0])
int getmachineid_malloc(char **b) {
assert(b);
strstrip(*b);
char* getlogname_malloc(void) {
long bufsize;
if (uid == 0)
return NULL;
return name;
return NULL;
return name;
int getttyname_malloc(char **r) {
assert(r);
return -errno;
p = path;
if (!(c = strdup(p)))
return -ENOMEM;
DIR *d;
int ret = 0;
return -errno;
bool is_dir;
if (ret == 0)
ret = -r;
if (!de)
if (ret == 0)
if (is_dir) {
int subdir_fd;
if (ret == 0)
if (ret == 0)
ret = r;
if (ret == 0)
} else if (!only_dirs) {
if (ret == 0)
closedir(d);
return ret;
int fd;
return -errno;
return -errno;
if (delete_root)
r = -errno;
return -errno;
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[] = {