2 * sysctl.c: General linux system control interface
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
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
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
21 #include <linux/config.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/ctype.h>
28 #include <linux/utsname.h>
29 #include <linux/capability.h>
30 #include <linux/smp_lock.h>
31 #include <linux/init.h>
32 #include <linux/kernel.h>
33 #include <linux/sysrq.h>
34 #include <linux/highuid.h>
35 #include <linux/writeback.h>
36 #include <linux/hugetlb.h>
37 #include <linux/security.h>
38 #include <asm/uaccess.h>
40 #ifdef CONFIG_ROOT_NFS
41 #include <linux/nfs_fs.h>
44 #if defined(CONFIG_SYSCTL)
46 /* External variables not in a header file. */
47 extern int panic_timeout;
49 extern int sysctl_overcommit_memory;
50 extern int sysctl_overcommit_ratio;
51 extern int max_threads;
52 extern atomic_t nr_queued_signals;
53 extern int max_queued_signals;
54 extern int sysrq_enabled;
55 extern int core_uses_pid;
56 extern char core_pattern[];
59 extern int sysctl_lower_zone_protection;
61 /* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
62 static int maxolduid = 65535;
66 extern char modprobe_path[];
69 extern char hotplug_path[];
71 #ifdef CONFIG_CHR_DEV_SG
72 extern int sg_big_buff;
75 extern size_t shm_ctlmax;
76 extern size_t shm_ctlall;
77 extern int shm_ctlmni;
78 extern int msg_ctlmax;
79 extern int msg_ctlmnb;
80 extern int msg_ctlmni;
81 extern int sem_ctls[];
85 extern char reboot_command [];
86 extern int stop_a_enabled;
89 #ifdef CONFIG_ARCH_S390
91 extern int sysctl_ieee_emulation_warnings;
93 extern int sysctl_userprocess_debug;
96 #if defined(CONFIG_PPC32) && defined(CONFIG_6xx)
97 extern unsigned long powersave_nap;
98 int proc_dol2crvec(ctl_table *table, int write, struct file *filp,
99 void *buffer, size_t *lenp);
102 #ifdef CONFIG_BSD_PROCESS_ACCT
103 extern int acct_parm[];
106 static int parse_table(int *, int, void *, size_t *, void *, size_t,
107 ctl_table *, void **);
108 static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
109 void *buffer, size_t *lenp);
111 static ctl_table root_table[];
112 static struct ctl_table_header root_table_header =
113 { root_table, LIST_HEAD_INIT(root_table_header.ctl_entry) };
115 static ctl_table kern_table[];
116 static ctl_table vm_table[];
118 extern ctl_table net_table[];
120 static ctl_table proc_table[];
121 static ctl_table fs_table[];
122 static ctl_table debug_table[];
123 static ctl_table dev_table[];
124 extern ctl_table random_table[];
126 /* /proc declarations: */
128 #ifdef CONFIG_PROC_FS
130 static ssize_t proc_readsys(struct file *, char *, size_t, loff_t *);
131 static ssize_t proc_writesys(struct file *, const char *, size_t, loff_t *);
132 static int proc_sys_permission(struct inode *, int);
134 struct file_operations proc_sys_file_operations = {
135 .read = proc_readsys,
136 .write = proc_writesys,
139 static struct inode_operations proc_sys_inode_operations = {
140 .permission = proc_sys_permission,
143 extern struct proc_dir_entry *proc_sys_root;
145 static void register_proc_table(ctl_table *, struct proc_dir_entry *);
146 static void unregister_proc_table(ctl_table *, struct proc_dir_entry *);
149 /* The default sysctl tables: */
151 static ctl_table root_table[] = {
152 {CTL_KERN, "kernel", NULL, 0, 0555, kern_table},
153 {CTL_VM, "vm", NULL, 0, 0555, vm_table},
155 {CTL_NET, "net", NULL, 0, 0555, net_table},
157 {CTL_PROC, "proc", NULL, 0, 0555, proc_table},
158 {CTL_FS, "fs", NULL, 0, 0555, fs_table},
159 {CTL_DEBUG, "debug", NULL, 0, 0555, debug_table},
160 {CTL_DEV, "dev", NULL, 0, 0555, dev_table},
164 static ctl_table kern_table[] = {
165 {KERN_OSTYPE, "ostype", system_utsname.sysname, 64,
166 0444, NULL, &proc_doutsstring, &sysctl_string},
167 {KERN_OSRELEASE, "osrelease", system_utsname.release, 64,
168 0444, NULL, &proc_doutsstring, &sysctl_string},
169 {KERN_VERSION, "version", system_utsname.version, 64,
170 0444, NULL, &proc_doutsstring, &sysctl_string},
171 {KERN_NODENAME, "hostname", system_utsname.nodename, 64,
172 0644, NULL, &proc_doutsstring, &sysctl_string},
173 {KERN_DOMAINNAME, "domainname", system_utsname.domainname, 64,
174 0644, NULL, &proc_doutsstring, &sysctl_string},
175 {KERN_PANIC, "panic", &panic_timeout, sizeof(int),
176 0644, NULL, &proc_dointvec},
177 {KERN_CORE_USES_PID, "core_uses_pid", &core_uses_pid, sizeof(int),
178 0644, NULL, &proc_dointvec},
179 {KERN_CORE_PATTERN, "core_pattern", core_pattern, 64,
180 0644, NULL, &proc_dostring, &sysctl_string},
181 {KERN_TAINTED, "tainted", &tainted, sizeof(int),
182 0644, NULL, &proc_dointvec},
183 {KERN_CAP_BSET, "cap-bound", &cap_bset, sizeof(kernel_cap_t),
184 0600, NULL, &proc_dointvec_bset},
185 #ifdef CONFIG_BLK_DEV_INITRD
186 {KERN_REALROOTDEV, "real-root-dev", &real_root_dev, sizeof(int),
187 0644, NULL, &proc_dointvec},
190 {KERN_SPARC_REBOOT, "reboot-cmd", reboot_command,
191 256, 0644, NULL, &proc_dostring, &sysctl_string },
192 {KERN_SPARC_STOP_A, "stop-a", &stop_a_enabled, sizeof (int),
193 0644, NULL, &proc_dointvec},
195 #if defined(CONFIG_PPC32) && defined(CONFIG_6xx)
196 {KERN_PPC_POWERSAVE_NAP, "powersave-nap", &powersave_nap, sizeof(int),
197 0644, NULL, &proc_dointvec},
198 {KERN_PPC_L2CR, "l2cr", NULL, 0,
199 0644, NULL, &proc_dol2crvec},
201 {KERN_CTLALTDEL, "ctrl-alt-del", &C_A_D, sizeof(int),
202 0644, NULL, &proc_dointvec},
203 {KERN_PRINTK, "printk", &console_loglevel, 4*sizeof(int),
204 0644, NULL, &proc_dointvec},
206 {KERN_MODPROBE, "modprobe", &modprobe_path, 256,
207 0644, NULL, &proc_dostring, &sysctl_string },
209 #ifdef CONFIG_HOTPLUG
210 {KERN_HOTPLUG, "hotplug", &hotplug_path, 256,
211 0644, NULL, &proc_dostring, &sysctl_string },
213 #ifdef CONFIG_CHR_DEV_SG
214 {KERN_SG_BIG_BUFF, "sg-big-buff", &sg_big_buff, sizeof (int),
215 0444, NULL, &proc_dointvec},
217 #ifdef CONFIG_BSD_PROCESS_ACCT
218 {KERN_ACCT, "acct", &acct_parm, 3*sizeof(int),
219 0644, NULL, &proc_dointvec},
221 {KERN_RTSIGNR, "rtsig-nr", &nr_queued_signals, sizeof(int),
222 0444, NULL, &proc_dointvec},
223 {KERN_RTSIGMAX, "rtsig-max", &max_queued_signals, sizeof(int),
224 0644, NULL, &proc_dointvec},
225 #ifdef CONFIG_SYSVIPC
226 {KERN_SHMMAX, "shmmax", &shm_ctlmax, sizeof (size_t),
227 0644, NULL, &proc_doulongvec_minmax},
228 {KERN_SHMALL, "shmall", &shm_ctlall, sizeof (size_t),
229 0644, NULL, &proc_doulongvec_minmax},
230 {KERN_SHMMNI, "shmmni", &shm_ctlmni, sizeof (int),
231 0644, NULL, &proc_dointvec},
232 {KERN_MSGMAX, "msgmax", &msg_ctlmax, sizeof (int),
233 0644, NULL, &proc_dointvec},
234 {KERN_MSGMNI, "msgmni", &msg_ctlmni, sizeof (int),
235 0644, NULL, &proc_dointvec},
236 {KERN_MSGMNB, "msgmnb", &msg_ctlmnb, sizeof (int),
237 0644, NULL, &proc_dointvec},
238 {KERN_SEM, "sem", &sem_ctls, 4*sizeof (int),
239 0644, NULL, &proc_dointvec},
241 #ifdef CONFIG_MAGIC_SYSRQ
242 {KERN_SYSRQ, "sysrq", &sysrq_enabled, sizeof (int),
243 0644, NULL, &proc_dointvec},
245 {KERN_CADPID, "cad_pid", &cad_pid, sizeof (int),
246 0600, NULL, &proc_dointvec},
247 {KERN_MAX_THREADS, "threads-max", &max_threads, sizeof(int),
248 0644, NULL, &proc_dointvec},
249 {KERN_RANDOM, "random", NULL, 0, 0555, random_table},
250 {KERN_OVERFLOWUID, "overflowuid", &overflowuid, sizeof(int), 0644, NULL,
251 &proc_dointvec_minmax, &sysctl_intvec, NULL,
252 &minolduid, &maxolduid},
253 {KERN_OVERFLOWGID, "overflowgid", &overflowgid, sizeof(int), 0644, NULL,
254 &proc_dointvec_minmax, &sysctl_intvec, NULL,
255 &minolduid, &maxolduid},
256 #ifdef CONFIG_ARCH_S390
257 #ifdef CONFIG_MATHEMU
258 {KERN_IEEE_EMULATION_WARNINGS,"ieee_emulation_warnings",
259 &sysctl_ieee_emulation_warnings,sizeof(int),0644,NULL,&proc_dointvec},
261 {KERN_S390_USER_DEBUG_LOGGING,"userprocess_debug",
262 &sysctl_userprocess_debug,sizeof(int),0644,NULL,&proc_dointvec},
264 {KERN_PIDMAX, "pid_max", &pid_max, sizeof (int),
265 0600, NULL, &proc_dointvec},
266 {KERN_PANIC_ON_OOPS,"panic_on_oops",
267 &panic_on_oops,sizeof(int),0644,NULL,&proc_dointvec},
271 /* Constants for minimum and maximum testing in vm_table.
272 We use these as one-element integer vectors. */
275 static int one_hundred = 100;
278 static ctl_table vm_table[] = {
279 {VM_OVERCOMMIT_MEMORY, "overcommit_memory", &sysctl_overcommit_memory,
280 sizeof(sysctl_overcommit_memory), 0644, NULL, &proc_dointvec},
281 {VM_OVERCOMMIT_RATIO, "overcommit_ratio",
282 &sysctl_overcommit_ratio, sizeof(sysctl_overcommit_ratio), 0644,
283 NULL, &proc_dointvec},
284 {VM_PAGE_CLUSTER, "page-cluster",
285 &page_cluster, sizeof(int), 0644, NULL, &proc_dointvec},
286 {VM_DIRTY_BACKGROUND, "dirty_background_ratio",
287 &dirty_background_ratio, sizeof(dirty_background_ratio),
288 0644, NULL, &proc_dointvec_minmax, &sysctl_intvec, NULL,
289 &zero, &one_hundred },
290 {VM_DIRTY_RATIO, "dirty_ratio", &vm_dirty_ratio,
291 sizeof(vm_dirty_ratio), 0644, NULL, &proc_dointvec_minmax,
292 &sysctl_intvec, NULL, &zero, &one_hundred },
293 {VM_DIRTY_WB_CS, "dirty_writeback_centisecs",
294 &dirty_writeback_centisecs, sizeof(dirty_writeback_centisecs), 0644,
295 NULL, &proc_dointvec_minmax, &sysctl_intvec, NULL,
296 /* Here, we define the range of possible values for
297 dirty_writeback_centisecs.
299 The default value is 5 seconds (500 centisec). We will use 1
300 centisec, the smallest possible value that could make any sort of
301 sense. If we allowed the user to set the interval to 0 seconds
302 (which would presumably mean to chew up all of the CPU looking for
303 dirty pages and writing them out, without taking a break), the
304 interval would effectively become 1 second (100 centisecs), due to
305 some nicely documented throttling code in wb_kupdate().
307 There is no maximum legal value for dirty_writeback. */
309 {VM_DIRTY_EXPIRE_CS, "dirty_expire_centisecs",
310 &dirty_expire_centisecs, sizeof(dirty_expire_centisecs), 0644,
311 NULL, &proc_dointvec},
312 { VM_NR_PDFLUSH_THREADS, "nr_pdflush_threads",
313 &nr_pdflush_threads, sizeof nr_pdflush_threads,
314 0444 /* read-only*/, NULL, &proc_dointvec},
315 {VM_SWAPPINESS, "swappiness", &vm_swappiness, sizeof(vm_swappiness),
316 0644, NULL, &proc_dointvec_minmax, &sysctl_intvec, NULL, &zero,
318 #ifdef CONFIG_HUGETLB_PAGE
319 {VM_HUGETLB_PAGES, "nr_hugepages", &htlbpage_max, sizeof(int), 0644,
320 NULL, &hugetlb_sysctl_handler},
322 {VM_LOWER_ZONE_PROTECTION, "lower_zone_protection",
323 &sysctl_lower_zone_protection, sizeof(sysctl_lower_zone_protection),
324 0644, NULL, &proc_dointvec_minmax, &sysctl_intvec, NULL, &zero,
329 static ctl_table proc_table[] = {
333 static ctl_table fs_table[] = {
334 {FS_NRINODE, "inode-nr", &inodes_stat, 2*sizeof(int),
335 0444, NULL, &proc_dointvec},
336 {FS_STATINODE, "inode-state", &inodes_stat, 7*sizeof(int),
337 0444, NULL, &proc_dointvec},
338 {FS_NRFILE, "file-nr", &files_stat, 3*sizeof(int),
339 0444, NULL, &proc_dointvec},
340 {FS_MAXFILE, "file-max", &files_stat.max_files, sizeof(int),
341 0644, NULL, &proc_dointvec},
342 {FS_DENTRY, "dentry-state", &dentry_stat, 6*sizeof(int),
343 0444, NULL, &proc_dointvec},
344 {FS_OVERFLOWUID, "overflowuid", &fs_overflowuid, sizeof(int), 0644, NULL,
345 &proc_dointvec_minmax, &sysctl_intvec, NULL,
346 &minolduid, &maxolduid},
347 {FS_OVERFLOWGID, "overflowgid", &fs_overflowgid, sizeof(int), 0644, NULL,
348 &proc_dointvec_minmax, &sysctl_intvec, NULL,
349 &minolduid, &maxolduid},
350 {FS_LEASES, "leases-enable", &leases_enable, sizeof(int),
351 0644, NULL, &proc_dointvec},
352 {FS_DIR_NOTIFY, "dir-notify-enable", &dir_notify_enable,
353 sizeof(int), 0644, NULL, &proc_dointvec},
354 {FS_LEASE_TIME, "lease-break-time", &lease_break_time, sizeof(int),
355 0644, NULL, &proc_dointvec},
359 static ctl_table debug_table[] = {
363 static ctl_table dev_table[] = {
367 extern void init_irq_proc (void);
369 void __init sysctl_init(void)
371 #ifdef CONFIG_PROC_FS
372 register_proc_table(root_table, proc_sys_root);
377 int do_sysctl(int *name, int nlen, void *oldval, size_t *oldlenp,
378 void *newval, size_t newlen)
380 struct list_head *tmp;
382 if (nlen <= 0 || nlen >= CTL_MAXNAME)
386 if (!oldlenp || get_user(old_len, oldlenp))
389 tmp = &root_table_header.ctl_entry;
391 struct ctl_table_header *head =
392 list_entry(tmp, struct ctl_table_header, ctl_entry);
393 void *context = NULL;
394 int error = parse_table(name, nlen, oldval, oldlenp,
395 newval, newlen, head->ctl_table,
399 if (error != -ENOTDIR)
402 } while (tmp != &root_table_header.ctl_entry);
406 asmlinkage long sys_sysctl(struct __sysctl_args __user *args)
408 struct __sysctl_args tmp;
411 if (copy_from_user(&tmp, args, sizeof(tmp)))
415 error = do_sysctl(tmp.name, tmp.nlen, tmp.oldval, tmp.oldlenp,
416 tmp.newval, tmp.newlen);
422 * ctl_perm does NOT grant the superuser all rights automatically, because
423 * some sysctl variables are readonly even to root.
426 static int test_perm(int mode, int op)
430 else if (in_egroup_p(0))
432 if ((mode & op & 0007) == op)
437 static inline int ctl_perm(ctl_table *table, int op)
440 error = security_sysctl(table, op);
443 return test_perm(table->mode, op);
446 static int parse_table(int *name, int nlen,
447 void __user *oldval, size_t __user *oldlenp,
448 void __user *newval, size_t newlen,
449 ctl_table *table, void **context)
455 if (get_user(n, name))
457 for ( ; table->ctl_name; table++) {
458 if (n == table->ctl_name || table->ctl_name == CTL_ANY) {
461 if (ctl_perm(table, 001))
463 if (table->strategy) {
464 error = table->strategy(
467 newval, newlen, context);
473 table = table->child;
476 error = do_sysctl_strategy(table, name, nlen,
478 newval, newlen, context);
485 /* Perform the actual read/write of a sysctl table entry. */
486 int do_sysctl_strategy (ctl_table *table,
488 void __user *oldval, size_t __user *oldlenp,
489 void __user *newval, size_t newlen, void **context)
498 if (ctl_perm(table, op))
501 if (table->strategy) {
502 rc = table->strategy(table, name, nlen, oldval, oldlenp,
503 newval, newlen, context);
510 /* If there is no strategy routine, or if the strategy returns
511 * zero, proceed with automatic r/w */
512 if (table->data && table->maxlen) {
513 if (oldval && oldlenp) {
514 get_user(len, oldlenp);
516 if (len > table->maxlen)
518 if(copy_to_user(oldval, table->data, len))
520 if(put_user(len, oldlenp))
524 if (newval && newlen) {
526 if (len > table->maxlen)
528 if(copy_from_user(table->data, newval, len))
536 * register_sysctl_table - register a sysctl hierarchy
537 * @table: the top-level table structure
538 * @insert_at_head: whether the entry should be inserted in front or at the end
540 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
541 * array. An entry with a ctl_name of 0 terminates the table.
543 * The members of the &ctl_table structure are used as follows:
545 * ctl_name - This is the numeric sysctl value used by sysctl(2). The number
546 * must be unique within that level of sysctl
548 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
549 * enter a sysctl file
551 * data - a pointer to data for use by proc_handler
553 * maxlen - the maximum size in bytes of the data
555 * mode - the file permissions for the /proc/sys file, and for sysctl(2)
557 * child - a pointer to the child sysctl table if this entry is a directory, or
560 * proc_handler - the text handler routine (described below)
562 * strategy - the strategy routine (described below)
564 * de - for internal use by the sysctl routines
566 * extra1, extra2 - extra pointers usable by the proc handler routines
568 * Leaf nodes in the sysctl tree will be represented by a single file
569 * under /proc; non-leaf nodes will be represented by directories.
571 * sysctl(2) can automatically manage read and write requests through
572 * the sysctl table. The data and maxlen fields of the ctl_table
573 * struct enable minimal validation of the values being written to be
574 * performed, and the mode field allows minimal authentication.
576 * More sophisticated management can be enabled by the provision of a
577 * strategy routine with the table entry. This will be called before
578 * any automatic read or write of the data is performed.
580 * The strategy routine may return
582 * < 0 - Error occurred (error is passed to user process)
584 * 0 - OK - proceed with automatic read or write.
586 * > 0 - OK - read or write has been done by the strategy routine, so
587 * return immediately.
589 * There must be a proc_handler routine for any terminal nodes
590 * mirrored under /proc/sys (non-terminals are handled by a built-in
591 * directory handler). Several default handlers are available to
592 * cover common cases -
594 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
595 * proc_dointvec_minmax(), proc_doulongvec_ms_jiffies_minmax(),
596 * proc_doulongvec_minmax()
598 * It is the handler's job to read the input buffer from user memory
599 * and process it. The handler should return 0 on success.
601 * This routine returns %NULL on a failure to register, and a pointer
602 * to the table header on success.
604 struct ctl_table_header *register_sysctl_table(ctl_table * table,
607 struct ctl_table_header *tmp;
608 tmp = kmalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
611 tmp->ctl_table = table;
612 INIT_LIST_HEAD(&tmp->ctl_entry);
614 list_add(&tmp->ctl_entry, &root_table_header.ctl_entry);
616 list_add_tail(&tmp->ctl_entry, &root_table_header.ctl_entry);
617 #ifdef CONFIG_PROC_FS
618 register_proc_table(table, proc_sys_root);
624 * unregister_sysctl_table - unregister a sysctl table hierarchy
625 * @header: the header returned from register_sysctl_table
627 * Unregisters the sysctl table and all children. proc entries may not
628 * actually be removed until they are no longer used by anyone.
630 void unregister_sysctl_table(struct ctl_table_header * header)
632 list_del(&header->ctl_entry);
633 #ifdef CONFIG_PROC_FS
634 unregister_proc_table(header->ctl_table, proc_sys_root);
643 #ifdef CONFIG_PROC_FS
645 /* Scan the sysctl entries in table and add them all into /proc */
646 static void register_proc_table(ctl_table * table, struct proc_dir_entry *root)
648 struct proc_dir_entry *de;
652 for (; table->ctl_name; table++) {
653 /* Can't do anything without a proc name. */
654 if (!table->procname)
656 /* Maybe we can't do anything with it... */
657 if (!table->proc_handler && !table->child) {
658 printk(KERN_WARNING "SYSCTL: Can't register %s\n",
663 len = strlen(table->procname);
667 if (table->proc_handler)
671 for (de = root->subdir; de; de = de->next) {
672 if (proc_match(len, table->procname, de))
675 /* If the subdir exists already, de is non-NULL */
679 de = create_proc_entry(table->procname, mode, root);
682 de->data = (void *) table;
683 if (table->proc_handler) {
684 de->proc_fops = &proc_sys_file_operations;
685 de->proc_iops = &proc_sys_inode_operations;
689 if (de->mode & S_IFDIR)
690 register_proc_table(table->child, de);
695 * Unregister a /proc sysctl table and any subdirectories.
697 static void unregister_proc_table(ctl_table * table, struct proc_dir_entry *root)
699 struct proc_dir_entry *de;
700 for (; table->ctl_name; table++) {
701 if (!(de = table->de))
703 if (de->mode & S_IFDIR) {
705 printk (KERN_ALERT "Help - malformed sysctl tree on free\n");
708 unregister_proc_table(table->child, de);
710 /* Don't unregister directories which still have entries.. */
715 /* Don't unregister proc entries that are still being used.. */
716 if (atomic_read(&de->count))
720 remove_proc_entry(table->procname, root);
724 static ssize_t do_rw_proc(int write, struct file * file, char * buf,
725 size_t count, loff_t *ppos)
728 struct proc_dir_entry *de;
729 struct ctl_table *table;
733 de = PDE(file->f_dentry->d_inode);
734 if (!de || !de->data)
736 table = (struct ctl_table *) de->data;
737 if (!table || !table->proc_handler)
739 op = (write ? 002 : 004);
740 if (ctl_perm(table, op))
746 * FIXME: we need to pass on ppos to the handler.
749 error = (*table->proc_handler) (table, write, file, buf, &res);
755 static ssize_t proc_readsys(struct file * file, char * buf,
756 size_t count, loff_t *ppos)
758 return do_rw_proc(0, file, buf, count, ppos);
761 static ssize_t proc_writesys(struct file * file, const char * buf,
762 size_t count, loff_t *ppos)
764 return do_rw_proc(1, file, (char *) buf, count, ppos);
767 static int proc_sys_permission(struct inode *inode, int op)
769 return test_perm(inode->i_mode, op);
773 * proc_dostring - read a string sysctl
774 * @table: the sysctl table
775 * @write: %TRUE if this is a write to the sysctl file
776 * @filp: the file structure
777 * @buffer: the user buffer
778 * @lenp: the size of the user buffer
780 * Reads/writes a string from/to the user buffer. If the kernel
781 * buffer provided is not large enough to hold the string, the
782 * string is truncated. The copied string is %NULL-terminated.
783 * If the string is being read by the user process, it is copied
784 * and a newline '\n' is added. It is truncated if the buffer is
787 * Returns 0 on success.
789 int proc_dostring(ctl_table *table, int write, struct file *filp,
790 void __user *buffer, size_t *lenp)
796 if (!table->data || !table->maxlen || !*lenp ||
797 (filp->f_pos && !write)) {
805 while (len < *lenp) {
808 if (c == 0 || c == '\n')
812 if (len >= table->maxlen)
813 len = table->maxlen-1;
814 if(copy_from_user(table->data, buffer, len))
816 ((char *) table->data)[len] = 0;
817 filp->f_pos += *lenp;
819 len = strlen(table->data);
820 if (len > table->maxlen)
825 if(copy_to_user(buffer, table->data, len))
828 if(put_user('\n', ((char *) buffer) + len))
839 * Special case of dostring for the UTS structure. This has locks
840 * to observe. Should this be in kernel/sys.c ????
843 static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
844 void __user *buffer, size_t *lenp)
850 r=proc_dostring(table,0,filp,buffer,lenp);
853 down_write(&uts_sem);
854 r=proc_dostring(table,1,filp,buffer,lenp);
866 static int do_proc_dointvec(ctl_table *table, int write, struct file *filp,
867 void __user *buffer, size_t *lenp, int conv, int op)
869 int *i, vleft, first=1, neg, val;
873 char buf[TMPBUFLEN], *p;
875 if (!table->data || !table->maxlen || !*lenp ||
876 (filp->f_pos && !write)) {
881 i = (int *) table->data;
882 vleft = table->maxlen / sizeof(int);
885 for (; left && vleft--; i++, first=0) {
889 if (get_user(c,(char __user *) buffer))
900 if (len > TMPBUFLEN-1)
902 if(copy_from_user(buf, buffer, len))
906 if (*p == '-' && left > 1) {
910 if (*p < '0' || *p > '9')
912 val = simple_strtoul(p, &p, 0) * conv;
914 if ((len < left) && *p && !isspace(*p))
921 case OP_SET: *i = val; break;
922 case OP_AND: *i &= val; break;
923 case OP_OR: *i |= val; break;
924 case OP_MAX: if(*i < val)
927 case OP_MIN: if(*i > val)
935 sprintf(p, "%d", (*i) / conv);
939 if(copy_to_user(buffer, buf, len))
946 if (!write && !first && left) {
947 if(put_user('\n', (char *) buffer))
965 filp->f_pos += *lenp;
970 * proc_dointvec - read a vector of integers
971 * @table: the sysctl table
972 * @write: %TRUE if this is a write to the sysctl file
973 * @filp: the file structure
974 * @buffer: the user buffer
975 * @lenp: the size of the user buffer
977 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
978 * values from/to the user buffer, treated as an ASCII string.
980 * Returns 0 on success.
982 int proc_dointvec(ctl_table *table, int write, struct file *filp,
983 void __user *buffer, size_t *lenp)
985 return do_proc_dointvec(table,write,filp,buffer,lenp,1,OP_SET);
989 * init may raise the set.
992 int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
993 void __user *buffer, size_t *lenp)
995 if (!capable(CAP_SYS_MODULE)) {
998 return do_proc_dointvec(table,write,filp,buffer,lenp,1,
999 (current->pid == 1) ? OP_SET : OP_AND);
1003 * proc_dointvec_minmax - read a vector of integers with min/max values
1004 * @table: the sysctl table
1005 * @write: %TRUE if this is a write to the sysctl file
1006 * @filp: the file structure
1007 * @buffer: the user buffer
1008 * @lenp: the size of the user buffer
1010 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1011 * values from/to the user buffer, treated as an ASCII string.
1013 * This routine will ensure the values are within the range specified by
1014 * table->extra1 (min) and table->extra2 (max).
1016 * Returns 0 on success.
1018 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
1019 void __user *buffer, size_t *lenp)
1021 int *i, *min, *max, vleft, first=1, neg, val;
1023 #define TMPBUFLEN 20
1024 char buf[TMPBUFLEN], *p;
1026 if (!table->data || !table->maxlen || !*lenp ||
1027 (filp->f_pos && !write)) {
1032 i = (int *) table->data;
1033 min = (int *) table->extra1;
1034 max = (int *) table->extra2;
1035 vleft = table->maxlen / sizeof(int);
1038 for (; left && vleft--; i++, min++, max++, first=0) {
1042 if(get_user(c, (char *) buffer))
1053 if (len > TMPBUFLEN-1)
1055 if(copy_from_user(buf, buffer, len))
1059 if (*p == '-' && left > 1) {
1063 if (*p < '0' || *p > '9')
1065 val = simple_strtoul(p, &p, 0);
1067 if ((len < left) && *p && !isspace(*p))
1074 if ((min && val < *min) || (max && val > *max))
1081 sprintf(p, "%d", *i);
1085 if(copy_to_user(buffer, buf, len))
1092 if (!write && !first && left) {
1093 if(put_user('\n', (char *) buffer))
1098 p = (char *) buffer;
1101 if(get_user(c, p++))
1111 filp->f_pos += *lenp;
1115 static int do_proc_doulongvec_minmax(ctl_table *table, int write,
1117 void __user *buffer, size_t *lenp,
1118 unsigned long convmul,
1119 unsigned long convdiv)
1121 #define TMPBUFLEN 20
1122 unsigned long *i, *min, *max, val;
1123 int vleft, first=1, neg;
1125 char buf[TMPBUFLEN], *p;
1127 if (!table->data || !table->maxlen || !*lenp ||
1128 (filp->f_pos && !write)) {
1133 i = (unsigned long *) table->data;
1134 min = (unsigned long *) table->extra1;
1135 max = (unsigned long *) table->extra2;
1136 vleft = table->maxlen / sizeof(unsigned long);
1139 for (; left && vleft--; i++, min++, max++, first=0) {
1143 if (get_user(c, (char __user *) buffer))
1154 if (len > TMPBUFLEN-1)
1156 if (copy_from_user(buf, buffer, len))
1160 if (*p == '-' && left > 1) {
1164 if (*p < '0' || *p > '9')
1166 val = simple_strtoul(p, &p, 0) * convmul / convdiv ;
1168 if ((len < left) && *p && !isspace(*p))
1177 if ((min && val < *min) || (max && val > *max))
1184 sprintf(p, "%lu", convdiv * (*i) / convmul);
1188 if(copy_to_user(buffer, buf, len))
1195 if (!write && !first && left) {
1196 if(put_user('\n', (char *) buffer))
1201 p = (char *) buffer;
1204 if(get_user(c, p++))
1214 filp->f_pos += *lenp;
1220 * proc_doulongvec_minmax - read a vector of long integers with min/max values
1221 * @table: the sysctl table
1222 * @write: %TRUE if this is a write to the sysctl file
1223 * @filp: the file structure
1224 * @buffer: the user buffer
1225 * @lenp: the size of the user buffer
1227 * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
1228 * values from/to the user buffer, treated as an ASCII string.
1230 * This routine will ensure the values are within the range specified by
1231 * table->extra1 (min) and table->extra2 (max).
1233 * Returns 0 on success.
1235 int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
1236 void __user *buffer, size_t *lenp)
1238 return do_proc_doulongvec_minmax(table, write, filp, buffer, lenp, 1l, 1l);
1242 * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
1243 * @table: the sysctl table
1244 * @write: %TRUE if this is a write to the sysctl file
1245 * @filp: the file structure
1246 * @buffer: the user buffer
1247 * @lenp: the size of the user buffer
1249 * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
1250 * values from/to the user buffer, treated as an ASCII string. The values
1251 * are treated as milliseconds, and converted to jiffies when they are stored.
1253 * This routine will ensure the values are within the range specified by
1254 * table->extra1 (min) and table->extra2 (max).
1256 * Returns 0 on success.
1258 int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
1260 void __user *buffer, size_t *lenp)
1262 return do_proc_doulongvec_minmax(table, write, filp, buffer,
1268 * proc_dointvec_jiffies - read a vector of integers as seconds
1269 * @table: the sysctl table
1270 * @write: %TRUE if this is a write to the sysctl file
1271 * @filp: the file structure
1272 * @buffer: the user buffer
1273 * @lenp: the size of the user buffer
1275 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1276 * values from/to the user buffer, treated as an ASCII string.
1277 * The values read are assumed to be in seconds, and are converted into
1280 * Returns 0 on success.
1282 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
1283 void __user *buffer, size_t *lenp)
1285 return do_proc_dointvec(table,write,filp,buffer,lenp,HZ,OP_SET);
1288 #else /* CONFIG_PROC_FS */
1290 int proc_dostring(ctl_table *table, int write, struct file *filp,
1291 void *buffer, size_t *lenp)
1296 static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
1297 void *buffer, size_t *lenp)
1302 int proc_dointvec(ctl_table *table, int write, struct file *filp,
1303 void *buffer, size_t *lenp)
1308 int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
1309 void *buffer, size_t *lenp)
1314 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
1315 void *buffer, size_t *lenp)
1320 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
1321 void *buffer, size_t *lenp)
1326 int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
1327 void *buffer, size_t *lenp)
1332 int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
1334 void *buffer, size_t *lenp)
1340 #endif /* CONFIG_PROC_FS */
1344 * General sysctl support routines
1347 /* The generic string strategy routine: */
1348 int sysctl_string(ctl_table *table, int *name, int nlen,
1349 void __user *oldval, size_t __user *oldlenp,
1350 void __user *newval, size_t newlen, void **context)
1354 if (!table->data || !table->maxlen)
1357 if (oldval && oldlenp) {
1358 if(get_user(len, oldlenp))
1361 l = strlen(table->data);
1362 if (len > l) len = l;
1363 if (len >= table->maxlen)
1364 len = table->maxlen;
1365 if(copy_to_user(oldval, table->data, len))
1367 if(put_user(0, ((char *) oldval) + len))
1369 if(put_user(len, oldlenp))
1373 if (newval && newlen) {
1375 if (len > table->maxlen)
1376 len = table->maxlen;
1377 if(copy_from_user(table->data, newval, len))
1379 if (len == table->maxlen)
1381 ((char *) table->data)[len] = 0;
1387 * This function makes sure that all of the integers in the vector
1388 * are between the minimum and maximum values given in the arrays
1389 * table->extra1 and table->extra2, respectively.
1391 int sysctl_intvec(ctl_table *table, int *name, int nlen,
1392 void *oldval, size_t *oldlenp,
1393 void *newval, size_t newlen, void **context)
1395 int i, *vec, *min, *max;
1398 if (newval && newlen) {
1399 if (newlen % sizeof(int) != 0)
1402 if (!table->extra1 && !table->extra2)
1405 if (newlen > table->maxlen)
1406 newlen = table->maxlen;
1407 length = newlen / sizeof(int);
1409 vec = (int *) newval;
1410 min = (int *) table->extra1;
1411 max = (int *) table->extra2;
1413 for (i = 0; i < length; i++) {
1415 get_user(value, vec + i);
1416 if (min && value < min[i])
1418 if (max && value > max[i])
1425 /* Strategy function to convert jiffies to seconds */
1426 int sysctl_jiffies(ctl_table *table, int *name, int nlen,
1427 void *oldval, size_t *oldlenp,
1428 void *newval, size_t newlen, void **context)
1433 if (get_user(olen, oldlenp))
1435 if (olen!=sizeof(int))
1438 if (put_user(*(int *)(table->data) / HZ, (int *)oldval) ||
1439 (oldlenp && put_user(sizeof(int),oldlenp)))
1442 if (newval && newlen) {
1444 if (newlen != sizeof(int))
1446 if (get_user(new, (int *)newval))
1448 *(int *)(table->data) = new*HZ;
1454 #else /* CONFIG_SYSCTL */
1457 extern asmlinkage long sys_sysctl(struct __sysctl_args __user *args)
1462 int sysctl_string(ctl_table *table, int *name, int nlen,
1463 void *oldval, size_t *oldlenp,
1464 void *newval, size_t newlen, void **context)
1469 int sysctl_intvec(ctl_table *table, int *name, int nlen,
1470 void *oldval, size_t *oldlenp,
1471 void *newval, size_t newlen, void **context)
1476 int sysctl_jiffies(ctl_table *table, int *name, int nlen,
1477 void *oldval, size_t *oldlenp,
1478 void *newval, size_t newlen, void **context)
1483 int proc_dostring(ctl_table *table, int write, struct file *filp,
1484 void *buffer, size_t *lenp)
1489 int proc_dointvec(ctl_table *table, int write, struct file *filp,
1490 void *buffer, size_t *lenp)
1495 int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
1496 void *buffer, size_t *lenp)
1501 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
1502 void *buffer, size_t *lenp)
1507 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
1508 void *buffer, size_t *lenp)
1513 int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
1514 void *buffer, size_t *lenp)
1519 int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
1521 void *buffer, size_t *lenp)
1526 struct ctl_table_header * register_sysctl_table(ctl_table * table,
1532 void unregister_sysctl_table(struct ctl_table_header * table)
1536 #endif /* CONFIG_SYSCTL */