]> Pileus Git - ~andy/linux/blob - kernel/sysctl.c
[PATCH] proc: remove trailing blank entry from pid_entry arrays
[~andy/linux] / kernel / sysctl.c
1 /*
2  * sysctl.c: General linux system control interface
3  *
4  * Begun 24 March 1995, Stephen Tweedie
5  * Added /proc support, Dec 1995
6  * Added bdflush entry and intvec min/max checking, 2/23/96, Tom Dyas.
7  * Added hooks for /proc/sys/net (minor, minor patch), 96/4/1, Mike Shaver.
8  * Added kernel/java-{interpreter,appletviewer}, 96/5/10, Mike Shaver.
9  * Dynamic registration fixes, Stephen Tweedie.
10  * Added kswapd-interval, ctrl-alt-del, printk stuff, 1/8/97, Chris Horn.
11  * Made sysctl support optional via CONFIG_SYSCTL, 1/10/97, Chris
12  *  Horn.
13  * Added proc_doulongvec_ms_jiffies_minmax, 09/08/99, Carlos H. Bauer.
14  * Added proc_doulongvec_minmax, 09/08/99, Carlos H. Bauer.
15  * Changed linked lists to use list.h instead of lists.h, 02/24/00, Bill
16  *  Wendling.
17  * The list_for_each() macro wasn't appropriate for the sysctl loop.
18  *  Removed it and replaced it with older style, 03/23/00, Bill Wendling
19  */
20
21 #include <linux/module.h>
22 #include <linux/mm.h>
23 #include <linux/swap.h>
24 #include <linux/slab.h>
25 #include <linux/sysctl.h>
26 #include <linux/proc_fs.h>
27 #include <linux/capability.h>
28 #include <linux/ctype.h>
29 #include <linux/utsname.h>
30 #include <linux/capability.h>
31 #include <linux/smp_lock.h>
32 #include <linux/init.h>
33 #include <linux/kernel.h>
34 #include <linux/kobject.h>
35 #include <linux/net.h>
36 #include <linux/sysrq.h>
37 #include <linux/highuid.h>
38 #include <linux/writeback.h>
39 #include <linux/hugetlb.h>
40 #include <linux/security.h>
41 #include <linux/initrd.h>
42 #include <linux/times.h>
43 #include <linux/limits.h>
44 #include <linux/dcache.h>
45 #include <linux/syscalls.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/acpi.h>
48
49 #include <asm/uaccess.h>
50 #include <asm/processor.h>
51
52 extern int proc_nr_files(ctl_table *table, int write, struct file *filp,
53                      void __user *buffer, size_t *lenp, loff_t *ppos);
54
55 #ifdef CONFIG_X86
56 #include <asm/nmi.h>
57 #endif
58
59 #if defined(CONFIG_SYSCTL)
60
61 /* External variables not in a header file. */
62 extern int C_A_D;
63 extern int sysctl_overcommit_memory;
64 extern int sysctl_overcommit_ratio;
65 extern int sysctl_panic_on_oom;
66 extern int max_threads;
67 extern int sysrq_enabled;
68 extern int core_uses_pid;
69 extern int suid_dumpable;
70 extern char core_pattern[];
71 extern int cad_pid;
72 extern int pid_max;
73 extern int min_free_kbytes;
74 extern int printk_ratelimit_jiffies;
75 extern int printk_ratelimit_burst;
76 extern int pid_max_min, pid_max_max;
77 extern int sysctl_drop_caches;
78 extern int percpu_pagelist_fraction;
79 extern int compat_log;
80
81 /* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
82 static int maxolduid = 65535;
83 static int minolduid;
84 static int min_percpu_pagelist_fract = 8;
85
86 static int ngroups_max = NGROUPS_MAX;
87
88 #ifdef CONFIG_KMOD
89 extern char modprobe_path[];
90 #endif
91 #ifdef CONFIG_CHR_DEV_SG
92 extern int sg_big_buff;
93 #endif
94 #ifdef CONFIG_SYSVIPC
95 static int proc_do_ipc_string(ctl_table *table, int write, struct file *filp,
96                 void __user *buffer, size_t *lenp, loff_t *ppos);
97 #endif
98
99 #ifdef __sparc__
100 extern char reboot_command [];
101 extern int stop_a_enabled;
102 extern int scons_pwroff;
103 #endif
104
105 #ifdef __hppa__
106 extern int pwrsw_enabled;
107 extern int unaligned_enabled;
108 #endif
109
110 #ifdef CONFIG_S390
111 #ifdef CONFIG_MATHEMU
112 extern int sysctl_ieee_emulation_warnings;
113 #endif
114 extern int sysctl_userprocess_debug;
115 extern int spin_retry;
116 #endif
117
118 extern int sysctl_hz_timer;
119
120 #ifdef CONFIG_BSD_PROCESS_ACCT
121 extern int acct_parm[];
122 #endif
123
124 #ifdef CONFIG_IA64
125 extern int no_unaligned_warning;
126 #endif
127
128 #ifdef CONFIG_RT_MUTEXES
129 extern int max_lock_depth;
130 #endif
131
132 #ifdef CONFIG_SYSCTL_SYSCALL
133 static int parse_table(int __user *, int, void __user *, size_t __user *,
134                 void __user *, size_t, ctl_table *, void **);
135 #endif
136
137 static int proc_do_uts_string(ctl_table *table, int write, struct file *filp,
138                   void __user *buffer, size_t *lenp, loff_t *ppos);
139
140 static ctl_table root_table[];
141 static struct ctl_table_header root_table_header =
142         { root_table, LIST_HEAD_INIT(root_table_header.ctl_entry) };
143
144 static ctl_table kern_table[];
145 static ctl_table vm_table[];
146 static ctl_table fs_table[];
147 static ctl_table debug_table[];
148 static ctl_table dev_table[];
149 extern ctl_table random_table[];
150 #ifdef CONFIG_UNIX98_PTYS
151 extern ctl_table pty_table[];
152 #endif
153 #ifdef CONFIG_INOTIFY_USER
154 extern ctl_table inotify_table[];
155 #endif
156
157 #ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
158 int sysctl_legacy_va_layout;
159 #endif
160
161 /* /proc declarations: */
162
163 #ifdef CONFIG_PROC_SYSCTL
164
165 static ssize_t proc_readsys(struct file *, char __user *, size_t, loff_t *);
166 static ssize_t proc_writesys(struct file *, const char __user *, size_t, loff_t *);
167 static int proc_opensys(struct inode *, struct file *);
168
169 struct file_operations proc_sys_file_operations = {
170         .open           = proc_opensys,
171         .read           = proc_readsys,
172         .write          = proc_writesys,
173 };
174
175 extern struct proc_dir_entry *proc_sys_root;
176
177 static void register_proc_table(ctl_table *, struct proc_dir_entry *, void *);
178 static void unregister_proc_table(ctl_table *, struct proc_dir_entry *);
179 #endif
180
181 /* The default sysctl tables: */
182
183 static ctl_table root_table[] = {
184         {
185                 .ctl_name       = CTL_KERN,
186                 .procname       = "kernel",
187                 .mode           = 0555,
188                 .child          = kern_table,
189         },
190         {
191                 .ctl_name       = CTL_VM,
192                 .procname       = "vm",
193                 .mode           = 0555,
194                 .child          = vm_table,
195         },
196 #ifdef CONFIG_NET
197         {
198                 .ctl_name       = CTL_NET,
199                 .procname       = "net",
200                 .mode           = 0555,
201                 .child          = net_table,
202         },
203 #endif
204         {
205                 .ctl_name       = CTL_FS,
206                 .procname       = "fs",
207                 .mode           = 0555,
208                 .child          = fs_table,
209         },
210         {
211                 .ctl_name       = CTL_DEBUG,
212                 .procname       = "debug",
213                 .mode           = 0555,
214                 .child          = debug_table,
215         },
216         {
217                 .ctl_name       = CTL_DEV,
218                 .procname       = "dev",
219                 .mode           = 0555,
220                 .child          = dev_table,
221         },
222
223         { .ctl_name = 0 }
224 };
225
226 static ctl_table kern_table[] = {
227 #ifndef CONFIG_UTS_NS
228         {
229                 .ctl_name       = KERN_OSTYPE,
230                 .procname       = "ostype",
231                 .data           = init_uts_ns.name.sysname,
232                 .maxlen         = sizeof(init_uts_ns.name.sysname),
233                 .mode           = 0444,
234                 .proc_handler   = &proc_do_uts_string,
235                 .strategy       = &sysctl_string,
236         },
237         {
238                 .ctl_name       = KERN_OSRELEASE,
239                 .procname       = "osrelease",
240                 .data           = init_uts_ns.name.release,
241                 .maxlen         = sizeof(init_uts_ns.name.release),
242                 .mode           = 0444,
243                 .proc_handler   = &proc_do_uts_string,
244                 .strategy       = &sysctl_string,
245         },
246         {
247                 .ctl_name       = KERN_VERSION,
248                 .procname       = "version",
249                 .data           = init_uts_ns.name.version,
250                 .maxlen         = sizeof(init_uts_ns.name.version),
251                 .mode           = 0444,
252                 .proc_handler   = &proc_do_uts_string,
253                 .strategy       = &sysctl_string,
254         },
255         {
256                 .ctl_name       = KERN_NODENAME,
257                 .procname       = "hostname",
258                 .data           = init_uts_ns.name.nodename,
259                 .maxlen         = sizeof(init_uts_ns.name.nodename),
260                 .mode           = 0644,
261                 .proc_handler   = &proc_do_uts_string,
262                 .strategy       = &sysctl_string,
263         },
264         {
265                 .ctl_name       = KERN_DOMAINNAME,
266                 .procname       = "domainname",
267                 .data           = init_uts_ns.name.domainname,
268                 .maxlen         = sizeof(init_uts_ns.name.domainname),
269                 .mode           = 0644,
270                 .proc_handler   = &proc_do_uts_string,
271                 .strategy       = &sysctl_string,
272         },
273 #else  /* !CONFIG_UTS_NS */
274         {
275                 .ctl_name       = KERN_OSTYPE,
276                 .procname       = "ostype",
277                 .data           = NULL,
278                 /* could maybe use __NEW_UTS_LEN here? */
279                 .maxlen         = FIELD_SIZEOF(struct new_utsname, sysname),
280                 .mode           = 0444,
281                 .proc_handler   = &proc_do_uts_string,
282                 .strategy       = &sysctl_string,
283         },
284         {
285                 .ctl_name       = KERN_OSRELEASE,
286                 .procname       = "osrelease",
287                 .data           = NULL,
288                 .maxlen         = FIELD_SIZEOF(struct new_utsname, release),
289                 .mode           = 0444,
290                 .proc_handler   = &proc_do_uts_string,
291                 .strategy       = &sysctl_string,
292         },
293         {
294                 .ctl_name       = KERN_VERSION,
295                 .procname       = "version",
296                 .data           = NULL,
297                 .maxlen         = FIELD_SIZEOF(struct new_utsname, version),
298                 .mode           = 0444,
299                 .proc_handler   = &proc_do_uts_string,
300                 .strategy       = &sysctl_string,
301         },
302         {
303                 .ctl_name       = KERN_NODENAME,
304                 .procname       = "hostname",
305                 .data           = NULL,
306                 .maxlen         = FIELD_SIZEOF(struct new_utsname, nodename),
307                 .mode           = 0644,
308                 .proc_handler   = &proc_do_uts_string,
309                 .strategy       = &sysctl_string,
310         },
311         {
312                 .ctl_name       = KERN_DOMAINNAME,
313                 .procname       = "domainname",
314                 .data           = NULL,
315                 .maxlen         = FIELD_SIZEOF(struct new_utsname, domainname),
316                 .mode           = 0644,
317                 .proc_handler   = &proc_do_uts_string,
318                 .strategy       = &sysctl_string,
319         },
320 #endif /* !CONFIG_UTS_NS */
321         {
322                 .ctl_name       = KERN_PANIC,
323                 .procname       = "panic",
324                 .data           = &panic_timeout,
325                 .maxlen         = sizeof(int),
326                 .mode           = 0644,
327                 .proc_handler   = &proc_dointvec,
328         },
329         {
330                 .ctl_name       = KERN_CORE_USES_PID,
331                 .procname       = "core_uses_pid",
332                 .data           = &core_uses_pid,
333                 .maxlen         = sizeof(int),
334                 .mode           = 0644,
335                 .proc_handler   = &proc_dointvec,
336         },
337         {
338                 .ctl_name       = KERN_CORE_PATTERN,
339                 .procname       = "core_pattern",
340                 .data           = core_pattern,
341                 .maxlen         = 128,
342                 .mode           = 0644,
343                 .proc_handler   = &proc_dostring,
344                 .strategy       = &sysctl_string,
345         },
346         {
347                 .ctl_name       = KERN_TAINTED,
348                 .procname       = "tainted",
349                 .data           = &tainted,
350                 .maxlen         = sizeof(int),
351                 .mode           = 0444,
352                 .proc_handler   = &proc_dointvec,
353         },
354         {
355                 .ctl_name       = KERN_CAP_BSET,
356                 .procname       = "cap-bound",
357                 .data           = &cap_bset,
358                 .maxlen         = sizeof(kernel_cap_t),
359                 .mode           = 0600,
360                 .proc_handler   = &proc_dointvec_bset,
361         },
362 #ifdef CONFIG_BLK_DEV_INITRD
363         {
364                 .ctl_name       = KERN_REALROOTDEV,
365                 .procname       = "real-root-dev",
366                 .data           = &real_root_dev,
367                 .maxlen         = sizeof(int),
368                 .mode           = 0644,
369                 .proc_handler   = &proc_dointvec,
370         },
371 #endif
372 #ifdef __sparc__
373         {
374                 .ctl_name       = KERN_SPARC_REBOOT,
375                 .procname       = "reboot-cmd",
376                 .data           = reboot_command,
377                 .maxlen         = 256,
378                 .mode           = 0644,
379                 .proc_handler   = &proc_dostring,
380                 .strategy       = &sysctl_string,
381         },
382         {
383                 .ctl_name       = KERN_SPARC_STOP_A,
384                 .procname       = "stop-a",
385                 .data           = &stop_a_enabled,
386                 .maxlen         = sizeof (int),
387                 .mode           = 0644,
388                 .proc_handler   = &proc_dointvec,
389         },
390         {
391                 .ctl_name       = KERN_SPARC_SCONS_PWROFF,
392                 .procname       = "scons-poweroff",
393                 .data           = &scons_pwroff,
394                 .maxlen         = sizeof (int),
395                 .mode           = 0644,
396                 .proc_handler   = &proc_dointvec,
397         },
398 #endif
399 #ifdef __hppa__
400         {
401                 .ctl_name       = KERN_HPPA_PWRSW,
402                 .procname       = "soft-power",
403                 .data           = &pwrsw_enabled,
404                 .maxlen         = sizeof (int),
405                 .mode           = 0644,
406                 .proc_handler   = &proc_dointvec,
407         },
408         {
409                 .ctl_name       = KERN_HPPA_UNALIGNED,
410                 .procname       = "unaligned-trap",
411                 .data           = &unaligned_enabled,
412                 .maxlen         = sizeof (int),
413                 .mode           = 0644,
414                 .proc_handler   = &proc_dointvec,
415         },
416 #endif
417         {
418                 .ctl_name       = KERN_CTLALTDEL,
419                 .procname       = "ctrl-alt-del",
420                 .data           = &C_A_D,
421                 .maxlen         = sizeof(int),
422                 .mode           = 0644,
423                 .proc_handler   = &proc_dointvec,
424         },
425         {
426                 .ctl_name       = KERN_PRINTK,
427                 .procname       = "printk",
428                 .data           = &console_loglevel,
429                 .maxlen         = 4*sizeof(int),
430                 .mode           = 0644,
431                 .proc_handler   = &proc_dointvec,
432         },
433 #ifdef CONFIG_KMOD
434         {
435                 .ctl_name       = KERN_MODPROBE,
436                 .procname       = "modprobe",
437                 .data           = &modprobe_path,
438                 .maxlen         = KMOD_PATH_LEN,
439                 .mode           = 0644,
440                 .proc_handler   = &proc_dostring,
441                 .strategy       = &sysctl_string,
442         },
443 #endif
444 #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
445         {
446                 .ctl_name       = KERN_HOTPLUG,
447                 .procname       = "hotplug",
448                 .data           = &uevent_helper,
449                 .maxlen         = UEVENT_HELPER_PATH_LEN,
450                 .mode           = 0644,
451                 .proc_handler   = &proc_dostring,
452                 .strategy       = &sysctl_string,
453         },
454 #endif
455 #ifdef CONFIG_CHR_DEV_SG
456         {
457                 .ctl_name       = KERN_SG_BIG_BUFF,
458                 .procname       = "sg-big-buff",
459                 .data           = &sg_big_buff,
460                 .maxlen         = sizeof (int),
461                 .mode           = 0444,
462                 .proc_handler   = &proc_dointvec,
463         },
464 #endif
465 #ifdef CONFIG_BSD_PROCESS_ACCT
466         {
467                 .ctl_name       = KERN_ACCT,
468                 .procname       = "acct",
469                 .data           = &acct_parm,
470                 .maxlen         = 3*sizeof(int),
471                 .mode           = 0644,
472                 .proc_handler   = &proc_dointvec,
473         },
474 #endif
475 #ifdef CONFIG_SYSVIPC
476         {
477                 .ctl_name       = KERN_SHMMAX,
478                 .procname       = "shmmax",
479                 .data           = NULL,
480                 .maxlen         = sizeof (size_t),
481                 .mode           = 0644,
482                 .proc_handler   = &proc_do_ipc_string,
483         },
484         {
485                 .ctl_name       = KERN_SHMALL,
486                 .procname       = "shmall",
487                 .data           = NULL,
488                 .maxlen         = sizeof (size_t),
489                 .mode           = 0644,
490                 .proc_handler   = &proc_do_ipc_string,
491         },
492         {
493                 .ctl_name       = KERN_SHMMNI,
494                 .procname       = "shmmni",
495                 .data           = NULL,
496                 .maxlen         = sizeof (int),
497                 .mode           = 0644,
498                 .proc_handler   = &proc_do_ipc_string,
499         },
500         {
501                 .ctl_name       = KERN_MSGMAX,
502                 .procname       = "msgmax",
503                 .data           = NULL,
504                 .maxlen         = sizeof (int),
505                 .mode           = 0644,
506                 .proc_handler   = &proc_do_ipc_string,
507         },
508         {
509                 .ctl_name       = KERN_MSGMNI,
510                 .procname       = "msgmni",
511                 .data           = NULL,
512                 .maxlen         = sizeof (int),
513                 .mode           = 0644,
514                 .proc_handler   = &proc_do_ipc_string,
515         },
516         {
517                 .ctl_name       = KERN_MSGMNB,
518                 .procname       =  "msgmnb",
519                 .data           = NULL,
520                 .maxlen         = sizeof (int),
521                 .mode           = 0644,
522                 .proc_handler   = &proc_do_ipc_string,
523         },
524         {
525                 .ctl_name       = KERN_SEM,
526                 .procname       = "sem",
527                 .data           = NULL,
528                 .maxlen         = 4*sizeof (int),
529                 .mode           = 0644,
530                 .proc_handler   = &proc_do_ipc_string,
531         },
532 #endif
533 #ifdef CONFIG_MAGIC_SYSRQ
534         {
535                 .ctl_name       = KERN_SYSRQ,
536                 .procname       = "sysrq",
537                 .data           = &sysrq_enabled,
538                 .maxlen         = sizeof (int),
539                 .mode           = 0644,
540                 .proc_handler   = &proc_dointvec,
541         },
542 #endif
543         {
544                 .ctl_name       = KERN_CADPID,
545                 .procname       = "cad_pid",
546                 .data           = &cad_pid,
547                 .maxlen         = sizeof (int),
548                 .mode           = 0600,
549                 .proc_handler   = &proc_dointvec,
550         },
551         {
552                 .ctl_name       = KERN_MAX_THREADS,
553                 .procname       = "threads-max",
554                 .data           = &max_threads,
555                 .maxlen         = sizeof(int),
556                 .mode           = 0644,
557                 .proc_handler   = &proc_dointvec,
558         },
559         {
560                 .ctl_name       = KERN_RANDOM,
561                 .procname       = "random",
562                 .mode           = 0555,
563                 .child          = random_table,
564         },
565 #ifdef CONFIG_UNIX98_PTYS
566         {
567                 .ctl_name       = KERN_PTY,
568                 .procname       = "pty",
569                 .mode           = 0555,
570                 .child          = pty_table,
571         },
572 #endif
573         {
574                 .ctl_name       = KERN_OVERFLOWUID,
575                 .procname       = "overflowuid",
576                 .data           = &overflowuid,
577                 .maxlen         = sizeof(int),
578                 .mode           = 0644,
579                 .proc_handler   = &proc_dointvec_minmax,
580                 .strategy       = &sysctl_intvec,
581                 .extra1         = &minolduid,
582                 .extra2         = &maxolduid,
583         },
584         {
585                 .ctl_name       = KERN_OVERFLOWGID,
586                 .procname       = "overflowgid",
587                 .data           = &overflowgid,
588                 .maxlen         = sizeof(int),
589                 .mode           = 0644,
590                 .proc_handler   = &proc_dointvec_minmax,
591                 .strategy       = &sysctl_intvec,
592                 .extra1         = &minolduid,
593                 .extra2         = &maxolduid,
594         },
595 #ifdef CONFIG_S390
596 #ifdef CONFIG_MATHEMU
597         {
598                 .ctl_name       = KERN_IEEE_EMULATION_WARNINGS,
599                 .procname       = "ieee_emulation_warnings",
600                 .data           = &sysctl_ieee_emulation_warnings,
601                 .maxlen         = sizeof(int),
602                 .mode           = 0644,
603                 .proc_handler   = &proc_dointvec,
604         },
605 #endif
606 #ifdef CONFIG_NO_IDLE_HZ
607         {
608                 .ctl_name       = KERN_HZ_TIMER,
609                 .procname       = "hz_timer",
610                 .data           = &sysctl_hz_timer,
611                 .maxlen         = sizeof(int),
612                 .mode           = 0644,
613                 .proc_handler   = &proc_dointvec,
614         },
615 #endif
616         {
617                 .ctl_name       = KERN_S390_USER_DEBUG_LOGGING,
618                 .procname       = "userprocess_debug",
619                 .data           = &sysctl_userprocess_debug,
620                 .maxlen         = sizeof(int),
621                 .mode           = 0644,
622                 .proc_handler   = &proc_dointvec,
623         },
624 #endif
625         {
626                 .ctl_name       = KERN_PIDMAX,
627                 .procname       = "pid_max",
628                 .data           = &pid_max,
629                 .maxlen         = sizeof (int),
630                 .mode           = 0644,
631                 .proc_handler   = &proc_dointvec_minmax,
632                 .strategy       = sysctl_intvec,
633                 .extra1         = &pid_max_min,
634                 .extra2         = &pid_max_max,
635         },
636         {
637                 .ctl_name       = KERN_PANIC_ON_OOPS,
638                 .procname       = "panic_on_oops",
639                 .data           = &panic_on_oops,
640                 .maxlen         = sizeof(int),
641                 .mode           = 0644,
642                 .proc_handler   = &proc_dointvec,
643         },
644         {
645                 .ctl_name       = KERN_PRINTK_RATELIMIT,
646                 .procname       = "printk_ratelimit",
647                 .data           = &printk_ratelimit_jiffies,
648                 .maxlen         = sizeof(int),
649                 .mode           = 0644,
650                 .proc_handler   = &proc_dointvec_jiffies,
651                 .strategy       = &sysctl_jiffies,
652         },
653         {
654                 .ctl_name       = KERN_PRINTK_RATELIMIT_BURST,
655                 .procname       = "printk_ratelimit_burst",
656                 .data           = &printk_ratelimit_burst,
657                 .maxlen         = sizeof(int),
658                 .mode           = 0644,
659                 .proc_handler   = &proc_dointvec,
660         },
661         {
662                 .ctl_name       = KERN_NGROUPS_MAX,
663                 .procname       = "ngroups_max",
664                 .data           = &ngroups_max,
665                 .maxlen         = sizeof (int),
666                 .mode           = 0444,
667                 .proc_handler   = &proc_dointvec,
668         },
669 #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86)
670         {
671                 .ctl_name       = KERN_UNKNOWN_NMI_PANIC,
672                 .procname       = "unknown_nmi_panic",
673                 .data           = &unknown_nmi_panic,
674                 .maxlen         = sizeof (int),
675                 .mode           = 0644,
676                 .proc_handler   = &proc_dointvec,
677         },
678         {
679                 .ctl_name       = KERN_NMI_WATCHDOG,
680                 .procname       = "nmi_watchdog",
681                 .data           = &nmi_watchdog_enabled,
682                 .maxlen         = sizeof (int),
683                 .mode           = 0644,
684                 .proc_handler   = &proc_nmi_enabled,
685         },
686 #endif
687 #if defined(CONFIG_X86)
688         {
689                 .ctl_name       = KERN_PANIC_ON_NMI,
690                 .procname       = "panic_on_unrecovered_nmi",
691                 .data           = &panic_on_unrecovered_nmi,
692                 .maxlen         = sizeof(int),
693                 .mode           = 0644,
694                 .proc_handler   = &proc_dointvec,
695         },
696         {
697                 .ctl_name       = KERN_BOOTLOADER_TYPE,
698                 .procname       = "bootloader_type",
699                 .data           = &bootloader_type,
700                 .maxlen         = sizeof (int),
701                 .mode           = 0444,
702                 .proc_handler   = &proc_dointvec,
703         },
704 #endif
705 #if defined(CONFIG_MMU)
706         {
707                 .ctl_name       = KERN_RANDOMIZE,
708                 .procname       = "randomize_va_space",
709                 .data           = &randomize_va_space,
710                 .maxlen         = sizeof(int),
711                 .mode           = 0644,
712                 .proc_handler   = &proc_dointvec,
713         },
714 #endif
715 #if defined(CONFIG_S390) && defined(CONFIG_SMP)
716         {
717                 .ctl_name       = KERN_SPIN_RETRY,
718                 .procname       = "spin_retry",
719                 .data           = &spin_retry,
720                 .maxlen         = sizeof (int),
721                 .mode           = 0644,
722                 .proc_handler   = &proc_dointvec,
723         },
724 #endif
725 #ifdef CONFIG_ACPI_SLEEP
726         {
727                 .ctl_name       = KERN_ACPI_VIDEO_FLAGS,
728                 .procname       = "acpi_video_flags",
729                 .data           = &acpi_video_flags,
730                 .maxlen         = sizeof (unsigned long),
731                 .mode           = 0644,
732                 .proc_handler   = &proc_doulongvec_minmax,
733         },
734 #endif
735 #ifdef CONFIG_IA64
736         {
737                 .ctl_name       = KERN_IA64_UNALIGNED,
738                 .procname       = "ignore-unaligned-usertrap",
739                 .data           = &no_unaligned_warning,
740                 .maxlen         = sizeof (int),
741                 .mode           = 0644,
742                 .proc_handler   = &proc_dointvec,
743         },
744 #endif
745 #ifdef CONFIG_COMPAT
746         {
747                 .ctl_name       = KERN_COMPAT_LOG,
748                 .procname       = "compat-log",
749                 .data           = &compat_log,
750                 .maxlen         = sizeof (int),
751                 .mode           = 0644,
752                 .proc_handler   = &proc_dointvec,
753         },
754 #endif
755 #ifdef CONFIG_RT_MUTEXES
756         {
757                 .ctl_name       = KERN_MAX_LOCK_DEPTH,
758                 .procname       = "max_lock_depth",
759                 .data           = &max_lock_depth,
760                 .maxlen         = sizeof(int),
761                 .mode           = 0644,
762                 .proc_handler   = &proc_dointvec,
763         },
764 #endif
765
766         { .ctl_name = 0 }
767 };
768
769 /* Constants for minimum and maximum testing in vm_table.
770    We use these as one-element integer vectors. */
771 static int zero;
772 static int one_hundred = 100;
773
774
775 static ctl_table vm_table[] = {
776         {
777                 .ctl_name       = VM_OVERCOMMIT_MEMORY,
778                 .procname       = "overcommit_memory",
779                 .data           = &sysctl_overcommit_memory,
780                 .maxlen         = sizeof(sysctl_overcommit_memory),
781                 .mode           = 0644,
782                 .proc_handler   = &proc_dointvec,
783         },
784         {
785                 .ctl_name       = VM_PANIC_ON_OOM,
786                 .procname       = "panic_on_oom",
787                 .data           = &sysctl_panic_on_oom,
788                 .maxlen         = sizeof(sysctl_panic_on_oom),
789                 .mode           = 0644,
790                 .proc_handler   = &proc_dointvec,
791         },
792         {
793                 .ctl_name       = VM_OVERCOMMIT_RATIO,
794                 .procname       = "overcommit_ratio",
795                 .data           = &sysctl_overcommit_ratio,
796                 .maxlen         = sizeof(sysctl_overcommit_ratio),
797                 .mode           = 0644,
798                 .proc_handler   = &proc_dointvec,
799         },
800         {
801                 .ctl_name       = VM_PAGE_CLUSTER,
802                 .procname       = "page-cluster", 
803                 .data           = &page_cluster,
804                 .maxlen         = sizeof(int),
805                 .mode           = 0644,
806                 .proc_handler   = &proc_dointvec,
807         },
808         {
809                 .ctl_name       = VM_DIRTY_BACKGROUND,
810                 .procname       = "dirty_background_ratio",
811                 .data           = &dirty_background_ratio,
812                 .maxlen         = sizeof(dirty_background_ratio),
813                 .mode           = 0644,
814                 .proc_handler   = &proc_dointvec_minmax,
815                 .strategy       = &sysctl_intvec,
816                 .extra1         = &zero,
817                 .extra2         = &one_hundred,
818         },
819         {
820                 .ctl_name       = VM_DIRTY_RATIO,
821                 .procname       = "dirty_ratio",
822                 .data           = &vm_dirty_ratio,
823                 .maxlen         = sizeof(vm_dirty_ratio),
824                 .mode           = 0644,
825                 .proc_handler   = &proc_dointvec_minmax,
826                 .strategy       = &sysctl_intvec,
827                 .extra1         = &zero,
828                 .extra2         = &one_hundred,
829         },
830         {
831                 .ctl_name       = VM_DIRTY_WB_CS,
832                 .procname       = "dirty_writeback_centisecs",
833                 .data           = &dirty_writeback_interval,
834                 .maxlen         = sizeof(dirty_writeback_interval),
835                 .mode           = 0644,
836                 .proc_handler   = &dirty_writeback_centisecs_handler,
837         },
838         {
839                 .ctl_name       = VM_DIRTY_EXPIRE_CS,
840                 .procname       = "dirty_expire_centisecs",
841                 .data           = &dirty_expire_interval,
842                 .maxlen         = sizeof(dirty_expire_interval),
843                 .mode           = 0644,
844                 .proc_handler   = &proc_dointvec_userhz_jiffies,
845         },
846         {
847                 .ctl_name       = VM_NR_PDFLUSH_THREADS,
848                 .procname       = "nr_pdflush_threads",
849                 .data           = &nr_pdflush_threads,
850                 .maxlen         = sizeof nr_pdflush_threads,
851                 .mode           = 0444 /* read-only*/,
852                 .proc_handler   = &proc_dointvec,
853         },
854         {
855                 .ctl_name       = VM_SWAPPINESS,
856                 .procname       = "swappiness",
857                 .data           = &vm_swappiness,
858                 .maxlen         = sizeof(vm_swappiness),
859                 .mode           = 0644,
860                 .proc_handler   = &proc_dointvec_minmax,
861                 .strategy       = &sysctl_intvec,
862                 .extra1         = &zero,
863                 .extra2         = &one_hundred,
864         },
865 #ifdef CONFIG_HUGETLB_PAGE
866          {
867                 .ctl_name       = VM_HUGETLB_PAGES,
868                 .procname       = "nr_hugepages",
869                 .data           = &max_huge_pages,
870                 .maxlen         = sizeof(unsigned long),
871                 .mode           = 0644,
872                 .proc_handler   = &hugetlb_sysctl_handler,
873                 .extra1         = (void *)&hugetlb_zero,
874                 .extra2         = (void *)&hugetlb_infinity,
875          },
876          {
877                 .ctl_name       = VM_HUGETLB_GROUP,
878                 .procname       = "hugetlb_shm_group",
879                 .data           = &sysctl_hugetlb_shm_group,
880                 .maxlen         = sizeof(gid_t),
881                 .mode           = 0644,
882                 .proc_handler   = &proc_dointvec,
883          },
884 #endif
885         {
886                 .ctl_name       = VM_LOWMEM_RESERVE_RATIO,
887                 .procname       = "lowmem_reserve_ratio",
888                 .data           = &sysctl_lowmem_reserve_ratio,
889                 .maxlen         = sizeof(sysctl_lowmem_reserve_ratio),
890                 .mode           = 0644,
891                 .proc_handler   = &lowmem_reserve_ratio_sysctl_handler,
892                 .strategy       = &sysctl_intvec,
893         },
894         {
895                 .ctl_name       = VM_DROP_PAGECACHE,
896                 .procname       = "drop_caches",
897                 .data           = &sysctl_drop_caches,
898                 .maxlen         = sizeof(int),
899                 .mode           = 0644,
900                 .proc_handler   = drop_caches_sysctl_handler,
901                 .strategy       = &sysctl_intvec,
902         },
903         {
904                 .ctl_name       = VM_MIN_FREE_KBYTES,
905                 .procname       = "min_free_kbytes",
906                 .data           = &min_free_kbytes,
907                 .maxlen         = sizeof(min_free_kbytes),
908                 .mode           = 0644,
909                 .proc_handler   = &min_free_kbytes_sysctl_handler,
910                 .strategy       = &sysctl_intvec,
911                 .extra1         = &zero,
912         },
913         {
914                 .ctl_name       = VM_PERCPU_PAGELIST_FRACTION,
915                 .procname       = "percpu_pagelist_fraction",
916                 .data           = &percpu_pagelist_fraction,
917                 .maxlen         = sizeof(percpu_pagelist_fraction),
918                 .mode           = 0644,
919                 .proc_handler   = &percpu_pagelist_fraction_sysctl_handler,
920                 .strategy       = &sysctl_intvec,
921                 .extra1         = &min_percpu_pagelist_fract,
922         },
923 #ifdef CONFIG_MMU
924         {
925                 .ctl_name       = VM_MAX_MAP_COUNT,
926                 .procname       = "max_map_count",
927                 .data           = &sysctl_max_map_count,
928                 .maxlen         = sizeof(sysctl_max_map_count),
929                 .mode           = 0644,
930                 .proc_handler   = &proc_dointvec
931         },
932 #endif
933         {
934                 .ctl_name       = VM_LAPTOP_MODE,
935                 .procname       = "laptop_mode",
936                 .data           = &laptop_mode,
937                 .maxlen         = sizeof(laptop_mode),
938                 .mode           = 0644,
939                 .proc_handler   = &proc_dointvec_jiffies,
940                 .strategy       = &sysctl_jiffies,
941         },
942         {
943                 .ctl_name       = VM_BLOCK_DUMP,
944                 .procname       = "block_dump",
945                 .data           = &block_dump,
946                 .maxlen         = sizeof(block_dump),
947                 .mode           = 0644,
948                 .proc_handler   = &proc_dointvec,
949                 .strategy       = &sysctl_intvec,
950                 .extra1         = &zero,
951         },
952         {
953                 .ctl_name       = VM_VFS_CACHE_PRESSURE,
954                 .procname       = "vfs_cache_pressure",
955                 .data           = &sysctl_vfs_cache_pressure,
956                 .maxlen         = sizeof(sysctl_vfs_cache_pressure),
957                 .mode           = 0644,
958                 .proc_handler   = &proc_dointvec,
959                 .strategy       = &sysctl_intvec,
960                 .extra1         = &zero,
961         },
962 #ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
963         {
964                 .ctl_name       = VM_LEGACY_VA_LAYOUT,
965                 .procname       = "legacy_va_layout",
966                 .data           = &sysctl_legacy_va_layout,
967                 .maxlen         = sizeof(sysctl_legacy_va_layout),
968                 .mode           = 0644,
969                 .proc_handler   = &proc_dointvec,
970                 .strategy       = &sysctl_intvec,
971                 .extra1         = &zero,
972         },
973 #endif
974 #ifdef CONFIG_SWAP
975         {
976                 .ctl_name       = VM_SWAP_TOKEN_TIMEOUT,
977                 .procname       = "swap_token_timeout",
978                 .data           = &swap_token_default_timeout,
979                 .maxlen         = sizeof(swap_token_default_timeout),
980                 .mode           = 0644,
981                 .proc_handler   = &proc_dointvec_jiffies,
982                 .strategy       = &sysctl_jiffies,
983         },
984 #endif
985 #ifdef CONFIG_NUMA
986         {
987                 .ctl_name       = VM_ZONE_RECLAIM_MODE,
988                 .procname       = "zone_reclaim_mode",
989                 .data           = &zone_reclaim_mode,
990                 .maxlen         = sizeof(zone_reclaim_mode),
991                 .mode           = 0644,
992                 .proc_handler   = &proc_dointvec,
993                 .strategy       = &sysctl_intvec,
994                 .extra1         = &zero,
995         },
996         {
997                 .ctl_name       = VM_MIN_UNMAPPED,
998                 .procname       = "min_unmapped_ratio",
999                 .data           = &sysctl_min_unmapped_ratio,
1000                 .maxlen         = sizeof(sysctl_min_unmapped_ratio),
1001                 .mode           = 0644,
1002                 .proc_handler   = &sysctl_min_unmapped_ratio_sysctl_handler,
1003                 .strategy       = &sysctl_intvec,
1004                 .extra1         = &zero,
1005                 .extra2         = &one_hundred,
1006         },
1007         {
1008                 .ctl_name       = VM_MIN_SLAB,
1009                 .procname       = "min_slab_ratio",
1010                 .data           = &sysctl_min_slab_ratio,
1011                 .maxlen         = sizeof(sysctl_min_slab_ratio),
1012                 .mode           = 0644,
1013                 .proc_handler   = &sysctl_min_slab_ratio_sysctl_handler,
1014                 .strategy       = &sysctl_intvec,
1015                 .extra1         = &zero,
1016                 .extra2         = &one_hundred,
1017         },
1018 #endif
1019 #ifdef CONFIG_X86_32
1020         {
1021                 .ctl_name       = VM_VDSO_ENABLED,
1022                 .procname       = "vdso_enabled",
1023                 .data           = &vdso_enabled,
1024                 .maxlen         = sizeof(vdso_enabled),
1025                 .mode           = 0644,
1026                 .proc_handler   = &proc_dointvec,
1027                 .strategy       = &sysctl_intvec,
1028                 .extra1         = &zero,
1029         },
1030 #endif
1031         { .ctl_name = 0 }
1032 };
1033
1034 static ctl_table fs_table[] = {
1035         {
1036                 .ctl_name       = FS_NRINODE,
1037                 .procname       = "inode-nr",
1038                 .data           = &inodes_stat,
1039                 .maxlen         = 2*sizeof(int),
1040                 .mode           = 0444,
1041                 .proc_handler   = &proc_dointvec,
1042         },
1043         {
1044                 .ctl_name       = FS_STATINODE,
1045                 .procname       = "inode-state",
1046                 .data           = &inodes_stat,
1047                 .maxlen         = 7*sizeof(int),
1048                 .mode           = 0444,
1049                 .proc_handler   = &proc_dointvec,
1050         },
1051         {
1052                 .ctl_name       = FS_NRFILE,
1053                 .procname       = "file-nr",
1054                 .data           = &files_stat,
1055                 .maxlen         = 3*sizeof(int),
1056                 .mode           = 0444,
1057                 .proc_handler   = &proc_nr_files,
1058         },
1059         {
1060                 .ctl_name       = FS_MAXFILE,
1061                 .procname       = "file-max",
1062                 .data           = &files_stat.max_files,
1063                 .maxlen         = sizeof(int),
1064                 .mode           = 0644,
1065                 .proc_handler   = &proc_dointvec,
1066         },
1067         {
1068                 .ctl_name       = FS_DENTRY,
1069                 .procname       = "dentry-state",
1070                 .data           = &dentry_stat,
1071                 .maxlen         = 6*sizeof(int),
1072                 .mode           = 0444,
1073                 .proc_handler   = &proc_dointvec,
1074         },
1075         {
1076                 .ctl_name       = FS_OVERFLOWUID,
1077                 .procname       = "overflowuid",
1078                 .data           = &fs_overflowuid,
1079                 .maxlen         = sizeof(int),
1080                 .mode           = 0644,
1081                 .proc_handler   = &proc_dointvec_minmax,
1082                 .strategy       = &sysctl_intvec,
1083                 .extra1         = &minolduid,
1084                 .extra2         = &maxolduid,
1085         },
1086         {
1087                 .ctl_name       = FS_OVERFLOWGID,
1088                 .procname       = "overflowgid",
1089                 .data           = &fs_overflowgid,
1090                 .maxlen         = sizeof(int),
1091                 .mode           = 0644,
1092                 .proc_handler   = &proc_dointvec_minmax,
1093                 .strategy       = &sysctl_intvec,
1094                 .extra1         = &minolduid,
1095                 .extra2         = &maxolduid,
1096         },
1097         {
1098                 .ctl_name       = FS_LEASES,
1099                 .procname       = "leases-enable",
1100                 .data           = &leases_enable,
1101                 .maxlen         = sizeof(int),
1102                 .mode           = 0644,
1103                 .proc_handler   = &proc_dointvec,
1104         },
1105 #ifdef CONFIG_DNOTIFY
1106         {
1107                 .ctl_name       = FS_DIR_NOTIFY,
1108                 .procname       = "dir-notify-enable",
1109                 .data           = &dir_notify_enable,
1110                 .maxlen         = sizeof(int),
1111                 .mode           = 0644,
1112                 .proc_handler   = &proc_dointvec,
1113         },
1114 #endif
1115 #ifdef CONFIG_MMU
1116         {
1117                 .ctl_name       = FS_LEASE_TIME,
1118                 .procname       = "lease-break-time",
1119                 .data           = &lease_break_time,
1120                 .maxlen         = sizeof(int),
1121                 .mode           = 0644,
1122                 .proc_handler   = &proc_dointvec,
1123         },
1124         {
1125                 .ctl_name       = FS_AIO_NR,
1126                 .procname       = "aio-nr",
1127                 .data           = &aio_nr,
1128                 .maxlen         = sizeof(aio_nr),
1129                 .mode           = 0444,
1130                 .proc_handler   = &proc_doulongvec_minmax,
1131         },
1132         {
1133                 .ctl_name       = FS_AIO_MAX_NR,
1134                 .procname       = "aio-max-nr",
1135                 .data           = &aio_max_nr,
1136                 .maxlen         = sizeof(aio_max_nr),
1137                 .mode           = 0644,
1138                 .proc_handler   = &proc_doulongvec_minmax,
1139         },
1140 #ifdef CONFIG_INOTIFY_USER
1141         {
1142                 .ctl_name       = FS_INOTIFY,
1143                 .procname       = "inotify",
1144                 .mode           = 0555,
1145                 .child          = inotify_table,
1146         },
1147 #endif  
1148 #endif
1149         {
1150                 .ctl_name       = KERN_SETUID_DUMPABLE,
1151                 .procname       = "suid_dumpable",
1152                 .data           = &suid_dumpable,
1153                 .maxlen         = sizeof(int),
1154                 .mode           = 0644,
1155                 .proc_handler   = &proc_dointvec,
1156         },
1157         { .ctl_name = 0 }
1158 };
1159
1160 static ctl_table debug_table[] = {
1161         { .ctl_name = 0 }
1162 };
1163
1164 static ctl_table dev_table[] = {
1165         { .ctl_name = 0 }
1166 };
1167
1168 extern void init_irq_proc (void);
1169
1170 static DEFINE_SPINLOCK(sysctl_lock);
1171
1172 /* called under sysctl_lock */
1173 static int use_table(struct ctl_table_header *p)
1174 {
1175         if (unlikely(p->unregistering))
1176                 return 0;
1177         p->used++;
1178         return 1;
1179 }
1180
1181 /* called under sysctl_lock */
1182 static void unuse_table(struct ctl_table_header *p)
1183 {
1184         if (!--p->used)
1185                 if (unlikely(p->unregistering))
1186                         complete(p->unregistering);
1187 }
1188
1189 /* called under sysctl_lock, will reacquire if has to wait */
1190 static void start_unregistering(struct ctl_table_header *p)
1191 {
1192         /*
1193          * if p->used is 0, nobody will ever touch that entry again;
1194          * we'll eliminate all paths to it before dropping sysctl_lock
1195          */
1196         if (unlikely(p->used)) {
1197                 struct completion wait;
1198                 init_completion(&wait);
1199                 p->unregistering = &wait;
1200                 spin_unlock(&sysctl_lock);
1201                 wait_for_completion(&wait);
1202                 spin_lock(&sysctl_lock);
1203         }
1204         /*
1205          * do not remove from the list until nobody holds it; walking the
1206          * list in do_sysctl() relies on that.
1207          */
1208         list_del_init(&p->ctl_entry);
1209 }
1210
1211 void __init sysctl_init(void)
1212 {
1213 #ifdef CONFIG_PROC_SYSCTL
1214         register_proc_table(root_table, proc_sys_root, &root_table_header);
1215         init_irq_proc();
1216 #endif
1217 }
1218
1219 #ifdef CONFIG_SYSCTL_SYSCALL
1220 int do_sysctl(int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp,
1221                void __user *newval, size_t newlen)
1222 {
1223         struct list_head *tmp;
1224         int error = -ENOTDIR;
1225
1226         if (nlen <= 0 || nlen >= CTL_MAXNAME)
1227                 return -ENOTDIR;
1228         if (oldval) {
1229                 int old_len;
1230                 if (!oldlenp || get_user(old_len, oldlenp))
1231                         return -EFAULT;
1232         }
1233         spin_lock(&sysctl_lock);
1234         tmp = &root_table_header.ctl_entry;
1235         do {
1236                 struct ctl_table_header *head =
1237                         list_entry(tmp, struct ctl_table_header, ctl_entry);
1238                 void *context = NULL;
1239
1240                 if (!use_table(head))
1241                         continue;
1242
1243                 spin_unlock(&sysctl_lock);
1244
1245                 error = parse_table(name, nlen, oldval, oldlenp, 
1246                                         newval, newlen, head->ctl_table,
1247                                         &context);
1248                 kfree(context);
1249
1250                 spin_lock(&sysctl_lock);
1251                 unuse_table(head);
1252                 if (error != -ENOTDIR)
1253                         break;
1254         } while ((tmp = tmp->next) != &root_table_header.ctl_entry);
1255         spin_unlock(&sysctl_lock);
1256         return error;
1257 }
1258
1259 asmlinkage long sys_sysctl(struct __sysctl_args __user *args)
1260 {
1261         struct __sysctl_args tmp;
1262         int error;
1263
1264         if (copy_from_user(&tmp, args, sizeof(tmp)))
1265                 return -EFAULT;
1266
1267         lock_kernel();
1268         error = do_sysctl(tmp.name, tmp.nlen, tmp.oldval, tmp.oldlenp,
1269                           tmp.newval, tmp.newlen);
1270         unlock_kernel();
1271         return error;
1272 }
1273 #endif /* CONFIG_SYSCTL_SYSCALL */
1274
1275 /*
1276  * ctl_perm does NOT grant the superuser all rights automatically, because
1277  * some sysctl variables are readonly even to root.
1278  */
1279
1280 static int test_perm(int mode, int op)
1281 {
1282         if (!current->euid)
1283                 mode >>= 6;
1284         else if (in_egroup_p(0))
1285                 mode >>= 3;
1286         if ((mode & op & 0007) == op)
1287                 return 0;
1288         return -EACCES;
1289 }
1290
1291 static inline int ctl_perm(ctl_table *table, int op)
1292 {
1293         int error;
1294         error = security_sysctl(table, op);
1295         if (error)
1296                 return error;
1297         return test_perm(table->mode, op);
1298 }
1299
1300 #ifdef CONFIG_SYSCTL_SYSCALL
1301 static int parse_table(int __user *name, int nlen,
1302                        void __user *oldval, size_t __user *oldlenp,
1303                        void __user *newval, size_t newlen,
1304                        ctl_table *table, void **context)
1305 {
1306         int n;
1307 repeat:
1308         if (!nlen)
1309                 return -ENOTDIR;
1310         if (get_user(n, name))
1311                 return -EFAULT;
1312         for ( ; table->ctl_name; table++) {
1313                 if (n == table->ctl_name || table->ctl_name == CTL_ANY) {
1314                         int error;
1315                         if (table->child) {
1316                                 if (ctl_perm(table, 001))
1317                                         return -EPERM;
1318                                 if (table->strategy) {
1319                                         error = table->strategy(
1320                                                 table, name, nlen,
1321                                                 oldval, oldlenp,
1322                                                 newval, newlen, context);
1323                                         if (error)
1324                                                 return error;
1325                                 }
1326                                 name++;
1327                                 nlen--;
1328                                 table = table->child;
1329                                 goto repeat;
1330                         }
1331                         error = do_sysctl_strategy(table, name, nlen,
1332                                                    oldval, oldlenp,
1333                                                    newval, newlen, context);
1334                         return error;
1335                 }
1336         }
1337         return -ENOTDIR;
1338 }
1339
1340 /* Perform the actual read/write of a sysctl table entry. */
1341 int do_sysctl_strategy (ctl_table *table, 
1342                         int __user *name, int nlen,
1343                         void __user *oldval, size_t __user *oldlenp,
1344                         void __user *newval, size_t newlen, void **context)
1345 {
1346         int op = 0, rc;
1347         size_t len;
1348
1349         if (oldval)
1350                 op |= 004;
1351         if (newval) 
1352                 op |= 002;
1353         if (ctl_perm(table, op))
1354                 return -EPERM;
1355
1356         if (table->strategy) {
1357                 rc = table->strategy(table, name, nlen, oldval, oldlenp,
1358                                      newval, newlen, context);
1359                 if (rc < 0)
1360                         return rc;
1361                 if (rc > 0)
1362                         return 0;
1363         }
1364
1365         /* If there is no strategy routine, or if the strategy returns
1366          * zero, proceed with automatic r/w */
1367         if (table->data && table->maxlen) {
1368                 if (oldval && oldlenp) {
1369                         if (get_user(len, oldlenp))
1370                                 return -EFAULT;
1371                         if (len) {
1372                                 if (len > table->maxlen)
1373                                         len = table->maxlen;
1374                                 if(copy_to_user(oldval, table->data, len))
1375                                         return -EFAULT;
1376                                 if(put_user(len, oldlenp))
1377                                         return -EFAULT;
1378                         }
1379                 }
1380                 if (newval && newlen) {
1381                         len = newlen;
1382                         if (len > table->maxlen)
1383                                 len = table->maxlen;
1384                         if(copy_from_user(table->data, newval, len))
1385                                 return -EFAULT;
1386                 }
1387         }
1388         return 0;
1389 }
1390 #endif /* CONFIG_SYSCTL_SYSCALL */
1391
1392 /**
1393  * register_sysctl_table - register a sysctl hierarchy
1394  * @table: the top-level table structure
1395  * @insert_at_head: whether the entry should be inserted in front or at the end
1396  *
1397  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1398  * array. An entry with a ctl_name of 0 terminates the table. 
1399  *
1400  * The members of the &ctl_table structure are used as follows:
1401  *
1402  * ctl_name - This is the numeric sysctl value used by sysctl(2). The number
1403  *            must be unique within that level of sysctl
1404  *
1405  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1406  *            enter a sysctl file
1407  *
1408  * data - a pointer to data for use by proc_handler
1409  *
1410  * maxlen - the maximum size in bytes of the data
1411  *
1412  * mode - the file permissions for the /proc/sys file, and for sysctl(2)
1413  *
1414  * child - a pointer to the child sysctl table if this entry is a directory, or
1415  *         %NULL.
1416  *
1417  * proc_handler - the text handler routine (described below)
1418  *
1419  * strategy - the strategy routine (described below)
1420  *
1421  * de - for internal use by the sysctl routines
1422  *
1423  * extra1, extra2 - extra pointers usable by the proc handler routines
1424  *
1425  * Leaf nodes in the sysctl tree will be represented by a single file
1426  * under /proc; non-leaf nodes will be represented by directories.
1427  *
1428  * sysctl(2) can automatically manage read and write requests through
1429  * the sysctl table.  The data and maxlen fields of the ctl_table
1430  * struct enable minimal validation of the values being written to be
1431  * performed, and the mode field allows minimal authentication.
1432  *
1433  * More sophisticated management can be enabled by the provision of a
1434  * strategy routine with the table entry.  This will be called before
1435  * any automatic read or write of the data is performed.
1436  *
1437  * The strategy routine may return
1438  *
1439  * < 0 - Error occurred (error is passed to user process)
1440  *
1441  * 0   - OK - proceed with automatic read or write.
1442  *
1443  * > 0 - OK - read or write has been done by the strategy routine, so
1444  *       return immediately.
1445  *
1446  * There must be a proc_handler routine for any terminal nodes
1447  * mirrored under /proc/sys (non-terminals are handled by a built-in
1448  * directory handler).  Several default handlers are available to
1449  * cover common cases -
1450  *
1451  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1452  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), 
1453  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1454  *
1455  * It is the handler's job to read the input buffer from user memory
1456  * and process it. The handler should return 0 on success.
1457  *
1458  * This routine returns %NULL on a failure to register, and a pointer
1459  * to the table header on success.
1460  */
1461 struct ctl_table_header *register_sysctl_table(ctl_table * table, 
1462                                                int insert_at_head)
1463 {
1464         struct ctl_table_header *tmp;
1465         tmp = kmalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
1466         if (!tmp)
1467                 return NULL;
1468         tmp->ctl_table = table;
1469         INIT_LIST_HEAD(&tmp->ctl_entry);
1470         tmp->used = 0;
1471         tmp->unregistering = NULL;
1472         spin_lock(&sysctl_lock);
1473         if (insert_at_head)
1474                 list_add(&tmp->ctl_entry, &root_table_header.ctl_entry);
1475         else
1476                 list_add_tail(&tmp->ctl_entry, &root_table_header.ctl_entry);
1477         spin_unlock(&sysctl_lock);
1478 #ifdef CONFIG_PROC_SYSCTL
1479         register_proc_table(table, proc_sys_root, tmp);
1480 #endif
1481         return tmp;
1482 }
1483
1484 /**
1485  * unregister_sysctl_table - unregister a sysctl table hierarchy
1486  * @header: the header returned from register_sysctl_table
1487  *
1488  * Unregisters the sysctl table and all children. proc entries may not
1489  * actually be removed until they are no longer used by anyone.
1490  */
1491 void unregister_sysctl_table(struct ctl_table_header * header)
1492 {
1493         might_sleep();
1494         spin_lock(&sysctl_lock);
1495         start_unregistering(header);
1496 #ifdef CONFIG_PROC_SYSCTL
1497         unregister_proc_table(header->ctl_table, proc_sys_root);
1498 #endif
1499         spin_unlock(&sysctl_lock);
1500         kfree(header);
1501 }
1502
1503 #else /* !CONFIG_SYSCTL */
1504 struct ctl_table_header * register_sysctl_table(ctl_table * table,
1505                                                 int insert_at_head)
1506 {
1507         return NULL;
1508 }
1509
1510 void unregister_sysctl_table(struct ctl_table_header * table)
1511 {
1512 }
1513
1514 #endif /* CONFIG_SYSCTL */
1515
1516 /*
1517  * /proc/sys support
1518  */
1519
1520 #ifdef CONFIG_PROC_SYSCTL
1521
1522 /* Scan the sysctl entries in table and add them all into /proc */
1523 static void register_proc_table(ctl_table * table, struct proc_dir_entry *root, void *set)
1524 {
1525         struct proc_dir_entry *de;
1526         int len;
1527         mode_t mode;
1528         
1529         for (; table->ctl_name; table++) {
1530                 /* Can't do anything without a proc name. */
1531                 if (!table->procname)
1532                         continue;
1533                 /* Maybe we can't do anything with it... */
1534                 if (!table->proc_handler && !table->child) {
1535                         printk(KERN_WARNING "SYSCTL: Can't register %s\n",
1536                                 table->procname);
1537                         continue;
1538                 }
1539
1540                 len = strlen(table->procname);
1541                 mode = table->mode;
1542
1543                 de = NULL;
1544                 if (table->proc_handler)
1545                         mode |= S_IFREG;
1546                 else {
1547                         mode |= S_IFDIR;
1548                         for (de = root->subdir; de; de = de->next) {
1549                                 if (proc_match(len, table->procname, de))
1550                                         break;
1551                         }
1552                         /* If the subdir exists already, de is non-NULL */
1553                 }
1554
1555                 if (!de) {
1556                         de = create_proc_entry(table->procname, mode, root);
1557                         if (!de)
1558                                 continue;
1559                         de->set = set;
1560                         de->data = (void *) table;
1561                         if (table->proc_handler)
1562                                 de->proc_fops = &proc_sys_file_operations;
1563                 }
1564                 table->de = de;
1565                 if (de->mode & S_IFDIR)
1566                         register_proc_table(table->child, de, set);
1567         }
1568 }
1569
1570 /*
1571  * Unregister a /proc sysctl table and any subdirectories.
1572  */
1573 static void unregister_proc_table(ctl_table * table, struct proc_dir_entry *root)
1574 {
1575         struct proc_dir_entry *de;
1576         for (; table->ctl_name; table++) {
1577                 if (!(de = table->de))
1578                         continue;
1579                 if (de->mode & S_IFDIR) {
1580                         if (!table->child) {
1581                                 printk (KERN_ALERT "Help - malformed sysctl tree on free\n");
1582                                 continue;
1583                         }
1584                         unregister_proc_table(table->child, de);
1585
1586                         /* Don't unregister directories which still have entries.. */
1587                         if (de->subdir)
1588                                 continue;
1589                 }
1590
1591                 /*
1592                  * In any case, mark the entry as goner; we'll keep it
1593                  * around if it's busy, but we'll know to do nothing with
1594                  * its fields.  We are under sysctl_lock here.
1595                  */
1596                 de->data = NULL;
1597
1598                 /* Don't unregister proc entries that are still being used.. */
1599                 if (atomic_read(&de->count))
1600                         continue;
1601
1602                 table->de = NULL;
1603                 remove_proc_entry(table->procname, root);
1604         }
1605 }
1606
1607 static ssize_t do_rw_proc(int write, struct file * file, char __user * buf,
1608                           size_t count, loff_t *ppos)
1609 {
1610         int op;
1611         struct proc_dir_entry *de = PDE(file->f_dentry->d_inode);
1612         struct ctl_table *table;
1613         size_t res;
1614         ssize_t error = -ENOTDIR;
1615         
1616         spin_lock(&sysctl_lock);
1617         if (de && de->data && use_table(de->set)) {
1618                 /*
1619                  * at that point we know that sysctl was not unregistered
1620                  * and won't be until we finish
1621                  */
1622                 spin_unlock(&sysctl_lock);
1623                 table = (struct ctl_table *) de->data;
1624                 if (!table || !table->proc_handler)
1625                         goto out;
1626                 error = -EPERM;
1627                 op = (write ? 002 : 004);
1628                 if (ctl_perm(table, op))
1629                         goto out;
1630                 
1631                 /* careful: calling conventions are nasty here */
1632                 res = count;
1633                 error = (*table->proc_handler)(table, write, file,
1634                                                 buf, &res, ppos);
1635                 if (!error)
1636                         error = res;
1637         out:
1638                 spin_lock(&sysctl_lock);
1639                 unuse_table(de->set);
1640         }
1641         spin_unlock(&sysctl_lock);
1642         return error;
1643 }
1644
1645 static int proc_opensys(struct inode *inode, struct file *file)
1646 {
1647         if (file->f_mode & FMODE_WRITE) {
1648                 /*
1649                  * sysctl entries that are not writable,
1650                  * are _NOT_ writable, capabilities or not.
1651                  */
1652                 if (!(inode->i_mode & S_IWUSR))
1653                         return -EPERM;
1654         }
1655
1656         return 0;
1657 }
1658
1659 static ssize_t proc_readsys(struct file * file, char __user * buf,
1660                             size_t count, loff_t *ppos)
1661 {
1662         return do_rw_proc(0, file, buf, count, ppos);
1663 }
1664
1665 static ssize_t proc_writesys(struct file * file, const char __user * buf,
1666                              size_t count, loff_t *ppos)
1667 {
1668         return do_rw_proc(1, file, (char __user *) buf, count, ppos);
1669 }
1670
1671 static int _proc_do_string(void* data, int maxlen, int write,
1672                            struct file *filp, void __user *buffer,
1673                            size_t *lenp, loff_t *ppos)
1674 {
1675         size_t len;
1676         char __user *p;
1677         char c;
1678         
1679         if (!data || !maxlen || !*lenp ||
1680             (*ppos && !write)) {
1681                 *lenp = 0;
1682                 return 0;
1683         }
1684         
1685         if (write) {
1686                 len = 0;
1687                 p = buffer;
1688                 while (len < *lenp) {
1689                         if (get_user(c, p++))
1690                                 return -EFAULT;
1691                         if (c == 0 || c == '\n')
1692                                 break;
1693                         len++;
1694                 }
1695                 if (len >= maxlen)
1696                         len = maxlen-1;
1697                 if(copy_from_user(data, buffer, len))
1698                         return -EFAULT;
1699                 ((char *) data)[len] = 0;
1700                 *ppos += *lenp;
1701         } else {
1702                 len = strlen(data);
1703                 if (len > maxlen)
1704                         len = maxlen;
1705                 if (len > *lenp)
1706                         len = *lenp;
1707                 if (len)
1708                         if(copy_to_user(buffer, data, len))
1709                                 return -EFAULT;
1710                 if (len < *lenp) {
1711                         if(put_user('\n', ((char __user *) buffer) + len))
1712                                 return -EFAULT;
1713                         len++;
1714                 }
1715                 *lenp = len;
1716                 *ppos += len;
1717         }
1718         return 0;
1719 }
1720
1721 /**
1722  * proc_dostring - read a string sysctl
1723  * @table: the sysctl table
1724  * @write: %TRUE if this is a write to the sysctl file
1725  * @filp: the file structure
1726  * @buffer: the user buffer
1727  * @lenp: the size of the user buffer
1728  * @ppos: file position
1729  *
1730  * Reads/writes a string from/to the user buffer. If the kernel
1731  * buffer provided is not large enough to hold the string, the
1732  * string is truncated. The copied string is %NULL-terminated.
1733  * If the string is being read by the user process, it is copied
1734  * and a newline '\n' is added. It is truncated if the buffer is
1735  * not large enough.
1736  *
1737  * Returns 0 on success.
1738  */
1739 int proc_dostring(ctl_table *table, int write, struct file *filp,
1740                   void __user *buffer, size_t *lenp, loff_t *ppos)
1741 {
1742         return _proc_do_string(table->data, table->maxlen, write, filp,
1743                                buffer, lenp, ppos);
1744 }
1745
1746 /*
1747  *      Special case of dostring for the UTS structure. This has locks
1748  *      to observe. Should this be in kernel/sys.c ????
1749  */
1750  
1751 #ifndef CONFIG_UTS_NS
1752 static int proc_do_uts_string(ctl_table *table, int write, struct file *filp,
1753                   void __user *buffer, size_t *lenp, loff_t *ppos)
1754 {
1755         int r;
1756
1757         if (!write) {
1758                 down_read(&uts_sem);
1759                 r=proc_dostring(table,0,filp,buffer,lenp, ppos);
1760                 up_read(&uts_sem);
1761         } else {
1762                 down_write(&uts_sem);
1763                 r=proc_dostring(table,1,filp,buffer,lenp, ppos);
1764                 up_write(&uts_sem);
1765         }
1766         return r;
1767 }
1768 #else /* !CONFIG_UTS_NS */
1769 static int proc_do_uts_string(ctl_table *table, int write, struct file *filp,
1770                   void __user *buffer, size_t *lenp, loff_t *ppos)
1771 {
1772         int r;
1773         struct uts_namespace* uts_ns = current->nsproxy->uts_ns;
1774         char* which;
1775
1776         switch (table->ctl_name) {
1777         case KERN_OSTYPE:
1778                 which = uts_ns->name.sysname;
1779                 break;
1780         case KERN_NODENAME:
1781                 which = uts_ns->name.nodename;
1782                 break;
1783         case KERN_OSRELEASE:
1784                 which = uts_ns->name.release;
1785                 break;
1786         case KERN_VERSION:
1787                 which = uts_ns->name.version;
1788                 break;
1789         case KERN_DOMAINNAME:
1790                 which = uts_ns->name.domainname;
1791                 break;
1792         default:
1793                 r = -EINVAL;
1794                 goto out;
1795         }
1796
1797         if (!write) {
1798                 down_read(&uts_sem);
1799                 r=_proc_do_string(which,table->maxlen,0,filp,buffer,lenp, ppos);
1800                 up_read(&uts_sem);
1801         } else {
1802                 down_write(&uts_sem);
1803                 r=_proc_do_string(which,table->maxlen,1,filp,buffer,lenp, ppos);
1804                 up_write(&uts_sem);
1805         }
1806  out:
1807         return r;
1808 }
1809 #endif /* !CONFIG_UTS_NS */
1810
1811 static int do_proc_dointvec_conv(int *negp, unsigned long *lvalp,
1812                                  int *valp,
1813                                  int write, void *data)
1814 {
1815         if (write) {
1816                 *valp = *negp ? -*lvalp : *lvalp;
1817         } else {
1818                 int val = *valp;
1819                 if (val < 0) {
1820                         *negp = -1;
1821                         *lvalp = (unsigned long)-val;
1822                 } else {
1823                         *negp = 0;
1824                         *lvalp = (unsigned long)val;
1825                 }
1826         }
1827         return 0;
1828 }
1829
1830 static int __do_proc_dointvec(void *tbl_data, ctl_table *table,
1831                   int write, struct file *filp, void __user *buffer,
1832                   size_t *lenp, loff_t *ppos,
1833                   int (*conv)(int *negp, unsigned long *lvalp, int *valp,
1834                               int write, void *data),
1835                   void *data)
1836 {
1837 #define TMPBUFLEN 21
1838         int *i, vleft, first=1, neg, val;
1839         unsigned long lval;
1840         size_t left, len;
1841         
1842         char buf[TMPBUFLEN], *p;
1843         char __user *s = buffer;
1844         
1845         if (!tbl_data || !table->maxlen || !*lenp ||
1846             (*ppos && !write)) {
1847                 *lenp = 0;
1848                 return 0;
1849         }
1850         
1851         i = (int *) tbl_data;
1852         vleft = table->maxlen / sizeof(*i);
1853         left = *lenp;
1854
1855         if (!conv)
1856                 conv = do_proc_dointvec_conv;
1857
1858         for (; left && vleft--; i++, first=0) {
1859                 if (write) {
1860                         while (left) {
1861                                 char c;
1862                                 if (get_user(c, s))
1863                                         return -EFAULT;
1864                                 if (!isspace(c))
1865                                         break;
1866                                 left--;
1867                                 s++;
1868                         }
1869                         if (!left)
1870                                 break;
1871                         neg = 0;
1872                         len = left;
1873                         if (len > sizeof(buf) - 1)
1874                                 len = sizeof(buf) - 1;
1875                         if (copy_from_user(buf, s, len))
1876                                 return -EFAULT;
1877                         buf[len] = 0;
1878                         p = buf;
1879                         if (*p == '-' && left > 1) {
1880                                 neg = 1;
1881                                 left--, p++;
1882                         }
1883                         if (*p < '0' || *p > '9')
1884                                 break;
1885
1886                         lval = simple_strtoul(p, &p, 0);
1887
1888                         len = p-buf;
1889                         if ((len < left) && *p && !isspace(*p))
1890                                 break;
1891                         if (neg)
1892                                 val = -val;
1893                         s += len;
1894                         left -= len;
1895
1896                         if (conv(&neg, &lval, i, 1, data))
1897                                 break;
1898                 } else {
1899                         p = buf;
1900                         if (!first)
1901                                 *p++ = '\t';
1902         
1903                         if (conv(&neg, &lval, i, 0, data))
1904                                 break;
1905
1906                         sprintf(p, "%s%lu", neg ? "-" : "", lval);
1907                         len = strlen(buf);
1908                         if (len > left)
1909                                 len = left;
1910                         if(copy_to_user(s, buf, len))
1911                                 return -EFAULT;
1912                         left -= len;
1913                         s += len;
1914                 }
1915         }
1916
1917         if (!write && !first && left) {
1918                 if(put_user('\n', s))
1919                         return -EFAULT;
1920                 left--, s++;
1921         }
1922         if (write) {
1923                 while (left) {
1924                         char c;
1925                         if (get_user(c, s++))
1926                                 return -EFAULT;
1927                         if (!isspace(c))
1928                                 break;
1929                         left--;
1930                 }
1931         }
1932         if (write && first)
1933                 return -EINVAL;
1934         *lenp -= left;
1935         *ppos += *lenp;
1936         return 0;
1937 #undef TMPBUFLEN
1938 }
1939
1940 static int do_proc_dointvec(ctl_table *table, int write, struct file *filp,
1941                   void __user *buffer, size_t *lenp, loff_t *ppos,
1942                   int (*conv)(int *negp, unsigned long *lvalp, int *valp,
1943                               int write, void *data),
1944                   void *data)
1945 {
1946         return __do_proc_dointvec(table->data, table, write, filp,
1947                         buffer, lenp, ppos, conv, data);
1948 }
1949
1950 /**
1951  * proc_dointvec - read a vector of integers
1952  * @table: the sysctl table
1953  * @write: %TRUE if this is a write to the sysctl file
1954  * @filp: the file structure
1955  * @buffer: the user buffer
1956  * @lenp: the size of the user buffer
1957  * @ppos: file position
1958  *
1959  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1960  * values from/to the user buffer, treated as an ASCII string. 
1961  *
1962  * Returns 0 on success.
1963  */
1964 int proc_dointvec(ctl_table *table, int write, struct file *filp,
1965                      void __user *buffer, size_t *lenp, loff_t *ppos)
1966 {
1967     return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
1968                             NULL,NULL);
1969 }
1970
1971 #define OP_SET  0
1972 #define OP_AND  1
1973 #define OP_OR   2
1974 #define OP_MAX  3
1975 #define OP_MIN  4
1976
1977 static int do_proc_dointvec_bset_conv(int *negp, unsigned long *lvalp,
1978                                       int *valp,
1979                                       int write, void *data)
1980 {
1981         int op = *(int *)data;
1982         if (write) {
1983                 int val = *negp ? -*lvalp : *lvalp;
1984                 switch(op) {
1985                 case OP_SET:    *valp = val; break;
1986                 case OP_AND:    *valp &= val; break;
1987                 case OP_OR:     *valp |= val; break;
1988                 case OP_MAX:    if(*valp < val)
1989                                         *valp = val;
1990                                 break;
1991                 case OP_MIN:    if(*valp > val)
1992                                 *valp = val;
1993                                 break;
1994                 }
1995         } else {
1996                 int val = *valp;
1997                 if (val < 0) {
1998                         *negp = -1;
1999                         *lvalp = (unsigned long)-val;
2000                 } else {
2001                         *negp = 0;
2002                         *lvalp = (unsigned long)val;
2003                 }
2004         }
2005         return 0;
2006 }
2007
2008 /*
2009  *      init may raise the set.
2010  */
2011  
2012 int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
2013                         void __user *buffer, size_t *lenp, loff_t *ppos)
2014 {
2015         int op;
2016
2017         if (!capable(CAP_SYS_MODULE)) {
2018                 return -EPERM;
2019         }
2020
2021         op = is_init(current) ? OP_SET : OP_AND;
2022         return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
2023                                 do_proc_dointvec_bset_conv,&op);
2024 }
2025
2026 struct do_proc_dointvec_minmax_conv_param {
2027         int *min;
2028         int *max;
2029 };
2030
2031 static int do_proc_dointvec_minmax_conv(int *negp, unsigned long *lvalp, 
2032                                         int *valp, 
2033                                         int write, void *data)
2034 {
2035         struct do_proc_dointvec_minmax_conv_param *param = data;
2036         if (write) {
2037                 int val = *negp ? -*lvalp : *lvalp;
2038                 if ((param->min && *param->min > val) ||
2039                     (param->max && *param->max < val))
2040                         return -EINVAL;
2041                 *valp = val;
2042         } else {
2043                 int val = *valp;
2044                 if (val < 0) {
2045                         *negp = -1;
2046                         *lvalp = (unsigned long)-val;
2047                 } else {
2048                         *negp = 0;
2049                         *lvalp = (unsigned long)val;
2050                 }
2051         }
2052         return 0;
2053 }
2054
2055 /**
2056  * proc_dointvec_minmax - read a vector of integers with min/max values
2057  * @table: the sysctl table
2058  * @write: %TRUE if this is a write to the sysctl file
2059  * @filp: the file structure
2060  * @buffer: the user buffer
2061  * @lenp: the size of the user buffer
2062  * @ppos: file position
2063  *
2064  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2065  * values from/to the user buffer, treated as an ASCII string.
2066  *
2067  * This routine will ensure the values are within the range specified by
2068  * table->extra1 (min) and table->extra2 (max).
2069  *
2070  * Returns 0 on success.
2071  */
2072 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
2073                   void __user *buffer, size_t *lenp, loff_t *ppos)
2074 {
2075         struct do_proc_dointvec_minmax_conv_param param = {
2076                 .min = (int *) table->extra1,
2077                 .max = (int *) table->extra2,
2078         };
2079         return do_proc_dointvec(table, write, filp, buffer, lenp, ppos,
2080                                 do_proc_dointvec_minmax_conv, &param);
2081 }
2082
2083 static int __do_proc_doulongvec_minmax(void *data, ctl_table *table, int write,
2084                                      struct file *filp,
2085                                      void __user *buffer,
2086                                      size_t *lenp, loff_t *ppos,
2087                                      unsigned long convmul,
2088                                      unsigned long convdiv)
2089 {
2090 #define TMPBUFLEN 21
2091         unsigned long *i, *min, *max, val;
2092         int vleft, first=1, neg;
2093         size_t len, left;
2094         char buf[TMPBUFLEN], *p;
2095         char __user *s = buffer;
2096         
2097         if (!data || !table->maxlen || !*lenp ||
2098             (*ppos && !write)) {
2099                 *lenp = 0;
2100                 return 0;
2101         }
2102         
2103         i = (unsigned long *) data;
2104         min = (unsigned long *) table->extra1;
2105         max = (unsigned long *) table->extra2;
2106         vleft = table->maxlen / sizeof(unsigned long);
2107         left = *lenp;
2108         
2109         for (; left && vleft--; i++, min++, max++, first=0) {
2110                 if (write) {
2111                         while (left) {
2112                                 char c;
2113                                 if (get_user(c, s))
2114                                         return -EFAULT;
2115                                 if (!isspace(c))
2116                                         break;
2117                                 left--;
2118                                 s++;
2119                         }
2120                         if (!left)
2121                                 break;
2122                         neg = 0;
2123                         len = left;
2124                         if (len > TMPBUFLEN-1)
2125                                 len = TMPBUFLEN-1;
2126                         if (copy_from_user(buf, s, len))
2127                                 return -EFAULT;
2128                         buf[len] = 0;
2129                         p = buf;
2130                         if (*p == '-' && left > 1) {
2131                                 neg = 1;
2132                                 left--, p++;
2133                         }
2134                         if (*p < '0' || *p > '9')
2135                                 break;
2136                         val = simple_strtoul(p, &p, 0) * convmul / convdiv ;
2137                         len = p-buf;
2138                         if ((len < left) && *p && !isspace(*p))
2139                                 break;
2140                         if (neg)
2141                                 val = -val;
2142                         s += len;
2143                         left -= len;
2144
2145                         if(neg)
2146                                 continue;
2147                         if ((min && val < *min) || (max && val > *max))
2148                                 continue;
2149                         *i = val;
2150                 } else {
2151                         p = buf;
2152                         if (!first)
2153                                 *p++ = '\t';
2154                         sprintf(p, "%lu", convdiv * (*i) / convmul);
2155                         len = strlen(buf);
2156                         if (len > left)
2157                                 len = left;
2158                         if(copy_to_user(s, buf, len))
2159                                 return -EFAULT;
2160                         left -= len;
2161                         s += len;
2162                 }
2163         }
2164
2165         if (!write && !first && left) {
2166                 if(put_user('\n', s))
2167                         return -EFAULT;
2168                 left--, s++;
2169         }
2170         if (write) {
2171                 while (left) {
2172                         char c;
2173                         if (get_user(c, s++))
2174                                 return -EFAULT;
2175                         if (!isspace(c))
2176                                 break;
2177                         left--;
2178                 }
2179         }
2180         if (write && first)
2181                 return -EINVAL;
2182         *lenp -= left;
2183         *ppos += *lenp;
2184         return 0;
2185 #undef TMPBUFLEN
2186 }
2187
2188 static int do_proc_doulongvec_minmax(ctl_table *table, int write,
2189                                      struct file *filp,
2190                                      void __user *buffer,
2191                                      size_t *lenp, loff_t *ppos,
2192                                      unsigned long convmul,
2193                                      unsigned long convdiv)
2194 {
2195         return __do_proc_doulongvec_minmax(table->data, table, write,
2196                         filp, buffer, lenp, ppos, convmul, convdiv);
2197 }
2198
2199 /**
2200  * proc_doulongvec_minmax - read a vector of long integers with min/max values
2201  * @table: the sysctl table
2202  * @write: %TRUE if this is a write to the sysctl file
2203  * @filp: the file structure
2204  * @buffer: the user buffer
2205  * @lenp: the size of the user buffer
2206  * @ppos: file position
2207  *
2208  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
2209  * values from/to the user buffer, treated as an ASCII string.
2210  *
2211  * This routine will ensure the values are within the range specified by
2212  * table->extra1 (min) and table->extra2 (max).
2213  *
2214  * Returns 0 on success.
2215  */
2216 int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
2217                            void __user *buffer, size_t *lenp, loff_t *ppos)
2218 {
2219     return do_proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos, 1l, 1l);
2220 }
2221
2222 /**
2223  * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
2224  * @table: the sysctl table
2225  * @write: %TRUE if this is a write to the sysctl file
2226  * @filp: the file structure
2227  * @buffer: the user buffer
2228  * @lenp: the size of the user buffer
2229  * @ppos: file position
2230  *
2231  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
2232  * values from/to the user buffer, treated as an ASCII string. The values
2233  * are treated as milliseconds, and converted to jiffies when they are stored.
2234  *
2235  * This routine will ensure the values are within the range specified by
2236  * table->extra1 (min) and table->extra2 (max).
2237  *
2238  * Returns 0 on success.
2239  */
2240 int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
2241                                       struct file *filp,
2242                                       void __user *buffer,
2243                                       size_t *lenp, loff_t *ppos)
2244 {
2245     return do_proc_doulongvec_minmax(table, write, filp, buffer,
2246                                      lenp, ppos, HZ, 1000l);
2247 }
2248
2249
2250 static int do_proc_dointvec_jiffies_conv(int *negp, unsigned long *lvalp,
2251                                          int *valp,
2252                                          int write, void *data)
2253 {
2254         if (write) {
2255                 if (*lvalp > LONG_MAX / HZ)
2256                         return 1;
2257                 *valp = *negp ? -(*lvalp*HZ) : (*lvalp*HZ);
2258         } else {
2259                 int val = *valp;
2260                 unsigned long lval;
2261                 if (val < 0) {
2262                         *negp = -1;
2263                         lval = (unsigned long)-val;
2264                 } else {
2265                         *negp = 0;
2266                         lval = (unsigned long)val;
2267                 }
2268                 *lvalp = lval / HZ;
2269         }
2270         return 0;
2271 }
2272
2273 static int do_proc_dointvec_userhz_jiffies_conv(int *negp, unsigned long *lvalp,
2274                                                 int *valp,
2275                                                 int write, void *data)
2276 {
2277         if (write) {
2278                 if (USER_HZ < HZ && *lvalp > (LONG_MAX / HZ) * USER_HZ)
2279                         return 1;
2280                 *valp = clock_t_to_jiffies(*negp ? -*lvalp : *lvalp);
2281         } else {
2282                 int val = *valp;
2283                 unsigned long lval;
2284                 if (val < 0) {
2285                         *negp = -1;
2286                         lval = (unsigned long)-val;
2287                 } else {
2288                         *negp = 0;
2289                         lval = (unsigned long)val;
2290                 }
2291                 *lvalp = jiffies_to_clock_t(lval);
2292         }
2293         return 0;
2294 }
2295
2296 static int do_proc_dointvec_ms_jiffies_conv(int *negp, unsigned long *lvalp,
2297                                             int *valp,
2298                                             int write, void *data)
2299 {
2300         if (write) {
2301                 *valp = msecs_to_jiffies(*negp ? -*lvalp : *lvalp);
2302         } else {
2303                 int val = *valp;
2304                 unsigned long lval;
2305                 if (val < 0) {
2306                         *negp = -1;
2307                         lval = (unsigned long)-val;
2308                 } else {
2309                         *negp = 0;
2310                         lval = (unsigned long)val;
2311                 }
2312                 *lvalp = jiffies_to_msecs(lval);
2313         }
2314         return 0;
2315 }
2316
2317 /**
2318  * proc_dointvec_jiffies - read a vector of integers as seconds
2319  * @table: the sysctl table
2320  * @write: %TRUE if this is a write to the sysctl file
2321  * @filp: the file structure
2322  * @buffer: the user buffer
2323  * @lenp: the size of the user buffer
2324  * @ppos: file position
2325  *
2326  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2327  * values from/to the user buffer, treated as an ASCII string. 
2328  * The values read are assumed to be in seconds, and are converted into
2329  * jiffies.
2330  *
2331  * Returns 0 on success.
2332  */
2333 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
2334                           void __user *buffer, size_t *lenp, loff_t *ppos)
2335 {
2336     return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
2337                             do_proc_dointvec_jiffies_conv,NULL);
2338 }
2339
2340 /**
2341  * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds
2342  * @table: the sysctl table
2343  * @write: %TRUE if this is a write to the sysctl file
2344  * @filp: the file structure
2345  * @buffer: the user buffer
2346  * @lenp: the size of the user buffer
2347  * @ppos: pointer to the file position
2348  *
2349  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2350  * values from/to the user buffer, treated as an ASCII string. 
2351  * The values read are assumed to be in 1/USER_HZ seconds, and 
2352  * are converted into jiffies.
2353  *
2354  * Returns 0 on success.
2355  */
2356 int proc_dointvec_userhz_jiffies(ctl_table *table, int write, struct file *filp,
2357                                  void __user *buffer, size_t *lenp, loff_t *ppos)
2358 {
2359     return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
2360                             do_proc_dointvec_userhz_jiffies_conv,NULL);
2361 }
2362
2363 /**
2364  * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds
2365  * @table: the sysctl table
2366  * @write: %TRUE if this is a write to the sysctl file
2367  * @filp: the file structure
2368  * @buffer: the user buffer
2369  * @lenp: the size of the user buffer
2370  * @ppos: file position
2371  * @ppos: the current position in the file
2372  *
2373  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2374  * values from/to the user buffer, treated as an ASCII string. 
2375  * The values read are assumed to be in 1/1000 seconds, and 
2376  * are converted into jiffies.
2377  *
2378  * Returns 0 on success.
2379  */
2380 int proc_dointvec_ms_jiffies(ctl_table *table, int write, struct file *filp,
2381                              void __user *buffer, size_t *lenp, loff_t *ppos)
2382 {
2383         return do_proc_dointvec(table, write, filp, buffer, lenp, ppos,
2384                                 do_proc_dointvec_ms_jiffies_conv, NULL);
2385 }
2386
2387 #ifdef CONFIG_SYSVIPC
2388 static int proc_do_ipc_string(ctl_table *table, int write, struct file *filp,
2389                 void __user *buffer, size_t *lenp, loff_t *ppos)
2390 {
2391         void *data;
2392         struct ipc_namespace *ns;
2393
2394         ns = current->nsproxy->ipc_ns;
2395
2396         switch (table->ctl_name) {
2397         case KERN_SHMMAX:
2398                 data = &ns->shm_ctlmax;
2399                 goto proc_minmax;
2400         case KERN_SHMALL:
2401                 data = &ns->shm_ctlall;
2402                 goto proc_minmax;
2403         case KERN_SHMMNI:
2404                 data = &ns->shm_ctlmni;
2405                 break;
2406         case KERN_MSGMAX:
2407                 data = &ns->msg_ctlmax;
2408                 break;
2409         case KERN_MSGMNI:
2410                 data = &ns->msg_ctlmni;
2411                 break;
2412         case KERN_MSGMNB:
2413                 data = &ns->msg_ctlmnb;
2414                 break;
2415         case KERN_SEM:
2416                 data = &ns->sem_ctls;
2417                 break;
2418         default:
2419                 return -EINVAL;
2420         }
2421
2422         return __do_proc_dointvec(data, table, write, filp, buffer,
2423                         lenp, ppos, NULL, NULL);
2424 proc_minmax:
2425         return __do_proc_doulongvec_minmax(data, table, write, filp, buffer,
2426                         lenp, ppos, 1l, 1l);
2427 }
2428 #endif
2429
2430 #else /* CONFIG_PROC_FS */
2431
2432 int proc_dostring(ctl_table *table, int write, struct file *filp,
2433                   void __user *buffer, size_t *lenp, loff_t *ppos)
2434 {
2435         return -ENOSYS;
2436 }
2437
2438 static int proc_do_uts_string(ctl_table *table, int write, struct file *filp,
2439                 void __user *buffer, size_t *lenp, loff_t *ppos)
2440 {
2441         return -ENOSYS;
2442 }
2443
2444 #ifdef CONFIG_SYSVIPC
2445 static int proc_do_ipc_string(ctl_table *table, int write, struct file *filp,
2446                 void __user *buffer, size_t *lenp, loff_t *ppos)
2447 {
2448         return -ENOSYS;
2449 }
2450 #endif
2451
2452 int proc_dointvec(ctl_table *table, int write, struct file *filp,
2453                   void __user *buffer, size_t *lenp, loff_t *ppos)
2454 {
2455         return -ENOSYS;
2456 }
2457
2458 int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
2459                         void __user *buffer, size_t *lenp, loff_t *ppos)
2460 {
2461         return -ENOSYS;
2462 }
2463
2464 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
2465                     void __user *buffer, size_t *lenp, loff_t *ppos)
2466 {
2467         return -ENOSYS;
2468 }
2469
2470 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
2471                     void __user *buffer, size_t *lenp, loff_t *ppos)
2472 {
2473         return -ENOSYS;
2474 }
2475
2476 int proc_dointvec_userhz_jiffies(ctl_table *table, int write, struct file *filp,
2477                     void __user *buffer, size_t *lenp, loff_t *ppos)
2478 {
2479         return -ENOSYS;
2480 }
2481
2482 int proc_dointvec_ms_jiffies(ctl_table *table, int write, struct file *filp,
2483                              void __user *buffer, size_t *lenp, loff_t *ppos)
2484 {
2485         return -ENOSYS;
2486 }
2487
2488 int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
2489                     void __user *buffer, size_t *lenp, loff_t *ppos)
2490 {
2491         return -ENOSYS;
2492 }
2493
2494 int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
2495                                       struct file *filp,
2496                                       void __user *buffer,
2497                                       size_t *lenp, loff_t *ppos)
2498 {
2499     return -ENOSYS;
2500 }
2501
2502
2503 #endif /* CONFIG_PROC_FS */
2504
2505
2506 #ifdef CONFIG_SYSCTL_SYSCALL
2507 /*
2508  * General sysctl support routines 
2509  */
2510
2511 /* The generic string strategy routine: */
2512 int sysctl_string(ctl_table *table, int __user *name, int nlen,
2513                   void __user *oldval, size_t __user *oldlenp,
2514                   void __user *newval, size_t newlen, void **context)
2515 {
2516         if (!table->data || !table->maxlen) 
2517                 return -ENOTDIR;
2518         
2519         if (oldval && oldlenp) {
2520                 size_t bufsize;
2521                 if (get_user(bufsize, oldlenp))
2522                         return -EFAULT;
2523                 if (bufsize) {
2524                         size_t len = strlen(table->data), copied;
2525
2526                         /* This shouldn't trigger for a well-formed sysctl */
2527                         if (len > table->maxlen)
2528                                 len = table->maxlen;
2529
2530                         /* Copy up to a max of bufsize-1 bytes of the string */
2531                         copied = (len >= bufsize) ? bufsize - 1 : len;
2532
2533                         if (copy_to_user(oldval, table->data, copied) ||
2534                             put_user(0, (char __user *)(oldval + copied)))
2535                                 return -EFAULT;
2536                         if (put_user(len, oldlenp))
2537                                 return -EFAULT;
2538                 }
2539         }
2540         if (newval && newlen) {
2541                 size_t len = newlen;
2542                 if (len > table->maxlen)
2543                         len = table->maxlen;
2544                 if(copy_from_user(table->data, newval, len))
2545                         return -EFAULT;
2546                 if (len == table->maxlen)
2547                         len--;
2548                 ((char *) table->data)[len] = 0;
2549         }
2550         return 1;
2551 }
2552
2553 /*
2554  * This function makes sure that all of the integers in the vector
2555  * are between the minimum and maximum values given in the arrays
2556  * table->extra1 and table->extra2, respectively.
2557  */
2558 int sysctl_intvec(ctl_table *table, int __user *name, int nlen,
2559                 void __user *oldval, size_t __user *oldlenp,
2560                 void __user *newval, size_t newlen, void **context)
2561 {
2562
2563         if (newval && newlen) {
2564                 int __user *vec = (int __user *) newval;
2565                 int *min = (int *) table->extra1;
2566                 int *max = (int *) table->extra2;
2567                 size_t length;
2568                 int i;
2569
2570                 if (newlen % sizeof(int) != 0)
2571                         return -EINVAL;
2572
2573                 if (!table->extra1 && !table->extra2)
2574                         return 0;
2575
2576                 if (newlen > table->maxlen)
2577                         newlen = table->maxlen;
2578                 length = newlen / sizeof(int);
2579
2580                 for (i = 0; i < length; i++) {
2581                         int value;
2582                         if (get_user(value, vec + i))
2583                                 return -EFAULT;
2584                         if (min && value < min[i])
2585                                 return -EINVAL;
2586                         if (max && value > max[i])
2587                                 return -EINVAL;
2588                 }
2589         }
2590         return 0;
2591 }
2592
2593 /* Strategy function to convert jiffies to seconds */ 
2594 int sysctl_jiffies(ctl_table *table, int __user *name, int nlen,
2595                 void __user *oldval, size_t __user *oldlenp,
2596                 void __user *newval, size_t newlen, void **context)
2597 {
2598         if (oldval) {
2599                 size_t olen;
2600                 if (oldlenp) { 
2601                         if (get_user(olen, oldlenp))
2602                                 return -EFAULT;
2603                         if (olen!=sizeof(int))
2604                                 return -EINVAL; 
2605                 }
2606                 if (put_user(*(int *)(table->data)/HZ, (int __user *)oldval) ||
2607                     (oldlenp && put_user(sizeof(int),oldlenp)))
2608                         return -EFAULT;
2609         }
2610         if (newval && newlen) { 
2611                 int new;
2612                 if (newlen != sizeof(int))
2613                         return -EINVAL; 
2614                 if (get_user(new, (int __user *)newval))
2615                         return -EFAULT;
2616                 *(int *)(table->data) = new*HZ; 
2617         }
2618         return 1;
2619 }
2620
2621 /* Strategy function to convert jiffies to seconds */ 
2622 int sysctl_ms_jiffies(ctl_table *table, int __user *name, int nlen,
2623                 void __user *oldval, size_t __user *oldlenp,
2624                 void __user *newval, size_t newlen, void **context)
2625 {
2626         if (oldval) {
2627                 size_t olen;
2628                 if (oldlenp) { 
2629                         if (get_user(olen, oldlenp))
2630                                 return -EFAULT;
2631                         if (olen!=sizeof(int))
2632                                 return -EINVAL; 
2633                 }
2634                 if (put_user(jiffies_to_msecs(*(int *)(table->data)), (int __user *)oldval) ||
2635                     (oldlenp && put_user(sizeof(int),oldlenp)))
2636                         return -EFAULT;
2637         }
2638         if (newval && newlen) { 
2639                 int new;
2640                 if (newlen != sizeof(int))
2641                         return -EINVAL; 
2642                 if (get_user(new, (int __user *)newval))
2643                         return -EFAULT;
2644                 *(int *)(table->data) = msecs_to_jiffies(new);
2645         }
2646         return 1;
2647 }
2648
2649 #else /* CONFIG_SYSCTL_SYSCALL */
2650
2651
2652 asmlinkage long sys_sysctl(struct __sysctl_args __user *args)
2653 {
2654         static int msg_count;
2655
2656         if (msg_count < 5) {
2657                 msg_count++;
2658                 printk(KERN_INFO
2659                         "warning: process `%s' used the removed sysctl "
2660                         "system call\n", current->comm);
2661         }
2662         return -ENOSYS;
2663 }
2664
2665 int sysctl_string(ctl_table *table, int __user *name, int nlen,
2666                   void __user *oldval, size_t __user *oldlenp,
2667                   void __user *newval, size_t newlen, void **context)
2668 {
2669         return -ENOSYS;
2670 }
2671
2672 int sysctl_intvec(ctl_table *table, int __user *name, int nlen,
2673                 void __user *oldval, size_t __user *oldlenp,
2674                 void __user *newval, size_t newlen, void **context)
2675 {
2676         return -ENOSYS;
2677 }
2678
2679 int sysctl_jiffies(ctl_table *table, int __user *name, int nlen,
2680                 void __user *oldval, size_t __user *oldlenp,
2681                 void __user *newval, size_t newlen, void **context)
2682 {
2683         return -ENOSYS;
2684 }
2685
2686 int sysctl_ms_jiffies(ctl_table *table, int __user *name, int nlen,
2687                 void __user *oldval, size_t __user *oldlenp,
2688                 void __user *newval, size_t newlen, void **context)
2689 {
2690         return -ENOSYS;
2691 }
2692
2693 #endif /* CONFIG_SYSCTL_SYSCALL */
2694
2695 /*
2696  * No sense putting this after each symbol definition, twice,
2697  * exception granted :-)
2698  */
2699 EXPORT_SYMBOL(proc_dointvec);
2700 EXPORT_SYMBOL(proc_dointvec_jiffies);
2701 EXPORT_SYMBOL(proc_dointvec_minmax);
2702 EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
2703 EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
2704 EXPORT_SYMBOL(proc_dostring);
2705 EXPORT_SYMBOL(proc_doulongvec_minmax);
2706 EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
2707 EXPORT_SYMBOL(register_sysctl_table);
2708 EXPORT_SYMBOL(sysctl_intvec);
2709 EXPORT_SYMBOL(sysctl_jiffies);
2710 EXPORT_SYMBOL(sysctl_ms_jiffies);
2711 EXPORT_SYMBOL(sysctl_string);
2712 EXPORT_SYMBOL(unregister_sysctl_table);