diff options
author | Joel Sherrill <joel.sherrill@OARcorp.com> | 2008-10-14 15:06:25 +0000 |
---|---|---|
committer | Joel Sherrill <joel.sherrill@OARcorp.com> | 2008-10-14 15:06:25 +0000 |
commit | e2324c081a4b1a0c6d7fdaaa99e0d5ef29325e20 (patch) | |
tree | 828a80e48298c57d4b897d3b9a74f9ade00bbdf1 /cpukit/libfs/src/pipe/pipe.c | |
parent | Fix formatting. (diff) | |
download | rtems-e2324c081a4b1a0c6d7fdaaa99e0d5ef29325e20.tar.bz2 |
2008-10-14 Wei Shen <cquark@gmail.com>
* Makefile.am, preinstall.am, libcsupport/src/mknod.c,
libcsupport/src/open.c, libcsupport/src/pipe.c, libfs/Makefile.am,
libfs/preinstall.am, libfs/src/imfs/imfs.h,
libfs/src/imfs/imfs_creat.c, libfs/src/imfs/imfs_debug.c,
libfs/src/imfs/imfs_eval.c, libfs/src/imfs/imfs_initsupp.c,
libfs/src/imfs/imfs_mknod.c, libfs/src/imfs/imfs_stat.c,
libfs/src/imfs/memfile.c: Initial commit of POSIX pipe support.
* libfs/src/imfs/imfs_fifo.c, libfs/src/pipe/fifo.c,
libfs/src/pipe/pipe.c, libfs/src/pipe/pipe.h: New files.
Diffstat (limited to 'cpukit/libfs/src/pipe/pipe.c')
-rw-r--r-- | cpukit/libfs/src/pipe/pipe.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/cpukit/libfs/src/pipe/pipe.c b/cpukit/libfs/src/pipe/pipe.c new file mode 100644 index 0000000000..339aa42f4e --- /dev/null +++ b/cpukit/libfs/src/pipe/pipe.c @@ -0,0 +1,83 @@ +/* + * pipe.c: anonymous pipe + * + * Author: Wei Shen <cquark@gmail.com> + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + */ + +#include <stdio.h> +#include <fcntl.h> +#include <rtems/libio_.h> +#include <rtems/seterr.h> + +/* Incremental number added to names of anonymous pipe files */ +uint16_t rtems_pipe_no = 0; + +/* + * Called by pipe() to create an anonymous pipe. + */ +int pipe_create( + int filsdes[2] +) +{ + rtems_filesystem_location_info_t loc; + rtems_libio_t *iop; + int err = 0; + + /* Create /tmp if not exists */ + if (rtems_filesystem_evaluate_path("/tmp", RTEMS_LIBIO_PERMS_RWX, &loc, TRUE) + != 0) { + if (errno != ENOENT) + return -1; + if (mkdir("/tmp", S_IRWXU|S_IRWXG|S_IRWXO|S_ISVTX) != 0) + return -1; + } + else + rtems_filesystem_freenode(&loc); + + /* /tmp/.fifoXXXX */ + char fifopath[15]; + memcpy(fifopath, "/tmp/.fifo", 10); + sprintf(fifopath + 10, "%04x", rtems_pipe_no ++); + + /* Try creating FIFO file until find an available file name */ + while (mkfifo(fifopath, S_IRUSR|S_IWUSR) != 0) { + if (errno != EEXIST) + return -1; + /* Just try once... */ + return -1; + sprintf(fifopath + 10, "%04x", rtems_pipe_no ++); + } + + /* Non-blocking open to avoid waiting for writers */ + filsdes[0] = open(fifopath, O_RDONLY | O_NONBLOCK); + if (filsdes[0] < 0) { + err = errno; + goto out; + } + + /* Reset open file to blocking mode */ + iop = rtems_libio_iop(filsdes[0]); + iop->flags &= ~LIBIO_FLAGS_NO_DELAY; + + filsdes[1] = open(fifopath, O_WRONLY); + + if (filsdes[1] < 0) { + err = errno; + close(filsdes[0]); + } + +out: + /* Delete file at errors, or else if pipe is successfully created + the file node will be deleted after it is closed by all. */ + unlink(fifopath); + + if (! err) + return 0; + rtems_set_errno_and_return_minus_one(err); +} |