ipc_sysctl.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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_minmax(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_minmax(&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_minmax NULL
  132. #define proc_ipcauto_dointvec_minmax NULL
  133. #endif
  134. static int zero;
  135. static int one = 1;
  136. static int int_max = INT_MAX;
  137. static struct ctl_table ipc_kern_table[] = {
  138. {
  139. .procname = "shmmax",
  140. .data = &init_ipc_ns.shm_ctlmax,
  141. .maxlen = sizeof (init_ipc_ns.shm_ctlmax),
  142. .mode = 0644,
  143. .proc_handler = proc_ipc_doulongvec_minmax,
  144. },
  145. {
  146. .procname = "shmall",
  147. .data = &init_ipc_ns.shm_ctlall,
  148. .maxlen = sizeof (init_ipc_ns.shm_ctlall),
  149. .mode = 0644,
  150. .proc_handler = proc_ipc_doulongvec_minmax,
  151. },
  152. {
  153. .procname = "shmmni",
  154. .data = &init_ipc_ns.shm_ctlmni,
  155. .maxlen = sizeof (init_ipc_ns.shm_ctlmni),
  156. .mode = 0644,
  157. .proc_handler = proc_ipc_dointvec,
  158. },
  159. {
  160. .procname = "shm_rmid_forced",
  161. .data = &init_ipc_ns.shm_rmid_forced,
  162. .maxlen = sizeof(init_ipc_ns.shm_rmid_forced),
  163. .mode = 0644,
  164. .proc_handler = proc_ipc_dointvec_minmax_orphans,
  165. .extra1 = &zero,
  166. .extra2 = &one,
  167. },
  168. {
  169. .procname = "msgmax",
  170. .data = &init_ipc_ns.msg_ctlmax,
  171. .maxlen = sizeof (init_ipc_ns.msg_ctlmax),
  172. .mode = 0644,
  173. .proc_handler = proc_ipc_dointvec_minmax,
  174. .extra1 = &zero,
  175. .extra2 = &int_max,
  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_minmax,
  183. .extra1 = &zero,
  184. .extra2 = &int_max,
  185. },
  186. {
  187. .procname = "msgmnb",
  188. .data = &init_ipc_ns.msg_ctlmnb,
  189. .maxlen = sizeof (init_ipc_ns.msg_ctlmnb),
  190. .mode = 0644,
  191. .proc_handler = proc_ipc_dointvec_minmax,
  192. .extra1 = &zero,
  193. .extra2 = &int_max,
  194. },
  195. {
  196. .procname = "sem",
  197. .data = &init_ipc_ns.sem_ctls,
  198. .maxlen = 4*sizeof (int),
  199. .mode = 0644,
  200. .proc_handler = proc_ipc_dointvec,
  201. },
  202. {
  203. .procname = "auto_msgmni",
  204. .data = &init_ipc_ns.auto_msgmni,
  205. .maxlen = sizeof(int),
  206. .mode = 0644,
  207. .proc_handler = proc_ipcauto_dointvec_minmax,
  208. .extra1 = &zero,
  209. .extra2 = &one,
  210. },
  211. #ifdef CONFIG_CHECKPOINT_RESTORE
  212. {
  213. .procname = "sem_next_id",
  214. .data = &init_ipc_ns.ids[IPC_SEM_IDS].next_id,
  215. .maxlen = sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id),
  216. .mode = 0644,
  217. .proc_handler = proc_ipc_dointvec_minmax,
  218. .extra1 = &zero,
  219. .extra2 = &int_max,
  220. },
  221. {
  222. .procname = "msg_next_id",
  223. .data = &init_ipc_ns.ids[IPC_MSG_IDS].next_id,
  224. .maxlen = sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id),
  225. .mode = 0644,
  226. .proc_handler = proc_ipc_dointvec_minmax,
  227. .extra1 = &zero,
  228. .extra2 = &int_max,
  229. },
  230. {
  231. .procname = "shm_next_id",
  232. .data = &init_ipc_ns.ids[IPC_SHM_IDS].next_id,
  233. .maxlen = sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id),
  234. .mode = 0644,
  235. .proc_handler = proc_ipc_dointvec_minmax,
  236. .extra1 = &zero,
  237. .extra2 = &int_max,
  238. },
  239. #endif
  240. {}
  241. };
  242. static struct ctl_table ipc_root_table[] = {
  243. {
  244. .procname = "kernel",
  245. .mode = 0555,
  246. .child = ipc_kern_table,
  247. },
  248. {}
  249. };
  250. static int __init ipc_sysctl_init(void)
  251. {
  252. register_sysctl_table(ipc_root_table);
  253. return 0;
  254. }
  255. __initcall(ipc_sysctl_init);