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