kernel-helloworld-test
annotate ktestprogram/kprog.c @ 0:dbbd63da261f
helloworld kernel module and program that reads the /dev/ktest
author | Eleni Maria Stea <eleni@mutantstargoat.com> |
---|---|
date | Wed, 10 May 2017 13:11:31 +0300 |
parents | |
children |
rev | line source |
---|---|
eleni@0 | 1 #include <fcntl.h> |
eleni@0 | 2 #include <stdio.h> |
eleni@0 | 3 #include <unistd.h> |
eleni@0 | 4 |
eleni@0 | 5 int main(void) |
eleni@0 | 6 { |
eleni@0 | 7 char buf[512]; |
eleni@0 | 8 size_t size; |
eleni@0 | 9 int fd; |
eleni@0 | 10 |
eleni@0 | 11 if((fd = open("/dev/ktest", O_RDONLY)) == -1) { |
eleni@0 | 12 fprintf(stderr, "Failed to open device ktest.\n"); |
eleni@0 | 13 return 1; |
eleni@0 | 14 } |
eleni@0 | 15 |
eleni@0 | 16 if((size = read(fd, buf, sizeof buf)) == -1) { |
eleni@0 | 17 fprintf(stderr, "Failed to read from device ktest.\n"); |
eleni@0 | 18 |
eleni@0 | 19 close(fd); |
eleni@0 | 20 return 1; |
eleni@0 | 21 } |
eleni@0 | 22 printf("num char: %d\n", (int)size); |
eleni@0 | 23 |
eleni@0 | 24 buf[size] = 0; |
eleni@0 | 25 printf("quoting kernel: %s", buf); |
eleni@0 | 26 close(fd); |
eleni@0 | 27 return 0; |
eleni@0 | 28 } |