ipc_sysctl.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2007
  3. *
  4. * Author: Eric Biederman <ebiederm@xmision.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2 of the
  9. * License.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/ipc.h>
  13. #include <linux/nsproxy.h>
  14. #include <linux/sysctl.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/ipc_namespace.h>
  17. #include <linux/msg.h>
  18. #include "util.h"
  19. static void *get_ipc(ctl_table *table)
  20. {
  21. char *which = table->data;
  22. struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
  23. which = (which - (char *)&init_ipc_ns) + (char *)ipc_ns;
  24. return which;
  25. }
  26. #ifdef CONFIG_PROC_SYSCTL
  27. static int proc_ipc_dointvec(ctl_table *table, int write,
  28. void __user *buffer, size_t *lenp, loff_t *ppos)
  29. {
  30. struct ctl_table ipc_table;
  31. memcpy(&ipc_table, table, sizeof(ipc_table));
  32. ipc_table.data = get_ipc(table);
  33. return proc_dointvec(&ipc_table, write, buffer, lenp, ppos);
  34. }
  35. static int proc_ipc_dointvec_minmax(ctl_table *table, int write,
  36. void __user *buffer, size_t *lenp, loff_t *ppos)
  37. {
  38. struct ctl_table ipc_table;
  39. memcpy(&ipc_table, table, sizeof(ipc_table));
  40. ipc_table.data = get_ipc(table);
  41. return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos);
  42. }
  43. static int proc_ipc_dointvec_minmax_orphans(ctl_table *table, int write,
  44. void __user *buffer, size_t *lenp, loff_t *ppos)
  45. {
  46. struct ipc_namespace *ns = current->nsproxy->ipc_ns;
  47. int err = proc_ipc_dointvec_minmax(table, write, buffer, lenp, ppos);
  48. if (err < 0)
  49. return err;
  50. if (ns->shm_rmid_forced)
  51. shm_destroy_orphaned(ns);
  52. return err;
  53. }
  54. static int proc_ipc_callback_dointvec(ctl_table *table, int write,
  55. void __user *buffer, size_t *lenp, loff_t *ppos)
  56. {
  57. struct ctl_table ipc_table;
  58. size_t lenp_bef = *lenp;
  59. int rc;
  60. memcpy(&ipc_table, table, sizeof(ipc_table));
  61. ipc_table.data = get_ipc(table);
  62. rc = proc_dointvec(&ipc_table, write, buffer, lenp, ppos);
  63. if (write && !rc && lenp_bef == *lenp)
  64. /*
  65. * Tunable has successfully been changed by hand. Disable its
  66. * automatic adjustment. This simply requires unregistering
  67. * the notifiers that trigger recalculation.
  68. */
  69. unregister_ipcns_notifier(current->nsproxy->ipc_ns);
  70. return rc;
  71. }
  72. static int proc_ipc_doulongvec_minmax(ctl_table *table, int write,
  73. void __user *buffer, size_t *lenp, loff_t *ppos)
  74. {
  75. struct ctl_table ipc_table;
  76. memcpy(&ipc_table, table, sizeof(ipc_table));
  77. ipc_table.data = get_ipc(table);
  78. return proc_doulongvec_minmax(&ipc_table, write, buffer,
  79. lenp, ppos);
  80. }
  81. /*
  82. * Routine that is called when the file "auto_msgmni" has successfully been
  83. * written.
  84. * Two values are allowed:
  85. * 0: unregister msgmni's callback routine from the ipc namespace notifier
  86. * chain. This means that msgmni won't be recomputed anymore upon memory
  87. * add/remove or ipc namespace creation/removal.
  88. * 1: register back the callback routine.
  89. */
  90. static void ipc_auto_callback(int val)
  91. {
  92. if (!val)
  93. unregister_ipcns_notifier(current->nsproxy->ipc_ns);
  94. else {
  95. /*
  96. * Re-enable automatic recomputing only if not already
  97. * enabled.
  98. */
  99. recompute_msgmni(current->nsproxy->ipc_ns);
  100. cond_register_ipcns_notifier(current->nsproxy->ipc_ns);
  101. }
  102. }
  103. static int proc_ipcauto_dointvec_minmax(ctl_table *table, int write,
  104. void __user *buffer, size_t *lenp, loff_t *ppos)
  105. {
  106. struct ctl_table ipc_table;
  107. size_t lenp_bef = *lenp;
  108. int oldval;
  109. int rc;
  110. memcpy(&ipc_table, table, sizeof(ipc_table));
  111. ipc_table.data = get_ipc(table);
  112. oldval = *((int *)(ipc_table.data));
  113. rc = proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos);
  114. if (write && !rc && lenp_bef == *lenp) {
  115. int newval = *((int *)(ipc_table.data));
  116. /*
  117. * The file "auto_msgmni" has correctly been set.
  118. * React by (un)registering the corresponding tunable, if the
  119. * value has changed.
  120. */
  121. if (newval != oldval)
  122. ipc_auto_callback(newval);
  123. }
  124. return rc;
  125. }
  126. #else
  127. #define proc_ipc_doulongvec_minmax NULL
  128. #define proc_ipc_dointvec NULL
  129. #define proc_ipc_dointvec_minmax NULL
  130. #define proc_ipc_dointvec_minmax_orphans NULL
  131. #define proc_ipc_callback_dointvec NULL
  132. #define proc_ipcauto_dointvec_minmax NULL
  133. #endif
  134. static int zero;
  135. static int one = 1;
  136. #ifdef CONFIG_CHECKPOINT_RESTORE
  137. static int int_max = INT_MAX;
  138. #endif
  139. static struct ctl_table ipc_kern_table[] = {
  140. {
  141. .procname = "shmmax",
  142. .data = &init_ipc_ns.shm_ctlmax,
  143. .maxlen = sizeof (init_ipc_ns.shm_ctlmax),
  144. .mode = 0644,
  145. .proc_handler = proc_ipc_doulongvec_minmax,
  146. },
  147. {
  148. .procname = "shmall",
  149. .data = &init_ipc_ns.shm_ctlall,
  150. .maxlen = sizeof (init_ipc_ns.shm_ctlall),
  151. .mode = 0644,
  152. .proc_handler = proc_ipc_doulongvec_minmax,
  153. },
  154. {
  155. .procname = "shmmni",
  156. .data = &init_ipc_ns.shm_ctlmni,
  157. .maxlen = sizeof (init_ipc_ns.shm_ctlmni),
  158. .mode = 0644,
  159. .proc_handler = proc_ipc_dointvec,
  160. },
  161. {
  162. .procname = "shm_rmid_forced",
  163. .data = &init_ipc_ns.shm_rmid_forced,
  164. .maxlen = sizeof(init_ipc_ns.shm_rmid_forced),
  165. .mode = 0644,
  166. .proc_handler = proc_ipc_dointvec_minmax_orphans,
  167. .extra1 = &zero,
  168. .extra2 = &one,
  169. },
  170. {
  171. .procname = "msgmax",
  172. .data = &init_ipc_ns.msg_ctlmax,
  173. .maxlen = sizeof (init_ipc_ns.msg_ctlmax),
  174. .mode = 0644,
  175. .proc_handler = proc_ipc_dointvec,
  176. },
  177. {
  178. .procname = "msgmni",
  179. .data = &init_ipc_ns.msg_ctlmni,
  180. .maxlen = sizeof (init_ipc_ns.msg_ctlmni),
  181. .mode = 0644,
  182. .proc_handler = proc_ipc_callback_dointvec,
  183. },
  184. {
  185. .procname = "msgmnb",
  186. .data = &init_ipc_ns.msg_ctlmnb,
  187. .maxlen = sizeof (init_ipc_ns.msg_ctlmnb),
  188. .mode = 0644,
  189. .proc_handler = proc_ipc_dointvec,
  190. },
  191. {
  192. .procname = "sem",
  193. .data = &init_ipc_ns.sem_ctls,
  194. .maxlen = 4*sizeof (int),
  195. .mode = 0644,
  196. .proc_handler = proc_ipc_dointvec,
  197. },
  198. {
  199. .procname = "auto_msgmni",
  200. .data = &init_ipc_ns.auto_msgmni,
  201. .maxlen = sizeof(int),
  202. .mode = 0644,
  203. .proc_handler = proc_ipcauto_dointvec_minmax,
  204. .extra1 = &zero,
  205. .extra2 = &one,
  206. },
  207. #ifdef CONFIG_CHECKPOINT_RESTORE
  208. {
  209. .procname = "sem_next_id",
  210. .data = &init_ipc_ns.ids[IPC_SEM_IDS].next_id,
  211. .maxlen = sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id),
  212. .mode = 0644,
  213. .proc_handler = proc_ipc_dointvec_minmax,
  214. .extra1 = &zero,
  215. .extra2 = &int_max,
  216. },
  217. {
  218. .procname = "msg_next_id",
  219. .data = &init_ipc_ns.ids[IPC_MSG_IDS].next_id,
  220. .maxlen = sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id),
  221. .mode = 0644,
  222. .proc_handler = proc_ipc_dointvec_minmax,
  223. .extra1 = &zero,
  224. .extra2 = &int_max,
  225. },
  226. {
  227. .procname = "shm_next_id",
  228. .data = &init_ipc_ns.ids[IPC_SHM_IDS].next_id,
  229. .maxlen = sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id),
  230. .mode = 0644,
  231. .proc_handler = proc_ipc_dointvec_minmax,
  232. .extra1 = &zero,
  233. .extra2 = &int_max,
  234. },
  235. #endif
  236. {}
  237. };
  238. static struct ctl_table ipc_root_table[] = {
  239. {
  240. .procname = "kernel",
  241. .mode = 0555,
  242. .child = ipc_kern_table,
  243. },
  244. {}
  245. };
  246. static int __init ipc_sysctl_init(void)
  247. {
  248. register_sysctl_table(ipc_root_table);
  249. return 0;
  250. }
  251. __initcall(ipc_sysctl_init);