As of erts version 5.5.3 the driver interface has been extended
(see
Old drivers (compiled with an
The
The driver call-back functions are called synchronously from the erlang emulator. If they take too long before completing, they can cause timeouts in the emulator. Use the queue or asynchronous calls if necessary, since the emulator must be responsive.
The driver structure contains the name of the driver and some 15 function pointers. These pointers are called at different times by the emulator.
The only exported function from the driver is
When writing a driver in C++, the driver entry should be of
When the driver has passed the
Do not declare the
typedef struct erl_drv_entry {
int (*init)(void); /* called at system start up for statically
linked drivers, and after loading for
dynamically loaded drivers */
#ifndef ERL_SYS_DRV
ErlDrvData (*start)(ErlDrvPort port, char *command);
/* called when open_port/2 is invoked.
return value -1 means failure. */
#else
ErlDrvData (*start)(ErlDrvPort port, char *command, SysDriverOpts* opts);
/* special options, only for system driver */
#endif
void (*stop)(ErlDrvData drv_data);
/* called when port is closed, and when the
emulator is halted. */
void (*output)(ErlDrvData drv_data, char *buf, int len);
/* called when we have output from erlang to
the port */
void (*ready_input)(ErlDrvData drv_data, ErlDrvEvent event);
/* called when we have input from one of
the driver's handles) */
void (*ready_output)(ErlDrvData drv_data, ErlDrvEvent event);
/* called when output is possible to one of
the driver's handles */
char *driver_name; /* name supplied as command
in open_port XXX ? */
void (*finish)(void); /* called before unloading the driver -
DYNAMIC DRIVERS ONLY */
void *handle; /* Reserved -- Used by emulator internally */
int (*control)(ErlDrvData drv_data, unsigned int command, char *buf,
int len, char **rbuf, int rlen);
/* "ioctl" for drivers - invoked by
port_control/3) */
void (*timeout)(ErlDrvData drv_data); /* Handling of timeout in driver */
void (*outputv)(ErlDrvData drv_data, ErlIOVec *ev);
/* called when we have output from erlang
to the port */
void (*ready_async)(ErlDrvData drv_data, ErlDrvThreadData thread_data);
void (*flush)(ErlDrvData drv_data);
/* called when the port is about to be
closed, and there is data in the
driver queue that needs to be flushed
before 'stop' can be called */
int (*call)(ErlDrvData drv_data, unsigned int command, char *buf,
int len, char **rbuf, int rlen, unsigned int *flags);
/* Works mostly like 'control', a syncronous
call into the driver. */
void (*event)(ErlDrvData drv_data, ErlDrvEvent event,
ErlDrvEventData event_data);
/* Called when an event selected by
driver_event() has occurred */
int extended_marker; /* ERL_DRV_EXTENDED_MARKER */
int major_version; /* ERL_DRV_EXTENDED_MAJOR_VERSION */
int minor_version; /* ERL_DRV_EXTENDED_MINOR_VERSION */
int driver_flags; /* ERL_DRV_FLAGs */
void *handle2; /* Reserved -- Used by emulator internally */
void (*process_exit)(ErlDrvData drv_data, ErlDrvMonitor *monitor);
/* Called when a process monitor fires */
void (*stop_select)(ErlDrvEvent event, void* reserved);
/* Called to close an event object */
} ErlDrvEntry;
This is called directly after the driver has been loaded by
This is called when the driver is instantiated, when
ERL_DRV_ERROR_GENERAL - general error, no error code
ERL_DRV_ERROR_ERRNO - error with error code in erl_errno
ERL_DRV_ERROR_BADARG - error, badarg
If an error code is returned, the port isn't started.
This is called when the port is closed, with
This is called when an erlang process has sent data to the
port. The data is pointed to by
This is called when a driver event (given in the
On unix the
On Windows the
To use this with threads and asynchronous routines, create a
pipe on unix and an Event on Windows. When the routine
completes, write to the pipe (use
This is the name of the driver, it must correspond to the
atom used in
This function is called by the
The driver is only unloaded as a result of calling
This field is reserved for the emulator's internal use. The
emulator will modify this field; therefore, it is important
that the
This is a special routine invoked with the erlang function
This is the fastest way of calling a driver and get a response. It won't make any context switch in the erlang emulator, and requires no message passing. It is suitable for calling C function to get faster execution, when erlang is too slow.
If the driver wants to return data, it should return it in
If the flag is set to
If the flag is set to
Using binaries is faster if more than a few bytes are returned.
The return value is the number of bytes returned in
This function is called any time after the driver's timer
reaches 0. The timer is activated with
This function is called whenever the port is written to. If
it is
The
This function is called after an asynchronous call has
completed. The asynchronous call is started with
This function is called from
The return value is the number of bytes returned in
Intentionally left undocumented.
This field should either be equal to
This field should equal
This field should equal
This field is used to pass driver capability information to the
runtime system. If the
This field is reserved for the emulator's internal use. The
emulator will modify this field; therefore, it is important
that the
This callback is called when a monitored process exits. The
This function is called on behalf of
A typical implementation on Unix is to do
Argument
In contrast to most of the other call-back functions,
It is not allowed to call any functions in the