public
Created

  • Download Gist
ioctl_module.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
/*
* ioctl test module --.
*/
 
#include <linux/module.h>
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/errno.h> /* error codes */
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
#include <linux/tty.h>
#include <linux/sched.h>
 
#include <linux/init.h>
 
MODULE_LICENSE("GPL");
 
/* attribute structures */
struct ioctl_test_t {
char field1;
char field2;
};
 
#define IOCTL_TEST _IOWR(0, 6, struct ioctl_test_t)
 
static int pseudo_device_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg);
 
//static struct file_operations pseudo_dev_proc_operations = {
// .ioctl = my_printk,
//};
static struct file_operations pseudo_dev_proc_operations;
 
static struct proc_dir_entry *proc_entry;
/* 'printk' version that prints to active tty. */
void my_printk(char *string)
{
struct tty_struct *my_tty;
 
my_tty = current->signal->tty;
 
if (my_tty != NULL) {
(*my_tty->driver->ops->write)(my_tty, string, strlen(string));
(*my_tty->driver->ops->write)(my_tty, "\015\012", 2);
}
}
 
static int __init initialization_routine(void) {
printk("<1> Loading module\n");
//printk("pseudo_dev_proc_operations = %d\n", pseudo_dev_proc_operations);
pseudo_dev_proc_operations.unlocked_ioctl = pseudo_device_ioctl;
pseudo_dev_proc_operations.compat_ioctl = pseudo_device_ioctl;
 
/* Start create proc entry */
proc_entry = create_proc_entry("ioctl_test", 0666, NULL);
if(!proc_entry)
{
printk("<1> Error creating /proc entry.\n");
return 1;
}
 
//proc_entry->owner = THIS_MODULE; <-- This is now deprecated
proc_entry->proc_fops = &pseudo_dev_proc_operations;
//proc_entry->proc_fops->write = my_printk;
 
return 0;
}
 
static void __exit cleanup_routine(void) {
 
printk("<1> Dumping module\n");
remove_proc_entry("ioctl_test", NULL);
 
return;
}
 
 
/***
* ioctl() entry point...
*/
static int pseudo_device_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg)
{
struct ioctl_test_t ioc;
printk("pseudo_device_ioctl\n");
switch (cmd){
 
case IOCTL_TEST:
copy_from_user(&ioc, (struct ioctl_test_t *)arg,
sizeof(struct ioctl_test_t));
printk("<1> ioctl: call to IOCTL_TEST (%d,%c)!\n",
ioc.field1, ioc.field2);
 
my_printk ("Got msg in kernel\n");
break;
default:
return -EINVAL;
break;
}
return 0;
}
 
module_init(initialization_routine);
module_exit(cleanup_routine);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.