#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> #include…
- User
#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/cdev.h> #include <linux/uaccess.h>
#define DEVICE_NAME "indeks_device" #define CLASS_NAME "indeks"
MODULE_LICENSE("GPL"); MODULE_AUTHOR("Michał Zwoliński"); MODULE_DESCRIPTION(""); MODULE_VERSION("0.1");
static int majorNumber; static char message[256] = {0}; static short size_of_message; static struct class *indeksClass = NULL; static struct cdev indeksCdev;
static int dev_open(struct inode *, struct file *); static int dev_release(struct inode *, struct file *); static ssize_t dev_read(struct file *, char *, size_t, loff_t *); static ssize_t dev_write(struct file *, const char *, size_t, loff_t *);
static struct file_operations fops = { .open = dev_open, .read = dev_read, .write = dev_write, .release = dev_release, };
static int __init indeks_init(void) { printk(KERN_INFO "Indeks:\n");
majorNumber = register_chrdev(0, DEVICE_NAME, &fops); if (majorNumber < 0) { printk(KERN_ALERT "n\n"); return majorNumber; }
indeksClass = class_create(THIS_MODULE, CLASS_NAME); if (IS_ERR(indeksClass)) { unregister_chrdev(majorNumber, DEVICE_NAME); printk(KERN_ALERT "n\n"); return PTR_ERR(indeksClass); }
device_create(indeksClass, NULL, MKDEV(majorNumber, 0), NULL, DEVICE_NAME); printk(KERN_INFO "g\n"); return 0; }
static void __exit indeks_exit(void) { device_destroy(indeksClass, MKDEV(majorNumber, 0)); class_unregister(indeksClass); class_destroy(indeksClass); unregister_chrdev(majorNumber, DEVICE_NAME); printk(KERN_INFO "pa\n"); }
static int dev_open(struct inode *inodep, struct file *filep) { return 0; }
static ssize_t dev_read(struct file *filep, char *buffer, size_t len, loff_t *offset) { int error_count = 0; error_count = copy_to_user(buffer, message, size_of_message);
return error_count == 0 ? (size_of_message = 0) : -EFAULT; }
static ssize_t dev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset) { snprintf(message, sizeof(message), "%s(%zu liter)", buffer, len); size_of_message = strlen(message);
if (strncmp(buffer, "michal zwolinski", len) == 0) { strcpy(message, "56149"); } else { strcpy(message, "Błędne imię lub nazwisko"); } size_of_message = strlen(message); return len; }
static int dev_release(struct inode *inodep, struct file *filep) { return 0; }
module_init(indeks_init); module_exit(indeks_exit); how to compile this
- Assistant