summaryrefslogtreecommitdiffstats
path: root/cpukit/libcsupport/src/libio.c
blob: e89634f090943445abc9b9bf6f94478be95266c7 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
 *  @file
 *
 *  @brief File Descriptor Routines
 *  @ingroup LibIOInternal
 */

/*
 *  COPYRIGHT (c) 1989-1999.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  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.
 */

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

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <rtems.h>
#include <rtems/libio_.h>
#include <rtems/assoc.h>

/* define this to alias O_NDELAY to  O_NONBLOCK, i.e.,
 * O_NDELAY is accepted on input but fcntl(F_GETFL) returns
 * O_NONBLOCK. This is because rtems has no distinction
 * between the two (but some systems have).
 * Note that accepting this alias creates a problem:
 * an application trying to clear the non-blocking flag
 * using a
 *
 *    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NDELAY);
 *
 * does (silently) ignore the operation.
 */
#undef ACCEPT_O_NDELAY_ALIAS

static const rtems_assoc_t access_modes_assoc[] = {
  { "READ",       LIBIO_FLAGS_READ,  O_RDONLY },
  { "WRITE",      LIBIO_FLAGS_WRITE, O_WRONLY },
  { "READ/WRITE", LIBIO_FLAGS_READ_WRITE, O_RDWR },
  { 0, 0, 0 },
};

static const rtems_assoc_t status_flags_assoc[] = {
#ifdef ACCEPT_O_NDELAY_ALIAS
  { "NO DELAY",  LIBIO_FLAGS_NO_DELAY,  O_NDELAY },
#endif
  { "NONBLOCK",  LIBIO_FLAGS_NO_DELAY,  O_NONBLOCK },
  { "APPEND",    LIBIO_FLAGS_APPEND,    O_APPEND },
  { "CREATE",    LIBIO_FLAGS_CREATE,    O_CREAT },
  { 0, 0, 0 },
};

uint32_t rtems_libio_fcntl_flags( int fcntl_flags )
{
  uint32_t   flags = 0;
  uint32_t   access_modes;

  /*
   * Access mode is a small integer
   */

  access_modes = (uint32_t) (fcntl_flags & O_ACCMODE);
  fcntl_flags &= ~O_ACCMODE;
  flags = rtems_assoc_local_by_remote( access_modes_assoc, access_modes );

  /*
   * Everything else is single bits
   */

  flags |= rtems_assoc_local_by_remote_bitfield(
    status_flags_assoc,
    (uint32_t) fcntl_flags
  );

  return flags;
}

int rtems_libio_to_fcntl_flags( uint32_t flags )
{
  int fcntl_flags = 0;

  if ( (flags & LIBIO_FLAGS_READ_WRITE) == LIBIO_FLAGS_READ_WRITE ) {
    fcntl_flags |= O_RDWR;
  } else if ( (flags & LIBIO_FLAGS_READ) == LIBIO_FLAGS_READ) {
    fcntl_flags |= O_RDONLY;
  } else if ( (flags & LIBIO_FLAGS_WRITE) == LIBIO_FLAGS_WRITE) {
    fcntl_flags |= O_WRONLY;
  }

  if ( (flags & LIBIO_FLAGS_NO_DELAY) == LIBIO_FLAGS_NO_DELAY ) {
    fcntl_flags |= O_NONBLOCK;
  }

  if ( (flags & LIBIO_FLAGS_APPEND) == LIBIO_FLAGS_APPEND ) {
    fcntl_flags |= O_APPEND;
  }

  if ( (flags & LIBIO_FLAGS_CREATE) == LIBIO_FLAGS_CREATE ) {
    fcntl_flags |= O_CREAT;
  }

  return fcntl_flags;
}

rtems_libio_t *rtems_libio_allocate( void )
{
  rtems_libio_t *iop = NULL;

  rtems_libio_lock();

  if (rtems_libio_iop_freelist) {
    iop = rtems_libio_iop_freelist;
    rtems_libio_iop_freelist = iop->data1;
    memset( iop, 0, sizeof(*iop) );
    iop->flags = LIBIO_FLAGS_OPEN;
  }

  rtems_libio_unlock();

  return iop;
}

void rtems_libio_free(
  rtems_libio_t *iop
)
{
  rtems_filesystem_location_free( &iop->pathinfo );

  rtems_libio_lock();

    iop->flags = 0;
    /* If the mapping_refcnt is non-zero, the deferred free will be
     * called by munmap. The iop is no longer good to use, but it cannot
     * be recycled until the mapped file is unmapped. deferred free knows
     * it can recycle the iop in case flags == 0 and iop->data1 == iop,
     * since these two conditions are not otherwise satisifed at
     * the same time. It may be possible that iop->data1 == iop when
     * flags != 0 because data1 is private to the driver. However, flags == 0
     * means a freed iop, and an iop on the freelist cannot store a pointer
     * to itself in data1, or else the freelist is corrupted. We can't use
     * NULL in data1 as an indicator because it is used by the tail of the
     * freelist. */
    if ( iop->mapping_refcnt == 0 ) {
      iop->data1 = rtems_libio_iop_freelist;
      rtems_libio_iop_freelist = iop;
    } else {
      iop->data1 = iop;
    }

  rtems_libio_unlock();
}

void rtems_libio_check_deferred_free(
  rtems_libio_t *iop
)
{
  rtems_libio_lock();
    if ( iop->mapping_refcnt == 0 && iop->flags == 0 && iop->data1 == iop) {
      /* No mappings and rtems_libio_free already called, recycle the iop */
      iop->data1 = rtems_libio_iop_freelist;
      rtems_libio_iop_freelist = iop;
    }
  rtems_libio_unlock();
}