⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sysctl.c

📁 Kernel code of linux kernel
💻 C
📖 第 1 页 / 共 5 页
字号:
				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(struct 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(struct 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;}/* *	Taint values can only be increased */static int proc_dointvec_taint(struct 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(struct 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, &param);}static int __do_proc_doulongvec_minmax(void *data, struct 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))					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(struct 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(struct 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(struct 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(struct 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(struct 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(struct 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(struct 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_vnr(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(struct ctl_table *table, int write, struct file *filp,		  void __user *buffer, size_t *lenp, loff_t *ppos){	return -ENOSYS;}int proc_dointvec(struct ctl_table *table, int write, struct file *filp,		  void __user *buffer, size_t *lenp, loff_t *ppos){	return -ENOSYS;}int proc_dointvec_minmax(struct ctl_table *table, int write, struct file *filp,		    void __user *buffer, size_t *lenp, loff_t *ppos){	return -ENOSYS;}int proc_dointvec_jiffies(struct ctl_table *table, int write, struct file *filp,		    void __user *buffer, size_t *lenp, loff_t *ppos){	return -ENOSYS;}int proc_dointvec_userhz_jiffies(struct ctl_table *table, int write, struct file *filp,		    void __user *buffer, size_t *lenp, loff_t *ppos){	return -ENOSYS;}int proc_dointvec_ms_jiffies(struct ctl_table *table, int write, struct file *filp,			     void __user *buffer, size_t *lenp, loff_t *ppos){	return -ENOSYS;}int proc_doulongvec_minmax(struct 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(struct ctl_table *table, int write,				      

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -