<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Apr 23, 2015 at 6:25 PM, Lennart Poettering <span dir="ltr"><<a href="mailto:lennart@poettering.net" target="_blank">lennart@poettering.net</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The kernel doesn't have any nice userspace APIs to translate PIDs like<br>
this, to my knowledge at least.<br>
<br>
That said, one can implement something like this by using the fact<br>
that the kernel will translate user credentials that are sent via<br>
AF_UNIX SCM_CREDENTIALS that pass through a PID namespace<br>
boundary. hence, let's say you have one pid $n that identifies the<br>
namespace you care about, and a container pid $a you want to translate<br>
to host pid $b, then you'd create an AF_UNIX/SOCK_SEQPACKET<br>
socketpair(), fork(), run setns() in the child to join the namespace<br>
of $n, and send a single datagram to the parent with sendmsg(), while<br>
explicitly specifiying the SCM_CREDENTIALS auxiliary field with the $a<br>
in it, and exit. In the parent you use recvmsg() to read that datagram<br>
with its SCM_CREDENTIALS, and use the pid you see as $b. Vice versa,<br>
if you want to translate the other way you would send the datagram the<br>
other way.<br>
<br>
nasty? absolutely! (also, only available for privileged processes, due<br>
to the setns() call).<br>
<br>
I am not aware of any nicer way.<br></blockquote><div><br></div><div>I think /proc/<pid>/status in current linux.git has additional fields for the process PIDs in all namespaces, so you could probably get the "outer" PID by looking at the container's /proc.</div></div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Mantas Mikulėnas <<a href="mailto:grawity@gmail.com" target="_blank">grawity@gmail.com</a>></div></div>
</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Apr 23, 2015 at 6:25 PM, Lennart Poettering <span dir="ltr"><<a href="mailto:lennart@poettering.net" target="_blank">lennart@poettering.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Fri, 24.04.15 00:10, WaLyong Cho (<a href="mailto:walyong.cho@samsung.com">walyong.cho@samsung.com</a>) wrote:<br>
<br>
> Hello,<br>
><br>
> I'm looking for a proper way what method to map PID between host and<br>
> container.<br>
><br>
> In case of systemctl, if I know pid in container(let's call this pid_c)<br>
> then I can find the pid in sight of host(let's call this pid_h) by using<br>
> "systemctl -M {container} pid_c" in host shell. But I can not find pid_c<br>
> when I know pid_h by using "systemctl status pid_h".<br>
><br>
> The former seems possible by parsing machine cgroup.<br>
> In the latter case, can we also find the pid_c?<br>
><br>
> And is there any other method to map between those?<br>
> For example, when if I use lxc then how I find each pid? (lxc-ps?)<br>
> According to my search, namespace can give me some of hints about this.<br>
> I could not find more detail.<br>
> Is there any proper libs or tools for this?<br>
<br>
</span>The kernel doesn't have any nice userspace APIs to translate PIDs like<br>
this, to my knowledge at least.<br>
<br>
That said, one can implement something like this by using the fact<br>
that the kernel will translate user credentials that are sent via<br>
AF_UNIX SCM_CREDENTIALS that pass through a PID namespace<br>
boundary. hence, let's say you have one pid $n that identifies the<br>
namespace you care about, and a container pid $a you want to translate<br>
to host pid $b, then you'd create an AF_UNIX/SOCK_SEQPACKET<br>
socketpair(), fork(), run setns() in the child to join the namespace<br>
of $n, and send a single datagram to the parent with sendmsg(), while<br>
explicitly specifiying the SCM_CREDENTIALS auxiliary field with the $a<br>
in it, and exit. In the parent you use recvmsg() to read that datagram<br>
with its SCM_CREDENTIALS, and use the pid you see as $b. Vice versa,<br>
if you want to translate the other way you would send the datagram the<br>
other way.<br>
<br>
nasty? absolutely! (also, only available for privileged processes, due<br>
to the setns() call).<br>
<br>
I am not aware of any nicer way.<br>
<span class="HOEnZb"><font color="#888888"><br>
Lennart<br>
<br>
--<br>
Lennart Poettering, Red Hat<br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
systemd-devel mailing list<br>
<a href="mailto:systemd-devel@lists.freedesktop.org">systemd-devel@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/systemd-devel" target="_blank">http://lists.freedesktop.org/mailman/listinfo/systemd-devel</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Mantas Mikulėnas <<a href="mailto:grawity@gmail.com" target="_blank">grawity@gmail.com</a>></div></div>
</div>