Import 2.3.12pre9
[davej-history.git] / kernel / sysctl.c
blob3b636f89850260ee666804b62e0a95768e3765f8
1 /*
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 Horn.
14 #include <linux/config.h>
15 #include <linux/malloc.h>
16 #include <linux/sysctl.h>
17 #include <linux/swapctl.h>
18 #include <linux/proc_fs.h>
19 #include <linux/ctype.h>
20 #include <linux/utsname.h>
21 #include <linux/smp_lock.h>
22 #include <linux/init.h>
24 #include <asm/uaccess.h>
26 #ifdef CONFIG_ROOT_NFS
27 #include <linux/nfs_fs.h>
28 #endif
30 #if defined(CONFIG_SYSCTL)
32 /* External variables not in a header file. */
33 extern int panic_timeout;
34 extern int console_loglevel, C_A_D;
35 extern int bdf_prm[], bdflush_min[], bdflush_max[];
36 extern int sysctl_overcommit_memory;
37 extern int max_threads;
38 extern int nr_queued_signals, max_queued_signals;
40 #ifdef CONFIG_KMOD
41 extern char modprobe_path[];
42 #endif
43 #ifdef CONFIG_CHR_DEV_SG
44 extern int sg_big_buff;
45 #endif
46 #ifdef CONFIG_SYSVIPC
47 extern int shmmax;
48 #endif
50 #ifdef __sparc__
51 extern char reboot_command [];
52 #endif
53 #ifdef __powerpc__
54 extern unsigned long htab_reclaim_on, zero_paged_on, powersave_nap;
55 int proc_dol2crvec(ctl_table *table, int write, struct file *filp,
56 void *buffer, size_t *lenp);
57 #endif
59 #ifdef CONFIG_BSD_PROCESS_ACCT
60 extern int acct_parm[];
61 #endif
63 extern int pgt_cache_water[];
65 static int parse_table(int *, int, void *, size_t *, void *, size_t,
66 ctl_table *, void **);
67 static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
68 void *buffer, size_t *lenp);
71 static ctl_table root_table[];
72 static struct ctl_table_header root_table_header =
73 {root_table, DNODE_SINGLE(&root_table_header)};
75 static ctl_table kern_table[];
76 static ctl_table vm_table[];
77 #ifdef CONFIG_NET
78 extern ctl_table net_table[];
79 #endif
80 static ctl_table proc_table[];
81 static ctl_table fs_table[];
82 static ctl_table debug_table[];
83 static ctl_table dev_table[];
86 /* /proc declarations: */
88 #ifdef CONFIG_PROC_FS
90 static ssize_t proc_readsys(struct file *, char *, size_t, loff_t *);
91 static ssize_t proc_writesys(struct file *, const char *, size_t, loff_t *);
92 static int proc_sys_permission(struct inode *, int);
94 struct file_operations proc_sys_file_operations =
96 NULL, /* lseek */
97 proc_readsys, /* read */
98 proc_writesys, /* write */
99 NULL, /* readdir */
100 NULL, /* poll */
101 NULL, /* ioctl */
102 NULL, /* mmap */
103 NULL, /* no special open code */
104 NULL, /* no special flush code */
105 NULL, /* no special release code */
106 NULL /* can't fsync */
109 struct inode_operations proc_sys_inode_operations =
111 &proc_sys_file_operations,
112 NULL, /* create */
113 NULL, /* lookup */
114 NULL, /* link */
115 NULL, /* unlink */
116 NULL, /* symlink */
117 NULL, /* mkdir */
118 NULL, /* rmdir */
119 NULL, /* mknod */
120 NULL, /* rename */
121 NULL, /* readlink */
122 NULL, /* follow_link */
123 NULL, /* get_block */
124 NULL, /* readpage */
125 NULL, /* writepage */
126 NULL, /* flushpage */
127 NULL, /* truncate */
128 proc_sys_permission, /* permission */
129 NULL, /* smap */
130 NULL /* revalidate */
133 extern struct proc_dir_entry proc_sys_root;
135 static void register_proc_table(ctl_table *, struct proc_dir_entry *);
136 static void unregister_proc_table(ctl_table *, struct proc_dir_entry *);
137 #endif
138 extern int inodes_stat[];
139 extern int dentry_stat[];
141 /* The default sysctl tables: */
143 static ctl_table root_table[] = {
144 {CTL_KERN, "kernel", NULL, 0, 0555, kern_table},
145 {CTL_VM, "vm", NULL, 0, 0555, vm_table},
146 #ifdef CONFIG_NET
147 {CTL_NET, "net", NULL, 0, 0555, net_table},
148 #endif
149 {CTL_PROC, "proc", NULL, 0, 0555, proc_table},
150 {CTL_FS, "fs", NULL, 0, 0555, fs_table},
151 {CTL_DEBUG, "debug", NULL, 0, 0555, debug_table},
152 {CTL_DEV, "dev", NULL, 0, 0555, dev_table},
156 static ctl_table kern_table[] = {
157 {KERN_OSTYPE, "ostype", system_utsname.sysname, 64,
158 0444, NULL, &proc_doutsstring, &sysctl_string},
159 {KERN_OSRELEASE, "osrelease", system_utsname.release, 64,
160 0444, NULL, &proc_doutsstring, &sysctl_string},
161 {KERN_VERSION, "version", system_utsname.version, 64,
162 0444, NULL, &proc_doutsstring, &sysctl_string},
163 {KERN_NODENAME, "hostname", system_utsname.nodename, 64,
164 0644, NULL, &proc_doutsstring, &sysctl_string},
165 {KERN_DOMAINNAME, "domainname", system_utsname.domainname, 64,
166 0644, NULL, &proc_doutsstring, &sysctl_string},
167 {KERN_PANIC, "panic", &panic_timeout, sizeof(int),
168 0644, NULL, &proc_dointvec},
169 #ifdef CONFIG_BLK_DEV_INITRD
170 {KERN_REALROOTDEV, "real-root-dev", &real_root_dev, sizeof(int),
171 0644, NULL, &proc_dointvec},
172 #endif
173 #ifdef __sparc__
174 {KERN_SPARC_REBOOT, "reboot-cmd", reboot_command,
175 256, 0644, NULL, &proc_dostring, &sysctl_string },
176 #endif
177 #ifdef __powerpc__
178 {KERN_PPC_HTABRECLAIM, "htab-reclaim", &htab_reclaim_on, sizeof(int),
179 0644, NULL, &proc_dointvec},
180 {KERN_PPC_ZEROPAGED, "zero-paged", &zero_paged_on, sizeof(int),
181 0644, NULL, &proc_dointvec},
182 {KERN_PPC_POWERSAVE_NAP, "powersave-nap", &powersave_nap, sizeof(int),
183 0644, NULL, &proc_dointvec},
184 {KERN_PPC_L2CR, "l2cr", NULL, 0,
185 0644, NULL, &proc_dol2crvec},
186 #endif
187 {KERN_CTLALTDEL, "ctrl-alt-del", &C_A_D, sizeof(int),
188 0644, NULL, &proc_dointvec},
189 {KERN_PRINTK, "printk", &console_loglevel, 4*sizeof(int),
190 0644, NULL, &proc_dointvec},
191 #ifdef CONFIG_KMOD
192 {KERN_MODPROBE, "modprobe", &modprobe_path, 256,
193 0644, NULL, &proc_dostring, &sysctl_string },
194 #endif
195 #ifdef CONFIG_CHR_DEV_SG
196 {KERN_SG_BIG_BUFF, "sg-big-buff", &sg_big_buff, sizeof (int),
197 0444, NULL, &proc_dointvec},
198 #endif
199 #ifdef CONFIG_BSD_PROCESS_ACCT
200 {KERN_ACCT, "acct", &acct_parm, 3*sizeof(int),
201 0644, NULL, &proc_dointvec},
202 #endif
203 {KERN_RTSIGNR, "rtsig-nr", &nr_queued_signals, sizeof(int),
204 0444, NULL, &proc_dointvec},
205 {KERN_RTSIGMAX, "rtsig-max", &max_queued_signals, sizeof(int),
206 0644, NULL, &proc_dointvec},
207 #ifdef CONFIG_SYSVIPC
208 {KERN_SHMMAX, "shmmax", &shmmax, sizeof (int),
209 0644, NULL, &proc_dointvec},
210 #endif
211 {KERN_MAX_THREADS, "threads-max", &max_threads, sizeof(int),
212 0644, NULL, &proc_dointvec},
216 static ctl_table vm_table[] = {
217 {VM_FREEPG, "freepages",
218 &freepages, sizeof(freepages_t), 0644, NULL, &proc_dointvec},
219 {VM_BDFLUSH, "bdflush", &bdf_prm, 9*sizeof(int), 0600, NULL,
220 &proc_dointvec_minmax, &sysctl_intvec, NULL,
221 &bdflush_min, &bdflush_max},
222 {VM_OVERCOMMIT_MEMORY, "overcommit_memory", &sysctl_overcommit_memory,
223 sizeof(sysctl_overcommit_memory), 0644, NULL, &proc_dointvec},
224 {VM_BUFFERMEM, "buffermem",
225 &buffer_mem, sizeof(buffer_mem_t), 0644, NULL, &proc_dointvec},
226 {VM_PAGECACHE, "pagecache",
227 &page_cache, sizeof(buffer_mem_t), 0644, NULL, &proc_dointvec},
228 {VM_PAGERDAEMON, "kswapd",
229 &pager_daemon, sizeof(pager_daemon_t), 0644, NULL, &proc_dointvec},
230 {VM_PGT_CACHE, "pagetable_cache",
231 &pgt_cache_water, 2*sizeof(int), 0600, NULL, &proc_dointvec},
232 {VM_PAGE_CLUSTER, "page-cluster",
233 &page_cluster, sizeof(int), 0600, NULL, &proc_dointvec},
237 static ctl_table proc_table[] = {
241 static ctl_table fs_table[] = {
242 {FS_NRINODE, "inode-nr", &inodes_stat, 2*sizeof(int),
243 0444, NULL, &proc_dointvec},
244 {FS_STATINODE, "inode-state", &inodes_stat, 7*sizeof(int),
245 0444, NULL, &proc_dointvec},
246 {FS_MAXINODE, "inode-max", &max_inodes, sizeof(int),
247 0644, NULL, &proc_dointvec},
248 {FS_NRFILE, "file-nr", &nr_files, 3*sizeof(int),
249 0444, NULL, &proc_dointvec},
250 {FS_MAXFILE, "file-max", &max_files, sizeof(int),
251 0644, NULL, &proc_dointvec},
252 {FS_NRSUPER, "super-nr", &nr_super_blocks, sizeof(int),
253 0444, NULL, &proc_dointvec},
254 {FS_MAXSUPER, "super-max", &max_super_blocks, sizeof(int),
255 0644, NULL, &proc_dointvec},
256 {FS_NRDQUOT, "dquot-nr", &nr_dquots, 2*sizeof(int),
257 0444, NULL, &proc_dointvec},
258 {FS_MAXDQUOT, "dquot-max", &max_dquots, sizeof(int),
259 0644, NULL, &proc_dointvec},
260 {FS_DENTRY, "dentry-state", &dentry_stat, 6*sizeof(int),
261 0444, NULL, &proc_dointvec},
265 static ctl_table debug_table[] = {
269 static ctl_table dev_table[] = {
274 void __init sysctl_init(void)
276 #ifdef CONFIG_PROC_FS
277 register_proc_table(root_table, &proc_sys_root);
278 #endif
282 int do_sysctl (int *name, int nlen,
283 void *oldval, size_t *oldlenp,
284 void *newval, size_t newlen)
286 int error;
287 struct ctl_table_header *tmp;
288 void *context;
290 if (nlen == 0 || nlen >= CTL_MAXNAME)
291 return -ENOTDIR;
293 if (oldval)
295 int old_len;
296 if (!oldlenp)
297 return -EFAULT;
298 if(get_user(old_len, oldlenp))
299 return -EFAULT;
301 tmp = &root_table_header;
302 do {
303 context = NULL;
304 error = parse_table(name, nlen, oldval, oldlenp,
305 newval, newlen, tmp->ctl_table, &context);
306 if (context)
307 kfree(context);
308 if (error != -ENOTDIR)
309 return error;
310 tmp = tmp->DLIST_NEXT(ctl_entry);
311 } while (tmp != &root_table_header);
312 return -ENOTDIR;
315 extern asmlinkage int sys_sysctl(struct __sysctl_args *args)
317 struct __sysctl_args tmp;
318 int error;
320 if(copy_from_user(&tmp, args, sizeof(tmp)))
321 return -EFAULT;
323 lock_kernel();
324 error = do_sysctl(tmp.name, tmp.nlen, tmp.oldval, tmp.oldlenp,
325 tmp.newval, tmp.newlen);
326 unlock_kernel();
327 return error;
330 /* Like in_group_p, but testing against egid, not fsgid */
331 static int in_egroup_p(gid_t grp)
333 if (grp != current->egid) {
334 int i = current->ngroups;
335 if (i) {
336 gid_t *groups = current->groups;
337 do {
338 if (*groups == grp)
339 goto out;
340 groups++;
341 i--;
342 } while (i);
344 return 0;
346 out:
347 return 1;
350 /* ctl_perm does NOT grant the superuser all rights automatically, because
351 some sysctl variables are readonly even to root. */
353 static int test_perm(int mode, int op)
355 if (!current->euid)
356 mode >>= 6;
357 else if (in_egroup_p(0))
358 mode >>= 3;
359 if ((mode & op & 0007) == op)
360 return 0;
361 return -EACCES;
364 static inline int ctl_perm(ctl_table *table, int op)
366 return test_perm(table->mode, op);
369 static int parse_table(int *name, int nlen,
370 void *oldval, size_t *oldlenp,
371 void *newval, size_t newlen,
372 ctl_table *table, void **context)
374 int error;
375 repeat:
376 if (!nlen)
377 return -ENOTDIR;
379 for ( ; table->ctl_name; table++) {
380 int n;
381 if(get_user(n,name))
382 return -EFAULT;
383 if (n == table->ctl_name ||
384 table->ctl_name == CTL_ANY) {
385 if (table->child) {
386 if (ctl_perm(table, 001))
387 return -EPERM;
388 if (table->strategy) {
389 error = table->strategy(
390 table, name, nlen,
391 oldval, oldlenp,
392 newval, newlen, context);
393 if (error)
394 return error;
396 name++;
397 nlen--;
398 table = table->child;
399 goto repeat;
401 error = do_sysctl_strategy(table, name, nlen,
402 oldval, oldlenp,
403 newval, newlen, context);
404 return error;
407 return -ENOTDIR;
410 /* Perform the actual read/write of a sysctl table entry. */
411 int do_sysctl_strategy (ctl_table *table,
412 int *name, int nlen,
413 void *oldval, size_t *oldlenp,
414 void *newval, size_t newlen, void **context)
416 int op = 0, rc, len;
418 if (oldval)
419 op |= 004;
420 if (newval)
421 op |= 002;
422 if (ctl_perm(table, op))
423 return -EPERM;
425 if (table->strategy) {
426 rc = table->strategy(table, name, nlen, oldval, oldlenp,
427 newval, newlen, context);
428 if (rc < 0)
429 return rc;
430 if (rc > 0)
431 return 0;
434 /* If there is no strategy routine, or if the strategy returns
435 * zero, proceed with automatic r/w */
436 if (table->data && table->maxlen) {
437 if (oldval && oldlenp) {
438 get_user(len, oldlenp);
439 if (len) {
440 if (len > table->maxlen)
441 len = table->maxlen;
442 if(copy_to_user(oldval, table->data, len))
443 return -EFAULT;
444 if(put_user(len, oldlenp))
445 return -EFAULT;
448 if (newval && newlen) {
449 len = newlen;
450 if (len > table->maxlen)
451 len = table->maxlen;
452 if(copy_from_user(table->data, newval, len))
453 return -EFAULT;
456 return 0;
459 struct ctl_table_header *register_sysctl_table(ctl_table * table,
460 int insert_at_head)
462 struct ctl_table_header *tmp;
463 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
464 if (!tmp)
465 return 0;
466 *tmp = ((struct ctl_table_header) {table, DNODE_NULL});
467 if (insert_at_head)
468 DLIST_INSERT_AFTER(&root_table_header, tmp, ctl_entry);
469 else
470 DLIST_INSERT_BEFORE(&root_table_header, tmp, ctl_entry);
471 #ifdef CONFIG_PROC_FS
472 register_proc_table(table, &proc_sys_root);
473 #endif
474 return tmp;
478 * Unlink and free a ctl_table.
480 void unregister_sysctl_table(struct ctl_table_header * header)
482 DLIST_DELETE(header, ctl_entry);
483 #ifdef CONFIG_PROC_FS
484 unregister_proc_table(header->ctl_table, &proc_sys_root);
485 #endif
486 kfree(header);
490 * /proc/sys support
493 #ifdef CONFIG_PROC_FS
495 /* Scan the sysctl entries in table and add them all into /proc */
496 static void register_proc_table(ctl_table * table, struct proc_dir_entry *root)
498 struct proc_dir_entry *de;
499 int len;
500 mode_t mode;
502 for (; table->ctl_name; table++) {
503 /* Can't do anything without a proc name. */
504 if (!table->procname)
505 continue;
506 /* Maybe we can't do anything with it... */
507 if (!table->proc_handler && !table->child) {
508 printk(KERN_WARNING "SYSCTL: Can't register %s\n",
509 table->procname);
510 continue;
513 len = strlen(table->procname);
514 mode = table->mode;
516 de = NULL;
517 if (table->proc_handler)
518 mode |= S_IFREG;
519 else {
520 mode |= S_IFDIR;
521 for (de = root->subdir; de; de = de->next) {
522 if (proc_match(len, table->procname, de))
523 break;
525 /* If the subdir exists already, de is non-NULL */
528 if (!de) {
529 de = create_proc_entry(table->procname, mode, root);
530 if (!de)
531 continue;
532 de->data = (void *) table;
533 if (table->proc_handler)
534 de->ops = &proc_sys_inode_operations;
537 table->de = de;
538 if (de->mode & S_IFDIR)
539 register_proc_table(table->child, de);
544 * Unregister a /proc sysctl table and any subdirectories.
546 static void unregister_proc_table(ctl_table * table, struct proc_dir_entry *root)
548 struct proc_dir_entry *de;
549 for (; table->ctl_name; table++) {
550 if (!(de = table->de))
551 continue;
552 if (de->mode & S_IFDIR) {
553 if (!table->child) {
554 printk (KERN_ALERT "Help - malformed sysctl tree on free\n");
555 continue;
557 unregister_proc_table(table->child, de);
559 /* Don't unregister directories which still have entries.. */
560 if (de->subdir)
561 continue;
564 /* Don't unregoster proc entries that are still being used.. */
565 if (de->count)
566 continue;
568 proc_unregister(root, de->low_ino);
569 table->de = NULL;
570 kfree(de);
574 static ssize_t do_rw_proc(int write, struct file * file, char * buf,
575 size_t count, loff_t *ppos)
577 int op;
578 struct proc_dir_entry *de;
579 struct ctl_table *table;
580 size_t res;
581 ssize_t error;
583 de = (struct proc_dir_entry*) file->f_dentry->d_inode->u.generic_ip;
584 if (!de || !de->data)
585 return -ENOTDIR;
586 table = (struct ctl_table *) de->data;
587 if (!table || !table->proc_handler)
588 return -ENOTDIR;
589 op = (write ? 002 : 004);
590 if (ctl_perm(table, op))
591 return -EPERM;
593 res = count;
596 * FIXME: we need to pass on ppos to the handler.
599 error = (*table->proc_handler) (table, write, file, buf, &res);
600 if (error)
601 return error;
602 return res;
605 static ssize_t proc_readsys(struct file * file, char * buf,
606 size_t count, loff_t *ppos)
608 return do_rw_proc(0, file, buf, count, ppos);
611 static ssize_t proc_writesys(struct file * file, const char * buf,
612 size_t count, loff_t *ppos)
614 return do_rw_proc(1, file, (char *) buf, count, ppos);
617 static int proc_sys_permission(struct inode *inode, int op)
619 return test_perm(inode->i_mode, op);
622 int proc_dostring(ctl_table *table, int write, struct file *filp,
623 void *buffer, size_t *lenp)
625 int len;
626 char *p, c;
628 if (!table->data || !table->maxlen || !*lenp ||
629 (filp->f_pos && !write)) {
630 *lenp = 0;
631 return 0;
634 if (write) {
635 len = 0;
636 p = buffer;
637 while (len < *lenp) {
638 if(get_user(c, p++))
639 return -EFAULT;
640 if (c == 0 || c == '\n')
641 break;
642 len++;
644 if (len >= table->maxlen)
645 len = table->maxlen-1;
646 if(copy_from_user(table->data, buffer, len))
647 return -EFAULT;
648 ((char *) table->data)[len] = 0;
649 filp->f_pos += *lenp;
650 } else {
651 len = strlen(table->data);
652 if (len > table->maxlen)
653 len = table->maxlen;
654 if (len > *lenp)
655 len = *lenp;
656 if (len)
657 if(copy_to_user(buffer, table->data, len))
658 return -EFAULT;
659 if (len < *lenp) {
660 if(put_user('\n', ((char *) buffer) + len))
661 return -EFAULT;
662 len++;
664 *lenp = len;
665 filp->f_pos += len;
667 return 0;
671 * Special case of dostring for the UTS structure. This has locks
672 * to observe. Should this be in kernel/sys.c ????
675 static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
676 void *buffer, size_t *lenp)
678 int r;
679 down(&uts_sem);
680 r=proc_dostring(table,write,filp,buffer,lenp);
681 up(&uts_sem);
682 return r;
685 static int do_proc_dointvec(ctl_table *table, int write, struct file *filp,
686 void *buffer, size_t *lenp, int conv)
688 int *i, vleft, first=1, len, left, neg, val;
689 #define TMPBUFLEN 20
690 char buf[TMPBUFLEN], *p;
692 if (!table->data || !table->maxlen || !*lenp ||
693 (filp->f_pos && !write)) {
694 *lenp = 0;
695 return 0;
698 i = (int *) table->data;
699 vleft = table->maxlen / sizeof(int);
700 left = *lenp;
702 for (; left && vleft--; i++, first=0) {
703 if (write) {
704 while (left) {
705 char c;
706 if(get_user(c,(char *) buffer))
707 return -EFAULT;
708 if (!isspace(c))
709 break;
710 left--;
711 ((char *) buffer)++;
713 if (!left)
714 break;
715 neg = 0;
716 len = left;
717 if (len > TMPBUFLEN-1)
718 len = TMPBUFLEN-1;
719 if(copy_from_user(buf, buffer, len))
720 return -EFAULT;
721 buf[len] = 0;
722 p = buf;
723 if (*p == '-' && left > 1) {
724 neg = 1;
725 left--, p++;
727 if (*p < '0' || *p > '9')
728 break;
729 val = simple_strtoul(p, &p, 0) * conv;
730 len = p-buf;
731 if ((len < left) && *p && !isspace(*p))
732 break;
733 if (neg)
734 val = -val;
735 buffer += len;
736 left -= len;
737 *i = val;
738 } else {
739 p = buf;
740 if (!first)
741 *p++ = '\t';
742 sprintf(p, "%d", (*i) / conv);
743 len = strlen(buf);
744 if (len > left)
745 len = left;
746 if(copy_to_user(buffer, buf, len))
747 return -EFAULT;
748 left -= len;
749 buffer += len;
753 if (!write && !first && left) {
754 if(put_user('\n', (char *) buffer))
755 return -EFAULT;
756 left--, buffer++;
758 if (write) {
759 p = (char *) buffer;
760 while (left) {
761 char c;
762 if(get_user(c, p++))
763 return -EFAULT;
764 if (!isspace(c))
765 break;
766 left--;
769 if (write && first)
770 return -EINVAL;
771 *lenp -= left;
772 filp->f_pos += *lenp;
773 return 0;
776 int proc_dointvec(ctl_table *table, int write, struct file *filp,
777 void *buffer, size_t *lenp)
779 return do_proc_dointvec(table,write,filp,buffer,lenp,1);
782 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
783 void *buffer, size_t *lenp)
785 int *i, *min, *max, vleft, first=1, len, left, neg, val;
786 #define TMPBUFLEN 20
787 char buf[TMPBUFLEN], *p;
789 if (!table->data || !table->maxlen || !*lenp ||
790 (filp->f_pos && !write)) {
791 *lenp = 0;
792 return 0;
795 i = (int *) table->data;
796 min = (int *) table->extra1;
797 max = (int *) table->extra2;
798 vleft = table->maxlen / sizeof(int);
799 left = *lenp;
801 for (; left && vleft--; i++, first=0) {
802 if (write) {
803 while (left) {
804 char c;
805 if(get_user(c, (char *) buffer))
806 return -EFAULT;
807 if (!isspace(c))
808 break;
809 left--;
810 ((char *) buffer)++;
812 if (!left)
813 break;
814 neg = 0;
815 len = left;
816 if (len > TMPBUFLEN-1)
817 len = TMPBUFLEN-1;
818 if(copy_from_user(buf, buffer, len))
819 return -EFAULT;
820 buf[len] = 0;
821 p = buf;
822 if (*p == '-' && left > 1) {
823 neg = 1;
824 left--, p++;
826 if (*p < '0' || *p > '9')
827 break;
828 val = simple_strtoul(p, &p, 0);
829 len = p-buf;
830 if ((len < left) && *p && !isspace(*p))
831 break;
832 if (neg)
833 val = -val;
834 buffer += len;
835 left -= len;
837 if (min && val < *min++)
838 continue;
839 if (max && val > *max++)
840 continue;
841 *i = val;
842 } else {
843 p = buf;
844 if (!first)
845 *p++ = '\t';
846 sprintf(p, "%d", *i);
847 len = strlen(buf);
848 if (len > left)
849 len = left;
850 if(copy_to_user(buffer, buf, len))
851 return -EFAULT;
852 left -= len;
853 buffer += len;
857 if (!write && !first && left) {
858 if(put_user('\n', (char *) buffer))
859 return -EFAULT;
860 left--, buffer++;
862 if (write) {
863 p = (char *) buffer;
864 while (left) {
865 char c;
866 if(get_user(c, p++))
867 return -EFAULT;
868 if (!isspace(c))
869 break;
870 left--;
873 if (write && first)
874 return -EINVAL;
875 *lenp -= left;
876 filp->f_pos += *lenp;
877 return 0;
880 /* Like proc_dointvec, but converts seconds to jiffies */
881 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
882 void *buffer, size_t *lenp)
884 return do_proc_dointvec(table,write,filp,buffer,lenp,HZ);
887 #else /* CONFIG_PROC_FS */
889 int proc_dostring(ctl_table *table, int write, struct file *filp,
890 void *buffer, size_t *lenp)
892 return -ENOSYS;
895 static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
896 void *buffer, size_t *lenp)
898 return -ENOSYS;
901 int proc_dointvec(ctl_table *table, int write, struct file *filp,
902 void *buffer, size_t *lenp)
904 return -ENOSYS;
907 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
908 void *buffer, size_t *lenp)
910 return -ENOSYS;
913 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
914 void *buffer, size_t *lenp)
916 return -ENOSYS;
919 #endif /* CONFIG_PROC_FS */
923 * General sysctl support routines
926 /* The generic string strategy routine: */
927 int sysctl_string(ctl_table *table, int *name, int nlen,
928 void *oldval, size_t *oldlenp,
929 void *newval, size_t newlen, void **context)
931 int l, len;
933 if (!table->data || !table->maxlen)
934 return -ENOTDIR;
936 if (oldval && oldlenp) {
937 if(get_user(len, oldlenp))
938 return -EFAULT;
939 if (len) {
940 l = strlen(table->data);
941 if (len > l) len = l;
942 if (len >= table->maxlen)
943 len = table->maxlen;
944 if(copy_to_user(oldval, table->data, len))
945 return -EFAULT;
946 if(put_user(0, ((char *) oldval) + len))
947 return -EFAULT;
948 if(put_user(len, oldlenp))
949 return -EFAULT;
952 if (newval && newlen) {
953 len = newlen;
954 if (len > table->maxlen)
955 len = table->maxlen;
956 if(copy_from_user(table->data, newval, len))
957 return -EFAULT;
958 if (len == table->maxlen)
959 len--;
960 ((char *) table->data)[len] = 0;
962 return 0;
966 * This function makes sure that all of the integers in the vector
967 * are between the minimum and maximum values given in the arrays
968 * table->extra1 and table->extra2, respectively.
970 int sysctl_intvec(ctl_table *table, int *name, int nlen,
971 void *oldval, size_t *oldlenp,
972 void *newval, size_t newlen, void **context)
974 int i, length, *vec, *min, *max;
976 if (newval && newlen) {
977 if (newlen % sizeof(int) != 0)
978 return -EINVAL;
980 if (!table->extra1 && !table->extra2)
981 return 0;
983 if (newlen > table->maxlen)
984 newlen = table->maxlen;
985 length = newlen / sizeof(int);
987 vec = (int *) newval;
988 min = (int *) table->extra1;
989 max = (int *) table->extra2;
991 for (i = 0; i < length; i++) {
992 int value;
993 get_user(value, vec + i);
994 if (min && value < min[i])
995 return -EINVAL;
996 if (max && value > max[i])
997 return -EINVAL;
1000 return 0;
1003 int do_string (
1004 void *oldval, size_t *oldlenp, void *newval, size_t newlen,
1005 int rdwr, char *data, size_t max)
1007 int l = strlen(data) + 1;
1008 if (newval && !rdwr)
1009 return -EPERM;
1010 if (newval && newlen >= max)
1011 return -EINVAL;
1012 if (oldval) {
1013 int old_l;
1014 if(get_user(old_l, oldlenp))
1015 return -EFAULT;
1016 if (l > old_l)
1017 return -ENOMEM;
1018 if(put_user(l, oldlenp) || copy_to_user(oldval, data, l))
1019 return -EFAULT;
1021 if (newval) {
1022 if(copy_from_user(data, newval, newlen))
1023 return -EFAULT;
1024 data[newlen] = 0;
1026 return 0;
1029 int do_int (
1030 void *oldval, size_t *oldlenp, void *newval, size_t newlen,
1031 int rdwr, int *data)
1033 if (newval && !rdwr)
1034 return -EPERM;
1035 if (newval && newlen != sizeof(int))
1036 return -EINVAL;
1037 if (oldval) {
1038 int old_l;
1039 if(get_user(old_l, oldlenp))
1040 return -EFAULT;
1041 if (old_l < sizeof(int))
1042 return -ENOMEM;
1043 if(put_user(sizeof(int), oldlenp)||copy_to_user(oldval, data, sizeof(int)))
1044 return -EFAULT;
1046 if (newval)
1047 if(copy_from_user(data, newval, sizeof(int)))
1048 return -EFAULT;
1049 return 0;
1052 int do_struct (
1053 void *oldval, size_t *oldlenp, void *newval, size_t newlen,
1054 int rdwr, void *data, size_t len)
1056 if (newval && !rdwr)
1057 return -EPERM;
1058 if (newval && newlen != len)
1059 return -EINVAL;
1060 if (oldval) {
1061 int old_l;
1062 if(get_user(old_l, oldlenp))
1063 return -EFAULT;
1064 if (old_l < len)
1065 return -ENOMEM;
1066 if(put_user(len, oldlenp) || copy_to_user(oldval, data, len))
1067 return -EFAULT;
1069 if (newval)
1070 if(copy_from_user(data, newval, len))
1071 return -EFAULT;
1072 return 0;
1076 #else /* CONFIG_SYSCTL */
1079 extern asmlinkage int sys_sysctl(struct __sysctl_args *args)
1081 return -ENOSYS;
1084 int sysctl_string(ctl_table *table, int *name, int nlen,
1085 void *oldval, size_t *oldlenp,
1086 void *newval, size_t newlen, void **context)
1088 return -ENOSYS;
1091 int sysctl_intvec(ctl_table *table, int *name, int nlen,
1092 void *oldval, size_t *oldlenp,
1093 void *newval, size_t newlen, void **context)
1095 return -ENOSYS;
1098 int proc_dostring(ctl_table *table, int write, struct file *filp,
1099 void *buffer, size_t *lenp)
1101 return -ENOSYS;
1104 int proc_dointvec(ctl_table *table, int write, struct file *filp,
1105 void *buffer, size_t *lenp)
1107 return -ENOSYS;
1110 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
1111 void *buffer, size_t *lenp)
1113 return -ENOSYS;
1116 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
1117 void *buffer, size_t *lenp)
1119 return -ENOSYS;
1122 struct ctl_table_header * register_sysctl_table(ctl_table * table,
1123 int insert_at_head)
1125 return 0;
1128 void unregister_sysctl_table(struct ctl_table_header * table)
1132 #endif /* CONFIG_SYSCTL */