sleep.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * sleep.c - create a /proc file, and if several processes try to open it at
  3. * 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. /*
  16. * The module's file functions
  17. */
  18. /*
  19. * Here we keep the last message received, to prove that we can process our
  20. * input
  21. */
  22. #define MESSAGE_LENGTH 80
  23. static char Message[MESSAGE_LENGTH];
  24. static struct proc_dir_entry *Our_Proc_File;
  25. #define PROC_ENTRY_FILENAME "sleep"
  26. /*
  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 *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 message[MESSAGE_LENGTH + 30];
  40. /*
  41. * Return 0 to signify end of file - that we have nothing
  42. * more to say at this point.
  43. */
  44. if (finished) {
  45. finished = 0;
  46. return 0;
  47. }
  48. /*
  49. * If you don't understand this by now, you're hopeless as a kernel
  50. * programmer.
  51. */
  52. sprintf(message, "Last input:%s\n", Message);
  53. for (i = 0; i < len && message[i]; i++)
  54. put_user(message[i], buf + i);
  55. finished = 1;
  56. return i; /* Return the number of bytes "read" */
  57. }
  58. /*
  59. * This function receives input from the user when the user writes to the /proc
  60. * file.
  61. */
  62. static ssize_t module_input(struct file *file, /* The file itself */
  63. const char *buf, /* The buffer with input */
  64. size_t length, /* The buffer's length */
  65. loff_t *offset) /* offset to file - ignore */
  66. {
  67. int i;
  68. /*
  69. * Put the input into Message, where module_output will later be
  70. * able to use it
  71. */
  72. for (i = 0; i < MESSAGE_LENGTH - 1 && i < length; i++)
  73. get_user(Message[i], buf + i);
  74. /*
  75. * we want a standard, zero terminated string
  76. */
  77. Message[i] = '\0';
  78. /*
  79. * We need to return the number of input characters used
  80. */
  81. return i;
  82. }
  83. /*
  84. * 1 if the file is currently open by somebody
  85. */
  86. int Already_Open = 0;
  87. /*
  88. * Queue of processes who want our file
  89. */
  90. DECLARE_WAIT_QUEUE_HEAD(WaitQ);
  91. /*
  92. * Called when the /proc file is opened
  93. */
  94. static int module_open(struct inode *inode, struct file *file)
  95. {
  96. /*
  97. * If the file's flags include O_NONBLOCK, it means the process doesn't
  98. * want to wait for the file. In this case, if the file is already
  99. * open, we should fail with -EAGAIN, meaning "you'll have to try
  100. * again", instead of blocking a process which would rather stay awake.
  101. */
  102. if ((file->f_flags & O_NONBLOCK) && Already_Open)
  103. return -EAGAIN;
  104. /*
  105. * This is the correct place for try_module_get(THIS_MODULE) because
  106. * if a process is in the loop, which is within the kernel module,
  107. * the kernel module must not be removed.
  108. */
  109. try_module_get(THIS_MODULE);
  110. /*
  111. * If the file is already open, wait until it isn't
  112. */
  113. while (Already_Open) {
  114. int i, is_sig = 0;
  115. /*
  116. * This function puts the current process, including any system
  117. * calls, such as us, to sleep. Execution will be resumed right
  118. * after the function call, either because somebody called
  119. * wake_up(&WaitQ) (only module_close does that, when the file
  120. * is closed) or when a signal, such as Ctrl-C, is sent
  121. * to the process
  122. */
  123. wait_event_interruptible(WaitQ, !Already_Open);
  124. /*
  125. * If we woke up because we got a signal we're not blocking,
  126. * return -EINTR (fail the system call). This allows processes
  127. * to be killed or stopped.
  128. */
  129. /*
  130. * Emmanuel Papirakis:
  131. *
  132. * This is a little update to work with 2.2.*. Signals now are
  133. * contained in two words (64 bits) and are stored in a structure that
  134. * contains an array of two unsigned longs. We now have to make 2
  135. * checks in our if.
  136. *
  137. * Ori Pomerantz:
  138. *
  139. * Nobody promised me they'll never use more than 64 bits, or that this
  140. * book won't be used for a version of Linux with a word size of 16
  141. * bits. This code would work in any case.
  142. */
  143. for (i = 0; i < _NSIG_WORDS && !is_sig; i++)
  144. is_sig = current->pending.signal.sig[i] & ~current->blocked.sig[i];
  145. if (is_sig) {
  146. /*
  147. * It's important to put module_put(THIS_MODULE) here,
  148. * because for processes where the open is interrupted
  149. * there will never be a corresponding close. If we
  150. * don't decrement the usage count here, we will be
  151. * left with a positive usage count which we'll have no
  152. * way to bring down to zero, giving us an immortal
  153. * module, which can only be killed by rebooting
  154. * the machine.
  155. */
  156. module_put(THIS_MODULE);
  157. return -EINTR;
  158. }
  159. }
  160. /*
  161. * If we got here, Already_Open must be zero
  162. */
  163. /*
  164. * Open the file
  165. */
  166. Already_Open = 1;
  167. return 0; /* Allow the access */
  168. }
  169. /*
  170. * Called when the /proc file is closed
  171. */
  172. int module_close(struct inode *inode, struct file *file)
  173. {
  174. /*
  175. * Set Already_Open to zero, so one of the processes in the WaitQ will
  176. * be able to set Already_Open back to one and to open the file. All
  177. * the other processes will be called when Already_Open is back to one,
  178. * so they'll go back to sleep.
  179. */
  180. Already_Open = 0;
  181. /*
  182. * Wake up all the processes in WaitQ, so if anybody is waiting for the
  183. * file, they can have it.
  184. */
  185. wake_up(&WaitQ);
  186. module_put(THIS_MODULE);
  187. return 0; /* success */
  188. }
  189. /*
  190. * Structures to register as the /proc file, with pointers to all the relevant
  191. * functions.
  192. */
  193. /*
  194. * File operations for our proc file. This is where we place pointers to all
  195. * the functions called when somebody tries to do something to our file. NULL
  196. * means we don't want to deal with something.
  197. */
  198. #ifdef HAVE_PROC_OPS
  199. static const struct proc_ops File_Ops_4_Our_Proc_File = {
  200. .proc_read = module_output, /* "read" from the file */
  201. .proc_write = module_input, /* "write" to the file */
  202. .proc_open = module_open, /* called when the /proc file is opened */
  203. .proc_release = module_close, /* called when it's closed */
  204. };
  205. #else
  206. static const struct file_operations File_Ops_4_Our_Proc_File = {
  207. .read = module_output,
  208. .write = module_input,
  209. .open = module_open,
  210. .release = module_close,
  211. };
  212. #endif
  213. /* Initialize the module - register the proc file */
  214. static int __init sleep_init(void)
  215. {
  216. Our_Proc_File =
  217. proc_create(PROC_ENTRY_FILENAME, 0644, NULL, &File_Ops_4_Our_Proc_File);
  218. if (Our_Proc_File == NULL) {
  219. remove_proc_entry(PROC_ENTRY_FILENAME, NULL);
  220. pr_debug("Error: Could not initialize /proc/%s\n", PROC_ENTRY_FILENAME);
  221. return -ENOMEM;
  222. }
  223. proc_set_size(Our_Proc_File, 80);
  224. proc_set_user(Our_Proc_File, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID);
  225. pr_info("/proc/test created\n");
  226. return 0;
  227. }
  228. /* Cleanup - unregister our file from /proc. This could get dangerous if
  229. * there are still processes waiting in WaitQ, because they are inside our
  230. * open function, which will get unloaded. I'll explain how to avoid removal
  231. * of a kernel module in such a case in chapter 10.
  232. */
  233. static void __exit sleep_exit(void)
  234. {
  235. remove_proc_entry(PROC_ENTRY_FILENAME, NULL);
  236. pr_debug("/proc/%s removed\n", PROC_ENTRY_FILENAME);
  237. }
  238. module_init(sleep_init);
  239. module_exit(sleep_exit);
  240. MODULE_LICENSE("GPL");