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