You could leverage the _KERNEL_ macro.
#ifdef __KERNEL__
#include <linux/ioctl.h>
#else
#include <sys/ioctl.h>
#endif
You may have to abstract the actual ioctl values too, but you get the idea.
Question
Is there is a common practice for userspace programs to include ioctl
codes used in a kernel module.
mydev.h:
#ifndef MYDEV_H
#define MYDEV_H
#define <linux/ioctl.h>
#define MYDEV_IOC_MAGIC 'C'
#define MYDEV_IOC_FOO _IO(MYDEV_IOC_MAGIC, 0)
#define MYDEV_IOC_BAR _IOW(MYDEV_IOC_MAGIC, 1, int)
#endif
I typically put my ioctl
codes in a header which I include in my kernel module code. I considered just including this header in my userspace applications, but I realized that the linux/ioctl.h file path may not exist on most systems (e.g. systems with no exported kernel headers).
The solution seems to be to change the include line to: #include <sys/ioctl.h>
; but then I couldn't use this header for my kernel module.
Is there a better solution to this problem, or is it common to have two separate but nearly identical header files?
Solution
You could leverage the _KERNEL_ macro.
#ifdef __KERNEL__
#include <linux/ioctl.h>
#else
#include <sys/ioctl.h>
#endif
You may have to abstract the actual ioctl values too, but you get the idea.