【问题标题】:Linux - Different threads of a process in different namespacesLinux - 不同命名空间中进程的不同线程
【发布时间】:2015-08-01 02:08:11
【问题描述】:

我希望在每个现有网络命名空间中打开一个 netlink 套接字,以侦听与接口关联的 LINK 消息。我想从一个过程中做到这一点。

根据setns() 文档-“给定一个引用命名空间的文件描述符,将调用线程与该命名空间重新关联。”。因此,我是否可以通过简单地使用 pthread_create() 为我需要的每个命名空间创建一个线程、调用 setns() 然后打开 netlink 套接字来完成我的任务。

我问的原因是因为我看到了有关setns() 作用于进程命名空间的相互矛盾的信息。

【问题讨论】:

  • 我不知道,但这似乎很容易测试。创建几个命名空间,产生几个线程,调用setns(),看看你会得到什么。
  • 你能引用你看到冲突信息的参考吗?

标签: c linux


【解决方案1】:

简短回答:是的,您可以...因为您正在处理网络命名空间。

长答案:

不同类型的 Linux 内核命名空间有时会有不同的约束,这些差异会让你偏离正轨。一个非常好的来源是 Micheal Kerrisk 的 setns(2) 手册页。特别是标题为“特定命名空间类型的详细信息”的部分(很遗憾,不支持深度链接)。

至于将进程或线程切换到不同的网络命名空间,以下适用:

Network, IPC, time, and UTS namespaces
    In order to reassociate itself with a new network, IPC,
    time, or UTS namespace, the caller must have the
    CAP_SYS_ADMIN capability both in its own user namespace
    and in the user namespace that owns the target namespace.

请注意,网络命名空间是“扁平的”,因此它们不会形成任何层次结构。

相比之下,PID 命名空间有不同的约束,最明显的是因为它们确实形成了层次结构:

    Reassociating with a PID namespace is allowed only if the
    target PID namespace is a descendant (child, grandchild,
    etc.)  of, or is the same as, the current PID namespace of
    the caller.

然后是完全不同的东西:挂载命名空间。

    A process can't join a new mount namespace if it is
    sharing filesystem-related attributes (the attributes
    whose sharing is controlled by the clone(2) CLONE_FS flag)
    with another process.

这是一种稍微复杂的说法,即一旦您的进程中有另一个线程(=第一个任务),则该进程的任何线程的挂载命名空间都不能再更改,第一个也不能更改,也没有任何其他任务。

【讨论】:

    猜你喜欢
    • 2016-11-13
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 2014-12-08
    相关资源
    最近更新 更多