patch-2_6_7-vs1_9_1_12
[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         CTL_DEBUG_DLIMIT,
31 };
32
33
34 unsigned int vx_debug_switch = 0;
35 unsigned int vx_debug_limit = 0;
36 unsigned int vx_debug_dlimit = 0;
37
38
39 static struct ctl_table_header *vserver_table_header;
40 static ctl_table vserver_table[];
41
42
43 void vserver_register_sysctl(void)
44 {
45         if (!vserver_table_header) {
46                 vserver_table_header = register_sysctl_table(vserver_table, 1);
47 #ifdef CONFIG_PROC_FS
48 //              if (vserver_table[0].de)
49 //                      vserver_table[0].de->owner = THIS_MODULE;
50 #endif
51         }
52                         
53 }
54
55 void vserver_unregister_sysctl(void)
56 {
57         if (vserver_table_header) {
58                 unregister_sysctl_table(vserver_table_header);
59                 vserver_table_header = NULL;
60         }
61 }
62
63
64 static int proc_dodebug(ctl_table *table, int write,
65         struct file *file, void *buffer, size_t *lenp)
66 {
67         char            tmpbuf[20], *p, c;
68         unsigned int    value;
69         size_t          left, len;
70
71         if ((file->f_pos && !write) || !*lenp) {
72                 *lenp = 0;
73                 return 0;
74         }
75
76         left = *lenp;
77
78         if (write) {
79                 if (!access_ok(VERIFY_READ, buffer, left))
80                         return -EFAULT;
81                 p = (char *) buffer;
82                 while (left && __get_user(c, p) >= 0 && isspace(c))
83                         left--, p++;
84                 if (!left)
85                         goto done;
86
87                 if (left > sizeof(tmpbuf) - 1)
88                         return -EINVAL;
89                 if (copy_from_user(tmpbuf, p, left))
90                         return -EFAULT;
91                 tmpbuf[left] = '\0';
92
93                 for (p = tmpbuf, value = 0; '0' <= *p && *p <= '9'; p++, left--)
94                         value = 10 * value + (*p - '0');
95                 if (*p && !isspace(*p))
96                         return -EINVAL;
97                 while (left && isspace(*p))
98                         left--, p++;
99                 *(unsigned int *) table->data = value;
100         } else {
101                 if (!access_ok(VERIFY_WRITE, buffer, left))
102                         return -EFAULT;
103                 len = sprintf(tmpbuf, "%d", *(unsigned int *) table->data);
104                 if (len > left)
105                         len = left;
106                 if (__copy_to_user(buffer, tmpbuf, len))
107                         return -EFAULT;
108                 if ((left -= len) > 0) {
109                         if (put_user('\n', (char *)buffer + len))
110                                 return -EFAULT;
111                         left--;
112                 }
113         }
114
115 done:
116         *lenp -= left;
117         file->f_pos += *lenp;
118         return 0;
119 }
120         
121
122
123 static ctl_table debug_table[] = {
124         {
125                 .ctl_name       = CTL_DEBUG_SWITCH,
126                 .procname       = "debug_switch",
127                 .data           = &vx_debug_switch,
128                 .maxlen         = sizeof(int),
129                 .mode           = 0644,
130                 .proc_handler   = &proc_dodebug
131         },
132         {
133                 .ctl_name       = CTL_DEBUG_LIMIT,
134                 .procname       = "debug_limit",
135                 .data           = &vx_debug_limit,
136                 .maxlen         = sizeof(int),
137                 .mode           = 0644,
138                 .proc_handler   = &proc_dodebug
139         },
140         {
141                 .ctl_name       = CTL_DEBUG_DLIMIT,
142                 .procname       = "debug_dlimit",
143                 .data           = &vx_debug_dlimit,
144                 .maxlen         = sizeof(int),
145                 .mode           = 0644,
146                 .proc_handler   = &proc_dodebug
147         },
148         { .ctl_name = 0 }
149 };
150
151 static ctl_table vserver_table[] = {
152         {
153                 .ctl_name       = CTL_VSERVER,
154                 .procname       = "vserver",
155                 .mode           = 0555,
156                 .child          = debug_table
157         },
158         { .ctl_name = 0 }
159 };
160