1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Create an Anonymous Pipe |
---|
5 | * @ingroup FIFO_PIPE |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * Author: Wei Shen <cquark@gmail.com> |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.com/license/LICENSE. |
---|
14 | */ |
---|
15 | |
---|
16 | #ifdef HAVE_CONFIG_H |
---|
17 | #include "config.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | #include <stdio.h> |
---|
21 | #include <string.h> |
---|
22 | #include <fcntl.h> |
---|
23 | #include <rtems/libio_.h> |
---|
24 | #include <rtems/seterr.h> |
---|
25 | #include <rtems/pipe.h> |
---|
26 | |
---|
27 | /* Incremental number added to names of anonymous pipe files */ |
---|
28 | /* FIXME: This approach is questionable */ |
---|
29 | static uint16_t rtems_pipe_no = 0; |
---|
30 | |
---|
31 | int pipe_create( |
---|
32 | int filsdes[2] |
---|
33 | ) |
---|
34 | { |
---|
35 | rtems_libio_t *iop; |
---|
36 | int err = 0; |
---|
37 | |
---|
38 | if (rtems_mkdir("/tmp", S_IRWXU | S_IRWXG | S_IRWXO) != 0) |
---|
39 | return -1; |
---|
40 | |
---|
41 | /* /tmp/.fifoXXXX */ |
---|
42 | char fifopath[15]; |
---|
43 | memcpy(fifopath, "/tmp/.fifo", 10); |
---|
44 | sprintf(fifopath + 10, "%04x", rtems_pipe_no ++); |
---|
45 | |
---|
46 | /* Try creating FIFO file until find an available file name */ |
---|
47 | while (mkfifo(fifopath, S_IRUSR|S_IWUSR) != 0) { |
---|
48 | if (errno != EEXIST){ |
---|
49 | return -1; |
---|
50 | } |
---|
51 | /* Just try once... */ |
---|
52 | return -1; |
---|
53 | /* sprintf(fifopath + 10, "%04x", rtems_pipe_no ++); */ |
---|
54 | } |
---|
55 | |
---|
56 | /* Non-blocking open to avoid waiting for writers */ |
---|
57 | filsdes[0] = open(fifopath, O_RDONLY | O_NONBLOCK); |
---|
58 | if (filsdes[0] < 0) { |
---|
59 | err = errno; |
---|
60 | /* Delete file at errors, or else if pipe is successfully created |
---|
61 | the file node will be deleted after it is closed by all. */ |
---|
62 | unlink(fifopath); |
---|
63 | } |
---|
64 | else { |
---|
65 | /* Reset open file to blocking mode */ |
---|
66 | iop = rtems_libio_iop(filsdes[0]); |
---|
67 | iop->flags &= ~LIBIO_FLAGS_NO_DELAY; |
---|
68 | |
---|
69 | filsdes[1] = open(fifopath, O_WRONLY); |
---|
70 | |
---|
71 | if (filsdes[1] < 0) { |
---|
72 | err = errno; |
---|
73 | close(filsdes[0]); |
---|
74 | } |
---|
75 | unlink(fifopath); |
---|
76 | } |
---|
77 | if(err != 0) |
---|
78 | rtems_set_errno_and_return_minus_one(err); |
---|
79 | return 0; |
---|
80 | } |
---|
81 | |
---|