namespace-util: introduce pidref_namespace_open()
authorMike Yuan <me@yhndnzj.com>
Mon, 22 Jul 2024 18:11:54 +0000 (20:11 +0200)
committerMike Yuan <me@yhndnzj.com>
Tue, 23 Jul 2024 17:30:54 +0000 (19:30 +0200)
src/basic/namespace-util.c
src/basic/namespace-util.h

index 739867672e25855b245f227d8812765435ab50fe..d9ad25fdd788409514c63b00b14fc767b19e726b 100644 (file)
@@ -42,8 +42,8 @@ static NamespaceType clone_flag_to_namespace_type(unsigned long clone_flag) {
         return _NAMESPACE_TYPE_INVALID;
 }
 
-int namespace_open(
-                pid_t pid,
+int pidref_namespace_open(
+                const PidRef *pidref,
                 int *ret_pidns_fd,
                 int *ret_mntns_fd,
                 int *ret_netns_fd,
@@ -52,13 +52,14 @@ int namespace_open(
 
         _cleanup_close_ int pidns_fd = -EBADF, mntns_fd = -EBADF, netns_fd = -EBADF,
                 userns_fd = -EBADF, root_fd = -EBADF;
+        int r;
 
-        assert(pid >= 0);
+        assert(pidref_is_set(pidref));
 
         if (ret_pidns_fd) {
                 const char *pidns;
 
-                pidns = pid_namespace_path(pid, NAMESPACE_PID);
+                pidns = pid_namespace_path(pidref->pid, NAMESPACE_PID);
                 pidns_fd = open(pidns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
                 if (pidns_fd < 0)
                         return -errno;
@@ -67,7 +68,7 @@ int namespace_open(
         if (ret_mntns_fd) {
                 const char *mntns;
 
-                mntns = pid_namespace_path(pid, NAMESPACE_MOUNT);
+                mntns = pid_namespace_path(pidref->pid, NAMESPACE_MOUNT);
                 mntns_fd = open(mntns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
                 if (mntns_fd < 0)
                         return -errno;
@@ -76,7 +77,7 @@ int namespace_open(
         if (ret_netns_fd) {
                 const char *netns;
 
-                netns = pid_namespace_path(pid, NAMESPACE_NET);
+                netns = pid_namespace_path(pidref->pid, NAMESPACE_NET);
                 netns_fd = open(netns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
                 if (netns_fd < 0)
                         return -errno;
@@ -85,7 +86,7 @@ int namespace_open(
         if (ret_userns_fd) {
                 const char *userns;
 
-                userns = pid_namespace_path(pid, NAMESPACE_USER);
+                userns = pid_namespace_path(pidref->pid, NAMESPACE_USER);
                 userns_fd = open(userns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
                 if (userns_fd < 0 && errno != ENOENT)
                         return -errno;
@@ -94,12 +95,16 @@ int namespace_open(
         if (ret_root_fd) {
                 const char *root;
 
-                root = procfs_file_alloca(pid, "root");
+                root = procfs_file_alloca(pidref->pid, "root");
                 root_fd = open(root, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
                 if (root_fd < 0)
                         return -errno;
         }
 
+        r = pidref_verify(pidref);
+        if (r < 0)
+                return r;
+
         if (ret_pidns_fd)
                 *ret_pidns_fd = TAKE_FD(pidns_fd);
 
@@ -118,6 +123,22 @@ int namespace_open(
         return 0;
 }
 
+int namespace_open(
+                pid_t pid,
+                int *ret_pidns_fd,
+                int *ret_mntns_fd,
+                int *ret_netns_fd,
+                int *ret_userns_fd,
+                int *ret_root_fd) {
+
+        assert(pid >= 0);
+
+        if (pid == 0)
+                pid = getpid_cached();
+
+        return pidref_namespace_open(&PIDREF_MAKE_FROM_PID(pid), ret_pidns_fd, ret_mntns_fd, ret_netns_fd, ret_userns_fd, ret_root_fd);
+}
+
 int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd) {
         int r;
 
index 545952a5d1093f6067846a1a058bd6c800076cb7..0a7616443dbe161c86a5bd69dbf4266b8c8bdb56 100644 (file)
@@ -3,6 +3,8 @@
 
 #include <sys/types.h>
 
+#include "pidref.h"
+
 typedef enum NamespaceType {
         NAMESPACE_CGROUP,
         NAMESPACE_IPC,
@@ -22,6 +24,13 @@ extern const struct namespace_info {
         unsigned int clone_flag;
 } namespace_info[_NAMESPACE_TYPE_MAX + 1];
 
+int pidref_namespace_open(
+                const PidRef *pidref,
+                int *ret_pidns_fd,
+                int *ret_mntns_fd,
+                int *ret_netns_fd,
+                int *ret_userns_fd,
+                int *ret_root_fd);
 int namespace_open(
                 pid_t pid,
                 int *ret_pidns_fd,