Domanda

I'm learning how to create custom system call and going to implement the code(save.c) that takes the ptr char pointer as argument and then copy the string pointed by ptr into sys_mybuf. implement the code(load.c) that takes ptr char pointer as argument and then copy the string in sys_mybuf into the buffer pointed by ptr. So, I expected the following codes. But It seems not to work. I want char array to be used by all the kernel system call code. What should I do?

save.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 }

load.c

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

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

~

È stato utile?

Soluzione

As I guessed in my comment. The problem is simply that you don't declare the variable in the file load.c.

For a quick solution, add these following lines to load.c:

#define STRING__SIZE 501
extern char sys_mybuf[STRING__SIZE];

This tells the compiler that sys_mybuf is a global variable declared in another translation unit (e.g. source file).

What you really should do, is put this in a header file, and include that header file by all files that needs to access the global data.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top