?? sysctl.c
字號:
/* * sysctl.c: General linux system control interface * * Begun 24 March 1995, Stephen Tweedie * Added /proc support, Dec 1995 * Added bdflush entry and intvec min/max checking, 2/23/96, Tom Dyas. * Added hooks for /proc/sys/net (minor, minor patch), 96/4/1, Mike Shaver. * Added kernel/java-{interpreter,appletviewer}, 96/5/10, Mike Shaver. * Dynamic registration fixes, Stephen Tweedie. * Added kswapd-interval, ctrl-alt-del, printk stuff, 1/8/97, Chris Horn. * Made sysctl support optional via CONFIG_SYSCTL, 1/10/97, Chris * Horn. * Added proc_doulongvec_ms_jiffies_minmax, 09/08/99, Carlos H. Bauer. * Added proc_doulongvec_minmax, 09/08/99, Carlos H. Bauer. * Changed linked lists to use list.h instead of lists.h, 02/24/00, Bill * Wendling. * The list_for_each() macro wasn't appropriate for the sysctl loop. * Removed it and replaced it with older style, 03/23/00, Bill Wendling */#include <linux/config.h>#include <linux/slab.h>#include <linux/sysctl.h>#include <linux/swapctl.h>#include <linux/proc_fs.h>#include <linux/ctype.h>#include <linux/utsname.h>#include <linux/capability.h>#include <linux/smp_lock.h>#include <linux/init.h>#include <linux/sysrq.h>#include <linux/highuid.h>#include <asm/uaccess.h>#ifdef CONFIG_ROOT_NFS#include <linux/nfs_fs.h>#endif#if defined(CONFIG_SYSCTL)/* External variables not in a header file. */extern int panic_timeout;extern int C_A_D;extern int bdf_prm[], bdflush_min[], bdflush_max[];extern int sysctl_overcommit_memory;extern int max_threads;extern atomic_t nr_queued_signals;extern int max_queued_signals;extern int sysrq_enabled;extern int core_uses_pid;extern int cad_pid;extern int fairsched;/* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */static int maxolduid = 65535;static int minolduid;#ifdef CONFIG_KMODextern char modprobe_path[];#endif#ifdef CONFIG_HOTPLUGextern char hotplug_path[];#endif#ifdef CONFIG_CHR_DEV_SGextern int sg_big_buff;#endif#ifdef CONFIG_SYSVIPCextern size_t shm_ctlmax;extern size_t shm_ctlall;extern int shm_ctlmni;extern int msg_ctlmax;extern int msg_ctlmnb;extern int msg_ctlmni;extern int sem_ctls[];#endif#ifdef __sparc__extern char reboot_command [];extern int stop_a_enabled;#endif#ifdef CONFIG_ARCH_S390#ifdef CONFIG_MATHEMUextern int sysctl_ieee_emulation_warnings;#endifextern int sysctl_userprocess_debug;#endif#ifdef CONFIG_PPC32extern unsigned long zero_paged_on, powersave_nap;int proc_dol2crvec(ctl_table *table, int write, struct file *filp, void *buffer, size_t *lenp);#endif#ifdef CONFIG_BSD_PROCESS_ACCTextern int acct_parm[];#endifextern int pgt_cache_water[];static int parse_table(int *, int, void *, size_t *, void *, size_t, ctl_table *, void **);static int proc_doutsstring(ctl_table *table, int write, struct file *filp, void *buffer, size_t *lenp);static ctl_table root_table[];static struct ctl_table_header root_table_header = { root_table, LIST_HEAD_INIT(root_table_header.ctl_entry) };static ctl_table kern_table[];static ctl_table vm_table[];#ifdef CONFIG_NETextern ctl_table net_table[];#endifstatic ctl_table proc_table[];static ctl_table fs_table[];static ctl_table debug_table[];static ctl_table dev_table[];extern ctl_table random_table[];/* /proc declarations: */#ifdef CONFIG_PROC_FSstatic ssize_t proc_readsys(struct file *, char *, size_t, loff_t *);static ssize_t proc_writesys(struct file *, const char *, size_t, loff_t *);static int proc_sys_permission(struct inode *, int);struct file_operations proc_sys_file_operations = { read: proc_readsys, write: proc_writesys,};static struct inode_operations proc_sys_inode_operations = { permission: proc_sys_permission,};extern struct proc_dir_entry *proc_sys_root;static void register_proc_table(ctl_table *, struct proc_dir_entry *);static void unregister_proc_table(ctl_table *, struct proc_dir_entry *);#endif/* The default sysctl tables: */static ctl_table root_table[] = { {CTL_KERN, "kernel", NULL, 0, 0555, kern_table}, {CTL_VM, "vm", NULL, 0, 0555, vm_table},#ifdef CONFIG_NET {CTL_NET, "net", NULL, 0, 0555, net_table},#endif {CTL_PROC, "proc", NULL, 0, 0555, proc_table}, {CTL_FS, "fs", NULL, 0, 0555, fs_table}, {CTL_DEBUG, "debug", NULL, 0, 0555, debug_table}, {CTL_DEV, "dev", NULL, 0, 0555, dev_table}, {0}};static ctl_table kern_table[] = { {KERN_OSTYPE, "ostype", system_utsname.sysname, 64, 0444, NULL, &proc_doutsstring, &sysctl_string}, {KERN_OSRELEASE, "osrelease", system_utsname.release, 64, 0444, NULL, &proc_doutsstring, &sysctl_string}, {KERN_VERSION, "version", system_utsname.version, 64, 0444, NULL, &proc_doutsstring, &sysctl_string}, {KERN_NODENAME, "hostname", system_utsname.nodename, 64, 0644, NULL, &proc_doutsstring, &sysctl_string}, {KERN_DOMAINNAME, "domainname", system_utsname.domainname, 64, 0644, NULL, &proc_doutsstring, &sysctl_string}, {KERN_PANIC, "panic", &panic_timeout, sizeof(int), 0644, NULL, &proc_dointvec}, {KERN_CORE_USES_PID, "core_uses_pid", &core_uses_pid, sizeof(int), 0644, NULL, &proc_dointvec}, {KERN_TAINTED, "tainted", &tainted, sizeof(int), 0644, NULL, &proc_dointvec}, {KERN_CAP_BSET, "cap-bound", &cap_bset, sizeof(kernel_cap_t), 0600, NULL, &proc_dointvec_bset},#ifdef CONFIG_BLK_DEV_INITRD {KERN_REALROOTDEV, "real-root-dev", &real_root_dev, sizeof(int), 0644, NULL, &proc_dointvec},#endif#ifdef __sparc__ {KERN_SPARC_REBOOT, "reboot-cmd", reboot_command, 256, 0644, NULL, &proc_dostring, &sysctl_string }, {KERN_SPARC_STOP_A, "stop-a", &stop_a_enabled, sizeof (int), 0644, NULL, &proc_dointvec},#endif#ifdef CONFIG_PPC32 {KERN_PPC_ZEROPAGED, "zero-paged", &zero_paged_on, sizeof(int), 0644, NULL, &proc_dointvec}, {KERN_PPC_POWERSAVE_NAP, "powersave-nap", &powersave_nap, sizeof(int), 0644, NULL, &proc_dointvec}, {KERN_PPC_L2CR, "l2cr", NULL, 0, 0644, NULL, &proc_dol2crvec},#endif {KERN_CTLALTDEL, "ctrl-alt-del", &C_A_D, sizeof(int), 0644, NULL, &proc_dointvec}, {KERN_PRINTK, "printk", &console_loglevel, 4*sizeof(int), 0644, NULL, &proc_dointvec},#ifdef CONFIG_KMOD {KERN_MODPROBE, "modprobe", &modprobe_path, 256, 0644, NULL, &proc_dostring, &sysctl_string },#endif#ifdef CONFIG_HOTPLUG {KERN_HOTPLUG, "hotplug", &hotplug_path, 256, 0644, NULL, &proc_dostring, &sysctl_string },#endif#ifdef CONFIG_CHR_DEV_SG {KERN_SG_BIG_BUFF, "sg-big-buff", &sg_big_buff, sizeof (int), 0444, NULL, &proc_dointvec},#endif#ifdef CONFIG_BSD_PROCESS_ACCT {KERN_ACCT, "acct", &acct_parm, 3*sizeof(int), 0644, NULL, &proc_dointvec},#endif {KERN_RTSIGNR, "rtsig-nr", &nr_queued_signals, sizeof(int), 0444, NULL, &proc_dointvec}, {KERN_RTSIGMAX, "rtsig-max", &max_queued_signals, sizeof(int), 0644, NULL, &proc_dointvec},#ifdef CONFIG_SYSVIPC {KERN_SHMMAX, "shmmax", &shm_ctlmax, sizeof (size_t), 0644, NULL, &proc_doulongvec_minmax}, {KERN_SHMALL, "shmall", &shm_ctlall, sizeof (size_t), 0644, NULL, &proc_doulongvec_minmax}, {KERN_SHMMNI, "shmmni", &shm_ctlmni, sizeof (int), 0644, NULL, &proc_dointvec}, {KERN_MSGMAX, "msgmax", &msg_ctlmax, sizeof (int), 0644, NULL, &proc_dointvec}, {KERN_MSGMNI, "msgmni", &msg_ctlmni, sizeof (int), 0644, NULL, &proc_dointvec}, {KERN_MSGMNB, "msgmnb", &msg_ctlmnb, sizeof (int), 0644, NULL, &proc_dointvec}, {KERN_SEM, "sem", &sem_ctls, 4*sizeof (int), 0644, NULL, &proc_dointvec},#endif#ifdef CONFIG_MAGIC_SYSRQ {KERN_SYSRQ, "sysrq", &sysrq_enabled, sizeof (int), 0644, NULL, &proc_dointvec},#endif #ifdef CONFIG_FAIRSCHED {KERN_FAIRSCHED, "fairsched", &fairsched, sizeof(int), 0644, NULL, &proc_dointvec},#endif {KERN_CADPID, "cad_pid", &cad_pid, sizeof (int), 0600, NULL, &proc_dointvec}, {KERN_MAX_THREADS, "threads-max", &max_threads, sizeof(int), 0644, NULL, &proc_dointvec}, {KERN_RANDOM, "random", NULL, 0, 0555, random_table}, {KERN_OVERFLOWUID, "overflowuid", &overflowuid, sizeof(int), 0644, NULL, &proc_dointvec_minmax, &sysctl_intvec, NULL, &minolduid, &maxolduid}, {KERN_OVERFLOWGID, "overflowgid", &overflowgid, sizeof(int), 0644, NULL, &proc_dointvec_minmax, &sysctl_intvec, NULL, &minolduid, &maxolduid},#ifdef CONFIG_ARCH_S390#ifdef CONFIG_MATHEMU {KERN_IEEE_EMULATION_WARNINGS,"ieee_emulation_warnings", &sysctl_ieee_emulation_warnings,sizeof(int),0644,NULL,&proc_dointvec},#endif {KERN_S390_USER_DEBUG_LOGGING,"userprocess_debug", &sysctl_userprocess_debug,sizeof(int),0644,NULL,&proc_dointvec},#endif {0}};static ctl_table vm_table[] = { {VM_BDFLUSH, "bdflush", &bdf_prm, 9*sizeof(int), 0644, NULL, &proc_dointvec_minmax, &sysctl_intvec, NULL, &bdflush_min, &bdflush_max}, {VM_OVERCOMMIT_MEMORY, "overcommit_memory", &sysctl_overcommit_memory, sizeof(sysctl_overcommit_memory), 0644, NULL, &proc_dointvec}, {VM_PAGERDAEMON, "kswapd", &pager_daemon, sizeof(pager_daemon_t), 0644, NULL, &proc_dointvec}, {VM_PGT_CACHE, "pagetable_cache", &pgt_cache_water, 2*sizeof(int), 0644, NULL, &proc_dointvec}, {VM_PAGE_CLUSTER, "page-cluster", &page_cluster, sizeof(int), 0644, NULL, &proc_dointvec}, {VM_MIN_READAHEAD, "min-readahead", &vm_min_readahead,sizeof(int), 0644, NULL, &proc_dointvec}, {VM_MAX_READAHEAD, "max-readahead", &vm_max_readahead,sizeof(int), 0644, NULL, &proc_dointvec}, {0}};static ctl_table proc_table[] = { {0}};static ctl_table fs_table[] = { {FS_NRINODE, "inode-nr", &inodes_stat, 2*sizeof(int), 0444, NULL, &proc_dointvec}, {FS_STATINODE, "inode-state", &inodes_stat, 7*sizeof(int), 0444, NULL, &proc_dointvec}, {FS_NRFILE, "file-nr", &files_stat, 3*sizeof(int), 0444, NULL, &proc_dointvec}, {FS_MAXFILE, "file-max", &files_stat.max_files, sizeof(int), 0644, NULL, &proc_dointvec}, {FS_NRDQUOT, "dquot-nr", &nr_dquots, 2*sizeof(int), 0444, NULL, &proc_dointvec}, {FS_DENTRY, "dentry-state", &dentry_stat, 6*sizeof(int), 0444, NULL, &proc_dointvec}, {FS_OVERFLOWUID, "overflowuid", &fs_overflowuid, sizeof(int), 0644, NULL, &proc_dointvec_minmax, &sysctl_intvec, NULL, &minolduid, &maxolduid}, {FS_OVERFLOWGID, "overflowgid", &fs_overflowgid, sizeof(int), 0644, NULL, &proc_dointvec_minmax, &sysctl_intvec, NULL, &minolduid, &maxolduid}, {FS_LEASES, "leases-enable", &leases_enable, sizeof(int), 0644, NULL, &proc_dointvec}, {FS_DIR_NOTIFY, "dir-notify-enable", &dir_notify_enable, sizeof(int), 0644, NULL, &proc_dointvec}, {FS_LEASE_TIME, "lease-break-time", &lease_break_time, sizeof(int), 0644, NULL, &proc_dointvec}, {0}};static ctl_table debug_table[] = { {0}};static ctl_table dev_table[] = { {0}}; extern void init_irq_proc (void);void __init sysctl_init(void){#ifdef CONFIG_PROC_FS register_proc_table(root_table, proc_sys_root); init_irq_proc();#endif}int do_sysctl(int *name, int nlen, void *oldval, size_t *oldlenp, void *newval, size_t newlen){ struct list_head *tmp; if (nlen <= 0 || nlen >= CTL_MAXNAME) return -ENOTDIR; if (oldval) { int old_len; if (!oldlenp || get_user(old_len, oldlenp)) return -EFAULT; } tmp = &root_table_header.ctl_entry; do { struct ctl_table_header *head = list_entry(tmp, struct ctl_table_header, ctl_entry); void *context = NULL; int error = parse_table(name, nlen, oldval, oldlenp, newval, newlen, head->ctl_table, &context); if (context) kfree(context); if (error != -ENOTDIR) return error; tmp = tmp->next; } while (tmp != &root_table_header.ctl_entry); return -ENOTDIR;}extern asmlinkage long sys_sysctl(struct __sysctl_args *args){ struct __sysctl_args tmp; int error; if (copy_from_user(&tmp, args, sizeof(tmp))) return -EFAULT; lock_kernel(); error = do_sysctl(tmp.name, tmp.nlen, tmp.oldval, tmp.oldlenp, tmp.newval, tmp.newlen); unlock_kernel(); return error;}/* * ctl_perm does NOT grant the superuser all rights automatically, because * some sysctl variables are readonly even to root. */static int test_perm(int mode, int op){ if (!current->euid) mode >>= 6; else if (in_egroup_p(0)) mode >>= 3; if ((mode & op & 0007) == op) return 0; return -EACCES;}static inline int ctl_perm(ctl_table *table, int op){ return test_perm(table->mode, op);}static int parse_table(int *name, int nlen, void *oldval, size_t *oldlenp, void *newval, size_t newlen, ctl_table *table, void **context){ int n;repeat: if (!nlen) return -ENOTDIR; if (get_user(n, name)) return -EFAULT; for ( ; table->ctl_name; table++) { if (n == table->ctl_name || table->ctl_name == CTL_ANY) { int error; if (table->child) { if (ctl_perm(table, 001)) return -EPERM; if (table->strategy) { error = table->strategy( table, name, nlen, oldval, oldlenp, newval, newlen, context); if (error) return error; } name++; nlen--; table = table->child; goto repeat; } error = do_sysctl_strategy(table, name, nlen, oldval, oldlenp, newval, newlen, context); return error; } } return -ENOTDIR;}/* Perform the actual read/write of a sysctl table entry. */int do_sysctl_strategy (ctl_table *table, int *name, int nlen, void *oldval, size_t *oldlenp, void *newval, size_t newlen, void **context){ int op = 0, rc; size_t len; if (oldval) op |= 004; if (newval) op |= 002; if (ctl_perm(table, op)) return -EPERM; if (table->strategy) { rc = table->strategy(table, name, nlen, oldval, oldlenp, newval, newlen, context); if (rc < 0) return rc; if (rc > 0) return 0; } /* If there is no strategy routine, or if the strategy returns * zero, proceed with automatic r/w */ if (table->data && table->maxlen) { if (oldval && oldlenp) { get_user(len, oldlenp); if (len) { if (len > table->maxlen) len = table->maxlen; if(copy_to_user(oldval, table->data, len)) return -EFAULT; if(put_user(len, oldlenp)) return -EFAULT; } } if (newval && newlen) { len = newlen; if (len > table->maxlen) len = table->maxlen; if(copy_from_user(table->data, newval, len)) return -EFAULT; } } return 0;}/** * register_sysctl_table - register a sysctl heirarchy * @table: the top-level table structure * @insert_at_head: whether the entry should be inserted in front or at the end * * Register a sysctl table heirarchy. @table should be a filled in ctl_table * array. An entry with a ctl_name of 0 terminates the table. *
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -