1
0

sleep.c 7.7 KB

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