This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / kernel / vserver / sysctl.c
1 /*
2  *  linux/kernel/sysctl.c
3  *
4  *  Virtual Context Support
5  *
6  *  Copyright (C) 2004  Herbert Pƶtzl
7  *
8  *  V0.01  basic structure
9  *
10  */
11
12 #include <linux/config.h>
13 #include <linux/errno.h>
14 #include <linux/vserver.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/ctype.h>
18 #include <linux/sysctl.h>
19 #include <linux/fs.h>
20
21 #include <asm/uaccess.h>
22 #include <asm/unistd.h>
23
24
25 #define CTL_VSERVER     4242    /* unused? */
26
27 enum {
28         CTL_DEBUG_SWITCH = 1,
29         CTL_DEBUG_LIMIT,
30 };
31
32
33 unsigned int vx_debug_switch = 0;
34 unsigned int vx_debug_limit = 0;
35
36
37 static struct ctl_table_header *vserver_table_header;
38 static ctl_table vserver_table[];
39
40
41 void vserver_register_sysctl(void)
42 {
43         if (!vserver_table_header) {
44                 vserver_table_header = register_sysctl_table(vserver_table, 1);
45 #ifdef CONFIG_PROC_FS
46 //              if (vserver_table[0].de)
47 //                      vserver_table[0].de->owner = THIS_MODULE;
48 #endif
49         }
50                         
51 }
52
53 void vserver_unregister_sysctl(void)
54 {
55         if (vserver_table_header) {
56                 unregister_sysctl_table(vserver_table_header);
57                 vserver_table_header = NULL;
58         }
59 }
60
61
62 static int proc_dodebug(ctl_table *table, int write,
63         struct file *file, void *buffer, size_t *lenp)
64 {
65         char            tmpbuf[20], *p, c;
66         unsigned int    value;
67         size_t          left, len;
68
69         if ((file->f_pos && !write) || !*lenp) {
70                 *lenp = 0;
71                 return 0;
72         }
73
74         left = *lenp;
75
76         if (write) {
77                 if (!access_ok(VERIFY_READ, buffer, left))
78                         return -EFAULT;
79                 p = (char *) buffer;
80                 while (left && __get_user(c, p) >= 0 && isspace(c))
81                         left--, p++;
82                 if (!left)
83                         goto done;
84
85                 if (left > sizeof(tmpbuf) - 1)
86                         return -EINVAL;
87                 if (copy_from_user(tmpbuf, p, left))
88                         return -EFAULT;
89                 tmpbuf[left] = '\0';
90
91                 for (p = tmpbuf, value = 0; '0' <= *p && *p <= '9'; p++, left--)
92                         value = 10 * value + (*p - '0');
93                 if (*p && !isspace(*p))
94                         return -EINVAL;
95                 while (left && isspace(*p))
96                         left--, p++;
97                 *(unsigned int *) table->data = value;
98         } else {
99                 if (!access_ok(VERIFY_WRITE, buffer, left))
100                         return -EFAULT;
101                 len = sprintf(tmpbuf, "%d", *(unsigned int *) table->data);
102                 if (len > left)
103                         len = left;
104                 if (__copy_to_user(buffer, tmpbuf, len))
105                         return -EFAULT;
106                 if ((left -= len) > 0) {
107                         if (put_user('\n', (char *)buffer + len))
108                                 return -EFAULT;
109                         left--;
110                 }
111         }
112
113 done:
114         *lenp -= left;
115         file->f_pos += *lenp;
116         return 0;
117 }
118         
119
120
121 static ctl_table debug_table[] = {
122         {
123                 .ctl_name       = CTL_DEBUG_SWITCH,
124                 .procname       = "debug_switch",
125                 .data           = &vx_debug_switch,
126                 .maxlen         = sizeof(int),
127                 .mode           = 0644,
128                 .proc_handler   = &proc_dodebug
129         },
130         {
131                 .ctl_name       = CTL_DEBUG_LIMIT,
132                 .procname       = "debug_limit",
133                 .data           = &vx_debug_limit,
134                 .maxlen         = sizeof(int),
135                 .mode           = 0644,
136                 .proc_handler   = &proc_dodebug
137         },
138         { .ctl_name = 0 }
139 };
140
141 static ctl_table vserver_table[] = {
142         {
143                 .ctl_name       = CTL_VSERVER,
144                 .procname       = "vserver",
145                 .mode           = 0555,
146                 .child          = debug_table
147         },
148         { .ctl_name = 0 }
149 };
150