1
0

sleep.c 7.9 KB

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