Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/moduleparam.h>
  4. #include <linux/init.h>
  5. #include <linux/fs.h>
  6. #include <string.h>
  7.  
  8. #define DEVICE_NAME "nulll"
  9.  
  10. static int capacity = 0;
  11. static int used = 0;
  12. module_param(capacity, int, S_IRUGO);
  13.  
  14. static int major_number;
  15.  
  16. static ssize_t device_write( struct file *, const char *, size_t, loff_t * );
  17.  
  18. static struct file_operations fops = {
  19. .write = device_write
  20. };
  21.  
  22. static int __init test_init (void) {
  23. major_number = register_chrdev(0, DEVICE_NAME, &fops);
  24.  
  25. if (major_number < 0) {
  26. printk("Register failed with %d\n", major_number);
  27. return major_number;
  28. }
  29. return 0;
  30. }
  31.  
  32. static void __exit test_exit (void) {
  33. unregister_chrdev( major_number, DEVICE_NAME );
  34. }
  35.  
  36. module_init( test_init );
  37. module_exit( test_exit );
  38.  
  39. static ssize_t device_write (struct file *filp, const char *buff, size_t len, loff_t * off) {
  40. if (capacity != 0 && (used + len) > capacity) return ENOSPC;
  41. capacity += len;
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement