mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 10:15:57 +01:00
ioctl begins to work
This commit is contained in:
@@ -1,5 +1,20 @@
|
||||
/*
|
||||
Give integer and a pointer to the kernel, and get one positive integer out.
|
||||
|
||||
Feels like an archaic API... so many weird restrictions for something that could be so simple!
|
||||
|
||||
TODO: must the ioctl numbers be globally unique? How to ensure that?
|
||||
|
||||
Ioctl is super picky about the ioctl numbers, it is very annoying: https://stackoverflow.com/questions/10071296/ioctl-is-not-called-if-cmd-2
|
||||
|
||||
See how do_vfs_ioctl highjacks several values. This "forces" use to use the _IOx macros...
|
||||
|
||||
Documentation/ioctl/ioctl-number.txt has some info:
|
||||
|
||||
_IO an ioctl with no parameters
|
||||
_IOW an ioctl with write parameters (copy_from_user)
|
||||
_IOR an ioctl with read parameters (copy_to_user)
|
||||
_IOWR an ioctl with both write and read parameters.
|
||||
*/
|
||||
|
||||
#include <linux/debugfs.h>
|
||||
|
||||
8
kernel_module/ioctl.h
Normal file
8
kernel_module/ioctl.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef IOCTL_H
|
||||
#define IOCTL_H
|
||||
|
||||
#include <linux/ioctl.h>
|
||||
|
||||
#define IOCTL0 _IOW(123, 0, char *)
|
||||
|
||||
#endif
|
||||
11
kernel_module/test/init_hello.c
Normal file
11
kernel_module/test/init_hello.c
Normal file
@@ -0,0 +1,11 @@
|
||||
/* Replacement init example for when we feel like running
|
||||
* a single non-interactive single executable Linux distro. */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void) {
|
||||
puts("hello world");
|
||||
while (1)
|
||||
sleep(0xFFFFFFFF);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -6,7 +7,8 @@
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "../ioctl.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
@@ -21,7 +23,7 @@ int main(int argc, char **argv)
|
||||
perror("open");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
ret = ioctl(fd, strtoul(argv[2], NULL, 10), argv[3]);
|
||||
ret = ioctl(fd, IOCTL0, argv[3]);
|
||||
if (ret == -1) {
|
||||
perror("ioctl");
|
||||
return EXIT_FAILURE;
|
||||
|
||||
Reference in New Issue
Block a user