1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// fd.cxx
|
4 |
|
|
//
|
5 |
|
|
// Fileio file descriptor implementation
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): nickg
|
43 |
|
|
// Contributors: nickg
|
44 |
|
|
// Date: 2000-05-25
|
45 |
|
|
// Purpose: Fileio file descriptor implementation
|
46 |
|
|
// Description: This file contains the implementation of the file
|
47 |
|
|
// descriptor functions.
|
48 |
|
|
//
|
49 |
|
|
//
|
50 |
|
|
//
|
51 |
|
|
//####DESCRIPTIONEND####
|
52 |
|
|
//
|
53 |
|
|
//==========================================================================
|
54 |
|
|
|
55 |
|
|
#include <pkgconf/hal.h>
|
56 |
|
|
#include <pkgconf/io_fileio.h>
|
57 |
|
|
|
58 |
|
|
#ifdef CYGPKG_KERNEL
|
59 |
|
|
#include <pkgconf/kernel.h>
|
60 |
|
|
#include <cyg/kernel/ktypes.h> // base kernel types
|
61 |
|
|
#endif
|
62 |
|
|
#include <cyg/infra/cyg_trac.h> // tracing macros
|
63 |
|
|
#include <cyg/infra/cyg_ass.h> // assertion macros
|
64 |
|
|
|
65 |
|
|
#include "fio.h" // Private header
|
66 |
|
|
|
67 |
|
|
//-----------------------------------------------------------------------------
|
68 |
|
|
// File data structures
|
69 |
|
|
|
70 |
|
|
#ifdef CYGPKG_KERNEL
|
71 |
|
|
// Mutex for controlling access to file desriptor arrays
|
72 |
|
|
Cyg_Mutex fdlock CYGBLD_ATTRIB_INIT_PRI(CYG_INIT_IO_FS);
|
73 |
|
|
|
74 |
|
|
// Array of per-file mutexes
|
75 |
|
|
static Cyg_Mutex file_lock[CYGNUM_FILEIO_NFILE] \
|
76 |
|
|
CYGBLD_ATTRIB_INIT_PRI(CYG_INIT_IO_FS);
|
77 |
|
|
#endif // ifdef CYGPKG_KERNEL
|
78 |
|
|
|
79 |
|
|
// Array of open file objects
|
80 |
|
|
static cyg_file file[CYGNUM_FILEIO_NFILE];
|
81 |
|
|
|
82 |
|
|
// Descriptor array
|
83 |
|
|
static cyg_file *desc[CYGNUM_FILEIO_NFD];
|
84 |
|
|
|
85 |
|
|
#define FD_ALLOCATED ((cyg_file *)1)
|
86 |
|
|
|
87 |
|
|
//==========================================================================
|
88 |
|
|
// Initialization
|
89 |
|
|
|
90 |
|
|
__externC void cyg_fd_init()
|
91 |
|
|
{
|
92 |
|
|
int i;
|
93 |
|
|
|
94 |
|
|
for( i = 0; i < CYGNUM_FILEIO_NFILE; i++ )
|
95 |
|
|
file[i].f_flag = 0;
|
96 |
|
|
|
97 |
|
|
for( i = 0; i < CYGNUM_FILEIO_NFD; i++ )
|
98 |
|
|
desc[i] = NULL;
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
//==========================================================================
|
102 |
|
|
// File object allocation
|
103 |
|
|
|
104 |
|
|
//--------------------------------------------------------------------------
|
105 |
|
|
// Locate and allocate a free file object.
|
106 |
|
|
|
107 |
|
|
__externC cyg_file *cyg_file_alloc()
|
108 |
|
|
{
|
109 |
|
|
int i;
|
110 |
|
|
cyg_file *fp = NULL;
|
111 |
|
|
|
112 |
|
|
FILEIO_MUTEX_LOCK(fdlock);
|
113 |
|
|
|
114 |
|
|
for( i = 0; i < CYGNUM_FILEIO_NFILE; i++ )
|
115 |
|
|
{
|
116 |
|
|
if( (file[i].f_flag & CYG_FALLOC) == 0 )
|
117 |
|
|
{
|
118 |
|
|
fp = &file[i];
|
119 |
|
|
fp->f_flag = CYG_FALLOC;
|
120 |
|
|
fp->f_ucount = 0;
|
121 |
|
|
break;
|
122 |
|
|
}
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
FILEIO_MUTEX_UNLOCK(fdlock);
|
126 |
|
|
|
127 |
|
|
return fp;
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
//--------------------------------------------------------------------------
|
131 |
|
|
// Free a file object. This is a straightforward freeing, usually used
|
132 |
|
|
// during error recovery. File objects are normally freed as a side
|
133 |
|
|
// effect of cyg_fd_assign() or cyg_fd_free().
|
134 |
|
|
|
135 |
|
|
__externC void cyg_file_free(cyg_file * fp)
|
136 |
|
|
{
|
137 |
|
|
FILEIO_MUTEX_LOCK(fdlock);
|
138 |
|
|
|
139 |
|
|
fp->f_flag = 0;
|
140 |
|
|
|
141 |
|
|
FILEIO_MUTEX_UNLOCK(fdlock);
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
//==========================================================================
|
145 |
|
|
// Internal routines for handling descriptor deallocation
|
146 |
|
|
// These must all be called with the fdlock already locked.
|
147 |
|
|
|
148 |
|
|
//--------------------------------------------------------------------------
|
149 |
|
|
// Decrement the use count on a file object and if it goes to zero,
|
150 |
|
|
// close the file and deallocate the file object.
|
151 |
|
|
//
|
152 |
|
|
// A word on locking here: It is necessary for the filesystem
|
153 |
|
|
// fo_close() function to be called with the file lock claimed, but
|
154 |
|
|
// the fdlock released, to permit other threads to perform fd-related
|
155 |
|
|
// operations. The original code here took the file lock and released
|
156 |
|
|
// the fdlock before the call and then locked the fdlock and released
|
157 |
|
|
// the file lock after. The idea was that there was no point at which
|
158 |
|
|
// a lock of some sort was not held. However, if two threads are
|
159 |
|
|
// running through this code simultaneously, this could lead to
|
160 |
|
|
// deadlock, particularly if the filesystem's syncmode specifies fstab
|
161 |
|
|
// or mtab level locking. So the code now unlocks the file lock before
|
162 |
|
|
// reclaiming the fdlock. This leaves a small window where no locks
|
163 |
|
|
// are held, where in theory some other thread could jump in and mess
|
164 |
|
|
// things up. However, this is benign; if the other thread is
|
165 |
|
|
// accessing some other file object there will be no conflict and by
|
166 |
|
|
// definition no other thread can access this file object since we are
|
167 |
|
|
// executing here because no file descriptors point to this file
|
168 |
|
|
// object any longer. Additionally, the file object is only marked
|
169 |
|
|
// free, by zeroing the f_flag field, once the fdlock has been
|
170 |
|
|
// reclaimed.
|
171 |
|
|
|
172 |
|
|
static int fp_ucount_dec( cyg_file *fp )
|
173 |
|
|
{
|
174 |
|
|
int error = 0;
|
175 |
|
|
if( (--fp->f_ucount) <= 0 )
|
176 |
|
|
{
|
177 |
|
|
cyg_file_lock( fp, fp->f_syncmode );
|
178 |
|
|
FILEIO_MUTEX_UNLOCK(fdlock);
|
179 |
|
|
|
180 |
|
|
error = fp->f_ops->fo_close(fp);
|
181 |
|
|
|
182 |
|
|
cyg_file_unlock( fp, fp->f_syncmode );
|
183 |
|
|
FILEIO_MUTEX_LOCK(fdlock);
|
184 |
|
|
|
185 |
|
|
fp->f_flag = 0;
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
return error;
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
//--------------------------------------------------------------------------
|
192 |
|
|
// Clear out a descriptor. If this is the last reference to the file
|
193 |
|
|
// object, then that will be closed and deallocated.
|
194 |
|
|
|
195 |
|
|
static int fd_close( int fd )
|
196 |
|
|
{
|
197 |
|
|
int error = 0;
|
198 |
|
|
cyg_file *fp;
|
199 |
|
|
|
200 |
|
|
CYG_ASSERT(((0 <= fd) && (fd<CYGNUM_FILEIO_NFD)), "fd out of range");
|
201 |
|
|
|
202 |
|
|
fp = desc[fd];
|
203 |
|
|
desc[fd] = FD_ALLOCATED;
|
204 |
|
|
|
205 |
|
|
if( fp != FD_ALLOCATED && fp != NULL)
|
206 |
|
|
{
|
207 |
|
|
// The descriptor is occupied, decrement its usecount and
|
208 |
|
|
// close the file if it goes zero.
|
209 |
|
|
|
210 |
|
|
error = fp_ucount_dec( fp );
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
return error;
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
|
217 |
|
|
//==========================================================================
|
218 |
|
|
// File descriptor allocation
|
219 |
|
|
|
220 |
|
|
//--------------------------------------------------------------------------
|
221 |
|
|
// Allocate a file descriptor. The allocated descriptor is set to the value
|
222 |
|
|
// FD_ALLOCATED to prevent it being reallocated by another thread.
|
223 |
|
|
|
224 |
|
|
__externC int cyg_fd_alloc(int low)
|
225 |
|
|
{
|
226 |
|
|
int fd;
|
227 |
|
|
|
228 |
|
|
CYG_ASSERT(((0 <= low) && (low<CYGNUM_FILEIO_NFD)),"fd out of range");
|
229 |
|
|
|
230 |
|
|
FILEIO_MUTEX_LOCK(fdlock);
|
231 |
|
|
|
232 |
|
|
for( fd = low; fd < CYGNUM_FILEIO_NFD; fd++ )
|
233 |
|
|
{
|
234 |
|
|
if( desc[fd] == NULL )
|
235 |
|
|
{
|
236 |
|
|
desc[fd] = FD_ALLOCATED;
|
237 |
|
|
FILEIO_MUTEX_UNLOCK(fdlock);
|
238 |
|
|
return fd;
|
239 |
|
|
}
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
FILEIO_MUTEX_UNLOCK(fdlock);
|
243 |
|
|
|
244 |
|
|
return -1;
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
//--------------------------------------------------------------------------
|
248 |
|
|
// Assign a file object to a descriptor. If the descriptor is already
|
249 |
|
|
// occupied, the occupying files usecount is decrement and it may be
|
250 |
|
|
// closed.
|
251 |
|
|
|
252 |
|
|
__externC void cyg_fd_assign(int fd, cyg_file *fp)
|
253 |
|
|
{
|
254 |
|
|
|
255 |
|
|
CYG_ASSERT(((0 <= fd) && (fd<CYGNUM_FILEIO_NFD)),"fd out of range");
|
256 |
|
|
|
257 |
|
|
FILEIO_MUTEX_LOCK(fdlock);
|
258 |
|
|
|
259 |
|
|
fd_close( fd );
|
260 |
|
|
|
261 |
|
|
fp->f_ucount++;
|
262 |
|
|
desc[fd] = fp;
|
263 |
|
|
|
264 |
|
|
FILEIO_MUTEX_UNLOCK(fdlock);
|
265 |
|
|
}
|
266 |
|
|
|
267 |
|
|
//--------------------------------------------------------------------------
|
268 |
|
|
// Free a descriptor. Any occupying files usecount is decremented and
|
269 |
|
|
// it may be closed.
|
270 |
|
|
|
271 |
|
|
__externC int cyg_fd_free(int fd)
|
272 |
|
|
{
|
273 |
|
|
int error;
|
274 |
|
|
|
275 |
|
|
CYG_ASSERT(((0 <= fd) && (fd<CYGNUM_FILEIO_NFD)),"fd out of range");
|
276 |
|
|
|
277 |
|
|
FILEIO_MUTEX_LOCK(fdlock);
|
278 |
|
|
|
279 |
|
|
error = fd_close( fd );
|
280 |
|
|
|
281 |
|
|
desc[fd] = NULL;
|
282 |
|
|
|
283 |
|
|
FILEIO_MUTEX_UNLOCK(fdlock);
|
284 |
|
|
|
285 |
|
|
return error;
|
286 |
|
|
}
|
287 |
|
|
|
288 |
|
|
//==========================================================================
|
289 |
|
|
// Descriptor to file object mapping
|
290 |
|
|
|
291 |
|
|
|
292 |
|
|
//--------------------------------------------------------------------------
|
293 |
|
|
// Map a descriptor to a file object. This is just a straightforward index
|
294 |
|
|
// into the descriptor array complicated by the need to lock the mutex and
|
295 |
|
|
// increment the usecount.
|
296 |
|
|
|
297 |
|
|
__externC cyg_file *cyg_fp_get( int fd )
|
298 |
|
|
{
|
299 |
|
|
CYG_ASSERT(((0 <= fd) && (fd<CYGNUM_FILEIO_NFD)),"fd out of range");
|
300 |
|
|
|
301 |
|
|
FILEIO_MUTEX_LOCK(fdlock);
|
302 |
|
|
|
303 |
|
|
cyg_file *fp = desc[fd];
|
304 |
|
|
|
305 |
|
|
if( fp != FD_ALLOCATED && fp != NULL)
|
306 |
|
|
{
|
307 |
|
|
// Increment use count while we work on this file
|
308 |
|
|
|
309 |
|
|
fp->f_ucount++;
|
310 |
|
|
}
|
311 |
|
|
else fp = NULL;
|
312 |
|
|
|
313 |
|
|
FILEIO_MUTEX_UNLOCK(fdlock);
|
314 |
|
|
|
315 |
|
|
return fp;
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
//--------------------------------------------------------------------------
|
319 |
|
|
// Free the usecount reference we acquired in cyg_fp_get(). If the usecount
|
320 |
|
|
// is zeroed, the file will be closed.
|
321 |
|
|
|
322 |
|
|
__externC void cyg_fp_free( cyg_file *fp )
|
323 |
|
|
{
|
324 |
|
|
FILEIO_MUTEX_LOCK(fdlock);
|
325 |
|
|
|
326 |
|
|
fp_ucount_dec( fp );
|
327 |
|
|
|
328 |
|
|
FILEIO_MUTEX_UNLOCK(fdlock);
|
329 |
|
|
}
|
330 |
|
|
|
331 |
|
|
//==========================================================================
|
332 |
|
|
// File locking protocol
|
333 |
|
|
|
334 |
|
|
void cyg_file_lock( cyg_file *fp , cyg_uint32 syncmode )
|
335 |
|
|
{
|
336 |
|
|
cyg_fs_lock( fp->f_mte, syncmode>>CYG_SYNCMODE_IO_SHIFT);
|
337 |
|
|
|
338 |
|
|
if( syncmode & CYG_SYNCMODE_IO_FILE )
|
339 |
|
|
{
|
340 |
|
|
fp->f_flag |= CYG_FLOCKED;
|
341 |
|
|
FILEIO_MUTEX_LOCK(file_lock[fp-&file[0]]);
|
342 |
|
|
}
|
343 |
|
|
}
|
344 |
|
|
|
345 |
|
|
void cyg_file_unlock( cyg_file *fp, cyg_uint32 syncmode )
|
346 |
|
|
{
|
347 |
|
|
cyg_fs_unlock( fp->f_mte, syncmode>>CYG_SYNCMODE_IO_SHIFT);
|
348 |
|
|
|
349 |
|
|
if( syncmode & CYG_SYNCMODE_IO_FILE )
|
350 |
|
|
{
|
351 |
|
|
fp->f_flag &= ~CYG_FLOCKED;
|
352 |
|
|
FILEIO_MUTEX_UNLOCK(file_lock[fp-&file[0]]);
|
353 |
|
|
}
|
354 |
|
|
}
|
355 |
|
|
|
356 |
|
|
|
357 |
|
|
//==========================================================================
|
358 |
|
|
// POSIX API routines
|
359 |
|
|
|
360 |
|
|
//--------------------------------------------------------------------------
|
361 |
|
|
// dup() - duplicate an FD into a random descriptor
|
362 |
|
|
|
363 |
|
|
__externC int dup( int fd )
|
364 |
|
|
{
|
365 |
|
|
cyg_file *fp = cyg_fp_get( fd );
|
366 |
|
|
|
367 |
|
|
if( fp == NULL )
|
368 |
|
|
{
|
369 |
|
|
errno = EBADF;
|
370 |
|
|
return -1;
|
371 |
|
|
}
|
372 |
|
|
|
373 |
|
|
int fd2 = cyg_fd_alloc(0);
|
374 |
|
|
|
375 |
|
|
if( fd2 == -1 )
|
376 |
|
|
{
|
377 |
|
|
errno = EMFILE;
|
378 |
|
|
return -1;
|
379 |
|
|
}
|
380 |
|
|
|
381 |
|
|
cyg_fd_assign( fd2, fp );
|
382 |
|
|
|
383 |
|
|
cyg_fp_free(fp);
|
384 |
|
|
|
385 |
|
|
return fd2;
|
386 |
|
|
}
|
387 |
|
|
|
388 |
|
|
//--------------------------------------------------------------------------
|
389 |
|
|
// dup2() - duplicate an FD into a chosen descriptor
|
390 |
|
|
|
391 |
|
|
__externC int dup2( int fd, int fd2 )
|
392 |
|
|
{
|
393 |
|
|
if( fd2 == fd ) return fd2;
|
394 |
|
|
|
395 |
|
|
if( fd2 < 0 || fd2 >= OPEN_MAX )
|
396 |
|
|
{
|
397 |
|
|
errno = EBADF;
|
398 |
|
|
return -1;
|
399 |
|
|
}
|
400 |
|
|
|
401 |
|
|
cyg_file *fp = cyg_fp_get( fd );
|
402 |
|
|
|
403 |
|
|
if( fp == NULL )
|
404 |
|
|
{
|
405 |
|
|
errno = EBADF;
|
406 |
|
|
return -1;
|
407 |
|
|
}
|
408 |
|
|
|
409 |
|
|
cyg_fd_assign( fd2, fp );
|
410 |
|
|
|
411 |
|
|
cyg_fp_free(fp);
|
412 |
|
|
|
413 |
|
|
return fd2;
|
414 |
|
|
}
|
415 |
|
|
|
416 |
|
|
// -------------------------------------------------------------------------
|
417 |
|
|
// EOF fd.cxx
|