简体   繁体   中英

Killing a process from the global scope using its kernel namespace PID

Having some difficulties with Linux kernel namespaces today, specifically correlating PIDs inside of a unique PID namespace to those within the global PID namespace

I need to be able to do one of the following:

a) Kill a process from the global scope using a PID assigned by the namespace

OR

b) Translate a namespace specific PID to a global PID, so I can kill the PID from the global scope

OR

c) Enable a process within a PID namespace to report to me its global PID, so I can kill the PID from the global scope

There is some discussion on the process structures which contain the PID information in namespace scenarios here . I'm not sure how / if I can access these structures from a userland application, or if I need to add in support via a kernel hack.

Why? I have an application which currently uses network namespaces. I am adding support for PID namespaces. Here is how it currently works:

Before the introduction of PID namespaces: The main application currently launches a bash console in another network namespace. It then uses that bash console to start programs and has those programs report their current PID. When the main application wants to kill a subprocess in that network namespace, it just tells the OS to kill the PID reported back.

With PID namespaces (broken state): The main application currently launches a bash console in another network and PID namespace. It then uses that bash console to start programs and has those programs report their current PID. However, the current PID reported back is not valid in the global PID namespace (it may be 10, when the PID in the global namespace is 56000). As a result, the main application cannot kill the subprocess in that network + PID namespace

As always, any guidance is appreciated

An approach could be search in the pid queue the process descriptor that matches the target pid, if the reporting shell is on the same workspace, it can make a system call to get the other process 'process descriptor' and make some kind of for loop to find the process descripton in /proc/< pid>

you may also want to take a look here: http://lkml.indiana.edu/hypermail/linux/kernel/0707.0/1701.html Specially, this part:

/*
 * the helpers to get the pid's id seen from different namespaces
 *
 * pid_nr() : global id, i.e. the id seen from the init namespace;
 * pid_vnr() : virtual id, i.e. the id seen from the namespace this pid
 * belongs to. this only makes sence when called in the
 * context of the task that belongs to the same namespace;
 * pid_nr_ns() : id seen from the ns specified.
 *
 * see also task_xid_nr() etc in include/linux/sched.h
 */

static inline pid_t pid_nr(struct pid *pid)
{
pid_t nr = 0;
if (pid)
-    nr = pid->nr;
+    nr = pid->numbers[0].nr;
return nr;
}

Hope it helps!

Best Regards!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM