当PID namespace中的init进程结束时,会销毁对应的PID namespace,并向所有其它的子进程发送SIGKILL。这也是为什么当我们手动kill掉容器的第一个init进程,容器会自动结束。

    image.png

    1. static struct task_struct *find_child_reaper(struct task_struct *father,
    2. struct list_head *dead)
    3. __releases(&tasklist_lock)
    4. __acquires(&tasklist_lock)
    5. {
    6. struct pid_namespace *pid_ns = task_active_pid_ns(father);
    7. struct task_struct *reaper = pid_ns->child_reaper; // pid_ns->child_reaper记录的pid namespace的1号进程
    8. struct task_struct *p, *n;
    9. if (likely(reaper != father))
    10. return reaper;
    11. //这个father要是命名空间的1号进程,它要退出了好像问题有点严重了...还会有托孤吗?这个命名空间是继续存在还是走向毁灭呢?
    12. //内核秉承稳定第一的理念,处理起事情来总是小心翼翼。首先它想到的就是退出的father是否还有活着的兄弟姐妹,有的话赶紧扶正上位,可别让整个命名空间群龙无首呀。
    13. reaper = find_alive_thread(father);
    14. if (reaper) {
    15. pid_ns->child_reaper = reaper;
    16. return reaper;
    17. }
    18. // 1号进程退出了,也没有alive_thread了
    19. write_unlock_irq(&tasklist_lock);
    20. list_for_each_entry_safe(p, n, dead, ptrace_entry) {
    21. list_del_init(&p->ptrace_entry);
    22. release_task(p);
    23. }
    24. zap_pid_ns_processes(pid_ns);
    25. write_lock_irq(&tasklist_lock);
    26. return father;
    27. }
    1. /*
    2. * When we die, we re-parent all our children, and try to:
    3. * 1. give them to another thread in our thread group, if such a member exists
    4. * 2. give it to the first ancestor process which prctl'd itself as a
    5. * child_subreaper for its children (like a service manager)
    6. * 3. give it to the init process (PID 1) in our pid namespace
    7. */
    8. static struct task_struct *find_new_reaper(struct task_struct *father,
    9. struct task_struct *child_reaper)
    10. {
    11. struct task_struct *thread, *reaper;
    12. thread = find_alive_thread(father);
    13. if (thread)
    14. return thread;
    15. if (father->signal->has_child_subreaper) {
    16. unsigned int ns_level = task_pid(father)->level;
    17. /*
    18. * Find the first ->is_child_subreaper ancestor in our pid_ns.
    19. * We can't check reaper != child_reaper to ensure we do not
    20. * cross the namespaces, the exiting parent could be injected
    21. * by setns() + fork().
    22. * We check pid->level, this is slightly more efficient than
    23. * task_active_pid_ns(reaper) != task_active_pid_ns(father).
    24. */
    25. for (reaper = father->real_parent;
    26. task_pid(reaper)->level == ns_level;
    27. reaper = reaper->real_parent) {
    28. if (reaper == &init_task)
    29. break;
    30. if (!reaper->signal->is_child_subreaper)
    31. continue;
    32. thread = find_alive_thread(reaper);
    33. if (thread)
    34. return thread;
    35. }
    36. }
    37. return child_reaper;
    38. }