【发布时间】:2021-12-11 05:40:38
【问题描述】:
我正在尝试检测特定位置 (myPath) 中的打开文件夹。我正在使用 strstr() 但我看到我的系统在执行此函数后总是崩溃(picture)。如果我删除检查(if(ret)),它会正常工作 这是我的代码:
struct task_struct *task_list;
struct fdtable * fdt = NULL;
unsigned int process_count = 0;
struct path files_path;
char *access_mode;
char *cwd;
int res;
char *myPath = "/home/anh/src/";
char *buf = (char *)kmalloc(GFP_KERNEL,100*sizeof(char));
char * ret;
for_each_process(task_list) {
fdt = files_fdtable(task_list->files);
int i=0;
while(fdt->fd[i] != NULL) {
files_path = fdt->fd[i]->f_path;
cwd = d_path(&files_path,buf,100*sizeof(char));
if(cwd){//Check if cwd != NULL
ret = strstr(cwd, myPath);//check seeked file path and myPath
if(ret)
{
printk(KERN_INFO "Open file with fd %d cwd: %s", i,cwd);
}
}
i++;
}
process_count++;
}
有人可以评论并支持我解决这个问题吗?为什么 strstr() 不能在 Linux 内核模块中使用,或者有其他方法可以验证字符串是否在字符串中?
最好的问候, 安赫
【问题讨论】:
-
字符串 needle 和 haystack 0 都终止了吗?
标签: c string linux-kernel filepath strstr