1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

process-util: modernize is_main_thread(); make sure get_process_ppid() won't return ppid == 0 (#35561)

Split out from #35242
This commit is contained in:
Yu Watanabe 2024-12-12 05:16:04 +09:00 committed by GitHub
commit ab5de638e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -702,15 +702,17 @@ int get_process_ppid(pid_t pid, pid_t *ret) {
assert(pid >= 0);
if (pid == 0 || pid == getpid_cached()) {
if (pid == 0)
pid = getpid_cached();
if (pid == 1) /* PID 1 has no parent, shortcut this case */
return -EADDRNOTAVAIL;
if (pid == getpid_cached()) {
if (ret)
*ret = getppid();
return 0;
}
if (pid == 1) /* PID 1 has no parent, shortcut this case */
return -EADDRNOTAVAIL;
p = procfs_file_alloca(pid, "stat");
r = read_one_line_file(p, &line);
if (r == -ENOENT)
@ -724,7 +726,6 @@ int get_process_ppid(pid_t pid, pid_t *ret) {
p = strrchr(line, ')');
if (!p)
return -EIO;
p++;
if (sscanf(p, " "
@ -733,9 +734,9 @@ int get_process_ppid(pid_t pid, pid_t *ret) {
&ppid) != 1)
return -EIO;
/* If ppid is zero the process has no parent. Which might be the case for PID 1 but also for
* processes originating in other namespaces that are inserted into a pidns. Return a recognizable
* error in this case. */
/* If ppid is zero the process has no parent. Which might be the case for PID 1 (caught above)
* but also for processes originating in other namespaces that are inserted into a pidns.
* Return a recognizable error in this case. */
if (ppid == 0)
return -EADDRNOTAVAIL;
@ -1226,12 +1227,12 @@ int pid_from_same_root_fs(pid_t pid) {
}
bool is_main_thread(void) {
static thread_local int cached = 0;
static thread_local int cached = -1;
if (_unlikely_(cached == 0))
cached = getpid_cached() == gettid() ? 1 : -1;
if (cached < 0)
cached = getpid_cached() == gettid();
return cached > 0;
return cached;
}
bool oom_score_adjust_is_valid(int oa) {