iPXE
posix_io.h
Go to the documentation of this file.
00001 #ifndef _IPXE_POSIX_IO_H
00002 #define _IPXE_POSIX_IO_H
00003 
00004 /** @file
00005  *
00006  * POSIX-like I/O
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00011 
00012 #include <stdint.h>
00013 #include <ipxe/uaccess.h>
00014 
00015 /** Minimum file descriptor that will ever be allocated */
00016 #define POSIX_FD_MIN ( 1 )
00017 
00018 /** Maximum file descriptor that will ever be allocated */
00019 #define POSIX_FD_MAX ( 31 )
00020 
00021 /** File descriptor set as used for select() */
00022 typedef uint32_t fd_set;
00023 
00024 extern int open ( const char *uri_string );
00025 extern ssize_t read_user ( int fd, userptr_t buffer,
00026                            off_t offset, size_t len );
00027 extern int select ( fd_set *readfds, int wait );
00028 extern ssize_t fsize ( int fd );
00029 extern int close ( int fd );
00030 
00031 /**
00032  * Zero a file descriptor set
00033  *
00034  * @v set               File descriptor set
00035  */
00036 static inline __attribute__ (( always_inline )) void
00037 FD_ZERO ( fd_set *set ) {
00038         *set = 0;
00039 }
00040 
00041 /**
00042  * Set a bit within a file descriptor set
00043  *
00044  * @v fd                File descriptor
00045  * @v set               File descriptor set
00046  */
00047 static inline __attribute__ (( always_inline )) void
00048 FD_SET ( int fd, fd_set *set ) {
00049         *set |= ( 1 << fd );
00050 }
00051 
00052 /**
00053  * Clear a bit within a file descriptor set
00054  *
00055  * @v fd                File descriptor
00056  * @v set               File descriptor set
00057  */
00058 static inline __attribute__ (( always_inline )) void
00059 FD_CLR ( int fd, fd_set *set ) {
00060         *set &= ~( 1 << fd );
00061 }
00062 
00063 /**
00064  * Test a bit within a file descriptor set
00065  *
00066  * @v fd                File descriptor
00067  * @v set               File descriptor set
00068  * @ret is_set          Corresponding bit is set
00069  */
00070 static inline __attribute__ (( always_inline )) int
00071 FD_ISSET ( int fd, fd_set *set ) {
00072         return ( *set & ( 1 << fd ) );
00073 }
00074 
00075 /**
00076  * Read data from file
00077  *
00078  * @v fd                File descriptor
00079  * @v buf               Data buffer
00080  * @v len               Maximum length to read
00081  * @ret len             Actual length read, or negative error number
00082  */
00083 static inline ssize_t read ( int fd, void *buf, size_t len ) {
00084         return read_user ( fd, virt_to_user ( buf ), 0, len );
00085 }
00086 
00087 #endif /* _IPXE_POSIX_IO_H */