Created
October 4, 2020 04:49
-
-
Save sepfy/cf0377993ab1a167f08ebe73c6dc9ada to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <fcntl.h> | |
#include <sys/ioctl.h> // for open | |
#include <unistd.h> // for close | |
#define IOCTL_MISCDEV_SET 0x00 | |
#define IOCTL_MISCDEV_GET 0x01 | |
struct miscdev_data { | |
int val; | |
char data[64]; | |
}; | |
int main(void) { | |
int fd, ret; | |
char buf[128] = {0}; | |
struct miscdev_data data; | |
fd = open("/dev/misc_dev", O_RDWR); | |
if(fd < 0) | |
perror("open"); | |
sprintf(buf, "User says hello\n"); | |
write(fd, buf, 128); | |
read(fd, buf, 128); | |
printf("Read data frm kenrel: %s\n", buf); | |
memset(&data, 0, sizeof(data)); | |
data.val = 10; | |
sprintf(data.data, "User says hi"); | |
ret = ioctl(fd, IOCTL_MISCDEV_SET, &data); | |
if(ret < 0) { | |
perror("ioctl set"); | |
} | |
ret = ioctl(fd, IOCTL_MISCDEV_GET, &data); | |
if(ret < 0) { | |
perror("ioctl get"); | |
} | |
printf("Get data: miscdata_data.val = %d, miscdata_data.data = %s\n", data.val, data.data); | |
if(fd > 0) | |
close(fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment