summaryrefslogtreecommitdiffstats
path: root/cpukit/libfs/src/pipe/pipe.c
blob: e46e8f58e757cd0ded52fdec1a872422e58d3273 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
 * @file
 *
 * @brief Create an Anonymous Pipe
 * @ingroup FIFO_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.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <rtems/libio_.h>
#include <rtems/seterr.h>
#include <rtems/pipe.h>

/* Incremental number added to names of anonymous pipe files */
/* FIXME: This approach is questionable */
static uint16_t rtems_pipe_no = 0;

int pipe_create(
  int filsdes[2]
)
{
  rtems_libio_t *iop;
  int err = 0;

  if (rtems_mkdir("/tmp", S_IRWXU | S_IRWXG | S_IRWXO) != 0)
    return -1;

  /* /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;
    /* 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);
  }
  else {
  /* 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]);
    }
  unlink(fifopath);
  }
  if(err != 0)
    rtems_set_errno_and_return_minus_one(err);
  return 0;
}