util.c revision 7b3e062cb6872e28ef5a2e069810070e28bbe0cd
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <langinfo.h>
#include <libintl.h>
#include <limits.h>
#include <locale.h>
#include <poll.h>
#include <pwd.h>
#include <sched.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/personality.h>
#include <syslog.h>
#include <unistd.h>
/* When we include libgen.h because we need dirname() we immediately
* undefine basename() since libgen.h defines it as a macro to the
* POSIX version which is really broken. We prefer GNU basename(). */
#include <libgen.h>
#ifdef HAVE_SYS_AUXV_H
#endif
#include "alloc-util.h"
#include "build.h"
#include "def.h"
#include "device-nodes.h"
#include "env-util.h"
#include "escape.h"
#include "exit-status.h"
#include "fd-util.h"
#include "fileio.h"
#include "formats-util.h"
#include "gunicode.h"
#include "hashmap.h"
#include "hostname-util.h"
#include "ioprio.h"
#include "log.h"
#include "macro.h"
#include "missing.h"
#include "mkdir.h"
#include "hexdecoct.h"
#include "parse-util.h"
#include "path-util.h"
#include "process-util.h"
#include "random-util.h"
#include "signal-util.h"
#include "sparse-endian.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "user-util.h"
#include "utf8.h"
#include "util.h"
#include "virt.h"
#include "dirent-util.h"
#include "stat-util.h"
/* Put this test here for a lack of better place */
int saved_argc = 0;
char **saved_argv = NULL;
long r;
return pgsz;
r = sysconf(_SC_PAGESIZE);
assert(r > 0);
return pgsz;
}
char **directory;
/* We fork this all off from a child process so that we can
* somewhat cleanly make use of SIGALRM to set a time limit */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
if (!pids)
return log_oom();
if (!seen)
return log_oom();
if (!d) {
continue;
}
FOREACH_DIRENT(de, d, break) {
int r;
if (!dirent_is_file(de))
continue;
continue;
}
if (r < 0)
return log_oom();
if (!path)
return log_oom();
if (null_or_empty_path(path)) {
continue;
}
if (pid < 0) {
continue;
} else if (pid == 0) {
char *_argv[2];
if (!argv) {
} else
}
if (r < 0)
return log_oom();
}
}
/* Abort execution of this process after the timout. We simply
* rely on SIGALRM as default action terminating the process,
* and turn on alarm(). */
if (timeout != USEC_INFINITY)
while (!hashmap_isempty(pids)) {
}
return 0;
}
int r;
char *name;
char **dirs = (char**) directories;
/* Executes all binaries in the directories in parallel and waits
* for them to finish. Optionally a timeout is applied. If a file
* with the same name exists in more than one directory, the
* earliest one wins. */
executor_pid = fork();
if (executor_pid < 0) {
return;
} else if (executor_pid == 0) {
}
}
bool plymouth_running(void) {
}
bool display_is_local(const char *display) {
return
display[0] == ':' &&
}
size_t k;
char *f, *c;
if (!display_is_local(display))
return -EINVAL;
if (!f)
return -ENOMEM;
c = stpcpy(f, "/tmp/.X11-unix/X");
c[k] = 0;
*path = f;
return 0;
}
char *p, *s;
int r;
unsigned n, m;
/* If it has a queue this is good enough for us */
return -ENOMEM;
free(p);
if (r >= 0) {
*ret = d;
return 0;
}
/* If it is a partition find the originating device */
return -ENOMEM;
free(p);
if (r < 0)
return -ENOENT;
/* Get parent dev_t */
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. */
return -ENOMEM;
free(p);
if (r >= 0) {
return 0;
}
return -ENOENT;
}
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 prot_from_flags(int flags) {
case O_RDONLY:
return PROT_READ;
case O_WRONLY:
return PROT_WRITE;
case O_RDWR:
return PROT_READ|PROT_WRITE;
default:
return -EINVAL;
}
}
bool stdout_is_tty, stderr_is_tty;
unsigned n, i;
char **l;
/* Spawns a temporary TTY agent, making sure it goes away when
* we go away */
parent_pid = getpid();
/* First we temporarily block all signals, so that the new
* child has them blocked initially. This way, we can be sure
* that SIGTERMs are not lost we might send to the agent. */
if (agent_pid < 0) {
return -errno;
}
if (agent_pid != 0) {
return 0;
}
/* In the child:
*
* Make sure the agent goes away when the parent dies */
/* Make sure we actually can kill the agent, if we need to, in
* case somebody invoked us from a shell script that trapped
* SIGTERM or so... */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
/* Check whether our parent died before we were able
* to set the death signal and unblock the signals */
if (getppid() != parent_pid)
/* Don't leak fds to the agent */
if (!stdout_is_tty || !stderr_is_tty) {
int fd;
* 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. */
if (fd < 0) {
}
if (!stdout_is_tty)
if (!stderr_is_tty)
if (fd > 2)
}
/* Count arguments */
;
/* Allocate strv */
l = alloca(sizeof(char *) * (n + 1));
/* Fill in arguments */
for (i = 0; i <= n; i++)
}
bool in_initrd(void) {
static 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.
*/
statfs("/", &s) >= 0 &&
is_temporary_fs(&s);
return saved;
}
/* hey glibc, APIs with callbacks without a user pointer are so useless */
const void *p;
int comparison;
l = 0;
u = nmemb;
while (l < u) {
idx = (l + u) / 2;
if (comparison < 0)
u = idx;
else if (comparison > 0)
l = idx + 1;
else
return (void *)p;
}
return NULL;
}
int on_ac_power(void) {
bool found_offline = false, found_online = false;
d = opendir("/sys/class/power_supply");
if (!d)
for (;;) {
char contents[6];
ssize_t n;
errno = 0;
return -errno;
if (!de)
break;
continue;
if (device < 0) {
continue;
return -errno;
}
if (fd < 0) {
continue;
return -errno;
}
if (n < 0)
return -errno;
continue;
safe_close(fd);
if (fd < 0) {
continue;
return -errno;
}
if (n < 0)
return -errno;
return -EIO;
if (contents[0] == '1') {
found_online = true;
break;
} else if (contents[0] == '0')
found_offline = true;
else
return -EIO;
}
return found_online || !found_offline;
}
bool id128_is_valid(const char *s) {
size_t i, l;
l = strlen(s);
if (l == 32) {
/* Simple formatted 128bit hex string */
for (i = 0; i < l; i++) {
char c = s[i];
if (!(c >= '0' && c <= '9') &&
!(c >= 'a' && c <= 'z') &&
!(c >= 'A' && c <= 'Z'))
return false;
}
} else if (l == 36) {
/* Formatted UUID */
for (i = 0; i < l; i++) {
char c = s[i];
if ((i == 8 || i == 13 || i == 18 || i == 23)) {
if (c != '-')
return false;
} else {
if (!(c >= '0' && c <= '9') &&
!(c >= 'a' && c <= 'z') &&
!(c >= 'A' && c <= 'Z'))
return false;
}
}
} else
return false;
return true;
}
const char *p;
int r;
if (!machine_name_is_valid(machine))
return -EINVAL;
if (r == -ENOENT)
return -EHOSTDOWN;
if (r < 0)
return r;
if (!s)
return -EIO;
return -EIO;
if (r < 0)
return r;
if (leader <= 1)
return -EIO;
return 0;
}
int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd) {
int rfd = -1;
if (mntns_fd) {
const char *mntns;
if (mntnsfd < 0)
return -errno;
}
if (pidns_fd) {
const char *pidns;
if (pidnsfd < 0)
return -errno;
}
if (netns_fd) {
const char *netns;
if (netnsfd < 0)
return -errno;
}
if (userns_fd) {
const char *userns;
return -errno;
}
if (root_fd) {
const char *root;
if (rfd < 0)
return -errno;
}
if (pidns_fd)
if (mntns_fd)
if (netns_fd)
if (userns_fd)
if (root_fd)
return 0;
}
if (userns_fd >= 0) {
/* Can't setns to your own userns, since then you could
* escalate from non-root to root in your own namespace, so
* check if namespaces equal before attempting to enter. */
int r;
return -ENOMEM;
if (r < 0)
return r;
if (r)
userns_fd = -1;
}
if (pidns_fd >= 0)
return -errno;
if (mntns_fd >= 0)
return -errno;
if (netns_fd >= 0)
return -errno;
if (userns_fd >= 0)
return -errno;
if (root_fd >= 0) {
return -errno;
if (chroot(".") < 0)
return -errno;
}
return reset_uid_gid();
}
uint64_t physical_memory(void) {
long mem;
/* We return this as uint64_t in case we are running as 32bit
* process on a 64bit kernel with huge amounts of memory */
}
int update_reboot_param_file(const char *param) {
int r = 0;
if (param) {
if (r < 0)
} else
(void) unlink(REBOOT_PARAM_FILE);
return 0;
}
int version(void) {
return 0;
}
bool fdname_is_valid(const char *s) {
const char *p;
/* Validates a name for $LISTEN_FDNAMES. We basically allow
* everything ASCII that's not a control character. Also, as
* special exception the ":" character is not allowed, as we
* use that as field separator in $LISTEN_FDNAMES.
*
* Note that the empty string is explicitly allowed
* here. However, we limit the length of the names to 255
* characters. */
if (!s)
return false;
for (p = s; *p; p++) {
if (*p < ' ')
return false;
if (*p >= 127)
return false;
if (*p == ':')
return false;
}
return p - s < 256;
}