process-util: port pid_from_same_root_fs() to pidref, and port three places over to it
This commit is contained in:
@@ -1228,18 +1228,53 @@ int pidref_is_alive(const PidRef *pidref) {
|
||||
return result;
|
||||
}
|
||||
|
||||
int pid_from_same_root_fs(pid_t pid) {
|
||||
const char *root;
|
||||
int pidref_from_same_root_fs(PidRef *a, PidRef *b) {
|
||||
_cleanup_(pidref_done) PidRef self = PIDREF_NULL;
|
||||
int r;
|
||||
|
||||
if (pid < 0)
|
||||
/* Checks if the two specified processes have the same root fs. Either can be specified as NULL in
|
||||
* which case we'll check against ourselves. */
|
||||
|
||||
if (!a || !b) {
|
||||
r = pidref_set_self(&self);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (!a)
|
||||
a = &self;
|
||||
if (!b)
|
||||
b = &self;
|
||||
}
|
||||
|
||||
if (!pidref_is_set(a) || !pidref_is_set(b))
|
||||
return -ESRCH;
|
||||
|
||||
/* If one of the two processes have the same root they cannot have the same root fs, but if both of
|
||||
* them do we don't know */
|
||||
if (pidref_is_remote(a) && pidref_is_remote(b))
|
||||
return -EREMOTE;
|
||||
if (pidref_is_remote(a) || pidref_is_remote(b))
|
||||
return false;
|
||||
|
||||
if (pid == 0 || pid == getpid_cached())
|
||||
if (pidref_equal(a, b))
|
||||
return true;
|
||||
|
||||
root = procfs_file_alloca(pid, "root");
|
||||
const char *roota = procfs_file_alloca(a->pid, "root");
|
||||
const char *rootb = procfs_file_alloca(b->pid, "root");
|
||||
|
||||
return inode_same(root, "/proc/1/root", 0);
|
||||
int result = inode_same(roota, rootb, 0);
|
||||
if (result == -ENOENT)
|
||||
return proc_mounted() == 0 ? -ENOSYS : -ESRCH;
|
||||
if (result < 0)
|
||||
return result;
|
||||
|
||||
r = pidref_verify(a);
|
||||
if (r < 0)
|
||||
return r;
|
||||
r = pidref_verify(b);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool is_main_thread(void) {
|
||||
|
||||
@@ -93,7 +93,7 @@ int pid_is_unwaited(pid_t pid);
|
||||
int pidref_is_unwaited(const PidRef *pidref);
|
||||
int pid_is_my_child(pid_t pid);
|
||||
int pidref_is_my_child(const PidRef *pidref);
|
||||
int pid_from_same_root_fs(pid_t pid);
|
||||
int pidref_from_same_root_fs(PidRef *a, PidRef *b);
|
||||
|
||||
bool is_main_thread(void);
|
||||
|
||||
|
||||
+8
-11
@@ -799,19 +799,16 @@ int running_in_chroot(void) {
|
||||
if (getenv_bool("SYSTEMD_IGNORE_CHROOT") > 0)
|
||||
return 0;
|
||||
|
||||
r = inode_same("/proc/1/root", "/", 0);
|
||||
if (r == -ENOENT) {
|
||||
r = proc_mounted();
|
||||
if (r == 0) {
|
||||
if (getpid_cached() == 1)
|
||||
return false; /* We will mount /proc, assuming we're not in a chroot. */
|
||||
r = pidref_from_same_root_fs(&PIDREF_MAKE_FROM_PID(1), NULL);
|
||||
if (r == -ENOSYS) {
|
||||
if (getpid_cached() == 1)
|
||||
return false; /* We will mount /proc, assuming we're not in a chroot. */
|
||||
|
||||
log_debug("/proc is not mounted, assuming we're in a chroot.");
|
||||
return true;
|
||||
}
|
||||
if (r > 0) /* If we have fake /proc/, we can't do the check properly. */
|
||||
return -ENOSYS;
|
||||
log_debug("/proc/ is not mounted, assuming we're in a chroot.");
|
||||
return true;
|
||||
}
|
||||
if (r == -ESRCH) /* We must have a fake /proc/, we can't do the check properly. */
|
||||
return -ENOSYS;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user