ipc_sysctl.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. 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,
  173. },
  174. {
  175. .procname = "msgmni",
  176. .data = &init_ipc_ns.msg_ctlmni,
  177. .maxlen = sizeof (init_ipc_ns.msg_ctlmni),
  178. .mode = 0644,
  179. .proc_handler = proc_ipc_callback_dointvec,
  180. },
  181. {
  182. .procname = "msgmnb",
  183. .data = &init_ipc_ns.msg_ctlmnb,
  184. .maxlen = sizeof (init_ipc_ns.msg_ctlmnb),
  185. .mode = 0644,
  186. .proc_handler = proc_ipc_dointvec,
  187. },
  188. {
  189. .procname = "sem",
  190. .data = &init_ipc_ns.sem_ctls,
  191. .maxlen = 4*sizeof (int),
  192. .mode = 0644,
  193. .proc_handler = proc_ipc_dointvec,
  194. },
  195. {
  196. .procname = "auto_msgmni",
  197. .data = &init_ipc_ns.auto_msgmni,
  198. .maxlen = sizeof(int),
  199. .mode = 0644,
  200. .proc_handler = proc_ipcauto_dointvec_minmax,
  201. .extra1 = &zero,
  202. .extra2 = &one,
  203. },
  204. {}
  205. };
  206. static struct ctl_table ipc_root_table[] = {
  207. {
  208. .procname = "kernel",
  209. .mode = 0555,
  210. .child = ipc_kern_table,
  211. },
  212. {}
  213. };
  214. static int __init ipc_sysctl_init(void)
  215. {
  216. register_sysctl_table(ipc_root_table);
  217. return 0;
  218. }
  219. __initcall(ipc_sysctl_init);