pipe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. #include <stdint.h>
  28. #if defined(RT_USING_POSIX)
  29. #include <dfs_file.h>
  30. #include <dfs_posix.h>
  31. #include <dfs_poll.h>
  32. static int pipe_fops_open(struct dfs_fd *fd)
  33. {
  34. rt_device_t device;
  35. rt_pipe_t *pipe;
  36. pipe = (rt_pipe_t *)fd->data;
  37. if (!pipe) return -1;
  38. device = &(pipe->parent);
  39. rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
  40. if (device->ref_count == 0)
  41. {
  42. pipe->fifo = rt_ringbuffer_create(pipe->bufsz);
  43. }
  44. switch (fd->flags & O_ACCMODE)
  45. {
  46. case O_RDONLY:
  47. pipe->readers ++;
  48. break;
  49. case O_WRONLY:
  50. pipe->writers ++;
  51. break;
  52. case O_RDWR:
  53. pipe->readers ++;
  54. pipe->writers ++;
  55. break;
  56. }
  57. device->ref_count ++;
  58. rt_mutex_release(&(pipe->lock));
  59. return 0;
  60. }
  61. static int pipe_fops_close(struct dfs_fd *fd)
  62. {
  63. rt_device_t device;
  64. rt_pipe_t *pipe;
  65. pipe = (rt_pipe_t *)fd->data;
  66. if (!pipe) return -1;
  67. device = &(pipe->parent);
  68. rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
  69. switch (fd->flags & O_ACCMODE)
  70. {
  71. case O_RDONLY:
  72. pipe->readers --;
  73. break;
  74. case O_WRONLY:
  75. pipe->writers --;
  76. break;
  77. case O_RDWR:
  78. pipe->readers --;
  79. pipe->writers --;
  80. break;
  81. }
  82. if (pipe->writers == 0)
  83. {
  84. rt_wqueue_wakeup(&(pipe->reader_queue), (void*)(POLLIN | POLLERR | POLLHUP));
  85. }
  86. if (pipe->readers == 0)
  87. {
  88. rt_wqueue_wakeup(&(pipe->writer_queue), (void*)(POLLOUT | POLLERR | POLLHUP));
  89. }
  90. if (device->ref_count == 1)
  91. {
  92. rt_ringbuffer_destroy(pipe->fifo);
  93. pipe->fifo = RT_NULL;
  94. }
  95. device->ref_count --;
  96. rt_mutex_release(&(pipe->lock));
  97. return 0;
  98. }
  99. static int pipe_fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
  100. {
  101. rt_pipe_t *pipe;
  102. int ret = 0;
  103. pipe = (rt_pipe_t *)fd->data;
  104. switch (cmd)
  105. {
  106. case FIONREAD:
  107. *((int*)args) = rt_ringbuffer_data_len(pipe->fifo);
  108. break;
  109. case FIONWRITE:
  110. *((int*)args) = rt_ringbuffer_space_len(pipe->fifo);
  111. break;
  112. default:
  113. ret = -EINVAL;
  114. break;
  115. }
  116. return ret;
  117. }
  118. static int pipe_fops_read(struct dfs_fd *fd, void *buf, size_t count)
  119. {
  120. int len = 0;
  121. rt_pipe_t *pipe;
  122. pipe = (rt_pipe_t *)fd->data;
  123. /* no process has the pipe open for writing, return end-of-file */
  124. if (pipe->writers == 0)
  125. return 0;
  126. rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
  127. while (1)
  128. {
  129. if (pipe->writers == 0)
  130. {
  131. goto out;
  132. }
  133. len = rt_ringbuffer_get(pipe->fifo, buf, count);
  134. if (len > 0)
  135. {
  136. break;
  137. }
  138. else
  139. {
  140. if (fd->flags & O_NONBLOCK)
  141. {
  142. len = -EAGAIN;
  143. goto out;
  144. }
  145. rt_mutex_release(&pipe->lock);
  146. rt_wqueue_wakeup(&(pipe->writer_queue), (void*)POLLOUT);
  147. rt_wqueue_wait(&(pipe->reader_queue), 0, -1);
  148. rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
  149. }
  150. }
  151. /* wakeup writer */
  152. rt_wqueue_wakeup(&(pipe->writer_queue), (void*)POLLOUT);
  153. out:
  154. rt_mutex_release(&pipe->lock);
  155. return len;
  156. }
  157. static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
  158. {
  159. int len;
  160. rt_pipe_t *pipe;
  161. int wakeup = 0;
  162. int ret = 0;
  163. uint8_t *pbuf;
  164. pipe = (rt_pipe_t *)fd->data;
  165. if (pipe->readers == 0)
  166. {
  167. ret = -EPIPE;
  168. goto out;
  169. }
  170. if (count == 0)
  171. return 0;
  172. pbuf = (uint8_t*)buf;
  173. rt_mutex_take(&pipe->lock, -1);
  174. while (1)
  175. {
  176. if (pipe->readers == 0)
  177. {
  178. if (ret == 0)
  179. ret = -EPIPE;
  180. break;
  181. }
  182. len = rt_ringbuffer_put(pipe->fifo, pbuf, count - ret);
  183. ret += len;
  184. pbuf += len;
  185. wakeup = 1;
  186. if (ret == count)
  187. {
  188. break;
  189. }
  190. else
  191. {
  192. if (fd->flags & O_NONBLOCK)
  193. {
  194. if (ret == 0)
  195. {
  196. ret = -EAGAIN;
  197. }
  198. break;
  199. }
  200. }
  201. rt_mutex_release(&pipe->lock);
  202. rt_wqueue_wakeup(&(pipe->reader_queue), (void*)POLLIN);
  203. /* pipe full, waiting on suspended write list */
  204. rt_wqueue_wait(&(pipe->writer_queue), 0, -1);
  205. rt_mutex_take(&pipe->lock, -1);
  206. }
  207. rt_mutex_release(&pipe->lock);
  208. if (wakeup)
  209. {
  210. rt_wqueue_wakeup(&(pipe->reader_queue), (void*)POLLIN);
  211. }
  212. out:
  213. return ret;
  214. }
  215. static int pipe_fops_poll(struct dfs_fd *fd, rt_pollreq_t *req)
  216. {
  217. int mask = 0;
  218. rt_pipe_t *pipe;
  219. int mode = 0;
  220. pipe = (rt_pipe_t *)fd->data;
  221. rt_poll_add(&(pipe->reader_queue), req);
  222. rt_poll_add(&(pipe->writer_queue), req);
  223. switch (fd->flags & O_ACCMODE)
  224. {
  225. case O_RDONLY:
  226. mode = 1;
  227. break;
  228. case O_WRONLY:
  229. mode = 2;
  230. break;
  231. case O_RDWR:
  232. mode = 3;
  233. break;
  234. }
  235. if (mode & 1)
  236. {
  237. if (rt_ringbuffer_data_len(pipe->fifo) != 0)
  238. {
  239. mask |= POLLIN;
  240. }
  241. if (pipe->writers == 0)
  242. {
  243. mask |= POLLHUP;
  244. }
  245. }
  246. if (mode & 2)
  247. {
  248. if (rt_ringbuffer_space_len(pipe->fifo) != 0)
  249. {
  250. mask |= POLLOUT;
  251. }
  252. if (pipe->readers == 0)
  253. {
  254. mask |= POLLERR;
  255. }
  256. }
  257. return mask;
  258. }
  259. static const struct dfs_file_ops pipe_fops =
  260. {
  261. pipe_fops_open,
  262. pipe_fops_close,
  263. pipe_fops_ioctl,
  264. pipe_fops_read,
  265. pipe_fops_write,
  266. RT_NULL,
  267. RT_NULL,
  268. RT_NULL,
  269. pipe_fops_poll,
  270. };
  271. #endif /* end of RT_USING_POSIX */
  272. rt_err_t rt_pipe_open (rt_device_t device, rt_uint16_t oflag)
  273. {
  274. rt_pipe_t *pipe = (rt_pipe_t *)device;
  275. if (device == RT_NULL) return -RT_EINVAL;
  276. rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
  277. if (pipe->fifo == RT_NULL)
  278. {
  279. pipe->fifo = rt_ringbuffer_create(pipe->bufsz);
  280. }
  281. rt_mutex_release(&(pipe->lock));
  282. return RT_EOK;
  283. }
  284. rt_err_t rt_pipe_close (rt_device_t device)
  285. {
  286. rt_pipe_t *pipe = (rt_pipe_t *)device;
  287. if (device == RT_NULL) return -RT_EINVAL;
  288. rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
  289. if (device->ref_count == 1)
  290. {
  291. rt_ringbuffer_destroy(pipe->fifo);
  292. pipe->fifo = RT_NULL;
  293. }
  294. rt_mutex_release(&(pipe->lock));
  295. return RT_EOK;
  296. }
  297. rt_size_t rt_pipe_read (rt_device_t device, rt_off_t pos, void *buffer, rt_size_t count)
  298. {
  299. uint8_t *pbuf;
  300. rt_size_t read_bytes = 0;
  301. rt_pipe_t *pipe = (rt_pipe_t *)device;
  302. if (device == RT_NULL)
  303. {
  304. rt_set_errno(-EINVAL);
  305. return 0;
  306. }
  307. if (count == 0) return 0;
  308. pbuf = (uint8_t*)buffer;
  309. rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
  310. while (read_bytes < count)
  311. {
  312. int len = rt_ringbuffer_get(pipe->fifo, &pbuf[read_bytes], count - read_bytes);
  313. if (len <= 0) break;
  314. read_bytes += len;
  315. }
  316. rt_mutex_release(&pipe->lock);
  317. return read_bytes;
  318. }
  319. rt_size_t rt_pipe_write (rt_device_t device, rt_off_t pos, const void *buffer, rt_size_t count)
  320. {
  321. uint8_t *pbuf;
  322. rt_size_t write_bytes = 0;
  323. rt_pipe_t *pipe = (rt_pipe_t *)device;
  324. if (device == RT_NULL)
  325. {
  326. rt_set_errno(-EINVAL);
  327. return 0;
  328. }
  329. if (count == 0) return 0;
  330. pbuf = (uint8_t*)buffer;
  331. rt_mutex_take(&pipe->lock, -1);
  332. while (write_bytes < count)
  333. {
  334. int len = rt_ringbuffer_put(pipe->fifo, &pbuf[write_bytes], count - write_bytes);
  335. if (len <= 0) break;
  336. write_bytes += len;
  337. }
  338. rt_mutex_release(&pipe->lock);
  339. return write_bytes;
  340. }
  341. rt_err_t rt_pipe_control(rt_device_t dev, int cmd, void *args)
  342. {
  343. return RT_EOK;
  344. }
  345. rt_pipe_t *rt_pipe_create(const char *name, int bufsz)
  346. {
  347. rt_pipe_t *pipe;
  348. rt_device_t dev;
  349. pipe = rt_malloc(sizeof(rt_pipe_t));
  350. if (pipe == RT_NULL) return RT_NULL;
  351. rt_memset(pipe, 0, sizeof(rt_pipe_t));
  352. rt_mutex_init(&(pipe->lock), name, RT_IPC_FLAG_FIFO);
  353. rt_list_init(&(pipe->reader_queue));
  354. rt_list_init(&(pipe->writer_queue));
  355. RT_ASSERT(bufsz < 0xFFFF);
  356. pipe->bufsz = bufsz;
  357. dev = &(pipe->parent);
  358. dev->type = RT_Device_Class_Pipe;
  359. dev->init = RT_NULL;
  360. dev->open = rt_pipe_open;
  361. dev->read = rt_pipe_read;
  362. dev->write = rt_pipe_write;
  363. dev->close = rt_pipe_close;
  364. dev->control = rt_pipe_control;
  365. dev->rx_indicate = RT_NULL;
  366. dev->tx_complete = RT_NULL;
  367. if (rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE) != 0)
  368. {
  369. rt_free(pipe);
  370. return RT_NULL;
  371. }
  372. #ifdef RT_USING_POSIX
  373. dev->fops = (void*)&pipe_fops;
  374. #endif
  375. return pipe;
  376. }
  377. int rt_pipe_delete(const char *name)
  378. {
  379. int result = 0;
  380. rt_device_t device;
  381. device = rt_device_find(name);
  382. if (device)
  383. {
  384. if (device->type == RT_Device_Class_Pipe)
  385. {
  386. rt_pipe_t *pipe;
  387. if (device->ref_count != 0)
  388. {
  389. return -RT_EBUSY;
  390. }
  391. pipe = (rt_pipe_t *)device;
  392. rt_mutex_detach(&(pipe->lock));
  393. rt_device_unregister(device);
  394. /* close fifo ringbuffer */
  395. if (pipe->fifo)
  396. {
  397. rt_ringbuffer_destroy(pipe->fifo);
  398. pipe->fifo = RT_NULL;
  399. }
  400. rt_free(pipe);
  401. }
  402. else
  403. {
  404. result = -ENODEV;
  405. }
  406. }
  407. else
  408. {
  409. result = -ENODEV;
  410. }
  411. return result;
  412. }
  413. #ifdef RT_USING_POSIX
  414. int pipe(int fildes[2])
  415. {
  416. rt_pipe_t *pipe;
  417. char dname[8];
  418. char dev_name[32];
  419. static int pipeno = 0;
  420. rt_snprintf(dname, sizeof(dname), "pipe%d", pipeno++);
  421. pipe = rt_pipe_create(dname, PIPE_BUFSZ);
  422. if (pipe == RT_NULL)
  423. {
  424. return -1;
  425. }
  426. rt_snprintf(dev_name, sizeof(dev_name), "/dev/%s", dname);
  427. fildes[0] = open(dev_name, O_RDONLY, 0);
  428. if (fildes[0] < 0)
  429. {
  430. return -1;
  431. }
  432. fildes[1] = open(dev_name, O_WRONLY, 0);
  433. if (fildes[1] < 0)
  434. {
  435. close(fildes[1]);
  436. return -1;
  437. }
  438. return 0;
  439. }
  440. int mkfifo(const char *path, mode_t mode)
  441. {
  442. rt_pipe_t *pipe;
  443. pipe = rt_pipe_create(path, PIPE_BUFSZ);
  444. if (pipe == RT_NULL)
  445. {
  446. return -1;
  447. }
  448. return 0;
  449. }
  450. #endif