📄 sysctl.c
字号:
return -EFAULT; if (!isspace(c)) break; left--; s++; } if (!left) break; neg = 0; len = left; if (len > TMPBUFLEN-1) len = TMPBUFLEN-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; val = simple_strtoul(p, &p, 0) * convmul / convdiv ; len = p-buf; if ((len < left) && *p && !isspace(*p)) break; if (neg) val = -val; s += len; left -= len; if(neg) continue; if ((min && val < *min) || (max && val > *max)) continue; *i = val; } else { p = buf; if (!first) *p++ = '\t'; sprintf(p, "%lu", convdiv * (*i) / convmul); 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_doulongvec_minmax(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos, unsigned long convmul, unsigned long convdiv){ return __do_proc_doulongvec_minmax(table->data, table, write, filp, buffer, lenp, ppos, convmul, convdiv);}/** * proc_doulongvec_minmax - read a vector of long 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 long) unsigned long * 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_doulongvec_minmax(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ return do_proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos, 1l, 1l);}/** * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values 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 long) unsigned long * values from/to the user buffer, treated as an ASCII string. The values * are treated as milliseconds, and converted to jiffies when they are stored. * * 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_doulongvec_ms_jiffies_minmax(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ return do_proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos, HZ, 1000l);}static int do_proc_dointvec_jiffies_conv(int *negp, unsigned long *lvalp, int *valp, int write, void *data){ if (write) { if (*lvalp > LONG_MAX / HZ) return 1; *valp = *negp ? -(*lvalp*HZ) : (*lvalp*HZ); } else { int val = *valp; unsigned long lval; if (val < 0) { *negp = -1; lval = (unsigned long)-val; } else { *negp = 0; lval = (unsigned long)val; } *lvalp = lval / HZ; } return 0;}static int do_proc_dointvec_userhz_jiffies_conv(int *negp, unsigned long *lvalp, int *valp, int write, void *data){ if (write) { if (USER_HZ < HZ && *lvalp > (LONG_MAX / HZ) * USER_HZ) return 1; *valp = clock_t_to_jiffies(*negp ? -*lvalp : *lvalp); } else { int val = *valp; unsigned long lval; if (val < 0) { *negp = -1; lval = (unsigned long)-val; } else { *negp = 0; lval = (unsigned long)val; } *lvalp = jiffies_to_clock_t(lval); } return 0;}static int do_proc_dointvec_ms_jiffies_conv(int *negp, unsigned long *lvalp, int *valp, int write, void *data){ if (write) { *valp = msecs_to_jiffies(*negp ? -*lvalp : *lvalp); } else { int val = *valp; unsigned long lval; if (val < 0) { *negp = -1; lval = (unsigned long)-val; } else { *negp = 0; lval = (unsigned long)val; } *lvalp = jiffies_to_msecs(lval); } return 0;}/** * proc_dointvec_jiffies - read a vector of integers as seconds * @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. * The values read are assumed to be in seconds, and are converted into * jiffies. * * Returns 0 on success. */int proc_dointvec_jiffies(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, do_proc_dointvec_jiffies_conv,NULL);}/** * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds * @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: pointer to the file position * * Reads/writes up to table->maxlen/sizeof(unsigned int) integer * values from/to the user buffer, treated as an ASCII string. * The values read are assumed to be in 1/USER_HZ seconds, and * are converted into jiffies. * * Returns 0 on success. */int proc_dointvec_userhz_jiffies(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, do_proc_dointvec_userhz_jiffies_conv,NULL);}/** * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds * @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 * @ppos: the current position in the file * * Reads/writes up to table->maxlen/sizeof(unsigned int) integer * values from/to the user buffer, treated as an ASCII string. * The values read are assumed to be in 1/1000 seconds, and * are converted into jiffies. * * Returns 0 on success. */int proc_dointvec_ms_jiffies(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, do_proc_dointvec_ms_jiffies_conv, NULL);}static int proc_do_cad_pid(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ struct pid *new_pid; pid_t tmp; int r; tmp = pid_nr(cad_pid); r = __do_proc_dointvec(&tmp, table, write, filp, buffer, lenp, ppos, NULL, NULL); if (r || !write) return r; new_pid = find_get_pid(tmp); if (!new_pid) return -ESRCH; put_pid(xchg(&cad_pid, new_pid)); return 0;}#else /* CONFIG_PROC_FS */int proc_dostring(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ return -ENOSYS;}int proc_dointvec(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ return -ENOSYS;}int proc_dointvec_bset(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ return -ENOSYS;}int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ return -ENOSYS;}int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ return -ENOSYS;}int proc_dointvec_userhz_jiffies(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ return -ENOSYS;}int proc_dointvec_ms_jiffies(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ return -ENOSYS;}int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ return -ENOSYS;}int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ return -ENOSYS;}#endif /* CONFIG_PROC_FS */#ifdef CONFIG_SYSCTL_SYSCALL/* * General sysctl support routines *//* The generic string strategy routine: */int sysctl_string(ctl_table *table, int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp, void __user *newval, size_t newlen){ if (!table->data || !table->maxlen) return -ENOTDIR; if (oldval && oldlenp) { size_t bufsize; if (get_user(bufsize, oldlenp)) return -EFAULT; if (bufsize) { size_t len = strlen(table->data), copied; /* This shouldn't trigger for a well-formed sysctl */ if (len > table->maxlen) len = table->maxlen; /* Copy up to a max of bufsize-1 bytes of the string */ copied = (len >= bufsize) ? bufsize - 1 : len; if (copy_to_user(oldval, table->data, copied) || put_user(0, (char __user *)(oldval + copied))) return -EFAULT; if (put_user(len, oldlenp)) return -EFAULT; } } if (newval && newlen) { size_t len = newlen; if (len > table->maxlen) len = table->maxlen; if(copy_from_user(table->data, newval, len)) return -EFAULT; if (len == table->maxlen) len--; ((char *) table->data)[len] = 0; } return 1;}/* * This function makes sure that all of the integers in the vector * are between the minimum and maximum values given in the arrays * table->extra1 and table->extra2, respectively. */int sysctl_intvec(ctl_table *table, int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp, void __user *newval, size_t newlen){ if (newval && newlen) { int __user *vec = (int __user *) newval; int *min = (int *) table->extra1; int *max = (int *) table->extra2; size_t length; int i; if (newlen % sizeof(int) != 0) return -EINVAL; if (!table->extra1 && !table->extra2) return 0; if (newlen > table->maxlen) newlen = table->maxlen; length = newlen / sizeof(int); for (i = 0; i < length; i++) { int value; if (get_user(value, vec + i)) return -EFAULT; if (min && value < min[i]) return -EINVAL; if (max && value > max[i]) return -EINVAL; } } return 0;}/* Strategy function to convert jiffies to seconds */ int sysctl_jiffies(ctl_table *table, int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp, void __user *newval, size_t newlen){ if (oldval && oldlenp) { size_t olen; if (get_user(olen, oldlenp)) return -EFAULT; if (olen) { int val; if (olen < sizeof(int)) return -EINVAL; val = *(int *)(table->data) / HZ; if (put_user(val, (int __user *)oldval)) return -EFAULT; if (put_user(sizeof(int), oldlenp)) return -EFAULT; } } if (newval && newlen) { int new; if (newlen != sizeof(int)) return -EINVAL; if (get_user(new, (int __user *)newval)) return -EFAULT; *(int *)(table->data) = new*HZ; } return 1;}/* Strategy function to convert jiffies to seconds */ int sysctl_ms_jiffies(ctl_table *table, int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp, void __user *newval, size_t newlen){ if (oldval && oldlenp) { size_t olen; if (get_user(olen, oldlenp)) return -EFAULT; if (olen) { int val; if (olen < sizeof(int)) return -EINVAL; val = jiffies_to_msecs(*(int *)(table->data)); if (put_user(val, (int __user *)oldval)) return -EFAULT; if (put_user(sizeof(int), oldlenp)) return -EFAULT; } } if (newval && newlen) { int new; if (newlen != sizeof(int)) return -EINVAL; if (get_user(new, (int __user *)newval)) return -EFAULT; *(int *)(table->data) = msecs_to_jiffies(new); } return 1;}#else /* CONFIG_SYSCTL_SYSCALL */asmlinkage long sys_sysctl(struct __sysctl_args __user *args){ static int msg_count; struct __sysctl_args tmp; int name[CTL_MAXNAME]; int i; /* Read in the sysctl name for better debug message logging */ if (copy_from_user(&tmp, args, sizeof(tmp))) return -EFAULT; if (tmp.nlen <= 0 || tmp.nlen >= CTL_MAXNAME) return -ENOTDIR; for (i = 0; i < tmp.nlen; i++) if (get_user(name[i], tmp.name + i)) return -EFAULT; /* Ignore accesses to kernel.version */ if ((tmp.nlen == 2) && (name[0] == CTL_KERN) && (name[1] == KERN_VERSION)) goto out; if (msg_count < 5) { msg_count++; printk(KERN_INFO "warning: process `%s' used the removed sysctl " "system call with ", current->comm); for (i = 0; i < tmp.nlen; i++) printk("%d.", name[i]); printk("\n"); }out: return -ENOSYS;}int sysctl_string(ctl_table *table, int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp, void __user *newval, size_t newlen){ return -ENOSYS;}int sysctl_intvec(ctl_table *table, int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp, void __user *newval, size_t newlen){ return -ENOSYS;}int sysctl_jiffies(ctl_table *table, int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp, void __user *newval, size_t newlen){ return -ENOSYS;}int sysctl_ms_jiffies(ctl_table *table, int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp, void __user *newval, size_t newlen){ return -ENOSYS;}#endif /* CONFIG_SYSCTL_SYSCALL *//* * No sense putting this after each symbol definition, twice, * exception granted :-) */EXPORT_SYMBOL(proc_dointvec);EXPORT_SYMBOL(proc_dointvec_jiffies);EXPORT_SYMBOL(proc_dointvec_minmax);EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);EXPORT_SYMBOL(proc_dointvec_ms_jiffies);EXPORT_SYMBOL(proc_dostring);EXPORT_SYMBOL(proc_doulongvec_minmax);EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);EXPORT_SYMBOL(register_sysctl_table);EXPORT_SYMBOL(sysctl_intvec);EXPORT_SYMBOL(sysctl_jiffies);EXPORT_SYMBOL(sysctl_ms_jiffies);EXPORT_SYMBOL(sysctl_string);EXPORT_SYMBOL(unregister_sysctl_table);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -