ipc_sysctl.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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(struct 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(struct 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(struct 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(struct 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(struct 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(struct 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(struct ctl_table *table, int write,
  104. void __user *buffer, size_t *lenp, loff_t *ppos)
  105. {
  106. struct ctl_table ipc_table;
  107. int oldval;
  108. int rc;
  109. memcpy(&ipc_table, table, sizeof(ipc_table));
  110. ipc_table.data = get_ipc(table);
  111. oldval = *((int *)(ipc_table.data));
  112. rc = proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos);
  113. if (write && !rc) {
  114. int newval = *((int *)(ipc_table.data));
  115. /*
  116. * The file "auto_msgmni" has correctly been set.
  117. * React by (un)registering the corresponding tunable, if the
  118. * value has changed.
  119. */
  120. if (newval != oldval)
  121. ipc_auto_callback(newval);
  122. }
  123. return rc;
  124. }
  125. #else
  126. #define proc_ipc_doulongvec_minmax NULL
  127. #define proc_ipc_dointvec NULL
  128. #define proc_ipc_dointvec_minmax NULL
  129. #define proc_ipc_dointvec_minmax_orphans NULL
  130. #define proc_ipc_callback_dointvec_minmax NULL
  131. #define proc_ipcauto_dointvec_minmax NULL
  132. #endif
  133. static int zero;
  134. static int one = 1;
  135. static int int_max = INT_MAX;
  136. static struct ctl_table ipc_kern_table[] = {
  137. {
  138. .procname = "shmmax",
  139. .data = &init_ipc_ns.shm_ctlmax,
  140. .maxlen = sizeof(init_ipc_ns.shm_ctlmax),
  141. .mode = 0644,
  142. .proc_handler = proc_ipc_doulongvec_minmax,
  143. },
  144. {
  145. .procname = "shmall",
  146. .data = &init_ipc_ns.shm_ctlall,
  147. .maxlen = sizeof(init_ipc_ns.shm_ctlall),
  148. .mode = 0644,
  149. .proc_handler = proc_ipc_doulongvec_minmax,
  150. },
  151. {
  152. .procname = "shmmni",
  153. .data = &init_ipc_ns.shm_ctlmni,
  154. .maxlen = sizeof(init_ipc_ns.shm_ctlmni),
  155. .mode = 0644,
  156. .proc_handler = proc_ipc_dointvec,
  157. },
  158. {
  159. .procname = "shm_rmid_forced",
  160. .data = &init_ipc_ns.shm_rmid_forced,
  161. .maxlen = sizeof(init_ipc_ns.shm_rmid_forced),
  162. .mode = 0644,
  163. .proc_handler = proc_ipc_dointvec_minmax_orphans,
  164. .extra1 = &zero,
  165. .extra2 = &one,
  166. },
  167. {
  168. .procname = "msgmax",
  169. .data = &init_ipc_ns.msg_ctlmax,
  170. .maxlen = sizeof(init_ipc_ns.msg_ctlmax),
  171. .mode = 0644,
  172. .proc_handler = proc_ipc_dointvec_minmax,
  173. .extra1 = &zero,
  174. .extra2 = &int_max,
  175. },
  176. {
  177. .procname = "msgmni",
  178. .data = &init_ipc_ns.msg_ctlmni,
  179. .maxlen = sizeof(init_ipc_ns.msg_ctlmni),
  180. .mode = 0644,
  181. .proc_handler = proc_ipc_callback_dointvec_minmax,
  182. .extra1 = &zero,
  183. .extra2 = &int_max,
  184. },
  185. {
  186. .procname = "msgmnb",
  187. .data = &init_ipc_ns.msg_ctlmnb,
  188. .maxlen = sizeof(init_ipc_ns.msg_ctlmnb),
  189. .mode = 0644,
  190. .proc_handler = proc_ipc_dointvec_minmax,
  191. .extra1 = &zero,
  192. .extra2 = &int_max,
  193. },
  194. {
  195. .procname = "sem",
  196. .data = &init_ipc_ns.sem_ctls,
  197. .maxlen = 4*sizeof(int),
  198. .mode = 0644,
  199. .proc_handler = proc_ipc_dointvec,
  200. },
  201. {
  202. .procname = "auto_msgmni",
  203. .data = &init_ipc_ns.auto_msgmni,
  204. .maxlen = sizeof(int),
  205. .mode = 0644,
  206. .proc_handler = proc_ipcauto_dointvec_minmax,
  207. .extra1 = &zero,
  208. .extra2 = &one,
  209. },
  210. #ifdef CONFIG_CHECKPOINT_RESTORE
  211. {
  212. .procname = "sem_next_id",
  213. .data = &init_ipc_ns.ids[IPC_SEM_IDS].next_id,
  214. .maxlen = sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id),
  215. .mode = 0644,
  216. .proc_handler = proc_ipc_dointvec_minmax,
  217. .extra1 = &zero,
  218. .extra2 = &int_max,
  219. },
  220. {
  221. .procname = "msg_next_id",
  222. .data = &init_ipc_ns.ids[IPC_MSG_IDS].next_id,
  223. .maxlen = sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id),
  224. .mode = 0644,
  225. .proc_handler = proc_ipc_dointvec_minmax,
  226. .extra1 = &zero,
  227. .extra2 = &int_max,
  228. },
  229. {
  230. .procname = "shm_next_id",
  231. .data = &init_ipc_ns.ids[IPC_SHM_IDS].next_id,
  232. .maxlen = sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id),
  233. .mode = 0644,
  234. .proc_handler = proc_ipc_dointvec_minmax,
  235. .extra1 = &zero,
  236. .extra2 = &int_max,
  237. },
  238. #endif
  239. {}
  240. };
  241. static struct ctl_table ipc_root_table[] = {
  242. {
  243. .procname = "kernel",
  244. .mode = 0555,
  245. .child = ipc_kern_table,
  246. },
  247. {}
  248. };
  249. static int __init ipc_sysctl_init(void)
  250. {
  251. register_sysctl_table(ipc_root_table);
  252. return 0;
  253. }
  254. device_initcall(ipc_sysctl_init);