source: rtems/cpukit/libfs/src/pipe/pipe.c @ 694c3ac6

4.104.11
Last change on this file since 694c3ac6 was 694c3ac6, checked in by Ralf Corsepius <ralf.corsepius@…>, on Mar 28, 2010 at 3:14:08 AM

Add HAVE_CONFIG_H support to let files receive configure defines.

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