source: rtems/c/src/lib/libc/libio_.h @ ee733965

4.104.114.84.95
Last change on this file since ee733965 was 2d733c42, checked in by Joel Sherrill <joel.sherrill@…>, on 01/20/99 at 15:48:22

More general fix based on bug report and patch from Ian Lance Taylor
<ian@…> to fix this problem:

There is a small bug in rtems_close in c/src/lib/libc/libio.c. It
does not check whether the file descriptor it is passed is open. This
can cause it to make a null dereference if it is passed a file
descriptor which is in the valid range but which was not opened, or
which was already closed.

  • Property mode set to 100644
File size: 6.7 KB
Line 
1/*
2 *  Libio Internal Information
3 *
4 *  COPYRIGHT (c) 1989-1998.
5 *  On-Line Applications Research Corporation (OAR).
6 *  Copyright assigned to U.S. Government, 1994.
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.OARcorp.com/rtems/license.html.
11 *
12 *  $Id$
13 */
14
15#ifndef __LIBIO__h
16#define __LIBIO__h
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22#include <rtems.h>
23#include <rtems/assoc.h>                /* assoc.h not included by rtems.h */
24#include <rtems/libio.h>
25
26#include <stdio.h>                      /* O_RDONLY, et.al. */
27#include <fcntl.h>                      /* O_RDONLY, et.al. */
28#include <assert.h>
29#include <stdarg.h>
30#include <errno.h>
31
32#if ! defined(O_NDELAY)
33# if defined(solaris2)
34#  define O_NDELAY O_NONBLOCK
35# elif defined(RTEMS_NEWLIB)
36#  define O_NDELAY _FNBIO
37# endif
38#endif
39
40#if !defined(ENOTSUP)
41#define ENOTSUP EOPNOTSUPP
42#endif
43
44#include <errno.h>
45#include <string.h>                     /* strcmp */
46#include <unistd.h>
47#include <stdlib.h>                     /* calloc() */
48
49#include "libio.h"                      /* libio.h not pulled in by rtems */
50
51
52/*
53 *  Semaphore to protect the io table
54 */
55
56#define RTEMS_LIBIO_SEM         rtems_build_name('L', 'B', 'I', 'O')
57#define RTEMS_LIBIO_IOP_SEM(n)  rtems_build_name('L', 'B', 'I', n)
58
59extern rtems_id        rtems_libio_semaphore;
60
61/*
62 *  File descriptor Table Information
63 */
64
65extern unsigned32      rtems_libio_number_iops;
66extern rtems_libio_t  *rtems_libio_iops;
67extern rtems_libio_t  *rtems_libio_last_iop;
68extern rtems_libio_t *rtems_libio_iop_freelist;
69
70/*
71 *  External I/O Handlers Table
72 *
73 *  Space for all possible handlers is preallocated
74 *  to speed up dispatch to external handlers.
75 */
76
77extern rtems_libio_handler_t   rtems_libio_handlers[15];
78
79/*
80 *  Default mode for all files.
81 */
82
83extern mode_t    rtems_filesystem_umask;
84
85/*
86 *  set_errno_and_return_minus_one
87 *
88 *  Macro to ease common way to return an error.
89 */
90
91#ifndef set_errno_and_return_minus_one
92#define set_errno_and_return_minus_one( _error ) \
93  do { errno = (_error); return -1; } while(0)
94#endif
95
96/*
97 *  rtems_libio_iop
98 *
99 *  Macro to return the file descriptor pointer.
100 */
101
102#define rtems_libio_iop(_fd) \
103  ((((unsigned32)(_fd)) < rtems_libio_number_iops) ? \
104         &rtems_libio_iops[_fd] : 0)
105
106/* 
107 *  rtems_libio_check_is_open
108 * 
109 *  Macro to check if a file descriptor is actually open.
110 */
111
112#define rtems_libio_check_is_open(_iop) \
113  do {                                               \
114      if (((_iop)->flags & LIBIO_FLAGS_OPEN) == 0) { \
115          errno = EBADF;                             \
116          return -1;                                 \
117      }                                              \
118  } while (0)
119
120/*
121 *  rtems_libio_check_fd
122 *
123 *  Macro to check if a file descriptor number is valid.
124 */
125
126#define rtems_libio_check_fd(_fd) \
127  do {                                                     \
128      if ((unsigned32) (_fd) >= rtems_libio_number_iops) { \
129          errno = EBADF;                                   \
130          return -1;                                       \
131      }                                                    \
132  } while (0)
133
134/*
135 *  rtems_libio_check_buffer
136 *
137 *  Macro to check if a buffer pointer is valid.
138 */
139
140#define rtems_libio_check_buffer(_buffer) \
141  do {                                    \
142      if ((_buffer) == 0) {               \
143          errno = EINVAL;                 \
144          return -1;                      \
145      }                                   \
146  } while (0)
147
148/*
149 *  rtems_libio_check_count
150 *
151 *  Macro to check if a count or length is valid.
152 */
153
154#define rtems_libio_check_count(_count) \
155  do {                                  \
156      if ((_count) == 0) {              \
157          return 0;                     \
158      }                                 \
159  } while (0)
160
161/*
162 *  rtems_libio_check_permissions
163 *
164 *  Macro to check if a file descriptor is open for this operation.
165 */
166
167#define rtems_libio_check_permissions(_iop, _flag)    \
168  do {                                                \
169      if (((_iop)->flags & (_flag)) == 0) {           \
170            set_errno_and_return_minus_one( EINVAL ); \
171            return -1;                                \
172      }                                               \
173  } while (0)
174
175/*
176 *  rtems_filesystem_is_separator
177 *
178 *  Macro to determine if a character is a path name separator.
179 *
180 *  NOTE:  This macro handles MS-DOS and UNIX style names.
181 */
182
183#define rtems_filesystem_is_separator( _ch ) \
184   ( ((_ch) == '/') || ((_ch) == '\\') || ((_ch) == '\0'))
185
186/*
187 *  rtems_filesystem_get_start_loc
188 *
189 *  Macro to determine if path is absolute or relative.
190 */
191
192#define rtems_filesystem_get_start_loc( _path, _index, _loc )  \
193  do {                                                         \
194    if ( rtems_filesystem_is_separator( (_path)[ 0 ] ) ) {     \
195      *(_loc) = rtems_filesystem_root;                         \
196      *(_index) = 1;                                           \
197    } else {                                                   \
198      *(_loc) = rtems_filesystem_current;                      \
199      *(_index) = 0;                                           \
200    }                                                          \
201  } while (0)
202
203#define rtems_filesystem_get_sym_start_loc( _path, _index, _loc )  \
204  do {                                                         \
205    if ( rtems_filesystem_is_separator( (_path)[ 0 ] ) ) {     \
206      *(_loc) = rtems_filesystem_root;                         \
207      *(_index) = 1;                                           \
208    } else {                                                   \
209      *(_index) = 0;                                           \
210    }                                                          \
211  } while (0)
212
213
214/*
215 *  External structures
216 */
217
218extern rtems_filesystem_location_info_t rtems_filesystem_current;
219extern rtems_filesystem_location_info_t rtems_filesystem_root;
220extern nlink_t                          rtems_filesystem_link_counts;
221
222
223/*
224 *  File Descriptor Routine Prototypes
225 */
226
227rtems_libio_t *rtems_libio_allocate(void);
228
229unsigned32 rtems_libio_fcntl_flags(
230  unsigned32 fcntl_flags
231);
232
233void rtems_libio_free(
234  rtems_libio_t *iop
235);
236
237int rtems_libio_is_open_files_in_fs(
238  rtems_filesystem_mount_table_entry_t *mt_entry
239);
240
241int rtems_libio_is_file_open(
242  void  *node_access
243);
244
245/*
246 *  File System Routine Prototypes
247 */
248
249int rtems_filesystem_evaluate_path(
250  const char                        *pathname,
251  int                                flags,
252  rtems_filesystem_location_info_t  *pathloc,
253  int                                follow_link
254);
255
256void rtems_filesystem_initialize();
257
258int init_fs_mount_table();
259
260#ifdef __cplusplus
261}
262#endif
263
264#endif
265/* end of include file */
266
267
268
Note: See TracBrowser for help on using the repository browser.