Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 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 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/ipc.h> |
| 14 | #include <linux/nsproxy.h> |
| 15 | #include <linux/sysctl.h> |
| 16 | #include <linux/uaccess.h> |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 17 | #include <linux/ipc_namespace.h> |
Nadia Derbey | 6546bc4 | 2008-04-29 01:00:45 -0700 | [diff] [blame] | 18 | #include <linux/msg.h> |
| 19 | #include "util.h" |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 20 | |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 21 | static void *get_ipc(ctl_table *table) |
| 22 | { |
| 23 | char *which = table->data; |
| 24 | struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns; |
| 25 | which = (which - (char *)&init_ipc_ns) + (char *)ipc_ns; |
| 26 | return which; |
| 27 | } |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 28 | |
Serge E. Hallyn | 11dea19 | 2009-04-02 16:58:27 -0700 | [diff] [blame] | 29 | #ifdef CONFIG_PROC_SYSCTL |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 30 | static int proc_ipc_dointvec(ctl_table *table, int write, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 31 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 32 | { |
| 33 | struct ctl_table ipc_table; |
| 34 | memcpy(&ipc_table, table, sizeof(ipc_table)); |
| 35 | ipc_table.data = get_ipc(table); |
| 36 | |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 37 | return proc_dointvec(&ipc_table, write, buffer, lenp, ppos); |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Nadia Derbey | 91cfb2b | 2008-04-29 01:00:44 -0700 | [diff] [blame] | 40 | static int proc_ipc_callback_dointvec(ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 41 | void __user *buffer, size_t *lenp, loff_t *ppos) |
Nadia Derbey | 91cfb2b | 2008-04-29 01:00:44 -0700 | [diff] [blame] | 42 | { |
Nadia Derbey | 6546bc4 | 2008-04-29 01:00:45 -0700 | [diff] [blame] | 43 | struct ctl_table ipc_table; |
Nadia Derbey | 91cfb2b | 2008-04-29 01:00:44 -0700 | [diff] [blame] | 44 | size_t lenp_bef = *lenp; |
| 45 | int rc; |
| 46 | |
Nadia Derbey | 6546bc4 | 2008-04-29 01:00:45 -0700 | [diff] [blame] | 47 | memcpy(&ipc_table, table, sizeof(ipc_table)); |
| 48 | ipc_table.data = get_ipc(table); |
| 49 | |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 50 | rc = proc_dointvec(&ipc_table, write, buffer, lenp, ppos); |
Nadia Derbey | 91cfb2b | 2008-04-29 01:00:44 -0700 | [diff] [blame] | 51 | |
| 52 | if (write && !rc && lenp_bef == *lenp) |
Nadia Derbey | 9eefe52 | 2008-07-25 01:48:08 -0700 | [diff] [blame] | 53 | /* |
| 54 | * Tunable has successfully been changed by hand. Disable its |
| 55 | * automatic adjustment. This simply requires unregistering |
| 56 | * the notifiers that trigger recalculation. |
| 57 | */ |
| 58 | unregister_ipcns_notifier(current->nsproxy->ipc_ns); |
Nadia Derbey | 91cfb2b | 2008-04-29 01:00:44 -0700 | [diff] [blame] | 59 | |
| 60 | return rc; |
| 61 | } |
| 62 | |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 63 | static int proc_ipc_doulongvec_minmax(ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 64 | void __user *buffer, size_t *lenp, loff_t *ppos) |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 65 | { |
| 66 | struct ctl_table ipc_table; |
| 67 | memcpy(&ipc_table, table, sizeof(ipc_table)); |
| 68 | ipc_table.data = get_ipc(table); |
| 69 | |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 70 | return proc_doulongvec_minmax(&ipc_table, write, buffer, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 71 | lenp, ppos); |
| 72 | } |
| 73 | |
akpm@linux-foundation.org | 4c2c3b4 | 2009-01-06 14:42:51 -0800 | [diff] [blame] | 74 | /* |
| 75 | * Routine that is called when the file "auto_msgmni" has successfully been |
| 76 | * written. |
| 77 | * Two values are allowed: |
| 78 | * 0: unregister msgmni's callback routine from the ipc namespace notifier |
| 79 | * chain. This means that msgmni won't be recomputed anymore upon memory |
| 80 | * add/remove or ipc namespace creation/removal. |
| 81 | * 1: register back the callback routine. |
| 82 | */ |
| 83 | static void ipc_auto_callback(int val) |
| 84 | { |
| 85 | if (!val) |
| 86 | unregister_ipcns_notifier(current->nsproxy->ipc_ns); |
| 87 | else { |
| 88 | /* |
| 89 | * Re-enable automatic recomputing only if not already |
| 90 | * enabled. |
| 91 | */ |
| 92 | recompute_msgmni(current->nsproxy->ipc_ns); |
| 93 | cond_register_ipcns_notifier(current->nsproxy->ipc_ns); |
| 94 | } |
| 95 | } |
| 96 | |
Nadia Derbey | 9eefe52 | 2008-07-25 01:48:08 -0700 | [diff] [blame] | 97 | static int proc_ipcauto_dointvec_minmax(ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 98 | void __user *buffer, size_t *lenp, loff_t *ppos) |
Nadia Derbey | 9eefe52 | 2008-07-25 01:48:08 -0700 | [diff] [blame] | 99 | { |
| 100 | struct ctl_table ipc_table; |
| 101 | size_t lenp_bef = *lenp; |
| 102 | int oldval; |
| 103 | int rc; |
| 104 | |
| 105 | memcpy(&ipc_table, table, sizeof(ipc_table)); |
| 106 | ipc_table.data = get_ipc(table); |
| 107 | oldval = *((int *)(ipc_table.data)); |
| 108 | |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 109 | rc = proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos); |
Nadia Derbey | 9eefe52 | 2008-07-25 01:48:08 -0700 | [diff] [blame] | 110 | |
| 111 | if (write && !rc && lenp_bef == *lenp) { |
| 112 | int newval = *((int *)(ipc_table.data)); |
| 113 | /* |
| 114 | * The file "auto_msgmni" has correctly been set. |
| 115 | * React by (un)registering the corresponding tunable, if the |
| 116 | * value has changed. |
| 117 | */ |
| 118 | if (newval != oldval) |
| 119 | ipc_auto_callback(newval); |
| 120 | } |
| 121 | |
| 122 | return rc; |
| 123 | } |
| 124 | |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 125 | #else |
| 126 | #define proc_ipc_doulongvec_minmax NULL |
| 127 | #define proc_ipc_dointvec NULL |
Nadia Derbey | 91cfb2b | 2008-04-29 01:00:44 -0700 | [diff] [blame] | 128 | #define proc_ipc_callback_dointvec NULL |
Nadia Derbey | 9eefe52 | 2008-07-25 01:48:08 -0700 | [diff] [blame] | 129 | #define proc_ipcauto_dointvec_minmax NULL |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 130 | #endif |
| 131 | |
Nadia Derbey | 9eefe52 | 2008-07-25 01:48:08 -0700 | [diff] [blame] | 132 | static int zero; |
| 133 | static int one = 1; |
| 134 | |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 135 | static struct ctl_table ipc_kern_table[] = { |
| 136 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 137 | .procname = "shmmax", |
| 138 | .data = &init_ipc_ns.shm_ctlmax, |
| 139 | .maxlen = sizeof (init_ipc_ns.shm_ctlmax), |
| 140 | .mode = 0644, |
| 141 | .proc_handler = proc_ipc_doulongvec_minmax, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 142 | }, |
| 143 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 144 | .procname = "shmall", |
| 145 | .data = &init_ipc_ns.shm_ctlall, |
| 146 | .maxlen = sizeof (init_ipc_ns.shm_ctlall), |
| 147 | .mode = 0644, |
| 148 | .proc_handler = proc_ipc_doulongvec_minmax, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 149 | }, |
| 150 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 151 | .procname = "shmmni", |
| 152 | .data = &init_ipc_ns.shm_ctlmni, |
| 153 | .maxlen = sizeof (init_ipc_ns.shm_ctlmni), |
| 154 | .mode = 0644, |
| 155 | .proc_handler = proc_ipc_dointvec, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 156 | }, |
| 157 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 158 | .procname = "msgmax", |
| 159 | .data = &init_ipc_ns.msg_ctlmax, |
| 160 | .maxlen = sizeof (init_ipc_ns.msg_ctlmax), |
| 161 | .mode = 0644, |
| 162 | .proc_handler = proc_ipc_dointvec, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 163 | }, |
| 164 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 165 | .procname = "msgmni", |
| 166 | .data = &init_ipc_ns.msg_ctlmni, |
| 167 | .maxlen = sizeof (init_ipc_ns.msg_ctlmni), |
| 168 | .mode = 0644, |
Nadia Derbey | 91cfb2b | 2008-04-29 01:00:44 -0700 | [diff] [blame] | 169 | .proc_handler = proc_ipc_callback_dointvec, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 170 | }, |
| 171 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 172 | .procname = "msgmnb", |
| 173 | .data = &init_ipc_ns.msg_ctlmnb, |
| 174 | .maxlen = sizeof (init_ipc_ns.msg_ctlmnb), |
| 175 | .mode = 0644, |
| 176 | .proc_handler = proc_ipc_dointvec, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 177 | }, |
| 178 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 179 | .procname = "sem", |
| 180 | .data = &init_ipc_ns.sem_ctls, |
| 181 | .maxlen = 4*sizeof (int), |
| 182 | .mode = 0644, |
| 183 | .proc_handler = proc_ipc_dointvec, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 184 | }, |
Nadia Derbey | 9eefe52 | 2008-07-25 01:48:08 -0700 | [diff] [blame] | 185 | { |
Nadia Derbey | 9eefe52 | 2008-07-25 01:48:08 -0700 | [diff] [blame] | 186 | .procname = "auto_msgmni", |
| 187 | .data = &init_ipc_ns.auto_msgmni, |
| 188 | .maxlen = sizeof(int), |
| 189 | .mode = 0644, |
| 190 | .proc_handler = proc_ipcauto_dointvec_minmax, |
| 191 | .extra1 = &zero, |
| 192 | .extra2 = &one, |
| 193 | }, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 194 | {} |
| 195 | }; |
| 196 | |
| 197 | static struct ctl_table ipc_root_table[] = { |
| 198 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 199 | .procname = "kernel", |
| 200 | .mode = 0555, |
| 201 | .child = ipc_kern_table, |
| 202 | }, |
| 203 | {} |
| 204 | }; |
| 205 | |
| 206 | static int __init ipc_sysctl_init(void) |
| 207 | { |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 208 | register_sysctl_table(ipc_root_table); |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | __initcall(ipc_sysctl_init); |