sleep.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 int already_open = 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) && 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. /* If the file is already open, wait until it is not. */
  87. while (already_open) {
  88. int i, is_sig = 0;
  89. /* This function puts the current process, including any system
  90. * calls, such as us, to sleep. Execution will be resumed right
  91. * after the function call, either because somebody called
  92. * wake_up(&waitq) (only module_close does that, when the file
  93. * is closed) or when a signal, such as Ctrl-C, is sent
  94. * to the process
  95. */
  96. wait_event_interruptible(waitq, !already_open);
  97. /* If we woke up because we got a signal we're not blocking,
  98. * return -EINTR (fail the system call). This allows processes
  99. * to be killed or stopped.
  100. */
  101. for (i = 0; i < _NSIG_WORDS && !is_sig; i++)
  102. is_sig = current->pending.signal.sig[i] & ~current->blocked.sig[i];
  103. if (is_sig) {
  104. /* It is important to put module_put(THIS_MODULE) here, because
  105. * for processes where the open is interrupted there will never
  106. * be a corresponding close. If we do not decrement the usage
  107. * count here, we will be left with a positive usage count
  108. * which we will have no way to bring down to zero, giving us
  109. * an immortal module, which can only be killed by rebooting
  110. * the machine.
  111. */
  112. module_put(THIS_MODULE);
  113. return -EINTR;
  114. }
  115. }
  116. /* If we got here, already_open must be zero. */
  117. /* Open the file */
  118. already_open = 1;
  119. return 0; /* Allow the access */
  120. }
  121. /* Called when the /proc file is closed */
  122. static int module_close(struct inode *inode, struct file *file)
  123. {
  124. /* Set already_open to zero, so one of the processes in the waitq will
  125. * be able to set already_open back to one and to open the file. All
  126. * the other processes will be called when already_open is back to one,
  127. * so they'll go back to sleep.
  128. */
  129. already_open = 0;
  130. /* Wake up all the processes in waitq, so if anybody is waiting for the
  131. * file, they can have it.
  132. */
  133. wake_up(&waitq);
  134. module_put(THIS_MODULE);
  135. return 0; /* success */
  136. }
  137. /* Structures to register as the /proc file, with pointers to all the relevant
  138. * functions.
  139. */
  140. /* File operations for our proc file. This is where we place pointers to all
  141. * the functions called when somebody tries to do something to our file. NULL
  142. * means we don't want to deal with something.
  143. */
  144. #ifdef HAVE_PROC_OPS
  145. static const struct proc_ops file_ops_4_our_proc_file = {
  146. .proc_read = module_output, /* "read" from the file */
  147. .proc_write = module_input, /* "write" to the file */
  148. .proc_open = module_open, /* called when the /proc file is opened */
  149. .proc_release = module_close, /* called when it's closed */
  150. };
  151. #else
  152. static const struct file_operations file_ops_4_our_proc_file = {
  153. .read = module_output,
  154. .write = module_input,
  155. .open = module_open,
  156. .release = module_close,
  157. };
  158. #endif
  159. /* Initialize the module - register the proc file */
  160. static int __init sleep_init(void)
  161. {
  162. our_proc_file =
  163. proc_create(PROC_ENTRY_FILENAME, 0644, NULL, &file_ops_4_our_proc_file);
  164. if (our_proc_file == NULL) {
  165. remove_proc_entry(PROC_ENTRY_FILENAME, NULL);
  166. pr_debug("Error: Could not initialize /proc/%s\n", PROC_ENTRY_FILENAME);
  167. return -ENOMEM;
  168. }
  169. proc_set_size(our_proc_file, 80);
  170. proc_set_user(our_proc_file, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID);
  171. pr_info("/proc/%s created\n", PROC_ENTRY_FILENAME);
  172. return 0;
  173. }
  174. /* Cleanup - unregister our file from /proc. This could get dangerous if
  175. * there are still processes waiting in waitq, because they are inside our
  176. * open function, which will get unloaded. I'll explain how to avoid removal
  177. * of a kernel module in such a case in chapter 10.
  178. */
  179. static void __exit sleep_exit(void)
  180. {
  181. remove_proc_entry(PROC_ENTRY_FILENAME, NULL);
  182. pr_debug("/proc/%s removed\n", PROC_ENTRY_FILENAME);
  183. }
  184. module_init(sleep_init);
  185. module_exit(sleep_exit);
  186. MODULE_LICENSE("GPL");