📄 sysctl.c
字号:
if (sysctl_perm(table, 001)) return -EPERM; name++; nlen--; table = table->child; goto repeat; } error = do_sysctl_strategy(table, name, nlen, oldval, oldlenp, newval, newlen); return error; } } return -ENOTDIR;}/* Perform the actual read/write of a sysctl table entry. */int do_sysctl_strategy (ctl_table *table, int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp, void __user *newval, size_t newlen){ int op = 0, rc; size_t len; if (oldval) op |= 004; if (newval) op |= 002; if (sysctl_perm(table, op)) return -EPERM; if (table->strategy) { rc = table->strategy(table, name, nlen, oldval, oldlenp, newval, newlen); 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) { if (get_user(len, oldlenp)) return -EFAULT; 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;}#endif /* CONFIG_SYSCTL_SYSCALL */static void sysctl_set_parent(struct ctl_table *parent, struct ctl_table *table){ for (; table->ctl_name || table->procname; table++) { table->parent = parent; if (table->child) sysctl_set_parent(table, table->child); }}static __init int sysctl_init(void){ sysctl_set_parent(NULL, root_table); return 0;}core_initcall(sysctl_init);/** * register_sysctl_table - register a sysctl hierarchy * @table: the top-level table structure * * Register a sysctl table hierarchy. @table should be a filled in ctl_table * array. An entry with a ctl_name of 0 terminates the table. * * The members of the &ctl_table structure are used as follows: * * ctl_name - This is the numeric sysctl value used by sysctl(2). The number * must be unique within that level of sysctl * * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not * enter a sysctl file * * data - a pointer to data for use by proc_handler * * maxlen - the maximum size in bytes of the data * * mode - the file permissions for the /proc/sys file, and for sysctl(2) * * child - a pointer to the child sysctl table if this entry is a directory, or * %NULL. * * proc_handler - the text handler routine (described below) * * strategy - the strategy routine (described below) * * de - for internal use by the sysctl routines * * extra1, extra2 - extra pointers usable by the proc handler routines * * Leaf nodes in the sysctl tree will be represented by a single file * under /proc; non-leaf nodes will be represented by directories. * * sysctl(2) can automatically manage read and write requests through * the sysctl table. The data and maxlen fields of the ctl_table * struct enable minimal validation of the values being written to be * performed, and the mode field allows minimal authentication. * * More sophisticated management can be enabled by the provision of a * strategy routine with the table entry. This will be called before * any automatic read or write of the data is performed. * * The strategy routine may return * * < 0 - Error occurred (error is passed to user process) * * 0 - OK - proceed with automatic read or write. * * > 0 - OK - read or write has been done by the strategy routine, so * return immediately. * * There must be a proc_handler routine for any terminal nodes * mirrored under /proc/sys (non-terminals are handled by a built-in * directory handler). Several default handlers are available to * cover common cases - * * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(), * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax() * * It is the handler's job to read the input buffer from user memory * and process it. The handler should return 0 on success. * * This routine returns %NULL on a failure to register, and a pointer * to the table header on success. */struct ctl_table_header *register_sysctl_table(ctl_table * table){ struct ctl_table_header *tmp; tmp = kmalloc(sizeof(struct ctl_table_header), GFP_KERNEL); if (!tmp) return NULL; tmp->ctl_table = table; INIT_LIST_HEAD(&tmp->ctl_entry); tmp->used = 0; tmp->unregistering = NULL; sysctl_set_parent(NULL, table); spin_lock(&sysctl_lock); list_add_tail(&tmp->ctl_entry, &root_table_header.ctl_entry); spin_unlock(&sysctl_lock); return tmp;}/** * unregister_sysctl_table - unregister a sysctl table hierarchy * @header: the header returned from register_sysctl_table * * Unregisters the sysctl table and all children. proc entries may not * actually be removed until they are no longer used by anyone. */void unregister_sysctl_table(struct ctl_table_header * header){ might_sleep(); spin_lock(&sysctl_lock); start_unregistering(header); spin_unlock(&sysctl_lock); kfree(header);}#else /* !CONFIG_SYSCTL */struct ctl_table_header *register_sysctl_table(ctl_table * table){ return NULL;}void unregister_sysctl_table(struct ctl_table_header * table){}#endif /* CONFIG_SYSCTL *//* * /proc/sys support */#ifdef CONFIG_PROC_SYSCTLstatic int _proc_do_string(void* data, int maxlen, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ size_t len; char __user *p; char c; if (!data || !maxlen || !*lenp) { *lenp = 0; return 0; } if (write) { len = 0; p = buffer; while (len < *lenp) { if (get_user(c, p++)) return -EFAULT; if (c == 0 || c == '\n') break; len++; } if (len >= maxlen) len = maxlen-1; if(copy_from_user(data, buffer, len)) return -EFAULT; ((char *) data)[len] = 0; *ppos += *lenp; } else { len = strlen(data); if (len > maxlen) len = maxlen; if (*ppos > len) { *lenp = 0; return 0; } data += *ppos; len -= *ppos; if (len > *lenp) len = *lenp; if (len) if(copy_to_user(buffer, data, len)) return -EFAULT; if (len < *lenp) { if(put_user('\n', ((char __user *) buffer) + len)) return -EFAULT; len++; } *lenp = len; *ppos += len; } return 0;}/** * proc_dostring - read a string sysctl * @table: the sysctl table * @write: %TRUE if this is a write to the sysctl file * @filp: the file structure * @buffer: the user buffer * @lenp: the size of the user buffer * @ppos: file position * * Reads/writes a string from/to the user buffer. If the kernel * buffer provided is not large enough to hold the string, the * string is truncated. The copied string is %NULL-terminated. * If the string is being read by the user process, it is copied * and a newline '\n' is added. It is truncated if the buffer is * not large enough. * * Returns 0 on success. */int proc_dostring(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ return _proc_do_string(table->data, table->maxlen, write, filp, buffer, lenp, ppos);}static int do_proc_dointvec_conv(int *negp, unsigned long *lvalp, int *valp, int write, void *data){ if (write) { *valp = *negp ? -*lvalp : *lvalp; } else { int val = *valp; if (val < 0) { *negp = -1; *lvalp = (unsigned long)-val; } else { *negp = 0; *lvalp = (unsigned long)val; } } return 0;}static int __do_proc_dointvec(void *tbl_data, ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos, int (*conv)(int *negp, unsigned long *lvalp, int *valp, int write, void *data), void *data){#define TMPBUFLEN 21 int *i, vleft, first=1, neg, val; unsigned long lval; size_t left, len; char buf[TMPBUFLEN], *p; char __user *s = buffer; if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) { *lenp = 0; return 0; } i = (int *) tbl_data; vleft = table->maxlen / sizeof(*i); left = *lenp; if (!conv) conv = do_proc_dointvec_conv; for (; left && vleft--; i++, first=0) { if (write) { while (left) { char c; if (get_user(c, s)) return -EFAULT; if (!isspace(c)) break; left--; s++; } if (!left) break; neg = 0; len = left; if (len > sizeof(buf) - 1) len = sizeof(buf) - 1; if (copy_from_user(buf, s, len)) return -EFAULT; buf[len] = 0; p = buf; if (*p == '-' && left > 1) { neg = 1; p++; } if (*p < '0' || *p > '9') break; lval = simple_strtoul(p, &p, 0); len = p-buf; if ((len < left) && *p && !isspace(*p)) break; if (neg) val = -val; s += len; left -= len; if (conv(&neg, &lval, i, 1, data)) break; } else { p = buf; if (!first) *p++ = '\t'; if (conv(&neg, &lval, i, 0, data)) break; sprintf(p, "%s%lu", neg ? "-" : "", lval); len = strlen(buf); if (len > left) len = left; if(copy_to_user(s, buf, len)) return -EFAULT; left -= len; s += len; } } if (!write && !first && left) { if(put_user('\n', s)) return -EFAULT; left--, s++; } if (write) { while (left) { char c; if (get_user(c, s++)) return -EFAULT; if (!isspace(c)) break; left--; } } if (write && first) return -EINVAL; *lenp -= left; *ppos += *lenp; return 0;#undef TMPBUFLEN}static int do_proc_dointvec(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos, int (*conv)(int *negp, unsigned long *lvalp, int *valp, int write, void *data), void *data){ return __do_proc_dointvec(table->data, table, write, filp, buffer, lenp, ppos, conv, data);}/** * proc_dointvec - read a vector of integers * @table: the sysctl table * @write: %TRUE if this is a write to the sysctl file * @filp: the file structure * @buffer: the user buffer * @lenp: the size of the user buffer * @ppos: file position * * Reads/writes up to table->maxlen/sizeof(unsigned int) integer * values from/to the user buffer, treated as an ASCII string. * * Returns 0 on success. */int proc_dointvec(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ return do_proc_dointvec(table,write,filp,buffer,lenp,ppos, NULL,NULL);}#define OP_SET 0#define OP_AND 1#define OP_OR 2static int do_proc_dointvec_bset_conv(int *negp, unsigned long *lvalp, int *valp, int write, void *data){ int op = *(int *)data; if (write) { int val = *negp ? -*lvalp : *lvalp; switch(op) { case OP_SET: *valp = val; break; case OP_AND: *valp &= val; break; case OP_OR: *valp |= val; break; } } else { int val = *valp; if (val < 0) { *negp = -1; *lvalp = (unsigned long)-val; } else { *negp = 0; *lvalp = (unsigned long)val; } } return 0;}/* * init may raise the set. */ int proc_dointvec_bset(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ int op; if (write && !capable(CAP_SYS_MODULE)) { return -EPERM; } op = is_init(current) ? OP_SET : OP_AND; return do_proc_dointvec(table,write,filp,buffer,lenp,ppos, do_proc_dointvec_bset_conv,&op);}/* * Taint values can only be increased */static int proc_dointvec_taint(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ int op; if (write && !capable(CAP_SYS_ADMIN)) return -EPERM; op = OP_OR; return do_proc_dointvec(table,write,filp,buffer,lenp,ppos, do_proc_dointvec_bset_conv,&op);}struct do_proc_dointvec_minmax_conv_param { int *min; int *max;};static int do_proc_dointvec_minmax_conv(int *negp, unsigned long *lvalp, int *valp, int write, void *data){ struct do_proc_dointvec_minmax_conv_param *param = data; if (write) { int val = *negp ? -*lvalp : *lvalp; if ((param->min && *param->min > val) || (param->max && *param->max < val)) return -EINVAL; *valp = val; } else { int val = *valp; if (val < 0) { *negp = -1; *lvalp = (unsigned long)-val; } else { *negp = 0; *lvalp = (unsigned long)val; } } return 0;}/** * proc_dointvec_minmax - read a vector of integers with min/max values * @table: the sysctl table * @write: %TRUE if this is a write to the sysctl file * @filp: the file structure * @buffer: the user buffer * @lenp: the size of the user buffer * @ppos: file position * * Reads/writes up to table->maxlen/sizeof(unsigned int) integer * values from/to the user buffer, treated as an ASCII string. * * This routine will ensure the values are within the range specified by * table->extra1 (min) and table->extra2 (max). * * Returns 0 on success. */int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ struct do_proc_dointvec_minmax_conv_param param = { .min = (int *) table->extra1, .max = (int *) table->extra2, }; return do_proc_dointvec(table, write, filp, buffer, lenp, ppos, do_proc_dointvec_minmax_conv, ¶m);}static int __do_proc_doulongvec_minmax(void *data, ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos, unsigned long convmul, unsigned long convdiv){#define TMPBUFLEN 21 unsigned long *i, *min, *max, val; int vleft, first=1, neg; size_t len, left; char buf[TMPBUFLEN], *p; char __user *s = buffer; if (!data || !table->maxlen || !*lenp || (*ppos && !write)) { *lenp = 0; return 0; } i = (unsigned long *) data; min = (unsigned long *) table->extra1; max = (unsigned long *) table->extra2; vleft = table->maxlen / sizeof(unsigned long); left = *lenp; for (; left && vleft--; i++, min++, max++, first=0) { if (write) { while (left) { char c; if (get_user(c, s))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -