sleep.c 7.5 KB

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