source: rtems/cpukit/libfs/src/pipe/pipe.c @ 11109ea

4.115
Last change on this file since 11109ea was 11109ea, checked in by Alex Ivanov <alexivanov97@…>, on 12/18/12 at 20:46:38

libfs: Doxygen Enhancement Task #2

http://www.google-melange.com/gci/task/view/google/gci2012/8032207

  • Property mode set to 100644
File size: 1.8 KB
Line 
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 <fcntl.h>
22#include <rtems/libio_.h>
23#include <rtems/seterr.h>
24#include <rtems/pipe.h>
25
26/* Incremental number added to names of anonymous pipe files */
27/* FIXME: This approach is questionable */
28static uint16_t rtems_pipe_no = 0;
29
30int pipe_create(
31  int filsdes[2]
32)
33{
34  rtems_libio_t *iop;
35  int err = 0;
36
37  if (rtems_mkdir("/tmp", S_IRWXU | S_IRWXG | S_IRWXO) != 0)
38    return -1;
39
40  /* /tmp/.fifoXXXX */
41  char fifopath[15];
42  memcpy(fifopath, "/tmp/.fifo", 10);
43  sprintf(fifopath + 10, "%04x", rtems_pipe_no ++);
44
45  /* Try creating FIFO file until find an available file name */
46  while (mkfifo(fifopath, S_IRUSR|S_IWUSR) != 0) {
47    if (errno != EEXIST){
48      return -1;
49    }
50    /* Just try once... */
51    return -1;
52    /* sprintf(fifopath + 10, "%04x", rtems_pipe_no ++); */
53  }
54
55  /* Non-blocking open to avoid waiting for writers */
56  filsdes[0] = open(fifopath, O_RDONLY | O_NONBLOCK);
57  if (filsdes[0] < 0) {
58    err = errno;
59    /* Delete file at errors, or else if pipe is successfully created
60     the file node will be deleted after it is closed by all. */
61    unlink(fifopath);
62  }
63  else {
64  /* Reset open file to blocking mode */
65    iop = rtems_libio_iop(filsdes[0]);
66    iop->flags &= ~LIBIO_FLAGS_NO_DELAY;
67
68    filsdes[1] = open(fifopath, O_WRONLY);
69
70    if (filsdes[1] < 0) {
71    err = errno;
72    close(filsdes[0]);
73    }
74  unlink(fifopath);
75  }
76  if(err != 0)
77    rtems_set_errno_and_return_minus_one(err);
78  return 0;
79}
80
Note: See TracBrowser for help on using the repository browser.