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