6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/file.h>
11 #include <linux/ioctl.h>
12 #include <linux/uaccess.h>
13 #include <linux/miscdevice.h>
15 struct fetch_fops_args {
20 #define MOCTEL_FETCH_FOPS _IOR('m', 1, struct fetch_fops_args)
22 static int ioctl_open(struct inode *nodp, struct file *filp) {
26 static const struct file_operations dummy_fops = {};
28 static long ioctl_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) {
29 struct fetch_fops_args args;
31 const struct file_operations *fops;
33 if (cmd != MOCTEL_FETCH_FOPS) return -EINVAL;
34 if (copy_from_user(&args, (struct fetch_fops_args __user *)arg, sizeof(args))) return -EINVAL;
36 if (!f.file) return -EBADF;
38 if (!fops) fops = dummy_fops;
40 if (copy_to_user((struct file_operations __user *)args.retp, fops, sizeof(*fops))) return -EINVAL;
44 static int ioctl_release(struct inode *nodp, struct file *filp) {
48 static const struct file_operations ioctl_fops = {
50 .ioctl = ioctl_ioctl, /* I have no idea about locking,
51 so just take the Big Kernel Lock implicitly. */
53 .release = ioctl_release
56 static struct miscdevice ioctl_miscdev = {
62 static int __init init_ioctl() {
64 printk(KERN_INFO "loading ioctl helper\n");
66 ret = misc_register(&ioctl_miscdev);
70 static void __exit cleanup_ioctl() {
71 printk(KERN_INFO "unloading ioctl helper\n");
72 misc_deregister(&ioctl_miscdev);
75 module_init(init_ioctl);
76 module_exit(cleanup_ioctl);