sleep.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 contained in
  129. * two words (64 bits) and are stored in a structure that contains an array of
  130. * two unsigned longs. We now have to make 2 checks in our if.
  131. *
  132. * Ori Pomerantz:
  133. *
  134. * Nobody promised me they'll never use more than 64 bits, or that this book
  135. * won't be used for a version of Linux with a word size of 16 bits. This code
  136. * would work in any case.
  137. */
  138. for (i = 0; i < _NSIG_WORDS && !is_sig; i++)
  139. is_sig =
  140. current->pending.signal.sig[i] & ~current->
  141. blocked.sig[i];
  142. if (is_sig) {
  143. /*
  144. * It's important to put module_put(THIS_MODULE) here,
  145. * because for processes where the open is interrupted
  146. * there will never be a corresponding close. If we
  147. * don't decrement the usage count here, we will be
  148. * left with a positive usage count which we'll have no
  149. * way to bring down to zero, giving us an immortal
  150. * module, which can only be killed by rebooting
  151. * the machine.
  152. */
  153. module_put(THIS_MODULE);
  154. return -EINTR;
  155. }
  156. }
  157. /*
  158. * If we got here, Already_Open must be zero
  159. */
  160. /*
  161. * Open the file
  162. */
  163. Already_Open = 1;
  164. return 0; /* Allow the access */
  165. }
  166. /*
  167. * Called when the /proc file is closed
  168. */
  169. int module_close(struct inode *inode, struct file *file)
  170. {
  171. /*
  172. * Set Already_Open to zero, so one of the processes in the WaitQ will
  173. * be able to set Already_Open back to one and to open the file. All
  174. * the other processes will be called when Already_Open is back to one,
  175. * so they'll go back to sleep.
  176. */
  177. Already_Open = 0;
  178. /*
  179. * Wake up all the processes in WaitQ, so if anybody is waiting for the
  180. * file, they can have it.
  181. */
  182. wake_up(&WaitQ);
  183. module_put(THIS_MODULE);
  184. return 0; /* success */
  185. }
  186. /*
  187. * Structures to register as the /proc file, with pointers to all the relevant
  188. * functions.
  189. */
  190. /*
  191. * File operations for our proc file. This is where we place pointers to all
  192. * the functions called when somebody tries to do something to our file. NULL
  193. * means we don't want to deal with something.
  194. */
  195. static struct proc_ops File_Ops_4_Our_Proc_File = {
  196. .proc_read = module_output, /* "read" from the file */
  197. .proc_write = module_input, /* "write" to the file */
  198. .proc_open = module_open, /* called when the /proc file is opened */
  199. .proc_release = module_close, /* called when it's closed */
  200. };
  201. /*
  202. * Module initialization and cleanup
  203. */
  204. /*
  205. * Initialize the module - register the proc file
  206. */
  207. int init_module()
  208. {
  209. Our_Proc_File = proc_create(PROC_ENTRY_FILENAME, 0644, NULL, &File_Ops_4_Our_Proc_File);
  210. if(Our_Proc_File == NULL)
  211. {
  212. remove_proc_entry(PROC_ENTRY_FILENAME, NULL);
  213. pr_debug("Error: Could not initialize /proc/%s\n", PROC_ENTRY_FILENAME);
  214. return -ENOMEM;
  215. }
  216. proc_set_size(Our_Proc_File, 80);
  217. proc_set_user(Our_Proc_File, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID);
  218. pr_info("/proc/test created\n");
  219. return 0;
  220. }
  221. /*
  222. * Cleanup - unregister our file from /proc. This could get dangerous if
  223. * there are still processes waiting in WaitQ, because they are inside our
  224. * open function, which will get unloaded. I'll explain how to avoid removal
  225. * of a kernel module in such a case in chapter 10.
  226. */
  227. void cleanup_module()
  228. {
  229. remove_proc_entry(PROC_ENTRY_FILENAME, NULL);
  230. pr_debug("/proc/%s removed\n", PROC_ENTRY_FILENAME);
  231. }
  232. MODULE_LICENSE("GPL");