From 025ba63a570a535ac0b070be52c6d5f5233a0a31 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Sun, 11 Jun 2017 22:33:01 +0100 Subject: [PATCH] ioctl begins to work --- kernel_module/ioctl.c | 15 +++++++++++++++ kernel_module/ioctl.h | 8 ++++++++ kernel_module/test/init_hello.c | 11 +++++++++++ kernel_module/test/ioctl.c | 6 ++++-- 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 kernel_module/ioctl.h create mode 100644 kernel_module/test/init_hello.c diff --git a/kernel_module/ioctl.c b/kernel_module/ioctl.c index 98b2089..fdb8344 100644 --- a/kernel_module/ioctl.c +++ b/kernel_module/ioctl.c @@ -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 diff --git a/kernel_module/ioctl.h b/kernel_module/ioctl.h new file mode 100644 index 0000000..4fad8c3 --- /dev/null +++ b/kernel_module/ioctl.h @@ -0,0 +1,8 @@ +#ifndef IOCTL_H +#define IOCTL_H + +#include + +#define IOCTL0 _IOW(123, 0, char *) + +#endif diff --git a/kernel_module/test/init_hello.c b/kernel_module/test/init_hello.c new file mode 100644 index 0000000..6c0b424 --- /dev/null +++ b/kernel_module/test/init_hello.c @@ -0,0 +1,11 @@ +/* Replacement init example for when we feel like running + * a single non-interactive single executable Linux distro. */ + +#include +#include + +int main(void) { + puts("hello world"); + while (1) + sleep(0xFFFFFFFF); +} diff --git a/kernel_module/test/ioctl.c b/kernel_module/test/ioctl.c index b90683f..34fddb0 100644 --- a/kernel_module/test/ioctl.c +++ b/kernel_module/test/ioctl.c @@ -1,4 +1,5 @@ #define _GNU_SOURCE +#include #include #include #include @@ -6,7 +7,8 @@ #include #include #include -#include + +#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;