2 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
5 File descriptors handling internals.
8 #include "__arosc_privdata.h"
18 #include <proto/exec.h>
19 #include <proto/dos.h>
20 #include <exec/memory.h>
21 #include <exec/semaphores.h>
23 #include <dos/filesystem.h>
24 #include <aros/symbolsets.h>
25 #include <aros/debug.h>
31 fdesc
*__getfdesc(register int fd
)
33 return ((__numslots
>fd
) && (fd
>=0))?__fd_array
[fd
]:NULL
;
36 void __setfdesc(register int fd
, fdesc
*desc
)
38 /* FIXME: Check if fd is in valid range... */
39 __fd_array
[fd
] = desc
;
42 int __getfirstfd(register int startfd
)
44 /* FIXME: Check if fd is in valid range... */
47 startfd
< __numslots
&& __fd_array
[startfd
];
54 int __getfdslot(int wanted_fd
)
56 if (wanted_fd
>=__numslots
)
60 tmp
= malloc((wanted_fd
+1)*sizeof(fdesc
*));
66 CopyMem(__fd_array
, tmp
, __numslots
*sizeof(fdesc
*));
72 bzero(__fd_array
+ __numslots
, (wanted_fd
- __numslots
+ 1) * sizeof(fdesc
*));
73 __numslots
= wanted_fd
+1;
75 else if (wanted_fd
< 0)
80 else if (__fd_array
[wanted_fd
])
88 LONG
__oflags2amode(int flags
)
92 /* filter out invalid modes */
93 switch (flags
& (O_CREAT
|O_TRUNC
|O_EXCL
))
100 if (flags
& O_WRITE
) openmode
|= FMF_WRITE
;
101 if (flags
& O_READ
) openmode
|= FMF_READ
;
102 if (flags
& O_EXEC
) openmode
|= FMF_EXECUTE
;
103 if (flags
& O_CREAT
) openmode
|= FMF_CREATE
;
104 if (flags
& O_NONBLOCK
) openmode
|= FMF_NONBLOCK
;
105 if (flags
& O_APPEND
) openmode
|= FMF_APPEND
;
110 int __open(int wanted_fd
, const char *pathname
, int flags
, int mode
)
112 BPTR fh
= NULL
, lock
= NULL
;
113 fdesc
*currdesc
= NULL
;
115 struct FileInfoBlock
*fib
= NULL
;
116 LONG openmode
= __oflags2amode(flags
);
118 if (__doupath
&& pathname
[0] == '\0')
120 /* On *nix "" is not really a valid file name. */
125 pathname
= __path_u2a(pathname
);
126 if (!pathname
) return -1;
128 D(bug("__open: entering, wanted fd = %d, path = %s, flags = %d, mode = %d\n", wanted_fd
, pathname
, flags
, mode
));
133 D(bug( "__open: exiting with error EINVAL\n"));
137 cblock
= AllocVec(sizeof(fcb
), MEMF_ANY
| MEMF_CLEAR
);
138 if (!cblock
) { D(bug("__open: no memory [1]\n")); goto err
; }
139 currdesc
= __alloc_fdesc();
140 if (!currdesc
) { D(bug("__open: no memory [2]\n")); goto err
; }
141 currdesc
->fdflags
= 0;
142 currdesc
->fcb
= cblock
;
144 wanted_fd
= __getfdslot(wanted_fd
);
145 if (wanted_fd
== -1) { D(bug("__open: no free fd\n")); goto err
; }
147 lock
= Lock((char *)pathname
, SHARED_LOCK
);
152 (IoErr() != ERROR_OBJECT_NOT_FOUND
) ||
153 /* If the file doesn't exist and the flag O_CREAT is not set return an error */
154 (IoErr() == ERROR_OBJECT_NOT_FOUND
&& !(flags
& O_CREAT
))
157 errno
= IoErr2errno(IoErr());
163 /* If the file exists, but O_EXCL is set, then return an error */
170 fib
= AllocDosObject(DOS_FIB
, NULL
);
173 errno
= IoErr2errno(IoErr());
177 if (!Examine(lock
, fib
))
180 The filesystem in which the file resides doesn't support
181 the EXAMINE action. It might be broken or might also not
182 be a filesystem at all. So let's assume the file is not a
185 fib
->fib_DirEntryType
= 0;
188 # warning implement softlink handling
190 /* Check if it's a directory or a softlink.
191 Softlinks are not handled yet, though */
192 if (fib
->fib_DirEntryType
> 0)
194 /* A directory cannot be opened for writing */
195 if (openmode
& FMF_WRITE
)
202 FreeDosObject(DOS_FIB
, fib
);
203 currdesc
->fcb
->isdir
= 1;
207 FreeDosObject(DOS_FIB
, fib
);
211 /* the file exists and it's not a directory or the file doesn't exist */
219 if (openmode
& (FMF_APPEND
| FMF_WRITE
))
220 openmode
|= FMF_READ
; /* force filesystem ACTION_FINDUPDATE */
222 if (!(fh
= Open ((char *)pathname
, openmode
)) )
224 ULONG ioerr
= IoErr();
225 D(bug("[clib] Open ioerr=%d\n", ioerr
));
226 errno
= IoErr2errno(ioerr
);
230 if((flags
& O_TRUNC
) && (flags
& (O_RDWR
| O_WRONLY
)))
232 if(SetFileSize(fh
, 0, OFFSET_BEGINNING
) != 0)
234 /* Ignore error if FSA_SET_FILE_SIZE is not implemented */
235 if(IoErr() != ERROR_NOT_IMPLEMENTED
)
237 errno
= IoErr2errno(IoErr());
244 currdesc
->fcb
->fh
= fh
;
245 currdesc
->fcb
->flags
= flags
;
246 currdesc
->fcb
->opencount
= 1;
248 __setfdesc(wanted_fd
, currdesc
);
250 D(bug("__open: exiting fd=%d\n", wanted_fd
));
255 if (fib
) FreeDosObject(DOS_FIB
, fib
);
256 if (cblock
) FreeVec(cblock
);
257 if (currdesc
) __free_fdesc(currdesc
);
258 if (fh
&& fh
!= lock
) Close(fh
);
259 if (lock
) UnLock(lock
);
261 D(bug("__open: exiting with error %d\n", errno
));
266 fdesc
*__alloc_fdesc(void)
270 fdesc
= AllocPooled(__fd_mempool
, sizeof(fdesc
));
272 D(bug("Allocated fdesc %x from %x pool\n", fdesc
, __fd_mempool
));
277 void __free_fdesc(fdesc
*fdesc
)
279 D(bug("Freeing fdesc %x from %x pool\n", fdesc
, __fd_mempool
));
280 FreePooled(__fd_mempool
, fdesc
, sizeof(fdesc
));
284 struct __reg_fdarray
{
291 /* Some local variables for register_init_fdarray */
292 static int __fdinit
= 0;
293 static struct SignalSemaphore __fdsem
;
294 static struct MinList __fdreglist
;
296 int __init_vars(void)
298 InitSemaphore(&__fdsem
);
299 NEWLIST(&__fdreglist
);
304 int __register_init_fdarray(fdesc
**__fdarray
, int numslots
)
306 /* arosc privdata should not be used inside this function,
307 * this function is called before aroscbase is initialized
309 struct __reg_fdarray
*regnode
= AllocVec(sizeof(struct __reg_fdarray
), MEMF_ANY
|MEMF_CLEAR
);
314 regnode
->task
= FindTask(NULL
);
315 regnode
->fdarray
= __fdarray
;
316 regnode
->numslots
= numslots
;
318 D(bug("Allocated regnode: %p, fdarray: %p, numslots: %d\n",
319 regnode
, regnode
->fdarray
, regnode
->numslots
322 ObtainSemaphore(&__fdsem
);
323 AddHead((struct List
*)&__fdreglist
, (struct Node
*)regnode
);
324 ReleaseSemaphore(&__fdsem
);
330 #warning perhaps this has to be handled in a different way...
331 int __init_stdfiles(void)
334 fcb
*infcb
= NULL
, *outfcb
= NULL
, *errfcb
= NULL
;
335 fdesc
*indesc
=NULL
, *outdesc
=NULL
, *errdesc
=NULL
;
336 int res
= __getfdslot(2);
341 !(infcb
= AllocVec(sizeof(fcb
), MEMF_ANY
| MEMF_CLEAR
)) ||
342 !(indesc
= __alloc_fdesc()) ||
343 !(outfcb
= AllocVec(sizeof(fcb
), MEMF_ANY
| MEMF_CLEAR
)) ||
344 !(outdesc
= __alloc_fdesc()) ||
345 !(errfcb
= AllocVec(sizeof(fcb
), MEMF_ANY
| MEMF_CLEAR
)) ||
346 !(errdesc
= __alloc_fdesc())
352 __free_fdesc(indesc
);
356 __free_fdesc(outdesc
);
360 __free_fdesc(errdesc
);
361 SetIoErr(ERROR_NO_FREE_STORE
);
366 outdesc
->fdflags
= 0;
367 errdesc
->fdflags
= 0;
370 outdesc
->fcb
= outfcb
;
371 errdesc
->fcb
= errfcb
;
373 me
= (struct Process
*)FindTask (NULL
);
374 indesc
->fcb
->fh
= Input();
375 outdesc
->fcb
->fh
= Output();
376 errdesc
->fcb
->fh
= me
->pr_CES
? me
->pr_CES
: me
->pr_COS
;
378 indesc
->fcb
->flags
= O_RDONLY
;
379 outdesc
->fcb
->flags
= O_WRONLY
| O_APPEND
;
380 errdesc
->fcb
->flags
= O_WRONLY
| O_APPEND
;
382 indesc
->fcb
->opencount
= outdesc
->fcb
->opencount
= errdesc
->fcb
->opencount
= 1;
383 indesc
->fcb
->privflags
= outdesc
->fcb
->privflags
= errdesc
->fcb
->privflags
= _FCB_DONTCLOSE_FH
;
385 __fd_array
[STDIN_FILENO
] = indesc
;
386 __fd_array
[STDOUT_FILENO
] = outdesc
;
387 __fd_array
[STDERR_FILENO
] = errdesc
;
392 static int __copy_fdarray(fdesc
**__src_fd_array
, int numslots
)
396 for(i
= numslots
- 1; i
>= 0; i
--)
398 if(__src_fd_array
[i
])
400 if(__getfdslot(i
) != i
)
403 if((__fd_array
[i
] = __alloc_fdesc()) == NULL
)
406 __fd_array
[i
]->fdflags
= 0;
407 __fd_array
[i
]->fcb
= __src_fd_array
[i
]->fcb
;
408 __fd_array
[i
]->fcb
->opencount
++;
417 struct __reg_fdarray
*regnodeit
, *regnode
= NULL
;
418 struct Task
*self
= FindTask(NULL
);
426 __fd_mempool
= CreatePool(MEMF_PUBLIC
, 16*sizeof(fdesc
), 16*sizeof(fdesc
));
428 ObtainSemaphore(&__fdsem
);
429 ForeachNode(&__fdreglist
, regnodeit
)
431 if (regnodeit
->task
== self
)
435 D(bug("Found regnode: %p, fdarray: %p, numslots: %d\n",
436 regnode
, regnode
->fdarray
, regnode
->numslots
438 Remove((struct Node
*)regnode
);
442 ReleaseSemaphore(&__fdsem
);
445 return __init_stdfiles();
448 int ok
= __copy_fdarray(regnode
->fdarray
, regnode
->numslots
);
464 DeletePool(__fd_mempool
);
469 void __updatestdio(void)
473 me
= (struct Process
*)FindTask(NULL
);
479 __fd_array
[STDIN_FILENO
]->fcb
->fh
= Input();
480 __fd_array
[STDOUT_FILENO
]->fcb
->fh
= Output();
481 __fd_array
[STDERR_FILENO
]->fcb
->fh
= me
->pr_CES
? me
->pr_CES
: me
->pr_COS
;
483 __fd_array
[STDIN_FILENO
]->fcb
->privflags
=
484 __fd_array
[STDOUT_FILENO
]->fcb
->privflags
=
485 __fd_array
[STDERR_FILENO
]->fcb
->privflags
= _FCB_DONTCLOSE_FH
;
488 ADD2INIT(__init_fd
, 2);
489 ADD2EXIT(__exit_fd
, 2);