pipe.c 19 KB

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