كود PHP:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
static dev_t major;
static int test_ret;
static int param_major = 0 , param_minor = 0;
static struct cdev device_struct;
module_param(param_major , int , 0);
module_param(param_minor , int , 0);
/*static int storm_read(struct file *filp , */
static int device_open(struct inode *node , struct file *filp)
{
printk(KERN_ALERT"Device is opend\n");
}
static ssize_t device_read(struct file *filp , char __user *buf , size_t count , loff_t *off)
{
char *required;
if(count > 35)
count = 35;
required = kmalloc(count *sizeof(char) , GFP_KERNEL);
if(!required)
return ENOMEM;
memcpy(required , "Character device written by St0rM" , count);
if(copy_to_user(buf , required , count))
return EFAULT;
kfree(required);
return count;
}
static int init_device(void)
{
struct file_operation ops =
{
.owner = THIS_MODULE; /* Owner*/
.read = device_read; /* Device opned for read */
.open = device_open; /* Device opened */
};
if(param_major && param_minor)
{
major = MKDEV(param_major , param_minor);
test_ret = register_chrdev_region(majot , 1 , "storm");
}else
{
test_ret = alloc_chrdev_region(&major , 0 , 1 , "/dev/storm");
}
if(test_ret < 0)
{
test_ret = 2; /* Device number allocation error*/
return EFAULT; /* Uptill now nothing we should worry about */
}
printk(KERN_ALERT"Driver Major is ready\n");
cdev_init(&device_struct , &ops);
device_struct.owner = THIS_MODULE;
/* Here we should make sure that every thing is allright */
test_ret = cdev_add(&device_struct , dev , 1);
if(test_ret < 0)
{
exit_device();
test_ret = 3; /* device adding error */
return EFAULT;
}
}
static void exit_device(void)
{
pritnk(KERN_ALERT"cleaning up\n");
unregister_chrdev_region(major , 1);
if(test_ret == 3) /* device error , no need to remove it */
return;
cdev_del(device_struct);
}
module_init(init_device);
module_exit(exit_device);
MODULE_AUTHOR("St0rM-MaN");
MODULE_DESCRIPTION("Simple character device module sending out a message");
MODULE_DEVICE("/dev/storm");
MODULE_LICENSE("Gpl");
المهم يعني اهم حاجه هو ال device دوت وظيفته مش اكتر من انه
بيعمل driver جديد ويعمله register in kernel internal devices
وكل علي عليك بس انك تعمل
mknod /dev/storm c [major][minor]
بعد ماتعمل كده جرب مثلا تعمل cat /dev/storm مثلا
او تكتب برنامج صغير يفتح ال device ويقرا منه
كود PHP:
int fd = open("/dev/storm" , O_RDONLY);
read(fd , string , 20);
اااااه قبل مانسي


مش يجيلي واحد يقولي عملي crash في الكيرنيل بتاعي انا قلت اهوه

striker ياريت تكون اول واحد نجربه


all this codes and topics is not for a real life use right now , until a good implementation is produced
codes are only for educational purpose
تعليق