pid_namespace.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Pid namespaces
  3. *
  4. * Authors:
  5. * (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc.
  6. * (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM
  7. * Many thanks to Oleg Nesterov for comments and help
  8. *
  9. */
  10. #include <linux/pid.h>
  11. #include <linux/pid_namespace.h>
  12. #include <linux/syscalls.h>
  13. #include <linux/err.h>
  14. #include <linux/acct.h>
  15. #include <linux/slab.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/reboot.h>
  18. #include <linux/export.h>
  19. #define BITS_PER_PAGE (PAGE_SIZE*8)
  20. struct pid_cache {
  21. int nr_ids;
  22. char name[16];
  23. struct kmem_cache *cachep;
  24. struct list_head list;
  25. };
  26. static LIST_HEAD(pid_caches_lh);
  27. static DEFINE_MUTEX(pid_caches_mutex);
  28. static struct kmem_cache *pid_ns_cachep;
  29. /*
  30. * creates the kmem cache to allocate pids from.
  31. * @nr_ids: the number of numerical ids this pid will have to carry
  32. */
  33. static struct kmem_cache *create_pid_cachep(int nr_ids)
  34. {
  35. struct pid_cache *pcache;
  36. struct kmem_cache *cachep;
  37. mutex_lock(&pid_caches_mutex);
  38. list_for_each_entry(pcache, &pid_caches_lh, list)
  39. if (pcache->nr_ids == nr_ids)
  40. goto out;
  41. pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL);
  42. if (pcache == NULL)
  43. goto err_alloc;
  44. snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids);
  45. cachep = kmem_cache_create(pcache->name,
  46. sizeof(struct pid) + (nr_ids - 1) * sizeof(struct upid),
  47. 0, SLAB_HWCACHE_ALIGN, NULL);
  48. if (cachep == NULL)
  49. goto err_cachep;
  50. pcache->nr_ids = nr_ids;
  51. pcache->cachep = cachep;
  52. list_add(&pcache->list, &pid_caches_lh);
  53. out:
  54. mutex_unlock(&pid_caches_mutex);
  55. return pcache->cachep;
  56. err_cachep:
  57. kfree(pcache);
  58. err_alloc:
  59. mutex_unlock(&pid_caches_mutex);
  60. return NULL;
  61. }
  62. static struct pid_namespace *create_pid_namespace(struct pid_namespace *parent_pid_ns)
  63. {
  64. struct pid_namespace *ns;
  65. unsigned int level = parent_pid_ns->level + 1;
  66. int i, err = -ENOMEM;
  67. ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
  68. if (ns == NULL)
  69. goto out;
  70. ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  71. if (!ns->pidmap[0].page)
  72. goto out_free;
  73. ns->pid_cachep = create_pid_cachep(level + 1);
  74. if (ns->pid_cachep == NULL)
  75. goto out_free_map;
  76. kref_init(&ns->kref);
  77. ns->level = level;
  78. ns->parent = get_pid_ns(parent_pid_ns);
  79. set_bit(0, ns->pidmap[0].page);
  80. atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
  81. for (i = 1; i < PIDMAP_ENTRIES; i++)
  82. atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE);
  83. err = pid_ns_prepare_proc(ns);
  84. if (err)
  85. goto out_put_parent_pid_ns;
  86. return ns;
  87. out_put_parent_pid_ns:
  88. put_pid_ns(parent_pid_ns);
  89. out_free_map:
  90. kfree(ns->pidmap[0].page);
  91. out_free:
  92. kmem_cache_free(pid_ns_cachep, ns);
  93. out:
  94. return ERR_PTR(err);
  95. }
  96. static void destroy_pid_namespace(struct pid_namespace *ns)
  97. {
  98. int i;
  99. for (i = 0; i < PIDMAP_ENTRIES; i++)
  100. kfree(ns->pidmap[i].page);
  101. kmem_cache_free(pid_ns_cachep, ns);
  102. }
  103. struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns)
  104. {
  105. if (!(flags & CLONE_NEWPID))
  106. return get_pid_ns(old_ns);
  107. if (flags & (CLONE_THREAD|CLONE_PARENT))
  108. return ERR_PTR(-EINVAL);
  109. return create_pid_namespace(old_ns);
  110. }
  111. void free_pid_ns(struct kref *kref)
  112. {
  113. struct pid_namespace *ns, *parent;
  114. ns = container_of(kref, struct pid_namespace, kref);
  115. parent = ns->parent;
  116. destroy_pid_namespace(ns);
  117. if (parent != NULL)
  118. put_pid_ns(parent);
  119. }
  120. EXPORT_SYMBOL_GPL(free_pid_ns);
  121. void zap_pid_ns_processes(struct pid_namespace *pid_ns)
  122. {
  123. int nr;
  124. int rc;
  125. struct task_struct *task, *me = current;
  126. /* Ignore SIGCHLD causing any terminated children to autoreap */
  127. spin_lock_irq(&me->sighand->siglock);
  128. me->sighand->action[SIGCHLD - 1].sa.sa_handler = SIG_IGN;
  129. spin_unlock_irq(&me->sighand->siglock);
  130. /*
  131. * The last thread in the cgroup-init thread group is terminating.
  132. * Find remaining pid_ts in the namespace, signal and wait for them
  133. * to exit.
  134. *
  135. * Note: This signals each threads in the namespace - even those that
  136. * belong to the same thread group, To avoid this, we would have
  137. * to walk the entire tasklist looking a processes in this
  138. * namespace, but that could be unnecessarily expensive if the
  139. * pid namespace has just a few processes. Or we need to
  140. * maintain a tasklist for each pid namespace.
  141. *
  142. */
  143. read_lock(&tasklist_lock);
  144. nr = next_pidmap(pid_ns, 1);
  145. while (nr > 0) {
  146. rcu_read_lock();
  147. task = pid_task(find_vpid(nr), PIDTYPE_PID);
  148. if (task && !__fatal_signal_pending(task))
  149. send_sig_info(SIGKILL, SEND_SIG_FORCED, task);
  150. rcu_read_unlock();
  151. nr = next_pidmap(pid_ns, nr);
  152. }
  153. read_unlock(&tasklist_lock);
  154. /* Firstly reap the EXIT_ZOMBIE children we may have. */
  155. do {
  156. clear_thread_flag(TIF_SIGPENDING);
  157. rc = sys_wait4(-1, NULL, __WALL, NULL);
  158. } while (rc != -ECHILD);
  159. /*
  160. * sys_wait4() above can't reap the TASK_DEAD children.
  161. * Make sure they all go away, see __unhash_process().
  162. */
  163. for (;;) {
  164. bool need_wait = false;
  165. read_lock(&tasklist_lock);
  166. if (!list_empty(&current->children)) {
  167. __set_current_state(TASK_UNINTERRUPTIBLE);
  168. need_wait = true;
  169. }
  170. read_unlock(&tasklist_lock);
  171. if (!need_wait)
  172. break;
  173. schedule();
  174. }
  175. if (pid_ns->reboot)
  176. current->signal->group_exit_code = pid_ns->reboot;
  177. acct_exit_ns(pid_ns);
  178. return;
  179. }
  180. #ifdef CONFIG_CHECKPOINT_RESTORE
  181. static int pid_ns_ctl_handler(struct ctl_table *table, int write,
  182. void __user *buffer, size_t *lenp, loff_t *ppos)
  183. {
  184. struct ctl_table tmp = *table;
  185. if (write && !capable(CAP_SYS_ADMIN))
  186. return -EPERM;
  187. /*
  188. * Writing directly to ns' last_pid field is OK, since this field
  189. * is volatile in a living namespace anyway and a code writing to
  190. * it should synchronize its usage with external means.
  191. */
  192. tmp.data = &current->nsproxy->pid_ns->last_pid;
  193. return proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  194. }
  195. extern int pid_max;
  196. static int zero = 0;
  197. static struct ctl_table pid_ns_ctl_table[] = {
  198. {
  199. .procname = "ns_last_pid",
  200. .maxlen = sizeof(int),
  201. .mode = 0666, /* permissions are checked in the handler */
  202. .proc_handler = pid_ns_ctl_handler,
  203. .extra1 = &zero,
  204. .extra2 = &pid_max,
  205. },
  206. { }
  207. };
  208. static struct ctl_path kern_path[] = { { .procname = "kernel", }, { } };
  209. #endif /* CONFIG_CHECKPOINT_RESTORE */
  210. int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
  211. {
  212. if (pid_ns == &init_pid_ns)
  213. return 0;
  214. switch (cmd) {
  215. case LINUX_REBOOT_CMD_RESTART2:
  216. case LINUX_REBOOT_CMD_RESTART:
  217. pid_ns->reboot = SIGHUP;
  218. break;
  219. case LINUX_REBOOT_CMD_POWER_OFF:
  220. case LINUX_REBOOT_CMD_HALT:
  221. pid_ns->reboot = SIGINT;
  222. break;
  223. default:
  224. return -EINVAL;
  225. }
  226. read_lock(&tasklist_lock);
  227. force_sig(SIGKILL, pid_ns->child_reaper);
  228. read_unlock(&tasklist_lock);
  229. do_exit(0);
  230. /* Not reached */
  231. return 0;
  232. }
  233. static __init int pid_namespaces_init(void)
  234. {
  235. pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
  236. #ifdef CONFIG_CHECKPOINT_RESTORE
  237. register_sysctl_paths(kern_path, pid_ns_ctl_table);
  238. #endif
  239. return 0;
  240. }
  241. __initcall(pid_namespaces_init);