pipe.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * File : pipe.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012-2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-09-30 Bernard first version.
  23. * 2017-11-08 JasonJiaJie fix memory leak issue when close a pipe.
  24. */
  25. #include <rthw.h>
  26. #include <rtdevice.h>
  27. #if defined(RT_USING_POSIX)
  28. #include <dfs_file.h>
  29. #include <dfs_posix.h>
  30. #include <dfs_poll.h>
  31. static int pipe_fops_open(struct dfs_fd *fd)
  32. {
  33. rt_device_t device;
  34. rt_pipe_t *pipe;
  35. pipe = (rt_pipe_t *)fd->data;
  36. if (!pipe) return -1;
  37. device = &(pipe->parent);
  38. rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
  39. if (device->ref_count == 0)
  40. {
  41. pipe->fifo = rt_ringbuffer_create(PIPE_BUFSZ);
  42. }
  43. switch (fd->flags & O_ACCMODE)
  44. {
  45. case O_RDONLY:
  46. pipe->readers ++;
  47. break;
  48. case O_WRONLY:
  49. pipe->writers ++;
  50. break;
  51. case O_RDWR:
  52. pipe->readers ++;
  53. pipe->writers ++;
  54. break;
  55. }
  56. device->ref_count ++;
  57. rt_mutex_release(&(pipe->lock));
  58. return 0;
  59. }
  60. static int pipe_fops_close(struct dfs_fd *fd)
  61. {
  62. rt_device_t device;
  63. rt_pipe_t *pipe;
  64. pipe = (rt_pipe_t *)fd->data;
  65. if (!pipe) return -1;
  66. device = &(pipe->parent);
  67. rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
  68. switch (fd->flags & O_ACCMODE)
  69. {
  70. case O_RDONLY:
  71. pipe->readers --;
  72. break;
  73. case O_WRONLY:
  74. pipe->writers --;
  75. break;
  76. case O_RDWR:
  77. pipe->readers --;
  78. pipe->writers --;
  79. break;
  80. }
  81. if (pipe->writers == 0)
  82. {
  83. rt_wqueue_wakeup(&(pipe->reader_queue), (void*)(POLLIN | POLLERR | POLLHUP));
  84. }
  85. if (pipe->readers == 0)
  86. {
  87. rt_wqueue_wakeup(&(pipe->writer_queue), (void*)(POLLOUT | POLLERR | POLLHUP));
  88. }
  89. if (device->ref_count == 1)
  90. {
  91. rt_ringbuffer_destroy(pipe->fifo);
  92. pipe->fifo = RT_NULL;
  93. }
  94. device->ref_count --;
  95. rt_mutex_release(&(pipe->lock));
  96. return 0;
  97. }
  98. static int pipe_fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
  99. {
  100. rt_pipe_t *pipe;
  101. int ret = 0;
  102. pipe = (rt_pipe_t *)fd->data;
  103. switch (cmd)
  104. {
  105. case FIONREAD:
  106. *((int*)args) = rt_ringbuffer_data_len(pipe->fifo);
  107. break;
  108. case FIONWRITE:
  109. *((int*)args) = rt_ringbuffer_space_len(pipe->fifo);
  110. break;
  111. default:
  112. ret = -EINVAL;
  113. break;
  114. }
  115. return ret;
  116. }
  117. static int pipe_fops_read(struct dfs_fd *fd, void *buf, size_t count)
  118. {
  119. int len = 0;
  120. rt_pipe_t *pipe;
  121. pipe = (rt_pipe_t *)fd->data;
  122. /* no process has the pipe open for writing, return end-of-file */
  123. if (pipe->writers == 0)
  124. return 0;
  125. rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
  126. while (1)
  127. {
  128. if (pipe->writers == 0)
  129. {
  130. goto out;
  131. }
  132. len = rt_ringbuffer_get(pipe->fifo, buf, count);
  133. if (len > 0)
  134. {
  135. break;
  136. }
  137. else
  138. {
  139. if (fd->flags & O_NONBLOCK)
  140. {
  141. len = -EAGAIN;
  142. goto out;
  143. }
  144. rt_mutex_release(&pipe->lock);
  145. rt_wqueue_wakeup(&(pipe->writer_queue), (void*)POLLOUT);
  146. rt_wqueue_wait(&(pipe->reader_queue), 0, -1);
  147. rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
  148. }
  149. }
  150. /* wakeup writer */
  151. rt_wqueue_wakeup(&(pipe->writer_queue), (void*)POLLOUT);
  152. out:
  153. rt_mutex_release(&pipe->lock);
  154. return len;
  155. }
  156. static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
  157. {
  158. int len;
  159. rt_pipe_t *pipe;
  160. int wakeup = 0;
  161. int ret = 0;
  162. uint8_t *pbuf;
  163. pipe = (rt_pipe_t *)fd->data;
  164. if (pipe->readers == 0)
  165. {
  166. ret = -EPIPE;
  167. goto out;
  168. }
  169. if (count == 0)
  170. return 0;
  171. pbuf = (uint8_t*)buf;
  172. rt_mutex_take(&pipe->lock, -1);
  173. while (1)
  174. {
  175. if (pipe->readers == 0)
  176. {
  177. if (ret == 0)
  178. ret = -EPIPE;
  179. break;
  180. }
  181. len = rt_ringbuffer_put(pipe->fifo, pbuf, count - ret);
  182. ret += len;
  183. pbuf += len;
  184. wakeup = 1;
  185. if (ret == count)
  186. {
  187. break;
  188. }
  189. else
  190. {
  191. if (fd->flags & O_NONBLOCK)
  192. {
  193. if (ret == 0)
  194. {
  195. ret = -EAGAIN;
  196. }
  197. break;
  198. }
  199. }
  200. rt_mutex_release(&pipe->lock);
  201. rt_wqueue_wakeup(&(pipe->reader_queue), (void*)POLLIN);
  202. /* pipe full, waiting on suspended write list */
  203. rt_wqueue_wait(&(pipe->writer_queue), 0, -1);
  204. rt_mutex_take(&pipe->lock, -1);
  205. }
  206. rt_mutex_release(&pipe->lock);
  207. if (wakeup)
  208. {
  209. rt_wqueue_wakeup(&(pipe->reader_queue), (void*)POLLIN);
  210. }
  211. out:
  212. return ret;
  213. }
  214. static int pipe_fops_poll(struct dfs_fd *fd, rt_pollreq_t *req)
  215. {
  216. int mask = 0;
  217. rt_pipe_t *pipe;
  218. int mode = 0;
  219. pipe = (rt_pipe_t *)fd->data;
  220. rt_poll_add(&(pipe->reader_queue), req);
  221. rt_poll_add(&(pipe->writer_queue), req);
  222. switch (fd->flags & O_ACCMODE)
  223. {
  224. case O_RDONLY:
  225. mode = 1;
  226. break;
  227. case O_WRONLY:
  228. mode = 2;
  229. break;
  230. case O_RDWR:
  231. mode = 3;
  232. break;
  233. }
  234. if (mode & 1)
  235. {
  236. if (rt_ringbuffer_data_len(pipe->fifo) != 0)
  237. {
  238. mask |= POLLIN;
  239. }
  240. if (pipe->writers == 0)
  241. {
  242. mask |= POLLHUP;
  243. }
  244. }
  245. if (mode & 2)
  246. {
  247. if (rt_ringbuffer_space_len(pipe->fifo) != 0)
  248. {
  249. mask |= POLLOUT;
  250. }
  251. if (pipe->readers == 0)
  252. {
  253. mask |= POLLERR;
  254. }
  255. }
  256. return mask;
  257. }
  258. static const struct dfs_file_ops pipe_fops =
  259. {
  260. pipe_fops_open,
  261. pipe_fops_close,
  262. pipe_fops_ioctl,
  263. pipe_fops_read,
  264. pipe_fops_write,
  265. RT_NULL,
  266. RT_NULL,
  267. RT_NULL,
  268. pipe_fops_poll,
  269. };
  270. rt_pipe_t *rt_pipe_create(const char *name)
  271. {
  272. rt_pipe_t *pipe;
  273. rt_device_t dev;
  274. pipe = rt_malloc(sizeof(rt_pipe_t));
  275. if (pipe == RT_NULL) return RT_NULL;
  276. rt_memset(pipe, 0, sizeof(rt_pipe_t));
  277. rt_mutex_init(&(pipe->lock), name, RT_IPC_FLAG_FIFO);
  278. rt_list_init(&(pipe->reader_queue));
  279. rt_list_init(&(pipe->writer_queue));
  280. dev = &(pipe->parent);
  281. dev->type = RT_Device_Class_Pipe;
  282. if (rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE) != 0)
  283. {
  284. rt_free(pipe);
  285. return RT_NULL;
  286. }
  287. dev->fops = (void*)&pipe_fops;
  288. return pipe;
  289. }
  290. int rt_pipe_delete(const char *name)
  291. {
  292. int result = 0;
  293. rt_device_t device;
  294. device = rt_device_find(name);
  295. if (device)
  296. {
  297. if (device->type == RT_Device_Class_Pipe)
  298. {
  299. rt_pipe_t *pipe;
  300. if (device->ref_count != 0)
  301. {
  302. return -RT_EBUSY;
  303. }
  304. pipe = (rt_pipe_t *)device;
  305. rt_mutex_detach(&(pipe->lock));
  306. rt_device_unregister(device);
  307. rt_free(pipe);
  308. }
  309. else
  310. {
  311. result = -1;
  312. }
  313. }
  314. else
  315. {
  316. result = -1;
  317. }
  318. return result;
  319. }
  320. int pipe(int fildes[2])
  321. {
  322. rt_pipe_t *pipe;
  323. char dname[8];
  324. char dev_name[32];
  325. static int pipeno = 0;
  326. rt_snprintf(dname, sizeof(dname), "pipe%d", pipeno++);
  327. pipe = rt_pipe_create(dname);
  328. if (pipe == RT_NULL)
  329. {
  330. return -1;
  331. }
  332. rt_snprintf(dev_name, sizeof(dev_name), "/dev/%s", dname);
  333. fildes[0] = open(dev_name, O_RDONLY, 0);
  334. if (fildes[0] < 0)
  335. {
  336. return -1;
  337. }
  338. fildes[1] = open(dev_name, O_WRONLY, 0);
  339. if (fildes[1] < 0)
  340. {
  341. close(fildes[1]);
  342. return -1;
  343. }
  344. return 0;
  345. }
  346. int mkfifo(const char *path, mode_t mode)
  347. {
  348. rt_pipe_t *pipe;
  349. pipe = rt_pipe_create(path);
  350. if (pipe == RT_NULL)
  351. {
  352. return -1;
  353. }
  354. return 0;
  355. }
  356. #endif