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