util.c revision 57f3067825d9361d7487f272bfaff3b36c684c62
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/***
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering This file is part of systemd.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Copyright 2010 Lennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
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
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
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 Poettering***/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <assert.h>
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner#include <string.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <unistd.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <errno.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <stdlib.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <signal.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <stdio.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <syslog.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sched.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/resource.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <linux/sched.h>
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers#include <sys/types.h>
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers#include <sys/stat.h>
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers#include <fcntl.h>
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers#include <dirent.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/ioctl.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <linux/vt.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <linux/tiocl.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <termios.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <stdarg.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/inotify.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/poll.h>
0affed79d2e30013f07cb94e6f07e3fcb81c02faLennart Poettering#include <libgen.h>
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek#include <ctype.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/prctl.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/utsname.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <pwd.h>
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers#include <netinet/ip.h>
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallace#include <linux/kd.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <dlfcn.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/wait.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/time.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <glob.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <grp.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/mman.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/vfs.h>
1b12a7b5896f94bdf33b3a6661ebabd761ea6adcHarald Hoyer#include <linux/magic.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "macro.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "util.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "ioprio.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "missing.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "log.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "strv.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "label.h"
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering#include "path-util.h"
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering#include "exit-status.h"
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering#include "hashmap.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint saved_argc = 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringchar **saved_argv = NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringsize_t page_size(void) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering static __thread size_t pgsz = 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering long r;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (_likely_(pgsz > 0))
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return pgsz;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = sysconf(_SC_PAGESIZE);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(r > 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering pgsz = (size_t) r;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return pgsz;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringbool streq_ptr(const char *a, const char *b) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* Like streq(), but tries to make sense of NULL pointers */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (a && b)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return streq(a, b);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!a && !b)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return true;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return false;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringusec_t now(clockid_t clock_id) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering struct timespec ts;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(clock_gettime(clock_id, &ts) == 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return timespec_load(&ts);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversdual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek assert(ts);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers ts->realtime = now(CLOCK_REALTIME);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers ts->monotonic = now(CLOCK_MONOTONIC);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers return ts;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers}
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sieversdual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers int64_t delta;
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers assert(ts);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers ts->realtime = u;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers if (u == 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering ts->monotonic = 0;
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers else {
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u;
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers ts->monotonic = now(CLOCK_MONOTONIC);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if ((int64_t) ts->monotonic > delta)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers ts->monotonic -= delta;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering ts->monotonic = 0;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers }
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return ts;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringusec_t timespec_load(const struct timespec *ts) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert(ts);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers return
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering (usec_t) ts->tv_sec * USEC_PER_SEC +
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering (usec_t) ts->tv_nsec / NSEC_PER_USEC;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstruct timespec *timespec_store(struct timespec *ts, usec_t u) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(ts);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers ts->tv_sec = (time_t) (u / USEC_PER_SEC);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return ts;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers}
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poetteringusec_t timeval_load(const struct timeval *tv) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(tv);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers return
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poettering (usec_t) tv->tv_sec * USEC_PER_SEC +
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering (usec_t) tv->tv_usec;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poetteringstruct timeval *timeval_store(struct timeval *tv, usec_t u) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(tv);
c49b30a23583ff39daaa26696bcab478d2fee0bbLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers tv->tv_sec = (time_t) (u / USEC_PER_SEC);
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return tv;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringbool endswith(const char *s, const char *postfix) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering size_t sl, pl;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(s);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(postfix);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sl = strlen(s);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering pl = strlen(postfix);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (pl == 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return true;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (sl < pl)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return false;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return memcmp(s + sl - pl, postfix, pl) == 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringbool startswith(const char *s, const char *prefix) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering size_t sl, pl;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(s);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(prefix);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sl = strlen(s);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering pl = strlen(prefix);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (pl == 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return true;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (sl < pl)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return false;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return memcmp(s, prefix, pl) == 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringbool startswith_no_case(const char *s, const char *prefix) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering size_t sl, pl;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering unsigned i;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(s);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(prefix);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sl = strlen(s);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering pl = strlen(prefix);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (pl == 0)
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna return true;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (sl < pl)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return false;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for(i = 0; i < pl; ++i)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (tolower(s[i]) != tolower(prefix[i]))
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return false;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return true;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringbool first_word(const char *s, const char *word) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering size_t sl, wl;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(s);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(word);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sl = strlen(s);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering wl = strlen(word);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (sl < wl)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return false;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (wl == 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return true;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (memcmp(s, word, wl) != 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return false;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return s[wl] == 0 ||
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering strchr(WHITESPACE, s[wl]);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint close_nointr(int fd) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(fd >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for (;;) {
2f7a4867babd3fd382e5495f21724358f30fa67dMichal Sekletar int r;
2f7a4867babd3fd382e5495f21724358f30fa67dMichal Sekletar
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = close(fd);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (r >= 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return r;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (errno != EINTR)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return -errno;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
0affed79d2e30013f07cb94e6f07e3fcb81c02faLennart Poetteringvoid close_nointr_nofail(int fd) {
0affed79d2e30013f07cb94e6f07e3fcb81c02faLennart Poettering int saved_errno = errno;
0affed79d2e30013f07cb94e6f07e3fcb81c02faLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* like close_nointr() but cannot fail, and guarantees errno
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering * is unchanged */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(close_nointr(fd) == 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering errno = saved_errno;
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringvoid close_many(const int fds[], unsigned n_fd) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering unsigned i;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for (i = 0; i < n_fd; i++)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering close_nointr_nofail(fds[i]);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna}
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagnaint parse_boolean(const char *v) {
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna assert(v);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna return 1;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna return 0;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer return -EINVAL;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna}
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagnaint parse_pid(const char *s, pid_t* ret_pid) {
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna unsigned long ul = 0;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna pid_t pid;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna int r;
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna assert(s);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna assert(ret_pid);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna r = safe_atolu(s, &ul);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna if (r < 0)
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna return r;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna pid = (pid_t) ul;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna if ((unsigned long) pid != ul)
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna return -ERANGE;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna if (pid <= 0)
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek return -ERANGE;
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna *ret_pid = pid;
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek return 0;
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek}
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmekint parse_uid(const char *s, uid_t* ret_uid) {
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna unsigned long ul = 0;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna uid_t uid;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna int r;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna assert(s);
8333c77edf8fd1654cd96f3f6ee0f078dd64b58bZbigniew Jędrzejewski-Szmek assert(ret_uid);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek r = safe_atolu(s, &ul);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna if (r < 0)
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna return r;
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna uid = (uid_t) ul;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if ((unsigned long) uid != ul)
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek return -ERANGE;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna *ret_uid = uid;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna return 0;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna}
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagnaint safe_atou(const char *s, unsigned *ret_u) {
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna char *x = NULL;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna unsigned long l;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek assert(s);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna assert(ret_u);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna errno = 0;
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek l = strtoul(s, &x, 0);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!x || *x || errno)
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek return errno ? -errno : -EINVAL;
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if ((unsigned long) (unsigned) l != l)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return -ERANGE;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *ret_u = (unsigned) l;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return 0;
7c2d80944afb4196f2eff614e8da1450dffcbeaaThomas Hindoe Paaboel Andersen}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmekint safe_atoi(const char *s, int *ret_i) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering char *x = NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering long l;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert(s);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(ret_i);
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering errno = 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering l = strtol(s, &x, 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!x || *x || errno)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return errno ? -errno : -EINVAL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if ((long) (int) l != l)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return -ERANGE;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *ret_i = (int) l;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint safe_atollu(const char *s, long long unsigned *ret_llu) {
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering char *x = NULL;
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering unsigned long long l;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(s);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(ret_llu);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers errno = 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering l = strtoull(s, &x, 0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering if (!x || *x || errno)
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering return errno ? -errno : -EINVAL;
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering *ret_llu = l;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint safe_atolli(const char *s, long long int *ret_lli) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering char *x = NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering long long l;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(s);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(ret_lli);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering errno = 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering l = strtoll(s, &x, 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!x || *x || errno)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return errno ? -errno : -EINVAL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *ret_lli = l;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
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 char *current;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering current = *state ? *state : (char*) c;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!*current || *c == 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering current += strspn(current, separator);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *l = strcspn(current, separator);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *state = current+*l;
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek return (char*) current;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
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 char *current, *e;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering bool escaped = false;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer current = *state ? *state : (char*) c;
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!*current || *c == 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering current += strspn(current, WHITESPACE);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek if (*current == '\'') {
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek current ++;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for (e = current; *e; e++) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (escaped)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering escaped = false;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else if (*e == '\\')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering escaped = true;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else if (*e == '\'')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *l = e-current;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *state = *e == 0 ? e : e+1;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } else if (*current == '\"') {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering current ++;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for (e = current; *e; e++) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (escaped)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering escaped = false;
7c2d80944afb4196f2eff614e8da1450dffcbeaaThomas Hindoe Paaboel Andersen else if (*e == '\\')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering escaped = true;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else if (*e == '\"')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers *l = e-current;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *state = *e == 0 ? e : e+1;
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering } else {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for (e = current; *e; e++) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (escaped)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering escaped = false;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else if (*e == '\\')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering escaped = true;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else if (strchr(WHITESPACE, *e))
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *l = e-current;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *state = e;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return (char*) current;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
6b2b6f30e38d67b032d6bdc6b47ae05e143e96c5Michal Schmidt
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint get_parent_of_pid(pid_t pid, pid_t *_ppid) {
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering int r;
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering _cleanup_fclose_ FILE *f = NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering char fn[PATH_MAX], line[LINE_MAX], *p;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering long unsigned ppid;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(pid > 0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert(_ppid);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers char_array_0(fn);
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering f = fopen(fn, "re");
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering if (!f)
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering return -errno;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!fgets(line, sizeof(line), f)) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers r = feof(f) ? -EIO : -errno;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering fclose(f);
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer return r;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering }
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
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
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering p = strrchr(line, ')');
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering if (!p)
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering return -EIO;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering p++;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering if (sscanf(p, " "
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*c " /* state */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%lu ", /* ppid */
c62e11ce3966c55d23520b9f0785c7e839cf7f37Lennart Poettering &ppid) != 1)
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering return -EIO;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering if ((long unsigned) (pid_t) ppid != ppid)
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering return -ERANGE;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering *_ppid = (pid_t) ppid;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering return 0;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering}
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poetteringint get_starttime_of_pid(pid_t pid, unsigned long long *st) {
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering _cleanup_fclose_ FILE *f = NULL;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering char fn[PATH_MAX], line[LINE_MAX], *p;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering assert(pid > 0);
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering assert(st);
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering char_array_0(fn);
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering f = fopen(fn, "re");
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering if (!f)
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering return -errno;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering if (!fgets(line, sizeof(line), f)) {
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering if (ferror(f))
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering return -errno;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering return -EIO;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering }
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
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
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering p = strrchr(line, ')');
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering if (!p)
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering return -EIO;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering p++;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering if (sscanf(p, " "
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*c " /* state */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*d " /* ppid */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*d " /* pgrp */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*d " /* session */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*d " /* tty_nr */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*d " /* tpgid */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*u " /* flags */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*u " /* minflt */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*u " /* cminflt */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*u " /* majflt */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*u " /* cmajflt */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*u " /* utime */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*u " /* stime */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*d " /* cutime */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*d " /* cstime */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*d " /* priority */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*d " /* nice */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*d " /* num_threads */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%*d " /* itrealvalue */
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "%llu " /* starttime */,
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering st) != 1)
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering return -EIO;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering return 0;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering}
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poetteringint write_one_line_file(const char *fn, const char *line) {
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering _cleanup_fclose_ FILE *f = NULL;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering assert(fn);
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering assert(line);
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering f = fopen(fn, "we");
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering if (!f)
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering return -errno;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering errno = 0;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering if (fputs(line, f) < 0)
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering return errno ? -errno : -EIO;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering if (!endswith(line, "\n"))
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering fputc('\n', f);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering fflush(f);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
82c1d8f4eb74ddd9be2c9b9b56d9dc564c599effLennart Poettering if (ferror(f))
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering return errno ? -errno : -EIO;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering return 0;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering}
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
a86a47ce1f63476631635fbcbc10af8877172114Lennart Poetteringint fchmod_umask(int fd, mode_t m) {
a86a47ce1f63476631635fbcbc10af8877172114Lennart Poettering mode_t u;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering int r;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering u = umask(0777);
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering r = fchmod(fd, m & (~u)) < 0 ? -errno : 0;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering umask(u);
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return r;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering}
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poetteringint write_one_line_file_atomic(const char *fn, const char *line) {
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering FILE *f;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering int r;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering char *p;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(fn);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(line);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = fopen_temporary(fn, &f, &p);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (r < 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return r;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering fchmod_umask(fileno(f), 0644);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering errno = 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (fputs(line, f) < 0) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = -errno;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering goto finish;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (!endswith(line, "\n"))
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers fputc('\n', f);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers fflush(f);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (ferror(f)) {
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering if (errno != 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = -errno;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = -EIO;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } else {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (rename(p, fn) < 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = -errno;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else
4b4bec19582c3ad2b7d25116f3c22c783274feadThomas Hindoe Paaboel Andersen r = 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringfinish:
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (r < 0)
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering unlink(p);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering fclose(f);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering free(p);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return r;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint read_one_line_file(const char *fn, char **line) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering _cleanup_fclose_ FILE *f = NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering char t[LINE_MAX], *c;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(fn);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(line);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen f = fopen(fn, "re");
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen if (!f)
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen return -errno;
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (!fgets(t, sizeof(t), f)) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (ferror(f))
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers return errno ? -errno : -EIO;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers t[0] = 0;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers }
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers c = strdup(t);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (!c)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return -ENOMEM;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering truncate_nl(c);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *line = c;
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint read_full_file(const char *fn, char **contents, size_t *size) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering _cleanup_fclose_ FILE *f = NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering size_t n, l;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering _cleanup_free_ char *buf = NULL;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers struct stat st;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering f = fopen(fn, "re");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!f)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return -errno;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (fstat(fileno(f), &st) < 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return -errno;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* Safety check */
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (st.st_size > 4*1024*1024)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return -E2BIG;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering n = st.st_size > 0 ? st.st_size : LINE_MAX;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering l = 0;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering for (;;) {
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering char *t;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering size_t k;
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering t = realloc(buf, n+1);
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering if (!t)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return -ENOMEM;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering buf = t;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering k = fread(buf + l, 1, n - l, f);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (k <= 0) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (ferror(f))
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return -errno;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering l += k;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering n *= 2;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* Safety check */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (n > 4*1024*1024)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return -E2BIG;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering buf[l] = 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *contents = buf;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering buf = NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (size)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *size = l;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint parse_env_file(
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *fname,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *separator, ...) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering int r = 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering char *contents = NULL, *p;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(fname);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(separator);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if ((r = read_full_file(fname, &contents, NULL)) < 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return r;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering p = contents;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for (;;) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *key = NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering p += strspn(p, separator);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering p += strspn(p, WHITESPACE);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!*p)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!strchr(COMMENTS, *p)) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering va_list ap;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering char **value;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering va_start(ap, separator);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering while ((key = va_arg(ap, char *))) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering size_t n;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering char *v;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers value = va_arg(ap, char **);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
84f6181c2ac99a0514ca5e0c8fc8c8e284caf789Lennart Poettering n = strlen(key);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (strncmp(p, key, n) != 0 ||
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner p[n] != '=')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering continue;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering p += n + 1;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering n = strcspn(p, separator);
84f6181c2ac99a0514ca5e0c8fc8c8e284caf789Lennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (n >= 2 &&
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering strchr(QUOTES, p[0]) &&
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers p[n-1] == p[0])
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers v = strndup(p+1, n-2);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers else
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers v = strndup(p, n);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!v) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers r = -ENOMEM;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering va_end(ap);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers goto fail;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
84f6181c2ac99a0514ca5e0c8fc8c8e284caf789Lennart Poettering if (v[0] == '\0') {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* return empty value strings as NULL */
free(v);
v = NULL;
}
free(*value);
*value = v;
p += n;
r ++;
break;
}
va_end(ap);
}
if (!key)
p += strcspn(p, separator);
}
fail:
free(contents);
return r;
}
int load_env_file(
const char *fname,
char ***rl) {
FILE *f;
char **m = NULL;
int r;
assert(fname);
assert(rl);
if (!(f = fopen(fname, "re")))
return -errno;
while (!feof(f)) {
char l[LINE_MAX], *p, *u;
char **t;
if (!fgets(l, sizeof(l), f)) {
if (feof(f))
break;
r = -errno;
goto finish;
}
p = strstrip(l);
if (!*p)
continue;
if (strchr(COMMENTS, *p))
continue;
if (!(u = normalize_env_assignment(p))) {
r = log_oom();
goto finish;
}
t = strv_append(m, u);
free(u);
if (!t) {
r = log_oom();
goto finish;
}
strv_free(m);
m = t;
}
r = 0;
*rl = m;
m = NULL;
finish:
if (f)
fclose(f);
strv_free(m);
return r;
}
int write_env_file(const char *fname, char **l) {
char **i, *p;
FILE *f;
int r;
r = fopen_temporary(fname, &f, &p);
if (r < 0)
return r;
fchmod_umask(fileno(f), 0644);
errno = 0;
STRV_FOREACH(i, l) {
fputs(*i, f);
fputc('\n', f);
}
fflush(f);
if (ferror(f)) {
if (errno != 0)
r = -errno;
else
r = -EIO;
} else {
if (rename(p, fname) < 0)
r = -errno;
else
r = 0;
}
if (r < 0)
unlink(p);
fclose(f);
free(p);
return r;
}
char *truncate_nl(char *s) {
assert(s);
s[strcspn(s, NEWLINE)] = 0;
return s;
}
int get_process_comm(pid_t pid, char **name) {
int r;
assert(name);
if (pid == 0)
r = read_one_line_file("/proc/self/comm", name);
else {
char *p;
if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0)
return -ENOMEM;
r = read_one_line_file(p, name);
free(p);
}
return r;
}
int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
char *r, *k;
int c;
bool space = false;
size_t left;
FILE *f;
assert(max_length > 0);
assert(line);
if (pid == 0)
f = fopen("/proc/self/cmdline", "re");
else {
char *p;
if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0)
return -ENOMEM;
f = fopen(p, "re");
free(p);
}
if (!f)
return -errno;
r = new(char, max_length);
if (!r) {
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 (!comm_fallback)
return -ENOENT;
h = get_process_comm(pid, &t);
if (h < 0)
return h;
r = strjoin("[", t, "]", NULL);
free(t);
if (!r)
return -ENOMEM;
}
*line = r;
return 0;
}
int is_kernel_thread(pid_t pid) {
char *p;
size_t count;
char c;
bool eof;
FILE *f;
if (pid == 0)
return 0;
if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0)
return -ENOMEM;
f = fopen(p, "re");
free(p);
if (!f)
return -errno;
count = fread(&c, 1, 1, f);
eof = feof(f);
fclose(f);
/* Kernel threads have an empty cmdline */
if (count <= 0)
return eof ? 1 : -errno;
return 0;
}
int get_process_exe(pid_t pid, char **name) {
int r;
assert(name);
if (pid == 0)
r = readlink_malloc("/proc/self/exe", name);
else {
char *p;
if (asprintf(&p, "/proc/%lu/exe", (unsigned long) pid) < 0)
return -ENOMEM;
r = readlink_malloc(p, name);
free(p);
}
return r;
}
static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
char *p;
FILE *f;
int r;
assert(uid);
if (pid == 0)
return getuid();
if (asprintf(&p, "/proc/%lu/status", (unsigned long) pid) < 0)
return -ENOMEM;
f = fopen(p, "re");
free(p);
if (!f)
return -errno;
while (!feof(f)) {
char line[LINE_MAX], *l;
if (!fgets(line, sizeof(line), f)) {
if (feof(f))
break;
r = -errno;
goto finish;
}
l = strstrip(line);
if (startswith(l, field)) {
l += strlen(field);
l += strspn(l, WHITESPACE);
l[strcspn(l, WHITESPACE)] = 0;
r = parse_uid(l, uid);
goto finish;
}
}
r = -EIO;
finish:
fclose(f);
return r;
}
int get_process_uid(pid_t pid, uid_t *uid) {
return get_process_id(pid, "Uid:", uid);
}
int get_process_gid(pid_t pid, gid_t *gid) {
return get_process_id(pid, "Gid:", gid);
}
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 readlink_and_canonicalize(const char *p, char **r) {
char *t, *s;
int j;
assert(p);
assert(r);
j = readlink_and_make_absolute(p, &t);
if (j < 0)
return j;
s = canonicalize_file_name(t);
if (s) {
free(t);
*r = s;
} else
*r = t;
path_kill_slashes(*r);
return 0;
}
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;
/* Drops trailing whitespace. Modifies the string in
* place. Returns pointer to first non-space character */
s += strspn(s, WHITESPACE);
for (e = strchr(s, 0); e > s; e --)
if (!strchr(WHITESPACE, e[-1]))
break;
*e = 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;
}
bool in_charset(const char *s, const char* charset) {
const char *i;
assert(s);
assert(charset);
for (i = s; *i; i++)
if (!strchr(charset, *i))
return false;
return true;
}
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 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. */
r = new(char, strlen(s)*4 + 1);
if (!r)
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_with_prefix(const char *s, size_t length, const char *prefix) {
char *r, *t;
const char *f;
size_t pl;
assert(s);
/* Undoes C style string escaping, and optionally prefixes it. */
pl = prefix ? strlen(prefix) : 0;
r = new(char, pl+length+1);
if (!r)
return r;
if (prefix)
memcpy(r, prefix, pl);
for (f = s, t = r + pl; 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;
a = unhexchar(f[1]);
b = unhexchar(f[2]);
if (a < 0 || b < 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;
a = unoctchar(f[0]);
b = unoctchar(f[1]);
c = unoctchar(f[2]);
if (a < 0 || b < 0 || c < 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_length(const char *s, size_t length) {
return cunescape_length_with_prefix(s, length, NULL);
}
char *cunescape(const char *s) {
assert(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 *ascii_strlower(char *t) {
char *p;
assert(t);
for (p = t; *p; p++)
if (*p >= 'A' && *p <= 'Z')
*p = *p - 'A' + 'a';
return t;
}
static bool ignore_file_allow_backup(const char *filename) {
assert(filename);
return
filename[0] == '.' ||
streq(filename, "lost+found") ||
streq(filename, "aquota.user") ||
streq(filename, "aquota.group") ||
endswith(filename, ".rpmnew") ||
endswith(filename, ".rpmsave") ||
endswith(filename, ".rpmorig") ||
endswith(filename, ".dpkg-old") ||
endswith(filename, ".dpkg-new") ||
endswith(filename, ".swp");
}
bool ignore_file(const char *filename) {
assert(filename);
if (endswith(filename, "~"))
return false;
return ignore_file_allow_backup(filename);
}
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;
}
static bool fd_in_set(int fd, const int fdset[], unsigned n_fdset) {
unsigned i;
assert(n_fdset == 0 || fdset);
for (i = 0; i < n_fdset; i++)
if (fdset[i] == fd)
return true;
return false;
}
int close_all_fds(const int except[], unsigned n_except) {
DIR *d;
struct dirent *de;
int r = 0;
assert(n_except == 0 || except);
d = opendir("/proc/self/fd");
if (!d) {
int fd;
struct rlimit rl;
/* When /proc isn't available (for example in chroots)
* the fallback is brute forcing through the fd
* table */
assert_se(getrlimit(RLIMIT_NOFILE, &rl) >= 0);
for (fd = 3; fd < (int) rl.rlim_max; fd ++) {
if (fd_in_set(fd, except, n_except))
continue;
if (close_nointr(fd) < 0)
if (errno != EBADF && r == 0)
r = -errno;
}
return r;
}
while ((de = readdir(d))) {
int fd = -1;
if (ignore_file(de->d_name))
continue;
if (safe_atoi(de->d_name, &fd) < 0)
/* Let's better ignore this, just in case */
continue;
if (fd < 3)
continue;
if (fd == dirfd(d))
continue;
if (fd_in_set(fd, except, n_except))
continue;
if (close_nointr(fd) < 0) {
/* Valgrind has its own FD and doesn't want to have it closed */
if (errno != EBADF && r == 0)
r = -errno;
}
}
closedir(d);
return r;
}
bool chars_intersect(const char *a, const char *b) {
const char *p;
/* Returns true if any of the chars in a are in b. */
for (p = a; *p; p++)
if (strchr(b, *p))
return true;
return false;
}
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_timestamp_pretty(char *buf, size_t l, usec_t t) {
usec_t n, d;
n = now(CLOCK_REALTIME);
if (t <= 0 || t > n || t + USEC_PER_DAY*7 <= t)
return NULL;
d = n - t;
if (d >= USEC_PER_YEAR)
snprintf(buf, l, "%llu years and %llu months ago",
(unsigned long long) (d / USEC_PER_YEAR),
(unsigned long long) ((d % USEC_PER_YEAR) / USEC_PER_MONTH));
else if (d >= USEC_PER_MONTH)
snprintf(buf, l, "%llu months and %llu days ago",
(unsigned long long) (d / USEC_PER_MONTH),
(unsigned long long) ((d % USEC_PER_MONTH) / USEC_PER_DAY));
else if (d >= USEC_PER_WEEK)
snprintf(buf, l, "%llu weeks and %llu days ago",
(unsigned long long) (d / USEC_PER_WEEK),
(unsigned long long) ((d % USEC_PER_WEEK) / USEC_PER_DAY));
else if (d >= 2*USEC_PER_DAY)
snprintf(buf, l, "%llu days ago", (unsigned long long) (d / USEC_PER_DAY));
else if (d >= 25*USEC_PER_HOUR)
snprintf(buf, l, "1 day and %lluh ago",
(unsigned long long) ((d - USEC_PER_DAY) / USEC_PER_HOUR));
else if (d >= 6*USEC_PER_HOUR)
snprintf(buf, l, "%lluh ago",
(unsigned long long) (d / USEC_PER_HOUR));
else if (d >= USEC_PER_HOUR)
snprintf(buf, l, "%lluh %llumin ago",
(unsigned long long) (d / USEC_PER_HOUR),
(unsigned long long) ((d % USEC_PER_HOUR) / USEC_PER_MINUTE));
else if (d >= 5*USEC_PER_MINUTE)
snprintf(buf, l, "%llumin ago",
(unsigned long long) (d / USEC_PER_MINUTE));
else if (d >= USEC_PER_MINUTE)
snprintf(buf, l, "%llumin %llus ago",
(unsigned long long) (d / USEC_PER_MINUTE),
(unsigned long long) ((d % USEC_PER_MINUTE) / USEC_PER_SEC));
else if (d >= USEC_PER_SEC)
snprintf(buf, l, "%llus ago",
(unsigned long long) (d / USEC_PER_SEC));
else if (d >= USEC_PER_MSEC)
snprintf(buf, l, "%llums ago",
(unsigned long long) (d / USEC_PER_MSEC));
else if (d > 0)
snprintf(buf, l, "%lluus ago",
(unsigned long long) d);
else
snprintf(buf, l, "now");
buf[l-1] = 0;
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;
if (t == 0) {
snprintf(p, l, "0");
p[l-1] = 0;
return p;
}
/* The result of this function can be parsed with parse_usec */
for (i = 0; i < ELEMENTSOF(table); i++) {
int k;
size_t n;
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 table[] =
"cifs\0"
"smbfs\0"
"ncpfs\0"
"nfs\0"
"nfs4\0"
"gfs\0"
"gfs2\0";
return nulstr_contains(table, fstype);
}
int chvt(int vt) {
_cleanup_close_ int fd;
fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
if (fd < 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)
return -errno;
return 0;
}
int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) {
struct termios old_termios, new_termios;
char c;
char line[LINE_MAX];
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;
if (t != (usec_t) -1) {
if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0) {
tcsetattr(fileno(f), TCSADRAIN, &old_termios);
return -ETIMEDOUT;
}
}
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 (t != (usec_t) -1)
if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0)
return -ETIMEDOUT;
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, ...) {
bool on_tty;
assert(ret);
assert(replies);
assert(text);
on_tty = isatty(STDOUT_FILENO);
for (;;) {
va_list ap;
char c;
int r;
bool need_nl = true;
if (on_tty)
fputs(ANSI_HIGHLIGHT_ON, stdout);
va_start(ap, text);
vprintf(text, ap);
va_end(ap);
if (on_tty)
fputs(ANSI_HIGHLIGHT_OFF, stdout);
fflush(stdout);
r = read_one_char(stdin, &c, (usec_t) -1, &need_nl);
if (r < 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_fd(int fd, bool switch_to_text) {
struct termios termios;
int r = 0;
/* Set terminal to some sane defaults */
assert(fd >= 0);
/* We leave locked terminal attributes untouched, so that
* Plymouth may set whatever it wants to set, and we don't
* interfere with that. */
/* Disable exclusive mode, just in case */
ioctl(fd, TIOCNXCL);
/* Switch to text mode */
if (switch_to_text)
ioctl(fd, KDSETMODE, KD_TEXT);
/* Enable console unicode mode */
ioctl(fd, KDSKBMODE, K_UNICODE);
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 reset_terminal(const char *name) {
int fd, r;
fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
if (fd < 0)
return fd;
r = reset_terminal_fd(fd, true);
close_nointr_nofail(fd);
return r;
}
int open_terminal(const char *name, int mode) {
int fd, r;
unsigned c = 0;
/*
* If a TTY is in the process of being closed opening it might
* cause EIO. This is horribly awful, but unlikely to be
* changed in the kernel. Hence we work around this problem by
* retrying a couple of times.
*
* https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245
*/
for (;;) {
fd = open(name, mode);
if (fd >= 0)
break;
if (errno != EIO)
return -errno;
/* Max 1s in total */
if (c >= 20)
return -errno;
usleep(50 * USEC_PER_MSEC);
c++;
}
if (fd < 0)
return -errno;
r = isatty(fd);
if (r < 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[LINE_MAX];
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,
usec_t timeout) {
int fd = -1, notify = -1, r = 0, wd = -1;
usec_t ts = 0;
struct sigaction sa_old, sa_new;
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 (timeout != (usec_t) -1)
ts = now(CLOCK_MONOTONIC);
if (!fail && !force) {
notify = inotify_init1(IN_CLOEXEC | (timeout != (usec_t) -1 ? IN_NONBLOCK : 0));
if (notify < 0) {
r = -errno;
goto fail;
}
wd = inotify_add_watch(notify, name, IN_CLOSE);
if (wd < 0) {
r = -errno;
goto fail;
}
}
for (;;) {
if (notify >= 0) {
r = flush_fd(notify);
if (r < 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 */
fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
if (fd < 0)
return fd;
/* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
* if we already own the tty. */
zero(sa_new);
sa_new.sa_handler = SIG_IGN;
sa_new.sa_flags = SA_RESTART;
assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
/* First, try to get the tty */
if (ioctl(fd, TIOCSCTTY, force) < 0)
r = -errno;
assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
/* Sometimes it makes sense to ignore TIOCSCTTY
* returning EPERM, i.e. when very likely we already
* are have this controlling terminal. */
if (r < 0 && r == -EPERM && ignore_tiocstty_eperm)
r = 0;
if (r < 0 && (force || fail || r != -EPERM)) {
goto fail;
}
if (r >= 0)
break;
assert(!fail);
assert(!force);
assert(notify >= 0);
for (;;) {
uint8_t inotify_buffer[sizeof(struct inotify_event) + FILENAME_MAX];
ssize_t l;
struct inotify_event *e;
if (timeout != (usec_t) -1) {
usec_t n;
n = now(CLOCK_MONOTONIC);
if (ts + timeout < n) {
r = -ETIMEDOUT;
goto fail;
}
r = fd_wait_for_event(fd, POLLIN, ts + timeout - n);
if (r < 0)
goto fail;
if (r == 0) {
r = -ETIMEDOUT;
goto fail;
}
}
l = read(notify, inotify_buffer, sizeof(inotify_buffer));
if (l < 0) {
if (errno == EINTR || errno == EAGAIN)
continue;
r = -errno;
goto fail;
}
e = (struct inotify_event*) inotify_buffer;
while (l > 0) {
size_t step;
if (e->wd != wd || !(e->mask & IN_CLOSE)) {
r = -EIO;
goto fail;
}
step = sizeof(struct inotify_event) + e->len;
assert(step <= (size_t) l);
e = (struct inotify_event*) ((uint8_t*) e + step);
l -= step;
}
break;
}
/* We close the tty fd here since if the old session
* ended our handle will be dead. It's important that
* we do this after sleeping, so that we don't enter
* an endless loop. */
close_nointr_nofail(fd);
}
if (notify >= 0)
close_nointr_nofail(notify);
r = reset_terminal_fd(fd, true);
if (r < 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|O_CLOEXEC)) < 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;
k = write(fd, p, nbytes);
if (k <= 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 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 }, /* default is 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 parse_nsec(const char *t, nsec_t *nsec) {
static const struct {
const char *suffix;
nsec_t nsec;
} table[] = {
{ "sec", NSEC_PER_SEC },
{ "s", NSEC_PER_SEC },
{ "min", NSEC_PER_MINUTE },
{ "hr", NSEC_PER_HOUR },
{ "h", NSEC_PER_HOUR },
{ "d", NSEC_PER_DAY },
{ "w", NSEC_PER_WEEK },
{ "msec", NSEC_PER_MSEC },
{ "ms", NSEC_PER_MSEC },
{ "m", NSEC_PER_MINUTE },
{ "usec", NSEC_PER_USEC },
{ "us", NSEC_PER_USEC },
{ "nsec", 1ULL },
{ "ns", 1ULL },
{ "", 1ULL }, /* default is nsec */
};
const char *p;
nsec_t r = 0;
assert(t);
assert(nsec);
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 += (nsec_t) l * table[i].nsec;
p = e + strlen(table[i].suffix);
break;
}
if (i >= ELEMENTSOF(table))
return -EINVAL;
} while (*p != 0);
*nsec = r;
return 0;
}
int parse_bytes(const char *t, off_t *bytes) {
static const struct {
const char *suffix;
off_t factor;
} table[] = {
{ "B", 1 },
{ "K", 1024ULL },
{ "M", 1024ULL*1024ULL },
{ "G", 1024ULL*1024ULL*1024ULL },
{ "T", 1024ULL*1024ULL*1024ULL*1024ULL },
{ "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
{ "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
{ "", 1 },
};
const char *p;
off_t r = 0;
assert(t);
assert(bytes);
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 += (off_t) l * table[i].factor;
p = e + strlen(table[i].suffix);
break;
}
if (i >= ELEMENTSOF(table))
return -EINVAL;
} while (*p != 0);
*bytes = r;
return 0;
}
int make_stdio(int fd) {
int r, s, t;
assert(fd >= 0);
r = dup3(fd, STDIN_FILENO, 0);
s = dup3(fd, STDOUT_FILENO, 0);
t = dup3(fd, STDERR_FILENO, 0);
if (fd >= 3)
close_nointr_nofail(fd);
if (r < 0 || s < 0 || t < 0)
return -errno;
/* We rely here that the new fd has O_CLOEXEC not set */
return 0;
}
int make_null_stdio(void) {
int null_fd;
null_fd = open("/dev/null", O_RDWR|O_NOCTTY);
if (null_fd < 0)
return -errno;
return make_stdio(null_fd);
}
bool is_device_path(const char *path) {
/* Returns true on paths that refer to a device, either in
* sysfs or in /dev */
return
path_startswith(path, "/dev/") ||
path_startswith(path, "/sys/");
}
int dir_is_empty(const char *path) {
_cleanup_closedir_ DIR *d;
int r;
d = opendir(path);
if (!d)
return -errno;
for (;;) {
struct dirent buf, *de;
r = readdir_r(d, &buf, &de);
if (r > 0)
return -r;
if (!de)
return 1;
if (!ignore_file(de->d_name))
return 0;
}
}
unsigned long long random_ull(void) {
_cleanup_close_ int fd;
uint64_t ull;
ssize_t r;
fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
goto fallback;
r = loop_read(fd, &ull, sizeof(ull), true);
if (r != sizeof(ull))
goto fallback;
return ull;
fallback:
return random() * RAND_MAX + random();
}
void rename_process(const char name[8]) {
assert(name);
/* This is a like a poor man's setproctitle(). It changes the
* comm field, argv[0], and also the glibc's internally used
* name of the process. For the first one a limit of 16 chars
* applies, to the second one usually one of 10 (i.e. length
* of "/sbin/init"), to the third one one of 7 (i.e. length of
* "systemd"). If you pass a longer string it will be
* truncated */
prctl(PR_SET_NAME, name);
if (program_invocation_name)
strncpy(program_invocation_name, name, strlen(program_invocation_name));
if (saved_argc > 0) {
int i;
if (saved_argv[0])
strncpy(saved_argv[0], name, strlen(saved_argv[0]));
for (i = 1; i < saved_argc; i++) {
if (!saved_argv[i])
break;
memset(saved_argv[i], 0, strlen(saved_argv[i]));
}
}
}
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 (!isempty(u.nodename) && !streq(u.nodename, "(none)"))
return strdup(u.nodename);
return strdup(u.sysname);
}
bool hostname_is_set(void) {
struct utsname u;
assert_se(uname(&u) >= 0);
return !isempty(u.nodename) && !streq(u.nodename, "(none)");
}
static char *lookup_uid(uid_t uid) {
long bufsize;
char *name;
_cleanup_free_ char *buf = NULL;
struct passwd pwbuf, *pw = NULL;
/* Shortcut things to avoid NSS lookups */
if (uid == 0)
return strdup("root");
bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
if (bufsize <= 0)
bufsize = 4096;
buf = malloc(bufsize);
if (!buf)
return NULL;
if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw)
return strdup(pw->pw_name);
if (asprintf(&name, "%lu", (unsigned long) uid) < 0)
return NULL;
return name;
}
char* getlogname_malloc(void) {
uid_t uid;
struct stat st;
if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
uid = st.st_uid;
else
uid = getuid();
return lookup_uid(uid);
}
char *getusername_malloc(void) {
const char *e;
e = getenv("USER");
if (e)
return strdup(e);
return lookup_uid(getuid());
}
int getttyname_malloc(int fd, char **r) {
char path[PATH_MAX], *c;
int k;
assert(r);
k = ttyname_r(fd, path, sizeof(path));
if (k != 0)
return -k;
char_array_0(path);
c = strdup(startswith(path, "/dev/") ? path + 5 : path);
if (!c)
return -ENOMEM;
*r = c;
return 0;
}
int getttyname_harder(int fd, char **r) {
int k;
char *s;
k = getttyname_malloc(fd, &s);
if (k < 0)
return k;
if (streq(s, "tty")) {
free(s);
return get_ctty(0, NULL, r);
}
*r = s;
return 0;
}
int get_ctty_devnr(pid_t pid, dev_t *d) {
int k;
char line[LINE_MAX], *p, *fn;
unsigned long ttynr;
FILE *f;
if (asprintf(&fn, "/proc/%lu/stat", (unsigned long) (pid <= 0 ? getpid() : pid)) < 0)
return -ENOMEM;
f = fopen(fn, "re");
free(fn);
if (!f)
return -errno;
if (!fgets(line, sizeof(line), f)) {
k = feof(f) ? -EIO : -errno;
fclose(f);
return k;
}
fclose(f);
p = strrchr(line, ')');
if (!p)
return -EIO;
p++;
if (sscanf(p, " "
"%*c " /* state */
"%*d " /* ppid */
"%*d " /* pgrp */
"%*d " /* session */
"%lu ", /* ttynr */
&ttynr) != 1)
return -EIO;
*d = (dev_t) ttynr;
return 0;
}
int get_ctty(pid_t pid, dev_t *_devnr, char **r) {
int k;
char fn[PATH_MAX], *s, *b, *p;
dev_t devnr;
assert(r);
k = get_ctty_devnr(pid, &devnr);
if (k < 0)
return k;
snprintf(fn, sizeof(fn), "/dev/char/%u:%u", major(devnr), minor(devnr));
char_array_0(fn);
if ((k = readlink_malloc(fn, &s)) < 0) {
if (k != -ENOENT)
return k;
/* This is an ugly hack */
if (major(devnr) == 136) {
if (asprintf(&b, "pts/%lu", (unsigned long) minor(devnr)) < 0)
return -ENOMEM;
*r = b;
if (_devnr)
*_devnr = devnr;
return 0;
}
/* Probably something like the ptys which have no
* symlink in /dev/char. Let's return something
* vaguely useful. */
if (!(b = strdup(fn + 5)))
return -ENOMEM;
*r = b;
if (_devnr)
*_devnr = devnr;
return 0;
}
if (startswith(s, "/dev/"))
p = s + 5;
else if (startswith(s, "../"))
p = s + 3;
else
p = s;
b = strdup(p);
free(s);
if (!b)
return -ENOMEM;
*r = b;
if (_devnr)
*_devnr = devnr;
return 0;
}
int rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) {
DIR *d;
int ret = 0;
assert(fd >= 0);
/* This returns the first error we run into, but nevertheless
* tries to go on. This closes the passed fd. */
d = fdopendir(fd);
if (!d) {
close_nointr_nofail(fd);
return errno == ENOENT ? 0 : -errno;
}
for (;;) {
struct dirent buf, *de;
bool is_dir, keep_around;
struct stat st;
int r;
r = readdir_r(d, &buf, &de);
if (r != 0 && ret == 0) {
ret = -r;
break;
}
if (!de)
break;
if (streq(de->d_name, ".") || streq(de->d_name, ".."))
continue;
if (de->d_type == DT_UNKNOWN ||
honour_sticky ||
(de->d_type == DT_DIR && root_dev)) {
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);
keep_around =
honour_sticky &&
(st.st_uid == 0 || st.st_uid == getuid()) &&
(st.st_mode & S_ISVTX);
} else {
is_dir = de->d_type == DT_DIR;
keep_around = false;
}
if (is_dir) {
int subdir_fd;
/* if root_dev is set, remove subdirectories only, if device is same as dir */
if (root_dev && st.st_dev != root_dev->st_dev)
continue;
subdir_fd = openat(fd, de->d_name,
O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
if (subdir_fd < 0) {
if (ret == 0 && errno != ENOENT)
ret = -errno;
continue;
}
r = rm_rf_children_dangerous(subdir_fd, only_dirs, honour_sticky, root_dev);
if (r < 0 && ret == 0)
ret = r;
if (!keep_around)
if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
if (ret == 0 && errno != ENOENT)
ret = -errno;
}
} else if (!only_dirs && !keep_around) {
if (unlinkat(fd, de->d_name, 0) < 0) {
if (ret == 0 && errno != ENOENT)
ret = -errno;
}
}
}
closedir(d);
return ret;
}
int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) {
struct statfs s;
assert(fd >= 0);
if (fstatfs(fd, &s) < 0) {
close_nointr_nofail(fd);
return -errno;
}
/* We refuse to clean disk file systems with this call. This
* is extra paranoia just to be sure we never ever remove
* non-state data */
if (s.f_type != TMPFS_MAGIC &&
s.f_type != RAMFS_MAGIC) {
log_error("Attempted to remove disk file system, and we can't allow that.");
close_nointr_nofail(fd);
return -EPERM;
}
return rm_rf_children_dangerous(fd, only_dirs, honour_sticky, root_dev);
}
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;
assert(path);
/* We refuse to clean the root file system with this
* call. This is extra paranoia to never cause a really
* seriously broken system. */
if (path_equal(path, "/")) {
log_error("Attempted to remove entire root file system, and we can't allow that.");
return -EPERM;
}
fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
if (fd < 0) {
if (errno != ENOTDIR)
return -errno;
if (!dangerous) {
if (statfs(path, &s) < 0)
return -errno;
if (s.f_type != TMPFS_MAGIC &&
s.f_type != RAMFS_MAGIC) {
log_error("Attempted to remove disk file system, and we can't allow that.");
return -EPERM;
}
}
if (delete_root && !only_dirs)
if (unlink(path) < 0 && errno != ENOENT)
return -errno;
return 0;
}
if (!dangerous) {
if (fstatfs(fd, &s) < 0) {
close_nointr_nofail(fd);
return -errno;
}
if (s.f_type != TMPFS_MAGIC &&
s.f_type != RAMFS_MAGIC) {
log_error("Attempted to remove disk file system, and we can't allow that.");
close_nointr_nofail(fd);
return -EPERM;
}
}
r = rm_rf_children_dangerous(fd, only_dirs, honour_sticky, NULL);
if (delete_root) {
if (honour_sticky && file_is_priv_sticky(path) > 0)
return r;
if (rmdir(path) < 0 && errno != ENOENT) {
if (r == 0)
r = -errno;
}
}
return r;
}
int rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky) {
return rm_rf_internal(path, only_dirs, delete_root, honour_sticky, false);
}
int rm_rf_dangerous(const char *path, bool only_dirs, bool delete_root, bool honour_sticky) {
return rm_rf_internal(path, only_dirs, delete_root, honour_sticky, true);
}
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 (mode != (mode_t) -1)
if (chmod(path, mode) < 0)
return -errno;
if (uid != (uid_t) -1 || gid != (gid_t) -1)
if (chown(path, uid, gid) < 0)
return -errno;
return 0;
}
int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid) {
assert(fd >= 0);
/* 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 (fchmod(fd, mode) < 0)
return -errno;
if (fchown(fd, 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;
}
}
int status_vprintf(const char *status, bool ellipse, const char *format, va_list ap) {
static const char status_indent[] = " "; /* "[" STATUS "] " */
_cleanup_free_ char *s = NULL;
_cleanup_close_ int fd = -1;
struct iovec iovec[5];
int n = 0;
assert(format);
/* This is independent of logging, as status messages are
* optional and go exclusively to the console. */
if (vasprintf(&s, format, ap) < 0)
return log_oom();
fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
if (fd < 0)
return fd;
if (ellipse) {
char *e;
size_t emax, sl;
int c;
c = fd_columns(fd);
if (c <= 0)
c = 80;
sl = status ? sizeof(status_indent)-1 : 0;
emax = c - sl - 1;
if (emax < 3)
emax = 3;
e = ellipsize(s, emax, 75);
if (e) {
free(s);
s = e;
}
}
zero(iovec);
if (status) {
if (!isempty(status)) {
IOVEC_SET_STRING(iovec[n++], "[");
IOVEC_SET_STRING(iovec[n++], status);
IOVEC_SET_STRING(iovec[n++], "] ");
} else
IOVEC_SET_STRING(iovec[n++], status_indent);
}
IOVEC_SET_STRING(iovec[n++], s);
IOVEC_SET_STRING(iovec[n++], "\n");
if (writev(fd, iovec, n) < 0)
return -errno;
return 0;
}
int status_printf(const char *status, bool ellipse, const char *format, ...) {
va_list ap;
int r;
assert(format);
va_start(ap, format);
r = status_vprintf(status, ellipse, format, ap);
va_end(ap);
return r;
}
int status_welcome(void) {
int r;
_cleanup_free_ char *pretty_name = NULL, *ansi_color = NULL;
r = parse_env_file("/etc/os-release", NEWLINE,
"PRETTY_NAME", &pretty_name,
"ANSI_COLOR", &ansi_color,
NULL);
if (r < 0 && r != -ENOENT)
log_warning("Failed to read /etc/os-release: %s", strerror(-r));
return status_printf(NULL, false,
"\nWelcome to \x1B[%sm%s\x1B[0m!\n",
isempty(ansi_color) ? "1" : ansi_color,
isempty(pretty_name) ? "Linux" : pretty_name);
}
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 == '}') {
const char *t;
if (!(t = strv_env_get_with_length(env, word+2, e-word-2)))
t = "";
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] == '$' && (*i)[1] != '{') {
char *e;
char **w, **m;
unsigned q;
if ((e = strv_env_get(env, *i+1))) {
if (!(m = strv_split_quoted(e))) {
r[k] = NULL;
strv_free(r);
return NULL;
}
} else
m = NULL;
q = strv_length(m);
l = l + q - 1;
if (!(w = realloc(r, sizeof(char*) * (l+1)))) {
r[k] = NULL;
strv_free(r);
strv_free(m);
return NULL;
}
r = w;
if (m) {
memcpy(r + k, m, q * sizeof(char*));
free(m);
}
k += q;
continue;
}
/* If ${FOO} appears as part of a word, replace it by the variable as-is */
if (!(r[k++] = replace_env(*i, env))) {
strv_free(r);
return NULL;
}
}
r[k] = NULL;
return r;
}
int fd_columns(int fd) {
struct winsize ws;
zero(ws);
if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
return -errno;
if (ws.ws_col <= 0)
return -EIO;
return ws.ws_col;
}
static unsigned columns_cached(bool cached) {
static __thread int parsed_columns = 0, env_columns = -1;
const char *e;
if (_likely_(parsed_columns > 0 && cached))
return parsed_columns;
if (_unlikely_(env_columns == -1)) {
e = getenv("COLUMNS");
if (e)
env_columns = atoi(e);
else
env_columns = 0;
}
if (env_columns > 0) {
parsed_columns = env_columns;
return parsed_columns;
}
if (parsed_columns <= 0 || !cached)
parsed_columns = fd_columns(STDOUT_FILENO);
if (parsed_columns <= 0)
parsed_columns = 80;
return parsed_columns;
}
unsigned columns(void) {
return columns_cached(true);
}
unsigned columns_uncached(void) {
return columns_cached(false);
}
int fd_lines(int fd) {
struct winsize ws;
zero(ws);
if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
return -errno;
if (ws.ws_row <= 0)
return -EIO;
return ws.ws_row;
}
unsigned lines(void) {
static __thread int parsed_lines = 0;
const char *e;
if (_likely_(parsed_lines > 0))
return parsed_lines;
e = getenv("LINES");
if (e)
parsed_lines = atoi(e);
if (parsed_lines <= 0)
parsed_lines = fd_lines(STDOUT_FILENO);
if (parsed_lines <= 0)
parsed_lines = 25;
return parsed_lines;
}
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_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
size_t x;
char *r;
assert(s);
assert(percent <= 100);
assert(new_length >= 3);
if (old_length <= 3 || old_length <= new_length)
return strndup(s, old_length);
r = new0(char, new_length+1);
if (!r)
return r;
x = (new_length * percent) / 100;
if (x > new_length - 3)
x = new_length - 3;
memcpy(r, s, x);
r[x] = '.';
r[x+1] = '.';
r[x+2] = '.';
memcpy(r + x + 3,
s + old_length - (new_length - x - 3),
new_length - x - 3);
return r;
}
char *ellipsize(const char *s, size_t length, unsigned percent) {
return ellipsize_mem(s, strlen(s), length, percent);
}
int touch(const char *path) {
int fd;
assert(path);
/* This just opens the file for writing, ensuring it
* exists. It doesn't call utimensat() the way /usr/bin/touch
* does it. */
fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644);
if (fd < 0)
return -errno;
close_nointr_nofail(fd);
return 0;
}
char *unquote(const char *s, const char* quotes) {
size_t l;
assert(s);
/* This is rather stupid, simply removes the heading and
* trailing quotes if there is one. Doesn't care about
* escaping or anything. We should make this smarter one
* day...*/
l = strlen(s);
if (l < 2)
return strdup(s);
if (strchr(quotes, s[0]) && s[l-1] == s[0])
return strndup(s+1, l-2);
return strdup(s);
}
char *normalize_env_assignment(const char *s) {
_cleanup_free_ char *name = NULL, *value = NULL, *p = NULL;
char *eq, *r;
eq = strchr(s, '=');
if (!eq) {
char *t;
r = strdup(s);
if (!r)
return NULL;
t = strstrip(r);
if (t == r)
return r;
memmove(r, t, strlen(t) + 1);
return r;
}
name = strndup(s, eq - s);
if (!name)
return NULL;
p = strdup(eq + 1);
if (!p)
return NULL;
value = unquote(strstrip(p), QUOTES);
if (!value)
return NULL;
if (asprintf(&r, "%s=%s", strstrip(name), value) < 0)
r = NULL;
return r;
}
int wait_for_terminate(pid_t pid, siginfo_t *status) {
siginfo_t dummy;
assert(pid >= 1);
if (!status)
status = &dummy;
for (;;) {
zero(*status);
if (waitid(P_PID, pid, status, WEXITED) < 0) {
if (errno == EINTR)
continue;
return -errno;
}
return 0;
}
}
int wait_for_terminate_and_warn(const char *name, pid_t pid) {
int r;
siginfo_t status;
assert(name);
assert(pid > 1);
r = wait_for_terminate(pid, &status);
if (r < 0) {
log_warning("Failed to wait for %s: %s", name, strerror(-r));
return r;
}
if (status.si_code == CLD_EXITED) {
if (status.si_status != 0) {
log_warning("%s failed with error code %i.", name, status.si_status);
return status.si_status;
}
log_debug("%s succeeded.", name);
return 0;
} else if (status.si_code == CLD_KILLED ||
status.si_code == CLD_DUMPED) {
log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status));
return -EPROTO;
}
log_warning("%s failed due to unknown reason.", name);
return -EPROTO;
}
_noreturn_ void freeze(void) {
/* Make sure nobody waits for us on a socket anymore */
close_all_fds(NULL, 0);
sync();
for (;;)
pause();
}
bool null_or_empty(struct stat *st) {
assert(st);
if (S_ISREG(st->st_mode) && st->st_size <= 0)
return true;
if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
return true;
return false;
}
int null_or_empty_path(const char *fn) {
struct stat st;
assert(fn);
if (stat(fn, &st) < 0)
return -errno;
return null_or_empty(&st);
}
DIR *xopendirat(int fd, const char *name, int flags) {
int nfd;
DIR *d;
nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags);
if (nfd < 0)
return NULL;
d = fdopendir(nfd);
if (!d) {
close_nointr_nofail(nfd);
return NULL;
}
return d;
}
int signal_from_string_try_harder(const char *s) {
int signo;
assert(s);
signo = signal_from_string(s);
if (signo <= 0)
if (startswith(s, "SIG"))
return signal_from_string(s+3);
return signo;
}
void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) {
assert(f);
assert(name);
assert(t);
if (!dual_timestamp_is_set(t))
return;
fprintf(f, "%s=%llu %llu\n",
name,
(unsigned long long) t->realtime,
(unsigned long long) t->monotonic);
}
void dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
unsigned long long a, b;
assert(value);
assert(t);
if (sscanf(value, "%lli %llu", &a, &b) != 2)
log_debug("Failed to parse finish timestamp value %s", value);
else {
t->realtime = a;
t->monotonic = b;
}
}
static char *tag_to_udev_node(const char *tagvalue, const char *by) {
char *dn, *t, *u;
int r;
/* FIXME: to follow udev's logic 100% we need to leave valid
* UTF8 chars unescaped */
u = unquote(tagvalue, "\"\'");
if (u == NULL)
return NULL;
t = xescape(u, "/ ");
free(u);
if (t == NULL)
return NULL;
r = asprintf(&dn, "/dev/disk/by-%s/%s", by, t);
free(t);
if (r < 0)
return NULL;
return dn;
}
char *fstab_node_to_udev_node(const char *p) {
assert(p);
if (startswith(p, "LABEL="))
return tag_to_udev_node(p+6, "label");
if (startswith(p, "UUID="))
return tag_to_udev_node(p+5, "uuid");
if (startswith(p, "PARTUUID="))
return tag_to_udev_node(p+9, "partuuid");
if (startswith(p, "PARTLABEL="))
return tag_to_udev_node(p+10, "partlabel");
return strdup(p);
}
bool tty_is_vc(const char *tty) {
assert(tty);
if (startswith(tty, "/dev/"))
tty += 5;
return vtnr_from_tty(tty) >= 0;
}
bool tty_is_console(const char *tty) {
assert(tty);
if (startswith(tty, "/dev/"))
tty += 5;
return streq(tty, "console");
}
int vtnr_from_tty(const char *tty) {
int i, r;
assert(tty);
if (startswith(tty, "/dev/"))
tty += 5;
if (!startswith(tty, "tty") )
return -EINVAL;
if (tty[3] < '0' || tty[3] > '9')
return -EINVAL;
r = safe_atoi(tty+3, &i);
if (r < 0)
return r;
if (i < 0 || i > 63)
return -EINVAL;
return i;
}
bool tty_is_vc_resolve(const char *tty) {
char *active = NULL;
bool b;
assert(tty);
if (startswith(tty, "/dev/"))
tty += 5;
/* Resolve where /dev/console is pointing to, if /sys is
* actually ours (i.e. not read-only-mounted which is a sign
* for container setups) */
if (streq(tty, "console") && path_is_read_only_fs("/sys") <= 0)
if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
/* If multiple log outputs are configured the
* last one is what /dev/console points to */
tty = strrchr(active, ' ');
if (tty)
tty++;
else
tty = active;
}
b = tty_is_vc(tty);
free(active);
return b;
}
const char *default_term_for_tty(const char *tty) {
assert(tty);
return tty_is_vc_resolve(tty) ? "TERM=linux" : "TERM=vt102";
}
bool dirent_is_file(const struct dirent *de) {
assert(de);
if (ignore_file(de->d_name))
return false;
if (de->d_type != DT_REG &&
de->d_type != DT_LNK &&
de->d_type != DT_UNKNOWN)
return false;
return true;
}
bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
assert(de);
if (de->d_type != DT_REG &&
de->d_type != DT_LNK &&
de->d_type != DT_UNKNOWN)
return false;
if (ignore_file_allow_backup(de->d_name))
return false;
return endswith(de->d_name, suffix);
}
void execute_directory(const char *directory, DIR *d, char *argv[]) {
DIR *_d = NULL;
struct dirent *de;
Hashmap *pids = NULL;
assert(directory);
/* Executes all binaries in a directory in parallel and waits
* until all they all finished. */
if (!d) {
if (!(_d = opendir(directory))) {
if (errno == ENOENT)
return;
log_error("Failed to enumerate directory %s: %m", directory);
return;
}
d = _d;
}
if (!(pids = hashmap_new(trivial_hash_func, trivial_compare_func))) {
log_error("Failed to allocate set.");
goto finish;
}
while ((de = readdir(d))) {
char *path;
pid_t pid;
int k;
if (!dirent_is_file(de))
continue;
if (asprintf(&path, "%s/%s", directory, de->d_name) < 0) {
log_oom();
continue;
}
if ((pid = fork()) < 0) {
log_error("Failed to fork: %m");
free(path);
continue;
}
if (pid == 0) {
char *_argv[2];
/* Child */
if (!argv) {
_argv[0] = path;
_argv[1] = NULL;
argv = _argv;
} else
argv[0] = path;
execv(path, argv);
log_error("Failed to execute %s: %m", path);
_exit(EXIT_FAILURE);
}
log_debug("Spawned %s as %lu", path, (unsigned long) pid);
if ((k = hashmap_put(pids, UINT_TO_PTR(pid), path)) < 0) {
log_error("Failed to add PID to set: %s", strerror(-k));
free(path);
}
}
while (!hashmap_isempty(pids)) {
pid_t pid = PTR_TO_UINT(hashmap_first_key(pids));
siginfo_t si;
char *path;
zero(si);
if (waitid(P_PID, pid, &si, WEXITED) < 0) {
if (errno == EINTR)
continue;
log_error("waitid() failed: %m");
goto finish;
}
if ((path = hashmap_remove(pids, UINT_TO_PTR(si.si_pid)))) {
if (!is_clean_exit(si.si_code, si.si_status, NULL)) {
if (si.si_code == CLD_EXITED)
log_error("%s exited with exit status %i.", path, si.si_status);
else
log_error("%s terminated by signal %s.", path, signal_to_string(si.si_status));
} else
log_debug("%s exited successfully.", path);
free(path);
}
}
finish:
if (_d)
closedir(_d);
if (pids)
hashmap_free_free(pids);
}
int kill_and_sigcont(pid_t pid, int sig) {
int r;
r = kill(pid, sig) < 0 ? -errno : 0;
if (r >= 0)
kill(pid, SIGCONT);
return r;
}
bool nulstr_contains(const char*nulstr, const char *needle) {
const char *i;
if (!nulstr)
return false;
NULSTR_FOREACH(i, nulstr)
if (streq(i, needle))
return true;
return false;
}
bool plymouth_running(void) {
return access("/run/plymouth/pid", F_OK) >= 0;
}
char* strshorten(char *s, size_t l) {
assert(s);
if (l < strlen(s))
s[l] = 0;
return s;
}
static bool hostname_valid_char(char c) {
return
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '-' ||
c == '_' ||
c == '.';
}
bool hostname_is_valid(const char *s) {
const char *p;
if (isempty(s))
return false;
for (p = s; *p; p++)
if (!hostname_valid_char(*p))
return false;
if (p-s > HOST_NAME_MAX)
return false;
return true;
}
char* hostname_cleanup(char *s) {
char *p, *d;
for (p = s, d = s; *p; p++)
if ((*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z') ||
(*p >= '0' && *p <= '9') ||
*p == '-' ||
*p == '_' ||
*p == '.')
*(d++) = *p;
*d = 0;
strshorten(s, HOST_NAME_MAX);
return s;
}
int pipe_eof(int fd) {
struct pollfd pollfd;
int r;
zero(pollfd);
pollfd.fd = fd;
pollfd.events = POLLIN|POLLHUP;
r = poll(&pollfd, 1, 0);
if (r < 0)
return -errno;
if (r == 0)
return 0;
return pollfd.revents & POLLHUP;
}
int fd_wait_for_event(int fd, int event, usec_t t) {
struct pollfd pollfd;
int r;
zero(pollfd);
pollfd.fd = fd;
pollfd.events = event;
r = poll(&pollfd, 1, t == (usec_t) -1 ? -1 : (int) (t / USEC_PER_MSEC));
if (r < 0)
return -errno;
if (r == 0)
return 0;
return pollfd.revents;
}
int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
FILE *f;
char *t;
const char *fn;
size_t k;
int fd;
assert(path);
assert(_f);
assert(_temp_path);
t = new(char, strlen(path) + 1 + 6 + 1);
if (!t)
return -ENOMEM;
fn = path_get_file_name(path);
k = fn-path;
memcpy(t, path, k);
t[k] = '.';
stpcpy(stpcpy(t+k+1, fn), "XXXXXX");
fd = mkostemp(t, O_WRONLY|O_CLOEXEC);
if (fd < 0) {
free(t);
return -errno;
}
f = fdopen(fd, "we");
if (!f) {
unlink(t);
free(t);
return -errno;
}
*_f = f;
*_temp_path = t;
return 0;
}
int terminal_vhangup_fd(int fd) {
assert(fd >= 0);
if (ioctl(fd, TIOCVHANGUP) < 0)
return -errno;
return 0;
}
int terminal_vhangup(const char *name) {
int fd, r;
fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
if (fd < 0)
return fd;
r = terminal_vhangup_fd(fd);
close_nointr_nofail(fd);
return r;
}
int vt_disallocate(const char *name) {
int fd, r;
unsigned u;
/* Deallocate the VT if possible. If not possible
* (i.e. because it is the active one), at least clear it
* entirely (including the scrollback buffer) */
if (!startswith(name, "/dev/"))
return -EINVAL;
if (!tty_is_vc(name)) {
/* So this is not a VT. I guess we cannot deallocate
* it then. But let's at least clear the screen */
fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
if (fd < 0)
return fd;
loop_write(fd,
"\033[r" /* clear scrolling region */
"\033[H" /* move home */
"\033[2J", /* clear screen */
10, false);
close_nointr_nofail(fd);
return 0;
}
if (!startswith(name, "/dev/tty"))
return -EINVAL;
r = safe_atou(name+8, &u);
if (r < 0)
return r;
if (u <= 0)
return -EINVAL;
/* Try to deallocate */
fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
if (fd < 0)
return fd;
r = ioctl(fd, VT_DISALLOCATE, u);
close_nointr_nofail(fd);
if (r >= 0)
return 0;
if (errno != EBUSY)
return -errno;
/* Couldn't deallocate, so let's clear it fully with
* scrollback */
fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
if (fd < 0)
return fd;
loop_write(fd,
"\033[r" /* clear scrolling region */
"\033[H" /* move home */
"\033[3J", /* clear screen including scrollback, requires Linux 2.6.40 */
10, false);
close_nointr_nofail(fd);
return 0;
}
int copy_file(const char *from, const char *to) {
int r, fdf, fdt;
assert(from);
assert(to);
fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fdf < 0)
return -errno;
fdt = open(to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY, 0644);
if (fdt < 0) {
close_nointr_nofail(fdf);
return -errno;
}
for (;;) {
char buf[PIPE_BUF];
ssize_t n, k;
n = read(fdf, buf, sizeof(buf));
if (n < 0) {
r = -errno;
close_nointr_nofail(fdf);
close_nointr(fdt);
unlink(to);
return r;
}
if (n == 0)
break;
errno = 0;
k = loop_write(fdt, buf, n, false);
if (n != k) {
r = k < 0 ? k : (errno ? -errno : -EIO);
close_nointr_nofail(fdf);
close_nointr(fdt);
unlink(to);
return r;
}
}
close_nointr_nofail(fdf);
r = close_nointr(fdt);
if (r < 0) {
unlink(to);
return r;
}
return 0;
}
int symlink_atomic(const char *from, const char *to) {
char *x;
_cleanup_free_ char *t;
const char *fn;
size_t k;
unsigned long long ull;
unsigned i;
int r;
assert(from);
assert(to);
t = new(char, strlen(to) + 1 + 16 + 1);
if (!t)
return -ENOMEM;
fn = path_get_file_name(to);
k = fn-to;
memcpy(t, to, k);
t[k] = '.';
x = stpcpy(t+k+1, fn);
ull = random_ull();
for (i = 0; i < 16; i++) {
*(x++) = hexchar(ull & 0xF);
ull >>= 4;
}
*x = 0;
if (symlink(from, t) < 0)
return -errno;
if (rename(t, to) < 0) {
r = -errno;
unlink(t);
return r;
}
return 0;
}
bool display_is_local(const char *display) {
assert(display);
return
display[0] == ':' &&
display[1] >= '0' &&
display[1] <= '9';
}
int socket_from_display(const char *display, char **path) {
size_t k;
char *f, *c;
assert(display);
assert(path);
if (!display_is_local(display))
return -EINVAL;
k = strspn(display+1, "0123456789");
f = new(char, sizeof("/tmp/.X11-unix/X") + k);
if (!f)
return -ENOMEM;
c = stpcpy(f, "/tmp/.X11-unix/X");
memcpy(c, display+1, k);
c[k] = 0;
*path = f;
return 0;
}
int get_user_creds(
const char **username,
uid_t *uid, gid_t *gid,
const char **home,
const char **shell) {
struct passwd *p;
uid_t u;
assert(username);
assert(*username);
/* We enforce some special rules for uid=0: in order to avoid
* NSS lookups for root we hardcode its data. */
if (streq(*username, "root") || streq(*username, "0")) {
*username = "root";
if (uid)
*uid = 0;
if (gid)
*gid = 0;
if (home)
*home = "/root";
if (shell)
*shell = "/bin/sh";
return 0;
}
if (parse_uid(*username, &u) >= 0) {
errno = 0;
p = getpwuid(u);
/* If there are multiple users with the same id, make
* sure to leave $USER to the configured value instead
* of the first occurrence in the database. However if
* the uid was configured by a numeric uid, then let's
* pick the real username from /etc/passwd. */
if (p)
*username = p->pw_name;
} else {
errno = 0;
p = getpwnam(*username);
}
if (!p)
return errno != 0 ? -errno : -ESRCH;
if (uid)
*uid = p->pw_uid;
if (gid)
*gid = p->pw_gid;
if (home)
*home = p->pw_dir;
if (shell)
*shell = p->pw_shell;
return 0;
}
int get_group_creds(const char **groupname, gid_t *gid) {
struct group *g;
gid_t id;
assert(groupname);
/* We enforce some special rules for gid=0: in order to avoid
* NSS lookups for root we hardcode its data. */
if (streq(*groupname, "root") || streq(*groupname, "0")) {
*groupname = "root";
if (gid)
*gid = 0;
return 0;
}
if (parse_gid(*groupname, &id) >= 0) {
errno = 0;
g = getgrgid(id);
if (g)
*groupname = g->gr_name;
} else {
errno = 0;
g = getgrnam(*groupname);
}
if (!g)
return errno != 0 ? -errno : -ESRCH;
if (gid)
*gid = g->gr_gid;
return 0;
}
int in_group(const char *name) {
gid_t gid, *gids;
int ngroups_max, r, i;
r = get_group_creds(&name, &gid);
if (r < 0)
return r;
if (getgid() == gid)
return 1;
if (getegid() == gid)
return 1;
ngroups_max = sysconf(_SC_NGROUPS_MAX);
assert(ngroups_max > 0);
gids = alloca(sizeof(gid_t) * ngroups_max);
r = getgroups(ngroups_max, gids);
if (r < 0)
return -errno;
for (i = 0; i < r; i++)
if (gids[i] == gid)
return 1;
return 0;
}
int glob_exists(const char *path) {
glob_t g;
int r, k;
assert(path);
zero(g);
errno = 0;
k = glob(path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
if (k == GLOB_NOMATCH)
r = 0;
else if (k == GLOB_NOSPACE)
r = -ENOMEM;
else if (k == 0)
r = !strv_isempty(g.gl_pathv);
else
r = errno ? -errno : -EIO;
globfree(&g);
return r;
}
int dirent_ensure_type(DIR *d, struct dirent *de) {
struct stat st;
assert(d);
assert(de);
if (de->d_type != DT_UNKNOWN)
return 0;
if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
return -errno;
de->d_type =
S_ISREG(st.st_mode) ? DT_REG :
S_ISDIR(st.st_mode) ? DT_DIR :
S_ISLNK(st.st_mode) ? DT_LNK :
S_ISFIFO(st.st_mode) ? DT_FIFO :
S_ISSOCK(st.st_mode) ? DT_SOCK :
S_ISCHR(st.st_mode) ? DT_CHR :
S_ISBLK(st.st_mode) ? DT_BLK :
DT_UNKNOWN;
return 0;
}
int in_search_path(const char *path, char **search) {
char **i, *parent;
int r;
r = path_get_parent(path, &parent);
if (r < 0)
return r;
r = 0;
STRV_FOREACH(i, search) {
if (path_equal(parent, *i)) {
r = 1;
break;
}
}
free(parent);
return r;
}
int get_files_in_directory(const char *path, char ***list) {
DIR *d;
int r = 0;
unsigned n = 0;
char **l = NULL;
assert(path);
/* Returns all files in a directory in *list, and the number
* of files as return value. If list is NULL returns only the
* number */
d = opendir(path);
if (!d)
return -errno;
for (;;) {
struct dirent buffer, *de;
int k;
k = readdir_r(d, &buffer, &de);
if (k != 0) {
r = -k;
goto finish;
}
if (!de)
break;
dirent_ensure_type(d, de);
if (!dirent_is_file(de))
continue;
if (list) {
if ((unsigned) r >= n) {
char **t;
n = MAX(16, 2*r);
t = realloc(l, sizeof(char*) * n);
if (!t) {
r = -ENOMEM;
goto finish;
}
l = t;
}
assert((unsigned) r < n);
l[r] = strdup(de->d_name);
if (!l[r]) {
r = -ENOMEM;
goto finish;
}
l[++r] = NULL;
} else
r++;
}
finish:
if (d)
closedir(d);
if (r >= 0) {
if (list)
*list = l;
} else
strv_free(l);
return r;
}
char *strjoin(const char *x, ...) {
va_list ap;
size_t l;
char *r, *p;
va_start(ap, x);
if (x) {
l = strlen(x);
for (;;) {
const char *t;
t = va_arg(ap, const char *);
if (!t)
break;
l += strlen(t);
}
} else
l = 0;
va_end(ap);
r = new(char, l+1);
if (!r)
return NULL;
if (x) {
p = stpcpy(r, x);
va_start(ap, x);
for (;;) {
const char *t;
t = va_arg(ap, const char *);
if (!t)
break;
p = stpcpy(p, t);
}
va_end(ap);
} else
r[0] = 0;
return r;
}
bool is_main_thread(void) {
static __thread int cached = 0;
if (_unlikely_(cached == 0))
cached = getpid() == gettid() ? 1 : -1;
return cached > 0;
}
int block_get_whole_disk(dev_t d, dev_t *ret) {
char *p, *s;
int r;
unsigned n, m;
assert(ret);
/* If it has a queue this is good enough for us */
if (asprintf(&p, "/sys/dev/block/%u:%u/queue", major(d), minor(d)) < 0)
return -ENOMEM;
r = access(p, F_OK);
free(p);
if (r >= 0) {
*ret = d;
return 0;
}
/* If it is a partition find the originating device */
if (asprintf(&p, "/sys/dev/block/%u:%u/partition", major(d), minor(d)) < 0)
return -ENOMEM;
r = access(p, F_OK);
free(p);
if (r < 0)
return -ENOENT;
/* Get parent dev_t */
if (asprintf(&p, "/sys/dev/block/%u:%u/../dev", major(d), minor(d)) < 0)
return -ENOMEM;
r = read_one_line_file(p, &s);
free(p);
if (r < 0)
return r;
r = sscanf(s, "%u:%u", &m, &n);
free(s);
if (r != 2)
return -EINVAL;
/* Only return this if it is really good enough for us. */
if (asprintf(&p, "/sys/dev/block/%u:%u/queue", m, n) < 0)
return -ENOMEM;
r = access(p, F_OK);
free(p);
if (r >= 0) {
*ret = makedev(m, n);
return 0;
}
return -ENOENT;
}
int file_is_priv_sticky(const char *p) {
struct stat st;
assert(p);
if (lstat(p, &st) < 0)
return -errno;
return
(st.st_uid == 0 || st.st_uid == getuid()) &&
(st.st_mode & S_ISVTX);
}
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_unshifted_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_unshifted, 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",
#ifdef SIGSTKFLT
[SIGSTKFLT] = "STKFLT", /* Linux on SPARC doesn't know SIGSTKFLT */
#endif
[SIGCHLD] = "CHLD",
[SIGCONT] = "CONT",
[SIGSTOP] = "STOP",
[SIGTSTP] = "TSTP",
[SIGTTIN] = "TTIN",
[SIGTTOU] = "TTOU",
[SIGURG] = "URG",
[SIGXCPU] = "XCPU",
[SIGXFSZ] = "XFSZ",
[SIGVTALRM] = "VTALRM",
[SIGPROF] = "PROF",
[SIGWINCH] = "WINCH",
[SIGIO] = "IO",
[SIGPWR] = "PWR",
[SIGSYS] = "SYS"
};
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(__signal, int);
const char *signal_to_string(int signo) {
static __thread char buf[12];
const char *name;
name = __signal_to_string(signo);
if (name)
return name;
if (signo >= SIGRTMIN && signo <= SIGRTMAX)
snprintf(buf, sizeof(buf) - 1, "RTMIN+%d", signo - SIGRTMIN);
else
snprintf(buf, sizeof(buf) - 1, "%d", signo);
char_array_0(buf);
return buf;
}
int signal_from_string(const char *s) {
int signo;
int offset = 0;
unsigned u;
signo =__signal_from_string(s);
if (signo > 0)
return signo;
if (startswith(s, "RTMIN+")) {
s += 6;
offset = SIGRTMIN;
}
if (safe_atou(s, &u) >= 0) {
signo = (int) u + offset;
if (signo > 0 && signo < _NSIG)
return signo;
}
return -1;
}
bool kexec_loaded(void) {
bool loaded = false;
char *s;
if (read_one_line_file("/sys/kernel/kexec_loaded", &s) >= 0) {
if (s[0] == '1')
loaded = true;
free(s);
}
return loaded;
}
int strdup_or_null(const char *a, char **b) {
char *c;
assert(b);
if (!a) {
*b = NULL;
return 0;
}
c = strdup(a);
if (!c)
return -ENOMEM;
*b = c;
return 0;
}
int prot_from_flags(int flags) {
switch (flags & O_ACCMODE) {
case O_RDONLY:
return PROT_READ;
case O_WRONLY:
return PROT_WRITE;
case O_RDWR:
return PROT_READ|PROT_WRITE;
default:
return -EINVAL;
}
}
char *format_bytes(char *buf, size_t l, off_t t) {
unsigned i;
static const struct {
const char *suffix;
off_t factor;
} table[] = {
{ "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
{ "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
{ "T", 1024ULL*1024ULL*1024ULL*1024ULL },
{ "G", 1024ULL*1024ULL*1024ULL },
{ "M", 1024ULL*1024ULL },
{ "K", 1024ULL },
};
for (i = 0; i < ELEMENTSOF(table); i++) {
if (t >= table[i].factor) {
snprintf(buf, l,
"%llu.%llu%s",
(unsigned long long) (t / table[i].factor),
(unsigned long long) (((t*10ULL) / table[i].factor) % 10ULL),
table[i].suffix);
goto finish;
}
}
snprintf(buf, l, "%lluB", (unsigned long long) t);
finish:
buf[l-1] = 0;
return buf;
}
void* memdup(const void *p, size_t l) {
void *r;
assert(p);
r = malloc(l);
if (!r)
return NULL;
memcpy(r, p, l);
return r;
}
int fd_inc_sndbuf(int fd, size_t n) {
int r, value;
socklen_t l = sizeof(value);
r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
if (r >= 0 &&
l == sizeof(value) &&
(size_t) value >= n*2)
return 0;
value = (int) n;
r = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value));
if (r < 0)
return -errno;
return 1;
}
int fd_inc_rcvbuf(int fd, size_t n) {
int r, value;
socklen_t l = sizeof(value);
r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
if (r >= 0 &&
l == sizeof(value) &&
(size_t) value >= n*2)
return 0;
value = (int) n;
r = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value));
if (r < 0)
return -errno;
return 1;
}
int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...) {
pid_t parent_pid, agent_pid;
int fd;
bool stdout_is_tty, stderr_is_tty;
unsigned n, i;
va_list ap;
char **l;
assert(pid);
assert(path);
parent_pid = getpid();
/* Spawns a temporary TTY agent, making sure it goes away when
* we go away */
agent_pid = fork();
if (agent_pid < 0)
return -errno;
if (agent_pid != 0) {
*pid = agent_pid;
return 0;
}
/* In the child:
*
* Make sure the agent goes away when the parent dies */
if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
_exit(EXIT_FAILURE);
/* Check whether our parent died before we were able
* to set the death signal */
if (getppid() != parent_pid)
_exit(EXIT_SUCCESS);
/* Don't leak fds to the agent */
close_all_fds(except, n_except);
stdout_is_tty = isatty(STDOUT_FILENO);
stderr_is_tty = isatty(STDERR_FILENO);
if (!stdout_is_tty || !stderr_is_tty) {
/* Detach from stdout/stderr. and reopen
* /dev/tty for them. This is important to
* ensure that when systemctl is started via
* popen() or a similar call that expects to
* read EOF we actually do generate EOF and
* not delay this indefinitely by because we
* keep an unused copy of stdin around. */
fd = open("/dev/tty", O_WRONLY);
if (fd < 0) {
log_error("Failed to open /dev/tty: %m");
_exit(EXIT_FAILURE);
}
if (!stdout_is_tty)
dup2(fd, STDOUT_FILENO);
if (!stderr_is_tty)
dup2(fd, STDERR_FILENO);
if (fd > 2)
close(fd);
}
/* Count arguments */
va_start(ap, path);
for (n = 0; va_arg(ap, char*); n++)
;
va_end(ap);
/* Allocate strv */
l = alloca(sizeof(char *) * (n + 1));
/* Fill in arguments */
va_start(ap, path);
for (i = 0; i <= n; i++)
l[i] = va_arg(ap, char*);
va_end(ap);
execv(path, l);
_exit(EXIT_FAILURE);
}
int setrlimit_closest(int resource, const struct rlimit *rlim) {
struct rlimit highest, fixed;
assert(rlim);
if (setrlimit(resource, rlim) >= 0)
return 0;
if (errno != EPERM)
return -errno;
/* So we failed to set the desired setrlimit, then let's try
* to get as close as we can */
assert_se(getrlimit(resource, &highest) == 0);
fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max);
fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max);
if (setrlimit(resource, &fixed) < 0)
return -errno;
return 0;
}
int getenv_for_pid(pid_t pid, const char *field, char **_value) {
char path[sizeof("/proc/")-1+10+sizeof("/environ")], *value = NULL;
int r;
FILE *f;
bool done = false;
size_t l;
assert(field);
assert(_value);
if (pid == 0)
pid = getpid();
snprintf(path, sizeof(path), "/proc/%lu/environ", (unsigned long) pid);
char_array_0(path);
f = fopen(path, "re");
if (!f)
return -errno;
l = strlen(field);
r = 0;
do {
char line[LINE_MAX];
unsigned i;
for (i = 0; i < sizeof(line)-1; i++) {
int c;
c = getc(f);
if (_unlikely_(c == EOF)) {
done = true;
break;
} else if (c == 0)
break;
line[i] = c;
}
line[i] = 0;
if (memcmp(line, field, l) == 0 && line[l] == '=') {
value = strdup(line + l + 1);
if (!value) {
r = -ENOMEM;
break;
}
r = 1;
break;
}
} while (!done);
fclose(f);
if (r >= 0)
*_value = value;
return r;
}
int can_sleep(const char *type) {
char *w, *state;
size_t l, k;
int r;
_cleanup_free_ char *p = NULL;
assert(type);
r = read_one_line_file("/sys/power/state", &p);
if (r < 0)
return r == -ENOENT ? 0 : r;
k = strlen(type);
FOREACH_WORD_SEPARATOR(w, l, p, WHITESPACE, state)
if (l == k && memcmp(w, type, l) == 0)
return true;
return false;
}
bool is_valid_documentation_url(const char *url) {
assert(url);
if (startswith(url, "http://") && url[7])
return true;
if (startswith(url, "https://") && url[8])
return true;
if (startswith(url, "file:") && url[5])
return true;
if (startswith(url, "info:") && url[5])
return true;
if (startswith(url, "man:") && url[4])
return true;
return false;
}
bool in_initrd(void) {
static __thread int saved = -1;
struct statfs s;
if (saved >= 0)
return saved;
/* We make two checks here:
*
* 1. the flag file /etc/initrd-release must exist
* 2. the root file system must be a memory file system
*
* The second check is extra paranoia, since misdetecting an
* initrd can have bad bad consequences due the initrd
* emptying when transititioning to the main systemd.
*/
saved = access("/etc/initrd-release", F_OK) >= 0 &&
statfs("/", &s) >= 0 &&
(s.f_type == TMPFS_MAGIC || s.f_type == RAMFS_MAGIC);
return saved;
}
void warn_melody(void) {
_cleanup_close_ int fd = -1;
fd = open("/dev/console", O_WRONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
return;
/* Yeah, this is synchronous. Kinda sucks. Bute well... */
ioctl(fd, KIOCSOUND, (int)(1193180/440));
usleep(125*USEC_PER_MSEC);
ioctl(fd, KIOCSOUND, (int)(1193180/220));
usleep(125*USEC_PER_MSEC);
ioctl(fd, KIOCSOUND, (int)(1193180/220));
usleep(125*USEC_PER_MSEC);
ioctl(fd, KIOCSOUND, 0);
}
int make_console_stdio(void) {
int fd, r;
/* Make /dev/console the controlling terminal and stdin/stdout/stderr */
fd = acquire_terminal("/dev/console", false, true, true, (usec_t) -1);
if (fd < 0) {
log_error("Failed to acquire terminal: %s", strerror(-fd));
return fd;
}
r = make_stdio(fd);
if (r < 0) {
log_error("Failed to duplicate terminal fd: %s", strerror(-r));
return r;
}
return 0;
}
int get_home_dir(char **_h) {
char *h;
const char *e;
uid_t u;
struct passwd *p;
assert(_h);
/* Take the user specified one */
e = getenv("HOME");
if (e) {
h = strdup(e);
if (!h)
return -ENOMEM;
*_h = h;
return 0;
}
/* Hardcode home directory for root to avoid NSS */
u = getuid();
if (u == 0) {
h = strdup("/root");
if (!h)
return -ENOMEM;
*_h = h;
return 0;
}
/* Check the database... */
errno = 0;
p = getpwuid(u);
if (!p)
return errno ? -errno : -ESRCH;
if (!path_is_absolute(p->pw_dir))
return -EINVAL;
h = strdup(p->pw_dir);
if (!h)
return -ENOMEM;
*_h = h;
return 0;
}
int get_shell(char **_sh) {
char *sh;
const char *e;
uid_t u;
struct passwd *p;
assert(_sh);
/* Take the user specified one */
e = getenv("SHELL");
if (e) {
sh = strdup(e);
if (!sh)
return -ENOMEM;
*_sh = sh;
return 0;
}
/* Hardcode home directory for root to avoid NSS */
u = getuid();
if (u == 0) {
sh = strdup("/bin/sh");
if (!sh)
return -ENOMEM;
*_sh = sh;
return 0;
}
/* Check the database... */
errno = 0;
p = getpwuid(u);
if (!p)
return errno ? -errno : -ESRCH;
if (!path_is_absolute(p->pw_shell))
return -EINVAL;
sh = strdup(p->pw_shell);
if (!sh)
return -ENOMEM;
*_sh = sh;
return 0;
}
void freep(void *p) {
free(*(void**) p);
}
void fclosep(FILE **f) {
if (*f)
fclose(*f);
}
void closep(int *fd) {
if (*fd >= 0)
close_nointr_nofail(*fd);
}
void closedirp(DIR **d) {
if (*d)
closedir(*d);
}
void umaskp(mode_t *u) {
umask(*u);
}