]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: make use of getpid_cached() wherever we can 6413/head
authorLennart Poettering <lennart@poettering.net>
Thu, 20 Jul 2017 14:19:18 +0000 (16:19 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 20 Jul 2017 18:27:24 +0000 (20:27 +0200)
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.

53 files changed:
src/activate/activate.c
src/basic/cgroup-util.c
src/basic/fd-util.c
src/basic/fileio.c
src/basic/fs-util.c
src/basic/log.c
src/basic/log.h
src/basic/process-util.c
src/basic/terminal-util.c
src/basic/util.c
src/basic/virt.c
src/core/automount.c
src/core/cgroup.c
src/core/dbus.c
src/core/execute.c
src/core/main.c
src/core/service.c
src/core/shutdown.c
src/initctl/initctl.c
src/journal-remote/journal-remote.c
src/journal-remote/journal-upload.c
src/journal/journald-kmsg.c
src/journal/journald-server.c
src/journal/journald-syslog.c
src/journal/journald.c
src/journal/sd-journal.c
src/journal/test-compress-benchmark.c
src/libsystemd/sd-bus/bus-creds.c
src/libsystemd/sd-bus/sd-bus.c
src/libsystemd/sd-bus/test-bus-benchmark.c
src/libsystemd/sd-bus/test-bus-kernel-bloom.c
src/libsystemd/sd-bus/test-bus-kernel.c
src/libsystemd/sd-bus/test-bus-zero-copy.c
src/libsystemd/sd-daemon/sd-daemon.c
src/libsystemd/sd-event/sd-event.c
src/libsystemd/sd-event/test-event.c
src/libsystemd/sd-netlink/sd-netlink.c
src/libsystemd/sd-resolve/sd-resolve.c
src/login/logind.c
src/login/pam_systemd.c
src/machine/machined.c
src/run/run.c
src/shared/ask-password-api.c
src/shared/condition.c
src/shared/pager.c
src/test/test-cgroup.c
src/test/test-log.c
src/test/test-process-util.c
src/test/test-signal-util.c
src/test/test-tmpfiles.c
src/timesync/timesyncd.c
src/udev/udevd.c
src/update-utmp/update-utmp.c

index 6ebd820410de0191e360ecf0799fb3fc90508730..4b82dca4918d750d404718c1f6a39ba54081a6fb 100644 (file)
@@ -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)
index 6344372610b70508d46132f27eb2c439249ac3f9..5dea078978432d9675ed3cb3b4546b89ff8ddc25 100644 (file)
@@ -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);
 
index 19ad20789b7cb9d8e2f558f16d4768576b8510e5..9d61044c89e76e7922257541682371689cf7399a 100644 (file)
@@ -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;
index 9a185e3e6098097746c4c2258dd9d3162212e6de..0678db4a7bac3707a6f8ed33f7b7eab6eb7a7843 100644 (file)
@@ -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;
index 8fe19ee4e419cc8b7f5f83cd3fbbc64e6c59eeaf..5e1163c6a74aa341dc5171306aa170db8e9e0b7e 100644 (file)
@@ -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;
index 3fd53800a090e6d82037e1fa2a9c51beaa20afb4..d0caeb5ca39182854e7447452d1c631e8c208112 100644 (file)
@@ -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),
index ff5d776b1d7054f2acd7c3e73eca8b2cf737782e..186747ff8e288a8ed16ece52fbda807e6caa1f31 100644 (file)
@@ -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__)
index 98adee6d9f3bac4709cc1d1f3f7a5412fee0ed91..ce6a59c8a438e6f5f7b1429857bd2dc3d1fe8a90 100644 (file)
@@ -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)
index 9a8ef825c57f38c3ea7c44eb930223c865555ea3..8a0419df75d28bc54a20e34e352e26483620a0cf 100644 (file)
@@ -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
index b52a5db31b9e8f997a2a3d4992e779b8110001dd..2f45128ed4160550102e6222c352617c579a2358 100644 (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
          * 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
index 5143ac6656664b23d8651b2559626c67a2a1a9bc..d8eeb54dbf43846dafbfe31027b9127d11486eba 100644 (file)
@@ -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");
index 0f72854ceb94c5e304be61fe80ccd543e4dad6c4..96de933e2327ec136c0573e84cc7ebea8061eee0 100644 (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(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;
index 71f307fb6cac8248b7f16b8507bfffa5e9a64459..fc8b9565a270633d2685c31764287da6360b034d 100644 (file)
@@ -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;
 
index cfc045d282c72ed5069f2fe4a8c6e3caeef67f43..96bf975f881ce2413b514a545619b817279f1de7 100644 (file)
@@ -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");
index 3efd56fefe33882b3591c35f97df1789261b9623..48b84815ca9f14f9051742f662f61f0128c6bb8f 100644 (file)
@@ -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 :
index babcab49780e4e1a61d08b9d98162700ae42b5ec..5d0f385d7bdcc7bb9f042a69269a51eeb8c6ea88 100644 (file)
@@ -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,
index 1a9044d2dabaf031797d6557b5e137cf584cc26c..2909aacf8dce738d9b5fd98e9654cb52239eef27 100644 (file)
@@ -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);
index a7d5e57936d18336f258733d01f2ece7e06ef72b..6d1cc3120a319e5598fd8f4e77aaaea482c6f9ba 100644 (file)
@@ -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;
index 6aeb5ad6148980db297e69ad1dfca88e027962e2..deb3f1b2a80a78fdd1c65bd11b7ae37a0361e326 100644 (file)
@@ -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,
index 36c1a32dcd8c2c77350c177575fb40157103525a..810206c6216d5fcb4b4d3e55fe12a7c7ff635466 100644 (file)
@@ -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...");
index e0858dda7bc9a9dd26ccb3fe55eaf25b73dbce9e..ea264989ab1b6052c98b9ad807b59bb638aed3fc 100644 (file)
@@ -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) {
index 70ea5620624dd4dfdc700592ec6c23f0658ae7b0..7fea85a5d842599bdea899a1c33aad8e61ed87b6 100644 (file)
@@ -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) {
index 6eb19e88669c1e4105626e37cf1f5c519c46ad4a..f391845ba9a9dc022cb02ddbbb54831841bc298a 100644 (file)
@@ -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();
 
index 8e034c8fa9cdaf5e9bfd8869d0d95f490ad50cda..17f855e96731cd4c9b340a802864affc9a776534 100644 (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 */
 
                 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)
index 1aaef387b49ab1ad3fa0c8d19f8a145bbbb28976..4dee764fddffbff673eede1825b6988a203677de 100644 (file)
@@ -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"),
index cd56470a33a86aea9b43778e6da587c9ca00b1eb..22054835baab03c9095f91f6f91814abb7f7b5af 100644 (file)
@@ -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;
index 4fb93ded73ef32b1156c2185243b8367434e6a3e..200b7928f291f7a20e6c82572e9c95db99e40037 100644 (file)
@@ -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
index 349fa57f2d43022604a4248a791d5e93127b0108..649fcdba44ce49c5a2aed03a3c55e534caa82463 100644 (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);
 
         if (pid == 0)
-                pid = getpid();
+                pid = getpid_cached();
 
         c = bus_creds_new();
         if (!c)
index 2f065c265784ddd1a1ced84879c8b1e28a8b2c3e..7059578eb4f002e2cdf0b5753ff54f8b8a970cb0 100644 (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->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) {
index 56ac2ab3dd54b408def3c1279846cd13ec7ba5d5..ef7abe4dd9dcdd08220cd019661f43f7b0e16f7f 100644 (file)
@@ -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)
index eb6179d7d213fa19053041f5af9fe8a44783bd5f..03d6f27d55d8bfee00ef9682209137f5a66fe7d4 100644 (file)
@@ -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)
index 22148173122cf5d7835207e401b12162e830878b..2fc22883e0504680d5db7fd711f81483e4fea87f 100644 (file)
@@ -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)
index 3380e8500a8671d44bc293d4190ff22d151fb418..e599427ce64b74d4ca7484ad1c4203111d18471a 100644 (file)
@@ -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)
index a9a32dd5a2b59d53e8a3d34667aeab1a2ff4138b..be1f01112e89231e23fb599abf2d8bbb45f2d33d 100644 (file)
@@ -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;
                 }
index b4686d0065b1a943312735bf11f15b4d6b0ebac0..320a1ca5c0e0e57f07122c2cb45f1faa150be332 100644 (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->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) {
index 8425378f3438f299c1657f83597fd66cb6aba18c..1a581ae23eb8cdf40e99190f9710d80060d4f573 100644 (file)
@@ -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);
index 68435564deebc2f1a7e27959aef863609dc39b4e..d67244676c27c453dfcdba80c8438ca47ba83f97 100644 (file)
@@ -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) {
index 60aa55de3b23643324ea7e4496666569605186fe..12fae65e6b1b5e615325613399d88e898e22fedd 100644 (file)
@@ -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;
index fb70972b80906022f320b643e53520ecad96f7f2..6046596684e512503ea52f92ca05be6675e03d61 100644 (file)
@@ -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,
index dab082a26ee9269f69b8d895bdef72c13a090553..df16907ed2d323336a7b54ebce4030a176b3fcec 100644 (file)
@@ -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,
index 8719e01de92d3cf979a3ed2a2bffc3e230b06a3c..d8ddbec8b954e6f00a320f4adf2cdb092958ce42 100644 (file)
@@ -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);
index 2e6765aa188b4652c36e18d79d00223be91fe209..86e304091832e2f4ff068e3d7fc6310cbbddc821 100644 (file)
@@ -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;
 
index e3b29e390cc19fefc9ac53223f03866206910365..0ce8e54573b9ecbf55c042aa1078c03e216dadf7 100644 (file)
@@ -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,
index 1af74c61f00bd76c197f06570ab2e259e3ffeeef..103f8d2e3952944b0643f62494effc01043d99db 100644 (file)
@@ -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;
index 4d7b02c63c4ae08b9001472a3bcbd54c838d7bab..ad48652527865e4f934c360691312b9cef0cd6ef 100644 (file)
@@ -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)
index 5336c196521d1560ddf9fd6a3c942f6160d74871..71e318a15bc19926a457aa183f0ce235b5a884fc 100644 (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_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);
 
index 626d2c6135b1d23efedaab57816f38211ec73da8..8ab569f477fced5505f9e7e06ed0798d25a7d90e 100644 (file)
@@ -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",
index f78a8c0b5321db7b07cc18bc0c288b3369d3fef3..046a96fdc88cf327a50f3842d9318c8613bd1ffe 100644 (file)
@@ -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);
 
index 671eb869cb9f9e3da9ab00530fbbde56b19b2b0b..92e39277849c46ca1ac0a3e422e60a26d9114117 100644 (file)
@@ -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);
 }
 
index a7c86d155a969d08b8b260fae393732b441309ed..b4c76048a460e9dfeaa84ce5261c923c76340834 100644 (file)
@@ -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);
index ff90f04070cab7695d7891b2eead570aaa10f0b3..86c14020b4560ba7a875750ba245f0e818ed60f7 100644 (file)
@@ -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");
index acbddd41807e8a794e636122b253ef683d42bff3..0e49e62688d6746cfe2cd7255253fedb333513b3 100644 (file)
@@ -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)
index ae9859ccadd88821a792ca1f6831c7fa3eb84b63..f68d60a134a0c13c0069d47b14afa8a09f39e1d1 100644 (file)
@@ -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