iPXE
Defines | Typedefs | Functions
posix_io.h File Reference

POSIX-like I/O. More...

#include <stdint.h>
#include <ipxe/uaccess.h>

Go to the source code of this file.

Defines

#define POSIX_FD_MIN   ( 1 )
 Minimum file descriptor that will ever be allocated.
#define POSIX_FD_MAX   ( 31 )
 Maximum file descriptor that will ever be allocated.

Typedefs

typedef uint32_t fd_set
 File descriptor set as used for select()

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
int open (const char *uri_string)
 Open file.
ssize_t read_user (int fd, userptr_t buffer, off_t offset, size_t len)
 Read data from file.
int select (fd_set *readfds, int wait)
 Check file descriptors for readiness.
ssize_t fsize (int fd)
 Determine file size.
int close (int fd)
 Close file.
static __attribute__ ((always_inline)) void FD_ZERO(fd_set *set)
 Zero a file descriptor set.

Detailed Description

POSIX-like I/O.

Definition in file posix_io.h.


Define Documentation

#define POSIX_FD_MIN   ( 1 )

Minimum file descriptor that will ever be allocated.

Definition at line 16 of file posix_io.h.

Referenced by posix_find_free_fd(), and select().

#define POSIX_FD_MAX   ( 31 )

Maximum file descriptor that will ever be allocated.

Definition at line 19 of file posix_io.h.

Referenced by posix_find_free_fd(), and select().


Typedef Documentation

typedef uint32_t fd_set

File descriptor set as used for select()

Definition at line 22 of file posix_io.h.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
int open ( const char *  uri_string)

Open file.

Parameters:
uri_stringURI string
Return values:
fdFile descriptor, or negative error number

Definition at line 176 of file posix_io.c.

References posix_file::data, DBG, EINPROGRESS, ENOMEM, posix_file::fd, INIT_LIST_HEAD, intf_init(), posix_file::list, list_add, list_empty, posix_file_finished(), posix_file_free(), posix_find_free_fd(), posix_file::rc, rc, ref_init, ref_put, posix_file::refcnt, step(), posix_file::xfer, xfer_open_uri_string(), and zalloc().

Referenced by int22(), peerdist_discovery_reply_values(), and pxenv_file_open().

                                    {
        struct posix_file *file;
        int fd;
        int rc;

        /* Find a free file descriptor to use */
        fd = posix_find_free_fd();
        if ( fd < 0 )
                return fd;

        /* Allocate and initialise structure */
        file = zalloc ( sizeof ( *file ) );
        if ( ! file )
                return -ENOMEM;
        ref_init ( &file->refcnt, posix_file_free );
        file->fd = fd;
        file->rc = -EINPROGRESS;
        intf_init ( &file->xfer, &posix_file_xfer_desc, &file->refcnt );
        INIT_LIST_HEAD ( &file->data );

        /* Open URI on data transfer interface */
        if ( ( rc = xfer_open_uri_string ( &file->xfer, uri_string ) ) != 0 )
                goto err;

        /* Wait for open to succeed or fail */
        while ( list_empty ( &file->data ) ) {
                step();
                if ( file->rc == 0 )
                        break;
                if ( file->rc != -EINPROGRESS ) {
                        rc = file->rc;
                        goto err;
                }
        }

        /* Add to list of open files.  List takes reference ownership. */
        list_add ( &file->list, &posix_files );
        DBG ( "POSIX opened %s as file %d\n", uri_string, fd );
        return fd;

 err:
        posix_file_finished ( file, rc );
        ref_put ( &file->refcnt );
        return rc;
}
ssize_t read_user ( int  fd,
userptr_t  buffer,
off_t  offset,
size_t  max_len 
)

Read data from file.

Parameters:
bufferData buffer
offsetStarting offset within data buffer
lenMaximum length to read
Return values:
lenActual length read, or negative error number

This call is non-blocking; if no data is available to read then -EWOULDBLOCK will be returned.

Definition at line 265 of file posix_io.c.

References assert, copy_to_user(), io_buffer::data, posix_file::data, EBADF, EINPROGRESS, EWOULDBLOCK, free_iob(), iob_len(), iob_pull, len, io_buffer::list, list_del, list_empty, list_for_each_entry, max_len, posix_file::pos, posix_fd_to_file(), posix_file::rc, and step().

Referenced by int22(), and pxenv_file_read().

                                                                             {
        struct posix_file *file;
        struct io_buffer *iobuf;
        size_t len;

        /* Identify file */
        file = posix_fd_to_file ( fd );
        if ( ! file )
                return -EBADF;

        /* Try to fetch more data if none available */
        if ( list_empty ( &file->data ) )
                step();

        /* Dequeue at most one received I/O buffer into user buffer */
        list_for_each_entry ( iobuf, &file->data, list ) {
                len = iob_len ( iobuf );
                if ( len > max_len )
                        len = max_len;
                copy_to_user ( buffer, offset, iobuf->data, len );
                iob_pull ( iobuf, len );
                if ( ! iob_len ( iobuf ) ) {
                        list_del ( &iobuf->list );
                        free_iob ( iobuf );
                }
                file->pos += len;
                assert ( len != 0 );
                return len;
        }

        /* If file has completed, return (after returning all data) */
        if ( file->rc != -EINPROGRESS ) {
                assert ( list_empty ( &file->data ) );
                return file->rc;
        }

        /* No data ready and file still in progress; return -WOULDBLOCK */
        return -EWOULDBLOCK;
}
int select ( fd_set readfds,
int  wait 
)

Check file descriptors for readiness.

Parameters:
readfdsFile descriptors to check
waitWait until data is ready
Return values:
nreadyNumber of ready file descriptors

Definition at line 229 of file posix_io.c.

References posix_file::data, EBADF, EINPROGRESS, posix_file::fd, list_empty, POSIX_FD_MAX, POSIX_FD_MIN, posix_fd_to_file(), posix_file::rc, and step().

Referenced by int22(), and pxenv_file_select().

                                         {
        struct posix_file *file;
        int fd;

        do {
                for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
                        if ( ! FD_ISSET ( fd, readfds ) )
                                continue;
                        file = posix_fd_to_file ( fd );
                        if ( ! file )
                                return -EBADF;
                        if ( ( list_empty ( &file->data ) ) &&
                             ( file->rc == -EINPROGRESS ) )
                                continue;
                        /* Data is available or status has changed */
                        FD_ZERO ( readfds );
                        FD_SET ( fd, readfds );
                        return 1;
                }
                step();
        } while ( wait );

        return 0;
}
ssize_t fsize ( int  fd)

Determine file size.

Parameters:
fdFile descriptor
Return values:
sizeFile size, or negative error number

Definition at line 311 of file posix_io.c.

References EBADF, posix_file::filesize, and posix_fd_to_file().

Referenced by int22(), and pxenv_get_file_size().

                         {
        struct posix_file *file;

        /* Identify file */
        file = posix_fd_to_file ( fd );
        if ( ! file )
                return -EBADF;

        return file->filesize;
}
int close ( int  fd)

Close file.

Parameters:
fdFile descriptor
Return values:
rcReturn status code

Definition at line 328 of file posix_io.c.

References EBADF, posix_file::list, list_del, posix_fd_to_file(), posix_file_finished(), ref_put, and posix_file::refcnt.

Referenced by int22(), ntp_deliver(), peerdist_discovery_reply_values(), and pxenv_file_close().

                     {
        struct posix_file *file;

        /* Identify file */
        file = posix_fd_to_file ( fd );
        if ( ! file )
                return -EBADF;

        /* Terminate data transfer */
        posix_file_finished ( file, 0 );

        /* Remove from list of open files and drop reference */
        list_del ( &file->list );
        ref_put ( &file->refcnt );
        return 0;
}
static __attribute__ ( (always_inline)  ) [inline, static]

Zero a file descriptor set.

Set a bit within a file descriptor set.

Parameters:
setFile descriptor set
fdFile descriptor
setFile descriptor set

Definition at line 36 of file posix_io.h.

                        {
        *set = 0;
}