1 | /** |
---|
2 | * @file rtems/libio_.h |
---|
3 | * |
---|
4 | * This file is the libio internal interface. |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * COPYRIGHT (c) 1989-2011. |
---|
9 | * On-Line Applications Research Corporation (OAR). |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.com/license/LICENSE. |
---|
14 | * |
---|
15 | * $Id$ |
---|
16 | */ |
---|
17 | |
---|
18 | #ifndef _RTEMS_RTEMS_LIBIO__H |
---|
19 | #define _RTEMS_RTEMS_LIBIO__H |
---|
20 | |
---|
21 | #include <rtems.h> |
---|
22 | #include <rtems/libio.h> /* include before standard IO */ |
---|
23 | |
---|
24 | #include <sys/types.h> |
---|
25 | |
---|
26 | #include <errno.h> |
---|
27 | |
---|
28 | #ifdef __cplusplus |
---|
29 | extern "C" { |
---|
30 | #endif |
---|
31 | |
---|
32 | /* |
---|
33 | * Semaphore to protect the io table |
---|
34 | */ |
---|
35 | |
---|
36 | #define RTEMS_LIBIO_SEM rtems_build_name('L', 'B', 'I', 'O') |
---|
37 | #define RTEMS_LIBIO_IOP_SEM(n) rtems_build_name('L', 'B', 'I', n) |
---|
38 | |
---|
39 | extern rtems_id rtems_libio_semaphore; |
---|
40 | |
---|
41 | /* |
---|
42 | * File descriptor Table Information |
---|
43 | */ |
---|
44 | |
---|
45 | extern uint32_t rtems_libio_number_iops; |
---|
46 | extern rtems_libio_t *rtems_libio_iops; |
---|
47 | extern rtems_libio_t *rtems_libio_last_iop; |
---|
48 | extern rtems_libio_t *rtems_libio_iop_freelist; |
---|
49 | |
---|
50 | /* |
---|
51 | * rtems_libio_iop |
---|
52 | * |
---|
53 | * Macro to return the file descriptor pointer. |
---|
54 | */ |
---|
55 | |
---|
56 | #define rtems_libio_iop(_fd) \ |
---|
57 | ((((uint32_t)(_fd)) < rtems_libio_number_iops) ? \ |
---|
58 | &rtems_libio_iops[_fd] : 0) |
---|
59 | |
---|
60 | /* |
---|
61 | * rtems_libio_iop_to_descriptor |
---|
62 | * |
---|
63 | * Macro to convert an internal file descriptor pointer (iop) into |
---|
64 | * the integer file descriptor used by the "section 2" system calls. |
---|
65 | */ |
---|
66 | |
---|
67 | #define rtems_libio_iop_to_descriptor(_iop) \ |
---|
68 | ((!(_iop)) ? -1 : (_iop - rtems_libio_iops)) |
---|
69 | |
---|
70 | /* |
---|
71 | * rtems_libio_check_is_open |
---|
72 | * |
---|
73 | * Macro to check if a file descriptor is actually open. |
---|
74 | */ |
---|
75 | |
---|
76 | #define rtems_libio_check_is_open(_iop) \ |
---|
77 | do { \ |
---|
78 | if (((_iop)->flags & LIBIO_FLAGS_OPEN) == 0) { \ |
---|
79 | errno = EBADF; \ |
---|
80 | return -1; \ |
---|
81 | } \ |
---|
82 | } while (0) |
---|
83 | |
---|
84 | /* |
---|
85 | * rtems_libio_check_fd |
---|
86 | * |
---|
87 | * Macro to check if a file descriptor number is valid. |
---|
88 | */ |
---|
89 | |
---|
90 | #define rtems_libio_check_fd(_fd) \ |
---|
91 | do { \ |
---|
92 | if ((uint32_t) (_fd) >= rtems_libio_number_iops) { \ |
---|
93 | errno = EBADF; \ |
---|
94 | return -1; \ |
---|
95 | } \ |
---|
96 | } while (0) |
---|
97 | |
---|
98 | /* |
---|
99 | * rtems_libio_check_buffer |
---|
100 | * |
---|
101 | * Macro to check if a buffer pointer is valid. |
---|
102 | */ |
---|
103 | |
---|
104 | #define rtems_libio_check_buffer(_buffer) \ |
---|
105 | do { \ |
---|
106 | if ((_buffer) == 0) { \ |
---|
107 | errno = EINVAL; \ |
---|
108 | return -1; \ |
---|
109 | } \ |
---|
110 | } while (0) |
---|
111 | |
---|
112 | /* |
---|
113 | * rtems_libio_check_count |
---|
114 | * |
---|
115 | * Macro to check if a count or length is valid. |
---|
116 | */ |
---|
117 | |
---|
118 | #define rtems_libio_check_count(_count) \ |
---|
119 | do { \ |
---|
120 | if ((_count) == 0) { \ |
---|
121 | return 0; \ |
---|
122 | } \ |
---|
123 | } while (0) |
---|
124 | |
---|
125 | /* |
---|
126 | * rtems_libio_check_permissions_with_error |
---|
127 | * |
---|
128 | * Macro to check if a file descriptor is open for this operation. |
---|
129 | * On failure, return the user specified error. |
---|
130 | */ |
---|
131 | |
---|
132 | #define rtems_libio_check_permissions_with_error(_iop, _flag, _errno) \ |
---|
133 | do { \ |
---|
134 | if (((_iop)->flags & (_flag)) == 0) { \ |
---|
135 | rtems_set_errno_and_return_minus_one( _errno ); \ |
---|
136 | return -1; \ |
---|
137 | } \ |
---|
138 | } while (0) |
---|
139 | |
---|
140 | /* |
---|
141 | * rtems_libio_check_permissions |
---|
142 | * |
---|
143 | * Macro to check if a file descriptor is open for this operation. |
---|
144 | * On failure, return EINVAL |
---|
145 | */ |
---|
146 | |
---|
147 | #define rtems_libio_check_permissions(_iop, _flag) \ |
---|
148 | rtems_libio_check_permissions_with_error(_iop, _flag, EINVAL ) |
---|
149 | |
---|
150 | /* |
---|
151 | * rtems_filesystem_freenode |
---|
152 | * |
---|
153 | * Macro to free a node. |
---|
154 | */ |
---|
155 | |
---|
156 | void rtems_filesystem_freenode( rtems_filesystem_location_info_t* node ); |
---|
157 | |
---|
158 | /* |
---|
159 | * External structures |
---|
160 | */ |
---|
161 | #include <rtems/userenv.h> |
---|
162 | |
---|
163 | extern rtems_user_env_t * rtems_current_user_env; |
---|
164 | extern rtems_user_env_t rtems_global_user_env; |
---|
165 | |
---|
166 | /* |
---|
167 | * Instantiate a private copy of the per user information for the calling task. |
---|
168 | */ |
---|
169 | |
---|
170 | rtems_status_code rtems_libio_set_private_env(void); |
---|
171 | rtems_status_code rtems_libio_share_private_env(rtems_id task_id) ; |
---|
172 | |
---|
173 | static inline void rtems_libio_lock( void ) |
---|
174 | { |
---|
175 | rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT ); |
---|
176 | } |
---|
177 | |
---|
178 | static inline void rtems_libio_unlock( void ) |
---|
179 | { |
---|
180 | rtems_semaphore_release( rtems_libio_semaphore ); |
---|
181 | } |
---|
182 | |
---|
183 | /* |
---|
184 | * File Descriptor Routine Prototypes |
---|
185 | */ |
---|
186 | |
---|
187 | rtems_libio_t *rtems_libio_allocate(void); |
---|
188 | |
---|
189 | uint32_t rtems_libio_fcntl_flags( |
---|
190 | uint32_t fcntl_flags |
---|
191 | ); |
---|
192 | |
---|
193 | uint32_t rtems_libio_to_fcntl_flags( |
---|
194 | uint32_t flags |
---|
195 | ); |
---|
196 | |
---|
197 | void rtems_libio_free( |
---|
198 | rtems_libio_t *iop |
---|
199 | ); |
---|
200 | |
---|
201 | int rtems_libio_is_open_files_in_fs( |
---|
202 | rtems_filesystem_mount_table_entry_t *mt_entry |
---|
203 | ); |
---|
204 | |
---|
205 | int rtems_libio_is_file_open( |
---|
206 | void *node_access |
---|
207 | ); |
---|
208 | |
---|
209 | /* |
---|
210 | * File System Routine Prototypes |
---|
211 | */ |
---|
212 | |
---|
213 | int rtems_filesystem_evaluate_relative_path( |
---|
214 | const char *pathname, |
---|
215 | size_t pathnamelen, |
---|
216 | int flags, |
---|
217 | rtems_filesystem_location_info_t *pathloc, |
---|
218 | int follow_link |
---|
219 | ); |
---|
220 | |
---|
221 | int rtems_filesystem_evaluate_path( |
---|
222 | const char *pathname, |
---|
223 | size_t pathnamelen, |
---|
224 | int flags, |
---|
225 | rtems_filesystem_location_info_t *pathloc, |
---|
226 | int follow_link |
---|
227 | ); |
---|
228 | |
---|
229 | int rtems_filesystem_dirname( |
---|
230 | const char *pathname |
---|
231 | ); |
---|
232 | |
---|
233 | int rtems_filesystem_prefix_separators( |
---|
234 | const char *pathname, |
---|
235 | int pathnamelen |
---|
236 | ); |
---|
237 | |
---|
238 | void rtems_filesystem_initialize(void); |
---|
239 | |
---|
240 | int init_fs_mount_table(void); |
---|
241 | |
---|
242 | int rtems_filesystem_is_separator(char ch); |
---|
243 | |
---|
244 | void rtems_filesystem_get_start_loc(const char *path, |
---|
245 | int *index, |
---|
246 | rtems_filesystem_location_info_t *loc); |
---|
247 | |
---|
248 | void rtems_filesystem_get_sym_start_loc(const char *path, |
---|
249 | int *index, |
---|
250 | rtems_filesystem_location_info_t *loc); |
---|
251 | |
---|
252 | static inline bool rtems_filesystem_is_root_location( |
---|
253 | const rtems_filesystem_location_info_t *loc |
---|
254 | ) |
---|
255 | { |
---|
256 | return loc->mt_entry->mt_fs_root.node_access == loc->node_access; |
---|
257 | } |
---|
258 | |
---|
259 | #ifdef __cplusplus |
---|
260 | } |
---|
261 | #endif |
---|
262 | |
---|
263 | #endif |
---|
264 | /* end of include file */ |
---|