blob: c3e2ac9cb5fb19e9158dc833ce6309d129c89d2d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sysctl.c: General linux system control interface
3 *
4 * Begun 24 March 1995, Stephen Tweedie
5 * Added /proc support, Dec 1995
6 * Added bdflush entry and intvec min/max checking, 2/23/96, Tom Dyas.
7 * Added hooks for /proc/sys/net (minor, minor patch), 96/4/1, Mike Shaver.
8 * Added kernel/java-{interpreter,appletviewer}, 96/5/10, Mike Shaver.
9 * Dynamic registration fixes, Stephen Tweedie.
10 * Added kswapd-interval, ctrl-alt-del, printk stuff, 1/8/97, Chris Horn.
11 * Made sysctl support optional via CONFIG_SYSCTL, 1/10/97, Chris
12 * Horn.
13 * Added proc_doulongvec_ms_jiffies_minmax, 09/08/99, Carlos H. Bauer.
14 * Added proc_doulongvec_minmax, 09/08/99, Carlos H. Bauer.
15 * Changed linked lists to use list.h instead of lists.h, 02/24/00, Bill
16 * Wendling.
17 * The list_for_each() macro wasn't appropriate for the sysctl loop.
18 * Removed it and replaced it with older style, 03/23/00, Bill Wendling
19 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/mm.h>
23#include <linux/swap.h>
24#include <linux/slab.h>
25#include <linux/sysctl.h>
26#include <linux/proc_fs.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/ctype.h>
29#include <linux/utsname.h>
30#include <linux/capability.h>
31#include <linux/smp_lock.h>
32#include <linux/init.h>
33#include <linux/kernel.h>
Kay Sievers0296b222005-11-11 05:33:52 +010034#include <linux/kobject.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030035#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sysrq.h>
37#include <linux/highuid.h>
38#include <linux/writeback.h>
39#include <linux/hugetlb.h>
40#include <linux/security.h>
41#include <linux/initrd.h>
42#include <linux/times.h>
43#include <linux/limits.h>
44#include <linux/dcache.h>
45#include <linux/syscalls.h>
Pavel Machekc255d842006-02-20 18:27:58 -080046#include <linux/nfs_fs.h>
47#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <asm/uaccess.h>
50#include <asm/processor.h>
51
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080052extern int proc_nr_files(ctl_table *table, int write, struct file *filp,
53 void __user *buffer, size_t *lenp, loff_t *ppos);
54
Andi Kleen29cbc782006-09-30 01:47:55 +020055#ifdef CONFIG_X86
56#include <asm/nmi.h>
Chuck Ebbert0741f4d2006-12-07 02:14:11 +010057#include <asm/stacktrace.h>
Andi Kleen29cbc782006-09-30 01:47:55 +020058#endif
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#if defined(CONFIG_SYSCTL)
61
62/* External variables not in a header file. */
63extern int C_A_D;
64extern int sysctl_overcommit_memory;
65extern int sysctl_overcommit_ratio;
KAMEZAWA Hiroyukifadd8fb2006-06-23 02:03:13 -070066extern int sysctl_panic_on_oom;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067extern int max_threads;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068extern int core_uses_pid;
Alan Coxd6e71142005-06-23 00:09:43 -070069extern int suid_dumpable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070extern char core_pattern[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070071extern int pid_max;
72extern int min_free_kbytes;
73extern int printk_ratelimit_jiffies;
74extern int printk_ratelimit_burst;
75extern int pid_max_min, pid_max_max;
Andrew Morton9d0243b2006-01-08 01:00:39 -080076extern int sysctl_drop_caches;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -080077extern int percpu_pagelist_fraction;
Andi Kleenbebfa102006-06-26 13:56:52 +020078extern int compat_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
81static int maxolduid = 65535;
82static int minolduid;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -080083static int min_percpu_pagelist_fract = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85static int ngroups_max = NGROUPS_MAX;
86
87#ifdef CONFIG_KMOD
88extern char modprobe_path[];
89#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#ifdef CONFIG_CHR_DEV_SG
91extern int sg_big_buff;
92#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94#ifdef __sparc__
95extern char reboot_command [];
96extern int stop_a_enabled;
97extern int scons_pwroff;
98#endif
99
100#ifdef __hppa__
101extern int pwrsw_enabled;
102extern int unaligned_enabled;
103#endif
104
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800105#ifdef CONFIG_S390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#ifdef CONFIG_MATHEMU
107extern int sysctl_ieee_emulation_warnings;
108#endif
109extern int sysctl_userprocess_debug;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700110extern int spin_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#endif
112
113extern int sysctl_hz_timer;
114
115#ifdef CONFIG_BSD_PROCESS_ACCT
116extern int acct_parm[];
117#endif
118
Jes Sorensend2b176e2006-02-28 09:42:23 -0800119#ifdef CONFIG_IA64
120extern int no_unaligned_warning;
121#endif
122
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700123#ifdef CONFIG_RT_MUTEXES
124extern int max_lock_depth;
125#endif
126
Eric W. Biedermanb89a8172006-09-27 01:51:04 -0700127#ifdef CONFIG_SYSCTL_SYSCALL
128static int parse_table(int __user *, int, void __user *, size_t __user *,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -0800129 void __user *, size_t, ctl_table *);
Eric W. Biedermanb89a8172006-09-27 01:51:04 -0700130#endif
131
Eric W. Biederman6b49a252006-12-08 02:39:57 -0800132
Randy Dunlapd6f8ff72006-10-19 23:28:34 -0700133#ifdef CONFIG_PROC_SYSCTL
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700134static int proc_do_cad_pid(ctl_table *table, int write, struct file *filp,
135 void __user *buffer, size_t *lenp, loff_t *ppos);
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800136static int proc_dointvec_taint(ctl_table *table, int write, struct file *filp,
137 void __user *buffer, size_t *lenp, loff_t *ppos);
Randy Dunlapd6f8ff72006-10-19 23:28:34 -0700138#endif
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static ctl_table root_table[];
141static struct ctl_table_header root_table_header =
142 { root_table, LIST_HEAD_INIT(root_table_header.ctl_entry) };
143
144static ctl_table kern_table[];
145static ctl_table vm_table[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static ctl_table fs_table[];
147static ctl_table debug_table[];
148static ctl_table dev_table[];
149extern ctl_table random_table[];
150#ifdef CONFIG_UNIX98_PTYS
151extern ctl_table pty_table[];
152#endif
Amy Griffis2d9048e2006-06-01 13:10:59 -0700153#ifdef CONFIG_INOTIFY_USER
Robert Love0399cb02005-07-13 12:38:18 -0400154extern ctl_table inotify_table[];
155#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
158int sysctl_legacy_va_layout;
159#endif
160
Eric W. Biederman9bc9a6b2006-12-08 02:39:56 -0800161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* /proc declarations: */
163
Eric W. Biedermanb89a8172006-09-27 01:51:04 -0700164#ifdef CONFIG_PROC_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166static ssize_t proc_readsys(struct file *, char __user *, size_t, loff_t *);
167static ssize_t proc_writesys(struct file *, const char __user *, size_t, loff_t *);
168static int proc_opensys(struct inode *, struct file *);
169
Helge Deller15ad7cd2006-12-06 20:40:36 -0800170const struct file_operations proc_sys_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 .open = proc_opensys,
172 .read = proc_readsys,
173 .write = proc_writesys,
174};
175
176extern struct proc_dir_entry *proc_sys_root;
177
Al Viro330d57f2005-11-04 10:18:40 +0000178static void register_proc_table(ctl_table *, struct proc_dir_entry *, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static void unregister_proc_table(ctl_table *, struct proc_dir_entry *);
180#endif
181
182/* The default sysctl tables: */
183
184static ctl_table root_table[] = {
185 {
186 .ctl_name = CTL_KERN,
187 .procname = "kernel",
188 .mode = 0555,
189 .child = kern_table,
190 },
191 {
192 .ctl_name = CTL_VM,
193 .procname = "vm",
194 .mode = 0555,
195 .child = vm_table,
196 },
197#ifdef CONFIG_NET
198 {
199 .ctl_name = CTL_NET,
200 .procname = "net",
201 .mode = 0555,
202 .child = net_table,
203 },
204#endif
205 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 .ctl_name = CTL_FS,
207 .procname = "fs",
208 .mode = 0555,
209 .child = fs_table,
210 },
211 {
212 .ctl_name = CTL_DEBUG,
213 .procname = "debug",
214 .mode = 0555,
215 .child = debug_table,
216 },
217 {
218 .ctl_name = CTL_DEV,
219 .procname = "dev",
220 .mode = 0555,
221 .child = dev_table,
222 },
Robert Love0eeca282005-07-12 17:06:03 -0400223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 { .ctl_name = 0 }
225};
226
227static ctl_table kern_table[] = {
228 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .ctl_name = KERN_PANIC,
230 .procname = "panic",
231 .data = &panic_timeout,
232 .maxlen = sizeof(int),
233 .mode = 0644,
234 .proc_handler = &proc_dointvec,
235 },
236 {
237 .ctl_name = KERN_CORE_USES_PID,
238 .procname = "core_uses_pid",
239 .data = &core_uses_pid,
240 .maxlen = sizeof(int),
241 .mode = 0644,
242 .proc_handler = &proc_dointvec,
243 },
244 {
245 .ctl_name = KERN_CORE_PATTERN,
246 .procname = "core_pattern",
247 .data = core_pattern,
Andi Kleend025c9d2006-09-30 23:29:28 -0700248 .maxlen = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .mode = 0644,
250 .proc_handler = &proc_dostring,
251 .strategy = &sysctl_string,
252 },
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800253#ifdef CONFIG_PROC_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 {
255 .ctl_name = KERN_TAINTED,
256 .procname = "tainted",
257 .data = &tainted,
258 .maxlen = sizeof(int),
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800259 .mode = 0644,
260 .proc_handler = &proc_dointvec_taint,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 },
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800262#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 {
264 .ctl_name = KERN_CAP_BSET,
265 .procname = "cap-bound",
266 .data = &cap_bset,
267 .maxlen = sizeof(kernel_cap_t),
268 .mode = 0600,
269 .proc_handler = &proc_dointvec_bset,
270 },
271#ifdef CONFIG_BLK_DEV_INITRD
272 {
273 .ctl_name = KERN_REALROOTDEV,
274 .procname = "real-root-dev",
275 .data = &real_root_dev,
276 .maxlen = sizeof(int),
277 .mode = 0644,
278 .proc_handler = &proc_dointvec,
279 },
280#endif
281#ifdef __sparc__
282 {
283 .ctl_name = KERN_SPARC_REBOOT,
284 .procname = "reboot-cmd",
285 .data = reboot_command,
286 .maxlen = 256,
287 .mode = 0644,
288 .proc_handler = &proc_dostring,
289 .strategy = &sysctl_string,
290 },
291 {
292 .ctl_name = KERN_SPARC_STOP_A,
293 .procname = "stop-a",
294 .data = &stop_a_enabled,
295 .maxlen = sizeof (int),
296 .mode = 0644,
297 .proc_handler = &proc_dointvec,
298 },
299 {
300 .ctl_name = KERN_SPARC_SCONS_PWROFF,
301 .procname = "scons-poweroff",
302 .data = &scons_pwroff,
303 .maxlen = sizeof (int),
304 .mode = 0644,
305 .proc_handler = &proc_dointvec,
306 },
307#endif
308#ifdef __hppa__
309 {
310 .ctl_name = KERN_HPPA_PWRSW,
311 .procname = "soft-power",
312 .data = &pwrsw_enabled,
313 .maxlen = sizeof (int),
314 .mode = 0644,
315 .proc_handler = &proc_dointvec,
316 },
317 {
318 .ctl_name = KERN_HPPA_UNALIGNED,
319 .procname = "unaligned-trap",
320 .data = &unaligned_enabled,
321 .maxlen = sizeof (int),
322 .mode = 0644,
323 .proc_handler = &proc_dointvec,
324 },
325#endif
326 {
327 .ctl_name = KERN_CTLALTDEL,
328 .procname = "ctrl-alt-del",
329 .data = &C_A_D,
330 .maxlen = sizeof(int),
331 .mode = 0644,
332 .proc_handler = &proc_dointvec,
333 },
334 {
335 .ctl_name = KERN_PRINTK,
336 .procname = "printk",
337 .data = &console_loglevel,
338 .maxlen = 4*sizeof(int),
339 .mode = 0644,
340 .proc_handler = &proc_dointvec,
341 },
342#ifdef CONFIG_KMOD
343 {
344 .ctl_name = KERN_MODPROBE,
345 .procname = "modprobe",
346 .data = &modprobe_path,
347 .maxlen = KMOD_PATH_LEN,
348 .mode = 0644,
349 .proc_handler = &proc_dostring,
350 .strategy = &sysctl_string,
351 },
352#endif
Andrew Morton57ae2502006-06-23 02:05:47 -0700353#if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 {
355 .ctl_name = KERN_HOTPLUG,
356 .procname = "hotplug",
Kay Sievers312c0042005-11-16 09:00:00 +0100357 .data = &uevent_helper,
358 .maxlen = UEVENT_HELPER_PATH_LEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 .mode = 0644,
360 .proc_handler = &proc_dostring,
361 .strategy = &sysctl_string,
362 },
363#endif
364#ifdef CONFIG_CHR_DEV_SG
365 {
366 .ctl_name = KERN_SG_BIG_BUFF,
367 .procname = "sg-big-buff",
368 .data = &sg_big_buff,
369 .maxlen = sizeof (int),
370 .mode = 0444,
371 .proc_handler = &proc_dointvec,
372 },
373#endif
374#ifdef CONFIG_BSD_PROCESS_ACCT
375 {
376 .ctl_name = KERN_ACCT,
377 .procname = "acct",
378 .data = &acct_parm,
379 .maxlen = 3*sizeof(int),
380 .mode = 0644,
381 .proc_handler = &proc_dointvec,
382 },
383#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384#ifdef CONFIG_MAGIC_SYSRQ
385 {
386 .ctl_name = KERN_SYSRQ,
387 .procname = "sysrq",
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800388 .data = &__sysrq_enabled,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 .maxlen = sizeof (int),
390 .mode = 0644,
391 .proc_handler = &proc_dointvec,
392 },
393#endif
Randy Dunlapd6f8ff72006-10-19 23:28:34 -0700394#ifdef CONFIG_PROC_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 {
396 .ctl_name = KERN_CADPID,
397 .procname = "cad_pid",
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700398 .data = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 .maxlen = sizeof (int),
400 .mode = 0600,
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700401 .proc_handler = &proc_do_cad_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 },
Randy Dunlapd6f8ff72006-10-19 23:28:34 -0700403#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 {
405 .ctl_name = KERN_MAX_THREADS,
406 .procname = "threads-max",
407 .data = &max_threads,
408 .maxlen = sizeof(int),
409 .mode = 0644,
410 .proc_handler = &proc_dointvec,
411 },
412 {
413 .ctl_name = KERN_RANDOM,
414 .procname = "random",
415 .mode = 0555,
416 .child = random_table,
417 },
418#ifdef CONFIG_UNIX98_PTYS
419 {
420 .ctl_name = KERN_PTY,
421 .procname = "pty",
422 .mode = 0555,
423 .child = pty_table,
424 },
425#endif
426 {
427 .ctl_name = KERN_OVERFLOWUID,
428 .procname = "overflowuid",
429 .data = &overflowuid,
430 .maxlen = sizeof(int),
431 .mode = 0644,
432 .proc_handler = &proc_dointvec_minmax,
433 .strategy = &sysctl_intvec,
434 .extra1 = &minolduid,
435 .extra2 = &maxolduid,
436 },
437 {
438 .ctl_name = KERN_OVERFLOWGID,
439 .procname = "overflowgid",
440 .data = &overflowgid,
441 .maxlen = sizeof(int),
442 .mode = 0644,
443 .proc_handler = &proc_dointvec_minmax,
444 .strategy = &sysctl_intvec,
445 .extra1 = &minolduid,
446 .extra2 = &maxolduid,
447 },
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800448#ifdef CONFIG_S390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449#ifdef CONFIG_MATHEMU
450 {
451 .ctl_name = KERN_IEEE_EMULATION_WARNINGS,
452 .procname = "ieee_emulation_warnings",
453 .data = &sysctl_ieee_emulation_warnings,
454 .maxlen = sizeof(int),
455 .mode = 0644,
456 .proc_handler = &proc_dointvec,
457 },
458#endif
459#ifdef CONFIG_NO_IDLE_HZ
460 {
461 .ctl_name = KERN_HZ_TIMER,
462 .procname = "hz_timer",
463 .data = &sysctl_hz_timer,
464 .maxlen = sizeof(int),
465 .mode = 0644,
466 .proc_handler = &proc_dointvec,
467 },
468#endif
469 {
470 .ctl_name = KERN_S390_USER_DEBUG_LOGGING,
471 .procname = "userprocess_debug",
472 .data = &sysctl_userprocess_debug,
473 .maxlen = sizeof(int),
474 .mode = 0644,
475 .proc_handler = &proc_dointvec,
476 },
477#endif
478 {
479 .ctl_name = KERN_PIDMAX,
480 .procname = "pid_max",
481 .data = &pid_max,
482 .maxlen = sizeof (int),
483 .mode = 0644,
484 .proc_handler = &proc_dointvec_minmax,
485 .strategy = sysctl_intvec,
486 .extra1 = &pid_max_min,
487 .extra2 = &pid_max_max,
488 },
489 {
490 .ctl_name = KERN_PANIC_ON_OOPS,
491 .procname = "panic_on_oops",
492 .data = &panic_on_oops,
493 .maxlen = sizeof(int),
494 .mode = 0644,
495 .proc_handler = &proc_dointvec,
496 },
497 {
498 .ctl_name = KERN_PRINTK_RATELIMIT,
499 .procname = "printk_ratelimit",
500 .data = &printk_ratelimit_jiffies,
501 .maxlen = sizeof(int),
502 .mode = 0644,
503 .proc_handler = &proc_dointvec_jiffies,
504 .strategy = &sysctl_jiffies,
505 },
506 {
507 .ctl_name = KERN_PRINTK_RATELIMIT_BURST,
508 .procname = "printk_ratelimit_burst",
509 .data = &printk_ratelimit_burst,
510 .maxlen = sizeof(int),
511 .mode = 0644,
512 .proc_handler = &proc_dointvec,
513 },
514 {
515 .ctl_name = KERN_NGROUPS_MAX,
516 .procname = "ngroups_max",
517 .data = &ngroups_max,
518 .maxlen = sizeof (int),
519 .mode = 0444,
520 .proc_handler = &proc_dointvec,
521 },
522#if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86)
523 {
524 .ctl_name = KERN_UNKNOWN_NMI_PANIC,
525 .procname = "unknown_nmi_panic",
526 .data = &unknown_nmi_panic,
527 .maxlen = sizeof (int),
528 .mode = 0644,
Don Zickus2fbe7b22006-09-26 10:52:27 +0200529 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 },
Don Zickus407984f2006-09-26 10:52:27 +0200531 {
532 .ctl_name = KERN_NMI_WATCHDOG,
533 .procname = "nmi_watchdog",
534 .data = &nmi_watchdog_enabled,
535 .maxlen = sizeof (int),
536 .mode = 0644,
537 .proc_handler = &proc_nmi_enabled,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 },
539#endif
540#if defined(CONFIG_X86)
541 {
Don Zickus8da5add2006-09-26 10:52:27 +0200542 .ctl_name = KERN_PANIC_ON_NMI,
543 .procname = "panic_on_unrecovered_nmi",
544 .data = &panic_on_unrecovered_nmi,
545 .maxlen = sizeof(int),
546 .mode = 0644,
547 .proc_handler = &proc_dointvec,
548 },
549 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 .ctl_name = KERN_BOOTLOADER_TYPE,
551 .procname = "bootloader_type",
552 .data = &bootloader_type,
553 .maxlen = sizeof (int),
554 .mode = 0444,
555 .proc_handler = &proc_dointvec,
556 },
Chuck Ebbert0741f4d2006-12-07 02:14:11 +0100557 {
558 .ctl_name = CTL_UNNUMBERED,
559 .procname = "kstack_depth_to_print",
560 .data = &kstack_depth_to_print,
561 .maxlen = sizeof(int),
562 .mode = 0644,
563 .proc_handler = &proc_dointvec,
564 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565#endif
Luke Yang7a9166e2006-02-20 18:28:07 -0800566#if defined(CONFIG_MMU)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 {
568 .ctl_name = KERN_RANDOMIZE,
569 .procname = "randomize_va_space",
570 .data = &randomize_va_space,
571 .maxlen = sizeof(int),
572 .mode = 0644,
573 .proc_handler = &proc_dointvec,
574 },
Luke Yang7a9166e2006-02-20 18:28:07 -0800575#endif
Martin Schwidefsky0152fb32006-01-14 13:21:00 -0800576#if defined(CONFIG_S390) && defined(CONFIG_SMP)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700577 {
578 .ctl_name = KERN_SPIN_RETRY,
579 .procname = "spin_retry",
580 .data = &spin_retry,
581 .maxlen = sizeof (int),
582 .mode = 0644,
583 .proc_handler = &proc_dointvec,
584 },
585#endif
Pavel Machekc255d842006-02-20 18:27:58 -0800586#ifdef CONFIG_ACPI_SLEEP
587 {
588 .ctl_name = KERN_ACPI_VIDEO_FLAGS,
589 .procname = "acpi_video_flags",
590 .data = &acpi_video_flags,
591 .maxlen = sizeof (unsigned long),
592 .mode = 0644,
Stefan Seyfried7f99f062006-03-02 02:54:34 -0800593 .proc_handler = &proc_doulongvec_minmax,
Pavel Machekc255d842006-02-20 18:27:58 -0800594 },
595#endif
Jes Sorensend2b176e2006-02-28 09:42:23 -0800596#ifdef CONFIG_IA64
597 {
598 .ctl_name = KERN_IA64_UNALIGNED,
599 .procname = "ignore-unaligned-usertrap",
600 .data = &no_unaligned_warning,
601 .maxlen = sizeof (int),
602 .mode = 0644,
603 .proc_handler = &proc_dointvec,
604 },
605#endif
Andi Kleenbebfa102006-06-26 13:56:52 +0200606#ifdef CONFIG_COMPAT
607 {
608 .ctl_name = KERN_COMPAT_LOG,
609 .procname = "compat-log",
610 .data = &compat_log,
611 .maxlen = sizeof (int),
612 .mode = 0644,
613 .proc_handler = &proc_dointvec,
614 },
615#endif
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700616#ifdef CONFIG_RT_MUTEXES
617 {
618 .ctl_name = KERN_MAX_LOCK_DEPTH,
619 .procname = "max_lock_depth",
620 .data = &max_lock_depth,
621 .maxlen = sizeof(int),
622 .mode = 0644,
623 .proc_handler = &proc_dointvec,
624 },
625#endif
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 { .ctl_name = 0 }
628};
629
630/* Constants for minimum and maximum testing in vm_table.
631 We use these as one-element integer vectors. */
632static int zero;
633static int one_hundred = 100;
634
635
636static ctl_table vm_table[] = {
637 {
638 .ctl_name = VM_OVERCOMMIT_MEMORY,
639 .procname = "overcommit_memory",
640 .data = &sysctl_overcommit_memory,
641 .maxlen = sizeof(sysctl_overcommit_memory),
642 .mode = 0644,
643 .proc_handler = &proc_dointvec,
644 },
645 {
KAMEZAWA Hiroyukifadd8fb2006-06-23 02:03:13 -0700646 .ctl_name = VM_PANIC_ON_OOM,
647 .procname = "panic_on_oom",
648 .data = &sysctl_panic_on_oom,
649 .maxlen = sizeof(sysctl_panic_on_oom),
650 .mode = 0644,
651 .proc_handler = &proc_dointvec,
652 },
653 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 .ctl_name = VM_OVERCOMMIT_RATIO,
655 .procname = "overcommit_ratio",
656 .data = &sysctl_overcommit_ratio,
657 .maxlen = sizeof(sysctl_overcommit_ratio),
658 .mode = 0644,
659 .proc_handler = &proc_dointvec,
660 },
661 {
662 .ctl_name = VM_PAGE_CLUSTER,
663 .procname = "page-cluster",
664 .data = &page_cluster,
665 .maxlen = sizeof(int),
666 .mode = 0644,
667 .proc_handler = &proc_dointvec,
668 },
669 {
670 .ctl_name = VM_DIRTY_BACKGROUND,
671 .procname = "dirty_background_ratio",
672 .data = &dirty_background_ratio,
673 .maxlen = sizeof(dirty_background_ratio),
674 .mode = 0644,
675 .proc_handler = &proc_dointvec_minmax,
676 .strategy = &sysctl_intvec,
677 .extra1 = &zero,
678 .extra2 = &one_hundred,
679 },
680 {
681 .ctl_name = VM_DIRTY_RATIO,
682 .procname = "dirty_ratio",
683 .data = &vm_dirty_ratio,
684 .maxlen = sizeof(vm_dirty_ratio),
685 .mode = 0644,
686 .proc_handler = &proc_dointvec_minmax,
687 .strategy = &sysctl_intvec,
688 .extra1 = &zero,
689 .extra2 = &one_hundred,
690 },
691 {
692 .ctl_name = VM_DIRTY_WB_CS,
693 .procname = "dirty_writeback_centisecs",
Bart Samwelf6ef9432006-03-24 03:15:48 -0800694 .data = &dirty_writeback_interval,
695 .maxlen = sizeof(dirty_writeback_interval),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 .mode = 0644,
697 .proc_handler = &dirty_writeback_centisecs_handler,
698 },
699 {
700 .ctl_name = VM_DIRTY_EXPIRE_CS,
701 .procname = "dirty_expire_centisecs",
Bart Samwelf6ef9432006-03-24 03:15:48 -0800702 .data = &dirty_expire_interval,
703 .maxlen = sizeof(dirty_expire_interval),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 .mode = 0644,
Bart Samwelf6ef9432006-03-24 03:15:48 -0800705 .proc_handler = &proc_dointvec_userhz_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 },
707 {
708 .ctl_name = VM_NR_PDFLUSH_THREADS,
709 .procname = "nr_pdflush_threads",
710 .data = &nr_pdflush_threads,
711 .maxlen = sizeof nr_pdflush_threads,
712 .mode = 0444 /* read-only*/,
713 .proc_handler = &proc_dointvec,
714 },
715 {
716 .ctl_name = VM_SWAPPINESS,
717 .procname = "swappiness",
718 .data = &vm_swappiness,
719 .maxlen = sizeof(vm_swappiness),
720 .mode = 0644,
721 .proc_handler = &proc_dointvec_minmax,
722 .strategy = &sysctl_intvec,
723 .extra1 = &zero,
724 .extra2 = &one_hundred,
725 },
726#ifdef CONFIG_HUGETLB_PAGE
727 {
728 .ctl_name = VM_HUGETLB_PAGES,
729 .procname = "nr_hugepages",
730 .data = &max_huge_pages,
731 .maxlen = sizeof(unsigned long),
732 .mode = 0644,
733 .proc_handler = &hugetlb_sysctl_handler,
734 .extra1 = (void *)&hugetlb_zero,
735 .extra2 = (void *)&hugetlb_infinity,
736 },
737 {
738 .ctl_name = VM_HUGETLB_GROUP,
739 .procname = "hugetlb_shm_group",
740 .data = &sysctl_hugetlb_shm_group,
741 .maxlen = sizeof(gid_t),
742 .mode = 0644,
743 .proc_handler = &proc_dointvec,
744 },
745#endif
746 {
747 .ctl_name = VM_LOWMEM_RESERVE_RATIO,
748 .procname = "lowmem_reserve_ratio",
749 .data = &sysctl_lowmem_reserve_ratio,
750 .maxlen = sizeof(sysctl_lowmem_reserve_ratio),
751 .mode = 0644,
752 .proc_handler = &lowmem_reserve_ratio_sysctl_handler,
753 .strategy = &sysctl_intvec,
754 },
755 {
Andrew Morton9d0243b2006-01-08 01:00:39 -0800756 .ctl_name = VM_DROP_PAGECACHE,
757 .procname = "drop_caches",
758 .data = &sysctl_drop_caches,
759 .maxlen = sizeof(int),
760 .mode = 0644,
761 .proc_handler = drop_caches_sysctl_handler,
762 .strategy = &sysctl_intvec,
763 },
764 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 .ctl_name = VM_MIN_FREE_KBYTES,
766 .procname = "min_free_kbytes",
767 .data = &min_free_kbytes,
768 .maxlen = sizeof(min_free_kbytes),
769 .mode = 0644,
770 .proc_handler = &min_free_kbytes_sysctl_handler,
771 .strategy = &sysctl_intvec,
772 .extra1 = &zero,
773 },
Rohit Seth8ad4b1f2006-01-08 01:00:40 -0800774 {
775 .ctl_name = VM_PERCPU_PAGELIST_FRACTION,
776 .procname = "percpu_pagelist_fraction",
777 .data = &percpu_pagelist_fraction,
778 .maxlen = sizeof(percpu_pagelist_fraction),
779 .mode = 0644,
780 .proc_handler = &percpu_pagelist_fraction_sysctl_handler,
781 .strategy = &sysctl_intvec,
782 .extra1 = &min_percpu_pagelist_fract,
783 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784#ifdef CONFIG_MMU
785 {
786 .ctl_name = VM_MAX_MAP_COUNT,
787 .procname = "max_map_count",
788 .data = &sysctl_max_map_count,
789 .maxlen = sizeof(sysctl_max_map_count),
790 .mode = 0644,
791 .proc_handler = &proc_dointvec
792 },
793#endif
794 {
795 .ctl_name = VM_LAPTOP_MODE,
796 .procname = "laptop_mode",
797 .data = &laptop_mode,
798 .maxlen = sizeof(laptop_mode),
799 .mode = 0644,
Bart Samweled5b43f2006-03-24 03:15:49 -0800800 .proc_handler = &proc_dointvec_jiffies,
801 .strategy = &sysctl_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 },
803 {
804 .ctl_name = VM_BLOCK_DUMP,
805 .procname = "block_dump",
806 .data = &block_dump,
807 .maxlen = sizeof(block_dump),
808 .mode = 0644,
809 .proc_handler = &proc_dointvec,
810 .strategy = &sysctl_intvec,
811 .extra1 = &zero,
812 },
813 {
814 .ctl_name = VM_VFS_CACHE_PRESSURE,
815 .procname = "vfs_cache_pressure",
816 .data = &sysctl_vfs_cache_pressure,
817 .maxlen = sizeof(sysctl_vfs_cache_pressure),
818 .mode = 0644,
819 .proc_handler = &proc_dointvec,
820 .strategy = &sysctl_intvec,
821 .extra1 = &zero,
822 },
823#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
824 {
825 .ctl_name = VM_LEGACY_VA_LAYOUT,
826 .procname = "legacy_va_layout",
827 .data = &sysctl_legacy_va_layout,
828 .maxlen = sizeof(sysctl_legacy_va_layout),
829 .mode = 0644,
830 .proc_handler = &proc_dointvec,
831 .strategy = &sysctl_intvec,
832 .extra1 = &zero,
833 },
834#endif
Christoph Lameter17436602006-01-18 17:42:32 -0800835#ifdef CONFIG_NUMA
836 {
837 .ctl_name = VM_ZONE_RECLAIM_MODE,
838 .procname = "zone_reclaim_mode",
839 .data = &zone_reclaim_mode,
840 .maxlen = sizeof(zone_reclaim_mode),
841 .mode = 0644,
842 .proc_handler = &proc_dointvec,
Christoph Lameterc84db232006-02-01 03:05:29 -0800843 .strategy = &sysctl_intvec,
844 .extra1 = &zero,
Christoph Lameter17436602006-01-18 17:42:32 -0800845 },
Christoph Lameter96146342006-07-03 00:24:13 -0700846 {
847 .ctl_name = VM_MIN_UNMAPPED,
848 .procname = "min_unmapped_ratio",
849 .data = &sysctl_min_unmapped_ratio,
850 .maxlen = sizeof(sysctl_min_unmapped_ratio),
851 .mode = 0644,
852 .proc_handler = &sysctl_min_unmapped_ratio_sysctl_handler,
853 .strategy = &sysctl_intvec,
854 .extra1 = &zero,
855 .extra2 = &one_hundred,
856 },
Christoph Lameter0ff38492006-09-25 23:31:52 -0700857 {
858 .ctl_name = VM_MIN_SLAB,
859 .procname = "min_slab_ratio",
860 .data = &sysctl_min_slab_ratio,
861 .maxlen = sizeof(sysctl_min_slab_ratio),
862 .mode = 0644,
863 .proc_handler = &sysctl_min_slab_ratio_sysctl_handler,
864 .strategy = &sysctl_intvec,
865 .extra1 = &zero,
866 .extra2 = &one_hundred,
867 },
Christoph Lameter17436602006-01-18 17:42:32 -0800868#endif
Ingo Molnare6e54942006-06-27 02:53:50 -0700869#ifdef CONFIG_X86_32
870 {
871 .ctl_name = VM_VDSO_ENABLED,
872 .procname = "vdso_enabled",
873 .data = &vdso_enabled,
874 .maxlen = sizeof(vdso_enabled),
875 .mode = 0644,
876 .proc_handler = &proc_dointvec,
877 .strategy = &sysctl_intvec,
878 .extra1 = &zero,
879 },
880#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 { .ctl_name = 0 }
882};
883
Eric W. Biederman2abc26f2007-02-14 00:34:07 -0800884#if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
885static ctl_table binfmt_misc_table[] = {
886 { .ctl_name = 0 }
887};
888#endif
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890static ctl_table fs_table[] = {
891 {
892 .ctl_name = FS_NRINODE,
893 .procname = "inode-nr",
894 .data = &inodes_stat,
895 .maxlen = 2*sizeof(int),
896 .mode = 0444,
897 .proc_handler = &proc_dointvec,
898 },
899 {
900 .ctl_name = FS_STATINODE,
901 .procname = "inode-state",
902 .data = &inodes_stat,
903 .maxlen = 7*sizeof(int),
904 .mode = 0444,
905 .proc_handler = &proc_dointvec,
906 },
907 {
908 .ctl_name = FS_NRFILE,
909 .procname = "file-nr",
910 .data = &files_stat,
911 .maxlen = 3*sizeof(int),
912 .mode = 0444,
Dipankar Sarma529bf6b2006-03-07 21:55:35 -0800913 .proc_handler = &proc_nr_files,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 },
915 {
916 .ctl_name = FS_MAXFILE,
917 .procname = "file-max",
918 .data = &files_stat.max_files,
919 .maxlen = sizeof(int),
920 .mode = 0644,
921 .proc_handler = &proc_dointvec,
922 },
923 {
924 .ctl_name = FS_DENTRY,
925 .procname = "dentry-state",
926 .data = &dentry_stat,
927 .maxlen = 6*sizeof(int),
928 .mode = 0444,
929 .proc_handler = &proc_dointvec,
930 },
931 {
932 .ctl_name = FS_OVERFLOWUID,
933 .procname = "overflowuid",
934 .data = &fs_overflowuid,
935 .maxlen = sizeof(int),
936 .mode = 0644,
937 .proc_handler = &proc_dointvec_minmax,
938 .strategy = &sysctl_intvec,
939 .extra1 = &minolduid,
940 .extra2 = &maxolduid,
941 },
942 {
943 .ctl_name = FS_OVERFLOWGID,
944 .procname = "overflowgid",
945 .data = &fs_overflowgid,
946 .maxlen = sizeof(int),
947 .mode = 0644,
948 .proc_handler = &proc_dointvec_minmax,
949 .strategy = &sysctl_intvec,
950 .extra1 = &minolduid,
951 .extra2 = &maxolduid,
952 },
953 {
954 .ctl_name = FS_LEASES,
955 .procname = "leases-enable",
956 .data = &leases_enable,
957 .maxlen = sizeof(int),
958 .mode = 0644,
959 .proc_handler = &proc_dointvec,
960 },
961#ifdef CONFIG_DNOTIFY
962 {
963 .ctl_name = FS_DIR_NOTIFY,
964 .procname = "dir-notify-enable",
965 .data = &dir_notify_enable,
966 .maxlen = sizeof(int),
967 .mode = 0644,
968 .proc_handler = &proc_dointvec,
969 },
970#endif
971#ifdef CONFIG_MMU
972 {
973 .ctl_name = FS_LEASE_TIME,
974 .procname = "lease-break-time",
975 .data = &lease_break_time,
976 .maxlen = sizeof(int),
977 .mode = 0644,
978 .proc_handler = &proc_dointvec,
979 },
980 {
981 .ctl_name = FS_AIO_NR,
982 .procname = "aio-nr",
983 .data = &aio_nr,
984 .maxlen = sizeof(aio_nr),
985 .mode = 0444,
Zach Brownd55b5fd2005-11-07 00:59:31 -0800986 .proc_handler = &proc_doulongvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 },
988 {
989 .ctl_name = FS_AIO_MAX_NR,
990 .procname = "aio-max-nr",
991 .data = &aio_max_nr,
992 .maxlen = sizeof(aio_max_nr),
993 .mode = 0644,
Zach Brownd55b5fd2005-11-07 00:59:31 -0800994 .proc_handler = &proc_doulongvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 },
Amy Griffis2d9048e2006-06-01 13:10:59 -0700996#ifdef CONFIG_INOTIFY_USER
Robert Love0399cb02005-07-13 12:38:18 -0400997 {
998 .ctl_name = FS_INOTIFY,
999 .procname = "inotify",
1000 .mode = 0555,
1001 .child = inotify_table,
1002 },
1003#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004#endif
Alan Coxd6e71142005-06-23 00:09:43 -07001005 {
1006 .ctl_name = KERN_SETUID_DUMPABLE,
1007 .procname = "suid_dumpable",
1008 .data = &suid_dumpable,
1009 .maxlen = sizeof(int),
1010 .mode = 0644,
1011 .proc_handler = &proc_dointvec,
1012 },
Eric W. Biederman2abc26f2007-02-14 00:34:07 -08001013#if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
1014 {
1015 .ctl_name = CTL_UNNUMBERED,
1016 .procname = "binfmt_misc",
1017 .mode = 0555,
1018 .child = binfmt_misc_table,
1019 },
1020#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 { .ctl_name = 0 }
1022};
1023
1024static ctl_table debug_table[] = {
1025 { .ctl_name = 0 }
1026};
1027
1028static ctl_table dev_table[] = {
1029 { .ctl_name = 0 }
Robert Love0eeca282005-07-12 17:06:03 -04001030};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Al Viro330d57f2005-11-04 10:18:40 +00001032static DEFINE_SPINLOCK(sysctl_lock);
1033
1034/* called under sysctl_lock */
1035static int use_table(struct ctl_table_header *p)
1036{
1037 if (unlikely(p->unregistering))
1038 return 0;
1039 p->used++;
1040 return 1;
1041}
1042
1043/* called under sysctl_lock */
1044static void unuse_table(struct ctl_table_header *p)
1045{
1046 if (!--p->used)
1047 if (unlikely(p->unregistering))
1048 complete(p->unregistering);
1049}
1050
1051/* called under sysctl_lock, will reacquire if has to wait */
1052static void start_unregistering(struct ctl_table_header *p)
1053{
1054 /*
1055 * if p->used is 0, nobody will ever touch that entry again;
1056 * we'll eliminate all paths to it before dropping sysctl_lock
1057 */
1058 if (unlikely(p->used)) {
1059 struct completion wait;
1060 init_completion(&wait);
1061 p->unregistering = &wait;
1062 spin_unlock(&sysctl_lock);
1063 wait_for_completion(&wait);
1064 spin_lock(&sysctl_lock);
1065 }
1066 /*
1067 * do not remove from the list until nobody holds it; walking the
1068 * list in do_sysctl() relies on that.
1069 */
1070 list_del_init(&p->ctl_entry);
1071}
1072
Eric W. Biederman805b5d52007-02-14 00:34:11 -08001073void sysctl_head_finish(struct ctl_table_header *head)
1074{
1075 if (!head)
1076 return;
1077 spin_lock(&sysctl_lock);
1078 unuse_table(head);
1079 spin_unlock(&sysctl_lock);
1080}
1081
1082struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
1083{
1084 struct ctl_table_header *head;
1085 struct list_head *tmp;
1086 spin_lock(&sysctl_lock);
1087 if (prev) {
1088 tmp = &prev->ctl_entry;
1089 unuse_table(prev);
1090 goto next;
1091 }
1092 tmp = &root_table_header.ctl_entry;
1093 for (;;) {
1094 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
1095
1096 if (!use_table(head))
1097 goto next;
1098 spin_unlock(&sysctl_lock);
1099 return head;
1100 next:
1101 tmp = tmp->next;
1102 if (tmp == &root_table_header.ctl_entry)
1103 break;
1104 }
1105 spin_unlock(&sysctl_lock);
1106 return NULL;
1107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109void __init sysctl_init(void)
1110{
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07001111#ifdef CONFIG_PROC_SYSCTL
Al Viro330d57f2005-11-04 10:18:40 +00001112 register_proc_table(root_table, proc_sys_root, &root_table_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113#endif
1114}
1115
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07001116#ifdef CONFIG_SYSCTL_SYSCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117int do_sysctl(int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp,
1118 void __user *newval, size_t newlen)
1119{
Eric W. Biederman805b5d52007-02-14 00:34:11 -08001120 struct ctl_table_header *head;
Al Viro330d57f2005-11-04 10:18:40 +00001121 int error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 if (nlen <= 0 || nlen >= CTL_MAXNAME)
1124 return -ENOTDIR;
1125 if (oldval) {
1126 int old_len;
1127 if (!oldlenp || get_user(old_len, oldlenp))
1128 return -EFAULT;
1129 }
Al Viro330d57f2005-11-04 10:18:40 +00001130
Eric W. Biederman805b5d52007-02-14 00:34:11 -08001131 for (head = sysctl_head_next(NULL); head;
1132 head = sysctl_head_next(head)) {
Al Viro330d57f2005-11-04 10:18:40 +00001133 error = parse_table(name, nlen, oldval, oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08001134 newval, newlen, head->ctl_table);
Eric W. Biederman805b5d52007-02-14 00:34:11 -08001135 if (error != -ENOTDIR) {
1136 sysctl_head_finish(head);
Al Viro330d57f2005-11-04 10:18:40 +00001137 break;
Eric W. Biederman805b5d52007-02-14 00:34:11 -08001138 }
1139 }
Al Viro330d57f2005-11-04 10:18:40 +00001140 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143asmlinkage long sys_sysctl(struct __sysctl_args __user *args)
1144{
1145 struct __sysctl_args tmp;
1146 int error;
1147
1148 if (copy_from_user(&tmp, args, sizeof(tmp)))
1149 return -EFAULT;
1150
1151 lock_kernel();
1152 error = do_sysctl(tmp.name, tmp.nlen, tmp.oldval, tmp.oldlenp,
1153 tmp.newval, tmp.newlen);
1154 unlock_kernel();
1155 return error;
1156}
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07001157#endif /* CONFIG_SYSCTL_SYSCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159/*
1160 * ctl_perm does NOT grant the superuser all rights automatically, because
1161 * some sysctl variables are readonly even to root.
1162 */
1163
1164static int test_perm(int mode, int op)
1165{
1166 if (!current->euid)
1167 mode >>= 6;
1168 else if (in_egroup_p(0))
1169 mode >>= 3;
1170 if ((mode & op & 0007) == op)
1171 return 0;
1172 return -EACCES;
1173}
1174
1175static inline int ctl_perm(ctl_table *table, int op)
1176{
1177 int error;
1178 error = security_sysctl(table, op);
1179 if (error)
1180 return error;
1181 return test_perm(table->mode, op);
1182}
1183
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07001184#ifdef CONFIG_SYSCTL_SYSCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185static int parse_table(int __user *name, int nlen,
1186 void __user *oldval, size_t __user *oldlenp,
1187 void __user *newval, size_t newlen,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08001188 ctl_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 int n;
1191repeat:
1192 if (!nlen)
1193 return -ENOTDIR;
1194 if (get_user(n, name))
1195 return -EFAULT;
Eric W. Biedermand99f1602006-11-05 23:52:12 -08001196 for ( ; table->ctl_name || table->procname; table++) {
1197 if (!table->ctl_name)
1198 continue;
Eric W. Biederman6703ddf2007-02-14 00:34:07 -08001199 if (n == table->ctl_name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int error;
1201 if (table->child) {
1202 if (ctl_perm(table, 001))
1203 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 name++;
1205 nlen--;
1206 table = table->child;
1207 goto repeat;
1208 }
1209 error = do_sysctl_strategy(table, name, nlen,
1210 oldval, oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08001211 newval, newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return error;
1213 }
1214 }
1215 return -ENOTDIR;
1216}
1217
1218/* Perform the actual read/write of a sysctl table entry. */
1219int do_sysctl_strategy (ctl_table *table,
1220 int __user *name, int nlen,
1221 void __user *oldval, size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08001222 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 int op = 0, rc;
1225 size_t len;
1226
1227 if (oldval)
1228 op |= 004;
1229 if (newval)
1230 op |= 002;
1231 if (ctl_perm(table, op))
1232 return -EPERM;
1233
1234 if (table->strategy) {
1235 rc = table->strategy(table, name, nlen, oldval, oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08001236 newval, newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (rc < 0)
1238 return rc;
1239 if (rc > 0)
1240 return 0;
1241 }
1242
1243 /* If there is no strategy routine, or if the strategy returns
1244 * zero, proceed with automatic r/w */
1245 if (table->data && table->maxlen) {
1246 if (oldval && oldlenp) {
1247 if (get_user(len, oldlenp))
1248 return -EFAULT;
1249 if (len) {
1250 if (len > table->maxlen)
1251 len = table->maxlen;
1252 if(copy_to_user(oldval, table->data, len))
1253 return -EFAULT;
1254 if(put_user(len, oldlenp))
1255 return -EFAULT;
1256 }
1257 }
1258 if (newval && newlen) {
1259 len = newlen;
1260 if (len > table->maxlen)
1261 len = table->maxlen;
1262 if(copy_from_user(table->data, newval, len))
1263 return -EFAULT;
1264 }
1265 }
1266 return 0;
1267}
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07001268#endif /* CONFIG_SYSCTL_SYSCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270/**
1271 * register_sysctl_table - register a sysctl hierarchy
1272 * @table: the top-level table structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 *
1274 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1275 * array. An entry with a ctl_name of 0 terminates the table.
1276 *
1277 * The members of the &ctl_table structure are used as follows:
1278 *
1279 * ctl_name - This is the numeric sysctl value used by sysctl(2). The number
1280 * must be unique within that level of sysctl
1281 *
1282 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1283 * enter a sysctl file
1284 *
1285 * data - a pointer to data for use by proc_handler
1286 *
1287 * maxlen - the maximum size in bytes of the data
1288 *
1289 * mode - the file permissions for the /proc/sys file, and for sysctl(2)
1290 *
1291 * child - a pointer to the child sysctl table if this entry is a directory, or
1292 * %NULL.
1293 *
1294 * proc_handler - the text handler routine (described below)
1295 *
1296 * strategy - the strategy routine (described below)
1297 *
1298 * de - for internal use by the sysctl routines
1299 *
1300 * extra1, extra2 - extra pointers usable by the proc handler routines
1301 *
1302 * Leaf nodes in the sysctl tree will be represented by a single file
1303 * under /proc; non-leaf nodes will be represented by directories.
1304 *
1305 * sysctl(2) can automatically manage read and write requests through
1306 * the sysctl table. The data and maxlen fields of the ctl_table
1307 * struct enable minimal validation of the values being written to be
1308 * performed, and the mode field allows minimal authentication.
1309 *
1310 * More sophisticated management can be enabled by the provision of a
1311 * strategy routine with the table entry. This will be called before
1312 * any automatic read or write of the data is performed.
1313 *
1314 * The strategy routine may return
1315 *
1316 * < 0 - Error occurred (error is passed to user process)
1317 *
1318 * 0 - OK - proceed with automatic read or write.
1319 *
1320 * > 0 - OK - read or write has been done by the strategy routine, so
1321 * return immediately.
1322 *
1323 * There must be a proc_handler routine for any terminal nodes
1324 * mirrored under /proc/sys (non-terminals are handled by a built-in
1325 * directory handler). Several default handlers are available to
1326 * cover common cases -
1327 *
1328 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1329 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1330 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1331 *
1332 * It is the handler's job to read the input buffer from user memory
1333 * and process it. The handler should return 0 on success.
1334 *
1335 * This routine returns %NULL on a failure to register, and a pointer
1336 * to the table header on success.
1337 */
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08001338struct ctl_table_header *register_sysctl_table(ctl_table * table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct ctl_table_header *tmp;
1341 tmp = kmalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
1342 if (!tmp)
1343 return NULL;
1344 tmp->ctl_table = table;
1345 INIT_LIST_HEAD(&tmp->ctl_entry);
Al Viro330d57f2005-11-04 10:18:40 +00001346 tmp->used = 0;
1347 tmp->unregistering = NULL;
1348 spin_lock(&sysctl_lock);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08001349 list_add_tail(&tmp->ctl_entry, &root_table_header.ctl_entry);
Al Viro330d57f2005-11-04 10:18:40 +00001350 spin_unlock(&sysctl_lock);
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07001351#ifdef CONFIG_PROC_SYSCTL
Al Viro330d57f2005-11-04 10:18:40 +00001352 register_proc_table(table, proc_sys_root, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353#endif
1354 return tmp;
1355}
1356
1357/**
1358 * unregister_sysctl_table - unregister a sysctl table hierarchy
1359 * @header: the header returned from register_sysctl_table
1360 *
1361 * Unregisters the sysctl table and all children. proc entries may not
1362 * actually be removed until they are no longer used by anyone.
1363 */
1364void unregister_sysctl_table(struct ctl_table_header * header)
1365{
Al Viro330d57f2005-11-04 10:18:40 +00001366 might_sleep();
1367 spin_lock(&sysctl_lock);
1368 start_unregistering(header);
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07001369#ifdef CONFIG_PROC_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 unregister_proc_table(header->ctl_table, proc_sys_root);
1371#endif
Al Viro330d57f2005-11-04 10:18:40 +00001372 spin_unlock(&sysctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 kfree(header);
1374}
1375
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07001376#else /* !CONFIG_SYSCTL */
1377struct ctl_table_header * register_sysctl_table(ctl_table * table,
1378 int insert_at_head)
1379{
1380 return NULL;
1381}
1382
1383void unregister_sysctl_table(struct ctl_table_header * table)
1384{
1385}
1386
1387#endif /* CONFIG_SYSCTL */
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389/*
1390 * /proc/sys support
1391 */
1392
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07001393#ifdef CONFIG_PROC_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395/* Scan the sysctl entries in table and add them all into /proc */
Al Viro330d57f2005-11-04 10:18:40 +00001396static void register_proc_table(ctl_table * table, struct proc_dir_entry *root, void *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 struct proc_dir_entry *de;
1399 int len;
1400 mode_t mode;
1401
Eric W. Biedermand99f1602006-11-05 23:52:12 -08001402 for (; table->ctl_name || table->procname; table++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /* Can't do anything without a proc name. */
1404 if (!table->procname)
1405 continue;
1406 /* Maybe we can't do anything with it... */
1407 if (!table->proc_handler && !table->child) {
1408 printk(KERN_WARNING "SYSCTL: Can't register %s\n",
1409 table->procname);
1410 continue;
1411 }
1412
1413 len = strlen(table->procname);
1414 mode = table->mode;
1415
1416 de = NULL;
1417 if (table->proc_handler)
1418 mode |= S_IFREG;
1419 else {
1420 mode |= S_IFDIR;
1421 for (de = root->subdir; de; de = de->next) {
1422 if (proc_match(len, table->procname, de))
1423 break;
1424 }
1425 /* If the subdir exists already, de is non-NULL */
1426 }
1427
1428 if (!de) {
1429 de = create_proc_entry(table->procname, mode, root);
1430 if (!de)
1431 continue;
Al Viro330d57f2005-11-04 10:18:40 +00001432 de->set = set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 de->data = (void *) table;
1434 if (table->proc_handler)
1435 de->proc_fops = &proc_sys_file_operations;
1436 }
1437 table->de = de;
1438 if (de->mode & S_IFDIR)
Al Viro330d57f2005-11-04 10:18:40 +00001439 register_proc_table(table->child, de, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441}
1442
1443/*
1444 * Unregister a /proc sysctl table and any subdirectories.
1445 */
1446static void unregister_proc_table(ctl_table * table, struct proc_dir_entry *root)
1447{
1448 struct proc_dir_entry *de;
Eric W. Biedermand99f1602006-11-05 23:52:12 -08001449 for (; table->ctl_name || table->procname; table++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (!(de = table->de))
1451 continue;
1452 if (de->mode & S_IFDIR) {
1453 if (!table->child) {
1454 printk (KERN_ALERT "Help - malformed sysctl tree on free\n");
1455 continue;
1456 }
1457 unregister_proc_table(table->child, de);
1458
1459 /* Don't unregister directories which still have entries.. */
1460 if (de->subdir)
1461 continue;
1462 }
1463
Al Viro330d57f2005-11-04 10:18:40 +00001464 /*
1465 * In any case, mark the entry as goner; we'll keep it
1466 * around if it's busy, but we'll know to do nothing with
1467 * its fields. We are under sysctl_lock here.
1468 */
1469 de->data = NULL;
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /* Don't unregister proc entries that are still being used.. */
1472 if (atomic_read(&de->count))
1473 continue;
1474
1475 table->de = NULL;
1476 remove_proc_entry(table->procname, root);
1477 }
1478}
1479
1480static ssize_t do_rw_proc(int write, struct file * file, char __user * buf,
1481 size_t count, loff_t *ppos)
1482{
1483 int op;
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -08001484 struct proc_dir_entry *de = PDE(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 struct ctl_table *table;
1486 size_t res;
Al Viro330d57f2005-11-04 10:18:40 +00001487 ssize_t error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Al Viro330d57f2005-11-04 10:18:40 +00001489 spin_lock(&sysctl_lock);
1490 if (de && de->data && use_table(de->set)) {
1491 /*
1492 * at that point we know that sysctl was not unregistered
1493 * and won't be until we finish
1494 */
1495 spin_unlock(&sysctl_lock);
1496 table = (struct ctl_table *) de->data;
1497 if (!table || !table->proc_handler)
1498 goto out;
1499 error = -EPERM;
1500 op = (write ? 002 : 004);
1501 if (ctl_perm(table, op))
1502 goto out;
1503
1504 /* careful: calling conventions are nasty here */
1505 res = count;
1506 error = (*table->proc_handler)(table, write, file,
1507 buf, &res, ppos);
1508 if (!error)
1509 error = res;
1510 out:
1511 spin_lock(&sysctl_lock);
1512 unuse_table(de->set);
1513 }
1514 spin_unlock(&sysctl_lock);
1515 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
1518static int proc_opensys(struct inode *inode, struct file *file)
1519{
1520 if (file->f_mode & FMODE_WRITE) {
1521 /*
1522 * sysctl entries that are not writable,
1523 * are _NOT_ writable, capabilities or not.
1524 */
1525 if (!(inode->i_mode & S_IWUSR))
1526 return -EPERM;
1527 }
1528
1529 return 0;
1530}
1531
1532static ssize_t proc_readsys(struct file * file, char __user * buf,
1533 size_t count, loff_t *ppos)
1534{
1535 return do_rw_proc(0, file, buf, count, ppos);
1536}
1537
1538static ssize_t proc_writesys(struct file * file, const char __user * buf,
1539 size_t count, loff_t *ppos)
1540{
1541 return do_rw_proc(1, file, (char __user *) buf, count, ppos);
1542}
1543
Adrian Bunkb1ba4dd2006-10-02 02:18:05 -07001544static int _proc_do_string(void* data, int maxlen, int write,
1545 struct file *filp, void __user *buffer,
1546 size_t *lenp, loff_t *ppos)
Sam Vilainf5dd3d62006-10-02 02:18:04 -07001547{
1548 size_t len;
1549 char __user *p;
1550 char c;
Oleg Nesterov8d060872007-02-10 01:46:38 -08001551
1552 if (!data || !maxlen || !*lenp) {
Sam Vilainf5dd3d62006-10-02 02:18:04 -07001553 *lenp = 0;
1554 return 0;
1555 }
Oleg Nesterov8d060872007-02-10 01:46:38 -08001556
Sam Vilainf5dd3d62006-10-02 02:18:04 -07001557 if (write) {
1558 len = 0;
1559 p = buffer;
1560 while (len < *lenp) {
1561 if (get_user(c, p++))
1562 return -EFAULT;
1563 if (c == 0 || c == '\n')
1564 break;
1565 len++;
1566 }
1567 if (len >= maxlen)
1568 len = maxlen-1;
1569 if(copy_from_user(data, buffer, len))
1570 return -EFAULT;
1571 ((char *) data)[len] = 0;
1572 *ppos += *lenp;
1573 } else {
1574 len = strlen(data);
1575 if (len > maxlen)
1576 len = maxlen;
Oleg Nesterov8d060872007-02-10 01:46:38 -08001577
1578 if (*ppos > len) {
1579 *lenp = 0;
1580 return 0;
1581 }
1582
1583 data += *ppos;
1584 len -= *ppos;
1585
Sam Vilainf5dd3d62006-10-02 02:18:04 -07001586 if (len > *lenp)
1587 len = *lenp;
1588 if (len)
1589 if(copy_to_user(buffer, data, len))
1590 return -EFAULT;
1591 if (len < *lenp) {
1592 if(put_user('\n', ((char __user *) buffer) + len))
1593 return -EFAULT;
1594 len++;
1595 }
1596 *lenp = len;
1597 *ppos += len;
1598 }
1599 return 0;
1600}
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602/**
1603 * proc_dostring - read a string sysctl
1604 * @table: the sysctl table
1605 * @write: %TRUE if this is a write to the sysctl file
1606 * @filp: the file structure
1607 * @buffer: the user buffer
1608 * @lenp: the size of the user buffer
1609 * @ppos: file position
1610 *
1611 * Reads/writes a string from/to the user buffer. If the kernel
1612 * buffer provided is not large enough to hold the string, the
1613 * string is truncated. The copied string is %NULL-terminated.
1614 * If the string is being read by the user process, it is copied
1615 * and a newline '\n' is added. It is truncated if the buffer is
1616 * not large enough.
1617 *
1618 * Returns 0 on success.
1619 */
1620int proc_dostring(ctl_table *table, int write, struct file *filp,
1621 void __user *buffer, size_t *lenp, loff_t *ppos)
1622{
Sam Vilainf5dd3d62006-10-02 02:18:04 -07001623 return _proc_do_string(table->data, table->maxlen, write, filp,
1624 buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628static int do_proc_dointvec_conv(int *negp, unsigned long *lvalp,
1629 int *valp,
1630 int write, void *data)
1631{
1632 if (write) {
1633 *valp = *negp ? -*lvalp : *lvalp;
1634 } else {
1635 int val = *valp;
1636 if (val < 0) {
1637 *negp = -1;
1638 *lvalp = (unsigned long)-val;
1639 } else {
1640 *negp = 0;
1641 *lvalp = (unsigned long)val;
1642 }
1643 }
1644 return 0;
1645}
1646
Kirill Korotaevfcfbd542006-10-02 02:18:23 -07001647static int __do_proc_dointvec(void *tbl_data, ctl_table *table,
1648 int write, struct file *filp, void __user *buffer,
1649 size_t *lenp, loff_t *ppos,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 int (*conv)(int *negp, unsigned long *lvalp, int *valp,
1651 int write, void *data),
1652 void *data)
1653{
1654#define TMPBUFLEN 21
1655 int *i, vleft, first=1, neg, val;
1656 unsigned long lval;
1657 size_t left, len;
1658
1659 char buf[TMPBUFLEN], *p;
1660 char __user *s = buffer;
1661
Kirill Korotaevfcfbd542006-10-02 02:18:23 -07001662 if (!tbl_data || !table->maxlen || !*lenp ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 (*ppos && !write)) {
1664 *lenp = 0;
1665 return 0;
1666 }
1667
Kirill Korotaevfcfbd542006-10-02 02:18:23 -07001668 i = (int *) tbl_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 vleft = table->maxlen / sizeof(*i);
1670 left = *lenp;
1671
1672 if (!conv)
1673 conv = do_proc_dointvec_conv;
1674
1675 for (; left && vleft--; i++, first=0) {
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 s++;
1685 }
1686 if (!left)
1687 break;
1688 neg = 0;
1689 len = left;
1690 if (len > sizeof(buf) - 1)
1691 len = sizeof(buf) - 1;
1692 if (copy_from_user(buf, s, len))
1693 return -EFAULT;
1694 buf[len] = 0;
1695 p = buf;
1696 if (*p == '-' && left > 1) {
1697 neg = 1;
BP, Praveenbd9b0ba2006-12-06 20:39:09 -08001698 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700 if (*p < '0' || *p > '9')
1701 break;
1702
1703 lval = simple_strtoul(p, &p, 0);
1704
1705 len = p-buf;
1706 if ((len < left) && *p && !isspace(*p))
1707 break;
1708 if (neg)
1709 val = -val;
1710 s += len;
1711 left -= len;
1712
1713 if (conv(&neg, &lval, i, 1, data))
1714 break;
1715 } else {
1716 p = buf;
1717 if (!first)
1718 *p++ = '\t';
1719
1720 if (conv(&neg, &lval, i, 0, data))
1721 break;
1722
1723 sprintf(p, "%s%lu", neg ? "-" : "", lval);
1724 len = strlen(buf);
1725 if (len > left)
1726 len = left;
1727 if(copy_to_user(s, buf, len))
1728 return -EFAULT;
1729 left -= len;
1730 s += len;
1731 }
1732 }
1733
1734 if (!write && !first && left) {
1735 if(put_user('\n', s))
1736 return -EFAULT;
1737 left--, s++;
1738 }
1739 if (write) {
1740 while (left) {
1741 char c;
1742 if (get_user(c, s++))
1743 return -EFAULT;
1744 if (!isspace(c))
1745 break;
1746 left--;
1747 }
1748 }
1749 if (write && first)
1750 return -EINVAL;
1751 *lenp -= left;
1752 *ppos += *lenp;
1753 return 0;
1754#undef TMPBUFLEN
1755}
1756
Kirill Korotaevfcfbd542006-10-02 02:18:23 -07001757static int do_proc_dointvec(ctl_table *table, int write, struct file *filp,
1758 void __user *buffer, size_t *lenp, loff_t *ppos,
1759 int (*conv)(int *negp, unsigned long *lvalp, int *valp,
1760 int write, void *data),
1761 void *data)
1762{
1763 return __do_proc_dointvec(table->data, table, write, filp,
1764 buffer, lenp, ppos, conv, data);
1765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/**
1768 * proc_dointvec - read a vector of integers
1769 * @table: the sysctl table
1770 * @write: %TRUE if this is a write to the sysctl file
1771 * @filp: the file structure
1772 * @buffer: the user buffer
1773 * @lenp: the size of the user buffer
1774 * @ppos: file position
1775 *
1776 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1777 * values from/to the user buffer, treated as an ASCII string.
1778 *
1779 * Returns 0 on success.
1780 */
1781int proc_dointvec(ctl_table *table, int write, struct file *filp,
1782 void __user *buffer, size_t *lenp, loff_t *ppos)
1783{
1784 return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
1785 NULL,NULL);
1786}
1787
1788#define OP_SET 0
1789#define OP_AND 1
Theodore Ts'o34f5a392007-02-10 01:45:24 -08001790#define OP_OR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792static int do_proc_dointvec_bset_conv(int *negp, unsigned long *lvalp,
1793 int *valp,
1794 int write, void *data)
1795{
1796 int op = *(int *)data;
1797 if (write) {
1798 int val = *negp ? -*lvalp : *lvalp;
1799 switch(op) {
1800 case OP_SET: *valp = val; break;
1801 case OP_AND: *valp &= val; break;
Theodore Ts'o34f5a392007-02-10 01:45:24 -08001802 case OP_OR: *valp |= val; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804 } else {
1805 int val = *valp;
1806 if (val < 0) {
1807 *negp = -1;
1808 *lvalp = (unsigned long)-val;
1809 } else {
1810 *negp = 0;
1811 *lvalp = (unsigned long)val;
1812 }
1813 }
1814 return 0;
1815}
1816
1817/*
1818 * init may raise the set.
1819 */
1820
1821int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
1822 void __user *buffer, size_t *lenp, loff_t *ppos)
1823{
1824 int op;
1825
Eric Paris6ff1b442007-02-10 01:43:19 -08001826 if (write && !capable(CAP_SYS_MODULE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return -EPERM;
1828 }
1829
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -07001830 op = is_init(current) ? OP_SET : OP_AND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
1832 do_proc_dointvec_bset_conv,&op);
1833}
1834
Theodore Ts'o34f5a392007-02-10 01:45:24 -08001835/*
1836 * Taint values can only be increased
1837 */
1838static int proc_dointvec_taint(ctl_table *table, int write, struct file *filp,
1839 void __user *buffer, size_t *lenp, loff_t *ppos)
1840{
1841 int op;
1842
1843 if (!capable(CAP_SYS_ADMIN))
1844 return -EPERM;
1845
1846 op = OP_OR;
1847 return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
1848 do_proc_dointvec_bset_conv,&op);
1849}
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851struct do_proc_dointvec_minmax_conv_param {
1852 int *min;
1853 int *max;
1854};
1855
1856static int do_proc_dointvec_minmax_conv(int *negp, unsigned long *lvalp,
1857 int *valp,
1858 int write, void *data)
1859{
1860 struct do_proc_dointvec_minmax_conv_param *param = data;
1861 if (write) {
1862 int val = *negp ? -*lvalp : *lvalp;
1863 if ((param->min && *param->min > val) ||
1864 (param->max && *param->max < val))
1865 return -EINVAL;
1866 *valp = val;
1867 } else {
1868 int val = *valp;
1869 if (val < 0) {
1870 *negp = -1;
1871 *lvalp = (unsigned long)-val;
1872 } else {
1873 *negp = 0;
1874 *lvalp = (unsigned long)val;
1875 }
1876 }
1877 return 0;
1878}
1879
1880/**
1881 * proc_dointvec_minmax - read a vector of integers with min/max values
1882 * @table: the sysctl table
1883 * @write: %TRUE if this is a write to the sysctl file
1884 * @filp: the file structure
1885 * @buffer: the user buffer
1886 * @lenp: the size of the user buffer
1887 * @ppos: file position
1888 *
1889 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1890 * values from/to the user buffer, treated as an ASCII string.
1891 *
1892 * This routine will ensure the values are within the range specified by
1893 * table->extra1 (min) and table->extra2 (max).
1894 *
1895 * Returns 0 on success.
1896 */
1897int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
1898 void __user *buffer, size_t *lenp, loff_t *ppos)
1899{
1900 struct do_proc_dointvec_minmax_conv_param param = {
1901 .min = (int *) table->extra1,
1902 .max = (int *) table->extra2,
1903 };
1904 return do_proc_dointvec(table, write, filp, buffer, lenp, ppos,
1905 do_proc_dointvec_minmax_conv, &param);
1906}
1907
Kirill Korotaevfcfbd542006-10-02 02:18:23 -07001908static int __do_proc_doulongvec_minmax(void *data, ctl_table *table, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 struct file *filp,
1910 void __user *buffer,
1911 size_t *lenp, loff_t *ppos,
1912 unsigned long convmul,
1913 unsigned long convdiv)
1914{
1915#define TMPBUFLEN 21
1916 unsigned long *i, *min, *max, val;
1917 int vleft, first=1, neg;
1918 size_t len, left;
1919 char buf[TMPBUFLEN], *p;
1920 char __user *s = buffer;
1921
Kirill Korotaevfcfbd542006-10-02 02:18:23 -07001922 if (!data || !table->maxlen || !*lenp ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 (*ppos && !write)) {
1924 *lenp = 0;
1925 return 0;
1926 }
1927
Kirill Korotaevfcfbd542006-10-02 02:18:23 -07001928 i = (unsigned long *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 min = (unsigned long *) table->extra1;
1930 max = (unsigned long *) table->extra2;
1931 vleft = table->maxlen / sizeof(unsigned long);
1932 left = *lenp;
1933
1934 for (; left && vleft--; i++, min++, max++, first=0) {
1935 if (write) {
1936 while (left) {
1937 char c;
1938 if (get_user(c, s))
1939 return -EFAULT;
1940 if (!isspace(c))
1941 break;
1942 left--;
1943 s++;
1944 }
1945 if (!left)
1946 break;
1947 neg = 0;
1948 len = left;
1949 if (len > TMPBUFLEN-1)
1950 len = TMPBUFLEN-1;
1951 if (copy_from_user(buf, s, len))
1952 return -EFAULT;
1953 buf[len] = 0;
1954 p = buf;
1955 if (*p == '-' && left > 1) {
1956 neg = 1;
BP, Praveenbd9b0ba2006-12-06 20:39:09 -08001957 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959 if (*p < '0' || *p > '9')
1960 break;
1961 val = simple_strtoul(p, &p, 0) * convmul / convdiv ;
1962 len = p-buf;
1963 if ((len < left) && *p && !isspace(*p))
1964 break;
1965 if (neg)
1966 val = -val;
1967 s += len;
1968 left -= len;
1969
1970 if(neg)
1971 continue;
1972 if ((min && val < *min) || (max && val > *max))
1973 continue;
1974 *i = val;
1975 } else {
1976 p = buf;
1977 if (!first)
1978 *p++ = '\t';
1979 sprintf(p, "%lu", convdiv * (*i) / convmul);
1980 len = strlen(buf);
1981 if (len > left)
1982 len = left;
1983 if(copy_to_user(s, buf, len))
1984 return -EFAULT;
1985 left -= len;
1986 s += len;
1987 }
1988 }
1989
1990 if (!write && !first && left) {
1991 if(put_user('\n', s))
1992 return -EFAULT;
1993 left--, s++;
1994 }
1995 if (write) {
1996 while (left) {
1997 char c;
1998 if (get_user(c, s++))
1999 return -EFAULT;
2000 if (!isspace(c))
2001 break;
2002 left--;
2003 }
2004 }
2005 if (write && first)
2006 return -EINVAL;
2007 *lenp -= left;
2008 *ppos += *lenp;
2009 return 0;
2010#undef TMPBUFLEN
2011}
2012
Kirill Korotaevfcfbd542006-10-02 02:18:23 -07002013static int do_proc_doulongvec_minmax(ctl_table *table, int write,
2014 struct file *filp,
2015 void __user *buffer,
2016 size_t *lenp, loff_t *ppos,
2017 unsigned long convmul,
2018 unsigned long convdiv)
2019{
2020 return __do_proc_doulongvec_minmax(table->data, table, write,
2021 filp, buffer, lenp, ppos, convmul, convdiv);
2022}
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024/**
2025 * proc_doulongvec_minmax - read a vector of long integers with min/max values
2026 * @table: the sysctl table
2027 * @write: %TRUE if this is a write to the sysctl file
2028 * @filp: the file structure
2029 * @buffer: the user buffer
2030 * @lenp: the size of the user buffer
2031 * @ppos: file position
2032 *
2033 * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
2034 * values from/to the user buffer, treated as an ASCII string.
2035 *
2036 * This routine will ensure the values are within the range specified by
2037 * table->extra1 (min) and table->extra2 (max).
2038 *
2039 * Returns 0 on success.
2040 */
2041int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
2042 void __user *buffer, size_t *lenp, loff_t *ppos)
2043{
2044 return do_proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos, 1l, 1l);
2045}
2046
2047/**
2048 * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
2049 * @table: the sysctl table
2050 * @write: %TRUE if this is a write to the sysctl file
2051 * @filp: the file structure
2052 * @buffer: the user buffer
2053 * @lenp: the size of the user buffer
2054 * @ppos: file position
2055 *
2056 * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
2057 * values from/to the user buffer, treated as an ASCII string. The values
2058 * are treated as milliseconds, and converted to jiffies when they are stored.
2059 *
2060 * This routine will ensure the values are within the range specified by
2061 * table->extra1 (min) and table->extra2 (max).
2062 *
2063 * Returns 0 on success.
2064 */
2065int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
2066 struct file *filp,
2067 void __user *buffer,
2068 size_t *lenp, loff_t *ppos)
2069{
2070 return do_proc_doulongvec_minmax(table, write, filp, buffer,
2071 lenp, ppos, HZ, 1000l);
2072}
2073
2074
2075static int do_proc_dointvec_jiffies_conv(int *negp, unsigned long *lvalp,
2076 int *valp,
2077 int write, void *data)
2078{
2079 if (write) {
Bart Samwelcba9f332006-03-24 03:15:50 -08002080 if (*lvalp > LONG_MAX / HZ)
2081 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 *valp = *negp ? -(*lvalp*HZ) : (*lvalp*HZ);
2083 } else {
2084 int val = *valp;
2085 unsigned long lval;
2086 if (val < 0) {
2087 *negp = -1;
2088 lval = (unsigned long)-val;
2089 } else {
2090 *negp = 0;
2091 lval = (unsigned long)val;
2092 }
2093 *lvalp = lval / HZ;
2094 }
2095 return 0;
2096}
2097
2098static int do_proc_dointvec_userhz_jiffies_conv(int *negp, unsigned long *lvalp,
2099 int *valp,
2100 int write, void *data)
2101{
2102 if (write) {
Bart Samwelcba9f332006-03-24 03:15:50 -08002103 if (USER_HZ < HZ && *lvalp > (LONG_MAX / HZ) * USER_HZ)
2104 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 *valp = clock_t_to_jiffies(*negp ? -*lvalp : *lvalp);
2106 } else {
2107 int val = *valp;
2108 unsigned long lval;
2109 if (val < 0) {
2110 *negp = -1;
2111 lval = (unsigned long)-val;
2112 } else {
2113 *negp = 0;
2114 lval = (unsigned long)val;
2115 }
2116 *lvalp = jiffies_to_clock_t(lval);
2117 }
2118 return 0;
2119}
2120
2121static int do_proc_dointvec_ms_jiffies_conv(int *negp, unsigned long *lvalp,
2122 int *valp,
2123 int write, void *data)
2124{
2125 if (write) {
2126 *valp = msecs_to_jiffies(*negp ? -*lvalp : *lvalp);
2127 } else {
2128 int val = *valp;
2129 unsigned long lval;
2130 if (val < 0) {
2131 *negp = -1;
2132 lval = (unsigned long)-val;
2133 } else {
2134 *negp = 0;
2135 lval = (unsigned long)val;
2136 }
2137 *lvalp = jiffies_to_msecs(lval);
2138 }
2139 return 0;
2140}
2141
2142/**
2143 * proc_dointvec_jiffies - read a vector of integers as seconds
2144 * @table: the sysctl table
2145 * @write: %TRUE if this is a write to the sysctl file
2146 * @filp: the file structure
2147 * @buffer: the user buffer
2148 * @lenp: the size of the user buffer
2149 * @ppos: file position
2150 *
2151 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2152 * values from/to the user buffer, treated as an ASCII string.
2153 * The values read are assumed to be in seconds, and are converted into
2154 * jiffies.
2155 *
2156 * Returns 0 on success.
2157 */
2158int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
2159 void __user *buffer, size_t *lenp, loff_t *ppos)
2160{
2161 return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
2162 do_proc_dointvec_jiffies_conv,NULL);
2163}
2164
2165/**
2166 * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds
2167 * @table: the sysctl table
2168 * @write: %TRUE if this is a write to the sysctl file
2169 * @filp: the file structure
2170 * @buffer: the user buffer
2171 * @lenp: the size of the user buffer
Randy Dunlap1e5d5332005-11-07 01:01:06 -08002172 * @ppos: pointer to the file position
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 *
2174 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2175 * values from/to the user buffer, treated as an ASCII string.
2176 * The values read are assumed to be in 1/USER_HZ seconds, and
2177 * are converted into jiffies.
2178 *
2179 * Returns 0 on success.
2180 */
2181int proc_dointvec_userhz_jiffies(ctl_table *table, int write, struct file *filp,
2182 void __user *buffer, size_t *lenp, loff_t *ppos)
2183{
2184 return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
2185 do_proc_dointvec_userhz_jiffies_conv,NULL);
2186}
2187
2188/**
2189 * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds
2190 * @table: the sysctl table
2191 * @write: %TRUE if this is a write to the sysctl file
2192 * @filp: the file structure
2193 * @buffer: the user buffer
2194 * @lenp: the size of the user buffer
Martin Waitz67be2dd2005-05-01 08:59:26 -07002195 * @ppos: file position
2196 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 *
2198 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2199 * values from/to the user buffer, treated as an ASCII string.
2200 * The values read are assumed to be in 1/1000 seconds, and
2201 * are converted into jiffies.
2202 *
2203 * Returns 0 on success.
2204 */
2205int proc_dointvec_ms_jiffies(ctl_table *table, int write, struct file *filp,
2206 void __user *buffer, size_t *lenp, loff_t *ppos)
2207{
2208 return do_proc_dointvec(table, write, filp, buffer, lenp, ppos,
2209 do_proc_dointvec_ms_jiffies_conv, NULL);
2210}
2211
Cedric Le Goater9ec52092006-10-02 02:19:00 -07002212static int proc_do_cad_pid(ctl_table *table, int write, struct file *filp,
2213 void __user *buffer, size_t *lenp, loff_t *ppos)
2214{
2215 struct pid *new_pid;
2216 pid_t tmp;
2217 int r;
2218
2219 tmp = pid_nr(cad_pid);
2220
2221 r = __do_proc_dointvec(&tmp, table, write, filp, buffer,
2222 lenp, ppos, NULL, NULL);
2223 if (r || !write)
2224 return r;
2225
2226 new_pid = find_get_pid(tmp);
2227 if (!new_pid)
2228 return -ESRCH;
2229
2230 put_pid(xchg(&cad_pid, new_pid));
2231 return 0;
2232}
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234#else /* CONFIG_PROC_FS */
2235
2236int proc_dostring(ctl_table *table, int write, struct file *filp,
2237 void __user *buffer, size_t *lenp, loff_t *ppos)
2238{
2239 return -ENOSYS;
2240}
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242int proc_dointvec(ctl_table *table, int write, struct file *filp,
2243 void __user *buffer, size_t *lenp, loff_t *ppos)
2244{
2245 return -ENOSYS;
2246}
2247
2248int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
2249 void __user *buffer, size_t *lenp, loff_t *ppos)
2250{
2251 return -ENOSYS;
2252}
2253
2254int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
2255 void __user *buffer, size_t *lenp, loff_t *ppos)
2256{
2257 return -ENOSYS;
2258}
2259
2260int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
2261 void __user *buffer, size_t *lenp, loff_t *ppos)
2262{
2263 return -ENOSYS;
2264}
2265
2266int proc_dointvec_userhz_jiffies(ctl_table *table, int write, struct file *filp,
2267 void __user *buffer, size_t *lenp, loff_t *ppos)
2268{
2269 return -ENOSYS;
2270}
2271
2272int proc_dointvec_ms_jiffies(ctl_table *table, int write, struct file *filp,
2273 void __user *buffer, size_t *lenp, loff_t *ppos)
2274{
2275 return -ENOSYS;
2276}
2277
2278int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
2279 void __user *buffer, size_t *lenp, loff_t *ppos)
2280{
2281 return -ENOSYS;
2282}
2283
2284int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
2285 struct file *filp,
2286 void __user *buffer,
2287 size_t *lenp, loff_t *ppos)
2288{
2289 return -ENOSYS;
2290}
2291
2292
2293#endif /* CONFIG_PROC_FS */
2294
2295
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07002296#ifdef CONFIG_SYSCTL_SYSCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297/*
2298 * General sysctl support routines
2299 */
2300
2301/* The generic string strategy routine: */
2302int sysctl_string(ctl_table *table, int __user *name, int nlen,
2303 void __user *oldval, size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08002304 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (!table->data || !table->maxlen)
2307 return -ENOTDIR;
2308
2309 if (oldval && oldlenp) {
Linus Torvaldsde9e0072005-12-31 17:00:29 -08002310 size_t bufsize;
2311 if (get_user(bufsize, oldlenp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return -EFAULT;
Linus Torvaldsde9e0072005-12-31 17:00:29 -08002313 if (bufsize) {
2314 size_t len = strlen(table->data), copied;
2315
2316 /* This shouldn't trigger for a well-formed sysctl */
2317 if (len > table->maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 len = table->maxlen;
Linus Torvaldsde9e0072005-12-31 17:00:29 -08002319
2320 /* Copy up to a max of bufsize-1 bytes of the string */
2321 copied = (len >= bufsize) ? bufsize - 1 : len;
2322
2323 if (copy_to_user(oldval, table->data, copied) ||
2324 put_user(0, (char __user *)(oldval + copied)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return -EFAULT;
Linus Torvaldsde9e0072005-12-31 17:00:29 -08002326 if (put_user(len, oldlenp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return -EFAULT;
2328 }
2329 }
2330 if (newval && newlen) {
Linus Torvaldsde9e0072005-12-31 17:00:29 -08002331 size_t len = newlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (len > table->maxlen)
2333 len = table->maxlen;
2334 if(copy_from_user(table->data, newval, len))
2335 return -EFAULT;
2336 if (len == table->maxlen)
2337 len--;
2338 ((char *) table->data)[len] = 0;
2339 }
Yi Yang82c9df82005-12-30 16:37:10 +08002340 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
2343/*
2344 * This function makes sure that all of the integers in the vector
2345 * are between the minimum and maximum values given in the arrays
2346 * table->extra1 and table->extra2, respectively.
2347 */
2348int sysctl_intvec(ctl_table *table, int __user *name, int nlen,
2349 void __user *oldval, size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08002350 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
2352
2353 if (newval && newlen) {
2354 int __user *vec = (int __user *) newval;
2355 int *min = (int *) table->extra1;
2356 int *max = (int *) table->extra2;
2357 size_t length;
2358 int i;
2359
2360 if (newlen % sizeof(int) != 0)
2361 return -EINVAL;
2362
2363 if (!table->extra1 && !table->extra2)
2364 return 0;
2365
2366 if (newlen > table->maxlen)
2367 newlen = table->maxlen;
2368 length = newlen / sizeof(int);
2369
2370 for (i = 0; i < length; i++) {
2371 int value;
2372 if (get_user(value, vec + i))
2373 return -EFAULT;
2374 if (min && value < min[i])
2375 return -EINVAL;
2376 if (max && value > max[i])
2377 return -EINVAL;
2378 }
2379 }
2380 return 0;
2381}
2382
2383/* Strategy function to convert jiffies to seconds */
2384int sysctl_jiffies(ctl_table *table, int __user *name, int nlen,
2385 void __user *oldval, size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08002386 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Alexey Dobriyan3ee75ac2007-02-10 01:44:39 -08002388 if (oldval && oldlenp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 size_t olen;
Alexey Dobriyan3ee75ac2007-02-10 01:44:39 -08002390
2391 if (get_user(olen, oldlenp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return -EFAULT;
Alexey Dobriyan3ee75ac2007-02-10 01:44:39 -08002393 if (olen) {
2394 int val;
2395
2396 if (olen < sizeof(int))
2397 return -EINVAL;
2398
2399 val = *(int *)(table->data) / HZ;
2400 if (put_user(val, (int __user *)oldval))
2401 return -EFAULT;
2402 if (put_user(sizeof(int), oldlenp))
2403 return -EFAULT;
2404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
2406 if (newval && newlen) {
2407 int new;
2408 if (newlen != sizeof(int))
2409 return -EINVAL;
2410 if (get_user(new, (int __user *)newval))
2411 return -EFAULT;
2412 *(int *)(table->data) = new*HZ;
2413 }
2414 return 1;
2415}
2416
2417/* Strategy function to convert jiffies to seconds */
2418int sysctl_ms_jiffies(ctl_table *table, int __user *name, int nlen,
2419 void __user *oldval, size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08002420 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Alexey Dobriyan3ee75ac2007-02-10 01:44:39 -08002422 if (oldval && oldlenp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 size_t olen;
Alexey Dobriyan3ee75ac2007-02-10 01:44:39 -08002424
2425 if (get_user(olen, oldlenp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return -EFAULT;
Alexey Dobriyan3ee75ac2007-02-10 01:44:39 -08002427 if (olen) {
2428 int val;
2429
2430 if (olen < sizeof(int))
2431 return -EINVAL;
2432
2433 val = jiffies_to_msecs(*(int *)(table->data));
2434 if (put_user(val, (int __user *)oldval))
2435 return -EFAULT;
2436 if (put_user(sizeof(int), oldlenp))
2437 return -EFAULT;
2438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440 if (newval && newlen) {
2441 int new;
2442 if (newlen != sizeof(int))
2443 return -EINVAL;
2444 if (get_user(new, (int __user *)newval))
2445 return -EFAULT;
2446 *(int *)(table->data) = msecs_to_jiffies(new);
2447 }
2448 return 1;
2449}
2450
Eric W. Biedermanc4b8b762006-12-08 02:39:55 -08002451
Eric W. Biedermanc4b8b762006-12-08 02:39:55 -08002452
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07002453#else /* CONFIG_SYSCTL_SYSCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455
2456asmlinkage long sys_sysctl(struct __sysctl_args __user *args)
2457{
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07002458 static int msg_count;
Eric W. Biederman0e009be2006-11-05 23:52:11 -08002459 struct __sysctl_args tmp;
2460 int name[CTL_MAXNAME];
2461 int i;
2462
2463 /* Read in the sysctl name for better debug message logging */
2464 if (copy_from_user(&tmp, args, sizeof(tmp)))
2465 return -EFAULT;
2466 if (tmp.nlen <= 0 || tmp.nlen >= CTL_MAXNAME)
2467 return -ENOTDIR;
2468 for (i = 0; i < tmp.nlen; i++)
2469 if (get_user(name[i], tmp.name + i))
2470 return -EFAULT;
2471
2472 /* Ignore accesses to kernel.version */
2473 if ((tmp.nlen == 2) && (name[0] == CTL_KERN) && (name[1] == KERN_VERSION))
2474 goto out;
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07002475
2476 if (msg_count < 5) {
2477 msg_count++;
2478 printk(KERN_INFO
2479 "warning: process `%s' used the removed sysctl "
Eric W. Biederman0e009be2006-11-05 23:52:11 -08002480 "system call with ", current->comm);
2481 for (i = 0; i < tmp.nlen; i++)
2482 printk("%d.", name[i]);
2483 printk("\n");
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07002484 }
Eric W. Biederman0e009be2006-11-05 23:52:11 -08002485out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 return -ENOSYS;
2487}
2488
2489int sysctl_string(ctl_table *table, int __user *name, int nlen,
2490 void __user *oldval, size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08002491 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
2493 return -ENOSYS;
2494}
2495
2496int sysctl_intvec(ctl_table *table, int __user *name, int nlen,
2497 void __user *oldval, size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08002498 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
2500 return -ENOSYS;
2501}
2502
2503int sysctl_jiffies(ctl_table *table, int __user *name, int nlen,
2504 void __user *oldval, size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08002505 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
2507 return -ENOSYS;
2508}
2509
2510int sysctl_ms_jiffies(ctl_table *table, int __user *name, int nlen,
2511 void __user *oldval, size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08002512 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
2514 return -ENOSYS;
2515}
2516
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07002517#endif /* CONFIG_SYSCTL_SYSCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519/*
2520 * No sense putting this after each symbol definition, twice,
2521 * exception granted :-)
2522 */
2523EXPORT_SYMBOL(proc_dointvec);
2524EXPORT_SYMBOL(proc_dointvec_jiffies);
2525EXPORT_SYMBOL(proc_dointvec_minmax);
2526EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
2527EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
2528EXPORT_SYMBOL(proc_dostring);
2529EXPORT_SYMBOL(proc_doulongvec_minmax);
2530EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
2531EXPORT_SYMBOL(register_sysctl_table);
2532EXPORT_SYMBOL(sysctl_intvec);
2533EXPORT_SYMBOL(sysctl_jiffies);
2534EXPORT_SYMBOL(sysctl_ms_jiffies);
2535EXPORT_SYMBOL(sysctl_string);
2536EXPORT_SYMBOL(unregister_sysctl_table);