tree-wide: make use of getpid_cached() wherever we can

This moves pretty much all uses of getpid() over to getpid_raw(). I
didn't specifically check whether the optimization is worth it for each
replacement, but in order to keep things simple and systematic I
switched over everything at once.
This commit is contained in:
Lennart Poettering 2017-07-20 16:19:18 +02:00
parent 5c30a6d2b8
commit df0ff12775
53 changed files with 161 additions and 151 deletions

View File

@ -31,6 +31,7 @@
#include "fd-util.h" #include "fd-util.h"
#include "log.h" #include "log.h"
#include "macro.h" #include "macro.h"
#include "process-util.h"
#include "signal-util.h" #include "signal-util.h"
#include "socket-util.h" #include "socket-util.h"
#include "string-util.h" #include "string-util.h"
@ -221,7 +222,7 @@ static int exec_process(const char* name, char **argv, char **env, int start_fd,
if (asprintf((char**)(envp + n_env++), "LISTEN_FDS=%i", n_fds) < 0) if (asprintf((char**)(envp + n_env++), "LISTEN_FDS=%i", n_fds) < 0)
return log_oom(); 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(); return log_oom();
if (arg_fdnames) { if (arg_fdnames) {
@ -271,7 +272,7 @@ static int fork_and_exec_process(const char* child, char** argv, char **env, int
if (!joined) if (!joined)
return log_oom(); return log_oom();
parent_pid = getpid(); parent_pid = getpid_cached();
child_pid = fork(); child_pid = fork();
if (child_pid < 0) if (child_pid < 0)

View File

@ -255,7 +255,7 @@ int cg_kill(
return -ENOMEM; return -ENOMEM;
} }
my_pid = getpid(); my_pid = getpid_cached();
do { do {
_cleanup_fclose_ FILE *f = NULL; _cleanup_fclose_ FILE *f = NULL;
@ -399,7 +399,7 @@ int cg_migrate(
if (!s) if (!s)
return -ENOMEM; return -ENOMEM;
my_pid = getpid(); my_pid = getpid_cached();
do { do {
_cleanup_fclose_ FILE *f = NULL; _cleanup_fclose_ FILE *f = NULL;
@ -825,7 +825,7 @@ int cg_attach(const char *controller, const char *path, pid_t pid) {
return r; return r;
if (pid == 0) if (pid == 0)
pid = getpid(); pid = getpid_cached();
xsprintf(c, PID_FMT "\n", pid); xsprintf(c, PID_FMT "\n", pid);

View File

@ -31,6 +31,7 @@
#include "missing.h" #include "missing.h"
#include "parse-util.h" #include "parse-util.h"
#include "path-util.h" #include "path-util.h"
#include "process-util.h"
#include "socket-util.h" #include "socket-util.h"
#include "stdio-util.h" #include "stdio-util.h"
#include "util.h" #include "util.h"
@ -282,7 +283,7 @@ int same_fd(int a, int b) {
return true; return true;
/* Try to use kcmp() if we have it. */ /* Try to use kcmp() if we have it. */
pid = getpid(); pid = getpid_cached();
r = kcmp(pid, pid, KCMP_FILE, a, b); r = kcmp(pid, pid, KCMP_FILE, a, b);
if (r == 0) if (r == 0)
return true; return true;

View File

@ -41,6 +41,7 @@
#include "missing.h" #include "missing.h"
#include "parse-util.h" #include "parse-util.h"
#include "path-util.h" #include "path-util.h"
#include "process-util.h"
#include "random-util.h" #include "random-util.h"
#include "stdio-util.h" #include "stdio-util.h"
#include "string-util.h" #include "string-util.h"
@ -1399,7 +1400,7 @@ int open_serialization_fd(const char *ident) {
if (fd < 0) { if (fd < 0) {
const char *path; 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); fd = open_tmpfile_unlinkable(path, O_RDWR|O_CLOEXEC);
if (fd < 0) if (fd < 0)
return fd; return fd;

View File

@ -307,7 +307,7 @@ int fd_warn_permissions(const char *path, int fd) {
if (st.st_mode & 0002) if (st.st_mode & 0002)
log_warning("Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway.", path); 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); 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; return 0;

View File

@ -84,7 +84,7 @@ void log_close_console(void) {
if (console_fd < 0) if (console_fd < 0)
return; return;
if (getpid() == 1) { if (getpid_cached() == 1) {
if (console_fd >= 3) if (console_fd >= 3)
safe_close(console_fd); safe_close(console_fd);
@ -140,7 +140,7 @@ static int create_log_socket(int type) {
/* We need a blocking fd here since we'd otherwise lose /* We need a blocking fd here since we'd otherwise lose
messages way too early. However, let's not hang forever in the messages way too early. However, let's not hang forever in the
unlikely case of a deadlock. */ unlikely case of a deadlock. */
if (getpid() == 1) if (getpid_cached() == 1)
timeval_store(&tv, 10 * USEC_PER_MSEC); timeval_store(&tv, 10 * USEC_PER_MSEC);
else else
timeval_store(&tv, 10 * USEC_PER_SEC); timeval_store(&tv, 10 * USEC_PER_SEC);
@ -248,7 +248,7 @@ int log_open(void) {
} }
if (!IN_SET(log_target, LOG_TARGET_AUTO, LOG_TARGET_SAFE) || if (!IN_SET(log_target, LOG_TARGET_AUTO, LOG_TARGET_SAFE) ||
getpid() == 1 || getpid_cached() == 1 ||
isatty(STDERR_FILENO) <= 0) { isatty(STDERR_FILENO) <= 0) {
if (IN_SET(log_target, LOG_TARGET_AUTO, if (IN_SET(log_target, LOG_TARGET_AUTO,
@ -370,7 +370,7 @@ static int write_to_console(
if (writev(console_fd, iovec, n) < 0) { 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 /* If somebody tried to kick us from our
* console tty (via vhangup() or suchlike), * console tty (via vhangup() or suchlike),
@ -423,7 +423,7 @@ static int write_to_syslog(
if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0) if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
return -EINVAL; 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[0], header_priority);
IOVEC_SET_STRING(iovec[1], header_time); IOVEC_SET_STRING(iovec[1], header_time);
@ -468,7 +468,7 @@ static int write_to_kmsg(
return 0; return 0;
xsprintf(header_priority, "<%i>", level); 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[0], header_priority);
IOVEC_SET_STRING(iovec[1], program_invocation_short_name); IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
@ -1189,7 +1189,7 @@ int log_syntax_internal(
va_end(ap); va_end(ap);
if (unit) 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( return log_struct_internal(
LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, level), LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, level),

View File

@ -30,6 +30,7 @@
#include "sd-id128.h" #include "sd-id128.h"
#include "macro.h" #include "macro.h"
#include "process-util.h"
typedef enum LogRealm { typedef enum LogRealm {
LOG_REALM_SYSTEMD, LOG_REALM_SYSTEMD,
@ -247,7 +248,7 @@ void log_assert_failed_return_realm(
#define log_notice(...) log_full(LOG_NOTICE, __VA_ARGS__) #define log_notice(...) log_full(LOG_NOTICE, __VA_ARGS__)
#define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__) #define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__)
#define log_error(...) log_full(LOG_ERR, __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 */ /* Logging triggered by an errno-like error */
#define log_debug_errno(error, ...) log_full_errno(LOG_DEBUG, error, __VA_ARGS__) #define log_debug_errno(error, ...) log_full_errno(LOG_DEBUG, error, __VA_ARGS__)
@ -255,7 +256,7 @@ void log_assert_failed_return_realm(
#define log_notice_errno(error, ...) log_full_errno(LOG_NOTICE, 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_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_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 #ifdef LOG_TRACE
# define log_trace(...) log_debug(__VA_ARGS__) # define log_trace(...) log_debug(__VA_ARGS__)

View File

@ -814,7 +814,7 @@ bool is_main_thread(void) {
static thread_local int cached = 0; static thread_local int cached = 0;
if (_unlikely_(cached == 0)) if (_unlikely_(cached == 0))
cached = getpid() == gettid() ? 1 : -1; cached = getpid_cached() == gettid() ? 1 : -1;
return cached > 0; return cached > 0;
} }
@ -878,7 +878,7 @@ const char* personality_to_string(unsigned long p) {
void valgrind_summary_hack(void) { void valgrind_summary_hack(void) {
#ifdef HAVE_VALGRIND_VALGRIND_H #ifdef HAVE_VALGRIND_VALGRIND_H
if (getpid() == 1 && RUNNING_ON_VALGRIND) { if (getpid_cached() == 1 && RUNNING_ON_VALGRIND) {
pid_t pid; pid_t pid;
pid = raw_clone(SIGCHLD); pid = raw_clone(SIGCHLD);
if (pid < 0) if (pid < 0)

View File

@ -1220,7 +1220,7 @@ bool colors_enabled(void) {
val = getenv_bool("SYSTEMD_COLORS"); val = getenv_bool("SYSTEMD_COLORS");
if (val >= 0) if (val >= 0)
enabled = val; enabled = val;
else if (getpid() == 1) else if (getpid_cached() == 1)
/* PID1 outputs to the console without holding it open all the time */ /* PID1 outputs to the console without holding it open all the time */
enabled = !getenv_terminal_is_dumb(); enabled = !getenv_terminal_is_dumb();
else else

View File

@ -219,7 +219,7 @@ int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *pa
/* Spawns a temporary TTY agent, making sure it goes away when /* Spawns a temporary TTY agent, making sure it goes away when
* we go away */ * we go away */
parent_pid = getpid(); parent_pid = getpid_cached();
/* First we temporarily block all signals, so that the new /* First we temporarily block all signals, so that the new
* child has them blocked initially. This way, we can be sure * child has them blocked initially. This way, we can be sure

View File

@ -422,7 +422,7 @@ int detect_container(void) {
goto finish; 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. */ /* If we are PID 1 we can just check our own environment variable, and that's authoritative. */
e = getenv("container"); e = getenv("container");

View File

@ -590,7 +590,7 @@ static void automount_enter_waiting(Automount *a) {
} }
xsprintf(options, "fd=%i,pgrp="PID_FMT",minproto=5,maxproto=5,direct", p[1], getpgrp()); 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) { if (mount(name, a->where, "autofs", 0, options) < 0) {
r = -errno; r = -errno;
goto fail; goto fail;

View File

@ -1593,7 +1593,7 @@ int unit_search_main_pid(Unit *u, pid_t *ret) {
if (r < 0) if (r < 0)
return r; return r;
mypid = getpid(); mypid = getpid_cached();
while (cg_read_pid(f, &npid) > 0) { while (cg_read_pid(f, &npid) > 0) {
pid_t ppid; pid_t ppid;

View File

@ -967,7 +967,7 @@ static int bus_init_private(Manager *m) {
if (MANAGER_IS_SYSTEM(m)) { if (MANAGER_IS_SYSTEM(m)) {
/* We want the private bus only when running as init */ /* We want the private bus only when running as init */
if (getpid() != 1) if (getpid_cached() != 1)
return 0; return 0;
strcpy(sa.un.sun_path, "/run/systemd/private"); strcpy(sa.un.sun_path, "/run/systemd/private");

View File

@ -1099,7 +1099,7 @@ static int setup_pam(
assert_se(sigprocmask_many(SIG_BLOCK, &old_ss, SIGTERM, -1) >= 0); assert_se(sigprocmask_many(SIG_BLOCK, &old_ss, SIGTERM, -1) >= 0);
parent_pid = getpid(); parent_pid = getpid_cached();
pam_pid = fork(); pam_pid = fork();
if (pam_pid < 0) { if (pam_pid < 0) {
@ -1506,7 +1506,7 @@ static int build_environment(
if (n_fds > 0) { if (n_fds > 0) {
_cleanup_free_ char *joined = NULL; _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; return -ENOMEM;
our_env[n_env++] = x; our_env[n_env++] = x;
@ -1525,7 +1525,7 @@ static int build_environment(
} }
if ((p->flags & EXEC_SET_WATCHDOG) && p->watchdog_usec > 0) { 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; return -ENOMEM;
our_env[n_env++] = x; our_env[n_env++] = x;
@ -2564,7 +2564,7 @@ static int exec_child(
} }
if (context->utmp_id) 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->tty_path,
context->utmp_mode == EXEC_UTMP_INIT ? INIT_PROCESS : context->utmp_mode == EXEC_UTMP_INIT ? INIT_PROCESS :
context->utmp_mode == EXEC_UTMP_LOGIN ? LOGIN_PROCESS : context->utmp_mode == EXEC_UTMP_LOGIN ? LOGIN_PROCESS :

View File

@ -154,7 +154,7 @@ noreturn static void crash(int sig) {
struct sigaction sa; struct sigaction sa;
pid_t pid; pid_t pid;
if (getpid() != 1) if (getpid_cached() != 1)
/* Pass this on immediately, if this is not PID 1 */ /* Pass this on immediately, if this is not PID 1 */
(void) raise(sig); (void) raise(sig);
else if (!arg_dump_core) else if (!arg_dump_core)
@ -860,7 +860,7 @@ static int parse_argv(int argc, char *argv[]) {
assert(argc >= 1); assert(argc >= 1);
assert(argv); assert(argv);
if (getpid() == 1) if (getpid_cached() == 1)
opterr = 0; opterr = 0;
while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0) while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
@ -1066,7 +1066,7 @@ static int parse_argv(int argc, char *argv[]) {
* parse_proc_cmdline_word() or ignore. */ * parse_proc_cmdline_word() or ignore. */
case '?': case '?':
if (getpid() != 1) if (getpid_cached() != 1)
return -EINVAL; return -EINVAL;
else else
return 0; return 0;
@ -1075,7 +1075,7 @@ static int parse_argv(int argc, char *argv[]) {
assert_not_reached("Unhandled option code."); 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 /* Hmm, when we aren't run as init system
* let's complain about excess arguments */ * let's complain about excess arguments */
@ -1389,7 +1389,7 @@ int main(int argc, char *argv[]) {
const char *error_message = NULL; const char *error_message = NULL;
#ifdef HAVE_SYSV_COMPAT #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 /* This is compatibility support for SysV, where
* calling init as a user is identical to telinit. */ * calling init as a user is identical to telinit. */
@ -1425,7 +1425,7 @@ int main(int argc, char *argv[]) {
log_set_upgrade_syslog_to_journal(true); log_set_upgrade_syslog_to_journal(true);
if (getpid() == 1) { if (getpid_cached() == 1) {
/* Disable the umask logic */ /* Disable the umask logic */
umask(0); umask(0);
@ -1436,7 +1436,7 @@ int main(int argc, char *argv[]) {
log_set_always_reopen_console(true); 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 */ /* Running outside of a container as PID 1 */
arg_system = true; arg_system = true;
@ -1521,7 +1521,7 @@ int main(int argc, char *argv[]) {
* might redirect output elsewhere. */ * might redirect output elsewhere. */
log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
} else if (getpid() == 1) { } else if (getpid_cached() == 1) {
/* Running inside a container, as PID 1 */ /* Running inside a container, as PID 1 */
arg_system = true; arg_system = true;
log_set_target(LOG_TARGET_CONSOLE); log_set_target(LOG_TARGET_CONSOLE);
@ -1545,7 +1545,7 @@ int main(int argc, char *argv[]) {
kernel_timestamp = DUAL_TIMESTAMP_NULL; 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) /* 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. */ * will process core dumps for system services by default. */
if (setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(RLIM_INFINITY)) < 0) if (setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(RLIM_INFINITY)) < 0)
@ -1580,7 +1580,7 @@ int main(int argc, char *argv[]) {
/* Mount /proc, /sys and friends, so that /proc/cmdline and /* Mount /proc, /sys and friends, so that /proc/cmdline and
* /proc/$PID/fd is available. */ * /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 */ /* Load the kernel modules early, so that we kdbus.ko is loaded before kdbusfs shall be mounted */
if (!skip_setup) if (!skip_setup)
@ -1706,7 +1706,7 @@ int main(int argc, char *argv[]) {
* tty. */ * tty. */
release_terminal(); release_terminal();
if (getpid() == 1 && !skip_setup) if (getpid_cached() == 1 && !skip_setup)
console_setup(); console_setup();
} }
@ -1718,7 +1718,7 @@ int main(int argc, char *argv[]) {
/* Make sure we leave a core dump without panicing the /* Make sure we leave a core dump without panicing the
* kernel. */ * kernel. */
if (getpid() == 1) { if (getpid_cached() == 1) {
install_crash_handler(); install_crash_handler();
r = mount_cgroup_controllers(arg_join_controllers); r = mount_cgroup_controllers(arg_join_controllers);
@ -2152,7 +2152,7 @@ finish:
* here explicitly. valgrind will only generate nice output on * here explicitly. valgrind will only generate nice output on
* exit(), not on exec(), hence let's do the former not the * exit(), not on exec(), hence let's do the former not the
* latter here. */ * latter here. */
if (getpid() == 1 && RUNNING_ON_VALGRIND) if (getpid_cached() == 1 && RUNNING_ON_VALGRIND)
return 0; return 0;
#endif #endif
@ -2228,10 +2228,10 @@ finish:
execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block); execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block);
log_error_errno(errno, "Failed to execute shutdown binary, %s: %m", 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) if (error_message)
manager_status_printf(NULL, STATUS_TYPE_EMERGENCY, manager_status_printf(NULL, STATUS_TYPE_EMERGENCY,
ANSI_HIGHLIGHT_RED "!!!!!!" ANSI_NORMAL, ANSI_HIGHLIGHT_RED "!!!!!!" ANSI_NORMAL,

View File

@ -157,7 +157,7 @@ static int service_set_main_pid(Service *s, pid_t pid) {
if (pid <= 1) if (pid <= 1)
return -EINVAL; return -EINVAL;
if (pid == getpid()) if (pid == getpid_cached())
return -EINVAL; return -EINVAL;
if (s->main_pid == pid && s->main_pid_known) if (s->main_pid == pid && s->main_pid_known)
@ -171,7 +171,7 @@ static int service_set_main_pid(Service *s, pid_t pid) {
s->main_pid = pid; s->main_pid = pid;
s->main_pid_known = true; 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); 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; s->main_pid_alien = true;
} else } else
@ -1283,7 +1283,7 @@ static int service_spawn(
return -ENOMEM; return -ENOMEM;
if (MANAGER_IS_USER(UNIT(s)->manager)) 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; return -ENOMEM;
if (s->socket_fd >= 0) { if (s->socket_fd >= 0) {
@ -3284,7 +3284,7 @@ static void service_notify_message(Unit *u, pid_t pid, char **tags, FDSet *fds)
log_unit_warning(u, "Failed to parse MAINPID= field in notification message: %s", e); log_unit_warning(u, "Failed to parse MAINPID= field in notification message: %s", e);
else if (pid == s->control_pid) else if (pid == s->control_pid)
log_unit_warning(u, "A control process cannot also be the main process"); 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"); log_unit_warning(u, "Service manager can't be main process, ignoring sd_notify() MAINPID= field");
else { else {
service_set_main_pid(s, pid); service_set_main_pid(s, pid);

View File

@ -180,7 +180,7 @@ int main(int argc, char *argv[]) {
umask(0022); umask(0022);
if (getpid() != 1) { if (getpid_cached() != 1) {
log_error("Not executed by init (PID 1)."); log_error("Not executed by init (PID 1).");
r = -EPERM; r = -EPERM;
goto error; goto error;

View File

@ -388,7 +388,7 @@ int main(int argc, char *argv[]) {
if (server_init(&server, (unsigned) n) < 0) if (server_init(&server, (unsigned) n) < 0)
return EXIT_FAILURE; 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, sd_notify(false,
"READY=1\n" "READY=1\n"
@ -415,7 +415,7 @@ int main(int argc, char *argv[]) {
r = EXIT_SUCCESS; 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: fail:
sd_notify(false, sd_notify(false,

View File

@ -86,7 +86,7 @@ static int spawn_child(const char* child, char** argv) {
if (pipe(fd) < 0) if (pipe(fd) < 0)
return log_error_errno(errno, "Failed to create pager pipe: %m"); return log_error_errno(errno, "Failed to create pager pipe: %m");
parent_pid = getpid(); parent_pid = getpid_cached();
child_pid = fork(); child_pid = fork();
if (child_pid < 0) { if (child_pid < 0) {
@ -1564,7 +1564,7 @@ int main(int argc, char **argv) {
log_debug("Watchdog is %sd.", enable_disable(r > 0)); log_debug("Watchdog is %sd.", enable_disable(r > 0));
log_debug("%s running as pid "PID_FMT, log_debug("%s running as pid "PID_FMT,
program_invocation_short_name, getpid()); program_invocation_short_name, getpid_cached());
sd_notify(false, sd_notify(false,
"READY=1\n" "READY=1\n"
"STATUS=Processing requests..."); "STATUS=Processing requests...");

View File

@ -811,7 +811,7 @@ int main(int argc, char **argv) {
goto cleanup; goto cleanup;
log_debug("%s running as pid "PID_FMT, log_debug("%s running as pid "PID_FMT,
program_invocation_short_name, getpid()); program_invocation_short_name, getpid_cached());
use_journal = optind >= argc; use_journal = optind >= argc;
if (use_journal) { if (use_journal) {

View File

@ -106,7 +106,7 @@ static bool is_us(const char *pid) {
if (parse_pid(pid, &t) < 0) if (parse_pid(pid, &t) < 0)
return false; return false;
return t == getpid(); return t == getpid_cached();
} }
static void dev_kmsg_record(Server *s, const char *p, size_t l) { static void dev_kmsg_record(Server *s, const char *p, size_t l) {

View File

@ -1095,7 +1095,7 @@ void server_driver_message(Server *s, const char *message_id, const char *format
/* Error handling below */ /* Error handling below */
va_end(ap); va_end(ap);
ucred.pid = getpid(); ucred.pid = getpid_cached();
ucred.uid = getuid(); ucred.uid = getuid();
ucred.gid = getgid(); ucred.gid = getgid();

View File

@ -99,7 +99,7 @@ static void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned
* let's fix it as good as we can, and retry */ * let's fix it as good as we can, and retry */
u = *ucred; u = *ucred;
u.pid = getpid(); u.pid = getpid_cached();
memcpy(CMSG_DATA(cmsg), &u, sizeof(struct ucred)); memcpy(CMSG_DATA(cmsg), &u, sizeof(struct ucred));
if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0) if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)

View File

@ -55,7 +55,7 @@ int main(int argc, char *argv[]) {
server_flush_to_var(&server, true); server_flush_to_var(&server, true);
server_flush_dev_kmsg(&server); 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, server_driver_message(&server,
"MESSAGE_ID=" SD_MESSAGE_JOURNAL_START_STR, "MESSAGE_ID=" SD_MESSAGE_JOURNAL_START_STR,
LOG_MESSAGE("Journal started"), LOG_MESSAGE("Journal started"),
@ -114,7 +114,7 @@ int main(int argc, char *argv[]) {
server_maybe_warn_forward_syslog_missed(&server); 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, server_driver_message(&server,
"MESSAGE_ID=" SD_MESSAGE_JOURNAL_STOP_STR, "MESSAGE_ID=" SD_MESSAGE_JOURNAL_STOP_STR,
LOG_MESSAGE("Journal stopped"), LOG_MESSAGE("Journal stopped"),

View File

@ -69,7 +69,7 @@ static bool journal_pid_changed(sd_journal *j) {
/* We don't support people creating a journal object and /* We don't support people creating a journal object and
* keeping it around over a fork(). Let's complain. */ * 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) { static int journal_put_error(sd_journal *j, int r, const char *path) {
@ -1715,7 +1715,7 @@ static sd_journal *journal_new(int flags, const char *path) {
if (!j) if (!j)
return NULL; return NULL;
j->original_pid = getpid(); j->original_pid = getpid_cached();
j->toplevel_fd = -1; j->toplevel_fd = -1;
j->inotify_fd = -1; j->inotify_fd = -1;
j->flags = flags; j->flags = flags;

View File

@ -170,7 +170,7 @@ int main(int argc, char *argv[]) {
if (argc == 3) if (argc == 3)
(void) safe_atozu(argv[2], &arg_start); (void) safe_atozu(argv[2], &arg_start);
else else
arg_start = getpid(); arg_start = getpid_cached();
NULSTR_FOREACH(i, "zeros\0simple\0random\0") { NULSTR_FOREACH(i, "zeros\0simple\0random\0") {
#ifdef HAVE_XZ #ifdef HAVE_XZ

View File

@ -165,7 +165,7 @@ _public_ int sd_bus_creds_new_from_pid(sd_bus_creds **ret, pid_t pid, uint64_t m
assert_return(ret, -EINVAL); assert_return(ret, -EINVAL);
if (pid == 0) if (pid == 0)
pid = getpid(); pid = getpid_cached();
c = bus_creds_new(); c = bus_creds_new();
if (!c) if (!c)

View File

@ -187,7 +187,7 @@ _public_ int sd_bus_new(sd_bus **ret) {
r->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME; r->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
r->hello_flags |= KDBUS_HELLO_ACCEPT_FD; r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
r->attach_flags |= KDBUS_ATTACH_NAMES; 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); assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);
@ -3131,7 +3131,7 @@ bool bus_pid_changed(sd_bus *bus) {
/* We don't support people creating a bus connection and /* We don't support people creating a bus connection and
* keeping it around over a fork(). Let's complain. */ * 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) { static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {

View File

@ -276,7 +276,7 @@ int main(int argc, char *argv[]) {
assert_se(arg_loop_usec > 0); assert_se(arg_loop_usec > 0);
if (type == TYPE_KDBUS) { 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); bus_ref = bus_kernel_create_bus(name, false, &bus_name);
if (bus_ref == -ENOENT) if (bus_ref == -ENOENT)

View File

@ -49,7 +49,7 @@ static void test_one(
sd_bus *a, *b; sd_bus *a, *b;
int r, found = 0; 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); bus_ref = bus_kernel_create_bus(name, false, &bus_name);
if (bus_ref == -ENOENT) if (bus_ref == -ENOENT)

View File

@ -41,7 +41,7 @@ int main(int argc, char *argv[]) {
log_set_max_level(LOG_DEBUG); 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); bus_ref = bus_kernel_create_bus(name, false, &bus_name);
if (bus_ref == -ENOENT) if (bus_ref == -ENOENT)

View File

@ -52,7 +52,7 @@ int main(int argc, char *argv[]) {
log_set_max_level(LOG_DEBUG); 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); bus_ref = bus_kernel_create_bus(name, false, &bus_name);
if (bus_ref == -ENOENT) if (bus_ref == -ENOENT)

View File

@ -70,7 +70,7 @@ _public_ int sd_listen_fds(int unset_environment) {
goto finish; goto finish;
/* Is this for us? */ /* Is this for us? */
if (getpid() != pid) { if (getpid_cached() != pid) {
r = 0; r = 0;
goto finish; goto finish;
} }
@ -518,7 +518,7 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
msghdr.msg_namelen = SOCKADDR_UN_LEN(sockaddr.un); 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) { if (n_fds > 0 || have_pid) {
/* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */ /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
@ -659,7 +659,7 @@ _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
goto finish; goto finish;
/* Is this for us? */ /* Is this for us? */
if (getpid() != pid) { if (getpid_cached() != pid) {
r = 0; r = 0;
goto finish; goto finish;
} }

View File

@ -436,7 +436,7 @@ _public_ int sd_event_new(sd_event** ret) {
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->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.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->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; e->perturb = USEC_INFINITY;
r = prioq_ensure_allocated(&e->pending, pending_prioq_compare); r = prioq_ensure_allocated(&e->pending, pending_prioq_compare);
@ -493,7 +493,7 @@ static bool event_pid_changed(sd_event *e) {
/* We don't support people creating an event loop and keeping /* We don't support people creating an event loop and keeping
* it around over a fork(). Let's complain. */ * 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) { static void source_io_unregister(sd_event_source *s) {

View File

@ -317,11 +317,11 @@ static void test_rtqueue(void) {
assert_se(sd_event_source_set_priority(v, -10) >= 0); assert_se(sd_event_source_set_priority(v, -10) >= 0);
assert(sigqueue(getpid(), SIGRTMIN+2, (union sigval) { .sival_int = 1 }) >= 0); assert(sigqueue(getpid_cached(), SIGRTMIN+2, (union sigval) { .sival_int = 1 }) >= 0);
assert(sigqueue(getpid(), SIGRTMIN+3, (union sigval) { .sival_int = 2 }) >= 0); assert(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 2 }) >= 0);
assert(sigqueue(getpid(), SIGUSR2, (union sigval) { .sival_int = 3 }) >= 0); assert(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 3 }) >= 0);
assert(sigqueue(getpid(), SIGRTMIN+3, (union sigval) { .sival_int = 4 }) >= 0); assert(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 4 }) >= 0);
assert(sigqueue(getpid(), SIGUSR2, (union sigval) { .sival_int = 5 }) >= 0); assert(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 5 }) >= 0);
assert_se(n_rtqueue == 0); assert_se(n_rtqueue == 0);
assert_se(last_rtqueue_sigval == 0); assert_se(last_rtqueue_sigval == 0);

View File

@ -44,7 +44,7 @@ static int sd_netlink_new(sd_netlink **ret) {
rtnl->n_ref = REFCNT_INIT; rtnl->n_ref = REFCNT_INIT;
rtnl->fd = -1; rtnl->fd = -1;
rtnl->sockaddr.nl.nl_family = AF_NETLINK; rtnl->sockaddr.nl.nl_family = AF_NETLINK;
rtnl->original_pid = getpid(); rtnl->original_pid = getpid_cached();
LIST_HEAD_INIT(rtnl->match_callbacks); LIST_HEAD_INIT(rtnl->match_callbacks);
@ -99,7 +99,7 @@ static bool rtnl_pid_changed(sd_netlink *rtnl) {
/* We don't support people creating an rtnl connection and /* We don't support people creating an rtnl connection and
* keeping it around over a fork(). Let's complain. */ * 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) { int sd_netlink_open_fd(sd_netlink **ret, int fd) {

View File

@ -459,7 +459,7 @@ static bool resolve_pid_changed(sd_resolve *r) {
/* We don't support people creating a resolver and keeping it /* We don't support people creating a resolver and keeping it
* around after fork(). Let's complain. */ * 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) { _public_ int sd_resolve_new(sd_resolve **ret) {
@ -473,7 +473,7 @@ _public_ int sd_resolve_new(sd_resolve **ret) {
return -ENOMEM; return -ENOMEM;
resolve->n_ref = 1; resolve->n_ref = 1;
resolve->original_pid = getpid(); resolve->original_pid = getpid_cached();
for (i = 0; i < _FD_MAX; i++) for (i = 0; i < _FD_MAX; i++)
resolve->fds[i] = -1; resolve->fds[i] = -1;

View File

@ -1254,7 +1254,7 @@ int main(int argc, char *argv[]) {
goto finish; 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, sd_notify(false,
"READY=1\n" "READY=1\n"
@ -1262,7 +1262,7 @@ int main(int argc, char *argv[]) {
r = manager_run(m); 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: finish:
sd_notify(false, sd_notify(false,

View File

@ -41,6 +41,7 @@
#include "login-util.h" #include "login-util.h"
#include "macro.h" #include "macro.h"
#include "parse-util.h" #include "parse-util.h"
#include "process-util.h"
#include "socket-util.h" #include "socket-util.h"
#include "strv.h" #include "strv.h"
#include "terminal-util.h" #include "terminal-util.h"
@ -372,7 +373,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
if (debug) if (debug)
pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: " 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", "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), strempty(service),
type, class, strempty(desktop), type, class, strempty(desktop),
strempty(seat), vtnr, strempty(tty), strempty(display), strempty(seat), vtnr, strempty(tty), strempty(display),
@ -387,7 +388,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
&reply, &reply,
"uusssssussbssa(sv)", "uusssssussbssa(sv)",
(uint32_t) pw->pw_uid, (uint32_t) pw->pw_uid,
(uint32_t) getpid(), (uint32_t) getpid_cached(),
service, service,
type, type,
class, class,

View File

@ -34,6 +34,7 @@
#include "label.h" #include "label.h"
#include "machine-image.h" #include "machine-image.h"
#include "machined.h" #include "machined.h"
#include "process-util.h"
#include "signal-util.h" #include "signal-util.h"
Manager *manager_new(void) { Manager *manager_new(void) {
@ -398,7 +399,7 @@ int main(int argc, char *argv[]) {
goto finish; 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, sd_notify(false,
"READY=1\n" "READY=1\n"
@ -406,7 +407,7 @@ int main(int argc, char *argv[]) {
r = manager_run(m); 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: finish:
manager_free(m); manager_free(m);

View File

@ -655,7 +655,7 @@ static int transient_scope_set_properties(sd_bus_message *m) {
if (r < 0) if (r < 0)
return r; 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) if (r < 0)
return r; return r;

View File

@ -49,6 +49,7 @@
#include "macro.h" #include "macro.h"
#include "missing.h" #include "missing.h"
#include "mkdir.h" #include "mkdir.h"
#include "process-util.h"
#include "random-util.h" #include "random-util.h"
#include "signal-util.h" #include "signal-util.h"
#include "socket-util.h" #include "socket-util.h"
@ -519,7 +520,7 @@ int ask_password_agent(
"AcceptCached=%i\n" "AcceptCached=%i\n"
"Echo=%i\n" "Echo=%i\n"
"NotAfter="USEC_FMT"\n", "NotAfter="USEC_FMT"\n",
getpid(), getpid_cached(),
socket_name, socket_name,
(flags & ASK_PASSWORD_ACCEPT_CACHED) ? 1 : 0, (flags & ASK_PASSWORD_ACCEPT_CACHED) ? 1 : 0,
(flags & ASK_PASSWORD_ECHO) ? 1 : 0, (flags & ASK_PASSWORD_ECHO) ? 1 : 0,

View File

@ -48,6 +48,7 @@
#include "parse-util.h" #include "parse-util.h"
#include "path-util.h" #include "path-util.h"
#include "proc-cmdline.h" #include "proc-cmdline.h"
#include "process-util.h"
#include "selinux-util.h" #include "selinux-util.h"
#include "smack-util.h" #include "smack-util.h"
#include "stat-util.h" #include "stat-util.h"
@ -164,7 +165,7 @@ static int condition_test_user(Condition *c) {
if (streq(username, c->parameter)) if (streq(username, c->parameter))
return 1; return 1;
if (getpid() == 1) if (getpid_cached() == 1)
return streq(c->parameter, "root"); return streq(c->parameter, "root");
u = c->parameter; u = c->parameter;
@ -188,7 +189,7 @@ static int condition_test_group(Condition *c) {
return in_gid(id); return in_gid(id);
/* Avoid any NSS lookups if we are PID1 */ /* Avoid any NSS lookups if we are PID1 */
if (getpid() == 1) if (getpid_cached() == 1)
return streq(c->parameter, "root"); return streq(c->parameter, "root");
return in_group(c->parameter) > 0; return in_group(c->parameter) > 0;

View File

@ -87,7 +87,7 @@ int pager_open(bool no_pager, bool jump_to_end) {
if (pipe(fd) < 0) if (pipe(fd) < 0)
return log_error_errno(errno, "Failed to create pager pipe: %m"); return log_error_errno(errno, "Failed to create pager pipe: %m");
parent_pid = getpid(); parent_pid = getpid_cached();
pager_pid = fork(); pager_pid = fork();
if (pager_pid < 0) if (pager_pid < 0)

View File

@ -35,19 +35,19 @@ int main(int argc, char*argv[]) {
assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, "/test-b/test-c") == 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_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")); assert_se(streq(path, "/test-b"));
free(path); free(path);
assert_se(cg_attach(SYSTEMD_CGROUP_CONTROLLER, "/test-a", 0) == 0); 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")); assert_se(path_equal(path, "/test-a"));
free(path); free(path);
assert_se(cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, "/test-b/test-d", 0) == 0); 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")); assert_se(path_equal(path, "/test-b/test-d"));
free(path); free(path);

View File

@ -39,7 +39,7 @@ int main(int argc, char* argv[]) {
log_open(); log_open();
log_struct(LOG_INFO, log_struct(LOG_INFO,
"MESSAGE=Waldo PID="PID_FMT, getpid(), "MESSAGE=Waldo PID="PID_FMT, getpid_cached(),
"SERVICE=piepapo", "SERVICE=piepapo",
NULL); NULL);
@ -47,12 +47,12 @@ int main(int argc, char* argv[]) {
log_open(); log_open();
log_struct(LOG_INFO, log_struct(LOG_INFO,
"MESSAGE=Foobar PID="PID_FMT, getpid(), "MESSAGE=Foobar PID="PID_FMT, getpid_cached(),
"SERVICE=foobar", "SERVICE=foobar",
NULL); NULL);
log_struct(LOG_INFO, 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", "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, (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", "SUFFIX=GOT IT",

View File

@ -115,7 +115,7 @@ static void test_pid_is_unwaited(void) {
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
assert_se(!pid_is_unwaited(pid)); 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)); assert_se(!pid_is_unwaited(-1));
} }
@ -132,7 +132,7 @@ static void test_pid_is_alive(void) {
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
assert_se(!pid_is_alive(pid)); 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(!pid_is_alive(-1));
} }
@ -205,149 +205,149 @@ static void test_get_process_cmdline_harder(void) {
assert_se(prctl(PR_SET_NAME, "testa") >= 0); 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]")); assert_se(streq(line, "[testa]"));
line = mfree(line); 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, "")); assert_se(streq(line, ""));
line = mfree(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, "[")); assert_se(streq(line, "["));
line = mfree(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, "[.")); assert_se(streq(line, "[."));
line = mfree(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, "[..")); assert_se(streq(line, "[.."));
line = mfree(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, "[...")); assert_se(streq(line, "[..."));
line = mfree(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, "[...]")); assert_se(streq(line, "[...]"));
line = mfree(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...]")); assert_se(streq(line, "[t...]"));
line = mfree(line); 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]")); assert_se(streq(line, "[testa]"));
line = mfree(line); line = mfree(line);
assert_se(write(fd, "\0\0\0\0\0\0\0\0\0", 10) == 10); 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]")); assert_se(streq(line, "[testa]"));
line = mfree(line); line = mfree(line);
assert_se(write(fd, "foo\0bar\0\0\0\0\0", 10) == 10); 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")); assert_se(streq(line, "foo bar"));
line = mfree(line); 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")); assert_se(streq(line, "foo bar"));
line = mfree(line); line = mfree(line);
assert_se(write(fd, "quux", 4) == 4); 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")); assert_se(streq(line, "foo bar quux"));
line = mfree(line); 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")); assert_se(streq(line, "foo bar quux"));
line = mfree(line); 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, "")); assert_se(streq(line, ""));
line = mfree(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, ".")); assert_se(streq(line, "."));
line = mfree(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, "..")); assert_se(streq(line, ".."));
line = mfree(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, "...")); assert_se(streq(line, "..."));
line = mfree(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...")); assert_se(streq(line, "f..."));
line = mfree(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, "fo...")); assert_se(streq(line, "fo..."));
line = mfree(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, "foo...")); assert_se(streq(line, "foo..."));
line = mfree(line); 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...")); assert_se(streq(line, "foo..."));
line = mfree(line); 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...")); assert_se(streq(line, "foo b..."));
line = mfree(line); 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...")); assert_se(streq(line, "foo ba..."));
line = mfree(line); 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...")); assert_se(streq(line, "foo bar..."));
line = mfree(line); 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...")); assert_se(streq(line, "foo bar..."));
line = mfree(line); 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")); assert_se(streq(line, "foo bar quux"));
line = mfree(line); 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")); assert_se(streq(line, "foo bar quux"));
line = mfree(line); 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")); assert_se(streq(line, "foo bar quux"));
line = mfree(line); line = mfree(line);
assert_se(ftruncate(fd, 0) >= 0); assert_se(ftruncate(fd, 0) >= 0);
assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 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]")); assert_se(streq(line, "[aaaa bbbb cccc]"));
line = mfree(line); 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...]")); assert_se(streq(line, "[aaaa...]"));
line = mfree(line); 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...]")); assert_se(streq(line, "[aaaa...]"));
line = mfree(line); 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...]")); assert_se(streq(line, "[aaaa b...]"));
line = mfree(line); line = mfree(line);

View File

@ -50,12 +50,12 @@ static void test_block_signals(void) {
static void test_ignore_signals(void) { static void test_ignore_signals(void) {
assert_se(ignore_signals(SIGINT, -1) >= 0); 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(ignore_signals(SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
assert_se(kill(getpid(), SIGUSR1) >= 0); assert_se(kill(getpid_cached(), SIGUSR1) >= 0);
assert_se(kill(getpid(), SIGUSR2) >= 0); assert_se(kill(getpid_cached(), SIGUSR2) >= 0);
assert_se(kill(getpid(), SIGTERM) >= 0); assert_se(kill(getpid_cached(), SIGTERM) >= 0);
assert_se(kill(getpid(), SIGPIPE) >= 0); assert_se(kill(getpid_cached(), SIGPIPE) >= 0);
assert_se(default_signals(SIGINT, SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0); assert_se(default_signals(SIGINT, SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
} }

View File

@ -45,7 +45,7 @@ int main(int argc, char** argv) {
fd = open_tmpfile_unlinkable(p, O_RDWR|O_CLOEXEC); fd = open_tmpfile_unlinkable(p, O_RDWR|O_CLOEXEC);
assert_se(fd >= 0); 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); (void) system(cmd);
assert_se(readlink_malloc(cmd + 6, &ans) >= 0); assert_se(readlink_malloc(cmd + 6, &ans) >= 0);
log_debug("link1: %s", ans); log_debug("link1: %s", ans);
@ -55,7 +55,7 @@ int main(int argc, char** argv) {
assert_se(fd >= 0); assert_se(fd >= 0);
assert_se(unlink(pattern) == 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); (void) system(cmd2);
assert_se(readlink_malloc(cmd2 + 6, &ans2) >= 0); assert_se(readlink_malloc(cmd2 + 6, &ans2) >= 0);
log_debug("link2: %s", ans2); log_debug("link2: %s", ans2);

View File

@ -25,6 +25,7 @@
#include "fd-util.h" #include "fd-util.h"
#include "fs-util.h" #include "fs-util.h"
#include "network-util.h" #include "network-util.h"
#include "process-util.h"
#include "signal-util.h" #include "signal-util.h"
#include "timesyncd-conf.h" #include "timesyncd-conf.h"
#include "timesyncd-manager.h" #include "timesyncd-manager.h"
@ -138,7 +139,7 @@ int main(int argc, char *argv[]) {
goto finish; 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, sd_notify(false,
"READY=1\n" "READY=1\n"
"STATUS=Daemon is running"); "STATUS=Daemon is running");

View File

@ -174,7 +174,7 @@ static void event_free(struct event *event) {
if (udev_list_node_is_empty(&event->manager->events)) { if (udev_list_node_is_empty(&event->manager->events)) {
/* only clean up the queue from the process that created it */ /* 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"); r = unlink("/run/udev/queue");
if (r < 0) if (r < 0)
log_warning_errno(errno, "could not unlink /run/udev/queue: %m"); log_warning_errno(errno, "could not unlink /run/udev/queue: %m");
@ -593,9 +593,9 @@ static int event_queue_insert(Manager *manager, struct udev_device *dev) {
/* only one process can add events to the queue */ /* only one process can add events to the queue */
if (manager->pid == 0) 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); event = new0(struct event, 1);
if (!event) if (!event)

View File

@ -33,6 +33,7 @@
#include "format-util.h" #include "format-util.h"
#include "log.h" #include "log.h"
#include "macro.h" #include "macro.h"
#include "process-util.h"
#include "special.h" #include "special.h"
#include "strv.h" #include "strv.h"
#include "unit-name.h" #include "unit-name.h"
@ -258,7 +259,7 @@ int main(int argc, char *argv[]) {
goto finish; 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")) if (streq(argv[1], "reboot"))
r = on_reboot(&c); r = on_reboot(&c);
@ -271,7 +272,7 @@ int main(int argc, char *argv[]) {
r = -EINVAL; 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: finish:
#ifdef HAVE_AUDIT #ifdef HAVE_AUDIT