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>
22 #include <linux/slab.h>
23 #include <linux/sysctl.h>
24 #include <linux/swapctl.h>
25 #include <linux/proc_fs.h>
26 #include <linux/ctype.h>
27 #include <linux/utsname.h>
28 #include <linux/capability.h>
29 #include <linux/smp_lock.h>
30 #include <linux/init.h>
31 #include <linux/sysrq.h>
32 #include <linux/highuid.h>
33 #include <linux/swap.h>
35 #include <asm/uaccess.h>
37 #ifdef CONFIG_ROOT_NFS
38 #include <linux/nfs_fs.h>
41 #if defined(CONFIG_SYSCTL)
43 /* External variables not in a header file. */
44 extern int panic_timeout;
46 extern int bdf_prm[], bdflush_min[], bdflush_max[];
47 extern int sysctl_overcommit_memory;
48 extern int max_threads;
49 extern atomic_t nr_queued_signals;
50 extern int max_queued_signals;
51 extern int sysrq_enabled;
52 extern int core_uses_pid;
53 extern int core_setuid_ok;
54 extern char core_pattern[];
56 extern int laptop_mode;
57 extern int block_dump;
59 /* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
60 static int maxolduid = 65535;
64 extern char modprobe_path[];
67 extern char hotplug_path[];
69 #ifdef CONFIG_CHR_DEV_SG
70 extern int sg_big_buff;
73 extern size_t shm_ctlmax;
74 extern size_t shm_ctlall;
75 extern int shm_ctlmni;
76 extern int msg_ctlmax;
77 extern int msg_ctlmnb;
78 extern int msg_ctlmni;
79 extern int sem_ctls[];
82 extern int exception_trace;
85 extern char reboot_command [];
86 extern int stop_a_enabled;
87 extern int scons_pwroff;
90 #ifdef CONFIG_ARCH_S390
92 extern int sysctl_ieee_emulation_warnings;
94 extern int sysctl_userprocess_debug;
98 extern unsigned long zero_paged_on, powersave_nap;
99 int proc_dol2crvec(ctl_table *table, int write, struct file *filp,
100 void *buffer, size_t *lenp);
101 int proc_dol3crvec(ctl_table *table, int write, struct file *filp,
102 void *buffer, size_t *lenp);
105 #ifdef CONFIG_BSD_PROCESS_ACCT
106 extern int acct_parm[];
109 extern int pgt_cache_water[];
111 static int parse_table(int *, int, void *, size_t *, void *, size_t,
112 ctl_table *, void **);
113 static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
114 void *buffer, size_t *lenp);
116 static ctl_table root_table[];
117 static struct ctl_table_header root_table_header =
118 { root_table, LIST_HEAD_INIT(root_table_header.ctl_entry) };
120 static ctl_table kern_table[];
121 static ctl_table vm_table[];
123 extern ctl_table net_table[];
125 static ctl_table proc_table[];
126 static ctl_table fs_table[];
127 static ctl_table debug_table[];
128 static ctl_table dev_table[];
129 extern ctl_table random_table[];
131 /* /proc declarations: */
133 #ifdef CONFIG_PROC_FS
135 static ssize_t proc_readsys(struct file *, char *, size_t, loff_t *);
136 static ssize_t proc_writesys(struct file *, const char *, size_t, loff_t *);
137 static int proc_sys_permission(struct inode *, int);
139 struct file_operations proc_sys_file_operations = {
141 write: proc_writesys,
144 static struct inode_operations proc_sys_inode_operations = {
145 permission: proc_sys_permission,
148 extern struct proc_dir_entry *proc_sys_root;
150 static void register_proc_table(ctl_table *, struct proc_dir_entry *, void *);
151 static void unregister_proc_table(ctl_table *, struct proc_dir_entry *);
154 /* The default sysctl tables: */
156 static ctl_table root_table[] = {
157 {CTL_KERN, "kernel", NULL, 0, 0555, kern_table},
158 {CTL_VM, "vm", NULL, 0, 0555, vm_table},
160 {CTL_NET, "net", NULL, 0, 0555, net_table},
162 {CTL_PROC, "proc", NULL, 0, 0555, proc_table},
163 {CTL_FS, "fs", NULL, 0, 0555, fs_table},
164 {CTL_DEBUG, "debug", NULL, 0, 0555, debug_table},
165 {CTL_DEV, "dev", NULL, 0, 0555, dev_table},
169 static ctl_table kern_table[] = {
170 {KERN_OSTYPE, "ostype", system_utsname.sysname, 64,
171 0444, NULL, &proc_doutsstring, &sysctl_string},
172 {KERN_OSRELEASE, "osrelease", system_utsname.release, 64,
173 0444, NULL, &proc_doutsstring, &sysctl_string},
174 {KERN_VERSION, "version", system_utsname.version, 64,
175 0444, NULL, &proc_doutsstring, &sysctl_string},
176 {KERN_NODENAME, "hostname", system_utsname.nodename, 64,
177 0644, NULL, &proc_doutsstring, &sysctl_string},
178 {KERN_DOMAINNAME, "domainname", system_utsname.domainname, 64,
179 0644, NULL, &proc_doutsstring, &sysctl_string},
180 {KERN_PANIC, "panic", &panic_timeout, sizeof(int),
181 0644, NULL, &proc_dointvec},
182 {KERN_CORE_USES_PID, "core_uses_pid", &core_uses_pid, sizeof(int),
183 0644, NULL, &proc_dointvec},
184 {KERN_CORE_SETUID, "core_setuid_ok", &core_setuid_ok, sizeof(int),
185 0644, NULL, &proc_dointvec},
186 {KERN_CORE_PATTERN, "core_pattern", core_pattern, 64,
187 0644, NULL, &proc_dostring, &sysctl_string},
188 {KERN_TAINTED, "tainted", &tainted, sizeof(int),
189 0644, NULL, &proc_dointvec},
190 {KERN_CAP_BSET, "cap-bound", &cap_bset, sizeof(kernel_cap_t),
191 0600, NULL, &proc_dointvec_bset},
192 #ifdef CONFIG_BLK_DEV_INITRD
193 {KERN_REALROOTDEV, "real-root-dev", &real_root_dev, sizeof(int),
194 0644, NULL, &proc_dointvec},
197 {KERN_SPARC_REBOOT, "reboot-cmd", reboot_command,
198 256, 0644, NULL, &proc_dostring, &sysctl_string },
199 {KERN_SPARC_STOP_A, "stop-a", &stop_a_enabled, sizeof (int),
200 0644, NULL, &proc_dointvec},
201 {KERN_SPARC_SCONS_PWROFF, "scons-poweroff", &scons_pwroff, sizeof (int),
202 0644, NULL, &proc_dointvec},
205 {KERN_PPC_ZEROPAGED, "zero-paged", &zero_paged_on, sizeof(int),
206 0644, NULL, &proc_dointvec},
207 {KERN_PPC_POWERSAVE_NAP, "powersave-nap", &powersave_nap, sizeof(int),
208 0644, NULL, &proc_dointvec},
209 {KERN_PPC_L2CR, "l2cr", NULL, 0,
210 0644, NULL, &proc_dol2crvec},
211 {KERN_PPC_L3CR, "l3cr", NULL, 0,
212 0644, NULL, &proc_dol3crvec},
214 {KERN_CTLALTDEL, "ctrl-alt-del", &C_A_D, sizeof(int),
215 0644, NULL, &proc_dointvec},
216 {KERN_PRINTK, "printk", &console_loglevel, 4*sizeof(int),
217 0644, NULL, &proc_dointvec},
219 {KERN_MODPROBE, "modprobe", &modprobe_path, 256,
220 0644, NULL, &proc_dostring, &sysctl_string },
222 #ifdef CONFIG_HOTPLUG
223 {KERN_HOTPLUG, "hotplug", &hotplug_path, 256,
224 0644, NULL, &proc_dostring, &sysctl_string },
226 #ifdef CONFIG_CHR_DEV_SG
227 {KERN_SG_BIG_BUFF, "sg-big-buff", &sg_big_buff, sizeof (int),
228 0444, NULL, &proc_dointvec},
230 #ifdef CONFIG_BSD_PROCESS_ACCT
231 {KERN_ACCT, "acct", &acct_parm, 3*sizeof(int),
232 0644, NULL, &proc_dointvec},
234 {KERN_RTSIGNR, "rtsig-nr", &nr_queued_signals, sizeof(int),
235 0444, NULL, &proc_dointvec},
236 {KERN_RTSIGMAX, "rtsig-max", &max_queued_signals, sizeof(int),
237 0644, NULL, &proc_dointvec},
238 #ifdef CONFIG_SYSVIPC
239 {KERN_SHMMAX, "shmmax", &shm_ctlmax, sizeof (size_t),
240 0644, NULL, &proc_doulongvec_minmax},
241 {KERN_SHMALL, "shmall", &shm_ctlall, sizeof (size_t),
242 0644, NULL, &proc_doulongvec_minmax},
243 {KERN_SHMMNI, "shmmni", &shm_ctlmni, sizeof (int),
244 0644, NULL, &proc_dointvec},
245 {KERN_MSGMAX, "msgmax", &msg_ctlmax, sizeof (int),
246 0644, NULL, &proc_dointvec},
247 {KERN_MSGMNI, "msgmni", &msg_ctlmni, sizeof (int),
248 0644, NULL, &proc_dointvec},
249 {KERN_MSGMNB, "msgmnb", &msg_ctlmnb, sizeof (int),
250 0644, NULL, &proc_dointvec},
251 {KERN_SEM, "sem", &sem_ctls, 4*sizeof (int),
252 0644, NULL, &proc_dointvec},
254 #ifdef CONFIG_MAGIC_SYSRQ
255 {KERN_SYSRQ, "sysrq", &sysrq_enabled, sizeof (int),
256 0644, NULL, &proc_dointvec},
258 {KERN_CADPID, "cad_pid", &cad_pid, sizeof (int),
259 0600, NULL, &proc_dointvec},
260 {KERN_MAX_THREADS, "threads-max", &max_threads, sizeof(int),
261 0644, NULL, &proc_dointvec},
262 {KERN_RANDOM, "random", NULL, 0, 0555, random_table},
263 {KERN_OVERFLOWUID, "overflowuid", &overflowuid, sizeof(int), 0644, NULL,
264 &proc_dointvec_minmax, &sysctl_intvec, NULL,
265 &minolduid, &maxolduid},
266 {KERN_OVERFLOWGID, "overflowgid", &overflowgid, sizeof(int), 0644, NULL,
267 &proc_dointvec_minmax, &sysctl_intvec, NULL,
268 &minolduid, &maxolduid},
269 #ifdef CONFIG_ARCH_S390
270 #ifdef CONFIG_MATHEMU
271 {KERN_IEEE_EMULATION_WARNINGS,"ieee_emulation_warnings",
272 &sysctl_ieee_emulation_warnings,sizeof(int),0644,NULL,&proc_dointvec},
274 {KERN_S390_USER_DEBUG_LOGGING,"userprocess_debug",
275 &sysctl_userprocess_debug,sizeof(int),0644,NULL,&proc_dointvec},
278 {KERN_EXCEPTION_TRACE,"exception-trace",
279 &exception_trace,sizeof(int),0644,NULL,&proc_dointvec},
284 static ctl_table vm_table[] = {
285 {VM_GFP_DEBUG, "vm_gfp_debug",
286 &vm_gfp_debug, sizeof(int), 0644, NULL, &proc_dointvec},
287 {VM_VFS_SCAN_RATIO, "vm_vfs_scan_ratio",
288 &vm_vfs_scan_ratio, sizeof(int), 0644, NULL, &proc_dointvec},
289 {VM_CACHE_SCAN_RATIO, "vm_cache_scan_ratio",
290 &vm_cache_scan_ratio, sizeof(int), 0644, NULL, &proc_dointvec},
291 {VM_MAPPED_RATIO, "vm_mapped_ratio",
292 &vm_mapped_ratio, sizeof(int), 0644, NULL, &proc_dointvec},
293 {VM_ANON_LRU, "vm_anon_lru",
294 &vm_anon_lru, sizeof(int), 0644, NULL, &proc_dointvec},
295 {VM_LRU_BALANCE_RATIO, "vm_lru_balance_ratio",
296 &vm_lru_balance_ratio, sizeof(int), 0644, NULL, &proc_dointvec},
297 {VM_PASSES, "vm_passes",
298 &vm_passes, sizeof(int), 0644, NULL, &proc_dointvec},
299 {VM_BDFLUSH, "bdflush", &bdf_prm, 9*sizeof(int), 0644, NULL,
300 &proc_dointvec_minmax, &sysctl_intvec, NULL,
301 &bdflush_min, &bdflush_max},
302 {VM_OVERCOMMIT_MEMORY, "overcommit_memory", &sysctl_overcommit_memory,
303 sizeof(sysctl_overcommit_memory), 0644, NULL, &proc_dointvec},
304 {VM_PAGERDAEMON, "kswapd",
305 &pager_daemon, sizeof(pager_daemon_t), 0644, NULL, &proc_dointvec},
306 {VM_PGT_CACHE, "pagetable_cache",
307 &pgt_cache_water, 2*sizeof(int), 0644, NULL, &proc_dointvec},
308 {VM_PAGE_CLUSTER, "page-cluster",
309 &page_cluster, sizeof(int), 0644, NULL, &proc_dointvec},
310 {VM_MIN_READAHEAD, "min-readahead",
311 &vm_min_readahead,sizeof(int), 0644, NULL, &proc_dointvec},
312 {VM_MAX_READAHEAD, "max-readahead",
313 &vm_max_readahead,sizeof(int), 0644, NULL, &proc_dointvec},
314 {VM_MAX_MAP_COUNT, "max_map_count",
315 &max_map_count, sizeof(int), 0644, NULL, &proc_dointvec},
316 {VM_LAPTOP_MODE, "laptop_mode",
317 &laptop_mode, sizeof(int), 0644, NULL, &proc_dointvec},
318 {VM_BLOCK_DUMP, "block_dump",
319 &block_dump, sizeof(int), 0644, NULL, &proc_dointvec},
323 static ctl_table proc_table[] = {
327 static ctl_table fs_table[] = {
328 {FS_NRINODE, "inode-nr", &inodes_stat, 2*sizeof(int),
329 0444, NULL, &proc_dointvec},
330 {FS_STATINODE, "inode-state", &inodes_stat, 7*sizeof(int),
331 0444, NULL, &proc_dointvec},
332 {FS_NRFILE, "file-nr", &files_stat, 3*sizeof(int),
333 0444, NULL, &proc_dointvec},
334 {FS_MAXFILE, "file-max", &files_stat.max_files, sizeof(int),
335 0644, NULL, &proc_dointvec},
336 {FS_DENTRY, "dentry-state", &dentry_stat, 6*sizeof(int),
337 0444, NULL, &proc_dointvec},
338 {FS_OVERFLOWUID, "overflowuid", &fs_overflowuid, sizeof(int), 0644, NULL,
339 &proc_dointvec_minmax, &sysctl_intvec, NULL,
340 &minolduid, &maxolduid},
341 {FS_OVERFLOWGID, "overflowgid", &fs_overflowgid, sizeof(int), 0644, NULL,
342 &proc_dointvec_minmax, &sysctl_intvec, NULL,
343 &minolduid, &maxolduid},
344 {FS_LEASES, "leases-enable", &leases_enable, sizeof(int),
345 0644, NULL, &proc_dointvec},
346 {FS_DIR_NOTIFY, "dir-notify-enable", &dir_notify_enable,
347 sizeof(int), 0644, NULL, &proc_dointvec},
348 {FS_LEASE_TIME, "lease-break-time", &lease_break_time, sizeof(int),
349 0644, NULL, &proc_dointvec},
353 static ctl_table debug_table[] = {
357 static ctl_table dev_table[] = {
361 extern void init_irq_proc (void);
363 static spinlock_t sysctl_lock = SPIN_LOCK_UNLOCKED;
365 /* called under sysctl_lock */
366 static int use_table(struct ctl_table_header *p)
368 if (unlikely(p->unregistering != NULL))
374 /* called under sysctl_lock */
375 static void unuse_table(struct ctl_table_header *p)
378 if (unlikely(p->unregistering != NULL))
379 complete(p->unregistering);
382 /* called under sysctl_lock, will reacquire if has to wait */
383 static void start_unregistering(struct ctl_table_header *p)
386 * if p->used is 0, nobody will ever touch that entry again;
387 * we'll eliminate all paths to it before dropping sysctl_lock
389 if (unlikely(p->used)) {
390 struct completion wait;
391 init_completion(&wait);
392 p->unregistering = &wait;
393 spin_unlock(&sysctl_lock);
394 wait_for_completion(&wait);
395 spin_lock(&sysctl_lock);
398 * do not remove from the list until nobody holds it; walking the
399 * list in do_sysctl() relies on that.
401 list_del_init(&p->ctl_entry);
404 void __init sysctl_init(void)
406 #ifdef CONFIG_PROC_FS
407 register_proc_table(root_table, proc_sys_root, &root_table_header);
412 int do_sysctl(int *name, int nlen, void *oldval, size_t *oldlenp,
413 void *newval, size_t newlen)
415 struct list_head *tmp;
416 int error = -ENOTDIR;
418 if (nlen <= 0 || nlen >= CTL_MAXNAME)
422 if (!oldlenp || get_user(old_len, oldlenp))
424 /* XXX: insufficient for SMP, but should be redundant anyway */
425 if ((ssize_t)old_len < 0)
428 spin_lock(&sysctl_lock);
429 tmp = &root_table_header.ctl_entry;
431 struct ctl_table_header *head =
432 list_entry(tmp, struct ctl_table_header, ctl_entry);
433 void *context = NULL;
435 if (!use_table(head))
438 spin_unlock(&sysctl_lock);
440 error = parse_table(name, nlen, oldval, oldlenp,
441 newval, newlen, head->ctl_table,
446 spin_lock(&sysctl_lock);
448 if (error != -ENOTDIR)
450 } while ((tmp = tmp->next) != &root_table_header.ctl_entry);
451 spin_unlock(&sysctl_lock);
455 extern asmlinkage long sys_sysctl(struct __sysctl_args *args)
457 struct __sysctl_args tmp;
460 if (copy_from_user(&tmp, args, sizeof(tmp)))
464 error = do_sysctl(tmp.name, tmp.nlen, tmp.oldval, tmp.oldlenp,
465 tmp.newval, tmp.newlen);
471 * ctl_perm does NOT grant the superuser all rights automatically, because
472 * some sysctl variables are readonly even to root.
475 static int test_perm(int mode, int op)
479 else if (in_egroup_p(0))
481 if ((mode & op & 0007) == op)
486 static inline int ctl_perm(ctl_table *table, int op)
488 return test_perm(table->mode, op);
491 static int parse_table(int *name, int nlen,
492 void *oldval, size_t *oldlenp,
493 void *newval, size_t newlen,
494 ctl_table *table, void **context)
500 if (get_user(n, name))
502 for ( ; table->ctl_name; table++) {
503 if (n == table->ctl_name || table->ctl_name == CTL_ANY) {
506 if (ctl_perm(table, 001))
508 if (table->strategy) {
509 error = table->strategy(
512 newval, newlen, context);
518 table = table->child;
521 error = do_sysctl_strategy(table, name, nlen,
523 newval, newlen, context);
530 /* Perform the actual read/write of a sysctl table entry. */
531 int do_sysctl_strategy (ctl_table *table,
533 void *oldval, size_t *oldlenp,
534 void *newval, size_t newlen, void **context)
543 if (ctl_perm(table, op))
546 if (table->strategy) {
547 rc = table->strategy(table, name, nlen, oldval, oldlenp,
548 newval, newlen, context);
555 /* If there is no strategy routine, or if the strategy returns
556 * zero, proceed with automatic r/w */
557 if (table->data && table->maxlen) {
558 if (oldval && oldlenp) {
559 if (get_user(len, oldlenp))
562 if (len > table->maxlen)
564 if(copy_to_user(oldval, table->data, len))
566 if(put_user(len, oldlenp))
570 if (newval && newlen) {
572 if (len > table->maxlen)
574 if(copy_from_user(table->data, newval, len))
582 * register_sysctl_table - register a sysctl hierarchy
583 * @table: the top-level table structure
584 * @insert_at_head: whether the entry should be inserted in front or at the end
586 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
587 * array. An entry with a ctl_name of 0 terminates the table.
589 * The members of the &ctl_table structure are used as follows:
591 * ctl_name - This is the numeric sysctl value used by sysctl(2). The number
592 * must be unique within that level of sysctl
594 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
595 * enter a sysctl file
597 * data - a pointer to data for use by proc_handler
599 * maxlen - the maximum size in bytes of the data
601 * mode - the file permissions for the /proc/sys file, and for sysctl(2)
603 * child - a pointer to the child sysctl table if this entry is a directory, or
606 * proc_handler - the text handler routine (described below)
608 * strategy - the strategy routine (described below)
610 * de - for internal use by the sysctl routines
612 * extra1, extra2 - extra pointers usable by the proc handler routines
614 * Leaf nodes in the sysctl tree will be represented by a single file
615 * under /proc; non-leaf nodes will be represented by directories.
617 * sysctl(2) can automatically manage read and write requests through
618 * the sysctl table. The data and maxlen fields of the ctl_table
619 * struct enable minimal validation of the values being written to be
620 * performed, and the mode field allows minimal authentication.
622 * More sophisticated management can be enabled by the provision of a
623 * strategy routine with the table entry. This will be called before
624 * any automatic read or write of the data is performed.
626 * The strategy routine may return
628 * < 0 - Error occurred (error is passed to user process)
630 * 0 - OK - proceed with automatic read or write.
632 * > 0 - OK - read or write has been done by the strategy routine, so
633 * return immediately.
635 * There must be a proc_handler routine for any terminal nodes
636 * mirrored under /proc/sys (non-terminals are handled by a built-in
637 * directory handler). Several default handlers are available to
638 * cover common cases -
640 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
641 * proc_dointvec_minmax(), proc_doulongvec_ms_jiffies_minmax(),
642 * proc_doulongvec_minmax()
644 * It is the handler's job to read the input buffer from user memory
645 * and process it. The handler should return 0 on success.
647 * This routine returns %NULL on a failure to register, and a pointer
648 * to the table header on success.
650 struct ctl_table_header *register_sysctl_table(ctl_table * table,
653 struct ctl_table_header *tmp;
654 tmp = kmalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
657 tmp->ctl_table = table;
658 INIT_LIST_HEAD(&tmp->ctl_entry);
660 tmp->unregistering = NULL;
661 spin_lock(&sysctl_lock);
663 list_add(&tmp->ctl_entry, &root_table_header.ctl_entry);
665 list_add_tail(&tmp->ctl_entry, &root_table_header.ctl_entry);
666 spin_unlock(&sysctl_lock);
667 #ifdef CONFIG_PROC_FS
668 register_proc_table(table, proc_sys_root, tmp);
674 * unregister_sysctl_table - unregister a sysctl table hierarchy
675 * @header: the header returned from register_sysctl_table
677 * Unregisters the sysctl table and all children. proc entries may not
678 * actually be removed until they are no longer used by anyone.
680 void unregister_sysctl_table(struct ctl_table_header * header)
682 spin_lock(&sysctl_lock);
683 start_unregistering(header);
684 #ifdef CONFIG_PROC_FS
685 unregister_proc_table(header->ctl_table, proc_sys_root);
687 spin_unlock(&sysctl_lock);
695 #ifdef CONFIG_PROC_FS
697 /* Scan the sysctl entries in table and add them all into /proc */
698 static void register_proc_table(ctl_table * table, struct proc_dir_entry *root, void *set)
700 struct proc_dir_entry *de;
704 for (; table->ctl_name; table++) {
705 /* Can't do anything without a proc name. */
706 if (!table->procname)
708 /* Maybe we can't do anything with it... */
709 if (!table->proc_handler && !table->child) {
710 printk(KERN_WARNING "SYSCTL: Can't register %s\n",
715 len = strlen(table->procname);
719 if (table->proc_handler)
723 for (de = root->subdir; de; de = de->next) {
724 if (proc_match(len, table->procname, de))
727 /* If the subdir exists already, de is non-NULL */
731 de = create_proc_entry(table->procname, mode, root);
735 de->data = (void *) table;
736 if (table->proc_handler) {
737 de->proc_fops = &proc_sys_file_operations;
738 de->proc_iops = &proc_sys_inode_operations;
742 if (de->mode & S_IFDIR)
743 register_proc_table(table->child, de, set);
748 * Unregister a /proc sysctl table and any subdirectories.
750 static void unregister_proc_table(ctl_table * table, struct proc_dir_entry *root)
752 struct proc_dir_entry *de;
753 for (; table->ctl_name; table++) {
754 if (!(de = table->de))
756 if (de->mode & S_IFDIR) {
758 printk (KERN_ALERT "Help - malformed sysctl tree on free\n");
761 unregister_proc_table(table->child, de);
763 /* Don't unregister directories which still have entries.. */
769 * In any case, mark the entry as goner; we'll keep it
770 * around if it's busy, but we'll know to do nothing with
771 * its fields. We are under sysctl_lock here.
775 /* Don't unregister proc entries that are still being used.. */
776 if (atomic_read(&de->count))
780 remove_proc_entry(table->procname, root);
784 static ssize_t do_rw_proc(int write, struct file * file, char * buf,
785 size_t count, loff_t *ppos)
788 struct proc_dir_entry *de =
789 (struct proc_dir_entry*) file->f_dentry->d_inode->u.generic_ip;
790 struct ctl_table *table;
792 ssize_t error = -ENOTDIR;
794 spin_lock(&sysctl_lock);
795 if (de && de->data && use_table(de->set)) {
797 * at that point we know that sysctl was not unregistered
798 * and won't be until we finish
800 spin_unlock(&sysctl_lock);
801 table = (struct ctl_table *) de->data;
802 if (!table || !table->proc_handler)
805 op = (write ? 002 : 004);
806 if (ctl_perm(table, op))
809 /* careful: calling conventions are nasty here */
813 * FIXME: we need to pass on ppos to the handler.
816 error = (*table->proc_handler)(table, write, file,
821 spin_lock(&sysctl_lock);
822 unuse_table(de->set);
824 spin_unlock(&sysctl_lock);
828 static ssize_t proc_readsys(struct file * file, char * buf,
829 size_t count, loff_t *ppos)
831 return do_rw_proc(0, file, buf, count, ppos);
834 static ssize_t proc_writesys(struct file * file, const char * buf,
835 size_t count, loff_t *ppos)
837 return do_rw_proc(1, file, (char *) buf, count, ppos);
840 static int proc_sys_permission(struct inode *inode, int op)
842 return test_perm(inode->i_mode, op);
846 * proc_dostring - read a string sysctl
847 * @table: the sysctl table
848 * @write: %TRUE if this is a write to the sysctl file
849 * @filp: the file structure
850 * @buffer: the user buffer
851 * @lenp: the size of the user buffer
853 * Reads/writes a string from/to the user buffer. If the kernel
854 * buffer provided is not large enough to hold the string, the
855 * string is truncated. The copied string is %NULL-terminated.
856 * If the string is being read by the user process, it is copied
857 * and a newline '\n' is added. It is truncated if the buffer is
860 * Returns 0 on success.
862 int proc_dostring(ctl_table *table, int write, struct file *filp,
863 void *buffer, size_t *lenp)
868 if (!table->data || !table->maxlen || !*lenp ||
869 (filp->f_pos && !write)) {
877 while (len < *lenp) {
878 if (get_user(c, p++))
880 if (c == 0 || c == '\n')
884 if (len >= table->maxlen)
885 len = table->maxlen-1;
886 if(copy_from_user(table->data, buffer, len))
888 ((char *) table->data)[len] = 0;
889 filp->f_pos += *lenp;
891 len = strlen(table->data);
892 if (len > table->maxlen)
897 if(copy_to_user(buffer, table->data, len))
900 if(put_user('\n', ((char *) buffer) + len))
911 * Special case of dostring for the UTS structure. This has locks
912 * to observe. Should this be in kernel/sys.c ????
915 static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
916 void *buffer, size_t *lenp)
922 r=proc_dostring(table,0,filp,buffer,lenp);
925 down_write(&uts_sem);
926 r=proc_dostring(table,1,filp,buffer,lenp);
938 static int do_proc_dointvec(ctl_table *table, int write, struct file *filp,
939 void *buffer, size_t *lenp, int conv, int op)
941 int *i, vleft, first=1, neg, val;
945 char buf[TMPBUFLEN], *p;
947 if (!table->data || !table->maxlen || !*lenp ||
948 (filp->f_pos && !write)) {
953 i = (int *) table->data;
954 vleft = table->maxlen / sizeof(int);
957 for (; left && vleft--; i++, first=0) {
961 if (get_user(c, (char *) buffer))
972 if (len > TMPBUFLEN-1)
974 if(copy_from_user(buf, buffer, len))
978 if (*p == '-' && left > 1) {
982 if (*p < '0' || *p > '9')
984 val = simple_strtoul(p, &p, 0) * conv;
986 if ((len < left) && *p && !isspace(*p))
993 case OP_SET: *i = val; break;
994 case OP_AND: *i &= val; break;
995 case OP_OR: *i |= val; break;
996 case OP_MAX: if(*i < val)
999 case OP_MIN: if(*i > val)
1007 sprintf(p, "%d", (*i) / conv);
1011 if(copy_to_user(buffer, buf, len))
1018 if (!write && !first && left) {
1019 if(put_user('\n', (char *) buffer))
1024 p = (char *) buffer;
1027 if (get_user(c, p++))
1037 filp->f_pos += *lenp;
1042 * proc_dointvec - read a vector of integers
1043 * @table: the sysctl table
1044 * @write: %TRUE if this is a write to the sysctl file
1045 * @filp: the file structure
1046 * @buffer: the user buffer
1047 * @lenp: the size of the user buffer
1049 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1050 * values from/to the user buffer, treated as an ASCII string.
1052 * Returns 0 on success.
1054 int proc_dointvec(ctl_table *table, int write, struct file *filp,
1055 void *buffer, size_t *lenp)
1057 return do_proc_dointvec(table,write,filp,buffer,lenp,1,OP_SET);
1061 * init may raise the set.
1064 int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
1065 void *buffer, size_t *lenp)
1067 if (!capable(CAP_SYS_MODULE)) {
1070 return do_proc_dointvec(table,write,filp,buffer,lenp,1,
1071 (current->pid == 1) ? OP_SET : OP_AND);
1075 * proc_dointvec_minmax - read a vector of integers with min/max values
1076 * @table: the sysctl table
1077 * @write: %TRUE if this is a write to the sysctl file
1078 * @filp: the file structure
1079 * @buffer: the user buffer
1080 * @lenp: the size of the user buffer
1082 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1083 * values from/to the user buffer, treated as an ASCII string.
1085 * This routine will ensure the values are within the range specified by
1086 * table->extra1 (min) and table->extra2 (max).
1088 * Returns 0 on success.
1090 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
1091 void *buffer, size_t *lenp)
1093 int *i, *min, *max, vleft, first=1, neg, val;
1095 #define TMPBUFLEN 20
1096 char buf[TMPBUFLEN], *p;
1098 if (!table->data || !table->maxlen || !*lenp ||
1099 (filp->f_pos && !write)) {
1104 i = (int *) table->data;
1105 min = (int *) table->extra1;
1106 max = (int *) table->extra2;
1107 vleft = table->maxlen / sizeof(int);
1110 for (; left && vleft--; i++, min++, max++, first=0) {
1114 if (get_user(c, (char *) buffer))
1125 if (len > TMPBUFLEN-1)
1127 if(copy_from_user(buf, buffer, len))
1131 if (*p == '-' && left > 1) {
1135 if (*p < '0' || *p > '9')
1137 val = simple_strtoul(p, &p, 0);
1139 if ((len < left) && *p && !isspace(*p))
1146 if ((min && val < *min) || (max && val > *max))
1153 sprintf(p, "%d", *i);
1157 if(copy_to_user(buffer, buf, len))
1164 if (!write && !first && left) {
1165 if(put_user('\n', (char *) buffer))
1170 p = (char *) buffer;
1173 if (get_user(c, p++))
1183 filp->f_pos += *lenp;
1187 static int do_proc_doulongvec_minmax(ctl_table *table, int write,
1189 void *buffer, size_t *lenp,
1190 unsigned long convmul,
1191 unsigned long convdiv)
1193 #define TMPBUFLEN 20
1194 unsigned long *i, *min, *max, val;
1195 int vleft, first=1, neg;
1197 char buf[TMPBUFLEN], *p;
1199 if (!table->data || !table->maxlen || !*lenp ||
1200 (filp->f_pos && !write)) {
1205 i = (unsigned long *) table->data;
1206 min = (unsigned long *) table->extra1;
1207 max = (unsigned long *) table->extra2;
1208 vleft = table->maxlen / sizeof(unsigned long);
1211 for (; left && vleft--; i++, first=0) {
1215 if (get_user(c, (char *) buffer))
1226 if (len > TMPBUFLEN-1)
1228 if(copy_from_user(buf, buffer, len))
1232 if (*p == '-' && left > 1) {
1236 if (*p < '0' || *p > '9')
1238 val = simple_strtoul(p, &p, 0) * convmul / convdiv ;
1240 if ((len < left) && *p && !isspace(*p))
1249 if (min && val < *min++)
1251 if (max && val > *max++)
1258 sprintf(p, "%lu", convdiv * (*i) / convmul);
1262 if(copy_to_user(buffer, buf, len))
1269 if (!write && !first && left) {
1270 if(put_user('\n', (char *) buffer))
1275 p = (char *) buffer;
1278 if (get_user(c, p++))
1288 filp->f_pos += *lenp;
1294 * proc_doulongvec_minmax - read a vector of long integers with min/max values
1295 * @table: the sysctl table
1296 * @write: %TRUE if this is a write to the sysctl file
1297 * @filp: the file structure
1298 * @buffer: the user buffer
1299 * @lenp: the size of the user buffer
1301 * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
1302 * values from/to the user buffer, treated as an ASCII string.
1304 * This routine will ensure the values are within the range specified by
1305 * table->extra1 (min) and table->extra2 (max).
1307 * Returns 0 on success.
1309 int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
1310 void *buffer, size_t *lenp)
1312 return do_proc_doulongvec_minmax(table, write, filp, buffer, lenp, 1l, 1l);
1316 * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
1317 * @table: the sysctl table
1318 * @write: %TRUE if this is a write to the sysctl file
1319 * @filp: the file structure
1320 * @buffer: the user buffer
1321 * @lenp: the size of the user buffer
1323 * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
1324 * values from/to the user buffer, treated as an ASCII string. The values
1325 * are treated as milliseconds, and converted to jiffies when they are stored.
1327 * This routine will ensure the values are within the range specified by
1328 * table->extra1 (min) and table->extra2 (max).
1330 * Returns 0 on success.
1332 int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
1334 void *buffer, size_t *lenp)
1336 return do_proc_doulongvec_minmax(table, write, filp, buffer,
1342 * proc_dointvec_jiffies - read a vector of integers as seconds
1343 * @table: the sysctl table
1344 * @write: %TRUE if this is a write to the sysctl file
1345 * @filp: the file structure
1346 * @buffer: the user buffer
1347 * @lenp: the size of the user buffer
1349 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1350 * values from/to the user buffer, treated as an ASCII string.
1351 * The values read are assumed to be in seconds, and are converted into
1354 * Returns 0 on success.
1356 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
1357 void *buffer, size_t *lenp)
1359 return do_proc_dointvec(table,write,filp,buffer,lenp,HZ,OP_SET);
1362 #else /* CONFIG_PROC_FS */
1364 int proc_dostring(ctl_table *table, int write, struct file *filp,
1365 void *buffer, size_t *lenp)
1370 static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
1371 void *buffer, size_t *lenp)
1376 int proc_dointvec(ctl_table *table, int write, struct file *filp,
1377 void *buffer, size_t *lenp)
1382 int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
1383 void *buffer, size_t *lenp)
1388 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
1389 void *buffer, size_t *lenp)
1394 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
1395 void *buffer, size_t *lenp)
1400 int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
1401 void *buffer, size_t *lenp)
1406 int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
1408 void *buffer, size_t *lenp)
1414 #endif /* CONFIG_PROC_FS */
1418 * General sysctl support routines
1421 /* The generic string strategy routine: */
1422 int sysctl_string(ctl_table *table, int *name, int nlen,
1423 void *oldval, size_t *oldlenp,
1424 void *newval, size_t newlen, void **context)
1428 if (!table->data || !table->maxlen)
1431 if (oldval && oldlenp) {
1432 if (get_user(len, oldlenp))
1435 l = strlen(table->data);
1436 if (len > l) len = l;
1437 if (len >= table->maxlen)
1438 len = table->maxlen;
1439 if(copy_to_user(oldval, table->data, len))
1441 if(put_user(0, ((char *) oldval) + len))
1443 if(put_user(len, oldlenp))
1447 if (newval && newlen) {
1449 if (len > table->maxlen)
1450 len = table->maxlen;
1451 if(copy_from_user(table->data, newval, len))
1453 if (len == table->maxlen)
1455 ((char *) table->data)[len] = 0;
1461 * This function makes sure that all of the integers in the vector
1462 * are between the minimum and maximum values given in the arrays
1463 * table->extra1 and table->extra2, respectively.
1465 int sysctl_intvec(ctl_table *table, int *name, int nlen,
1466 void *oldval, size_t *oldlenp,
1467 void *newval, size_t newlen, void **context)
1469 int i, *vec, *min, *max;
1472 if (newval && newlen) {
1473 if (newlen % sizeof(int) != 0)
1476 if (!table->extra1 && !table->extra2)
1479 if (newlen > table->maxlen)
1480 newlen = table->maxlen;
1481 length = newlen / sizeof(int);
1483 vec = (int *) newval;
1484 min = (int *) table->extra1;
1485 max = (int *) table->extra2;
1487 for (i = 0; i < length; i++) {
1489 if (get_user(value, vec + i))
1491 if (min && value < min[i])
1493 if (max && value > max[i])
1500 /* Strategy function to convert jiffies to seconds */
1501 int sysctl_jiffies(ctl_table *table, int *name, int nlen,
1502 void *oldval, size_t *oldlenp,
1503 void *newval, size_t newlen, void **context)
1508 if (get_user(olen, oldlenp))
1510 if (olen!=sizeof(int))
1513 if (put_user(*(int *)(table->data) / HZ, (int *)oldval) ||
1514 (oldlenp && put_user(sizeof(int),oldlenp)))
1517 if (newval && newlen) {
1519 if (newlen != sizeof(int))
1521 if (get_user(new, (int *)newval))
1523 *(int *)(table->data) = new*HZ;
1529 #else /* CONFIG_SYSCTL */
1532 extern asmlinkage long sys_sysctl(struct __sysctl_args *args)
1537 int sysctl_string(ctl_table *table, int *name, int nlen,
1538 void *oldval, size_t *oldlenp,
1539 void *newval, size_t newlen, void **context)
1544 int sysctl_intvec(ctl_table *table, int *name, int nlen,
1545 void *oldval, size_t *oldlenp,
1546 void *newval, size_t newlen, void **context)
1551 int sysctl_jiffies(ctl_table *table, int *name, int nlen,
1552 void *oldval, size_t *oldlenp,
1553 void *newval, size_t newlen, void **context)
1558 int proc_dostring(ctl_table *table, int write, struct file *filp,
1559 void *buffer, size_t *lenp)
1564 int proc_dointvec(ctl_table *table, int write, struct file *filp,
1565 void *buffer, size_t *lenp)
1570 int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
1571 void *buffer, size_t *lenp)
1576 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
1577 void *buffer, size_t *lenp)
1582 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
1583 void *buffer, size_t *lenp)
1588 int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
1589 void *buffer, size_t *lenp)
1594 int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
1596 void *buffer, size_t *lenp)
1601 struct ctl_table_header * register_sysctl_table(ctl_table * table,
1607 void unregister_sysctl_table(struct ctl_table_header * table)
1611 #endif /* CONFIG_SYSCTL */