source: rtems/c/src/exec/libcsupport/src/libio.c @ 3473f605

4.104.114.84.95
Last change on this file since 3473f605 was 3473f605, checked in by Joel Sherrill <joel.sherrill@…>, on 08/27/97 at 20:33:49

Added error numbers and changed default error code from 0 to -1 for
error number mapping.

  • Property mode set to 100644
File size: 12.2 KB
Line 
1/*
2 *  Provide UNIX/POSIX-like io system calls for RTEMS using the
3 *  RTEMS IO manager
4 *
5 *  $Id$
6 */
7
8#include <rtems.h>
9#include <rtems/assoc.h>                /* assoc.h not included by rtems.h */
10
11#include <stdio.h>                      /* O_RDONLY, et.al. */
12#include <fcntl.h>                      /* O_RDONLY, et.al. */
13#include <assert.h>
14
15#if ! defined(O_NDELAY)
16# if defined(solaris2)
17#  define O_NDELAY O_NONBLOCK
18# elif defined(RTEMS_NEWLIB)
19#  define O_NDELAY _FNBIO
20# endif
21#endif
22
23
24#include <errno.h>
25#include <string.h>                     /* strcmp */
26#include <unistd.h>
27#include <stdlib.h>                     /* calloc() */
28
29#include "libio.h"                      /* libio.h not pulled in by rtems */
30
31/*
32 * Semaphore to protect the io table
33 */
34
35Objects_Id rtems_libio_semaphore;
36
37#define RTEMS_LIBIO_SEM         rtems_build_name('L', 'B', 'I', 'O')
38#define RTEMS_LIBIO_IOP_SEM(n)  rtems_build_name('L', 'B', 'I', n)
39
40unsigned32     rtems_libio_number_iops;
41rtems_libio_t *rtems_libio_iops;
42rtems_libio_t *rtems_libio_last_iop;
43
44#define rtems_libio_iop(fd)    ((((unsigned32)(fd)) < rtems_libio_number_iops) ? \
45                                       &rtems_libio_iops[fd] : 0)
46
47#define rtems_libio_check_fd(fd) \
48    do { \
49        if ((unsigned32) (fd) >= rtems_libio_number_iops) \
50        { \
51            errno = EBADF; \
52            return -1; \
53        } \
54    } while (0)
55
56#define rtems_libio_check_buffer(buffer) \
57    do { \
58        if ((buffer) == 0) \
59        { \
60            errno = EINVAL; \
61            return -1; \
62        } \
63    } while (0)
64
65#define rtems_libio_check_count(count) \
66    do { \
67        if ((count) == 0) \
68        { \
69            return 0; \
70        } \
71    } while (0)
72
73#define rtems_libio_check_permissions(iop, flag) \
74    do { \
75        if (((iop)->flags & (flag)) == 0) \
76        { \
77              errno = EINVAL; \
78              return -1; \
79        } \
80    } while (0)
81
82/*
83 * External I/O handlers
84 *
85 * Space for all possible handlers is preallocated
86 * to speed up dispatch to external handlers.
87 */
88
89static rtems_libio_handler_t handlers[15];
90
91void
92rtems_register_libio_handler(
93    int                         handler_flag,
94    const rtems_libio_handler_t *handler
95)
96{
97  int handler_index = rtems_file_descriptor_type_index(handler_flag);
98
99  if ((handler_index < 0) || (handler_index >= 15))
100    rtems_fatal_error_occurred( RTEMS_INVALID_NUMBER );
101  handlers[handler_index] = *handler;
102}
103
104
105void
106rtems_libio_config(
107    rtems_configuration_table *config,
108    unsigned32                 max_fds
109)
110{
111    rtems_libio_number_iops = max_fds;
112
113    /*
114     * tweak config to reflect # of semaphores we will need
115     */
116
117    /* one for iop table */
118    config->RTEMS_api_configuration->maximum_semaphores += 1;
119    config->RTEMS_api_configuration->maximum_semaphores += max_fds;
120}
121
122/*
123 * Called by bsp startup code to init the libio area.
124 */
125
126void
127rtems_libio_init(void)
128{
129    rtems_status_code rc;
130
131    if (rtems_libio_number_iops > 0)
132    {
133        rtems_libio_iops = (rtems_libio_t *) calloc(rtems_libio_number_iops,
134                                                    sizeof(rtems_libio_t));
135        if (rtems_libio_iops == NULL)
136            rtems_fatal_error_occurred(RTEMS_NO_MEMORY);
137
138        rtems_libio_last_iop = rtems_libio_iops + (rtems_libio_number_iops - 1);
139    }
140
141    rc = rtems_semaphore_create(
142      RTEMS_LIBIO_SEM,
143      1,
144      RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
145      RTEMS_NO_PRIORITY,
146      &rtems_libio_semaphore
147    );
148    if (rc != RTEMS_SUCCESSFUL)
149        rtems_fatal_error_occurred(rc);
150}
151
152/*
153 * Convert RTEMS status to a UNIX errno
154 */
155
156rtems_assoc_t errno_assoc[] = {
157    { "OK",                 RTEMS_SUCCESSFUL,                0 },
158    { "TIMEOUT",            RTEMS_TIMEOUT,                   ETIME },
159    { "NO MEMORY",          RTEMS_NO_MEMORY,                 ENOMEM },
160    { "NO DEVICE",          RTEMS_UNSATISFIED,               ENOSYS },
161    { "INVALID NUMBER",     RTEMS_INVALID_NUMBER,            EBADF},
162    { "NOT RESOURCE OWNER", RTEMS_NOT_OWNER_OF_RESOURCE,     EPERM},
163    { 0, 0, 0 },
164};
165
166static unsigned32
167rtems_libio_errno(rtems_status_code code)
168{
169    int rc;
170   
171    if ((rc = rtems_assoc_remote_by_local(errno_assoc, (unsigned32) code)))
172    {
173        errno = rc;
174        return -1;
175    }
176    return -1;
177}
178
179/*
180 * Convert UNIX fnctl(2) flags to ones that RTEMS drivers understand
181 */
182
183rtems_assoc_t access_modes_assoc[] = {
184    { "READ",       LIBIO_FLAGS_READ,  O_RDONLY },
185    { "WRITE",      LIBIO_FLAGS_WRITE, O_WRONLY },
186    { "READ/WRITE", LIBIO_FLAGS_READ_WRITE, O_RDWR },
187    { 0, 0, 0 },
188};
189
190rtems_assoc_t status_flags_assoc[] = {
191    { "NO DELAY",  LIBIO_FLAGS_NO_DELAY,  O_NDELAY },
192    { "APPEND",    LIBIO_FLAGS_APPEND,    O_APPEND },
193    { "CREATE",    LIBIO_FLAGS_CREATE,    O_CREAT },
194    { 0, 0, 0 },
195};
196
197static unsigned32
198rtems_libio_fcntl_flags(unsigned32 fcntl_flags)
199{
200    unsigned32 flags = 0;
201    unsigned32 access_modes;
202
203    /*
204     * Access mode is a small integer
205     */
206   
207    access_modes = fcntl_flags & O_ACCMODE;
208    fcntl_flags &= ~O_ACCMODE;
209    flags = rtems_assoc_local_by_remote(access_modes_assoc, access_modes);
210
211    /*
212     * Everything else is single bits
213     */
214
215    flags |= rtems_assoc_local_by_remote_bitfield(status_flags_assoc, fcntl_flags);
216    return flags;
217}
218
219
220static rtems_libio_t *
221rtems_libio_allocate(void)
222{
223    rtems_libio_t *iop;
224    rtems_status_code rc;
225   
226    rtems_semaphore_obtain(rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
227
228    for (iop = rtems_libio_iops; iop <= rtems_libio_last_iop; iop++)
229        if ((iop->flags & LIBIO_FLAGS_OPEN) == 0)
230        {
231            /*
232             * Got one; create a semaphore for it
233             */
234
235            rc = rtems_semaphore_create(
236              RTEMS_LIBIO_IOP_SEM(iop - rtems_libio_iops),
237              1,
238              RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
239              RTEMS_NO_PRIORITY,
240              &iop->sem
241            );
242            if (rc != RTEMS_SUCCESSFUL)
243                goto failed;
244           
245            iop->flags = LIBIO_FLAGS_OPEN;
246            goto done;
247        }
248   
249failed:
250    iop = 0;
251   
252done:
253    rtems_semaphore_release(rtems_libio_semaphore);
254    return iop;
255}
256
257static void
258rtems_libio_free(rtems_libio_t *iop)
259{
260    rtems_semaphore_obtain(rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
261
262    if (iop->sem)
263        rtems_semaphore_delete(iop->sem);
264    (void) memset(iop, 0, sizeof(*iop));
265
266    rtems_semaphore_release(rtems_libio_semaphore);
267}
268
269int
270__rtems_open(
271    const char   *pathname,
272    unsigned32    flag,
273    unsigned32    mode)
274{
275    rtems_status_code rc;
276    rtems_libio_t *iop = 0;
277    rtems_driver_name_t *np;
278    rtems_libio_open_close_args_t args;
279
280    /*
281     * Additional external I/O handlers would be supported by
282     * adding code to pick apart the pathname appropriately.
283     * The networking code does not require changes here since
284     * network file descriptors are obtained using socket(), not
285     * open().
286     */
287
288    if ((rc = rtems_io_lookup_name(pathname, &np)) != RTEMS_SUCCESSFUL)
289        goto done;
290
291    iop = rtems_libio_allocate();
292    if (iop == 0)
293    {
294        rc = RTEMS_TOO_MANY;
295        goto done;
296    }
297   
298    iop->driver = np;
299    iop->pathname = (char *) pathname;
300    iop->flags |= rtems_libio_fcntl_flags(flag);
301
302    args.iop = iop;
303    args.flags = iop->flags;
304    args.mode = mode;
305
306    rc = rtems_io_open(np->major, np->minor, (void *) &args);
307   
308done:
309 
310    if (rc != RTEMS_SUCCESSFUL)
311    {
312        if (iop)
313            rtems_libio_free(iop);
314        return rtems_libio_errno(rc);
315    }
316   
317    return iop - rtems_libio_iops;
318}
319   
320int
321__rtems_close(
322    int  fd
323  )   
324{
325    rtems_status_code rc;
326    rtems_driver_name_t *np;
327    rtems_libio_t *iop;
328    rtems_libio_open_close_args_t args;
329    int status;
330
331    if (rtems_file_descriptor_type(fd)) {
332        int (*fp)(int fd);
333
334        fp = handlers[rtems_file_descriptor_type_index(fd)].close;
335        if (fp == NULL) {
336            errno = EBADF;
337            return -1;
338        }
339        status = (*fp)(fd);
340        rtems_libio_free(iop);
341        return status;
342    }
343    iop = rtems_libio_iop(fd);
344    rtems_libio_check_fd(fd);
345
346    np = iop->driver;
347
348    args.iop = iop;
349    args.flags = 0;
350    args.mode = 0;
351   
352    rc = rtems_io_close(np->major, np->minor, (void *) &args);
353
354    rtems_libio_free(iop);
355
356    if (rc != RTEMS_SUCCESSFUL)
357        return rtems_libio_errno(rc);
358    return 0;
359}
360   
361int
362__rtems_read(
363    int       fd,
364    void *    buffer,
365    unsigned32 count
366  )
367{
368    rtems_status_code rc;
369    rtems_driver_name_t *np;
370    rtems_libio_t *iop;
371    rtems_libio_rw_args_t args;
372
373    if (rtems_file_descriptor_type(fd)) {
374        int (*fp)(int fd, void *buffer, unsigned32 count);
375
376        fp = handlers[rtems_file_descriptor_type_index(fd)].read;
377        if (fp == NULL) {
378            errno = EBADF;
379            return -1;
380        }
381        return (*fp)(fd, buffer, count);
382    }
383    iop = rtems_libio_iop(fd);
384    rtems_libio_check_fd(fd);
385    rtems_libio_check_buffer(buffer);
386    rtems_libio_check_count(count);
387    rtems_libio_check_permissions(iop, LIBIO_FLAGS_READ);
388
389    np = iop->driver;
390
391    args.iop = iop;
392    args.offset = iop->offset;
393    args.buffer = buffer;
394    args.count = count;
395    args.flags = iop->flags;
396    args.bytes_moved = 0;
397
398    rc = rtems_io_read(np->major, np->minor, (void *) &args);
399
400    iop->offset += args.bytes_moved;
401
402    if (rc != RTEMS_SUCCESSFUL)
403        return rtems_libio_errno(rc);
404
405    return args.bytes_moved;
406}
407
408int
409__rtems_write(
410    int         fd,
411    const void *buffer,
412    unsigned32  count
413  )
414{
415    rtems_status_code rc;
416    rtems_driver_name_t *np;
417    rtems_libio_t *iop;
418    rtems_libio_rw_args_t args;
419
420    if (rtems_file_descriptor_type(fd)) {
421        int (*fp)(int fd, const void *buffer, unsigned32 count);
422
423        fp = handlers[rtems_file_descriptor_type_index(fd)].write;
424        if (fp == NULL) {
425            errno = EBADF;
426            return -1;
427        }
428        return (*fp)(fd, buffer, count);
429    }
430    iop = rtems_libio_iop(fd);
431    rtems_libio_check_fd(fd);
432    rtems_libio_check_buffer(buffer);
433    rtems_libio_check_count(count);
434    rtems_libio_check_permissions(iop, LIBIO_FLAGS_WRITE);
435
436    np = iop->driver;
437
438    args.iop = iop;
439    args.offset = iop->offset;
440    args.buffer = (void *) buffer;
441    args.count = count;
442    args.flags = iop->flags;
443    args.bytes_moved = 0;
444
445    rc = rtems_io_write(np->major, np->minor, (void *) &args);
446
447    iop->offset += args.bytes_moved;
448
449    if (rc != RTEMS_SUCCESSFUL)
450        return rtems_libio_errno(rc);
451
452    return args.bytes_moved;
453}
454
455int
456__rtems_ioctl(
457    int         fd,
458    unsigned32  command,
459    void *      buffer)
460{
461    rtems_status_code rc;
462    rtems_driver_name_t *np;
463    rtems_libio_t *iop;
464    rtems_libio_ioctl_args_t args;
465
466    if (rtems_file_descriptor_type(fd)) {
467        int (*fp)(int fd, unsigned32 command, void *buffer);
468
469        fp = handlers[rtems_file_descriptor_type_index(fd)].ioctl;
470        if (fp == NULL) {
471            errno = EBADF;
472            return -1;
473        }
474        return (*fp)(fd, command, buffer);
475    }
476    iop = rtems_libio_iop(fd);
477    rtems_libio_check_fd(fd);
478
479    np = iop->driver;
480
481    args.iop = iop;
482    args.command = command;
483    args.buffer = buffer;
484
485    rc = rtems_io_control(np->major, np->minor, (void *) &args);
486
487    if (rc != RTEMS_SUCCESSFUL)
488        return rtems_libio_errno(rc);
489
490    return args.ioctl_return;
491}
492   
493/*
494 * internal only??
495 */
496
497
498int
499__rtems_lseek(
500    int                  fd,
501    rtems_libio_offset_t offset,
502    int                  whence
503  )   
504{
505    rtems_libio_t *iop;
506
507    if (rtems_file_descriptor_type(fd)) {
508        int (*fp)(int fd, rtems_libio_offset_t offset, int whence);
509
510        fp = handlers[rtems_file_descriptor_type_index(fd)].lseek;
511        if (fp == NULL) {
512            errno = EBADF;
513            return -1;
514        }
515        return (*fp)(fd, offset, whence);
516    }
517    iop = rtems_libio_iop(fd);
518    rtems_libio_check_fd(fd);
519
520    switch (whence)
521    {
522        case SEEK_SET:
523            iop->offset = offset;
524            break;
525
526        case SEEK_CUR:
527            iop->offset += offset;
528            break;
529
530        case SEEK_END:
531            iop->offset = iop->size - offset;
532            break;
533
534        default:
535            errno = EINVAL;
536            return -1;
537    }
538    return 0;
539}
Note: See TracBrowser for help on using the repository browser.