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