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