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

This moves pretty much all uses of getpid() over to getpid_raw(). I
didn't specifically check whether the optimization is worth it for each
replacement, but in order to keep things simple and systematic I
switched over everything at once.
This commit is contained in:
Lennart Poettering
2017-07-20 20:27:24 +02:00
parent 5c30a6d2b8
commit df0ff12775
53 changed files with 161 additions and 151 deletions
+3 -3
View 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);
+2 -1
View 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;
+2 -1
View 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;
+1 -1
View 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;
+7 -7
View 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),
+3 -2
View 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__)
+2 -2
View 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)
+1 -1
View 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
+1 -1
View File
@@ -219,7 +219,7 @@ int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *pa
/* Spawns a temporary TTY agent, making sure it goes away when
* 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
+1 -1
View 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");