From df0ff127758809a45105893772de76082d12a26d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 20 Jul 2017 16:19:18 +0200 Subject: [PATCH] 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. --- src/activate/activate.c | 5 +- src/basic/cgroup-util.c | 6 +- src/basic/fd-util.c | 3 +- src/basic/fileio.c | 3 +- src/basic/fs-util.c | 2 +- src/basic/log.c | 14 ++-- src/basic/log.h | 5 +- src/basic/process-util.c | 4 +- src/basic/terminal-util.c | 2 +- src/basic/util.c | 2 +- src/basic/virt.c | 2 +- src/core/automount.c | 2 +- src/core/cgroup.c | 2 +- src/core/dbus.c | 2 +- src/core/execute.c | 8 +- src/core/main.c | 30 ++++---- src/core/service.c | 8 +- src/core/shutdown.c | 2 +- src/initctl/initctl.c | 4 +- src/journal-remote/journal-remote.c | 4 +- src/journal-remote/journal-upload.c | 2 +- src/journal/journald-kmsg.c | 2 +- src/journal/journald-server.c | 2 +- src/journal/journald-syslog.c | 2 +- src/journal/journald.c | 4 +- src/journal/sd-journal.c | 4 +- src/journal/test-compress-benchmark.c | 2 +- src/libsystemd/sd-bus/bus-creds.c | 2 +- src/libsystemd/sd-bus/sd-bus.c | 4 +- src/libsystemd/sd-bus/test-bus-benchmark.c | 2 +- src/libsystemd/sd-bus/test-bus-kernel-bloom.c | 2 +- src/libsystemd/sd-bus/test-bus-kernel.c | 2 +- src/libsystemd/sd-bus/test-bus-zero-copy.c | 2 +- src/libsystemd/sd-daemon/sd-daemon.c | 6 +- src/libsystemd/sd-event/sd-event.c | 4 +- src/libsystemd/sd-event/test-event.c | 10 +-- src/libsystemd/sd-netlink/sd-netlink.c | 4 +- src/libsystemd/sd-resolve/sd-resolve.c | 4 +- src/login/logind.c | 4 +- src/login/pam_systemd.c | 5 +- src/machine/machined.c | 5 +- src/run/run.c | 2 +- src/shared/ask-password-api.c | 3 +- src/shared/condition.c | 5 +- src/shared/pager.c | 2 +- src/test/test-cgroup.c | 6 +- src/test/test-log.c | 6 +- src/test/test-process-util.c | 76 +++++++++---------- src/test/test-signal-util.c | 10 +-- src/test/test-tmpfiles.c | 4 +- src/timesync/timesyncd.c | 3 +- src/udev/udevd.c | 6 +- src/update-utmp/update-utmp.c | 5 +- 53 files changed, 161 insertions(+), 151 deletions(-) diff --git a/src/activate/activate.c b/src/activate/activate.c index 6ebd820410..4b82dca491 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 6344372610..5dea078978 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 19ad20789b..9d61044c89 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 9a185e3e60..0678db4a7b 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 8fe19ee4e4..5e1163c6a7 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 3fd53800a0..d0caeb5ca3 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 ff5d776b1d..186747ff8e 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 98adee6d9f..ce6a59c8a4 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 9a8ef825c5..8a0419df75 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 b52a5db31b..2f45128ed4 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 5143ac6656..d8eeb54dbf 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 0f72854ceb..96de933e23 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 71f307fb6c..fc8b9565a2 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 cfc045d282..96bf975f88 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 3efd56fefe..48b84815ca 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 babcab4978..5d0f385d7b 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 1a9044d2da..2909aacf8d 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 a7d5e57936..6d1cc3120a 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 6aeb5ad614..deb3f1b2a8 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 36c1a32dcd..810206c621 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 e0858dda7b..ea264989ab 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 70ea562062..7fea85a5d8 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 6eb19e8866..f391845ba9 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 8e034c8fa9..17f855e967 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 1aaef387b4..4dee764fdd 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 cd56470a33..22054835ba 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 4fb93ded73..200b7928f2 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 349fa57f2d..649fcdba44 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 2f065c2657..7059578eb4 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 56ac2ab3dd..ef7abe4dd9 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 eb6179d7d2..03d6f27d55 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 2214817312..2fc22883e0 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 3380e8500a..e599427ce6 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 a9a32dd5a2..be1f01112e 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 b4686d0065..320a1ca5c0 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 8425378f34..1a581ae23e 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 68435564de..d67244676c 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 60aa55de3b..12fae65e6b 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 fb70972b80..6046596684 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 dab082a26e..df16907ed2 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 8719e01de9..d8ddbec8b9 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 2e6765aa18..86e3040918 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 e3b29e390c..0ce8e54573 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 1af74c61f0..103f8d2e39 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 4d7b02c63c..ad48652527 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 5336c19652..71e318a15b 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 626d2c6135..8ab569f477 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 f78a8c0b53..046a96fdc8 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 671eb869cb..92e3927784 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 a7c86d155a..b4c76048a4 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 ff90f04070..86c14020b4 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 acbddd4180..0e49e62688 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 ae9859ccad..f68d60a134 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