 |
» |
|
|
 |
You must use the EVM read and write functions
to write a program that performs any of the following operations: Passes events to another process through a pipe or
socket connection Analyzes events stored in a file Receives events from a process other than the EVM
daemon
You cannot write events directly using the standard
UNIX write functions, because the event handle contains only a pointer
to the body of the event and because the location of the body can
change each time the event is modified. Conversely, when you read
an event, it is not enough to read the body; a handle must be created
to enable you to reference the event through the API functions. Example 4-5 shows how to write events to a file, to read them from a file into
your program, and to validate them. Example 4-5 introduces the following functions: EvmEventWrite — Writes an
event to an open file descriptor. For more information about this
function, see EvmEventWrite(3). EvmEventRead — Creates a
new event structure and populates it with an event read from a file
descriptor. EvmEventDestroy must be used to free
the new event. For more information about this function, see EvmEventRead(3). EvmEventValidate — Performs
a data integrity check on the event. This check validates an event
that is received over a connection or retrieved from storage. For
more information about this function, see EvmEventValidate(3).
Example 4-5 Reading and Writing
Events  |
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <evm/evm.h>
int main()
{ EvmEvent_t event_in,event_out;
EvmStatus_t status;
EvmItemValue_t itemval,itemval_name;
int fd; /* Create an event containing a name. */
itemval_name.NAME="sys.unix.evm";
EvmEventCreateVa(&event_out,EvmITEM_NAME, itemval_name, EvmITEM_NONE);
/* Create an output log file and use EvmEventWrite() to write the event to it.
You can write the event to any file descriptor, including a pipe to another process because
the event is a binary data package, do not write it directly to a terminal or printer.*/
fd = open("eventlog",O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd < 0)
{fprintf(stderr,"Failed to open output log file\n");
exit(1); }
status = EvmEventWrite(fd,event_out);
if (status != EvmERROR_NONE)
{fprintf(stderr,"Failed to write event to log file\n");
exit(1); }
/* Read the event back in using EvmEventRead(). Note that you produce a different event this time,
and you have to supply a pointer to the event handle, not the handle itself. */
lseek(fd,0,SEEK_SET);
status = EvmEventRead(fd,&event_in);
if (status != EvmERROR_NONE)
{fprintf(stderr,"Failed to read event from log file\n");
exit(1); }
/* Because the incoming event has been outside the control of this process, it is important to verify its integrity.
Use the EvmEventValidate() function to do this each time you either read an event from a file or receive it from
any process other than the EVM daemon.*/
status = EvmEventValidate(event_in);
if (status != EvmERROR_NONE)
{fprintf(stderr,"Event read from logfile is invalid");
exit(1); }
/* You can show that the event just read is the same event that was just written out by retrieving and
displaying its name. */
status = EvmItemGet(event_in,EvmITEM_NAME,&itemval);
if(status == EvmERROR_NONE)
{ fprintf(stdout,"Event name: %s\n",itemval.NAME);
EvmItemRelease(EvmITEM_NAME,itemval); } /* Free the space used by the event.*/
EvmEventDestroy(event_in);
EvmEventDestroy(event_out);} |
 |
|