linux在内核中打开文件,c - 在Linux内核中读取文件 - 堆栈内存溢出

所以在我明白“你不应该那样做”之前,我知道我不应该这样做。 但这是对某些研究工作的肮脏,快速的概念证明。 如果该概念可行,我将考虑将信息正确导出到模块中。 我对这方面的知识非常少,因为我只使用过sysfs来公开我在模块中生成的值,因此朝正确方向的任何指针将不胜感激。 我做了很多谷歌搜索,不幸的是我缺席了。

我需要实现的是:我需要为运行3.10.9内核的特定Android板(Odroid XU3)读取功率传感器和GPU的设备和sysfs值。

我的模块在哪里运行:我正在编写一个cpufreq电源调节器,它试图根据系统信息创建FSM(因此需要读取文件)。

在我的州长中,我试图将文件打开为文件指针,然后将其保存并读取每个州长的滴答声。

struct file* f_mali_freq;

struct file* f_mali_load;

struct file* f_big_ina231;

struct file* f_little_ina231;

struct file* f_mem_ina231;

struct file* f_gpu_ina231;

然后,我尝试实现类似于此处和此处说明的方法。

目前,我已经尝试对传感器的文件进行以下操作:

定义和构造

#define INA231_IOCGREG _IOR('i', 1, struct ina231_iocreg *)

#define INA231_IOCSSTATUS _IOW('i', 2, struct ina231_iocreg *)

#define INA231_IOCGSTATUS _IOR('i', 3, struct ina231_iocreg *)

struct ina231_iocreg {

unsigned char name[20];

unsigned int enable;

unsigned int cur_uV;

unsigned int cur_uA;

unsigned int cur_uW;

} __attribute__ ((packed));

从我的州长初始化中调用

f_big_ina231 = open_ina231("/dev/sensor_arm");

static struct file *open_ina231(const char *path)

{

mm_segment_t old_fs = get_fs();

struct ina231_iocreg reg;

struct file *f;

int ret;

f = file_open(path);

if (!f)

return NULL; //

set_fs(KERNEL_DS);

if (!f->f_op || !f->f_op->unlocked_ioctl)

goto out_error;

/* enable if necessary */

ret = f->f_op->unlocked_ioctl(f, INA231_IOCGSTATUS, (long unsigned int) &reg);

if (ret)

goto out_error;

if (!reg.enable) {

reg.enable = true;

ret = f->f_op->unlocked_ioctl(f, INA231_IOCSSTATUS, (long unsigned int) &reg);

if (ret)

goto out_error;

}

set_fs(old_fs);

return f;

out_error:

KERNEL_DEBUG_MSG(" [OPTIGOV] open_ina231 out_error\n");

set_fs(old_fs);

filp_close(f, NULL);

return NULL;

}

static struct file* file_open(const char *path){

struct file *f = NULL;

f = filp_open(path, O_RDWR, 0);

if(IS_ERR(f)){

KERNEL_DEBUG_MSG(" [OPTIGOV] file_open failed filp_open\n");

return NULL;

}

return f;

}

打开文件总是失败,因此我唯一可以得出的结论是该文件不存在。 这样,我使用“ opened”标志将代码移到我的州长的重复任务中,这将阻止文件被重复打开。 这在无法打开文件的情况下获得了相同的结果。

我尝试了一种更简化的方法(如下),但也失败了。

f_mali_load = file_open("/sys/bus/platform/drivers/mali/11800000.mali/utilization");

static struct file* file_open(const char *path){

struct file *f = NULL;

mm_segment_t oldfs;

int err = 0;

oldfs = get_fs();

set_fs(get_ds());

f = filp_open(path, O_RDONLY, 0);

set_fs(oldfs);

if(IS_ERR(f)){

KERNEL_DEBUG_MSG(" [OPTIGOV] file_open failed filp_open\n");

return NULL; //

}

KERNEL_DEBUG_MSG(" [OPTIGOV] file_open succedded filp_open\n");

return f;

}

我希望有人可以帮助或指出正确的方向,因为我无法终生弄清自己的缺失。

无论如何,我一直无法找到测试内核中是否存在文件的方法(除了尝试打开它,这是我正在做的事情)。

干杯

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值