Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6] / ipc / ipc_sysctl.c
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>
17 #include <linux/ipc_namespace.h>
18
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
27 #ifdef CONFIG_PROC_FS
28 static int proc_ipc_dointvec(ctl_table *table, int write, struct file *filp,
29         void __user *buffer, size_t *lenp, loff_t *ppos)
30 {
31         struct ctl_table ipc_table;
32         memcpy(&ipc_table, table, sizeof(ipc_table));
33         ipc_table.data = get_ipc(table);
34
35         return proc_dointvec(&ipc_table, write, filp, buffer, lenp, ppos);
36 }
37
38 static int proc_ipc_doulongvec_minmax(ctl_table *table, int write,
39         struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos)
40 {
41         struct ctl_table ipc_table;
42         memcpy(&ipc_table, table, sizeof(ipc_table));
43         ipc_table.data = get_ipc(table);
44
45         return proc_doulongvec_minmax(&ipc_table, write, filp, buffer,
46                                         lenp, ppos);
47 }
48
49 #else
50 #define proc_ipc_doulongvec_minmax NULL
51 #define proc_ipc_dointvec          NULL
52 #endif
53
54 #ifdef CONFIG_SYSCTL_SYSCALL
55 /* The generic sysctl ipc data routine. */
56 static int sysctl_ipc_data(ctl_table *table, int __user *name, int nlen,
57                 void __user *oldval, size_t __user *oldlenp,
58                 void __user *newval, size_t newlen)
59 {
60         size_t len;
61         void *data;
62
63         /* Get out of I don't have a variable */
64         if (!table->data || !table->maxlen)
65                 return -ENOTDIR;
66
67         data = get_ipc(table);
68         if (!data)
69                 return -ENOTDIR;
70
71         if (oldval && oldlenp) {
72                 if (get_user(len, oldlenp))
73                         return -EFAULT;
74                 if (len) {
75                         if (len > table->maxlen)
76                                 len = table->maxlen;
77                         if (copy_to_user(oldval, data, len))
78                                 return -EFAULT;
79                         if (put_user(len, oldlenp))
80                                 return -EFAULT;
81                 }
82         }
83
84         if (newval && newlen) {
85                 if (newlen > table->maxlen)
86                         newlen = table->maxlen;
87
88                 if (copy_from_user(data, newval, newlen))
89                         return -EFAULT;
90         }
91         return 1;
92 }
93 #else
94 #define sysctl_ipc_data NULL
95 #endif
96
97 static struct ctl_table ipc_kern_table[] = {
98         {
99                 .ctl_name       = KERN_SHMMAX,
100                 .procname       = "shmmax",
101                 .data           = &init_ipc_ns.shm_ctlmax,
102                 .maxlen         = sizeof (init_ipc_ns.shm_ctlmax),
103                 .mode           = 0644,
104                 .proc_handler   = proc_ipc_doulongvec_minmax,
105                 .strategy       = sysctl_ipc_data,
106         },
107         {
108                 .ctl_name       = KERN_SHMALL,
109                 .procname       = "shmall",
110                 .data           = &init_ipc_ns.shm_ctlall,
111                 .maxlen         = sizeof (init_ipc_ns.shm_ctlall),
112                 .mode           = 0644,
113                 .proc_handler   = proc_ipc_doulongvec_minmax,
114                 .strategy       = sysctl_ipc_data,
115         },
116         {
117                 .ctl_name       = KERN_SHMMNI,
118                 .procname       = "shmmni",
119                 .data           = &init_ipc_ns.shm_ctlmni,
120                 .maxlen         = sizeof (init_ipc_ns.shm_ctlmni),
121                 .mode           = 0644,
122                 .proc_handler   = proc_ipc_dointvec,
123                 .strategy       = sysctl_ipc_data,
124         },
125         {
126                 .ctl_name       = KERN_MSGMAX,
127                 .procname       = "msgmax",
128                 .data           = &init_ipc_ns.msg_ctlmax,
129                 .maxlen         = sizeof (init_ipc_ns.msg_ctlmax),
130                 .mode           = 0644,
131                 .proc_handler   = proc_ipc_dointvec,
132                 .strategy       = sysctl_ipc_data,
133         },
134         {
135                 .ctl_name       = KERN_MSGMNI,
136                 .procname       = "msgmni",
137                 .data           = &init_ipc_ns.msg_ctlmni,
138                 .maxlen         = sizeof (init_ipc_ns.msg_ctlmni),
139                 .mode           = 0644,
140                 .proc_handler   = proc_ipc_dointvec,
141                 .strategy       = sysctl_ipc_data,
142         },
143         {
144                 .ctl_name       = KERN_MSGMNB,
145                 .procname       =  "msgmnb",
146                 .data           = &init_ipc_ns.msg_ctlmnb,
147                 .maxlen         = sizeof (init_ipc_ns.msg_ctlmnb),
148                 .mode           = 0644,
149                 .proc_handler   = proc_ipc_dointvec,
150                 .strategy       = sysctl_ipc_data,
151         },
152         {
153                 .ctl_name       = KERN_SEM,
154                 .procname       = "sem",
155                 .data           = &init_ipc_ns.sem_ctls,
156                 .maxlen         = 4*sizeof (int),
157                 .mode           = 0644,
158                 .proc_handler   = proc_ipc_dointvec,
159                 .strategy       = sysctl_ipc_data,
160         },
161         {}
162 };
163
164 static struct ctl_table ipc_root_table[] = {
165         {
166                 .ctl_name       = CTL_KERN,
167                 .procname       = "kernel",
168                 .mode           = 0555,
169                 .child          = ipc_kern_table,
170         },
171         {}
172 };
173
174 static int __init ipc_sysctl_init(void)
175 {
176         register_sysctl_table(ipc_root_table);
177         return 0;
178 }
179
180 __initcall(ipc_sysctl_init);