pipe.c 19 KB

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