2

我正在学习如何创建自定义系统调用并实现将 ptr char 指针作为参数的代码(save.c),然后将指向的字符串复制ptrsys_mybuf. 实现将ptrchar 指针作为参数的代码(load.c),然后将字符串复制sys_mybufptr. 所以,我期待以下代码。但这似乎不起作用。我希望所有内核系统调用代码都使用 char 数组。我应该怎么办?

保存.c

  1 #include <linux/kernel.h>
  2 #define STRING__SIZE 501
  3 char sys_mybuf[STRING__SIZE]; // a string of at most size 500.
  4 asmlinkage int sys_save(char* ptr)
  5 {
  6     int index = 0;

 17 
 18     ptr[index] = '\0';
 19     return index; // the number of bytes actually read.
 20 }

加载.c

  1 #include <linux/kernel.h>
  2 // extern
  3 asmlinkage int sys_load(char* ptr)

 17     ptr[index] = '\0';
 18     return index;
 19 }

~

4

1 回答 1

3

正如我在评论中猜测的那样。问题只是您没有在文件中声明变量load.c

要获得快速解决方案,请将以下几行添加到load.c

#define STRING__SIZE 501
extern char sys_mybuf[STRING__SIZE];

这告诉编译器这sys_mybuf是在另一个翻译单元(例如源文件)中声明的全局变量。

真正应该做的是将其放入头文件中,并将该头文件包含在需要访问全局数据的所有文件中。

于 2013-10-29T13:00:31.397 回答