blob: d3497465cc0a5aa461dabce18b0f7af4dc537cea [file] [log] [blame]
Eric W. Biedermana5494dc2007-02-14 00:34:06 -08001/*
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 Emelyanovae5e1b22008-02-08 04:18:22 -080017#include <linux/ipc_namespace.h>
Nadia Derbey6546bc42008-04-29 01:00:45 -070018#include <linux/msg.h>
19#include "util.h"
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080020
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080021static 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. Biedermana5494dc2007-02-14 00:34:06 -080028
Nadia Derbey6546bc42008-04-29 01:00:45 -070029/*
30 * Routine that is called when a tunable has successfully been changed by
31 * hand and it has a callback routine registered on the ipc namespace notifier
32 * chain: we don't want such tunables to be recomputed anymore upon memory
33 * add/remove or ipc namespace creation/removal.
34 * They can come back to a recomputable state by being set to a <0 value.
35 */
36static void tunable_set_callback(int val)
37{
38 if (val >= 0)
39 unregister_ipcns_notifier(current->nsproxy->ipc_ns);
40 else {
41 /*
42 * Re-enable automatic recomputing only if not already
43 * enabled.
44 */
45 recompute_msgmni(current->nsproxy->ipc_ns);
46 cond_register_ipcns_notifier(current->nsproxy->ipc_ns);
47 }
48}
49
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080050#ifdef CONFIG_PROC_FS
51static int proc_ipc_dointvec(ctl_table *table, int write, struct file *filp,
52 void __user *buffer, size_t *lenp, loff_t *ppos)
53{
54 struct ctl_table ipc_table;
55 memcpy(&ipc_table, table, sizeof(ipc_table));
56 ipc_table.data = get_ipc(table);
57
58 return proc_dointvec(&ipc_table, write, filp, buffer, lenp, ppos);
59}
60
Nadia Derbey91cfb2b2008-04-29 01:00:44 -070061static int proc_ipc_callback_dointvec(ctl_table *table, int write,
62 struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos)
63{
Nadia Derbey6546bc42008-04-29 01:00:45 -070064 struct ctl_table ipc_table;
Nadia Derbey91cfb2b2008-04-29 01:00:44 -070065 size_t lenp_bef = *lenp;
66 int rc;
67
Nadia Derbey6546bc42008-04-29 01:00:45 -070068 memcpy(&ipc_table, table, sizeof(ipc_table));
69 ipc_table.data = get_ipc(table);
70
71 rc = proc_dointvec(&ipc_table, write, filp, buffer, lenp, ppos);
Nadia Derbey91cfb2b2008-04-29 01:00:44 -070072
73 if (write && !rc && lenp_bef == *lenp)
Nadia Derbey6546bc42008-04-29 01:00:45 -070074 tunable_set_callback(*((int *)(ipc_table.data)));
Nadia Derbey91cfb2b2008-04-29 01:00:44 -070075
76 return rc;
77}
78
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080079static int proc_ipc_doulongvec_minmax(ctl_table *table, int write,
80 struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos)
81{
82 struct ctl_table ipc_table;
83 memcpy(&ipc_table, table, sizeof(ipc_table));
84 ipc_table.data = get_ipc(table);
85
86 return proc_doulongvec_minmax(&ipc_table, write, filp, buffer,
87 lenp, ppos);
88}
89
90#else
91#define proc_ipc_doulongvec_minmax NULL
92#define proc_ipc_dointvec NULL
Nadia Derbey91cfb2b2008-04-29 01:00:44 -070093#define proc_ipc_callback_dointvec NULL
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080094#endif
95
96#ifdef CONFIG_SYSCTL_SYSCALL
97/* The generic sysctl ipc data routine. */
98static int sysctl_ipc_data(ctl_table *table, int __user *name, int nlen,
99 void __user *oldval, size_t __user *oldlenp,
100 void __user *newval, size_t newlen)
101{
102 size_t len;
103 void *data;
104
105 /* Get out of I don't have a variable */
106 if (!table->data || !table->maxlen)
107 return -ENOTDIR;
108
109 data = get_ipc(table);
110 if (!data)
111 return -ENOTDIR;
112
113 if (oldval && oldlenp) {
114 if (get_user(len, oldlenp))
115 return -EFAULT;
116 if (len) {
117 if (len > table->maxlen)
118 len = table->maxlen;
119 if (copy_to_user(oldval, data, len))
120 return -EFAULT;
121 if (put_user(len, oldlenp))
122 return -EFAULT;
123 }
124 }
125
126 if (newval && newlen) {
127 if (newlen > table->maxlen)
128 newlen = table->maxlen;
129
130 if (copy_from_user(data, newval, newlen))
131 return -EFAULT;
132 }
133 return 1;
134}
Nadia Derbey91cfb2b2008-04-29 01:00:44 -0700135
136static int sysctl_ipc_registered_data(ctl_table *table, int __user *name,
137 int nlen, void __user *oldval, size_t __user *oldlenp,
138 void __user *newval, size_t newlen)
139{
140 int rc;
141
142 rc = sysctl_ipc_data(table, name, nlen, oldval, oldlenp, newval,
143 newlen);
144
Nadia Derbey6546bc42008-04-29 01:00:45 -0700145 if (newval && newlen && rc > 0) {
Nadia Derbey91cfb2b2008-04-29 01:00:44 -0700146 /*
Nadia Derbey6546bc42008-04-29 01:00:45 -0700147 * Tunable has successfully been changed from userland
Nadia Derbey91cfb2b2008-04-29 01:00:44 -0700148 */
Nadia Derbey6546bc42008-04-29 01:00:45 -0700149 int *data = get_ipc(table);
150
151 tunable_set_callback(*data);
152 }
Nadia Derbey91cfb2b2008-04-29 01:00:44 -0700153
154 return rc;
155}
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800156#else
157#define sysctl_ipc_data NULL
Nadia Derbey91cfb2b2008-04-29 01:00:44 -0700158#define sysctl_ipc_registered_data NULL
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800159#endif
160
161static struct ctl_table ipc_kern_table[] = {
162 {
163 .ctl_name = KERN_SHMMAX,
164 .procname = "shmmax",
165 .data = &init_ipc_ns.shm_ctlmax,
166 .maxlen = sizeof (init_ipc_ns.shm_ctlmax),
167 .mode = 0644,
168 .proc_handler = proc_ipc_doulongvec_minmax,
169 .strategy = sysctl_ipc_data,
170 },
171 {
172 .ctl_name = KERN_SHMALL,
173 .procname = "shmall",
174 .data = &init_ipc_ns.shm_ctlall,
175 .maxlen = sizeof (init_ipc_ns.shm_ctlall),
176 .mode = 0644,
177 .proc_handler = proc_ipc_doulongvec_minmax,
178 .strategy = sysctl_ipc_data,
179 },
180 {
181 .ctl_name = KERN_SHMMNI,
182 .procname = "shmmni",
183 .data = &init_ipc_ns.shm_ctlmni,
184 .maxlen = sizeof (init_ipc_ns.shm_ctlmni),
185 .mode = 0644,
186 .proc_handler = proc_ipc_dointvec,
187 .strategy = sysctl_ipc_data,
188 },
189 {
190 .ctl_name = KERN_MSGMAX,
191 .procname = "msgmax",
192 .data = &init_ipc_ns.msg_ctlmax,
193 .maxlen = sizeof (init_ipc_ns.msg_ctlmax),
194 .mode = 0644,
195 .proc_handler = proc_ipc_dointvec,
196 .strategy = sysctl_ipc_data,
197 },
198 {
199 .ctl_name = KERN_MSGMNI,
200 .procname = "msgmni",
201 .data = &init_ipc_ns.msg_ctlmni,
202 .maxlen = sizeof (init_ipc_ns.msg_ctlmni),
203 .mode = 0644,
Nadia Derbey91cfb2b2008-04-29 01:00:44 -0700204 .proc_handler = proc_ipc_callback_dointvec,
205 .strategy = sysctl_ipc_registered_data,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800206 },
207 {
208 .ctl_name = KERN_MSGMNB,
209 .procname = "msgmnb",
210 .data = &init_ipc_ns.msg_ctlmnb,
211 .maxlen = sizeof (init_ipc_ns.msg_ctlmnb),
212 .mode = 0644,
213 .proc_handler = proc_ipc_dointvec,
214 .strategy = sysctl_ipc_data,
215 },
216 {
217 .ctl_name = KERN_SEM,
218 .procname = "sem",
219 .data = &init_ipc_ns.sem_ctls,
220 .maxlen = 4*sizeof (int),
221 .mode = 0644,
222 .proc_handler = proc_ipc_dointvec,
223 .strategy = sysctl_ipc_data,
224 },
225 {}
226};
227
228static struct ctl_table ipc_root_table[] = {
229 {
230 .ctl_name = CTL_KERN,
231 .procname = "kernel",
232 .mode = 0555,
233 .child = ipc_kern_table,
234 },
235 {}
236};
237
238static int __init ipc_sysctl_init(void)
239{
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800240 register_sysctl_table(ipc_root_table);
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800241 return 0;
242}
243
244__initcall(ipc_sysctl_init);