sleep.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * sleep.c - create a /proc file, and if several processes try to open it
  3. * at the same time, put all but one to sleep.
  4. */
  5. #include <linux/kernel.h> /* We're doing kernel work */
  6. #include <linux/module.h> /* Specifically, a module */
  7. #include <linux/proc_fs.h> /* Necessary because we use proc fs */
  8. #include <linux/sched.h> /* For putting processes to sleep and
  9. waking them up */
  10. #include <linux/uaccess.h> /* for get_user and put_user */
  11. #include <linux/version.h>
  12. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
  13. #define HAVE_PROC_OPS
  14. #endif
  15. /* Here we keep the last message received, to prove that we can process our
  16. * input.
  17. */
  18. #define MESSAGE_LENGTH 80
  19. static char message[MESSAGE_LENGTH];
  20. static struct proc_dir_entry *our_proc_file;
  21. #define PROC_ENTRY_FILENAME "sleep"
  22. /* Since we use the file operations struct, we can't use the special proc
  23. * output provisions - we have to use a standard read function, which is this
  24. * function.
  25. */
  26. static ssize_t module_output(struct file *file, /* see include/linux/fs.h */
  27. char __user *buf, /* The buffer to put data to
  28. (in the user segment) */
  29. size_t len, /* The length of the buffer */
  30. loff_t *offset)
  31. {
  32. static int finished = 0;
  33. int i;
  34. char output_msg[MESSAGE_LENGTH + 30];
  35. /* Return 0 to signify end of file - that we have nothing more to say
  36. * at this point.
  37. */
  38. if (finished) {
  39. finished = 0;
  40. return 0;
  41. }
  42. sprintf(output_msg, "Last input:%s\n", message);
  43. for (i = 0; i < len && output_msg[i]; i++)
  44. put_user(output_msg[i], buf + i);
  45. finished = 1;
  46. return i; /* Return the number of bytes "read" */
  47. }
  48. /* This function receives input from the user when the user writes to the
  49. * /proc file.
  50. */
  51. static ssize_t module_input(struct file *file, /* The file itself */
  52. const char __user *buf, /* The buffer with input */
  53. size_t length, /* The buffer's length */
  54. loff_t *offset) /* offset to file - ignore */
  55. {
  56. int i;
  57. /* Put the input into Message, where module_output will later be able
  58. * to use it.
  59. */
  60. for (i = 0; i < MESSAGE_LENGTH - 1 && i < length; i++)
  61. get_user(message[i], buf + i);
  62. /* we want a standard, zero terminated string */
  63. message[i] = '\0';
  64. /* We need to return the number of input characters used */
  65. return i;
  66. }
  67. /* 1 if the file is currently open by somebody */
  68. static atomic_t already_open = ATOMIC_INIT(0);
  69. /* Queue of processes who want our file */
  70. static DECLARE_WAIT_QUEUE_HEAD(waitq);
  71. /* Called when the /proc file is opened */
  72. static int module_open(struct inode *inode, struct file *file)
  73. {
  74. /* If the file's flags include O_NONBLOCK, it means the process does not
  75. * want to wait for the file. In this case, if the file is already open,
  76. * we should fail with -EAGAIN, meaning "you will have to try again",
  77. * instead of blocking a process which would rather stay awake.
  78. */
  79. if ((file->f_flags & O_NONBLOCK) && atomic_read(&already_open))
  80. return -EAGAIN;
  81. /* This is the correct place for try_module_get(THIS_MODULE) because if
  82. * a process is in the loop, which is within the kernel module,
  83. * the kernel module must not be removed.
  84. */
  85. try_module_get(THIS_MODULE);
  86. while (atomic_cmpxchg(&already_open, 0, 1)) {
  87. int i, is_sig = 0;
  88. /* This function puts the current process, including any system
  89. * calls, such as us, to sleep. Execution will be resumed right
  90. * after the function call, either because somebody called
  91. * wake_up(&waitq) (only module_close does that, when the file
  92. * is closed) or when a signal, such as Ctrl-C, is sent
  93. * to the process
  94. */
  95. wait_event_interruptible(waitq, !atomic_read(&already_open));
  96. /* If we woke up because we got a signal we're not blocking,
  97. * return -EINTR (fail the system call). This allows processes
  98. * to be killed or stopped.
  99. */
  100. for (i = 0; i < _NSIG_WORDS && !is_sig; i++)
  101. is_sig = current->pending.signal.sig[i] & ~current->blocked.sig[i];
  102. if (is_sig) {
  103. /* It is important to put module_put(THIS_MODULE) here, because
  104. * for processes where the open is interrupted there will never
  105. * be a corresponding close. If we do not decrement the usage
  106. * count here, we will be left with a positive usage count
  107. * which we will have no way to bring down to zero, giving us
  108. * an immortal module, which can only be killed by rebooting
  109. * the machine.
  110. */
  111. module_put(THIS_MODULE);
  112. return -EINTR;
  113. }
  114. }
  115. return 0; /* Allow the access */
  116. }
  117. /* Called when the /proc file is closed */
  118. static int module_close(struct inode *inode, struct file *file)
  119. {
  120. /* Set already_open to zero, so one of the processes in the waitq will
  121. * be able to set already_open back to one and to open the file. All
  122. * the other processes will be called when already_open is back to one,
  123. * so they'll go back to sleep.
  124. */
  125. atomic_set(&already_open, 0);
  126. /* Wake up all the processes in waitq, so if anybody is waiting for the
  127. * file, they can have it.
  128. */
  129. wake_up(&waitq);
  130. module_put(THIS_MODULE);
  131. return 0; /* success */
  132. }
  133. /* Structures to register as the /proc file, with pointers to all the relevant
  134. * functions.
  135. */
  136. /* File operations for our proc file. This is where we place pointers to all
  137. * the functions called when somebody tries to do something to our file. NULL
  138. * means we don't want to deal with something.
  139. */
  140. #ifdef HAVE_PROC_OPS
  141. static const struct proc_ops file_ops_4_our_proc_file = {
  142. .proc_read = module_output, /* "read" from the file */
  143. .proc_write = module_input, /* "write" to the file */
  144. .proc_open = module_open, /* called when the /proc file is opened */
  145. .proc_release = module_close, /* called when it's closed */
  146. .proc_lseek = noop_llseek, /* return file->f_pos */
  147. };
  148. #else
  149. static const struct file_operations file_ops_4_our_proc_file = {
  150. .read = module_output,
  151. .write = module_input,
  152. .open = module_open,
  153. .release = module_close,
  154. .llseek = noop_llseek,
  155. };
  156. #endif
  157. /* Initialize the module - register the proc file */
  158. static int __init sleep_init(void)
  159. {
  160. our_proc_file =
  161. proc_create(PROC_ENTRY_FILENAME, 0644, NULL, &file_ops_4_our_proc_file);
  162. if (our_proc_file == NULL) {
  163. remove_proc_entry(PROC_ENTRY_FILENAME, NULL);
  164. pr_debug("Error: Could not initialize /proc/%s\n", PROC_ENTRY_FILENAME);
  165. return -ENOMEM;
  166. }
  167. proc_set_size(our_proc_file, 80);
  168. proc_set_user(our_proc_file, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID);
  169. pr_info("/proc/%s created\n", PROC_ENTRY_FILENAME);
  170. return 0;
  171. }
  172. /* Cleanup - unregister our file from /proc. This could get dangerous if
  173. * there are still processes waiting in waitq, because they are inside our
  174. * open function, which will get unloaded. I'll explain how to avoid removal
  175. * of a kernel module in such a case in chapter 10.
  176. */
  177. static void __exit sleep_exit(void)
  178. {
  179. remove_proc_entry(PROC_ENTRY_FILENAME, NULL);
  180. pr_debug("/proc/%s removed\n", PROC_ENTRY_FILENAME);
  181. }
  182. module_init(sleep_init);
  183. module_exit(sleep_exit);
  184. MODULE_LICENSE("GPL");