source: rtems/cpukit/libfs/src/pipe/pipe.c @ 44be50c

4.104.115
Last change on this file since 44be50c was 44be50c, checked in by Joel Sherrill <joel.sherrill@…>, on 12/28/09 at 16:36:08

2009-12-28 Shrikant Gaikwad <n3oo3n@…>

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