Import 2.1.118
[davej-history.git] / kernel / sysctl.c
blob132739310866f7629e427c9f007993cdd4c1d698
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/sched.h>
16 #include <linux/mm.h>
17 #include <linux/sysctl.h>
18 #include <linux/swapctl.h>
19 #include <linux/proc_fs.h>
20 #include <linux/malloc.h>
21 #include <linux/stat.h>
22 #include <linux/ctype.h>
23 #include <linux/utsname.h>
24 #include <linux/swapctl.h>
25 #include <linux/smp.h>
26 #include <linux/smp_lock.h>
27 #include <linux/init.h>
28 #include <linux/fs.h>
30 #include <asm/bitops.h>
31 #include <asm/uaccess.h>
33 #ifdef CONFIG_ROOT_NFS
34 #include <linux/nfs_fs.h>
35 #endif
37 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
39 /* External variables not in a header file. */
40 extern int panic_timeout;
41 extern int console_loglevel, C_A_D, swapout_interval;
42 extern int bdf_prm[], bdflush_min[], bdflush_max[];
43 extern char binfmt_java_interpreter[], binfmt_java_appletviewer[];
44 extern int sysctl_overcommit_memory;
45 #ifdef CONFIG_KMOD
46 extern char modprobe_path[];
47 #endif
48 #ifdef CONFIG_CHR_DEV_SG
49 extern int sg_big_buff;
50 #endif
52 #ifdef __sparc__
53 extern char reboot_command [];
54 #endif
55 #ifdef __powerpc__
56 extern unsigned long htab_reclaim_on, zero_paged_on, powersave_nap;
57 #endif
59 extern int pgt_cache_water[];
61 static int parse_table(int *, int, void *, size_t *, void *, size_t,
62 ctl_table *, void **);
63 static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
64 void *buffer, size_t *lenp);
67 static ctl_table root_table[];
68 static struct ctl_table_header root_table_header =
69 {root_table, DNODE_SINGLE(&root_table_header)};
71 static ctl_table kern_table[];
72 static ctl_table vm_table[];
73 extern ctl_table net_table[];
74 static ctl_table proc_table[];
75 static ctl_table fs_table[];
76 static ctl_table debug_table[];
77 static ctl_table dev_table[];
80 /* /proc declarations: */
82 #ifdef CONFIG_PROC_FS
84 static ssize_t proc_readsys(struct file *, char *, size_t, loff_t *);
85 static ssize_t proc_writesys(struct file *, const char *, size_t, loff_t *);
86 static int proc_sys_permission(struct inode *, int);
88 struct file_operations proc_sys_file_operations =
90 NULL, /* lseek */
91 proc_readsys, /* read */
92 proc_writesys, /* write */
93 NULL, /* readdir */
94 NULL, /* poll */
95 NULL, /* ioctl */
96 NULL, /* mmap */
97 NULL, /* no special open code */
98 NULL, /* no special flush code */
99 NULL, /* no special release code */
100 NULL /* can't fsync */
103 struct inode_operations proc_sys_inode_operations =
105 &proc_sys_file_operations,
106 NULL, /* create */
107 NULL, /* lookup */
108 NULL, /* link */
109 NULL, /* unlink */
110 NULL, /* symlink */
111 NULL, /* mkdir */
112 NULL, /* rmdir */
113 NULL, /* mknod */
114 NULL, /* rename */
115 NULL, /* readlink */
116 NULL, /* follow_link */
117 NULL, /* readpage */
118 NULL, /* writepage */
119 NULL, /* bmap */
120 NULL, /* truncate */
121 proc_sys_permission
124 extern struct proc_dir_entry proc_sys_root;
126 extern int inodes_stat[];
127 extern int dentry_stat[];
128 static void register_proc_table(ctl_table *, struct proc_dir_entry *);
129 static void unregister_proc_table(ctl_table *, struct proc_dir_entry *);
130 #endif
132 /* The default sysctl tables: */
134 static ctl_table root_table[] = {
135 {CTL_KERN, "kernel", NULL, 0, 0555, kern_table},
136 {CTL_VM, "vm", NULL, 0, 0555, vm_table},
137 {CTL_NET, "net", NULL, 0, 0555, net_table},
138 {CTL_PROC, "proc", NULL, 0, 0555, proc_table},
139 {CTL_FS, "fs", NULL, 0, 0555, fs_table},
140 {CTL_DEBUG, "debug", NULL, 0, 0555, debug_table},
141 {CTL_DEV, "dev", NULL, 0, 0555, dev_table},
145 static ctl_table kern_table[] = {
146 {KERN_OSTYPE, "ostype", system_utsname.sysname, 64,
147 0444, NULL, &proc_doutsstring, &sysctl_string},
148 {KERN_OSRELEASE, "osrelease", system_utsname.release, 64,
149 0444, NULL, &proc_doutsstring, &sysctl_string},
150 {KERN_VERSION, "version", system_utsname.version, 64,
151 0444, NULL, &proc_doutsstring, &sysctl_string},
152 {KERN_NODENAME, "hostname", system_utsname.nodename, 64,
153 0644, NULL, &proc_doutsstring, &sysctl_string},
154 {KERN_DOMAINNAME, "domainname", system_utsname.domainname, 64,
155 0644, NULL, &proc_doutsstring, &sysctl_string},
156 {KERN_PANIC, "panic", &panic_timeout, sizeof(int),
157 0644, NULL, &proc_dointvec},
158 #ifdef CONFIG_BLK_DEV_INITRD
159 {KERN_REALROOTDEV, "real-root-dev", &real_root_dev, sizeof(int),
160 0644, NULL, &proc_dointvec},
161 #endif
162 #ifdef CONFIG_BINFMT_JAVA
163 {KERN_JAVA_INTERPRETER, "java-interpreter", binfmt_java_interpreter,
164 64, 0644, NULL, &proc_dostring, &sysctl_string },
165 {KERN_JAVA_APPLETVIEWER, "java-appletviewer", binfmt_java_appletviewer,
166 64, 0644, NULL, &proc_dostring, &sysctl_string },
167 #endif
168 #ifdef __sparc__
169 {KERN_SPARC_REBOOT, "reboot-cmd", reboot_command,
170 256, 0644, NULL, &proc_dostring, &sysctl_string },
171 #endif
172 #ifdef __powerpc__
173 {KERN_PPC_HTABRECLAIM, "htab-reclaim", &htab_reclaim_on, sizeof(int),
174 0644, NULL, &proc_dointvec},
175 {KERN_PPC_ZEROPAGED, "zero-paged", &zero_paged_on, sizeof(int),
176 0644, NULL, &proc_dointvec},
177 {KERN_PPC_POWERSAVE_NAP, "powersave-nap", &powersave_nap, sizeof(int),
178 0644, NULL, &proc_dointvec},
179 #endif
180 {KERN_CTLALTDEL, "ctrl-alt-del", &C_A_D, sizeof(int),
181 0644, NULL, &proc_dointvec},
182 {KERN_PRINTK, "printk", &console_loglevel, 4*sizeof(int),
183 0644, NULL, &proc_dointvec},
184 #ifdef CONFIG_KMOD
185 {KERN_MODPROBE, "modprobe", &modprobe_path, 256,
186 0644, NULL, &proc_dostring, &sysctl_string },
187 #endif
188 #ifdef CONFIG_CHR_DEV_SG
189 {KERN_SG_BIG_BUFF, "sg-big-buff", &sg_big_buff, sizeof (int),
190 0444, NULL, &proc_dointvec},
191 #endif
195 static ctl_table vm_table[] = {
196 {VM_SWAPCTL, "swapctl",
197 &swap_control, sizeof(swap_control_t), 0644, NULL, &proc_dointvec},
198 {VM_SWAPOUT, "swapout_interval",
199 &swapout_interval, sizeof(int), 0644, NULL, &proc_dointvec},
200 {VM_FREEPG, "freepages",
201 &freepages, sizeof(freepages_t), 0644, NULL, &proc_dointvec},
202 {VM_BDFLUSH, "bdflush", &bdf_prm, 9*sizeof(int), 0600, NULL,
203 &proc_dointvec_minmax, &sysctl_intvec, NULL,
204 &bdflush_min, &bdflush_max},
205 {VM_OVERCOMMIT_MEMORY, "overcommit_memory", &sysctl_overcommit_memory,
206 sizeof(sysctl_overcommit_memory), 0644, NULL, &proc_dointvec},
207 {VM_BUFFERMEM, "buffermem",
208 &buffer_mem, sizeof(buffer_mem_t), 0644, NULL, &proc_dointvec},
209 {VM_PAGECACHE, "pagecache",
210 &page_cache, sizeof(buffer_mem_t), 0644, NULL, &proc_dointvec},
211 {VM_PAGERDAEMON, "kswapd",
212 &pager_daemon, sizeof(pager_daemon_t), 0644, NULL, &proc_dointvec},
213 {VM_PGT_CACHE, "pagetable_cache",
214 &pgt_cache_water, 2*sizeof(int), 0600, NULL, &proc_dointvec},
218 static ctl_table proc_table[] = {
222 static ctl_table fs_table[] = {
223 {FS_NRINODE, "inode-nr", &inodes_stat, 2*sizeof(int),
224 0444, NULL, &proc_dointvec},
225 {FS_STATINODE, "inode-state", &inodes_stat, 7*sizeof(int),
226 0444, NULL, &proc_dointvec},
227 {FS_MAXINODE, "inode-max", &max_inodes, sizeof(int),
228 0644, NULL, &proc_dointvec},
229 {FS_NRFILE, "file-nr", &nr_files, 3*sizeof(int),
230 0444, NULL, &proc_dointvec},
231 {FS_MAXFILE, "file-max", &max_files, sizeof(int),
232 0644, NULL, &proc_dointvec},
233 {FS_NRSUPER, "super-nr", &nr_super_blocks, sizeof(int),
234 0444, NULL, &proc_dointvec},
235 {FS_MAXSUPER, "super-max", &max_super_blocks, sizeof(int),
236 0644, NULL, &proc_dointvec},
237 {FS_NRDQUOT, "dquot-nr", &nr_dquots, 2*sizeof(int),
238 0444, NULL, &proc_dointvec},
239 {FS_MAXDQUOT, "dquot-max", &max_dquots, sizeof(int),
240 0644, NULL, &proc_dointvec},
241 {FS_DENTRY, "dentry-state", &dentry_stat, 6*sizeof(int),
242 0444, NULL, &proc_dointvec},
246 static ctl_table debug_table[] = {
250 static ctl_table dev_table[] = {
255 void __init sysctl_init(void)
257 #ifdef CONFIG_PROC_FS
258 register_proc_table(root_table, &proc_sys_root);
259 #endif
263 int do_sysctl (int *name, int nlen,
264 void *oldval, size_t *oldlenp,
265 void *newval, size_t newlen)
267 int error;
268 struct ctl_table_header *tmp;
269 void *context;
271 if (nlen == 0 || nlen >= CTL_MAXNAME)
272 return -ENOTDIR;
274 if (oldval)
276 int old_len;
277 if (!oldlenp)
278 return -EFAULT;
279 if(get_user(old_len, oldlenp))
280 return -EFAULT;
282 tmp = &root_table_header;
283 do {
284 context = NULL;
285 error = parse_table(name, nlen, oldval, oldlenp,
286 newval, newlen, tmp->ctl_table, &context);
287 if (context)
288 kfree(context);
289 if (error != -ENOTDIR)
290 return error;
291 tmp = tmp->DLIST_NEXT(ctl_entry);
292 } while (tmp != &root_table_header);
293 return -ENOTDIR;
296 extern asmlinkage int sys_sysctl(struct __sysctl_args *args)
298 struct __sysctl_args tmp;
299 int error;
301 if(copy_from_user(&tmp, args, sizeof(tmp)))
302 return -EFAULT;
304 lock_kernel();
305 error = do_sysctl(tmp.name, tmp.nlen, tmp.oldval, tmp.oldlenp,
306 tmp.newval, tmp.newlen);
307 unlock_kernel();
308 return error;
311 /* Like in_group_p, but testing against egid, not fsgid */
312 static int in_egroup_p(gid_t grp)
314 if (grp != current->egid) {
315 int i = current->ngroups;
316 if (i) {
317 gid_t *groups = current->groups;
318 do {
319 if (*groups == grp)
320 goto out;
321 groups++;
322 i--;
323 } while (i);
325 return 0;
327 out:
328 return 1;
331 /* ctl_perm does NOT grant the superuser all rights automatically, because
332 some sysctl variables are readonly even to root. */
334 static int test_perm(int mode, int op)
336 if (!current->euid)
337 mode >>= 6;
338 else if (in_egroup_p(0))
339 mode >>= 3;
340 if ((mode & op & 0007) == op)
341 return 0;
342 return -EACCES;
345 static inline int ctl_perm(ctl_table *table, int op)
347 return test_perm(table->mode, op);
350 static int parse_table(int *name, int nlen,
351 void *oldval, size_t *oldlenp,
352 void *newval, size_t newlen,
353 ctl_table *table, void **context)
355 int error;
356 repeat:
357 if (!nlen)
358 return -ENOTDIR;
360 for ( ; table->ctl_name; table++) {
361 int n;
362 if(get_user(n,name))
363 return -EFAULT;
364 if (n == table->ctl_name ||
365 table->ctl_name == CTL_ANY) {
366 if (table->child) {
367 if (ctl_perm(table, 001))
368 return -EPERM;
369 if (table->strategy) {
370 error = table->strategy(
371 table, name, nlen,
372 oldval, oldlenp,
373 newval, newlen, context);
374 if (error)
375 return error;
377 name++;
378 nlen--;
379 table = table->child;
380 goto repeat;
382 error = do_sysctl_strategy(table, name, nlen,
383 oldval, oldlenp,
384 newval, newlen, context);
385 return error;
388 return -ENOTDIR;
391 /* Perform the actual read/write of a sysctl table entry. */
392 int do_sysctl_strategy (ctl_table *table,
393 int *name, int nlen,
394 void *oldval, size_t *oldlenp,
395 void *newval, size_t newlen, void **context)
397 int op = 0, rc, len;
399 if (oldval)
400 op |= 004;
401 if (newval)
402 op |= 002;
403 if (ctl_perm(table, op))
404 return -EPERM;
406 if (table->strategy) {
407 rc = table->strategy(table, name, nlen, oldval, oldlenp,
408 newval, newlen, context);
409 if (rc < 0)
410 return rc;
411 if (rc > 0)
412 return 0;
415 /* If there is no strategy routine, or if the strategy returns
416 * zero, proceed with automatic r/w */
417 if (table->data && table->maxlen) {
418 if (oldval && oldlenp) {
419 get_user(len, oldlenp);
420 if (len) {
421 if (len > table->maxlen)
422 len = table->maxlen;
423 if(copy_to_user(oldval, table->data, len))
424 return -EFAULT;
425 if(put_user(len, oldlenp))
426 return -EFAULT;
429 if (newval && newlen) {
430 len = newlen;
431 if (len > table->maxlen)
432 len = table->maxlen;
433 if(copy_from_user(table->data, newval, len))
434 return -EFAULT;
437 return 0;
440 struct ctl_table_header *register_sysctl_table(ctl_table * table,
441 int insert_at_head)
443 struct ctl_table_header *tmp;
444 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
445 if (!tmp)
446 return 0;
447 *tmp = ((struct ctl_table_header) {table, DNODE_NULL});
448 if (insert_at_head)
449 DLIST_INSERT_AFTER(&root_table_header, tmp, ctl_entry);
450 else
451 DLIST_INSERT_BEFORE(&root_table_header, tmp, ctl_entry);
452 #ifdef CONFIG_PROC_FS
453 register_proc_table(table, &proc_sys_root);
454 #endif
455 return tmp;
459 * Unlink and free a ctl_table.
461 void unregister_sysctl_table(struct ctl_table_header * header)
463 DLIST_DELETE(header, ctl_entry);
464 #ifdef CONFIG_PROC_FS
465 unregister_proc_table(header->ctl_table, &proc_sys_root);
466 #endif
467 kfree(header);
471 * /proc/sys support
474 #ifdef CONFIG_PROC_FS
476 /* Scan the sysctl entries in table and add them all into /proc */
477 static void register_proc_table(ctl_table * table, struct proc_dir_entry *root)
479 struct proc_dir_entry *de;
480 int len;
481 mode_t mode;
483 for (; table->ctl_name; table++) {
484 /* Can't do anything without a proc name. */
485 if (!table->procname)
486 continue;
487 /* Maybe we can't do anything with it... */
488 if (!table->proc_handler && !table->child) {
489 printk(KERN_WARNING "SYSCTL: Can't register %s\n",
490 table->procname);
491 continue;
494 len = strlen(table->procname);
495 mode = table->mode;
497 de = NULL;
498 if (table->proc_handler)
499 mode |= S_IFREG;
500 else {
501 mode |= S_IFDIR;
502 for (de = root->subdir; de; de = de->next) {
503 if (proc_match(len, table->procname, de))
504 break;
506 /* If the subdir exists already, de is non-NULL */
509 if (!de) {
510 de = create_proc_entry(table->procname, mode, root);
511 if (!de)
512 continue;
513 de->data = (void *) table;
514 if (table->proc_handler)
515 de->ops = &proc_sys_inode_operations;
518 table->de = de;
519 if (de->mode & S_IFDIR)
520 register_proc_table(table->child, de);
525 * Unregister a /proc sysctl table and any subdirectories.
527 static void unregister_proc_table(ctl_table * table, struct proc_dir_entry *root)
529 struct proc_dir_entry *de;
530 for (; table->ctl_name; table++) {
531 if (!(de = table->de))
532 continue;
533 if (de->mode & S_IFDIR) {
534 if (!table->child) {
535 printk (KERN_ALERT "Help - malformed sysctl tree on free\n");
536 continue;
538 unregister_proc_table(table->child, de);
540 /* Don't unregister proc directories which still have
541 entries... */
542 if (!((de->mode & S_IFDIR) && de->subdir)) {
543 proc_unregister(root, de->low_ino);
544 table->de = NULL;
545 kfree(de);
550 static ssize_t do_rw_proc(int write, struct file * file, char * buf,
551 size_t count, loff_t *ppos)
553 int op;
554 struct proc_dir_entry *de;
555 struct ctl_table *table;
556 size_t res;
557 ssize_t error;
559 de = (struct proc_dir_entry*) file->f_dentry->d_inode->u.generic_ip;
560 if (!de || !de->data)
561 return -ENOTDIR;
562 table = (struct ctl_table *) de->data;
563 if (!table || !table->proc_handler)
564 return -ENOTDIR;
565 op = (write ? 002 : 004);
566 if (ctl_perm(table, op))
567 return -EPERM;
569 res = count;
572 * FIXME: we need to pass on ppos to the handler.
575 error = (*table->proc_handler) (table, write, file, buf, &res);
576 if (error)
577 return error;
578 return res;
581 static ssize_t proc_readsys(struct file * file, char * buf,
582 size_t count, loff_t *ppos)
584 return do_rw_proc(0, file, buf, count, ppos);
587 static ssize_t proc_writesys(struct file * file, const char * buf,
588 size_t count, loff_t *ppos)
590 return do_rw_proc(1, file, (char *) buf, count, ppos);
593 static int proc_sys_permission(struct inode *inode, int op)
595 return test_perm(inode->i_mode, op);
598 int proc_dostring(ctl_table *table, int write, struct file *filp,
599 void *buffer, size_t *lenp)
601 int len;
602 char *p, c;
604 if (!table->data || !table->maxlen || !*lenp ||
605 (filp->f_pos && !write)) {
606 *lenp = 0;
607 return 0;
610 if (write) {
611 len = 0;
612 p = buffer;
613 while (len < *lenp) {
614 if(get_user(c, p++))
615 return -EFAULT;
616 if (c == 0 || c == '\n')
617 break;
618 len++;
620 if (len >= table->maxlen)
621 len = table->maxlen-1;
622 if(copy_from_user(table->data, buffer, len))
623 return -EFAULT;
624 ((char *) table->data)[len] = 0;
625 filp->f_pos += *lenp;
626 } else {
627 len = strlen(table->data);
628 if (len > table->maxlen)
629 len = table->maxlen;
630 if (len > *lenp)
631 len = *lenp;
632 if (len)
633 if(copy_to_user(buffer, table->data, len))
634 return -EFAULT;
635 if (len < *lenp) {
636 if(put_user('\n', ((char *) buffer) + len))
637 return -EFAULT;
638 len++;
640 *lenp = len;
641 filp->f_pos += len;
643 return 0;
647 * Special case of dostring for the UTS structure. This has locks
648 * to observe. Should this be in kernel/sys.c ????
651 static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
652 void *buffer, size_t *lenp)
654 int r;
655 down(&uts_sem);
656 r=proc_dostring(table,write,filp,buffer,lenp);
657 up(&uts_sem);
658 return r;
661 static int do_proc_dointvec(ctl_table *table, int write, struct file *filp,
662 void *buffer, size_t *lenp, int conv)
664 int *i, vleft, first=1, len, left, neg, val;
665 #define TMPBUFLEN 20
666 char buf[TMPBUFLEN], *p;
668 if (!table->data || !table->maxlen || !*lenp ||
669 (filp->f_pos && !write)) {
670 *lenp = 0;
671 return 0;
674 i = (int *) table->data;
675 vleft = table->maxlen / sizeof(int);
676 left = *lenp;
678 for (; left && vleft--; i++, first=0) {
679 if (write) {
680 while (left) {
681 char c;
682 if(get_user(c,(char *) buffer))
683 return -EFAULT;
684 if (!isspace(c))
685 break;
686 left--;
687 ((char *) buffer)++;
689 if (!left)
690 break;
691 neg = 0;
692 len = left;
693 if (len > TMPBUFLEN-1)
694 len = TMPBUFLEN-1;
695 if(copy_from_user(buf, buffer, len))
696 return -EFAULT;
697 buf[len] = 0;
698 p = buf;
699 if (*p == '-' && left > 1) {
700 neg = 1;
701 left--, p++;
703 if (*p < '0' || *p > '9')
704 break;
705 val = simple_strtoul(p, &p, 0) * conv;
706 len = p-buf;
707 if ((len < left) && *p && !isspace(*p))
708 break;
709 if (neg)
710 val = -val;
711 buffer += len;
712 left -= len;
713 *i = val;
714 } else {
715 p = buf;
716 if (!first)
717 *p++ = '\t';
718 sprintf(p, "%d", (*i) / conv);
719 len = strlen(buf);
720 if (len > left)
721 len = left;
722 if(copy_to_user(buffer, buf, len))
723 return -EFAULT;
724 left -= len;
725 buffer += len;
729 if (!write && !first && left) {
730 if(put_user('\n', (char *) buffer))
731 return -EFAULT;
732 left--, buffer++;
734 if (write) {
735 p = (char *) buffer;
736 while (left) {
737 char c;
738 if(get_user(c, p++))
739 return -EFAULT;
740 if (!isspace(c))
741 break;
742 left--;
745 if (write && first)
746 return -EINVAL;
747 *lenp -= left;
748 filp->f_pos += *lenp;
749 return 0;
752 int proc_dointvec(ctl_table *table, int write, struct file *filp,
753 void *buffer, size_t *lenp)
755 return do_proc_dointvec(table,write,filp,buffer,lenp,1);
758 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
759 void *buffer, size_t *lenp)
761 int *i, *min, *max, vleft, first=1, len, left, neg, val;
762 #define TMPBUFLEN 20
763 char buf[TMPBUFLEN], *p;
765 if (!table->data || !table->maxlen || !*lenp ||
766 (filp->f_pos && !write)) {
767 *lenp = 0;
768 return 0;
771 i = (int *) table->data;
772 min = (int *) table->extra1;
773 max = (int *) table->extra2;
774 vleft = table->maxlen / sizeof(int);
775 left = *lenp;
777 for (; left && vleft--; i++, first=0) {
778 if (write) {
779 while (left) {
780 char c;
781 if(get_user(c, (char *) buffer))
782 return -EFAULT;
783 if (!isspace(c))
784 break;
785 left--;
786 ((char *) buffer)++;
788 if (!left)
789 break;
790 neg = 0;
791 len = left;
792 if (len > TMPBUFLEN-1)
793 len = TMPBUFLEN-1;
794 if(copy_from_user(buf, buffer, len))
795 return -EFAULT;
796 buf[len] = 0;
797 p = buf;
798 if (*p == '-' && left > 1) {
799 neg = 1;
800 left--, p++;
802 if (*p < '0' || *p > '9')
803 break;
804 val = simple_strtoul(p, &p, 0);
805 len = p-buf;
806 if ((len < left) && *p && !isspace(*p))
807 break;
808 if (neg)
809 val = -val;
810 buffer += len;
811 left -= len;
813 if (min && val < *min++)
814 continue;
815 if (max && val > *max++)
816 continue;
817 *i = val;
818 } else {
819 p = buf;
820 if (!first)
821 *p++ = '\t';
822 sprintf(p, "%d", *i);
823 len = strlen(buf);
824 if (len > left)
825 len = left;
826 if(copy_to_user(buffer, buf, len))
827 return -EFAULT;
828 left -= len;
829 buffer += len;
833 if (!write && !first && left) {
834 if(put_user('\n', (char *) buffer))
835 return -EFAULT;
836 left--, buffer++;
838 if (write) {
839 p = (char *) buffer;
840 while (left) {
841 char c;
842 if(get_user(c, p++))
843 return -EFAULT;
844 if (!isspace(c))
845 break;
846 left--;
849 if (write && first)
850 return -EINVAL;
851 *lenp -= left;
852 filp->f_pos += *lenp;
853 return 0;
856 /* Like proc_dointvec, but converts seconds to jiffies */
857 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
858 void *buffer, size_t *lenp)
860 return do_proc_dointvec(table,write,filp,buffer,lenp,HZ);
863 #else /* CONFIG_PROC_FS */
865 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
866 void *buffer, size_t *lenp)
868 return -ENOSYS;
871 int proc_dostring(ctl_table *table, int write, struct file *filp,
872 void *buffer, size_t *lenp)
874 return -ENOSYS;
877 int proc_dointvec(ctl_table *table, int write, struct file *filp,
878 void *buffer, size_t *lenp)
880 return -ENOSYS;
883 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
884 void *buffer, size_t *lenp)
886 return -ENOSYS;
889 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
890 void *buffer, size_t *lenp)
892 return -ENOSYS;
895 #endif /* CONFIG_PROC_FS */
899 * General sysctl support routines
902 /* The generic string strategy routine: */
903 int sysctl_string(ctl_table *table, int *name, int nlen,
904 void *oldval, size_t *oldlenp,
905 void *newval, size_t newlen, void **context)
907 int l, len;
909 if (!table->data || !table->maxlen)
910 return -ENOTDIR;
912 if (oldval && oldlenp) {
913 if(get_user(len, oldlenp))
914 return -EFAULT;
915 if (len) {
916 l = strlen(table->data);
917 if (len > l) len = l;
918 if (len >= table->maxlen)
919 len = table->maxlen;
920 if(copy_to_user(oldval, table->data, len))
921 return -EFAULT;
922 if(put_user(0, ((char *) oldval) + len))
923 return -EFAULT;
924 if(put_user(len, oldlenp))
925 return -EFAULT;
928 if (newval && newlen) {
929 len = newlen;
930 if (len > table->maxlen)
931 len = table->maxlen;
932 if(copy_from_user(table->data, newval, len))
933 return -EFAULT;
934 if (len == table->maxlen)
935 len--;
936 ((char *) table->data)[len] = 0;
938 return 0;
942 * This function makes sure that all of the integers in the vector
943 * are between the minimum and maximum values given in the arrays
944 * table->extra1 and table->extra2, respectively.
946 int sysctl_intvec(ctl_table *table, int *name, int nlen,
947 void *oldval, size_t *oldlenp,
948 void *newval, size_t newlen, void **context)
950 int i, length, *vec, *min, *max;
952 if (newval && newlen) {
953 if (newlen % sizeof(int) != 0)
954 return -EINVAL;
956 if (!table->extra1 && !table->extra2)
957 return 0;
959 if (newlen > table->maxlen)
960 newlen = table->maxlen;
961 length = newlen / sizeof(int);
963 vec = (int *) newval;
964 min = (int *) table->extra1;
965 max = (int *) table->extra2;
967 for (i = 0; i < length; i++) {
968 int value;
969 get_user(value, vec + i);
970 if (min && value < min[i])
971 return -EINVAL;
972 if (max && value > max[i])
973 return -EINVAL;
976 return 0;
979 int do_string (
980 void *oldval, size_t *oldlenp, void *newval, size_t newlen,
981 int rdwr, char *data, size_t max)
983 int l = strlen(data) + 1;
984 if (newval && !rdwr)
985 return -EPERM;
986 if (newval && newlen >= max)
987 return -EINVAL;
988 if (oldval) {
989 int old_l;
990 if(get_user(old_l, oldlenp))
991 return -EFAULT;
992 if (l > old_l)
993 return -ENOMEM;
994 if(put_user(l, oldlenp) || copy_to_user(oldval, data, l))
995 return -EFAULT;
997 if (newval) {
998 if(copy_from_user(data, newval, newlen))
999 return -EFAULT;
1000 data[newlen] = 0;
1002 return 0;
1005 int do_int (
1006 void *oldval, size_t *oldlenp, void *newval, size_t newlen,
1007 int rdwr, int *data)
1009 if (newval && !rdwr)
1010 return -EPERM;
1011 if (newval && newlen != sizeof(int))
1012 return -EINVAL;
1013 if (oldval) {
1014 int old_l;
1015 if(get_user(old_l, oldlenp))
1016 return -EFAULT;
1017 if (old_l < sizeof(int))
1018 return -ENOMEM;
1019 if(put_user(sizeof(int), oldlenp)||copy_to_user(oldval, data, sizeof(int)))
1020 return -EFAULT;
1022 if (newval)
1023 if(copy_from_user(data, newval, sizeof(int)))
1024 return -EFAULT;
1025 return 0;
1028 int do_struct (
1029 void *oldval, size_t *oldlenp, void *newval, size_t newlen,
1030 int rdwr, void *data, size_t len)
1032 if (newval && !rdwr)
1033 return -EPERM;
1034 if (newval && newlen != len)
1035 return -EINVAL;
1036 if (oldval) {
1037 int old_l;
1038 if(get_user(old_l, oldlenp))
1039 return -EFAULT;
1040 if (old_l < len)
1041 return -ENOMEM;
1042 if(put_user(len, oldlenp) || copy_to_user(oldval, data, len))
1043 return -EFAULT;
1045 if (newval)
1046 if(copy_from_user(data, newval, len))
1047 return -EFAULT;
1048 return 0;
1052 #else /* CONFIG_PROC_FS && CONFIG_SYSCTL */
1055 extern asmlinkage int sys_sysctl(struct __sysctl_args *args)
1057 return -ENOSYS;
1060 int sysctl_string(ctl_table *table, int *name, int nlen,
1061 void *oldval, size_t *oldlenp,
1062 void *newval, size_t newlen, void **context)
1064 return -ENOSYS;
1067 int sysctl_intvec(ctl_table *table, int *name, int nlen,
1068 void *oldval, size_t *oldlenp,
1069 void *newval, size_t newlen, void **context)
1071 return -ENOSYS;
1074 int proc_dostring(ctl_table *table, int write, struct file *filp,
1075 void *buffer, size_t *lenp)
1077 return -ENOSYS;
1080 int proc_dointvec(ctl_table *table, int write, struct file *filp,
1081 void *buffer, size_t *lenp)
1083 return -ENOSYS;
1086 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
1087 void *buffer, size_t *lenp)
1089 return -ENOSYS;
1092 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
1093 void *buffer, size_t *lenp)
1095 return -ENOSYS;
1098 struct ctl_table_header * register_sysctl_table(ctl_table * table,
1099 int insert_at_head)
1101 return 0;
1104 void unregister_sysctl_table(struct ctl_table_header * table)
1108 #endif /* CONFIG_PROC_FS && CONFIG_SYSCTL */