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