免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3596 | 回复: 6
打印 上一主题 下一主题

module_param 的第三个参数应如何理解? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-03-03 17:50 |只看该作者 |倒序浏览
The module_param() macro takes 3 arguments: the name of the variable, its type and permissions for the corresponding file in sysfs.


例子

  1. static short int myshort = 1;

  2. ......
  3. /*
  4. * module_param(foo, int, 0000)
  5. * The first param is the parameters name
  6. * The second param is it's data type
  7. * [color=Red]The final argument is the permissions bits[/color],
  8. * for exposing parameters in sysfs (if non-zero) at a later stage.
  9. */

  10. module_param(myshort, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  11. ......
复制代码
我不解的是,命令行传递的参数为什么非得跟访问许可联系起来? 而且我测试了一下,上面的代码好像并不直接影响 myshort 的值。

论坛徽章:
0
2 [报告]
发表于 2010-03-03 18:19 |只看该作者
perm指定了在sysfs中相应文件的访问权限,访问权限如同linux文件访问权限

如果是
module_param(num,int,S_IRUGO);

就可以cat /sys/module/hello/parameters/num,也就是只能读不能写

论坛徽章:
0
3 [报告]
发表于 2010-03-03 18:27 |只看该作者
明白! {:3_190:}

谢谢。

论坛徽章:
0
4 [报告]
发表于 2010-03-03 19:19 |只看该作者
本帖最后由 retuor 于 2010-03-03 19:23 编辑

又有问题了呀。

$ ls -l /sys/module/hello_6/parameters/
total 0
--w--w---- 1 root root 4096 Mar  3 19:22 x


$ sudo echo "123" > /sys/module/hello_6/parameters/x
-bash: /sys/module/hello_6/parameters/x: Permission denied

改不了它的值。

如果想用 S_IWOTH,则编译会出错
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

static int x;

module_param(x, int, S_IWGRP | S_IWUSR |S_IWOTH);

static int __init hello_5_init (void)
{

  printk(KERN_INFO "Loadding......");
  printk(KERN_INFO "x is an integer: %d\n", x);

  return 0;

}

static void __exit hello_5_exit (void)
{
  printk ("<1>" "Exiting......");
  printk ("<1>" "%d\n" , x);

}

module_init(hello_5_init);
module_exit(hello_5_exit);
error: size of array 'type name' is negative


没有红色部分就可以编译通过。

论坛徽章:
0
5 [报告]
发表于 2010-03-04 12:37 |只看该作者
使用S_IRUGO|S_IWUSR

论坛徽章:
0
6 [报告]
发表于 2010-03-04 12:38 |只看该作者
还有玩内核建议你使用root  or  su -

论坛徽章:
0
7 [报告]
发表于 2010-03-04 19:03 |只看该作者
恩,我起初还以为那个权限对 root 无效呢。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP