如何检查 ioctl 上的现有参数
How to check for existing argument on ioctl
我想试试如何使用i/o-controls可加载内核模块,这里是字符设备。
问题是:如何检查用户空间端的 ioctl
调用是否有参数。我发现在 ioctl
上,参数是可选的。
用户空间函数内部:
// set a parameter - this is a proper call
if( ioctl(fd, IOCTL_SET_PARAM1, 5)<0 )
{
fprintf(stderr,"Error while ioctl: %s\n", strerror(errno));
}
// call a setter without argument - this should cause an error
if( ioctl(fd, IOCTL_SET_PARAM1)<0 )
{
fprintf(stderr,"Error while ioctl: %s\n", strerror(errno));
}
对应的内核模块句柄:
long
fops_unlocked_ioctl (struct file *p_file,
unsigned int cmd,
unsigned long arg)
{
switch(cmd)
{
case IOCTL_SET_PARAM1:
printk(KERN_INFO "IOCTL called with IOCTL_SET_PARAM1\n");
if( /* how to check for the argument here? */ )
{
printk(KERN_WARNING "Missing argument\n");
return -EINVAL;
}
param1 = (unsigned short) arg;
printk(KERN_INFO "param1 set to %d\n",param1);
break;
default:
printk(KERN_WARNING "IOCTL called with wrong request code.\n");
return -EINVAL;
}
return 0;
}
此致,
亚历克斯
Jonathan Leffler 说得完全正确,用户应该知道该做什么,不该做什么。
顺便说一句。我制定了一个解决方案,用带有计数和原始参数的数组替换参数。在用户空间方面,用户没有任何变化。在内核空间方面有两个宏来获取原始参数和参数数量。
代码来了:
expdev.h
#ifndef EXPDEV_H_INCLUDED
#define EXPDEV_H_INCLUDED
#ifndef __KERNEL__
#include <stdint.h>
#endif // __KERNEL__
#include "pp_narg.h"
enum {
IOCTL_SET_PARAM1,
};
/* ioctl - wrapper *************************************************** */
#ifndef __KERNEL__
long int argW[2]={0};
long int PP_IOCTL_WRAPARG( long int narg, long int arg )
{
argW[0] = narg;
argW[1] = arg;
return (long int)(argW);
}
#define PP_IOCTL_NARG(n,fd,cmd,arg,...) \
( (n==3) ? \
( ioctl(fd,cmd,(long int)(PP_IOCTL_WRAPARG(1,(long int)arg))) ) : \
( ioctl(fd,cmd,(long int)(PP_IOCTL_WRAPARG(0,(long int)arg))) ) )
#define ioctl(...) \
(PP_IOCTL_NARG(PP_NARG(__VA_ARGS__), \
__VA_ARGS__, (long int)(0),(long int)(0)))
#else // __KERNEL__
#define IOCTL_ARGC(argW) (((long int*)argW)[0])
#define IOCTL_ARG(argW) (((long int*)argW)[1])
#endif // __KERNEL__
#endif // EXPDEV_H_INCLUDED
为了计算参数,我遵循这个 post。
pp_narg.h
/*
Source: https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s
*/
#ifndef PP_NARG_INCLUDED
#define PP_NARG_INCLUDED
#define PP_NARG(...) \
PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
#define PP_NARG_(...) \
PP_ARG_N(__VA_ARGS__)
#define PP_ARG_N( \
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
_61,_62,_63,N,...) N
#define PP_RSEQ_N() \
63,62,61,60, \
59,58,57,56,55,54,53,52,51,50, \
49,48,47,46,45,44,43,42,41,40, \
39,38,37,36,35,34,33,32,31,30, \
29,28,27,26,25,24,23,22,21,20, \
19,18,17,16,15,14,13,12,11,10, \
9,8,7,6,5,4,3,2,1,0
#endif // PP_NARG_INCLUDED
现在在用户空间函数中调用(与往常一样):
#include "expdev.h"
// set a parameter
if( ioctl(fd, IOCTL_SET_PARAM1, 5)<0 )
{
fprintf(stderr,"Error while ioctl: %s\n", strerror(errno));
}
// call a setter without argument - this should cause an error
if( ioctl(fd, IOCTL_SET_PARAM1)<0 )
{
fprintf(stderr,"Error while ioctl: %s\n", strerror(errno));
}
和内核空间方面(注意IOCTL_ARGC(arg)
和IOCTL_ARG(arg)
):
#include "expdev.h"
long
fops_unlocked_ioctl (struct file *p_file,
unsigned int cmd,
unsigned long arg)
{
switch(cmd)
{
case IOCTL_SET_PARAM1:
printk(KERN_INFO "IOCTL called with IOCTL_SET_PARAM1\n");
if( IOCTL_ARGC(arg)!=1 )
{
printk(KERN_ERR "Missing argument.\n");
return -EINVAL;
}
param1 = (unsigned short) IOCTL_ARG(arg);
printk(KERN_INFO "param1 set to %d\n",param1);
break;
default:
printk(KERN_WARNING "IOCTL called with wrong request code.\n");
return -EINVAL;
}
return 0;
}
这导致 kern.log 上的以下输出:
kernel: [18577.042438] IOCTL called with IOCTL_SET_PARAM1
kernel: [18577.042439] param1 set to 5
kernel: [18577.042442] IOCTL called with IOCTL_SET_PARAM1
kernel: [18577.042443] Missing argument.
在用户空间方面:
Error while ioctl: Invalid argument
注意:
宏重新定义ioctl
。这意味着在同一源内的不同设备上使用 ioctl
将导致不支持此宏的设备出现错误(例如,如果您使用标准 tty)。
我想试试如何使用i/o-controls可加载内核模块,这里是字符设备。
问题是:如何检查用户空间端的 ioctl
调用是否有参数。我发现在 ioctl
上,参数是可选的。
用户空间函数内部:
// set a parameter - this is a proper call
if( ioctl(fd, IOCTL_SET_PARAM1, 5)<0 )
{
fprintf(stderr,"Error while ioctl: %s\n", strerror(errno));
}
// call a setter without argument - this should cause an error
if( ioctl(fd, IOCTL_SET_PARAM1)<0 )
{
fprintf(stderr,"Error while ioctl: %s\n", strerror(errno));
}
对应的内核模块句柄:
long
fops_unlocked_ioctl (struct file *p_file,
unsigned int cmd,
unsigned long arg)
{
switch(cmd)
{
case IOCTL_SET_PARAM1:
printk(KERN_INFO "IOCTL called with IOCTL_SET_PARAM1\n");
if( /* how to check for the argument here? */ )
{
printk(KERN_WARNING "Missing argument\n");
return -EINVAL;
}
param1 = (unsigned short) arg;
printk(KERN_INFO "param1 set to %d\n",param1);
break;
default:
printk(KERN_WARNING "IOCTL called with wrong request code.\n");
return -EINVAL;
}
return 0;
}
此致, 亚历克斯
Jonathan Leffler 说得完全正确,用户应该知道该做什么,不该做什么。
顺便说一句。我制定了一个解决方案,用带有计数和原始参数的数组替换参数。在用户空间方面,用户没有任何变化。在内核空间方面有两个宏来获取原始参数和参数数量。
代码来了:
expdev.h
#ifndef EXPDEV_H_INCLUDED
#define EXPDEV_H_INCLUDED
#ifndef __KERNEL__
#include <stdint.h>
#endif // __KERNEL__
#include "pp_narg.h"
enum {
IOCTL_SET_PARAM1,
};
/* ioctl - wrapper *************************************************** */
#ifndef __KERNEL__
long int argW[2]={0};
long int PP_IOCTL_WRAPARG( long int narg, long int arg )
{
argW[0] = narg;
argW[1] = arg;
return (long int)(argW);
}
#define PP_IOCTL_NARG(n,fd,cmd,arg,...) \
( (n==3) ? \
( ioctl(fd,cmd,(long int)(PP_IOCTL_WRAPARG(1,(long int)arg))) ) : \
( ioctl(fd,cmd,(long int)(PP_IOCTL_WRAPARG(0,(long int)arg))) ) )
#define ioctl(...) \
(PP_IOCTL_NARG(PP_NARG(__VA_ARGS__), \
__VA_ARGS__, (long int)(0),(long int)(0)))
#else // __KERNEL__
#define IOCTL_ARGC(argW) (((long int*)argW)[0])
#define IOCTL_ARG(argW) (((long int*)argW)[1])
#endif // __KERNEL__
#endif // EXPDEV_H_INCLUDED
为了计算参数,我遵循这个 post。
pp_narg.h
/*
Source: https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s
*/
#ifndef PP_NARG_INCLUDED
#define PP_NARG_INCLUDED
#define PP_NARG(...) \
PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
#define PP_NARG_(...) \
PP_ARG_N(__VA_ARGS__)
#define PP_ARG_N( \
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
_61,_62,_63,N,...) N
#define PP_RSEQ_N() \
63,62,61,60, \
59,58,57,56,55,54,53,52,51,50, \
49,48,47,46,45,44,43,42,41,40, \
39,38,37,36,35,34,33,32,31,30, \
29,28,27,26,25,24,23,22,21,20, \
19,18,17,16,15,14,13,12,11,10, \
9,8,7,6,5,4,3,2,1,0
#endif // PP_NARG_INCLUDED
现在在用户空间函数中调用(与往常一样):
#include "expdev.h"
// set a parameter
if( ioctl(fd, IOCTL_SET_PARAM1, 5)<0 )
{
fprintf(stderr,"Error while ioctl: %s\n", strerror(errno));
}
// call a setter without argument - this should cause an error
if( ioctl(fd, IOCTL_SET_PARAM1)<0 )
{
fprintf(stderr,"Error while ioctl: %s\n", strerror(errno));
}
和内核空间方面(注意IOCTL_ARGC(arg)
和IOCTL_ARG(arg)
):
#include "expdev.h"
long
fops_unlocked_ioctl (struct file *p_file,
unsigned int cmd,
unsigned long arg)
{
switch(cmd)
{
case IOCTL_SET_PARAM1:
printk(KERN_INFO "IOCTL called with IOCTL_SET_PARAM1\n");
if( IOCTL_ARGC(arg)!=1 )
{
printk(KERN_ERR "Missing argument.\n");
return -EINVAL;
}
param1 = (unsigned short) IOCTL_ARG(arg);
printk(KERN_INFO "param1 set to %d\n",param1);
break;
default:
printk(KERN_WARNING "IOCTL called with wrong request code.\n");
return -EINVAL;
}
return 0;
}
这导致 kern.log 上的以下输出:
kernel: [18577.042438] IOCTL called with IOCTL_SET_PARAM1
kernel: [18577.042439] param1 set to 5
kernel: [18577.042442] IOCTL called with IOCTL_SET_PARAM1
kernel: [18577.042443] Missing argument.
在用户空间方面:
Error while ioctl: Invalid argument
注意:
宏重新定义ioctl
。这意味着在同一源内的不同设备上使用 ioctl
将导致不支持此宏的设备出现错误(例如,如果您使用标准 tty)。