#include "fd-util.h"
#include "log.h"
#include "macro.h"
+#include "process-util.h"
#include "signal-util.h"
#include "socket-util.h"
#include "string-util.h"
if (asprintf((char**)(envp + n_env++), "LISTEN_FDS=%i", n_fds) < 0)
return log_oom();
- if (asprintf((char**)(envp + n_env++), "LISTEN_PID=" PID_FMT, getpid()) < 0)
+ if (asprintf((char**)(envp + n_env++), "LISTEN_PID=" PID_FMT, getpid_cached()) < 0)
return log_oom();
if (arg_fdnames) {
if (!joined)
return log_oom();
- parent_pid = getpid();
+ parent_pid = getpid_cached();
child_pid = fork();
if (child_pid < 0)
return -ENOMEM;
}
- my_pid = getpid();
+ my_pid = getpid_cached();
do {
_cleanup_fclose_ FILE *f = NULL;
if (!s)
return -ENOMEM;
- my_pid = getpid();
+ my_pid = getpid_cached();
do {
_cleanup_fclose_ FILE *f = NULL;
return r;
if (pid == 0)
- pid = getpid();
+ pid = getpid_cached();
xsprintf(c, PID_FMT "\n", pid);
#include "missing.h"
#include "parse-util.h"
#include "path-util.h"
+#include "process-util.h"
#include "socket-util.h"
#include "stdio-util.h"
#include "util.h"
return true;
/* Try to use kcmp() if we have it. */
- pid = getpid();
+ pid = getpid_cached();
r = kcmp(pid, pid, KCMP_FILE, a, b);
if (r == 0)
return true;
#include "missing.h"
#include "parse-util.h"
#include "path-util.h"
+#include "process-util.h"
#include "random-util.h"
#include "stdio-util.h"
#include "string-util.h"
if (fd < 0) {
const char *path;
- path = getpid() == 1 ? "/run/systemd" : "/tmp";
+ path = getpid_cached() == 1 ? "/run/systemd" : "/tmp";
fd = open_tmpfile_unlinkable(path, O_RDWR|O_CLOEXEC);
if (fd < 0)
return fd;
if (st.st_mode & 0002)
log_warning("Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway.", path);
- if (getpid() == 1 && (st.st_mode & 0044) != 0044)
+ if (getpid_cached() == 1 && (st.st_mode & 0044) != 0044)
log_warning("Configuration file %s is marked world-inaccessible. This has no effect as configuration data is accessible via APIs without restrictions. Proceeding anyway.", path);
return 0;
if (console_fd < 0)
return;
- if (getpid() == 1) {
+ if (getpid_cached() == 1) {
if (console_fd >= 3)
safe_close(console_fd);
/* We need a blocking fd here since we'd otherwise lose
messages way too early. However, let's not hang forever in the
unlikely case of a deadlock. */
- if (getpid() == 1)
+ if (getpid_cached() == 1)
timeval_store(&tv, 10 * USEC_PER_MSEC);
else
timeval_store(&tv, 10 * USEC_PER_SEC);
}
if (!IN_SET(log_target, LOG_TARGET_AUTO, LOG_TARGET_SAFE) ||
- getpid() == 1 ||
+ getpid_cached() == 1 ||
isatty(STDERR_FILENO) <= 0) {
if (IN_SET(log_target, LOG_TARGET_AUTO,
if (writev(console_fd, iovec, n) < 0) {
- if (errno == EIO && getpid() == 1) {
+ if (errno == EIO && getpid_cached() == 1) {
/* If somebody tried to kick us from our
* console tty (via vhangup() or suchlike),
if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
return -EINVAL;
- xsprintf(header_pid, "["PID_FMT"]: ", getpid());
+ xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
IOVEC_SET_STRING(iovec[0], header_priority);
IOVEC_SET_STRING(iovec[1], header_time);
return 0;
xsprintf(header_priority, "<%i>", level);
- xsprintf(header_pid, "["PID_FMT"]: ", getpid());
+ xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
IOVEC_SET_STRING(iovec[0], header_priority);
IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
va_end(ap);
if (unit)
- unit_fmt = getpid() == 1 ? "UNIT=%s" : "USER_UNIT=%s";
+ unit_fmt = getpid_cached() == 1 ? "UNIT=%s" : "USER_UNIT=%s";
return log_struct_internal(
LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, level),
#include "sd-id128.h"
#include "macro.h"
+#include "process-util.h"
typedef enum LogRealm {
LOG_REALM_SYSTEMD,
#define log_notice(...) log_full(LOG_NOTICE, __VA_ARGS__)
#define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__)
#define log_error(...) log_full(LOG_ERR, __VA_ARGS__)
-#define log_emergency(...) log_full(getpid() == 1 ? LOG_EMERG : LOG_ERR, __VA_ARGS__)
+#define log_emergency(...) log_full(getpid_cached() == 1 ? LOG_EMERG : LOG_ERR, __VA_ARGS__)
/* Logging triggered by an errno-like error */
#define log_debug_errno(error, ...) log_full_errno(LOG_DEBUG, error, __VA_ARGS__)
#define log_notice_errno(error, ...) log_full_errno(LOG_NOTICE, error, __VA_ARGS__)
#define log_warning_errno(error, ...) log_full_errno(LOG_WARNING, error, __VA_ARGS__)
#define log_error_errno(error, ...) log_full_errno(LOG_ERR, error, __VA_ARGS__)
-#define log_emergency_errno(error, ...) log_full_errno(getpid() == 1 ? LOG_EMERG : LOG_ERR, error, __VA_ARGS__)
+#define log_emergency_errno(error, ...) log_full_errno(getpid_cached() == 1 ? LOG_EMERG : LOG_ERR, error, __VA_ARGS__)
#ifdef LOG_TRACE
# define log_trace(...) log_debug(__VA_ARGS__)
static thread_local int cached = 0;
if (_unlikely_(cached == 0))
- cached = getpid() == gettid() ? 1 : -1;
+ cached = getpid_cached() == gettid() ? 1 : -1;
return cached > 0;
}
void valgrind_summary_hack(void) {
#ifdef HAVE_VALGRIND_VALGRIND_H
- if (getpid() == 1 && RUNNING_ON_VALGRIND) {
+ if (getpid_cached() == 1 && RUNNING_ON_VALGRIND) {
pid_t pid;
pid = raw_clone(SIGCHLD);
if (pid < 0)
val = getenv_bool("SYSTEMD_COLORS");
if (val >= 0)
enabled = val;
- else if (getpid() == 1)
+ else if (getpid_cached() == 1)
/* PID1 outputs to the console without holding it open all the time */
enabled = !getenv_terminal_is_dumb();
else
/* Spawns a temporary TTY agent, making sure it goes away when
* we go away */
- parent_pid = getpid();
+ parent_pid = getpid_cached();
/* First we temporarily block all signals, so that the new
* child has them blocked initially. This way, we can be sure
goto finish;
}
- if (getpid() == 1) {
+ if (getpid_cached() == 1) {
/* If we are PID 1 we can just check our own environment variable, and that's authoritative. */
e = getenv("container");
}
xsprintf(options, "fd=%i,pgrp="PID_FMT",minproto=5,maxproto=5,direct", p[1], getpgrp());
- xsprintf(name, "systemd-"PID_FMT, getpid());
+ xsprintf(name, "systemd-"PID_FMT, getpid_cached());
if (mount(name, a->where, "autofs", 0, options) < 0) {
r = -errno;
goto fail;
if (r < 0)
return r;
- mypid = getpid();
+ mypid = getpid_cached();
while (cg_read_pid(f, &npid) > 0) {
pid_t ppid;
if (MANAGER_IS_SYSTEM(m)) {
/* We want the private bus only when running as init */
- if (getpid() != 1)
+ if (getpid_cached() != 1)
return 0;
strcpy(sa.un.sun_path, "/run/systemd/private");
assert_se(sigprocmask_many(SIG_BLOCK, &old_ss, SIGTERM, -1) >= 0);
- parent_pid = getpid();
+ parent_pid = getpid_cached();
pam_pid = fork();
if (pam_pid < 0) {
if (n_fds > 0) {
_cleanup_free_ char *joined = NULL;
- if (asprintf(&x, "LISTEN_PID="PID_FMT, getpid()) < 0)
+ if (asprintf(&x, "LISTEN_PID="PID_FMT, getpid_cached()) < 0)
return -ENOMEM;
our_env[n_env++] = x;
}
if ((p->flags & EXEC_SET_WATCHDOG) && p->watchdog_usec > 0) {
- if (asprintf(&x, "WATCHDOG_PID="PID_FMT, getpid()) < 0)
+ if (asprintf(&x, "WATCHDOG_PID="PID_FMT, getpid_cached()) < 0)
return -ENOMEM;
our_env[n_env++] = x;
}
if (context->utmp_id)
- utmp_put_init_process(context->utmp_id, getpid(), getsid(0),
+ utmp_put_init_process(context->utmp_id, getpid_cached(), getsid(0),
context->tty_path,
context->utmp_mode == EXEC_UTMP_INIT ? INIT_PROCESS :
context->utmp_mode == EXEC_UTMP_LOGIN ? LOGIN_PROCESS :
struct sigaction sa;
pid_t pid;
- if (getpid() != 1)
+ if (getpid_cached() != 1)
/* Pass this on immediately, if this is not PID 1 */
(void) raise(sig);
else if (!arg_dump_core)
assert(argc >= 1);
assert(argv);
- if (getpid() == 1)
+ if (getpid_cached() == 1)
opterr = 0;
while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
* parse_proc_cmdline_word() or ignore. */
case '?':
- if (getpid() != 1)
+ if (getpid_cached() != 1)
return -EINVAL;
else
return 0;
assert_not_reached("Unhandled option code.");
}
- if (optind < argc && getpid() != 1) {
+ if (optind < argc && getpid_cached() != 1) {
/* Hmm, when we aren't run as init system
* let's complain about excess arguments */
const char *error_message = NULL;
#ifdef HAVE_SYSV_COMPAT
- if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
+ if (getpid_cached() != 1 && strstr(program_invocation_short_name, "init")) {
/* This is compatibility support for SysV, where
* calling init as a user is identical to telinit. */
log_set_upgrade_syslog_to_journal(true);
- if (getpid() == 1) {
+ if (getpid_cached() == 1) {
/* Disable the umask logic */
umask(0);
log_set_always_reopen_console(true);
}
- if (getpid() == 1 && detect_container() <= 0) {
+ if (getpid_cached() == 1 && detect_container() <= 0) {
/* Running outside of a container as PID 1 */
arg_system = true;
* might redirect output elsewhere. */
log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
- } else if (getpid() == 1) {
+ } else if (getpid_cached() == 1) {
/* Running inside a container, as PID 1 */
arg_system = true;
log_set_target(LOG_TARGET_CONSOLE);
kernel_timestamp = DUAL_TIMESTAMP_NULL;
}
- if (getpid() == 1) {
+ if (getpid_cached() == 1) {
/* Don't limit the core dump size, so that coredump handlers such as systemd-coredump (which honour the limit)
* will process core dumps for system services by default. */
if (setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(RLIM_INFINITY)) < 0)
/* Mount /proc, /sys and friends, so that /proc/cmdline and
* /proc/$PID/fd is available. */
- if (getpid() == 1) {
+ if (getpid_cached() == 1) {
/* Load the kernel modules early, so that we kdbus.ko is loaded before kdbusfs shall be mounted */
if (!skip_setup)
* tty. */
release_terminal();
- if (getpid() == 1 && !skip_setup)
+ if (getpid_cached() == 1 && !skip_setup)
console_setup();
}
/* Make sure we leave a core dump without panicing the
* kernel. */
- if (getpid() == 1) {
+ if (getpid_cached() == 1) {
install_crash_handler();
r = mount_cgroup_controllers(arg_join_controllers);
* here explicitly. valgrind will only generate nice output on
* exit(), not on exec(), hence let's do the former not the
* latter here. */
- if (getpid() == 1 && RUNNING_ON_VALGRIND)
+ if (getpid_cached() == 1 && RUNNING_ON_VALGRIND)
return 0;
#endif
execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block);
log_error_errno(errno, "Failed to execute shutdown binary, %s: %m",
- getpid() == 1 ? "freezing" : "quitting");
+ getpid_cached() == 1 ? "freezing" : "quitting");
}
- if (getpid() == 1) {
+ if (getpid_cached() == 1) {
if (error_message)
manager_status_printf(NULL, STATUS_TYPE_EMERGENCY,
ANSI_HIGHLIGHT_RED "!!!!!!" ANSI_NORMAL,
if (pid <= 1)
return -EINVAL;
- if (pid == getpid())
+ if (pid == getpid_cached())
return -EINVAL;
if (s->main_pid == pid && s->main_pid_known)
s->main_pid = pid;
s->main_pid_known = true;
- if (get_process_ppid(pid, &ppid) >= 0 && ppid != getpid()) {
+ if (get_process_ppid(pid, &ppid) >= 0 && ppid != getpid_cached()) {
log_unit_warning(UNIT(s), "Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", pid);
s->main_pid_alien = true;
} else
return -ENOMEM;
if (MANAGER_IS_USER(UNIT(s)->manager))
- if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0)
+ if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid_cached()) < 0)
return -ENOMEM;
if (s->socket_fd >= 0) {
log_unit_warning(u, "Failed to parse MAINPID= field in notification message: %s", e);
else if (pid == s->control_pid)
log_unit_warning(u, "A control process cannot also be the main process");
- else if (pid == getpid() || pid == 1)
+ else if (pid == getpid_cached() || pid == 1)
log_unit_warning(u, "Service manager can't be main process, ignoring sd_notify() MAINPID= field");
else {
service_set_main_pid(s, pid);
umask(0022);
- if (getpid() != 1) {
+ if (getpid_cached() != 1) {
log_error("Not executed by init (PID 1).");
r = -EPERM;
goto error;
if (server_init(&server, (unsigned) n) < 0)
return EXIT_FAILURE;
- log_debug("systemd-initctl running as pid "PID_FMT, getpid());
+ log_debug("systemd-initctl running as pid "PID_FMT, getpid_cached());
sd_notify(false,
"READY=1\n"
r = EXIT_SUCCESS;
- log_debug("systemd-initctl stopped as pid "PID_FMT, getpid());
+ log_debug("systemd-initctl stopped as pid "PID_FMT, getpid_cached());
fail:
sd_notify(false,
if (pipe(fd) < 0)
return log_error_errno(errno, "Failed to create pager pipe: %m");
- parent_pid = getpid();
+ parent_pid = getpid_cached();
child_pid = fork();
if (child_pid < 0) {
log_debug("Watchdog is %sd.", enable_disable(r > 0));
log_debug("%s running as pid "PID_FMT,
- program_invocation_short_name, getpid());
+ program_invocation_short_name, getpid_cached());
sd_notify(false,
"READY=1\n"
"STATUS=Processing requests...");
goto cleanup;
log_debug("%s running as pid "PID_FMT,
- program_invocation_short_name, getpid());
+ program_invocation_short_name, getpid_cached());
use_journal = optind >= argc;
if (use_journal) {
if (parse_pid(pid, &t) < 0)
return false;
- return t == getpid();
+ return t == getpid_cached();
}
static void dev_kmsg_record(Server *s, const char *p, size_t l) {
/* Error handling below */
va_end(ap);
- ucred.pid = getpid();
+ ucred.pid = getpid_cached();
ucred.uid = getuid();
ucred.gid = getgid();
* let's fix it as good as we can, and retry */
u = *ucred;
- u.pid = getpid();
+ u.pid = getpid_cached();
memcpy(CMSG_DATA(cmsg), &u, sizeof(struct ucred));
if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
server_flush_to_var(&server, true);
server_flush_dev_kmsg(&server);
- log_debug("systemd-journald running as pid "PID_FMT, getpid());
+ log_debug("systemd-journald running as pid "PID_FMT, getpid_cached());
server_driver_message(&server,
"MESSAGE_ID=" SD_MESSAGE_JOURNAL_START_STR,
LOG_MESSAGE("Journal started"),
server_maybe_warn_forward_syslog_missed(&server);
}
- log_debug("systemd-journald stopped as pid "PID_FMT, getpid());
+ log_debug("systemd-journald stopped as pid "PID_FMT, getpid_cached());
server_driver_message(&server,
"MESSAGE_ID=" SD_MESSAGE_JOURNAL_STOP_STR,
LOG_MESSAGE("Journal stopped"),
/* We don't support people creating a journal object and
* keeping it around over a fork(). Let's complain. */
- return j->original_pid != getpid();
+ return j->original_pid != getpid_cached();
}
static int journal_put_error(sd_journal *j, int r, const char *path) {
if (!j)
return NULL;
- j->original_pid = getpid();
+ j->original_pid = getpid_cached();
j->toplevel_fd = -1;
j->inotify_fd = -1;
j->flags = flags;
if (argc == 3)
(void) safe_atozu(argv[2], &arg_start);
else
- arg_start = getpid();
+ arg_start = getpid_cached();
NULSTR_FOREACH(i, "zeros\0simple\0random\0") {
#ifdef HAVE_XZ
assert_return(ret, -EINVAL);
if (pid == 0)
- pid = getpid();
+ pid = getpid_cached();
c = bus_creds_new();
if (!c)
r->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
r->attach_flags |= KDBUS_ATTACH_NAMES;
- r->original_pid = getpid();
+ r->original_pid = getpid_cached();
assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);
/* We don't support people creating a bus connection and
* keeping it around over a fork(). Let's complain. */
- return bus->original_pid != getpid();
+ return bus->original_pid != getpid_cached();
}
static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
assert_se(arg_loop_usec > 0);
if (type == TYPE_KDBUS) {
- assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
+ assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid_cached()) >= 0);
bus_ref = bus_kernel_create_bus(name, false, &bus_name);
if (bus_ref == -ENOENT)
sd_bus *a, *b;
int r, found = 0;
- assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
+ assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid_cached()) >= 0);
bus_ref = bus_kernel_create_bus(name, false, &bus_name);
if (bus_ref == -ENOENT)
log_set_max_level(LOG_DEBUG);
- assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
+ assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid_cached()) >= 0);
bus_ref = bus_kernel_create_bus(name, false, &bus_name);
if (bus_ref == -ENOENT)
log_set_max_level(LOG_DEBUG);
- assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
+ assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid_cached()) >= 0);
bus_ref = bus_kernel_create_bus(name, false, &bus_name);
if (bus_ref == -ENOENT)
goto finish;
/* Is this for us? */
- if (getpid() != pid) {
+ if (getpid_cached() != pid) {
r = 0;
goto finish;
}
msghdr.msg_namelen = SOCKADDR_UN_LEN(sockaddr.un);
- have_pid = pid != 0 && pid != getpid();
+ have_pid = pid != 0 && pid != getpid_cached();
if (n_fds > 0 || have_pid) {
/* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
goto finish;
/* Is this for us? */
- if (getpid() != pid) {
+ if (getpid_cached() != pid) {
r = 0;
goto finish;
}
e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->boottime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
e->realtime.next = e->boottime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = USEC_INFINITY;
e->realtime.wakeup = e->boottime.wakeup = e->monotonic.wakeup = e->realtime_alarm.wakeup = e->boottime_alarm.wakeup = WAKEUP_CLOCK_DATA;
- e->original_pid = getpid();
+ e->original_pid = getpid_cached();
e->perturb = USEC_INFINITY;
r = prioq_ensure_allocated(&e->pending, pending_prioq_compare);
/* We don't support people creating an event loop and keeping
* it around over a fork(). Let's complain. */
- return e->original_pid != getpid();
+ return e->original_pid != getpid_cached();
}
static void source_io_unregister(sd_event_source *s) {
assert_se(sd_event_source_set_priority(v, -10) >= 0);
- assert(sigqueue(getpid(), SIGRTMIN+2, (union sigval) { .sival_int = 1 }) >= 0);
- assert(sigqueue(getpid(), SIGRTMIN+3, (union sigval) { .sival_int = 2 }) >= 0);
- assert(sigqueue(getpid(), SIGUSR2, (union sigval) { .sival_int = 3 }) >= 0);
- assert(sigqueue(getpid(), SIGRTMIN+3, (union sigval) { .sival_int = 4 }) >= 0);
- assert(sigqueue(getpid(), SIGUSR2, (union sigval) { .sival_int = 5 }) >= 0);
+ assert(sigqueue(getpid_cached(), SIGRTMIN+2, (union sigval) { .sival_int = 1 }) >= 0);
+ assert(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 2 }) >= 0);
+ assert(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 3 }) >= 0);
+ assert(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 4 }) >= 0);
+ assert(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 5 }) >= 0);
assert_se(n_rtqueue == 0);
assert_se(last_rtqueue_sigval == 0);
rtnl->n_ref = REFCNT_INIT;
rtnl->fd = -1;
rtnl->sockaddr.nl.nl_family = AF_NETLINK;
- rtnl->original_pid = getpid();
+ rtnl->original_pid = getpid_cached();
LIST_HEAD_INIT(rtnl->match_callbacks);
/* We don't support people creating an rtnl connection and
* keeping it around over a fork(). Let's complain. */
- return rtnl->original_pid != getpid();
+ return rtnl->original_pid != getpid_cached();
}
int sd_netlink_open_fd(sd_netlink **ret, int fd) {
/* We don't support people creating a resolver and keeping it
* around after fork(). Let's complain. */
- return r->original_pid != getpid();
+ return r->original_pid != getpid_cached();
}
_public_ int sd_resolve_new(sd_resolve **ret) {
return -ENOMEM;
resolve->n_ref = 1;
- resolve->original_pid = getpid();
+ resolve->original_pid = getpid_cached();
for (i = 0; i < _FD_MAX; i++)
resolve->fds[i] = -1;
goto finish;
}
- log_debug("systemd-logind running as pid "PID_FMT, getpid());
+ log_debug("systemd-logind running as pid "PID_FMT, getpid_cached());
sd_notify(false,
"READY=1\n"
r = manager_run(m);
- log_debug("systemd-logind stopped as pid "PID_FMT, getpid());
+ log_debug("systemd-logind stopped as pid "PID_FMT, getpid_cached());
finish:
sd_notify(false,
#include "login-util.h"
#include "macro.h"
#include "parse-util.h"
+#include "process-util.h"
#include "socket-util.h"
#include "strv.h"
#include "terminal-util.h"
if (debug)
pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: "
"uid="UID_FMT" pid="PID_FMT" service=%s type=%s class=%s desktop=%s seat=%s vtnr=%"PRIu32" tty=%s display=%s remote=%s remote_user=%s remote_host=%s",
- pw->pw_uid, getpid(),
+ pw->pw_uid, getpid_cached(),
strempty(service),
type, class, strempty(desktop),
strempty(seat), vtnr, strempty(tty), strempty(display),
&reply,
"uusssssussbssa(sv)",
(uint32_t) pw->pw_uid,
- (uint32_t) getpid(),
+ (uint32_t) getpid_cached(),
service,
type,
class,
#include "label.h"
#include "machine-image.h"
#include "machined.h"
+#include "process-util.h"
#include "signal-util.h"
Manager *manager_new(void) {
goto finish;
}
- log_debug("systemd-machined running as pid "PID_FMT, getpid());
+ log_debug("systemd-machined running as pid "PID_FMT, getpid_cached());
sd_notify(false,
"READY=1\n"
r = manager_run(m);
- log_debug("systemd-machined stopped as pid "PID_FMT, getpid());
+ log_debug("systemd-machined stopped as pid "PID_FMT, getpid_cached());
finish:
manager_free(m);
if (r < 0)
return r;
- r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid());
+ r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid_cached());
if (r < 0)
return r;
#include "macro.h"
#include "missing.h"
#include "mkdir.h"
+#include "process-util.h"
#include "random-util.h"
#include "signal-util.h"
#include "socket-util.h"
"AcceptCached=%i\n"
"Echo=%i\n"
"NotAfter="USEC_FMT"\n",
- getpid(),
+ getpid_cached(),
socket_name,
(flags & ASK_PASSWORD_ACCEPT_CACHED) ? 1 : 0,
(flags & ASK_PASSWORD_ECHO) ? 1 : 0,
#include "parse-util.h"
#include "path-util.h"
#include "proc-cmdline.h"
+#include "process-util.h"
#include "selinux-util.h"
#include "smack-util.h"
#include "stat-util.h"
if (streq(username, c->parameter))
return 1;
- if (getpid() == 1)
+ if (getpid_cached() == 1)
return streq(c->parameter, "root");
u = c->parameter;
return in_gid(id);
/* Avoid any NSS lookups if we are PID1 */
- if (getpid() == 1)
+ if (getpid_cached() == 1)
return streq(c->parameter, "root");
return in_group(c->parameter) > 0;
if (pipe(fd) < 0)
return log_error_errno(errno, "Failed to create pager pipe: %m");
- parent_pid = getpid();
+ parent_pid = getpid_cached();
pager_pid = fork();
if (pager_pid < 0)
assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, "/test-b/test-c") == 0);
assert_se(cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, "/test-b", 0) == 0);
- assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid(), &path) == 0);
+ assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path) == 0);
assert_se(streq(path, "/test-b"));
free(path);
assert_se(cg_attach(SYSTEMD_CGROUP_CONTROLLER, "/test-a", 0) == 0);
- assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid(), &path) == 0);
+ assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path) == 0);
assert_se(path_equal(path, "/test-a"));
free(path);
assert_se(cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, "/test-b/test-d", 0) == 0);
- assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid(), &path) == 0);
+ assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path) == 0);
assert_se(path_equal(path, "/test-b/test-d"));
free(path);
log_open();
log_struct(LOG_INFO,
- "MESSAGE=Waldo PID="PID_FMT, getpid(),
+ "MESSAGE=Waldo PID="PID_FMT, getpid_cached(),
"SERVICE=piepapo",
NULL);
log_open();
log_struct(LOG_INFO,
- "MESSAGE=Foobar PID="PID_FMT, getpid(),
+ "MESSAGE=Foobar PID="PID_FMT, getpid_cached(),
"SERVICE=foobar",
NULL);
log_struct(LOG_INFO,
- "MESSAGE=Foobar PID="PID_FMT, getpid(),
+ "MESSAGE=Foobar PID="PID_FMT, getpid_cached(),
"FORMAT_STR_TEST=1=%i A=%c 2=%hi 3=%li 4=%lli 1=%p foo=%s 2.5=%g 3.5=%g 4.5=%Lg",
(int) 1, 'A', (short) 2, (long int) 3, (long long int) 4, (void*) 1, "foo", (float) 2.5f, (double) 3.5, (long double) 4.5,
"SUFFIX=GOT IT",
waitpid(pid, &status, 0);
assert_se(!pid_is_unwaited(pid));
}
- assert_se(pid_is_unwaited(getpid()));
+ assert_se(pid_is_unwaited(getpid_cached()));
assert_se(!pid_is_unwaited(-1));
}
waitpid(pid, &status, 0);
assert_se(!pid_is_alive(pid));
}
- assert_se(pid_is_alive(getpid()));
+ assert_se(pid_is_alive(getpid_cached()));
assert_se(!pid_is_alive(-1));
}
assert_se(prctl(PR_SET_NAME, "testa") >= 0);
- assert_se(get_process_cmdline(getpid(), 0, false, &line) == -ENOENT);
+ assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
- assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
assert_se(streq(line, "[testa]"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 1, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0);
assert_se(streq(line, ""));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 2, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0);
assert_se(streq(line, "["));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 3, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0);
assert_se(streq(line, "[."));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 4, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0);
assert_se(streq(line, "[.."));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 5, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0);
assert_se(streq(line, "[..."));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 6, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0);
assert_se(streq(line, "[...]"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 7, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0);
assert_se(streq(line, "[t...]"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 8, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0);
assert_se(streq(line, "[testa]"));
line = mfree(line);
assert_se(write(fd, "\0\0\0\0\0\0\0\0\0", 10) == 10);
- assert_se(get_process_cmdline(getpid(), 0, false, &line) == -ENOENT);
+ assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
- assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
assert_se(streq(line, "[testa]"));
line = mfree(line);
assert_se(write(fd, "foo\0bar\0\0\0\0\0", 10) == 10);
- assert_se(get_process_cmdline(getpid(), 0, false, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0);
assert_se(streq(line, "foo bar"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
assert_se(streq(line, "foo bar"));
line = mfree(line);
assert_se(write(fd, "quux", 4) == 4);
- assert_se(get_process_cmdline(getpid(), 0, false, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) >= 0);
assert_se(streq(line, "foo bar quux"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
assert_se(streq(line, "foo bar quux"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 1, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 1, true, &line) >= 0);
assert_se(streq(line, ""));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 2, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 2, true, &line) >= 0);
assert_se(streq(line, "."));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 3, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 3, true, &line) >= 0);
assert_se(streq(line, ".."));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 4, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 4, true, &line) >= 0);
assert_se(streq(line, "..."));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 5, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 5, true, &line) >= 0);
assert_se(streq(line, "f..."));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 6, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 6, true, &line) >= 0);
assert_se(streq(line, "fo..."));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 7, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 7, true, &line) >= 0);
assert_se(streq(line, "foo..."));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 8, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 8, true, &line) >= 0);
assert_se(streq(line, "foo..."));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 9, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 9, true, &line) >= 0);
assert_se(streq(line, "foo b..."));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 10, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0);
assert_se(streq(line, "foo ba..."));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 11, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0);
assert_se(streq(line, "foo bar..."));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 12, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0);
assert_se(streq(line, "foo bar..."));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 13, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 13, true, &line) >= 0);
assert_se(streq(line, "foo bar quux"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 14, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 14, true, &line) >= 0);
assert_se(streq(line, "foo bar quux"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 1000, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 1000, true, &line) >= 0);
assert_se(streq(line, "foo bar quux"));
line = mfree(line);
assert_se(ftruncate(fd, 0) >= 0);
assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0);
- assert_se(get_process_cmdline(getpid(), 0, false, &line) == -ENOENT);
+ assert_se(get_process_cmdline(getpid_cached(), 0, false, &line) == -ENOENT);
- assert_se(get_process_cmdline(getpid(), 0, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 0, true, &line) >= 0);
assert_se(streq(line, "[aaaa bbbb cccc]"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 10, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 10, true, &line) >= 0);
assert_se(streq(line, "[aaaa...]"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 11, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 11, true, &line) >= 0);
assert_se(streq(line, "[aaaa...]"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid(), 12, true, &line) >= 0);
+ assert_se(get_process_cmdline(getpid_cached(), 12, true, &line) >= 0);
assert_se(streq(line, "[aaaa b...]"));
line = mfree(line);
static void test_ignore_signals(void) {
assert_se(ignore_signals(SIGINT, -1) >= 0);
- assert_se(kill(getpid(), SIGINT) >= 0);
+ assert_se(kill(getpid_cached(), SIGINT) >= 0);
assert_se(ignore_signals(SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
- assert_se(kill(getpid(), SIGUSR1) >= 0);
- assert_se(kill(getpid(), SIGUSR2) >= 0);
- assert_se(kill(getpid(), SIGTERM) >= 0);
- assert_se(kill(getpid(), SIGPIPE) >= 0);
+ assert_se(kill(getpid_cached(), SIGUSR1) >= 0);
+ assert_se(kill(getpid_cached(), SIGUSR2) >= 0);
+ assert_se(kill(getpid_cached(), SIGTERM) >= 0);
+ assert_se(kill(getpid_cached(), SIGPIPE) >= 0);
assert_se(default_signals(SIGINT, SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
}
fd = open_tmpfile_unlinkable(p, O_RDWR|O_CLOEXEC);
assert_se(fd >= 0);
- assert_se(asprintf(&cmd, "ls -l /proc/"PID_FMT"/fd/%d", getpid(), fd) > 0);
+ assert_se(asprintf(&cmd, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd) > 0);
(void) system(cmd);
assert_se(readlink_malloc(cmd + 6, &ans) >= 0);
log_debug("link1: %s", ans);
assert_se(fd >= 0);
assert_se(unlink(pattern) == 0);
- assert_se(asprintf(&cmd2, "ls -l /proc/"PID_FMT"/fd/%d", getpid(), fd2) > 0);
+ assert_se(asprintf(&cmd2, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd2) > 0);
(void) system(cmd2);
assert_se(readlink_malloc(cmd2 + 6, &ans2) >= 0);
log_debug("link2: %s", ans2);
#include "fd-util.h"
#include "fs-util.h"
#include "network-util.h"
+#include "process-util.h"
#include "signal-util.h"
#include "timesyncd-conf.h"
#include "timesyncd-manager.h"
goto finish;
}
- log_debug("systemd-timesyncd running as pid " PID_FMT, getpid());
+ log_debug("systemd-timesyncd running as pid " PID_FMT, getpid_cached());
sd_notify(false,
"READY=1\n"
"STATUS=Daemon is running");
if (udev_list_node_is_empty(&event->manager->events)) {
/* only clean up the queue from the process that created it */
- if (event->manager->pid == getpid()) {
+ if (event->manager->pid == getpid_cached()) {
r = unlink("/run/udev/queue");
if (r < 0)
log_warning_errno(errno, "could not unlink /run/udev/queue: %m");
/* only one process can add events to the queue */
if (manager->pid == 0)
- manager->pid = getpid();
+ manager->pid = getpid_cached();
- assert(manager->pid == getpid());
+ assert(manager->pid == getpid_cached());
event = new0(struct event, 1);
if (!event)
#include "format-util.h"
#include "log.h"
#include "macro.h"
+#include "process-util.h"
#include "special.h"
#include "strv.h"
#include "unit-name.h"
goto finish;
}
- log_debug("systemd-update-utmp running as pid "PID_FMT, getpid());
+ log_debug("systemd-update-utmp running as pid "PID_FMT, getpid_cached());
if (streq(argv[1], "reboot"))
r = on_reboot(&c);
r = -EINVAL;
}
- log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid());
+ log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid_cached());
finish:
#ifdef HAVE_AUDIT