ipc_sysctl.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_callback_dointvec(ctl_table *table, int write,
  36. void __user *buffer, size_t *lenp, loff_t *ppos)
  37. {
  38. struct ctl_table ipc_table;
  39. size_t lenp_bef = *lenp;
  40. int rc;
  41. memcpy(&ipc_table, table, sizeof(ipc_table));
  42. ipc_table.data = get_ipc(table);
  43. rc = proc_dointvec(&ipc_table, write, buffer, lenp, ppos);
  44. if (write && !rc && lenp_bef == *lenp)
  45. /*
  46. * Tunable has successfully been changed by hand. Disable its
  47. * automatic adjustment. This simply requires unregistering
  48. * the notifiers that trigger recalculation.
  49. */
  50. unregister_ipcns_notifier(current->nsproxy->ipc_ns);
  51. return rc;
  52. }
  53. static int proc_ipc_doulongvec_minmax(ctl_table *table, int write,
  54. void __user *buffer, size_t *lenp, loff_t *ppos)
  55. {
  56. struct ctl_table ipc_table;
  57. memcpy(&ipc_table, table, sizeof(ipc_table));
  58. ipc_table.data = get_ipc(table);
  59. return proc_doulongvec_minmax(&ipc_table, write, buffer,
  60. lenp, ppos);
  61. }
  62. /*
  63. * Routine that is called when the file "auto_msgmni" has successfully been
  64. * written.
  65. * Two values are allowed:
  66. * 0: unregister msgmni's callback routine from the ipc namespace notifier
  67. * chain. This means that msgmni won't be recomputed anymore upon memory
  68. * add/remove or ipc namespace creation/removal.
  69. * 1: register back the callback routine.
  70. */
  71. static void ipc_auto_callback(int val)
  72. {
  73. if (!val)
  74. unregister_ipcns_notifier(current->nsproxy->ipc_ns);
  75. else {
  76. /*
  77. * Re-enable automatic recomputing only if not already
  78. * enabled.
  79. */
  80. recompute_msgmni(current->nsproxy->ipc_ns);
  81. cond_register_ipcns_notifier(current->nsproxy->ipc_ns);
  82. }
  83. }
  84. static int proc_ipcauto_dointvec_minmax(ctl_table *table, int write,
  85. void __user *buffer, size_t *lenp, loff_t *ppos)
  86. {
  87. struct ctl_table ipc_table;
  88. size_t lenp_bef = *lenp;
  89. int oldval;
  90. int rc;
  91. memcpy(&ipc_table, table, sizeof(ipc_table));
  92. ipc_table.data = get_ipc(table);
  93. oldval = *((int *)(ipc_table.data));
  94. rc = proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos);
  95. if (write && !rc && lenp_bef == *lenp) {
  96. int newval = *((int *)(ipc_table.data));
  97. /*
  98. * The file "auto_msgmni" has correctly been set.
  99. * React by (un)registering the corresponding tunable, if the
  100. * value has changed.
  101. */
  102. if (newval != oldval)
  103. ipc_auto_callback(newval);
  104. }
  105. return rc;
  106. }
  107. #else
  108. #define proc_ipc_doulongvec_minmax NULL
  109. #define proc_ipc_dointvec NULL
  110. #define proc_ipc_callback_dointvec NULL
  111. #define proc_ipcauto_dointvec_minmax NULL
  112. #endif
  113. static int zero;
  114. static int one = 1;
  115. static struct ctl_table ipc_kern_table[] = {
  116. {
  117. .procname = "shmmax",
  118. .data = &init_ipc_ns.shm_ctlmax,
  119. .maxlen = sizeof (init_ipc_ns.shm_ctlmax),
  120. .mode = 0644,
  121. .proc_handler = proc_ipc_doulongvec_minmax,
  122. },
  123. {
  124. .procname = "shmall",
  125. .data = &init_ipc_ns.shm_ctlall,
  126. .maxlen = sizeof (init_ipc_ns.shm_ctlall),
  127. .mode = 0644,
  128. .proc_handler = proc_ipc_doulongvec_minmax,
  129. },
  130. {
  131. .procname = "shmmni",
  132. .data = &init_ipc_ns.shm_ctlmni,
  133. .maxlen = sizeof (init_ipc_ns.shm_ctlmni),
  134. .mode = 0644,
  135. .proc_handler = proc_ipc_dointvec,
  136. },
  137. {
  138. .procname = "msgmax",
  139. .data = &init_ipc_ns.msg_ctlmax,
  140. .maxlen = sizeof (init_ipc_ns.msg_ctlmax),
  141. .mode = 0644,
  142. .proc_handler = proc_ipc_dointvec,
  143. },
  144. {
  145. .procname = "msgmni",
  146. .data = &init_ipc_ns.msg_ctlmni,
  147. .maxlen = sizeof (init_ipc_ns.msg_ctlmni),
  148. .mode = 0644,
  149. .proc_handler = proc_ipc_callback_dointvec,
  150. },
  151. {
  152. .procname = "msgmnb",
  153. .data = &init_ipc_ns.msg_ctlmnb,
  154. .maxlen = sizeof (init_ipc_ns.msg_ctlmnb),
  155. .mode = 0644,
  156. .proc_handler = proc_ipc_dointvec,
  157. },
  158. {
  159. .procname = "sem",
  160. .data = &init_ipc_ns.sem_ctls,
  161. .maxlen = 4*sizeof (int),
  162. .mode = 0644,
  163. .proc_handler = proc_ipc_dointvec,
  164. },
  165. {
  166. .procname = "auto_msgmni",
  167. .data = &init_ipc_ns.auto_msgmni,
  168. .maxlen = sizeof(int),
  169. .mode = 0644,
  170. .proc_handler = proc_ipcauto_dointvec_minmax,
  171. .extra1 = &zero,
  172. .extra2 = &one,
  173. },
  174. {}
  175. };
  176. static struct ctl_table ipc_root_table[] = {
  177. {
  178. .procname = "kernel",
  179. .mode = 0555,
  180. .child = ipc_kern_table,
  181. },
  182. {}
  183. };
  184. static int __init ipc_sysctl_init(void)
  185. {
  186. register_sysctl_table(ipc_root_table);
  187. return 0;
  188. }
  189. __initcall(ipc_sysctl_init);