You must add two arguments to your syscall: the buffer for writing and its size. Then you can use snprintf() to print any line you want. You just need to make sure that you are using the correct syscall definition macro. Since you need 2 arguments, we need SYSCALL_DEFINE2 here:
#include <linux/kernel.h> /* For snprintf() */ #include <sys/syscall.h> /* For SYSCALL_DEFINE* macros */ SYSCALL_DEFINE2(sys_helloworld, char *, buff, size_t, buff_sz) { snprintf(buff, buff_sz, "Hello world\n"); return 0; }
For completeness and depending on the context, you can change the return value to something that lets you know if the string has been truncated or not.
Custom code can call it like this:
#include <stdio.h> #include <linux/kernel.h> #include <sys/syscall.h> #include <unistd.h> int main() { char buf[32]; long int a = syscall(314, buf, sizeof(buf)); printf("System call sys_helloworld returned %ld\n", a); printf("buf = %s\n", buf); return 0; }
Note that it is usually better to use the SYSCALL_DEFINE* macros to define your syscall, instead of manually entering the text asmlinkage long .... , even for syscall with no arguments (you would use SYSCALL_DEFINE0 ). These macros are defined in include/sys/syscall.h and you should use them.
Filipe gonรงalves
source share