sleep.c 7.5 KB

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