Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/security.h>
  5. #include <linux/uaccess.h> /* copy_from/to_user */
  6. #include <linux/fs.h>
  7.  
  8. #define FILE_NAME "mojaNazwa"
  9. MODULE_LICENSE("GPL"); // Licencja, bez zalaczenia moga wystapic bledy kompilacji
  10.  
  11. MODULE_AUTHOR("autor"); // autor widoczne w modinfo
  12.  
  13. MODULE_DESCRIPTION("zad3"); // opis modulu - modinfo
  14.  
  15. MODULE_VERSION("0.1"); //wersja
  16.  
  17. static int f_open(struct inode *, struct file *);
  18.  
  19. static int f_release(struct inode *, struct file *);
  20.  
  21. static ssize_t f_read(struct file *, char *, size_t , loff_t *);
  22.  
  23. static ssize_t f_write(struct file *, const char *, size_t , loff_t *);
  24.  
  25. static void cleanup(void);
  26.  
  27. static int init_start(void);
  28.  
  29. static int result;
  30. static int Device_Open = 0;
  31. static char msg[255] = {0};
  32. static char msg_in[240] = {0};
  33. static char *msg_Ptr;
  34. static int size_of_message;
  35.  
  36. static struct file_operations fops = {
  37.  
  38. .read = f_read,
  39. .write = f_write,
  40. .open = f_open,
  41. .release = f_release
  42.  
  43. };
  44.  
  45. static int __init init_start(void)
  46. {
  47. printk(KERN_INFO "Ladowanie modulu\n");
  48. result = register_chrdev(0, FILE_NAME, &fops);
  49. if (result < 0) {
  50. printk(KERN_ALERT "Niepowodzenie w rejestracji urzadzenia %d\n", result);
  51. return result;
  52. }
  53.  
  54. printk(KERN_INFO "Moj major number to : %d\n", result);
  55. printk(KERN_INFO "Moj plik nalezy stworzyc w katalogu /dev \n");
  56. printk(KERN_INFO "'mknod -m 777 /dev/%s c %d 0'.\n", FILE_NAME, result);
  57. printk(KERN_INFO "Usun plik po zamknieciu modulu.\n");
  58.  
  59. return 0;
  60. }
  61.  
  62. static void __exit cleanup(void)
  63. {
  64. printk(KERN_INFO "Odlacznie modulu");
  65. unregister_chrdev(result, FILE_NAME);
  66. }
  67.  
  68. static int f_open(struct inode *inode, struct file *file)
  69. {
  70. if (Device_Open)
  71. return -EBUSY;
  72.  
  73. Device_Open++;
  74. sprintf(msg, "Wczytano : %s", msg_in);
  75. size_of_message = strlen(msg);
  76. msg_Ptr = msg;
  77. printk(KERN_INFO "%s", msg);
  78. try_module_get(THIS_MODULE);
  79.  
  80. return 0;
  81. }
  82.  
  83. static int f_release(struct inode *inode, struct file *file)
  84. {
  85. Device_Open--;
  86. module_put(THIS_MODULE);
  87.  
  88. return 0;
  89. }
  90.  
  91. static ssize_t f_read(struct file *filp,
  92. char *buffer,
  93. size_t length,
  94. loff_t * offset)
  95. {
  96. int bytes_read = 0;
  97.  
  98. if (*msg_Ptr == 0)
  99. return 0;
  100.  
  101. while (bytes_read != size_of_message && *msg_Ptr != '\n') {
  102.  
  103. put_user(*(msg_Ptr++), buffer++);
  104. length--;
  105. bytes_read++;
  106. }
  107.  
  108. return bytes_read;
  109. }
  110.  
  111. static ssize_t f_write(struct file *filp, const char *buff, size_t len, loff_t * off)
  112. {
  113. sprintf(msg_in, "%s", buff);
  114. size_of_message = strlen(msg_in);
  115. printk(KERN_INFO "Odebrano %zu znakow od uzytkownika\n", len);
  116. return len;
  117. }
  118.  
  119. module_init(init_start);
  120. module_exit(cleanup);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement