commit 0864d311766498563331f486909a0d950ba7de87
Author: Andrew Soutar <andrew@andrewsoutar.com>
Date:   Mon Jul 31 02:19:16 2017 -0400

    cryptsetup: fix infinite timeout (#6486)
    
    0004f698d causes `arg_timeout` to be infinity instead of 0 when timeout=0. The
    logic here now matches this change.
    
    Fixes #6381

diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c
index 3b4c08616..08ed7e53b 100644
--- a/src/cryptsetup/cryptsetup.c
+++ b/src/cryptsetup/cryptsetup.c
@@ -56,7 +56,7 @@ static bool arg_tcrypt_veracrypt = false;
 static char **arg_tcrypt_keyfiles = NULL;
 static uint64_t arg_offset = 0;
 static uint64_t arg_skip = 0;
-static usec_t arg_timeout = 0;
+static usec_t arg_timeout = USEC_INFINITY;
 
 /* Options Debian's crypttab knows we don't:
 
@@ -670,10 +670,10 @@ int main(int argc, char *argv[]) {
                 if (arg_discards)
                         flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
 
-                if (arg_timeout > 0)
-                        until = now(CLOCK_MONOTONIC) + arg_timeout;
-                else
+                if (arg_timeout == USEC_INFINITY)
                         until = 0;
+                else
+                        until = now(CLOCK_MONOTONIC) + arg_timeout;
 
                 arg_key_size = (arg_key_size > 0 ? arg_key_size : (256 / 8));
 
commit 5c30a6d2b805ae9b5dd0ad003b9ee86b8965bc47
Author: Lennart Poettering <lennart@poettering.net>
Date:   Thu Jul 20 15:46:05 2017 +0200

    process-util: add getpid_cached() as a caching wrapper for getpid()
    
    Let's make getpid() fast again.

diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index b80cacaa4..98adee6d9 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -922,6 +922,68 @@ int ioprio_parse_priority(const char *s, int *ret) {
         return 0;
 }
 
+/* The cached PID, possible values:
+ *
+ *     == UNSET [0]  → cache not initialized yet
+ *     == BUSY [-1]  → some thread is initializing it at the moment
+ *     any other     → the cached PID
+ */
+
+#define CACHED_PID_UNSET ((pid_t) 0)
+#define CACHED_PID_BUSY ((pid_t) -1)
+
+static pid_t cached_pid = CACHED_PID_UNSET;
+
+static void reset_cached_pid(void) {
+        /* Invoked in the child after a fork(), i.e. at the first moment the PID changed */
+        cached_pid = CACHED_PID_UNSET;
+}
+
+/* We use glibc __register_atfork() + __dso_handle directly here, as they are not included in the glibc
+ * headers. __register_atfork() is mostly equivalent to pthread_atfork(), but doesn't require us to link against
+ * libpthread, as it is part of glibc anyway. */
+extern int __register_atfork(void (*prepare) (void), void (*parent) (void), void (*child) (void), void * __dso_handle);
+extern void* __dso_handle __attribute__ ((__weak__));
+
+pid_t getpid_cached(void) {
+        pid_t current_value;
+
+        /* getpid_cached() is much like getpid(), but caches the value in local memory, to avoid having to invoke a
+         * system call each time. This restores glibc behaviour from before 2.24, when getpid() was unconditionally
+         * cached. Starting with 2.24 getpid() started to become prohibitively expensive when used for detecting when
+         * objects were used across fork()s. With this caching the old behaviour is somewhat restored.
+         *
+         * https://bugzilla.redhat.com/show_bug.cgi?id=1443976
+         * https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=1d2bc2eae969543b89850e35e532f3144122d80a
+         */
+
+        current_value = __sync_val_compare_and_swap(&cached_pid, CACHED_PID_UNSET, CACHED_PID_BUSY);
+
+        switch (current_value) {
+
+        case CACHED_PID_UNSET: { /* Not initialized yet, then do so now */
+                pid_t new_pid;
+
+                new_pid = getpid();
+
+                if (__register_atfork(NULL, NULL, reset_cached_pid, __dso_handle) != 0) {
+                        /* OOM? Let's try again later */
+                        cached_pid = CACHED_PID_UNSET;
+                        return new_pid;
+                }
+
+                cached_pid = new_pid;
+                return new_pid;
+        }
+
+        case CACHED_PID_BUSY: /* Somebody else is currently initializing */
+                return getpid();
+
+        default: /* Properly initialized */
+                return current_value;
+        }
+}
+
 static const char *const ioprio_class_table[] = {
         [IOPRIO_CLASS_NONE] = "none",
         [IOPRIO_CLASS_RT] = "realtime",
diff --git a/src/basic/process-util.h b/src/basic/process-util.h
index 28d8d7499..17746b4eb 100644
--- a/src/basic/process-util.h
+++ b/src/basic/process-util.h
@@ -119,3 +119,5 @@ static inline bool ioprio_priority_is_valid(int i) {
 }
 
 int ioprio_parse_priority(const char *s, int *ret);
+
+pid_t getpid_cached(void);
diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c
index c5edbcc5d..f78a8c0b5 100644
--- a/src/test/test-process-util.c
+++ b/src/test/test-process-util.c
@@ -410,6 +410,61 @@ static void test_rename_process(void) {
         test_rename_process_one("1234567", 1); /* should always fit */
 }
 
+static void test_getpid_cached(void) {
+        siginfo_t si;
+        pid_t a, b, c, d, e, f, child;
+
+        a = raw_getpid();
+        b = getpid_cached();
+        c = getpid();
+
+        assert_se(a == b && a == c);
+
+        child = fork();
+        assert_se(child >= 0);
+
+        if (child == 0) {
+                /* In child */
+                a = raw_getpid();
+                b = getpid_cached();
+                c = getpid();
+
+                assert_se(a == b && a == c);
+                _exit(0);
+        }
+
+        d = raw_getpid();
+        e = getpid_cached();
+        f = getpid();
+
+        assert_se(a == d && a == e && a == f);
+
+        assert_se(wait_for_terminate(child, &si) >= 0);
+        assert_se(si.si_status == 0);
+        assert_se(si.si_code == CLD_EXITED);
+}
+
+#define MEASURE_ITERATIONS (10000000LLU)
+
+static void test_getpid_measure(void) {
+        unsigned long long i;
+        usec_t t, q;
+
+        t = now(CLOCK_MONOTONIC);
+        for (i = 0; i < MEASURE_ITERATIONS; i++)
+                (void) getpid();
+        q = now(CLOCK_MONOTONIC) - t;
+
+        log_info(" glibc getpid(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
+
+        t = now(CLOCK_MONOTONIC);
+        for (i = 0; i < MEASURE_ITERATIONS; i++)
+                (void) getpid_cached();
+        q = now(CLOCK_MONOTONIC) - t;
+
+        log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
+}
+
 int main(int argc, char *argv[]) {
 
         log_set_max_level(LOG_DEBUG);
@@ -434,6 +489,8 @@ int main(int argc, char *argv[]) {
         test_personality();
         test_get_process_cmdline_harder();
         test_rename_process();
+        test_getpid_cached();
+        test_getpid_measure();
 
         return 0;
 }
commit df0ff127758809a45105893772de76082d12a26d
Author: Lennart Poettering <lennart@poettering.net>
Date:   Thu Jul 20 16:19:18 2017 +0200

    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.

diff --git a/src/activate/activate.c b/src/activate/activate.c
index 6ebd82041..4b82dca49 100644
--- a/src/activate/activate.c
+++ b/src/activate/activate.c
@@ -31,6 +31,7 @@
 #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"
@@ -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)
                         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) {
@@ -271,7 +272,7 @@ static int fork_and_exec_process(const char* child, char** argv, char **env, int
         if (!joined)
                 return log_oom();
 
-        parent_pid = getpid();
+        parent_pid = getpid_cached();
 
         child_pid = fork();
         if (child_pid < 0)
diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c
index 634437261..5dea07897 100644
--- a/src/basic/cgroup-util.c
+++ b/src/basic/cgroup-util.c
@@ -255,7 +255,7 @@ int cg_kill(
                         return -ENOMEM;
         }
 
-        my_pid = getpid();
+        my_pid = getpid_cached();
 
         do {
                 _cleanup_fclose_ FILE *f = NULL;
@@ -399,7 +399,7 @@ int cg_migrate(
         if (!s)
                 return -ENOMEM;
 
-        my_pid = getpid();
+        my_pid = getpid_cached();
 
         do {
                 _cleanup_fclose_ FILE *f = NULL;
@@ -825,7 +825,7 @@ int cg_attach(const char *controller, const char *path, pid_t pid) {
                 return r;
 
         if (pid == 0)
-                pid = getpid();
+                pid = getpid_cached();
 
         xsprintf(c, PID_FMT "\n", pid);
 
diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c
index 19ad20789..9d61044c8 100644
--- a/src/basic/fd-util.c
+++ b/src/basic/fd-util.c
@@ -31,6 +31,7 @@
 #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"
@@ -282,7 +283,7 @@ int same_fd(int a, int b) {
                 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;
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index 9a185e3e6..0678db4a7 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -41,6 +41,7 @@
 #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"
@@ -1399,7 +1400,7 @@ int open_serialization_fd(const char *ident) {
         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;
diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c
index 8fe19ee4e..5e1163c6a 100644
--- a/src/basic/fs-util.c
+++ b/src/basic/fs-util.c
@@ -307,7 +307,7 @@ int fd_warn_permissions(const char *path, int 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;
diff --git a/src/basic/log.c b/src/basic/log.c
index 3fd53800a..d0caeb5ca 100644
--- a/src/basic/log.c
+++ b/src/basic/log.c
@@ -84,7 +84,7 @@ void log_close_console(void) {
         if (console_fd < 0)
                 return;
 
-        if (getpid() == 1) {
+        if (getpid_cached() == 1) {
                 if (console_fd >= 3)
                         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
         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);
@@ -248,7 +248,7 @@ int log_open(void) {
         }
 
         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,
@@ -370,7 +370,7 @@ static int write_to_console(
 
         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),
@@ -423,7 +423,7 @@ static int write_to_syslog(
         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);
@@ -468,7 +468,7 @@ static int write_to_kmsg(
                 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);
@@ -1189,7 +1189,7 @@ int log_syntax_internal(
         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),
diff --git a/src/basic/log.h b/src/basic/log.h
index ff5d776b1..186747ff8 100644
--- a/src/basic/log.h
+++ b/src/basic/log.h
@@ -30,6 +30,7 @@
 #include "sd-id128.h"
 
 #include "macro.h"
+#include "process-util.h"
 
 typedef enum LogRealm {
         LOG_REALM_SYSTEMD,
@@ -247,7 +248,7 @@ void log_assert_failed_return_realm(
 #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__)
@@ -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_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__)
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index 98adee6d9..ce6a59c8a 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -814,7 +814,7 @@ bool is_main_thread(void) {
         static thread_local int cached = 0;
 
         if (_unlikely_(cached == 0))
-                cached = getpid() == gettid() ? 1 : -1;
+                cached = getpid_cached() == gettid() ? 1 : -1;
 
         return cached > 0;
 }
@@ -878,7 +878,7 @@ const char* personality_to_string(unsigned long p) {
 
 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)
diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c
index 9a8ef825c..8a0419df7 100644
--- a/src/basic/terminal-util.c
+++ b/src/basic/terminal-util.c
@@ -1220,7 +1220,7 @@ bool colors_enabled(void) {
                 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
diff --git a/src/basic/util.c b/src/basic/util.c
index b52a5db31..2f45128ed 100644
--- a/src/basic/util.c
+++ b/src/basic/util.c
@@ -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
          * 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
diff --git a/src/basic/virt.c b/src/basic/virt.c
index 5143ac665..d8eeb54db 100644
--- a/src/basic/virt.c
+++ b/src/basic/virt.c
@@ -422,7 +422,7 @@ int detect_container(void) {
                 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");
diff --git a/src/core/automount.c b/src/core/automount.c
index 0f72854ce..96de933e2 100644
--- a/src/core/automount.c
+++ b/src/core/automount.c
@@ -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(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;
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
index 71f307fb6..fc8b9565a 100644
--- a/src/core/cgroup.c
+++ b/src/core/cgroup.c
@@ -1593,7 +1593,7 @@ int unit_search_main_pid(Unit *u, pid_t *ret) {
         if (r < 0)
                 return r;
 
-        mypid = getpid();
+        mypid = getpid_cached();
         while (cg_read_pid(f, &npid) > 0)  {
                 pid_t ppid;
 
diff --git a/src/core/dbus.c b/src/core/dbus.c
index cfc045d28..96bf975f8 100644
--- a/src/core/dbus.c
+++ b/src/core/dbus.c
@@ -967,7 +967,7 @@ static int bus_init_private(Manager *m) {
         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");
diff --git a/src/core/execute.c b/src/core/execute.c
index 3efd56fef..48b84815c 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -1099,7 +1099,7 @@ static int setup_pam(
 
         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) {
@@ -1506,7 +1506,7 @@ static int build_environment(
         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;
 
@@ -1525,7 +1525,7 @@ static int build_environment(
         }
 
         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;
 
@@ -2564,7 +2564,7 @@ static int exec_child(
                 }
 
         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 :
diff --git a/src/core/main.c b/src/core/main.c
index babcab497..5d0f385d7 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -154,7 +154,7 @@ noreturn static void crash(int sig) {
         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)
@@ -860,7 +860,7 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 1);
         assert(argv);
 
-        if (getpid() == 1)
+        if (getpid_cached() == 1)
                 opterr = 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. */
 
                 case '?':
-                        if (getpid() != 1)
+                        if (getpid_cached() != 1)
                                 return -EINVAL;
                         else
                                 return 0;
@@ -1075,7 +1075,7 @@ static int parse_argv(int argc, char *argv[]) {
                         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 */
 
@@ -1389,7 +1389,7 @@ int main(int argc, char *argv[]) {
         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. */
 
@@ -1425,7 +1425,7 @@ int main(int argc, char *argv[]) {
 
         log_set_upgrade_syslog_to_journal(true);
 
-        if (getpid() == 1) {
+        if (getpid_cached() == 1) {
                 /* Disable the umask logic */
                 umask(0);
 
@@ -1436,7 +1436,7 @@ int main(int argc, char *argv[]) {
                 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;
@@ -1521,7 +1521,7 @@ int main(int argc, char *argv[]) {
                  * 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);
@@ -1545,7 +1545,7 @@ int main(int argc, char *argv[]) {
                 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)
@@ -1580,7 +1580,7 @@ int main(int argc, char *argv[]) {
 
         /* 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)
@@ -1706,7 +1706,7 @@ int main(int argc, char *argv[]) {
                  * tty. */
                 release_terminal();
 
-                if (getpid() == 1 && !skip_setup)
+                if (getpid_cached() == 1 && !skip_setup)
                         console_setup();
         }
 
@@ -1718,7 +1718,7 @@ int main(int argc, char *argv[]) {
 
         /* 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);
@@ -2152,7 +2152,7 @@ finish:
          * 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
 
@@ -2228,10 +2228,10 @@ finish:
 
                 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,
diff --git a/src/core/service.c b/src/core/service.c
index 1a9044d2d..2909aacf8 100644
--- a/src/core/service.c
+++ b/src/core/service.c
@@ -157,7 +157,7 @@ static int service_set_main_pid(Service *s, pid_t pid) {
         if (pid <= 1)
                 return -EINVAL;
 
-        if (pid == getpid())
+        if (pid == getpid_cached())
                 return -EINVAL;
 
         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_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
@@ -1283,7 +1283,7 @@ static int service_spawn(
                         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) {
@@ -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);
                 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);
diff --git a/src/core/shutdown.c b/src/core/shutdown.c
index a7d5e5793..6d1cc3120 100644
--- a/src/core/shutdown.c
+++ b/src/core/shutdown.c
@@ -180,7 +180,7 @@ int main(int argc, char *argv[]) {
 
         umask(0022);
 
-        if (getpid() != 1) {
+        if (getpid_cached() != 1) {
                 log_error("Not executed by init (PID 1).");
                 r = -EPERM;
                 goto error;
diff --git a/src/initctl/initctl.c b/src/initctl/initctl.c
index 6aeb5ad61..deb3f1b2a 100644
--- a/src/initctl/initctl.c
+++ b/src/initctl/initctl.c
@@ -388,7 +388,7 @@ int main(int argc, char *argv[]) {
         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"
@@ -415,7 +415,7 @@ int main(int argc, char *argv[]) {
 
         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,
diff --git a/src/journal-remote/journal-remote.c b/src/journal-remote/journal-remote.c
index 36c1a32dc..810206c62 100644
--- a/src/journal-remote/journal-remote.c
+++ b/src/journal-remote/journal-remote.c
@@ -86,7 +86,7 @@ static int spawn_child(const char* child, char** argv) {
         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) {
@@ -1564,7 +1564,7 @@ int main(int argc, char **argv) {
                 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...");
diff --git a/src/journal-remote/journal-upload.c b/src/journal-remote/journal-upload.c
index e0858dda7..ea264989a 100644
--- a/src/journal-remote/journal-upload.c
+++ b/src/journal-remote/journal-upload.c
@@ -811,7 +811,7 @@ int main(int argc, char **argv) {
                 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) {
diff --git a/src/journal/journald-kmsg.c b/src/journal/journald-kmsg.c
index 70ea56206..7fea85a5d 100644
--- a/src/journal/journald-kmsg.c
+++ b/src/journal/journald-kmsg.c
@@ -106,7 +106,7 @@ static bool is_us(const char *pid) {
         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) {
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
index 6eb19e886..f391845ba 100644
--- a/src/journal/journald-server.c
+++ b/src/journal/journald-server.c
@@ -1095,7 +1095,7 @@ void server_driver_message(Server *s, const char *message_id, const char *format
         /* Error handling below */
         va_end(ap);
 
-        ucred.pid = getpid();
+        ucred.pid = getpid_cached();
         ucred.uid = getuid();
         ucred.gid = getgid();
 
diff --git a/src/journal/journald-syslog.c b/src/journal/journald-syslog.c
index 8e034c8fa..17f855e96 100644
--- a/src/journal/journald-syslog.c
+++ b/src/journal/journald-syslog.c
@@ -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 */
 
                 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)
diff --git a/src/journal/journald.c b/src/journal/journald.c
index 1aaef387b..4dee764fd 100644
--- a/src/journal/journald.c
+++ b/src/journal/journald.c
@@ -55,7 +55,7 @@ int main(int argc, char *argv[]) {
         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"),
@@ -114,7 +114,7 @@ int main(int argc, char *argv[]) {
                 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"),
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index cd56470a3..22054835b 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -69,7 +69,7 @@ static bool journal_pid_changed(sd_journal *j) {
         /* 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) {
@@ -1715,7 +1715,7 @@ static sd_journal *journal_new(int flags, 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;
diff --git a/src/journal/test-compress-benchmark.c b/src/journal/test-compress-benchmark.c
index 4fb93ded7..200b7928f 100644
--- a/src/journal/test-compress-benchmark.c
+++ b/src/journal/test-compress-benchmark.c
@@ -170,7 +170,7 @@ int main(int argc, char *argv[]) {
         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
diff --git a/src/libsystemd/sd-bus/bus-creds.c b/src/libsystemd/sd-bus/bus-creds.c
index 349fa57f2..649fcdba4 100644
--- a/src/libsystemd/sd-bus/bus-creds.c
+++ b/src/libsystemd/sd-bus/bus-creds.c
@@ -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);
 
         if (pid == 0)
-                pid = getpid();
+                pid = getpid_cached();
 
         c = bus_creds_new();
         if (!c)
diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c
index 2f065c265..7059578eb 100644
--- a/src/libsystemd/sd-bus/sd-bus.c
+++ b/src/libsystemd/sd-bus/sd-bus.c
@@ -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->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);
 
@@ -3131,7 +3131,7 @@ bool bus_pid_changed(sd_bus *bus) {
         /* 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) {
diff --git a/src/libsystemd/sd-bus/test-bus-benchmark.c b/src/libsystemd/sd-bus/test-bus-benchmark.c
index 56ac2ab3d..ef7abe4dd 100644
--- a/src/libsystemd/sd-bus/test-bus-benchmark.c
+++ b/src/libsystemd/sd-bus/test-bus-benchmark.c
@@ -276,7 +276,7 @@ int main(int argc, char *argv[]) {
         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)
diff --git a/src/libsystemd/sd-bus/test-bus-kernel-bloom.c b/src/libsystemd/sd-bus/test-bus-kernel-bloom.c
index eb6179d7d..03d6f27d5 100644
--- a/src/libsystemd/sd-bus/test-bus-kernel-bloom.c
+++ b/src/libsystemd/sd-bus/test-bus-kernel-bloom.c
@@ -49,7 +49,7 @@ static void test_one(
         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)
diff --git a/src/libsystemd/sd-bus/test-bus-kernel.c b/src/libsystemd/sd-bus/test-bus-kernel.c
index 221481731..2fc22883e 100644
--- a/src/libsystemd/sd-bus/test-bus-kernel.c
+++ b/src/libsystemd/sd-bus/test-bus-kernel.c
@@ -41,7 +41,7 @@ int main(int argc, char *argv[]) {
 
         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)
diff --git a/src/libsystemd/sd-bus/test-bus-zero-copy.c b/src/libsystemd/sd-bus/test-bus-zero-copy.c
index 3380e8500..e599427ce 100644
--- a/src/libsystemd/sd-bus/test-bus-zero-copy.c
+++ b/src/libsystemd/sd-bus/test-bus-zero-copy.c
@@ -52,7 +52,7 @@ int main(int argc, char *argv[]) {
 
         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)
diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c
index a9a32dd5a..be1f01112 100644
--- a/src/libsystemd/sd-daemon/sd-daemon.c
+++ b/src/libsystemd/sd-daemon/sd-daemon.c
@@ -70,7 +70,7 @@ _public_ int sd_listen_fds(int unset_environment) {
                 goto finish;
 
         /* Is this for us? */
-        if (getpid() != pid) {
+        if (getpid_cached() != pid) {
                 r = 0;
                 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);
 
-        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. */
@@ -659,7 +659,7 @@ _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
                         goto finish;
 
                 /* Is this for us? */
-                if (getpid() != pid) {
+                if (getpid_cached() != pid) {
                         r = 0;
                         goto finish;
                 }
diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c
index b4686d006..320a1ca5c 100644
--- a/src/libsystemd/sd-event/sd-event.c
+++ b/src/libsystemd/sd-event/sd-event.c
@@ -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->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);
@@ -493,7 +493,7 @@ static bool event_pid_changed(sd_event *e) {
         /* 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) {
diff --git a/src/libsystemd/sd-event/test-event.c b/src/libsystemd/sd-event/test-event.c
index 8425378f3..1a581ae23 100644
--- a/src/libsystemd/sd-event/test-event.c
+++ b/src/libsystemd/sd-event/test-event.c
@@ -317,11 +317,11 @@ static void test_rtqueue(void) {
 
         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);
diff --git a/src/libsystemd/sd-netlink/sd-netlink.c b/src/libsystemd/sd-netlink/sd-netlink.c
index 68435564d..d67244676 100644
--- a/src/libsystemd/sd-netlink/sd-netlink.c
+++ b/src/libsystemd/sd-netlink/sd-netlink.c
@@ -44,7 +44,7 @@ static int sd_netlink_new(sd_netlink **ret) {
         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);
 
@@ -99,7 +99,7 @@ static bool rtnl_pid_changed(sd_netlink *rtnl) {
         /* 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) {
diff --git a/src/libsystemd/sd-resolve/sd-resolve.c b/src/libsystemd/sd-resolve/sd-resolve.c
index 60aa55de3..12fae65e6 100644
--- a/src/libsystemd/sd-resolve/sd-resolve.c
+++ b/src/libsystemd/sd-resolve/sd-resolve.c
@@ -459,7 +459,7 @@ static bool resolve_pid_changed(sd_resolve *r) {
         /* 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) {
@@ -473,7 +473,7 @@ _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;
diff --git a/src/login/logind.c b/src/login/logind.c
index fb70972b8..604659668 100644
--- a/src/login/logind.c
+++ b/src/login/logind.c
@@ -1254,7 +1254,7 @@ int main(int argc, char *argv[]) {
                 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"
@@ -1262,7 +1262,7 @@ int main(int argc, char *argv[]) {
 
         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,
diff --git a/src/login/pam_systemd.c b/src/login/pam_systemd.c
index dab082a26..df16907ed 100644
--- a/src/login/pam_systemd.c
+++ b/src/login/pam_systemd.c
@@ -41,6 +41,7 @@
 #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"
@@ -372,7 +373,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         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),
@@ -387,7 +388,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                                &reply,
                                "uusssssussbssa(sv)",
                                (uint32_t) pw->pw_uid,
-                               (uint32_t) getpid(),
+                               (uint32_t) getpid_cached(),
                                service,
                                type,
                                class,
diff --git a/src/machine/machined.c b/src/machine/machined.c
index 8719e01de..d8ddbec8b 100644
--- a/src/machine/machined.c
+++ b/src/machine/machined.c
@@ -34,6 +34,7 @@
 #include "label.h"
 #include "machine-image.h"
 #include "machined.h"
+#include "process-util.h"
 #include "signal-util.h"
 
 Manager *manager_new(void) {
@@ -398,7 +399,7 @@ int main(int argc, char *argv[]) {
                 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"
@@ -406,7 +407,7 @@ int main(int argc, char *argv[]) {
 
         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);
diff --git a/src/run/run.c b/src/run/run.c
index 2e6765aa1..86e304091 100644
--- a/src/run/run.c
+++ b/src/run/run.c
@@ -655,7 +655,7 @@ static int transient_scope_set_properties(sd_bus_message *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;
 
diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c
index e3b29e390..0ce8e5457 100644
--- a/src/shared/ask-password-api.c
+++ b/src/shared/ask-password-api.c
@@ -49,6 +49,7 @@
 #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"
@@ -519,7 +520,7 @@ int ask_password_agent(
                 "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,
diff --git a/src/shared/condition.c b/src/shared/condition.c
index 1af74c61f..103f8d2e3 100644
--- a/src/shared/condition.c
+++ b/src/shared/condition.c
@@ -48,6 +48,7 @@
 #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"
@@ -164,7 +165,7 @@ static int condition_test_user(Condition *c) {
         if (streq(username, c->parameter))
                 return 1;
 
-        if (getpid() == 1)
+        if (getpid_cached() == 1)
                 return streq(c->parameter, "root");
 
         u = c->parameter;
@@ -188,7 +189,7 @@ static int condition_test_group(Condition *c) {
                 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;
diff --git a/src/shared/pager.c b/src/shared/pager.c
index 4d7b02c63..ad4865252 100644
--- a/src/shared/pager.c
+++ b/src/shared/pager.c
@@ -87,7 +87,7 @@ int pager_open(bool no_pager, bool jump_to_end) {
         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)
diff --git a/src/test/test-cgroup.c b/src/test/test-cgroup.c
index 5336c1965..71e318a15 100644
--- a/src/test/test-cgroup.c
+++ b/src/test/test-cgroup.c
@@ -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_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);
 
diff --git a/src/test/test-log.c b/src/test/test-log.c
index 626d2c613..8ab569f47 100644
--- a/src/test/test-log.c
+++ b/src/test/test-log.c
@@ -39,7 +39,7 @@ int main(int argc, char* argv[]) {
         log_open();
 
         log_struct(LOG_INFO,
-                   "MESSAGE=Waldo PID="PID_FMT, getpid(),
+                   "MESSAGE=Waldo PID="PID_FMT, getpid_cached(),
                    "SERVICE=piepapo",
                    NULL);
 
@@ -47,12 +47,12 @@ int main(int argc, char* argv[]) {
         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",
diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c
index f78a8c0b5..046a96fdc 100644
--- a/src/test/test-process-util.c
+++ b/src/test/test-process-util.c
@@ -115,7 +115,7 @@ static void test_pid_is_unwaited(void) {
                 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));
 }
 
@@ -132,7 +132,7 @@ static void test_pid_is_alive(void) {
                 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));
 }
 
@@ -205,149 +205,149 @@ static void test_get_process_cmdline_harder(void) {
 
         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);
 
diff --git a/src/test/test-signal-util.c b/src/test/test-signal-util.c
index 671eb869c..92e392778 100644
--- a/src/test/test-signal-util.c
+++ b/src/test/test-signal-util.c
@@ -50,12 +50,12 @@ static void test_block_signals(void) {
 
 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);
 }
 
diff --git a/src/test/test-tmpfiles.c b/src/test/test-tmpfiles.c
index a7c86d155..b4c76048a 100644
--- a/src/test/test-tmpfiles.c
+++ b/src/test/test-tmpfiles.c
@@ -45,7 +45,7 @@ int main(int argc, char** argv) {
         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);
@@ -55,7 +55,7 @@ int main(int argc, char** argv) {
         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);
diff --git a/src/timesync/timesyncd.c b/src/timesync/timesyncd.c
index ff90f0407..86c14020b 100644
--- a/src/timesync/timesyncd.c
+++ b/src/timesync/timesyncd.c
@@ -25,6 +25,7 @@
 #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"
@@ -138,7 +139,7 @@ int main(int argc, char *argv[]) {
                 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");
diff --git a/src/udev/udevd.c b/src/udev/udevd.c
index acbddd418..0e49e6268 100644
--- a/src/udev/udevd.c
+++ b/src/udev/udevd.c
@@ -174,7 +174,7 @@ static void event_free(struct event *event) {
 
         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");
@@ -593,9 +593,9 @@ static int event_queue_insert(Manager *manager, struct udev_device *dev) {
 
         /* 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)
diff --git a/src/update-utmp/update-utmp.c b/src/update-utmp/update-utmp.c
index ae9859cca..f68d60a13 100644
--- a/src/update-utmp/update-utmp.c
+++ b/src/update-utmp/update-utmp.c
@@ -33,6 +33,7 @@
 #include "format-util.h"
 #include "log.h"
 #include "macro.h"
+#include "process-util.h"
 #include "special.h"
 #include "strv.h"
 #include "unit-name.h"
@@ -258,7 +259,7 @@ int main(int argc, char *argv[]) {
                 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);
@@ -271,7 +272,7 @@ int main(int argc, char *argv[]) {
                 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
commit 98eda38aed6a10c4f6d6ad0cac6e5361e87de52b
Author: Harald Hoyer <harald@hoyer.xyz>
Date:   Thu Jul 20 19:13:09 2017 +0200

    call chase_symlinks without the /sysroot prefix (#6411)
    
    In case fstab-generator is called in the initrd, chase_symlinks()
    returns with a canonical path "/sysroot/sysroot/<mountpoint>", if the
    "/sysroot" prefix is present in the path.
    
    This patch skips the "/sysroot" prefix for the chase_symlinks() call,
    because "/sysroot" is already the root directory and chase_symlinks()
    prepends the root directory in the canonical path returned.

diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c
index ea5ceb39c..8fc4c8d17 100644
--- a/src/fstab-generator/fstab-generator.c
+++ b/src/fstab-generator/fstab-generator.c
@@ -526,7 +526,7 @@ static int parse_fstab(bool initrd) {
                         continue;
                 }
 
-                where = initrd ? strappend("/sysroot/", me->mnt_dir) : strdup(me->mnt_dir);
+                where = strdup(me->mnt_dir);
                 if (!where)
                         return log_oom();
 
commit 4f5993c3f48d6f5454f72aeb10b4642fde758076
Author: Felipe Sateler <fsateler@users.noreply.github.com>
Date:   Mon Aug 28 13:49:03 2017 -0300

    shared: Add a linker script so that all functions are tagget @SD_SHARED instead of @Base (#6669)
    
    This helps prevent symbol collisions with other programs and libraries. In particular,
    because PAM modules are loaded into the process that is creating the session, and
    systemd creates PAM sessions, the potential for collisions is high.
    
    Disambiguate all systemd calls by tagging a 'version' SD_SHARED.
    
    Fixes #6624

diff --git a/src/shared/libshared.sym b/src/shared/libshared.sym
new file mode 100644
index 000000000..e4ae17eed
--- /dev/null
+++ b/src/shared/libshared.sym
@@ -0,0 +1,3 @@
+SD_SHARED {
+	global: *;
+};
diff --git a/src/shared/meson.build b/src/shared/meson.build
index 2eaef11a2..ce84d007d 100644
--- a/src/shared/meson.build
+++ b/src/shared/meson.build
@@ -136,6 +136,8 @@ libshared_deps = [threads,
                   liblz4,
                   libblkid]
 
+libshared_sym_path = '@0@/libshared.sym'.format(meson.current_source_dir())
+
 libshared = shared_library(
         libshared_name,
         shared_sources,
@@ -144,7 +146,8 @@ libshared = shared_library(
         libsystemd_internal_sources,
         libudev_sources,
         include_directories : includes,
-        link_args : ['-shared'],
+        link_args : ['-shared',
+                    '-Wl,--version-script=' + libshared_sym_path],
         c_args : ['-fvisibility=default'],
         dependencies : libshared_deps,
         install : true,
