pipe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. #endif /* end of RT_USING_POSIX */
  271. rt_err_t rt_pipe_open (rt_device_t device, rt_uint16_t oflag)
  272. {
  273. rt_pipe_t *pipe = (rt_pipe_t *)device;
  274. if (device == RT_NULL) return -RT_EINVAL;
  275. rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
  276. if (pipe->fifo == RT_NULL)
  277. {
  278. pipe->fifo = rt_ringbuffer_create(pipe->bufsz);
  279. }
  280. rt_mutex_release(&(pipe->lock));
  281. return RT_EOK;
  282. }
  283. rt_err_t rt_pipe_close (rt_device_t device)
  284. {
  285. rt_pipe_t *pipe = (rt_pipe_t *)device;
  286. if (device == RT_NULL) return -RT_EINVAL;
  287. rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
  288. if (device->ref_count == 1)
  289. {
  290. rt_ringbuffer_destroy(pipe->fifo);
  291. pipe->fifo = RT_NULL;
  292. }
  293. rt_mutex_release(&(pipe->lock));
  294. return RT_EOK;
  295. }
  296. rt_size_t rt_pipe_read (rt_device_t device, rt_off_t pos, void *buffer, rt_size_t count)
  297. {
  298. uint8_t *pbuf;
  299. int read_bytes = 0;
  300. rt_pipe_t *pipe = (rt_pipe_t *)device;
  301. if (device == RT_NULL) return -EINVAL;
  302. if (count == 0) return 0;
  303. pbuf = (uint8_t*)buffer;
  304. rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
  305. while (read_bytes < count)
  306. {
  307. int len = rt_ringbuffer_get(pipe->fifo, &pbuf[read_bytes], count - read_bytes);
  308. if (len <= 0) break;
  309. read_bytes += len;
  310. }
  311. rt_mutex_release(&pipe->lock);
  312. return read_bytes;
  313. }
  314. rt_size_t rt_pipe_write (rt_device_t device, rt_off_t pos, const void *buffer, rt_size_t count)
  315. {
  316. uint8_t *pbuf;
  317. int write_bytes = 0;
  318. rt_pipe_t *pipe = (rt_pipe_t *)device;
  319. if (device == RT_NULL) return -EINVAL;
  320. if (count == 0) return 0;
  321. pbuf = (uint8_t*)buffer;
  322. rt_mutex_take(&pipe->lock, -1);
  323. while (write_bytes < count)
  324. {
  325. int len = rt_ringbuffer_put(pipe->fifo, &pbuf[write_bytes], count - write_bytes);
  326. if (len <= 0) break;
  327. write_bytes += len;
  328. }
  329. rt_mutex_release(&pipe->lock);
  330. return write_bytes;
  331. }
  332. rt_err_t rt_pipe_control(rt_device_t dev, int cmd, void *args)
  333. {
  334. return RT_EOK;
  335. }
  336. rt_pipe_t *rt_pipe_create(const char *name, int bufsz)
  337. {
  338. rt_pipe_t *pipe;
  339. rt_device_t dev;
  340. pipe = rt_malloc(sizeof(rt_pipe_t));
  341. if (pipe == RT_NULL) return RT_NULL;
  342. rt_memset(pipe, 0, sizeof(rt_pipe_t));
  343. rt_mutex_init(&(pipe->lock), name, RT_IPC_FLAG_FIFO);
  344. rt_list_init(&(pipe->reader_queue));
  345. rt_list_init(&(pipe->writer_queue));
  346. RT_ASSERT(bufsz < 0xFFFF);
  347. pipe->bufsz = bufsz;
  348. dev = &(pipe->parent);
  349. dev->type = RT_Device_Class_Pipe;
  350. dev->init = RT_NULL;
  351. dev->open = rt_pipe_open;
  352. dev->read = rt_pipe_read;
  353. dev->write = rt_pipe_write;
  354. dev->close = rt_pipe_close;
  355. dev->control = rt_pipe_control;
  356. dev->rx_indicate = RT_NULL;
  357. dev->tx_complete = RT_NULL;
  358. if (rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE) != 0)
  359. {
  360. rt_free(pipe);
  361. return RT_NULL;
  362. }
  363. #ifdef RT_USING_POSIX
  364. dev->fops = (void*)&pipe_fops;
  365. #endif
  366. return pipe;
  367. }
  368. int rt_pipe_delete(const char *name)
  369. {
  370. int result = 0;
  371. rt_device_t device;
  372. device = rt_device_find(name);
  373. if (device)
  374. {
  375. if (device->type == RT_Device_Class_Pipe)
  376. {
  377. rt_pipe_t *pipe;
  378. if (device->ref_count != 0)
  379. {
  380. return -RT_EBUSY;
  381. }
  382. pipe = (rt_pipe_t *)device;
  383. rt_mutex_detach(&(pipe->lock));
  384. rt_device_unregister(device);
  385. /* close fifo ringbuffer */
  386. if (pipe->fifo)
  387. {
  388. rt_ringbuffer_destroy(pipe->fifo);
  389. pipe->fifo = RT_NULL;
  390. }
  391. rt_free(pipe);
  392. }
  393. else
  394. {
  395. result = -ENODEV;
  396. }
  397. }
  398. else
  399. {
  400. result = -ENODEV;
  401. }
  402. return result;
  403. }
  404. #ifdef RT_USING_POSIX
  405. int pipe(int fildes[2])
  406. {
  407. rt_pipe_t *pipe;
  408. char dname[8];
  409. char dev_name[32];
  410. static int pipeno = 0;
  411. rt_snprintf(dname, sizeof(dname), "pipe%d", pipeno++);
  412. pipe = rt_pipe_create(dname, PIPE_BUFSZ);
  413. if (pipe == RT_NULL)
  414. {
  415. return -1;
  416. }
  417. rt_snprintf(dev_name, sizeof(dev_name), "/dev/%s", dname);
  418. fildes[0] = open(dev_name, O_RDONLY, 0);
  419. if (fildes[0] < 0)
  420. {
  421. return -1;
  422. }
  423. fildes[1] = open(dev_name, O_WRONLY, 0);
  424. if (fildes[1] < 0)
  425. {
  426. close(fildes[1]);
  427. return -1;
  428. }
  429. return 0;
  430. }
  431. int mkfifo(const char *path, mode_t mode)
  432. {
  433. rt_pipe_t *pipe;
  434. pipe = rt_pipe_create(path, PIPE_BUFSZ);
  435. if (pipe == RT_NULL)
  436. {
  437. return -1;
  438. }
  439. return 0;
  440. }
  441. #endif