source: rtems/c/src/lib/libc/libio.c @ cca4400

4.104.114.84.95
Last change on this file since cca4400 was cca4400, checked in by Joel Sherrill <joel.sherrill@…>, on 12/10/98 at 23:31:54

Merged Eric Norum's select patch that was based on 4.0 and resolved
all conflicts.

  • Property mode set to 100644
File size: 7.9 KB
Line 
1/*
2 *  This file contains the support infrastructure used to manage the
3 *  table of integer style file descriptors used by the low level
4 *  POSIX system calls like open(), read, fstat(), etc.
5 *
6 *  COPYRIGHT (c) 1989-1998.
7 *  On-Line Applications Research Corporation (OAR).
8 *  Copyright assigned to U.S. Government, 1994.
9 *
10 *  The license and distribution terms for this file may be
11 *  found in the file LICENSE in this distribution or at
12 *  http://www.OARcorp.com/rtems/license.html.
13 *
14 *  $Id$
15 */
16
17#include "libio_.h"                   /* libio_.h pulls in rtems */
18#include <rtems.h>
19#include <rtems/assoc.h>                /* assoc.h not included by rtems.h */
20
21#include <stdio.h>                      /* O_RDONLY, et.al. */
22#include <fcntl.h>                      /* O_RDONLY, et.al. */
23#include <assert.h>
24#include <errno.h>
25
26#if ! defined(O_NDELAY)
27# if defined(solaris2)
28#  define O_NDELAY O_NONBLOCK
29# elif defined(RTEMS_NEWLIB)
30#  define O_NDELAY _FNBIO
31# endif
32#endif
33
34
35#include <errno.h>
36#include <string.h>                     /* strcmp */
37#include <unistd.h>
38#include <stdlib.h>                     /* calloc() */
39
40#include "libio.h"                      /* libio.h not pulled in by rtems */
41
42/*                           
43 *  File descriptor Table Information
44 */         
45
46extern unsigned32  rtems_libio_number_iops;
47rtems_id           rtems_libio_semaphore;
48rtems_libio_t     *rtems_libio_iops;
49rtems_libio_t     *rtems_libio_last_iop;
50rtems_libio_t     *rtems_libio_iop_freelist;
51
52/*
53 *  External I/O Handlers Table
54 *
55 *  Space for all possible handlers is preallocated
56 *  to speed up dispatch to external handlers.
57 */
58
59rtems_libio_handler_t   rtems_libio_handlers[15];
60
61/*
62 *  Default mode for all files.
63 */
64
65mode_t    rtems_filesystem_umask;
66
67/*
68 *  rtems_register_libio_handler
69 *
70 *  This function registers an external IO handler set.  This lets
71 *  other subsystems have their own versions of many of the system
72 *  calls.  For example, the networking code registers handlers which
73 *  map the system calls for read() and write() to socket calls.
74 *
75 */
76
77void rtems_register_libio_handler(
78  int                          handler_flag,
79  const rtems_libio_handler_t *handler
80)
81{
82  int handler_index = (handler_flag >> LIBIO_FLAGS_HANDLER_SHIFT) - 1;
83
84  if ((handler_index < 0) || (handler_index >= 15))
85    rtems_fatal_error_occurred( RTEMS_INVALID_NUMBER );
86
87  rtems_libio_handlers[handler_index] = *handler;
88}
89
90/*
91 *  rtems_libio_init
92 *
93 *  Called by BSP startup code to initialize the libio subsystem.
94 */
95
96void rtems_libio_init( void )
97{
98    rtems_status_code rc;
99    int i;
100    rtems_libio_t *iop;
101
102    if (rtems_libio_number_iops > 0)
103    {
104        rtems_libio_iops = (rtems_libio_t *) calloc(rtems_libio_number_iops,
105                                                    sizeof(rtems_libio_t));
106        if (rtems_libio_iops == NULL)
107            rtems_fatal_error_occurred(RTEMS_NO_MEMORY);
108
109        iop = rtems_libio_iop_freelist = rtems_libio_iops;
110        for (i = 0 ; i < (rtems_libio_number_iops - 1) ; i++, iop++)
111                iop->data1 = iop + 1;
112        iop->data1 = NULL;
113    }
114
115  /*
116   *  Create the binary semaphore used to provide mutual exclusion
117   *  on the IOP Table.
118   */
119
120  rc = rtems_semaphore_create(
121    RTEMS_LIBIO_SEM,
122    1,
123    RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
124    RTEMS_NO_PRIORITY,
125    &rtems_libio_semaphore
126  );
127  if ( rc != RTEMS_SUCCESSFUL )
128    rtems_fatal_error_occurred( rc );
129
130  /*
131   *  Initialize the base file system infrastructure.
132   */
133
134  rtems_filesystem_initialize();
135}
136
137/*
138 *  rtems_libio_fcntl_flags
139 *
140 *  Convert UNIX fnctl(2) flags to ones that RTEMS drivers understand
141 */
142
143rtems_assoc_t access_modes_assoc[] = {
144  { "READ",       LIBIO_FLAGS_READ,  O_RDONLY },
145  { "WRITE",      LIBIO_FLAGS_WRITE, O_WRONLY },
146  { "READ/WRITE", LIBIO_FLAGS_READ_WRITE, O_RDWR },
147  { 0, 0, 0 },
148};
149
150rtems_assoc_t status_flags_assoc[] = {
151  { "NO DELAY",  LIBIO_FLAGS_NO_DELAY,  O_NDELAY },
152  { "APPEND",    LIBIO_FLAGS_APPEND,    O_APPEND },
153  { "CREATE",    LIBIO_FLAGS_CREATE,    O_CREAT },
154  { 0, 0, 0 },
155};
156
157unsigned32 rtems_libio_fcntl_flags(
158  unsigned32 fcntl_flags
159)
160{
161  unsigned32 flags = 0;
162  unsigned32 access_modes;
163
164  /*
165   * Access mode is a small integer
166   */
167 
168  access_modes = fcntl_flags & O_ACCMODE;
169  fcntl_flags &= ~O_ACCMODE;
170  flags = rtems_assoc_local_by_remote( access_modes_assoc, access_modes );
171
172  /*
173   * Everything else is single bits
174   */
175
176  flags |=
177     rtems_assoc_local_by_remote_bitfield(status_flags_assoc, fcntl_flags);
178  return flags;
179}
180
181/*
182 *  rtems_libio_allocate
183 *
184 *  This routine searches the IOP Table for an unused entry.  If it
185 *  finds one, it returns it.  Otherwise, it returns NULL.
186 */
187
188rtems_libio_t *rtems_libio_allocate( void )
189{
190  rtems_libio_t *iop;
191  rtems_status_code rc;
192 
193  rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
194
195  if (rtems_libio_iop_freelist) {
196    iop = rtems_libio_iop_freelist;
197    rc = rtems_semaphore_create(
198      RTEMS_LIBIO_IOP_SEM(iop - rtems_libio_iops),
199      1,
200      RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
201      RTEMS_NO_PRIORITY,
202      &iop->sem
203    );
204    if (rc != RTEMS_SUCCESSFUL)
205      goto failed;
206    rtems_libio_iop_freelist = iop->data1;
207    iop->data1 = 0;
208    iop->flags = LIBIO_FLAGS_OPEN;
209    goto done;
210  }
211 
212failed:
213  iop = 0;
214 
215done:
216  rtems_semaphore_release( rtems_libio_semaphore );
217  return iop;
218}
219
220/*
221 *  rtems_libio_free
222 *
223 *  This routine frees the resources associated with an IOP (file descriptor)
224 *  and clears the slot in the IOP Table.
225 */
226
227void rtems_libio_free(
228  rtems_libio_t *iop
229)
230{
231  rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
232
233    if (iop->sem)
234      rtems_semaphore_delete(iop->sem);
235
236    iop->data1 = rtems_libio_iop_freelist;
237    rtems_libio_iop_freelist = iop;
238
239  rtems_semaphore_release(rtems_libio_semaphore);
240}
241
242/*
243 *  rtems_libio_is_open_files_in_fs
244 *
245 *  This routine scans the entire file descriptor table to determine if the
246 *  are any active file descriptors that refer to the at least one node in the
247 *  file system that we are trying to dismount.
248 *
249 *  If there is at least one node in the file system referenced by the mount
250 *  table entry a 1 is returned, otherwise a 0 is returned.
251 */
252
253int rtems_libio_is_open_files_in_fs(
254  rtems_filesystem_mount_table_entry_t * fs_mt_entry
255)
256{
257  rtems_libio_t     *iop;
258  int                result = 0;
259
260  rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
261             
262  /*
263   *  Look for any active file descriptor entry.
264   */         
265     
266  for ( iop=rtems_libio_iops ; iop <= rtems_libio_last_iop ; iop++ ) {
267       
268    if ((iop->flags & LIBIO_FLAGS_OPEN) != 0) {
269       
270       /*
271        *  Check if this node is under the file system that we
272        *  are trying to dismount.
273        */
274
275       if ( iop->pathinfo.mt_entry == fs_mt_entry ) {
276          result = 1;
277          break;
278       }
279    }
280  }
281
282  rtems_semaphore_release( rtems_libio_semaphore );
283 
284  return result;
285}
286
287/*
288 *  rtems_libio_is_file_open
289 *
290 *  This routine scans the entire file descriptor table to determine if the
291 *  given file refers to an active file descriptor.
292 *
293 *  If the given file is open a 1 is returned, otherwise a 0 is returned.
294 */
295
296int rtems_libio_is_file_open(
297  void         *node_access
298)
299{
300  rtems_libio_t     *iop;
301  int                result=0;
302
303  rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
304
305  /*
306   *  Look for any active file descriptor entry.
307   */
308 
309  for ( iop=rtems_libio_iops ; iop <= rtems_libio_last_iop ; iop++ ) {
310 
311    if ((iop->flags & LIBIO_FLAGS_OPEN) != 0) {
312 
313       /*
314        *  Check if this node is under the file system that we
315        *  are trying to dismount.
316        */
317
318       if ( iop->pathinfo.node_access == node_access ) {
319          result = 1;
320          break;
321       }
322    }
323  }
324
325  rtems_semaphore_release( rtems_libio_semaphore );
326
327  return result;
328}
329
330
Note: See TracBrowser for help on using the repository browser.