pipe.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-09-30 Bernard first version.
  9. * 2017-11-08 JasonJiaJie fix memory leak issue when close a pipe.
  10. * 2023-06-28 shell return POLLHUP when writer closed its channel on poll()
  11. * fix flag test on pipe_fops_open()
  12. * 2023-12-02 shell Make read pipe operation interruptable.
  13. */
  14. #include <rthw.h>
  15. #include <rtdevice.h>
  16. #include <stdint.h>
  17. #include <sys/errno.h>
  18. #include <ipc/condvar.h>
  19. #if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE)
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <poll.h>
  23. #include <sys/ioctl.h>
  24. #include <dfs_file.h>
  25. #include <resource_id.h>
  26. /* check RT_UNAMED_PIPE_NUMBER */
  27. #ifndef RT_UNAMED_PIPE_NUMBER
  28. #define RT_UNAMED_PIPE_NUMBER 64
  29. #endif
  30. #define BITS(x) _BITS(x)
  31. #define _BITS(x) (sizeof(#x) - 1)
  32. struct check_rt_unamed_pipe_number
  33. {
  34. /* -4 for "pipe" prefix */
  35. /* -1 for '\0' postfix */
  36. char _check[RT_NAME_MAX - 4 - 1 - BITS(RT_UNAMED_PIPE_NUMBER)];
  37. };
  38. /* check end */
  39. static void *resoure_id[RT_UNAMED_PIPE_NUMBER];
  40. static resource_id_t id_mgr = RESOURCE_ID_INIT(RT_UNAMED_PIPE_NUMBER, resoure_id);
  41. /**
  42. * @brief This function will open a pipe.
  43. *
  44. * @param fd is the file descriptor.
  45. *
  46. * @return Return the operation status.
  47. * When the return value is 0, it means the operation is successful.
  48. * When the return value is -1, it means the file descriptor is invalid.
  49. * When the return value is -RT_ENOMEM, it means insufficient memory allocation failed.
  50. */
  51. static int pipe_fops_open(struct dfs_file *fd)
  52. {
  53. int rc = 0;
  54. rt_pipe_t *pipe;
  55. pipe = (rt_pipe_t *)fd->vnode->data;
  56. if (!pipe)
  57. {
  58. return -1;
  59. }
  60. rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
  61. if ((fd->flags & O_ACCMODE) == O_RDONLY)
  62. {
  63. pipe->reader += 1;
  64. }
  65. if ((fd->flags & O_ACCMODE) == O_WRONLY)
  66. {
  67. pipe->writer += 1;
  68. }
  69. if (fd->vnode->ref_count == 1)
  70. {
  71. pipe->fifo = rt_ringbuffer_create(pipe->bufsz);
  72. if (pipe->fifo == RT_NULL)
  73. {
  74. rc = -RT_ENOMEM;
  75. goto __exit;
  76. }
  77. }
  78. if ((fd->flags & O_ACCMODE) == O_RDONLY && !pipe->writer)
  79. {
  80. /* wait for partner */
  81. rc = rt_condvar_timedwait(&pipe->waitfor_parter, &pipe->lock,
  82. RT_INTERRUPTIBLE, RT_WAITING_FOREVER);
  83. if (rc != 0)
  84. {
  85. pipe->reader--;
  86. }
  87. }
  88. else if ((fd->flags & O_ACCMODE) == O_WRONLY)
  89. {
  90. rt_condvar_broadcast(&pipe->waitfor_parter);
  91. }
  92. __exit:
  93. rt_mutex_release(&pipe->lock);
  94. return rc;
  95. }
  96. /**
  97. * @brief This function will close a pipe.
  98. *
  99. * @param fd is the file descriptor.
  100. *
  101. * @return Return the operation status.
  102. * When the return value is 0, it means the operation is successful.
  103. * When the return value is -1, it means the file descriptor is invalid.
  104. */
  105. static int pipe_fops_close(struct dfs_file *fd)
  106. {
  107. rt_device_t device;
  108. rt_pipe_t *pipe;
  109. pipe = (rt_pipe_t *)fd->vnode->data;
  110. if (!pipe)
  111. {
  112. return -1;
  113. }
  114. device = &pipe->parent;
  115. rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
  116. if ((fd->flags & O_RDONLY) == O_RDONLY)
  117. {
  118. pipe->reader -= 1;
  119. }
  120. if ((fd->flags & O_WRONLY) == O_WRONLY)
  121. {
  122. pipe->writer -= 1;
  123. while (!rt_list_isempty(&pipe->reader_queue.waiting_list))
  124. {
  125. rt_wqueue_wakeup(&pipe->reader_queue, (void*)POLLIN);
  126. }
  127. }
  128. if (fd->vnode->ref_count == 1)
  129. {
  130. if (pipe->fifo != RT_NULL)
  131. {
  132. rt_ringbuffer_destroy(pipe->fifo);
  133. }
  134. pipe->fifo = RT_NULL;
  135. }
  136. rt_mutex_release(&pipe->lock);
  137. if (fd->vnode->ref_count == 1 && pipe->is_named == RT_FALSE)
  138. {
  139. /* delete the unamed pipe */
  140. rt_pipe_delete(device->parent.name);
  141. }
  142. return 0;
  143. }
  144. /**
  145. * @brief This function will get the pipe space size depends on the command.
  146. *
  147. * @param fd is the file descriptor.
  148. *
  149. * @param cmd is the command. It determines what data will get.
  150. *
  151. * FIONREAD The command to get the number of bytes in the pipe.
  152. *
  153. * FIONWRITE The command to get the number of bytes can be written to the pipe.
  154. *
  155. * @param args is the pointer to the data to store the read data.
  156. *
  157. * @return Return the operation status.
  158. * When the return value is 0, it means the operation is successful.
  159. * When the return value is -EINVAL, it means the command is invalid.
  160. */
  161. static int pipe_fops_ioctl(struct dfs_file *fd, int cmd, void *args)
  162. {
  163. rt_pipe_t *pipe;
  164. int ret = 0;
  165. pipe = (rt_pipe_t *)fd->vnode->data;
  166. if (args == RT_NULL)
  167. {
  168. return -EINVAL;
  169. }
  170. switch ((rt_ubase_t)cmd)
  171. {
  172. case FIONREAD:
  173. *((int*)args) = rt_ringbuffer_data_len(pipe->fifo);
  174. break;
  175. case FIONWRITE:
  176. *((int*)args) = rt_ringbuffer_space_len(pipe->fifo);
  177. break;
  178. default:
  179. ret = -EINVAL;
  180. break;
  181. }
  182. return ret;
  183. }
  184. /**
  185. * @brief This function will read data from pipe.
  186. *
  187. * @param fd is the file descriptor.
  188. *
  189. * @param buf is the buffer to store the read data.
  190. *
  191. * @param count is the length of data to be read.
  192. *
  193. * @return Return the length of data read.
  194. * When the return value is 0, it means O_NONBLOCK is enabled and there is no thread that has the pipe open for writing.
  195. * When the return value is -EAGAIN, it means there are no data to be read.
  196. */
  197. #ifdef RT_USING_DFS_V2
  198. static ssize_t pipe_fops_read(struct dfs_file *fd, void *buf, size_t count, off_t *pos)
  199. #else
  200. static ssize_t pipe_fops_read(struct dfs_file *fd, void *buf, size_t count)
  201. #endif
  202. {
  203. int len = 0;
  204. rt_pipe_t *pipe;
  205. pipe = (rt_pipe_t *)fd->vnode->data;
  206. /* no process has the pipe open for writing, return end-of-file */
  207. rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
  208. while (1)
  209. {
  210. len = rt_ringbuffer_get(pipe->fifo, buf, count);
  211. if (len > 0 || pipe->writer == 0)
  212. {
  213. break;
  214. }
  215. else
  216. {
  217. if (fd->flags & O_NONBLOCK)
  218. {
  219. len = -EAGAIN;
  220. goto out;
  221. }
  222. rt_mutex_release(&pipe->lock);
  223. rt_wqueue_wakeup(&pipe->writer_queue, (void*)POLLOUT);
  224. if (rt_wqueue_wait_interruptible(&pipe->reader_queue, 0, -1) == -RT_EINTR)
  225. return -EINTR;
  226. rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
  227. }
  228. }
  229. /* wakeup writer */
  230. rt_wqueue_wakeup(&pipe->writer_queue, (void*)POLLOUT);
  231. out:
  232. rt_mutex_release(&pipe->lock);
  233. return len;
  234. }
  235. /**
  236. * @brief This function will write data to pipe.
  237. *
  238. * @param fd is the file descriptor.
  239. *
  240. * @param buf is a pointer to the data buffer to be written.
  241. *
  242. * @param count is the length of data to be write.
  243. *
  244. * @return Return the length of data written.
  245. * When the return value is -EAGAIN, it means O_NONBLOCK is enabled and there are no space to be written.
  246. * When the return value is -EPIPE, it means there is no thread that has the pipe open for reading.
  247. */
  248. #ifdef RT_USING_DFS_V2
  249. static ssize_t pipe_fops_write(struct dfs_file *fd, const void *buf, size_t count, off_t *pos)
  250. #else
  251. static ssize_t pipe_fops_write(struct dfs_file *fd, const void *buf, size_t count)
  252. #endif
  253. {
  254. int len;
  255. rt_pipe_t *pipe;
  256. int wakeup = 0;
  257. int ret = 0;
  258. uint8_t *pbuf;
  259. pipe = (rt_pipe_t *)fd->vnode->data;
  260. if (count == 0)
  261. {
  262. return 0;
  263. }
  264. pbuf = (uint8_t*)buf;
  265. rt_mutex_take(&pipe->lock, -1);
  266. while (1)
  267. {
  268. len = rt_ringbuffer_put(pipe->fifo, pbuf, count - ret);
  269. ret += len;
  270. pbuf += len;
  271. wakeup = 1;
  272. if (ret == count)
  273. {
  274. break;
  275. }
  276. else
  277. {
  278. if (fd->flags & O_NONBLOCK)
  279. {
  280. if (ret == 0)
  281. {
  282. ret = -EAGAIN;
  283. }
  284. break;
  285. }
  286. }
  287. rt_mutex_release(&pipe->lock);
  288. rt_wqueue_wakeup(&pipe->reader_queue, (void*)POLLIN);
  289. /* pipe full, waiting on suspended write list */
  290. if (rt_wqueue_wait_interruptible(&pipe->writer_queue, 0, -1) == -RT_EINTR)
  291. return -EINTR;
  292. rt_mutex_take(&pipe->lock, -1);
  293. }
  294. rt_mutex_release(&pipe->lock);
  295. if (wakeup)
  296. {
  297. rt_wqueue_wakeup(&pipe->reader_queue, (void*)POLLIN);
  298. }
  299. return ret;
  300. }
  301. /**
  302. * @brief This function will get the pipe status.
  303. *
  304. * @param fd is the file descriptor.
  305. *
  306. * @param req is the request type.
  307. *
  308. * @return mask of the pipe status.
  309. * POLLIN means there is data to be read.
  310. * POLLHUP means there is no thread that occupied the pipe to open for writing.
  311. * POLLOUT means there is space to be written.
  312. * POLLERR means there is no thread that occupied the pipe to open for reading.
  313. */
  314. static int pipe_fops_poll(struct dfs_file *fd, rt_pollreq_t *req)
  315. {
  316. int mask = 0;
  317. rt_pipe_t *pipe;
  318. int mode = 0;
  319. pipe = (rt_pipe_t *)fd->vnode->data;
  320. rt_poll_add(&pipe->reader_queue, req);
  321. rt_poll_add(&pipe->writer_queue, req);
  322. switch (fd->flags & O_ACCMODE)
  323. {
  324. case O_RDONLY:
  325. mode = 1;
  326. break;
  327. case O_WRONLY:
  328. mode = 2;
  329. break;
  330. case O_RDWR:
  331. mode = 3;
  332. break;
  333. }
  334. if (mode & 1)
  335. {
  336. if (rt_ringbuffer_data_len(pipe->fifo) != 0)
  337. {
  338. mask |= POLLIN;
  339. }
  340. else if (pipe->writer == 0)
  341. {
  342. mask = POLLHUP;
  343. }
  344. }
  345. if (mode & 2)
  346. {
  347. if (rt_ringbuffer_space_len(pipe->fifo) != 0)
  348. {
  349. mask |= POLLOUT;
  350. }
  351. }
  352. return mask;
  353. }
  354. static const struct dfs_file_ops pipe_fops =
  355. {
  356. .open = pipe_fops_open,
  357. .close = pipe_fops_close,
  358. .ioctl = pipe_fops_ioctl,
  359. .read = pipe_fops_read,
  360. .write = pipe_fops_write,
  361. .poll = pipe_fops_poll,
  362. };
  363. #endif /* defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE) */
  364. /**
  365. * @brief This function will open the pipe and actually creates the pipe buffer.
  366. *
  367. * @param device is a pointer to the pipe device descriptor.
  368. *
  369. * @param oflag is the open method, but it is not used yet.
  370. *
  371. * @return Return the operation status.
  372. * When the return value is RT_EOK, the operation is successful.
  373. * When the return value is -RT_EINVAL, it means the device handle is empty.
  374. * When the return value is -RT_ENOMEM, it means insufficient memory allocation failed.
  375. */
  376. rt_err_t rt_pipe_open(rt_device_t device, rt_uint16_t oflag)
  377. {
  378. rt_pipe_t *pipe = (rt_pipe_t *)device;
  379. rt_err_t ret = RT_EOK;
  380. if (device == RT_NULL)
  381. {
  382. ret = -RT_EINVAL;
  383. goto __exit;
  384. }
  385. rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
  386. if (pipe->fifo == RT_NULL)
  387. {
  388. pipe->fifo = rt_ringbuffer_create(pipe->bufsz);
  389. if (pipe->fifo == RT_NULL)
  390. {
  391. ret = -RT_ENOMEM;
  392. }
  393. }
  394. rt_mutex_release(&pipe->lock);
  395. __exit:
  396. return ret;
  397. }
  398. /**
  399. * @brief This function will close the pipe and release the pipe buffer.
  400. *
  401. * @param device is a pointer to the pipe device descriptor.
  402. *
  403. * @return Return the operation status.
  404. * When the return value is RT_EOK, the operation is successful.
  405. * When the return value is -RT_EINVAL, it means the device handle is empty.
  406. */
  407. rt_err_t rt_pipe_close(rt_device_t device)
  408. {
  409. rt_pipe_t *pipe = (rt_pipe_t *)device;
  410. if (device == RT_NULL)
  411. {
  412. return -RT_EINVAL;
  413. }
  414. rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
  415. rt_ringbuffer_destroy(pipe->fifo);
  416. pipe->fifo = RT_NULL;
  417. rt_mutex_release(&pipe->lock);
  418. return RT_EOK;
  419. }
  420. /**
  421. * @brief This function will read the specified length of data from the pipe.
  422. *
  423. * @param device is a pointer to the pipe device descriptor.
  424. *
  425. * @param pos is a parameter compatible with POSIX standard interface (currently meaningless, just pass in 0).
  426. *
  427. * @param buffer is a pointer to the buffer to store the read data.
  428. *
  429. * @param count is the length of data to be read.
  430. *
  431. * @return Return the length of data read.
  432. * When the return value is 0, it means the pipe device handle is empty or the count is 0.
  433. */
  434. rt_ssize_t rt_pipe_read(rt_device_t device, rt_off_t pos, void *buffer, rt_size_t count)
  435. {
  436. uint8_t *pbuf;
  437. rt_size_t read_bytes = 0;
  438. rt_pipe_t *pipe = (rt_pipe_t *)device;
  439. if (device == RT_NULL)
  440. {
  441. rt_set_errno(-EINVAL);
  442. return 0;
  443. }
  444. if (count == 0)
  445. {
  446. return 0;
  447. }
  448. pbuf = (uint8_t*)buffer;
  449. rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
  450. while (read_bytes < count)
  451. {
  452. int len = rt_ringbuffer_get(pipe->fifo, &pbuf[read_bytes], count - read_bytes);
  453. if (len <= 0)
  454. {
  455. break;
  456. }
  457. read_bytes += len;
  458. }
  459. rt_mutex_release(&pipe->lock);
  460. return read_bytes;
  461. }
  462. /**
  463. * @brief This function will write the specified length of data to the pipe.
  464. *
  465. * @param device is a pointer to the pipe device descriptor.
  466. *
  467. * @param pos is a parameter compatible with POSIX standard interface (currently meaningless, just pass in 0).
  468. *
  469. * @param buffer is a pointer to the data buffer to be written.
  470. *
  471. * @param count is the length of data to be written.
  472. *
  473. * @return Return the length of data written.
  474. * When the return value is 0, it means the pipe device handle is empty or the count is 0.
  475. */
  476. rt_ssize_t rt_pipe_write(rt_device_t device, rt_off_t pos, const void *buffer, rt_size_t count)
  477. {
  478. uint8_t *pbuf;
  479. rt_size_t write_bytes = 0;
  480. rt_pipe_t *pipe = (rt_pipe_t *)device;
  481. if (device == RT_NULL)
  482. {
  483. rt_set_errno(-EINVAL);
  484. return 0;
  485. }
  486. if (count == 0)
  487. {
  488. return 0;
  489. }
  490. pbuf = (uint8_t*)buffer;
  491. rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
  492. while (write_bytes < count)
  493. {
  494. int len = rt_ringbuffer_put(pipe->fifo, &pbuf[write_bytes], count - write_bytes);
  495. if (len <= 0)
  496. {
  497. break;
  498. }
  499. write_bytes += len;
  500. }
  501. rt_mutex_release(&pipe->lock);
  502. return write_bytes;
  503. }
  504. /**
  505. * @brief This function is not used yet.
  506. *
  507. * @param dev is not used yet.
  508. *
  509. * @param cmd is not used yet.
  510. *
  511. * @param args is not used yet.
  512. *
  513. * @return Always return RT_EOK.
  514. */
  515. rt_err_t rt_pipe_control(rt_device_t dev, int cmd, void *args)
  516. {
  517. return RT_EOK;
  518. }
  519. #ifdef RT_USING_DEVICE_OPS
  520. const static struct rt_device_ops pipe_ops =
  521. {
  522. RT_NULL,
  523. rt_pipe_open,
  524. rt_pipe_close,
  525. rt_pipe_read,
  526. rt_pipe_write,
  527. rt_pipe_control,
  528. };
  529. #endif /* RT_USING_DEVICE_OPS */
  530. /**
  531. * @brief This function will initialize a pipe device.
  532. * The system allocates a pipe handle from dynamic heap memory, initializes the pipe handle
  533. * with the specified value, and registers the pipe device with the system.
  534. *
  535. * @param name is the name of pipe device.
  536. *
  537. * @param bufsz is the size of pipe buffer.
  538. *
  539. * @return Return the pointer to the pipe device.
  540. * When the return value is RT_NULL, it means the initialization failed.
  541. */
  542. rt_pipe_t *rt_pipe_create(const char *name, int bufsz)
  543. {
  544. rt_pipe_t *pipe;
  545. rt_device_t dev;
  546. RT_ASSERT(name != RT_NULL);
  547. RT_ASSERT(bufsz < 0xFFFF);
  548. if (rt_device_find(name) != RT_NULL)
  549. {
  550. /* pipe device has been created */
  551. return RT_NULL;
  552. }
  553. pipe = (rt_pipe_t *)rt_malloc(sizeof(rt_pipe_t));
  554. if (pipe == RT_NULL) return RT_NULL;
  555. rt_memset(pipe, 0, sizeof(rt_pipe_t));
  556. pipe->is_named = RT_TRUE; /* initialize as a named pipe */
  557. #if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE)
  558. pipe->pipeno = -1;
  559. #endif
  560. rt_mutex_init(&pipe->lock, name, RT_IPC_FLAG_FIFO);
  561. rt_wqueue_init(&pipe->reader_queue);
  562. rt_wqueue_init(&pipe->writer_queue);
  563. rt_condvar_init(&pipe->waitfor_parter, "piwfp");
  564. pipe->writer = 0;
  565. pipe->reader = 0;
  566. pipe->bufsz = bufsz;
  567. dev = &pipe->parent;
  568. dev->type = RT_Device_Class_Pipe;
  569. #ifdef RT_USING_DEVICE_OPS
  570. dev->ops = &pipe_ops;
  571. #else
  572. dev->init = RT_NULL;
  573. dev->open = rt_pipe_open;
  574. dev->read = rt_pipe_read;
  575. dev->write = rt_pipe_write;
  576. dev->close = rt_pipe_close;
  577. dev->control = rt_pipe_control;
  578. #endif
  579. dev->rx_indicate = RT_NULL;
  580. dev->tx_complete = RT_NULL;
  581. rt_device_register(&pipe->parent, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE);
  582. #if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE)
  583. dev->fops = (void *)&pipe_fops;
  584. #endif
  585. return pipe;
  586. }
  587. /**
  588. * @brief This function will delete a pipe device.
  589. * The system will release the pipe handle and unregister the pipe device from the system.
  590. *
  591. * @param pipe is the pointer to the pipe device.
  592. *
  593. * @return Return the operation status.
  594. * When the return value is 0, it means the operation is successful.
  595. * When the return value is -RT_EINVAL, it means the pipe device is not found or the device isn't a pipe.
  596. * When the return value is -RT_EBUSY, it means the pipe device is busy.
  597. */
  598. int rt_pipe_delete(const char *name)
  599. {
  600. int result = 0;
  601. rt_device_t device;
  602. device = rt_device_find(name);
  603. if (device)
  604. {
  605. if (device->type == RT_Device_Class_Pipe)
  606. {
  607. rt_pipe_t *pipe;
  608. pipe = (rt_pipe_t *)device;
  609. rt_condvar_detach(&pipe->waitfor_parter);
  610. rt_mutex_detach(&pipe->lock);
  611. #if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE)
  612. resource_id_put(&id_mgr, pipe->pipeno);
  613. #endif
  614. rt_device_unregister(device);
  615. /* close fifo ringbuffer */
  616. if (pipe->fifo)
  617. {
  618. rt_ringbuffer_destroy(pipe->fifo);
  619. pipe->fifo = RT_NULL;
  620. }
  621. rt_free(pipe);
  622. }
  623. else
  624. {
  625. result = -ENODEV;
  626. }
  627. }
  628. else
  629. {
  630. result = -ENODEV;
  631. }
  632. return result;
  633. }
  634. #if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE)
  635. /**
  636. * @brief This function will creat a anonymous pipe.
  637. *
  638. * @param fildes[0] is the read handle.
  639. * fildes[1] is the write handle.
  640. *
  641. * @return Return the operation status.
  642. * When the return value is 0, it means the operation is successful.
  643. * When the return value is -1, it means the operation is failed.
  644. */
  645. int pipe(int fildes[2])
  646. {
  647. rt_pipe_t *pipe;
  648. char dname[8];
  649. char dev_name[32];
  650. int pipeno = 0;
  651. pipeno = resource_id_get(&id_mgr);
  652. if (pipeno == -1)
  653. {
  654. return -1;
  655. }
  656. rt_snprintf(dname, sizeof(dname), "pipe%d", pipeno);
  657. pipe = rt_pipe_create(dname, RT_USING_POSIX_PIPE_SIZE);
  658. if (pipe == RT_NULL)
  659. {
  660. resource_id_put(&id_mgr, pipeno);
  661. return -1;
  662. }
  663. pipe->is_named = RT_FALSE; /* unamed pipe */
  664. pipe->pipeno = pipeno;
  665. rt_snprintf(dev_name, sizeof(dev_name), "/dev/%s", dname);
  666. fildes[1] = open(dev_name, O_WRONLY, 0);
  667. if (fildes[1] < 0)
  668. {
  669. rt_pipe_delete(dname);
  670. return -1;
  671. }
  672. fildes[0] = open(dev_name, O_RDONLY, 0);
  673. if (fildes[0] < 0)
  674. {
  675. close(fildes[1]);
  676. rt_pipe_delete(dname);
  677. return -1;
  678. }
  679. return 0;
  680. }
  681. /**
  682. * @brief This function will create a named pipe.
  683. *
  684. * @param path is the name of pipe device.
  685. *
  686. * @param mode is not used yet.
  687. *
  688. * @return Return the operation status.
  689. * When the return value is 0, it means the operation is successful.
  690. * When the return value is -1, it means the operation is failed.
  691. */
  692. int mkfifo(const char *path, mode_t mode)
  693. {
  694. rt_pipe_t *pipe;
  695. pipe = rt_pipe_create(path, RT_USING_POSIX_PIPE_SIZE);
  696. if (pipe == RT_NULL)
  697. {
  698. return -1;
  699. }
  700. return 0;
  701. }
  702. #endif /* defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE) */