This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-05-05 00:00:00 +00:00
parent 0ef494b681
commit aea97698c3
48 changed files with 840 additions and 798 deletions

View File

@@ -0,0 +1,45 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#anonymous-inode */
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h> /* sleep */
#include <include/anonymous_inode.h>
int main(int argc, char **argv) {
char buf[1024];
int fd_ioctl, fd_ioctl_anon, ret;
size_t i, nreads;
if (argc < 2) {
puts("Usage: ./prog <ioctl-file> [<nreads>]");
return EXIT_FAILURE;
} else if (argc > 2) {
nreads = strtol(argv[2], NULL, 10);
} else {
nreads = 3;
}
fd_ioctl = open(argv[1], O_RDONLY);
if (fd_ioctl == -1) {
perror("open");
return EXIT_FAILURE;
}
ret = ioctl(fd_ioctl, LKMC_ANONYMOUS_INODE_GET_FD, &fd_ioctl_anon);
if (ret == -1) {
perror("ioctl");
return EXIT_FAILURE;
}
for (i = 0; i < nreads; ++i) {
ret = read(fd_ioctl_anon, buf, sizeof(buf));
printf("%.*s\n", ret, buf);
}
close(fd_ioctl_anon);
close(fd_ioctl);
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,66 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#ioctl */
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <include/ioctl.h>
int main(int argc, char **argv) {
char *ioctl_path;
int fd, request, arg0, arg1, arg_int, ret;
lkmc_ioctl_struct arg_struct;
if (argc < 2) {
puts("Usage: ./prog <ioctl-file> <request> [<arg>...]");
return EXIT_FAILURE;
}
ioctl_path = argv[1];
request = strtol(argv[2], NULL, 10);
if (argc > 3) {
arg0 = strtol(argv[3], NULL, 10);
}
if (argc > 4) {
arg1 = strtol(argv[4], NULL, 10);
}
fd = open(ioctl_path, O_RDONLY);
if (fd == -1) {
perror("open");
return EXIT_FAILURE;
}
switch (request)
{
case 0:
arg_int = arg0;
ret = ioctl(fd, LKMC_IOCTL_INC, &arg_int);
if (ret != -1) {
printf("%d\n", arg_int);
}
break;
case 1:
arg_struct.i = arg0;
arg_struct.j = arg1;
ret = ioctl(fd, LKMC_IOCTL_INC_DEC, &arg_struct);
if (ret != -1) {
printf("%d %d\n", arg_struct.i, arg_struct.j);
}
break;
default:
puts("error: unknown request");
return EXIT_FAILURE;
}
if (ret == -1) {
perror("ioctl");
printf("errno = %d\n", errno);
return EXIT_FAILURE;
}
close(fd);
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,51 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#netlink-sockets */
#include <linux/netlink.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <include/netlink.h>
#define MAX_PAYLOAD 1024
/* Some of these structs fields must be zeroed.
* We could brute force memset them, but
* TODO determine exactly which, and move into main. */
int sock_fd;
struct iovec iov;
struct msghdr msg;
struct nlmsghdr *nlh;
struct sockaddr_nl src_addr, dest_addr;
int main(void) {
sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_USER);
if (sock_fd < 0) {
perror("socket");
return EXIT_FAILURE;
}
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid();
bind(sock_fd, (struct sockaddr *)&src_addr, sizeof(src_addr));
dest_addr.nl_family = AF_NETLINK;
nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
nlh->nlmsg_pid = getpid();
nlh->nlmsg_flags = 0;
strcpy(NLMSG_DATA(nlh), "user request");
iov.iov_base = (void *)nlh;
iov.iov_len = nlh->nlmsg_len;
msg.msg_name = (void *)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
fprintf(stderr, "before sendmsg\n");
sendmsg(sock_fd, &msg, 0);
fprintf(stderr, "after sendmsg\n");
recvmsg(sock_fd, &msg, 0);
printf("%s\n", (char *)NLMSG_DATA(nlh));
close(sock_fd);
}

View File

@@ -0,0 +1,41 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#poll */
#define _XOPEN_SOURCE 700
#include <assert.h>
#include <fcntl.h> /* creat, O_CREAT */
#include <poll.h> /* poll */
#include <stdio.h> /* printf, puts, snprintf */
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
#include <unistd.h> /* read */
int main(int argc, char **argv) {
char buf[1024];
int fd, i, n;
short revents;
struct pollfd pfd;
if (argc < 2) {
fprintf(stderr, "usage: %s <poll-device>\n", argv[0]);
exit(EXIT_FAILURE);
}
fd = open(argv[1], O_RDONLY | O_NONBLOCK);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
pfd.fd = fd;
pfd.events = POLLIN;
while (1) {
puts("loop");
i = poll(&pfd, 1, -1);
if (i == -1) {
perror("poll");
assert(0);
}
revents = pfd.revents;
if (revents & POLLIN) {
n = read(pfd.fd, buf, sizeof(buf));
printf("POLLIN n=%d buf=%.*s\n", n, n, buf);
}
}
}

View File

@@ -0,0 +1,111 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#uio */
#if 1
/* Adapted from: https://yurovsky.github.io/2014/10/10/linux-uio-gpio-interrupt */
#define _XOPEN_SOURCE 700
#include <fcntl.h> /* open */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h> /* write */
#include <assert.h>
#include <sys/mman.h>
int main(int argc, char **argv) {
char *dev = "/dev/uio0";
if (argc > 1) {
dev = argv[1];
exit(EXIT_FAILURE);
}
int fd = open(dev, O_RDWR);
if (fd < 0) {
perror("open");
exit(EXIT_FAILURE);
}
/* TODO not supported by this kernel module? */
#if 0
int *addr = mmap(NULL, sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
perror("mmap");
assert(0);
}
*addr = 0x12345678;
#endif
while (1) {
uint32_t info = 1;
size_t nb = write(fd, &info, sizeof(info));
if (nb < sizeof(info)) {
perror("write");
close(fd);
exit(EXIT_FAILURE);
}
nb = read(fd, &info, sizeof(info));
if (nb == sizeof(info)) {
printf(__FILE__ " read = %u\n", info);
}
}
}
#else
/* Ripped from the kernel docs. */
#define _XOPEN_SOURCE 700
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <unistd.h>
int main(void) {
int uiofd;
int configfd;
int err;
int i;
unsigned icount;
unsigned char command_high;
uiofd = open("/dev/uio0", O_RDONLY);
if (uiofd < 0) {
perror("uio open:");
return errno;
}
configfd = open("/sys/class/uio/uio0/device/config", O_RDWR);
if (configfd < 0) {
perror("config open:");
return errno;
}
err = pread(configfd, &command_high, 1, 5);
if (err != 1) {
perror("command config read:");
return errno;
}
command_high &= ~0x4;
for(i = 0;; ++i) {
fprintf(stderr, "Interrupts: %d\n", icount);
err = pwrite(configfd, &command_high, 1, 5);
if (err != 1) {
perror("config write:");
break;
}
err = read(uiofd, &icount, 4);
if (err != 4) {
perror("uio read:");
break;
}
}
return errno;
}
#endif