Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/security.h>
- #include <linux/uaccess.h> /* copy_from/to_user */
- #include <linux/fs.h>
- #define FILE_NAME "mojaNazwa"
- MODULE_LICENSE("GPL"); // Licencja, bez zalaczenia moga wystapic bledy kompilacji
- MODULE_AUTHOR("autor"); // autor widoczne w modinfo
- MODULE_DESCRIPTION("zad3"); // opis modulu - modinfo
- MODULE_VERSION("0.1"); //wersja
- static int f_open(struct inode *, struct file *);
- static int f_release(struct inode *, struct file *);
- static ssize_t f_read(struct file *, char *, size_t , loff_t *);
- static ssize_t f_write(struct file *, const char *, size_t , loff_t *);
- static void cleanup(void);
- static int init_start(void);
- static int result;
- static int Device_Open = 0;
- static char msg[255] = {0};
- static char msg_in[240] = {0};
- static char *msg_Ptr;
- static int size_of_message;
- static struct file_operations fops = {
- .read = f_read,
- .write = f_write,
- .open = f_open,
- .release = f_release
- };
- static int __init init_start(void)
- {
- printk(KERN_INFO "Ladowanie modulu\n");
- result = register_chrdev(0, FILE_NAME, &fops);
- if (result < 0) {
- printk(KERN_ALERT "Niepowodzenie w rejestracji urzadzenia %d\n", result);
- return result;
- }
- printk(KERN_INFO "Moj major number to : %d\n", result);
- printk(KERN_INFO "Moj plik nalezy stworzyc w katalogu /dev \n");
- printk(KERN_INFO "'mknod -m 777 /dev/%s c %d 0'.\n", FILE_NAME, result);
- printk(KERN_INFO "Usun plik po zamknieciu modulu.\n");
- return 0;
- }
- static void __exit cleanup(void)
- {
- printk(KERN_INFO "Odlacznie modulu");
- unregister_chrdev(result, FILE_NAME);
- }
- static int f_open(struct inode *inode, struct file *file)
- {
- if (Device_Open)
- return -EBUSY;
- Device_Open++;
- sprintf(msg, "Wczytano : %s", msg_in);
- size_of_message = strlen(msg);
- msg_Ptr = msg;
- printk(KERN_INFO "%s", msg);
- try_module_get(THIS_MODULE);
- return 0;
- }
- static int f_release(struct inode *inode, struct file *file)
- {
- Device_Open--;
- module_put(THIS_MODULE);
- return 0;
- }
- static ssize_t f_read(struct file *filp,
- char *buffer,
- size_t length,
- loff_t * offset)
- {
- int bytes_read = 0;
- if (*msg_Ptr == 0)
- return 0;
- while (bytes_read != size_of_message && *msg_Ptr != '\n') {
- put_user(*(msg_Ptr++), buffer++);
- length--;
- bytes_read++;
- }
- return bytes_read;
- }
- static ssize_t f_write(struct file *filp, const char *buff, size_t len, loff_t * off)
- {
- sprintf(msg_in, "%s", buff);
- size_of_message = strlen(msg_in);
- printk(KERN_INFO "Odebrano %zu znakow od uzytkownika\n", len);
- return len;
- }
- module_init(init_start);
- module_exit(cleanup);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement