IOChannel

Describes an input/output channel which is implemented as file descriptor on POSIX systems.

Summary
IOChannelDescribes an input/output channel which is implemented as file descriptor on POSIX systems.
CopyrightThis program is free software.
Files
C-kern/api/io/iochannel.hHeader file IOChannel.
C-kern/platform/Linux/io/iochannel.cImplementation file IOChannel impl.
Types
struct iochannel_tMake iochannel_t an alias of sys_iochannel_t.
Enumerations
iochannel_eStandard channels which are usually open at process start by convention.
Functions
query
nropen_iochannelReturns number of opened I/O data channels.
test
unittest_io_iochannelTest iochannel_t functionality.
iochannel_tDescribes an input/output channel like a file or network connection.
lifetime
iochannel_FREEStatic initializer.
initcopy_iochannelMakes ioc a duplicate of from_ioc.
free_iochannelCloses the I/O channel (file descriptor).
query
isfree_iochannelReturns true if ioc equals iochannel_FREE.
isvalid_iochannelReturns true if ioc is initialized and valid.
accessmode_iochannelReturns accessmode_e for an io channel.
I/O
read_iochannelReads size bytes from the data stream referenced by ioc into buffer.
write_iochannelWrites size bytes from buffer to the data stream referenced by ioc.
inline implementation
Functions
isfree_iochannelImplements iochannel_t.isfree_iochannel.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2013 Jörg Seebohn

Files

C-kern/api/io/iochannel.h

Header file IOChannel.

C-kern/platform/Linux/io/iochannel.c

Implementation file IOChannel impl.

Types

struct iochannel_t

Make iochannel_t an alias of sys_iochannel_t.

Enumerations

iochannel_e

Standard channels which are usually open at process start by convention.

iochannel_STDINThe default standard input channel.
iochannel_STDOUTThe default standard output channel.
iochannel_STDERRThe default standard error (output) channel.

Functions

Summary
query
nropen_iochannelReturns number of opened I/O data channels.
test
unittest_io_iochannelTest iochannel_t functionality.

query

nropen_iochannel

int nropen_iochannel(/*out*/size_t *number_open)

Returns number of opened I/O data channels.  The number of underlying data streams could be smaller cause oseveral I/O channel could reference the same data object.  You can use this function at the beginning and end of any transaction to check if an I/O object (file, network socket...) was not closed properly.

test

unittest_io_iochannel

int unittest_io_iochannel(void)

Test iochannel_t functionality.

iochannel_t

typedef sys_iochannel_t iochannel_t

Describes an input/output channel like a file or network connection.  On POSIX systems it is a synonym for a file descriptor.

A sys_iochannel_t, which is returned by io_directory and other functions can be manipulated by this interface.  Do not free a descriptor which is returned by such functions else the underlying object will no more work.

If you use initcopy_iochannel to initialize an iochannel_t from a value acquired by a call to <io_socket> or any other such function, you need to call free_iochannel.

Summary
lifetime
iochannel_FREEStatic initializer.
initcopy_iochannelMakes ioc a duplicate of from_ioc.
free_iochannelCloses the I/O channel (file descriptor).
query
isfree_iochannelReturns true if ioc equals iochannel_FREE.
isvalid_iochannelReturns true if ioc is initialized and valid.
accessmode_iochannelReturns accessmode_e for an io channel.
I/O
read_iochannelReads size bytes from the data stream referenced by ioc into buffer.
write_iochannelWrites size bytes from buffer to the data stream referenced by ioc.

lifetime

iochannel_FREE

#define iochannel_FREE sys_iochannel_FREE

Static initializer.

initcopy_iochannel

int initcopy_iochannel(/*out*/iochannel_t *ioc,
iochannel_t from_ioc)

Makes ioc a duplicate of from_ioc.  Freeing ioc does not affect from_ioc.  Writing to ioc or reading from it does affect the content of the underlying I/O data stream.

free_iochannel

int free_iochannel(iochannel_t *ioc)

Closes the I/O channel (file descriptor).  If any other iochannel_t references the same I/O data stream (file, network connection,...) the stream keeps opened.  Do not call this function if you got the iochannel_t from an io_XXX function.  Such functions return the original iochannel_t and do not make a copy.

query

isfree_iochannel

static inline bool isfree_iochannel(const iochannel_t ioc)

Returns true if ioc equals iochannel_FREE.

isvalid_iochannel

bool isvalid_iochannel(const iochannel_t ioc)

Returns true if ioc is initialized and valid.  A return value of true implies isfree_iochannel returns false.  The function checks that ioc refers to a valid iochannel which is known to the operating system.  It is therefore more costly than isfree_iochannel.

accessmode_iochannel

uint8_t accessmode_iochannel(const iochannel_t ioc)

Returns accessmode_e for an io channel.  Returns accessmode_READ, accessmode_WRITE or the combination accessmode_RDWR.  In case of an error accessmode_NONE is returned.

I/O

read_iochannel

int read_iochannel(iochannel_t ioc,
size_t size,
/*out*/void *buffer/*[size]*/,
/*out*/size_t *bytes_read)

Reads size bytes from the data stream referenced by ioc into buffer.  If an error occurrs or end of input is reached less then size bytes are returned.  Less then size bytes could be returned also if the data stream is configured to operate in non blocking mode which is recommended.  Check value bytes_read to determine how many bytes were read.

Returns

0Read data and bytes_read contains the number of read bytes.  If bytes_read is 0 end of input (end of file) is reached.
EAGAINData stream operates in non blocking mode and no bytes could be read.  No error log is written.
EBADFioc is closed, has an invalid value or is not open for reading.

write_iochannel

int write_iochannel(iochannel_t ioc,
size_t size,
const void *buffer/*[size]*/,
/*out*/size_t *bytes_written)

Writes size bytes from buffer to the data stream referenced by ioc.  Returns EAGAIN in case data stream is in non blocking mode and no bytes could be written (due to no more system buffer space).  Less then size bytes could be written if the data stream is configured to operate in non blocking mode which is recommended.  Check value bytes_written to determine how many bytes were written.

Returns

0The first *bytes_written data bytes from buffer are written.
EAGAINData stream operates in non blocking mode and no bytes could be written.  No error log is written.
EPIPEReceiver has closed its connection or closed it during a blocking write.  No error log is written.
EBADFioc is closed, has an invalid value or is not open for writing.

inline implementation

Functions

isfree_iochannel

static inline bool isfree_iochannel(iochannel_t ioc)

Implements iochannel_t.isfree_iochannel.

Describes an input/output channel which is implemented as file descriptor on POSIX systems.
Implements IOChannel.
typedef sys_iochannel_t iochannel_t
Describes an input/output channel like a file or network connection.
#define sys_iochannel_t int
Choose Posix file descriptor type.
int nropen_iochannel(/*out*/size_t *number_open)
Returns number of opened I/O data channels.
int unittest_io_iochannel(void)
Test iochannel_t functionality.
#define iochannel_FREE sys_iochannel_FREE
Static initializer.
int initcopy_iochannel(/*out*/iochannel_t *ioc,
iochannel_t from_ioc)
Makes ioc a duplicate of from_ioc.
int free_iochannel(iochannel_t *ioc)
Closes the I/O channel (file descriptor).
static inline bool isfree_iochannel(const iochannel_t ioc)
Returns true if ioc equals iochannel_FREE.
bool isvalid_iochannel(const iochannel_t ioc)
Returns true if ioc is initialized and valid.
uint8_t accessmode_iochannel(const iochannel_t ioc)
Returns accessmode_e for an io channel.
Determines how you can access a data block (on disk or in memory).
int read_iochannel(iochannel_t ioc,
size_t size,
/*out*/void *buffer/*[size]*/,
/*out*/size_t *bytes_read)
Reads size bytes from the data stream referenced by ioc into buffer.
int write_iochannel(iochannel_t ioc,
size_t size,
const void *buffer/*[size]*/,
/*out*/size_t *bytes_written)
Writes size bytes from buffer to the data stream referenced by ioc.
static inline bool isfree_iochannel(iochannel_t ioc)
Implements iochannel_t.isfree_iochannel.
#define io_directory(dir) (dirfd((DIR*)CONST_CAST(directory_t, dir)))
Implements directory_t.io_directory.
Allows for reading (only).
Allows for writing (only).
Combination of accessmode_READ and accessmode_WRITE.
Neither read nor write nor exec is allowed.
Close