pipe.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*
  2. * Copyright (c) 2006-2021, 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. static int pipe_fops_read(struct dfs_file *fd, void *buf, size_t count)
  176. {
  177. int len = 0;
  178. rt_pipe_t *pipe;
  179. pipe = (rt_pipe_t *)fd->vnode->data;
  180. /* no process has the pipe open for writing, return end-of-file */
  181. rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
  182. while (1)
  183. {
  184. len = rt_ringbuffer_get(pipe->fifo, buf, count);
  185. if (len > 0 || pipe->writer == 0)
  186. {
  187. break;
  188. }
  189. else
  190. {
  191. if (fd->flags & O_NONBLOCK)
  192. {
  193. len = -EAGAIN;
  194. goto out;
  195. }
  196. rt_mutex_release(&pipe->lock);
  197. rt_wqueue_wakeup(&pipe->writer_queue, (void*)POLLOUT);
  198. rt_wqueue_wait(&pipe->reader_queue, 0, -1);
  199. rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
  200. }
  201. }
  202. /* wakeup writer */
  203. rt_wqueue_wakeup(&pipe->writer_queue, (void*)POLLOUT);
  204. out:
  205. rt_mutex_release(&pipe->lock);
  206. return len;
  207. }
  208. /**
  209. * @brief This function will write data to pipe.
  210. *
  211. * @param fd is the file descriptor.
  212. *
  213. * @param buf is a pointer to the data buffer to be written.
  214. *
  215. * @param count is the length of data to be write.
  216. *
  217. * @return Return the length of data written.
  218. * When the return value is -EAGAIN, it means O_NONBLOCK is enabled and there are no space to be written.
  219. * When the return value is -EPIPE, it means there is no thread that has the pipe open for reading.
  220. */
  221. static int pipe_fops_write(struct dfs_file *fd, const void *buf, size_t count)
  222. {
  223. int len;
  224. rt_pipe_t *pipe;
  225. int wakeup = 0;
  226. int ret = 0;
  227. uint8_t *pbuf;
  228. pipe = (rt_pipe_t *)fd->vnode->data;
  229. if (count == 0)
  230. {
  231. return 0;
  232. }
  233. pbuf = (uint8_t*)buf;
  234. rt_mutex_take(&pipe->lock, -1);
  235. while (1)
  236. {
  237. len = rt_ringbuffer_put(pipe->fifo, pbuf, count - ret);
  238. ret += len;
  239. pbuf += len;
  240. wakeup = 1;
  241. if (ret == count)
  242. {
  243. break;
  244. }
  245. else
  246. {
  247. if (fd->flags & O_NONBLOCK)
  248. {
  249. if (ret == 0)
  250. {
  251. ret = -EAGAIN;
  252. }
  253. break;
  254. }
  255. }
  256. rt_mutex_release(&pipe->lock);
  257. rt_wqueue_wakeup(&pipe->reader_queue, (void*)POLLIN);
  258. /* pipe full, waiting on suspended write list */
  259. rt_wqueue_wait(&pipe->writer_queue, 0, -1);
  260. rt_mutex_take(&pipe->lock, -1);
  261. }
  262. rt_mutex_release(&pipe->lock);
  263. if (wakeup)
  264. {
  265. rt_wqueue_wakeup(&pipe->reader_queue, (void*)POLLIN);
  266. }
  267. return ret;
  268. }
  269. /**
  270. * @brief This function will get the pipe status.
  271. *
  272. * @param fd is the file descriptor.
  273. *
  274. * @param req is the request type.
  275. *
  276. * @return mask of the pipe status.
  277. * POLLIN means there is data to be read.
  278. * POLLHUP means there is no thread that occupied the pipe to open for writing.
  279. * POLLOUT means there is space to be written.
  280. * POLLERR means there is no thread that occupied the pipe to open for reading.
  281. */
  282. static int pipe_fops_poll(struct dfs_file *fd, rt_pollreq_t *req)
  283. {
  284. int mask = 0;
  285. rt_pipe_t *pipe;
  286. int mode = 0;
  287. pipe = (rt_pipe_t *)fd->vnode->data;
  288. rt_poll_add(&pipe->reader_queue, req);
  289. rt_poll_add(&pipe->writer_queue, req);
  290. switch (fd->flags & O_ACCMODE)
  291. {
  292. case O_RDONLY:
  293. mode = 1;
  294. break;
  295. case O_WRONLY:
  296. mode = 2;
  297. break;
  298. case O_RDWR:
  299. mode = 3;
  300. break;
  301. }
  302. if (mode & 1)
  303. {
  304. if (rt_ringbuffer_data_len(pipe->fifo) != 0)
  305. {
  306. mask |= POLLIN;
  307. }
  308. }
  309. if (mode & 2)
  310. {
  311. if (rt_ringbuffer_space_len(pipe->fifo) != 0)
  312. {
  313. mask |= POLLOUT;
  314. }
  315. }
  316. return mask;
  317. }
  318. static const struct dfs_file_ops pipe_fops =
  319. {
  320. pipe_fops_open,
  321. pipe_fops_close,
  322. pipe_fops_ioctl,
  323. pipe_fops_read,
  324. pipe_fops_write,
  325. RT_NULL,
  326. RT_NULL,
  327. RT_NULL,
  328. pipe_fops_poll,
  329. };
  330. #endif /* defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE) */
  331. /**
  332. * @brief This function will open the pipe and actually creates the pipe buffer.
  333. *
  334. * @param device is a pointer to the pipe device descriptor.
  335. *
  336. * @param oflag is the open method, but it is not used yet.
  337. *
  338. * @return Return the operation status.
  339. * When the return value is RT_EOK, the operation is successful.
  340. * When the return value is -RT_EINVAL, it means the device handle is empty.
  341. * When the return value is -RT_ENOMEM, it means insufficient memory allocation failed.
  342. */
  343. rt_err_t rt_pipe_open(rt_device_t device, rt_uint16_t oflag)
  344. {
  345. rt_pipe_t *pipe = (rt_pipe_t *)device;
  346. rt_err_t ret = RT_EOK;
  347. if (device == RT_NULL)
  348. {
  349. ret = -RT_EINVAL;
  350. goto __exit;
  351. }
  352. rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
  353. if (pipe->fifo == RT_NULL)
  354. {
  355. pipe->fifo = rt_ringbuffer_create(pipe->bufsz);
  356. if (pipe->fifo == RT_NULL)
  357. {
  358. ret = -RT_ENOMEM;
  359. }
  360. }
  361. rt_mutex_release(&pipe->lock);
  362. __exit:
  363. return ret;
  364. }
  365. /**
  366. * @brief This function will close the pipe and release the pipe buffer.
  367. *
  368. * @param device is a pointer to the pipe device descriptor.
  369. *
  370. * @return Return the operation status.
  371. * When the return value is RT_EOK, the operation is successful.
  372. * When the return value is -RT_EINVAL, it means the device handle is empty.
  373. */
  374. rt_err_t rt_pipe_close(rt_device_t device)
  375. {
  376. rt_pipe_t *pipe = (rt_pipe_t *)device;
  377. if (device == RT_NULL)
  378. {
  379. return -RT_EINVAL;
  380. }
  381. rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
  382. rt_ringbuffer_destroy(pipe->fifo);
  383. pipe->fifo = RT_NULL;
  384. rt_mutex_release(&pipe->lock);
  385. return RT_EOK;
  386. }
  387. /**
  388. * @brief This function will read the specified length of data from the pipe.
  389. *
  390. * @param device is a pointer to the pipe device descriptor.
  391. *
  392. * @param pos is a parameter compatible with POSIX standard interface (currently meaningless, just pass in 0).
  393. *
  394. * @param buffer is a pointer to the buffer to store the read data.
  395. *
  396. * @param count is the length of data to be read.
  397. *
  398. * @return Return the length of data read.
  399. * When the return value is 0, it means the pipe device handle is empty or the count is 0.
  400. */
  401. rt_ssize_t rt_pipe_read(rt_device_t device, rt_off_t pos, void *buffer, rt_size_t count)
  402. {
  403. uint8_t *pbuf;
  404. rt_size_t read_bytes = 0;
  405. rt_pipe_t *pipe = (rt_pipe_t *)device;
  406. if (device == RT_NULL)
  407. {
  408. rt_set_errno(-EINVAL);
  409. return 0;
  410. }
  411. if (count == 0)
  412. {
  413. return 0;
  414. }
  415. pbuf = (uint8_t*)buffer;
  416. rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
  417. while (read_bytes < count)
  418. {
  419. int len = rt_ringbuffer_get(pipe->fifo, &pbuf[read_bytes], count - read_bytes);
  420. if (len <= 0)
  421. {
  422. break;
  423. }
  424. read_bytes += len;
  425. }
  426. rt_mutex_release(&pipe->lock);
  427. return read_bytes;
  428. }
  429. /**
  430. * @brief This function will write the specified length of data to the pipe.
  431. *
  432. * @param device is a pointer to the pipe device descriptor.
  433. *
  434. * @param pos is a parameter compatible with POSIX standard interface (currently meaningless, just pass in 0).
  435. *
  436. * @param buffer is a pointer to the data buffer to be written.
  437. *
  438. * @param count is the length of data to be written.
  439. *
  440. * @return Return the length of data written.
  441. * When the return value is 0, it means the pipe device handle is empty or the count is 0.
  442. */
  443. rt_ssize_t rt_pipe_write(rt_device_t device, rt_off_t pos, const void *buffer, rt_size_t count)
  444. {
  445. uint8_t *pbuf;
  446. rt_size_t write_bytes = 0;
  447. rt_pipe_t *pipe = (rt_pipe_t *)device;
  448. if (device == RT_NULL)
  449. {
  450. rt_set_errno(-EINVAL);
  451. return 0;
  452. }
  453. if (count == 0)
  454. {
  455. return 0;
  456. }
  457. pbuf = (uint8_t*)buffer;
  458. rt_mutex_take(&pipe->lock, -1);
  459. while (write_bytes < count)
  460. {
  461. int len = rt_ringbuffer_put(pipe->fifo, &pbuf[write_bytes], count - write_bytes);
  462. if (len <= 0)
  463. {
  464. break;
  465. }
  466. write_bytes += len;
  467. }
  468. rt_mutex_release(&pipe->lock);
  469. return write_bytes;
  470. }
  471. /**
  472. * @brief This function is not used yet.
  473. *
  474. * @param dev is not used yet.
  475. *
  476. * @param cmd is not used yet.
  477. *
  478. * @param args is not used yet.
  479. *
  480. * @return Always return RT_EOK.
  481. */
  482. rt_err_t rt_pipe_control(rt_device_t dev, int cmd, void *args)
  483. {
  484. return RT_EOK;
  485. }
  486. #ifdef RT_USING_DEVICE_OPS
  487. const static struct rt_device_ops pipe_ops =
  488. {
  489. RT_NULL,
  490. rt_pipe_open,
  491. rt_pipe_close,
  492. rt_pipe_read,
  493. rt_pipe_write,
  494. rt_pipe_control,
  495. };
  496. #endif /* RT_USING_DEVICE_OPS */
  497. /**
  498. * @brief This function will initialize a pipe device.
  499. * The system allocates a pipe handle from dynamic heap memory, initializes the pipe handle
  500. * with the specified value, and registers the pipe device with the system.
  501. *
  502. * @param name is the name of pipe device.
  503. *
  504. * @param bufsz is the size of pipe buffer.
  505. *
  506. * @return Return the pointer to the pipe device.
  507. * When the return value is RT_NULL, it means the initialization failed.
  508. */
  509. rt_pipe_t *rt_pipe_create(const char *name, int bufsz)
  510. {
  511. rt_pipe_t *pipe;
  512. rt_device_t dev;
  513. pipe = (rt_pipe_t *)rt_malloc(sizeof(rt_pipe_t));
  514. if (pipe == RT_NULL) return RT_NULL;
  515. rt_memset(pipe, 0, sizeof(rt_pipe_t));
  516. pipe->is_named = RT_TRUE; /* initialize as a named pipe */
  517. #if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE)
  518. pipe->pipeno = -1;
  519. #endif
  520. rt_mutex_init(&pipe->lock, name, RT_IPC_FLAG_FIFO);
  521. rt_wqueue_init(&pipe->reader_queue);
  522. rt_wqueue_init(&pipe->writer_queue);
  523. pipe->writer = 0;
  524. pipe->reader = 0;
  525. RT_ASSERT(bufsz < 0xFFFF);
  526. pipe->bufsz = bufsz;
  527. dev = &pipe->parent;
  528. dev->type = RT_Device_Class_Pipe;
  529. #ifdef RT_USING_DEVICE_OPS
  530. dev->ops = &pipe_ops;
  531. #else
  532. dev->init = RT_NULL;
  533. dev->open = rt_pipe_open;
  534. dev->read = rt_pipe_read;
  535. dev->write = rt_pipe_write;
  536. dev->close = rt_pipe_close;
  537. dev->control = rt_pipe_control;
  538. #endif
  539. dev->rx_indicate = RT_NULL;
  540. dev->tx_complete = RT_NULL;
  541. if (rt_device_register(&pipe->parent, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE) != 0)
  542. {
  543. rt_mutex_detach(&pipe->lock);
  544. #if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE)
  545. resource_id_put(&id_mgr, pipe->pipeno);
  546. #endif
  547. rt_free(pipe);
  548. return RT_NULL;
  549. }
  550. #if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE)
  551. dev->fops = (void *)&pipe_fops;
  552. #endif
  553. return pipe;
  554. }
  555. /**
  556. * @brief This function will delete a pipe device.
  557. * The system will release the pipe handle and unregister the pipe device from the system.
  558. *
  559. * @param pipe is the pointer to the pipe device.
  560. *
  561. * @return Return the operation status.
  562. * When the return value is 0, it means the operation is successful.
  563. * When the return value is -RT_EINVAL, it means the pipe device is not found or the device isn't a pipe.
  564. * When the return value is -RT_EBUSY, it means the pipe device is busy.
  565. */
  566. int rt_pipe_delete(const char *name)
  567. {
  568. int result = 0;
  569. rt_device_t device;
  570. device = rt_device_find(name);
  571. if (device)
  572. {
  573. if (device->type == RT_Device_Class_Pipe)
  574. {
  575. rt_pipe_t *pipe;
  576. pipe = (rt_pipe_t *)device;
  577. rt_mutex_detach(&pipe->lock);
  578. #if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE)
  579. resource_id_put(&id_mgr, pipe->pipeno);
  580. #endif
  581. rt_device_unregister(device);
  582. /* close fifo ringbuffer */
  583. if (pipe->fifo)
  584. {
  585. rt_ringbuffer_destroy(pipe->fifo);
  586. pipe->fifo = RT_NULL;
  587. }
  588. rt_free(pipe);
  589. }
  590. else
  591. {
  592. result = -ENODEV;
  593. }
  594. }
  595. else
  596. {
  597. result = -ENODEV;
  598. }
  599. return result;
  600. }
  601. #if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE)
  602. /**
  603. * @brief This function will creat a anonymous pipe.
  604. *
  605. * @param fildes[0] is the read handle.
  606. * fildes[1] is the write handle.
  607. *
  608. * @return Return the operation status.
  609. * When the return value is 0, it means the operation is successful.
  610. * When the return value is -1, it means the operation is failed.
  611. */
  612. int pipe(int fildes[2])
  613. {
  614. rt_pipe_t *pipe;
  615. char dname[8];
  616. char dev_name[32];
  617. int pipeno = 0;
  618. pipeno = resource_id_get(&id_mgr);
  619. if (pipeno == -1)
  620. {
  621. return -1;
  622. }
  623. rt_snprintf(dname, sizeof(dname), "pipe%d", pipeno);
  624. pipe = rt_pipe_create(dname, RT_USING_POSIX_PIPE_SIZE);
  625. if (pipe == RT_NULL)
  626. {
  627. resource_id_put(&id_mgr, pipeno);
  628. return -1;
  629. }
  630. pipe->is_named = RT_FALSE; /* unamed pipe */
  631. pipe->pipeno = pipeno;
  632. rt_snprintf(dev_name, sizeof(dev_name), "/dev/%s", dname);
  633. fildes[0] = open(dev_name, O_RDONLY, 0);
  634. if (fildes[0] < 0)
  635. {
  636. return -1;
  637. }
  638. fildes[1] = open(dev_name, O_WRONLY, 0);
  639. if (fildes[1] < 0)
  640. {
  641. close(fildes[0]);
  642. return -1;
  643. }
  644. return 0;
  645. }
  646. /**
  647. * @brief This function will create a named pipe.
  648. *
  649. * @param path is the name of pipe device.
  650. *
  651. * @param mode is not used yet.
  652. *
  653. * @return Return the operation status.
  654. * When the return value is 0, it means the operation is successful.
  655. * When the return value is -1, it means the operation is failed.
  656. */
  657. int mkfifo(const char *path, mode_t mode)
  658. {
  659. rt_pipe_t *pipe;
  660. pipe = rt_pipe_create(path, RT_USING_POSIX_PIPE_SIZE);
  661. if (pipe == RT_NULL)
  662. {
  663. return -1;
  664. }
  665. return 0;
  666. }
  667. #endif /* defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE) */