Erl Interface-4.0.1
Erl Interface-4.0.1
Note:
By default, the Erl_Interface library is only guaranteed to be compatible with other Erlang/OTP components
from the same release as the libraries themselves. For information about how to communicate with Erlang/OTP
components from earlier releases, see function ei_set_compat_rel.
Scope
In the following sections, these topics are described:
• Compiling your code for use with Erl_Interface
• Initializing Erl_Interface
• Encoding, decoding, and sending Erlang terms
• Building terms and patterns
• Pattern matching
• Connecting to a distributed Erlang node
• Using the Erlang Port Mapper Daemon (EPMD)
• Sending and receiving Erlang messages
• Remote procedure calls
• Using global names
• Using the registry
Prerequisites
It is assumed that the reader is familiar with the Erlang programming language.
#include "ei.h"
Determine where the top directory of your OTP installation is. To find this, start Erlang and enter the following
command at the Eshell prompt:
To compile your code, ensure that your C compiler knows where to find ei.h by specifying an appropriate -I
argument on the command line, or add it to the CFLAGS definition in your Makefile. The correct value for this path
is $OTPROOT/lib/erl_interface-$EIVSN/include, where:
• $OTPROOT is the path reported by code:root_dir/0 in the example above.
• $EIVSN is the version of the Erl_Interface application, for example, erl_interface-3.2.3.
Compiling the code:
$ cc -c -I/usr/local/otp/lib/erl_interface-3.2.3/include myprog.c
When linking:
• Specify the path to libei.a with -L$OTPROOT/lib/erl_interface-3.2.3/lib.
• Specify the name of the library with -lei.
Do this on the command line or add the flags to the LDFLAGS definition in your Makefile.
Linking the code:
$ ld -L/usr/local/otp/lib/erl_interface-3.2.3/
lib myprog.o -lei -o myprog
On some systems it can be necessary to link with some more libraries (for example, libnsl.a and libsocket.a
on Solaris, or wsock32.lib on Windows) to use the communication facilities of Erl_Interface.
If you use the Erl_Interface functions in a threaded application based on POSIX threads or Solaris threads,
then Erl_Interface needs access to some of the synchronization facilities in your threads package. You must
specify extra compiler flags to indicate which of the packages you use. Define _REENTRANT and either STHREADS
or PTHREADS. The default is to use POSIX threads if _REENTRANT is specified.
ei_x_buff buf;
ei_x_new(&buf);
int i = 0;
ei_x_encode_tuple_header(&buf, 2);
ei_x_encode_atom(&buf, "tobbe");
ei_x_encode_long(&buf, 3928);
ei_x_buff buf;
ei_x_new(&buf);
ei_x_format_wo_ver(&buf, "{~a,~i}", "tobbe", 3928);
For a complete description of the different format directives, see the the ei_x_format_wo_ver function.
The following example is more complex:
ei_x_buff buf;
int i = 0;
ei_x_new(&buf);
ei_x_format_wo_ver(&buf,
"[{name,~a},{age,~i},{data,[{adr,~s,~i}]}]",
"madonna",
21,
"E-street", 42);
ei_print_term(stdout, buf.buff, &i);
ei_x_free(&buf);
As in the previous examples, it is your responsibility to free the memory allocated for Erlang terms. In this example,
ei_x_free() ensures that the data pointed to by buf is released.
int sockfd;
const char* node_name = "einode@durin"; /* An example */
if ((sockfd = ei_connect(ec, nodename)) < 0)
fprintf(stderr, "ERROR: ei_connect failed");
When you use ei_connect to connect to an Erlang node, a connection is first made to epmd and, if the node is
known, a connection is then made to the Erlang node.
C nodes can also register themselves with epmd if they want other nodes in the system to be able to find and connect
to them.
Before registering with epmd, you must first create a listen socket and bind it to a port. Then:
int pub;
pub is a file descriptor now connected to epmd. epmd monitors the other end of the connection. If it detects that the
connection has been closed, the node becomes unregistered. So, if you explicitly close the descriptor or if your node
fails, it becomes unregistered from epmd.
Notice that on some systems (such as VxWorks), a failed node is not detected by this mechanism, as the operating
system does not automatically close descriptors that were left open when the node failed. If a node has failed in this
way, epmd prevents you from registering a new node with the old name, as it thinks that the old name is still in use.
In this case, you must close the port explicitly
ei_x_buff buf;
ei_x_new_with_version(&buf);
ei_x_encode_tuple_header(&buf, 2);
ei_x_encode_pid(&buf, ei_self(ec));
ei_x_encode_atom(&buf, "Hello world");
ei_reg_send(ec,fd,"my_server",buf,buf.index);
The first element of the tuple that is sent is your own pid. This enables my_server to reply. For more information
about the primitives, see the ei_connect module.
erlang_msg msg;
int index = 0;
int version;
int arity = 0;
erlang_pid pid;
ei_x_buff buf;
ei_x_new(&buf);
for (;;) {
int got = ei_xreceive_msg(fd, &msg, &x);
if (got == ERL_TICK)
continue;
if (got == ERL_ERROR) {
fprintf(stderr, "ei_xreceive_msg, got==%d", got);
exit(1);
}
break;
}
ei_decode_version(buf.buff, &index, &version);
ei_decode_tuple_header(buf.buff, &index, &arity);
if (arity != 2) {
fprintf(stderr, "got wrong message");
exit(1);
}
ei_decode_pid(buf.buff, &index, &pid);
To provide robustness, a distributed Erlang node occasionally polls all its connected neighbors in an attempt to detect
failed nodes or communication links. A node that receives such a message is expected to respond immediately with
an ERL_TICK message. This is done automatically by ei_xreceive_msg(). However, when this has occurred,
ei_xreceive_msg returns ERL_TICK to the caller without storing a message into the erlang_msg structure.
When a message has been received, it is the caller's responsibility to free the received message.
For more information, see the ei_connect and ei modules.
ei_x_new(&result);
ei_x_new(&args);
ei_x_encode_list_header(&args, 1);
ei_x_encode_pid(&args, &check_pid);
ei_x_encode_empty_list(&args);
For more information about ei_rpc() and its companions ei_rpc_to() and ei_rpc_from(), see the
ei_connect module.
char **names;
int count;
int i;
names = ei_global_names(ec,fd,&count);
if (names)
for (i=0; i<count; i++)
printf("%s\n",names[i]);
free(names);
ei_global_names allocates and returns a buffer containing all the names known to the global module in
Kernel. count is initialized to indicate the number of names in the array. The array of strings in names is terminated
by a NULL pointer, so it is not necessary to use count to determine when the last name is reached.
It is the caller's responsibility to free the array. ei_global_names allocates the array and all the strings using a
single call to malloc(), so free(names) is all that is necessary.
To look up one of the names:
ETERM *pid;
char node[256];
erlang_pid the_pid;
if (ei_global_whereis(ec,fd,"schedule",&the_pid,node) < 0)
fprintf(stderr, "ei_global_whereis error\n");
If "schedule" is known to the global module in Kernel, an Erlang pid is written to the_pid. This pid that can
be used to send messages to the schedule service. Also, node is initialized to contain the name of the node where the
service is registered, so that you can make a connection to it by simply passing the variable to ei_connect.
Before registering a name, you should already have registered your port number with epmd. This is not strictly
necessary, but if you neglect to do so, then other nodes wishing to communicate with your service cannot find or
connect to your process.
Create a name that Erlang processes can use to communicate with your service:
ei_global_register(fd,servicename,ei_self(ec));
After registering the name, use ei_accept to wait for incoming connections.
Note:
Remember to free pid later with ei_x_free.
To unregister a name:
ei_global_unregister(ec,fd,servicename);
This section describes the use of the registry, a simple mechanism for storing key-value pairs in a C-node, as well as
backing them up or restoring them from an Mnesia table on an Erlang node. For more detailed information about
the individual API functions, see the registry module.
Keys are strings, that is, NULL-terminated arrays of characters, and values are arbitrary objects. Although integers
and floating point numbers are treated specially by the registry, you can store strings or binary objects of any type
as pointers.
To start, open a registry:
ei_reg *reg;
reg = ei_reg_open(45);
The number 45 in the example indicates the approximate number of objects that you expect to store in the registry.
Internally the registry uses hash tables with collision chaining, so there is no absolute upper limit on the number of
objects that the registry can contain, but if performance or memory usage is important, then you are to choose a number
accordingly. The registry can be resized later.
You can open as many registries as you like (if memory permits).
Objects are stored and retrieved through set and get functions. The following example shows how to store integers,
floats, strings, and arbitrary binary objects:
ei_reg_setival(reg,"age",29);
ei_reg_setfval(reg,"height",1.85);
strcpy(name,"Martin");
ei_reg_setsval(reg,"name",name);
b->l = 42;
b->m = 12;
ei_reg_setpval(reg,"jox",b,sizeof(*b));
If you try to store an object in the registry and there is an existing object with the same key, the new value replaces the
old one. This is done regardless of whether the new object and the old one have the same type, so you can, for example,
replace a string with an integer. If the existing value is a string or binary, it is freed before the new value is assigned.
Stored values are retrieved from the registry as follows:
long i;
double f;
char *s;
struct bonk *b;
int size;
i = ei_reg_getival(reg,"age");
f = ei_reg_getfval(reg,"height");
s = ei_reg_getsval(reg,"name");
b = ei_reg_getpval(reg,"jox",&size);
In all the above examples, the object must exist and it must be of the right type for the specified operation. If you do
not know the type of an object, you can ask:
ei_reg_stat(reg,"name",&buf);
ei_reg_delete(reg,"name");
When you are finished with a registry, close it to remove all the objects and free the memory back to the system:
ei_reg_close(reg);
This example back up the contents of the registry to the specified Mnesia table "mtab". Once a registry has been
backed up to Mnesia like this, more backups only affect objects that have been modified since the most recent backup,
that is, objects that have been created, changed, or deleted. The backup operation is done as a single atomic transaction,
so that either the entire backup is performed or none of it.
Likewise, a registry can be restored from a Mnesia table:
This reads the entire contents of "mtab" into the specified registry. After the restore, all the objects in the registry
are marked as unmodified, so a later backup only affects objects that you have modified since the restore.
Notice that if you restore to a non-empty registry, objects in the table overwrite objects in the registry with the same
keys. Also, the entire contents of the registry is marked as unmodified after the restore, including any modified objects
that were not overwritten by the restore operation. This may not be your intention.
2 Reference Manual
ei
C Library
The library ei contains macros and functions to encode and decode the Erlang binary term format.
ei allows you to convert atoms, lists, numbers, and binaries to and from the binary format. This is useful when writing
port programs and drivers. ei uses a given buffer, no dynamic memory (except ei_decode_fun()) and is often
quite fast.
ei also handles C-nodes, C-programs that talks Erlang distribution with Erlang nodes (or other C-nodes) using the
Erlang distribution format.The ei library is thread safe, and using threads, one process can handle multiple C-nodes.
The decode and encode functions use a buffer and an index into the buffer, which points at the point where to encode
and decode. The index is updated to point right after the term encoded/decoded. No checking is done whether the term
fits in the buffer or not. If encoding goes outside the buffer, the program can crash.
All functions take two parameters:
• buf is a pointer to the buffer where the binary data is or will be.
• index is a pointer to an index into the buffer. This parameter is incremented with the size of the term decoded/
encoded.
The data is thus at buf[*index] when an ei function is called.
All encode functions assume that the buf and index parameters point to a buffer large enough for the data. To get the
size of an encoded term, without encoding it, pass NULL instead of a buffer pointer. Parameter index is incremented,
but nothing will be encoded. This is the way in ei to "preflight" term encoding.
There are also encode functions that use a dynamic buffer. It is often more convenient to use these to encode data. All
encode functions comes in two versions; those starting with ei_x_ use a dynamic buffer of type ei_x_buff.
All functions return 0 if successful, otherwise -1 (for example, if a term is not of the expected type, or the data to
decode is an invalid Erlang term).
Some of the decode functions need a pre-allocated buffer. This buffer must be allocated large enough, and for non-
compound types the ei_get_type() function returns the size required (notice that for strings an extra byte is
needed for the NULL-terminator).
Data Types
ei_term
typedef struct {
char ei_type;
int arity;
int size;
union {
long i_val;
double d_val;
char atom_name[MAXATOMLEN_UTF8];
erlang_pid pid;
erlang_port port;
erlang_ref ref;
} value;
} ei_term;
Structure written by ei_decode_ei_term(). The ei_type field is the type of the term which equals to
what ei_get_type() sets *type to.
ei_x_buff
A dynamically resized buffer. It is a struct with two fields of interest for the user:
char *buff
Pointer to the dynamically allocated buffer.
int index
Offset to the next byte to write which also equals the amount of bytes currently written.
An ei_x_buff is initialized by calling either ei_x_new() or ei_x_new_with_version(). The
memory used by an initialized ei_x_buff is released by calling ei_x_free().
erlang_char_encoding
typedef enum {
ERLANG_ASCII = 1,
ERLANG_LATIN1 = 2,
ERLANG_UTF8 = 4
} erlang_char_encoding;
The character encodings used for atoms. ERLANG_ASCII represents 7-bit ASCII. Latin-1 and UTF-8 are
different extensions of 7-bit ASCII. All 7-bit ASCII characters are valid Latin-1 and UTF-8 characters. ASCII
and Latin-1 both represent each character by one byte. An UTF-8 character can consist of 1-4 bytes. Notice that
these constants are bit-flags and can be combined with bitwise OR.
erlang_fun
Opaque data type representing an Erlang fun.
erlang_pid
Opaque data type representing an Erlang process identifier.
erlang_port
Opaque data type representing an Erlang port identifier.
erlang_ref
Opaque data type representing an Erlang reference.
erlang_trace
Opaque data type representing an Erlang sequential trace token.
Exports
Returns 0 if a and b are equal. Returns a value less than 0 if a compares as less than b. Returns a value larger than
0 if a compares as larger than b.
int ei_decode_atom_as(const char *buf, int *index, char *p, int plen,
erlang_char_encoding want, erlang_char_encoding* was, erlang_char_encoding*
result)
Types:
erlang_char_encoding
Decodes an atom from the binary format. The NULL-terminated name of the atom is placed in buffer at p of length
plen bytes.
The wanted string encoding is specified by want. The original encoding used in the binary format (Latin-1 or UTF-8)
can be obtained from *was. The encoding of the resulting string (7-bit ASCII, Latin-1, or UTF-8) can be obtained
from *result. Both was and result can be NULL. *result can differ from want if want is a bitwise OR'd
combination like ERLANG_LATIN1|ERLANG_UTF8 or if *result turns out to be pure 7-bit ASCII (compatible
with both Latin-1 and UTF-8).
This function fails if the atom is too long for the buffer or if it cannot be represented with encoding want.
This function was introduced in Erlang/OTP R16 as part of a first step to support UTF-8 atoms.
int ei_decode_binary(const char *buf, int *index, void *p, long *len)
Decodes a binary from the binary format. Parameter len is set to the actual size of the binary. Notice that
ei_decode_binary() assumes that there is enough room for the binary. The size required can be fetched by
ei_get_type().
bitoffsp
Either NULL or *bitoffsp returns the number of unused bits in the first byte pointed to by *pp. The value of
*bitoffsp is between 0 and 7. Unused bits in the first byte are the most significant bits.
nbitsp
Either NULL or *nbitsp returns the length of the bit string in bits.
Returns 0 if it was a bit string term.
The number of bytes pointed to by *pp, which are part of the bit string, is (*bitoffsp + *nbitsp + 7)/8.
If (*bitoffsp + *bitsp)%8 > 0 then only (*bitoffsp + *bitsp)%8 bits of the last byte are used.
Unused bits in the last byte are the least significant bits.
The values of unused bits in the first and last byte are undefined and cannot be relied on.
Number of bits may be divisible by 8, which means a binary decodable by ei_decode_binary is also decodable
by ei_decode_bitstring.
int ei_decode_iodata(const char *buf, int *index, int *size, char *outbuf)
Decodes a term of the type iodata(). The iodata() term will be flattened an written into the buffer pointed to
by the outbuf argument. The byte size of the iodata is written into the integer variable pointed to by the size
argument. Both size and outbuf can be set to NULL. The integer pointed to by the index argument is updated
to refer to the term following after the iodata() term regardless of the the state of the size and the outbuf
arguments.
Note that the buffer pointed to by the outbuf argument must be large enough if a non NULL value is passed as
outbuf. You typically want to call ei_decode_iodata() twice. First with a non NULL size argument and a
NULL outbuf argument in order to determine the size of the buffer needed, and then once again in order to do the
actual decoding. Note that the integer pointed to by index will be updated by the call determining the size as well,
so you need to reset it before the second call doing the actual decoding.
Returns 0 on success and -1 on failure. Failure might be either due to invalid encoding of the term or due to the term
not being of the type iodata(). On failure, the integer pointed to by the index argument will be updated to refer
to the sub term where the failure was detected.
erlang_ref
Decodes a reference from the binary format.
int ei_decode_ulonglong(const char *buf, int *index, unsigned long long *p)
Decodes a GCC unsigned long long or Visual C++ unsigned __int64 (64-bit) integer from the binary
format.
int ei_encode_binary(char *buf, int *index, const void *p, long len)
int ei_x_encode_binary(ei_x_buff* x, const void *p, long len)
Types:
ei_x_buff
Encodes a binary in the binary format. The data is at p, of len bytes length.
int ei_encode_bitstring(char *buf, int *index, const char *p, size_t bitoffs,
size_t nbits)
int ei_x_encode_bitstring(ei_x_buff* x, const char *p, size_t bitoffs, size_t
nbits)
Types:
ei_x_buff
Encodes a bit string in the binary format.
The data is at p. The length of the bit string is nbits bits. The first bitoffs bits of the data at p are unused. The
first byte which is part of the bit string is p[bitoffs/8]. The bitoffs%8 most significant bits of the first byte
p[bitoffs/8] are unused.
The number of bytes which is part of the bit string is (bitoffs + nbits + 7)/8. If (bitoffs + nbits)%8
> 0 then only (bitoffs + nbits)%8 bits of the last byte are used. Unused bits in the last byte are the least
significant bits.
The values of unused bits are disregarded and does not need to be cleared.
Note:
It may seem that there is no way to create a list without knowing the number of elements in advance. But indeed
there is a way. Notice that the list [a, b, c] can be written as [a | [b | [c]]]. Using this, a list can
be written as conses.
while (something()) {
ei_x_encode_list_header(&x, 1);
ei_x_encode_ulong(&x, i); /* just an example */
}
ei_x_encode_empty_list(&x);
ei_x_encode_map_header(&x, 2);
ei_x_encode_atom(&x, "a");
ei_x_encode_string(&x, "Apple");
ei_x_encode_atom(&x, "b");
ei_x_encode_string(&x, "Banana");
erlang_trace
Encodes an Erlang trace token in the binary format. Parameter p points to a erlang_trace structure which should
have been obtained earlier with ei_decode_trace().
int ei_get_type(const char *buf, const int *index, int *type, int *size)
Returns the type in *type and size in *size of the encoded term. For strings and atoms, size is the number of
characters not including the terminating NULL. For binaries and bitstrings, *size is the number of bytes. For lists,
tuples and maps, *size is the arity of the object. For bignum integers, *size is the number of bytes for the absolute
value of the bignum. For other types, *size is 0. In all cases, index is left unchanged.
Currently *type is one of:
ERL_ATOM_EXT
Decode using either ei_decode_atom(), ei_decode_atom_as(), or ei_decode_boolean().
ERL_BINARY_EXT
Decode using either ei_decode_binary(), ei_decode_bitstring(), or ei_decode_iodata().
ERL_BIT_BINARY_EXT
Decode using ei_decode_bitstring().
ERL_FLOAT_EXT
Decode using ei_decode_double().
ERL_NEW_FUN_EXT
ERL_FUN_EXT
ERL_EXPORT_EXT
Decode using ei_decode_fun().
ERL_SMALL_INTEGER_EXT
ERL_INTEGER_EXT
ERL_SMALL_BIG_EXT
ERL_LARGE_BIG_EXT
Decode using either ei_decode_char(), ei_decode_long(), ei_decode_longlong(),
ei_decode_ulong(), ei_decode_ulonglong(), or ei_decode_bignum().
ERL_LIST_EXT
ERL_NIL_EXT
Decode using either ei_decode_list_header(), or ei_decode_iodata().
ERL_STRING_EXT
Decode using either ei_decode_string(), or ei_decode_iodata().
ERL_MAP_EXT
Decode using ei_decode_map_header().
ERL_PID_EXT
Decode using ei_decode_pid().
ERL_PORT_EXT
Decode using ei_decode_port().
ERL_NEW_REFERENCE_EXT
Decode using ei_decode_ref().
ERL_SMALL_TUPLE_EXT
ERL_LARGE_TUPLE_EXT
Decode using ei_decode_tuple_header().
Instead of decoding a term you can also skipped past it if you are not interested in the data by usage of
ei_skip_term().
int ei_init(void)
Initialize the ei library. This function should be called once (and only once) before calling any other functionality
in the ei library.
Note:
If this function is called, it can only be called once and must be called before any other functions in the ei library
are called.
Note:
This can be useful when you want to hold arbitrary terms: skip them and copy the binary term data to some buffer.
~a An atom, char*
~c A character, char
~s A string, char*
~i An integer, int
~l A long integer, long int
~u A unsigned long integer, unsigned long int
~f A float, float
~d A double float, double float
~p An Erlang pid, erlang_pid*
int ei_x_free(ei_x_buff* x)
Types:
ei_x_buff
Deallocates the dynamically allocated content of the buffer referred by x. After deallocation, the buff field is set
to NULL.
int ei_x_new(ei_x_buff* x)
int ei_x_new_with_version(ei_x_buff* x)
Types:
ei_x_buff
Initialize the dynamically realizable buffer referred to by x. The fields of the structure pointed to by parameter x is
filled in, and a default buffer is allocated. ei_x_new_with_version() also puts an initial version byte, which
is used in the binary format (so that ei_x_encode_version() will not be needed.)
Debug Information
Some tips on what to check when the emulator does not seem to receive the terms that you send:
• Be careful with the version header, use ei_x_new_with_version() when appropriate.
• Turn on distribution tracing on the Erlang node.
• Check the result codes from ei_decode_-calls.
ei_connect
C Library
This module enables C-programs to communicate with Erlang nodes, using the Erlang distribution over TCP/IP.
A C-node appears to Erlang as a hidden node. That is, Erlang processes that know the name of the C-node
can communicate with it in a normal manner, but the node name is not shown in the listing provided by
erlang:nodes/0 in ERTS.
The environment variable ERL_EPMD_PORT can be used to indicate which logical cluster a C-node belongs to.
Time-Out Functions
Most functions appear in a version with the suffix _tmo appended to the function name. Those functions take an
extra argument, a time-out in milliseconds. The semantics is this: for each communication primitive involved in the
operation, if the primitive does not complete within the time specified, the function returns an error and erl_errno
is set to ETIMEDOUT. With communication primitive is meant an operation on the socket, like connect, accept,
recv, or send.
Clearly the time-outs are for implementing fault tolerance, not to keep hard real-time promises. The _tmo functions
are for detecting non-responsive peers and to avoid blocking on socket operations.
A time-out value of 0 (zero) means that time-outs are disabled. Calling a _tmo function with the last argument as 0
is therefore the same thing as calling the function without the _tmo suffix.
As with all other functions starting with ei_, you are not expected to put the socket in non-blocking mode yourself
in the program. Every use of non-blocking mode is embedded inside the time-out functions. The socket will always
be back in blocking mode after the operations are completed (regardless of the result). To avoid problems, leave the
socket options alone. ei handles any socket options that need modification.
In all other senses, the _tmo functions inherit all the return values and the semantics from the functions without the
_tmo suffix.
EI_SCLBK_FLG_FULL_IMPL
If set, the accept(), connect(), writev(), write(), and read() callbacks implements timeouts.
The timeout is passed in the tmo argument and is given in milli seconds. Note that the tmo argument to
these callbacks differ from the timeout arguments in the ei API. Zero means a zero timeout. That is, poll and
timeout immediately unless the operation is successful. EI_SCLBK_INF_TMO (max unsigned) means
infinite timeout. The file descriptor is in blocking mode when a callback is called, and it must be in blocking
mode when the callback returns.
If not set, ei will implement the timeout using select() in order to determine when to call the callbacks
and when to time out. The tmo arguments of the accept(), connect(), writev(), write(), and
read() callbacks should be ignored. The callbacks may be called in non-blocking mode. The callbacks are
not allowed to change between blocking and non-blocking mode. In order for this to work, select() needs
to interact with the socket primitives used the same way as it interacts with the ordinary socket primitives.
If this is not the case, the callbacks need to implement timeouts and this flag should be set.
More flags may be introduced in the future.
int (*socket)(void **ctx, void *setup_ctx)
Create a socket and a context for the socket.
On success it should set *ctx to point to a context for the created socket. This context will be passed to all
other socket callbacks. This function will be passed the same setup_context as passed to the preceeding
ei_connect_init_ussi() or ei_connect_xinit_ussi() call.
Note:
During the lifetime of a socket, the pointer *ctx has to remain the same. That is, it cannot later be relocated.
Note:
During the lifetime of a socket, the pointer *ctx has to remain the same. That is, it cannot later be relocated.
Note:
During the lifetime of a socket, the file descriptor has to remain the same. That is, repeated calls to this callback
with the same context should always report the same file descriptor.
The file descriptor has to be a real file descriptor. That is, no other operation should be able to get the same
file descriptor until it has been released by the close() callback.
Data Types
ei_cnode
Opaque data type representing a C-node. A ei_cnode structure is initialized by calling
ei_connect_init() or friends.
ei_socket_callbacks
typedef struct {
int flags;
int (*socket)(void **ctx, void *setup_ctx);
int (*close)(void *ctx);
int (*listen)(void *ctx, void *addr, int *len, int backlog);
int (*accept)(void **ctx, void *addr, int *len, unsigned tmo);
int (*connect)(void *ctx, void *addr, int len, unsigned tmo);
int (*writev)(void *ctx, const void *iov, int iovcnt, ssize_t *len, unsigned tmo);
int (*write)(void *ctx, const char *buf, ssize_t *len, unsigned tmo);
int (*read)(void *ctx, char *buf, ssize_t *len, unsigned tmo);
int (*handshake_packet_header_size)(void *ctx, int *sz);
int (*connect_handshake_complete)(void *ctx);
int (*accept_handshake_complete)(void *ctx);
int (*get_fd)(void *ctx, int *fd);
} ei_socket_callbacks;
Callbacks functions for a User Supplied Socket Implementation. Documentation of each field can be found in the
User Supplied Socket Implementation section above.
ErlConnect
typedef struct {
char ipadr[4]; /* Ip v4 address in network byte order */
char nodename[MAXNODELEN];
} ErlConnect;
IP v4 address.
erlang_msg
typedef struct {
long msgtype;
erlang_pid from;
erlang_pid to;
char toname[MAXATOMLEN+1];
char cookie[MAXATOMLEN+1];
erlang_trace token;
} erlang_msg;
Exports
int n = 0;
struct in_addr addr;
ei_cnode ec;
addr.s_addr = inet_addr("150.236.14.75");
if (ei_connect_xinit(&ec,
"chivas",
"madonna",
"[email protected]",
&addr;
"cookie...",
n++) < 0) {
fprintf(stderr,"ERROR when initializing: %d",erl_errno);
exit(-1);
}
Example 2:
int ei_get_tracelevel(void)
void ei_set_tracelevel(int level)
Used to set tracing on the distribution. The levels are different verbosity levels. A higher level means more information.
See also section Debug Information.
These functions are not thread safe.
• port is a pointer to an integer containing the port number to bind to. If *port equals 0 when calling
ei_listen(), the socket will be bound to an ephemeral port. On success, ei_listen() will update the
value of *port to the port actually bound to.
• backlog is maximum backlog of pending connections.
ei_listen will create a socket, bind to a port on the local interface identified by adr (or all local interfaces if
ei_listen() is called), and mark the socket as a passive socket (that is, a socket that will be used for accepting
incoming connections).
On success, a file descriptor is returned which can be used in a call to ei_accept(). On failure, ERL_ERROR is
returned and erl_errno is set to EIO.
Also, errno values from socket(2) and connect(2) system calls may be propagated into erl_errno.
int ei_receive_encoded(int fd, char **mbufp, int *bufsz, erlang_msg *msg, int
*msglen)
Types:
erlang_msg
This function is retained for compatibility with code generated by the interface compiler and with code following
examples in the same application.
In essence, the function performs the same operation as ei_xreceive_msg, but instead of using an ei_x_buff,
the function expects a pointer to a character pointer (mbufp), where the character pointer is to point to a memory
area allocated by malloc. Argument bufsz is to be a pointer to an integer containing the exact size (in bytes) of
the memory area. The function may reallocate the memory area and will in such cases put the new size in *bufsz
and update *mbufp.
Returns either ERL_TICK or the msgtype field of the erlang_msg *msg. The length of the message is put in
*msglen. On error a value < 0 is returned.
It is recommended to use ei_xreceive_msg instead when possible, for the sake of readability. However, the
function will be retained in the interface for compatibility and will not be removed in future releases without prior
notice.
Equivalent to ei_receive_encoded with an optional time-out argument, see the description at the beginning of
this manual page.
int ei_reg_send(ei_cnode* ec, int fd, char* server_name, char* buf, int len)
Types:
ei_cnode
Sends an Erlang term to a registered process.
• fd is an open descriptor to an Erlang connection.
• server_name is the registered name of the intended recipient.
• buf is the buffer containing the term in binary format.
• len is the length of the message in bytes.
Returns 0 if successful, otherwise -1. In the latter case it sets erl_errno to EIO.
Example:
Send the atom "ok" to the process "worker":
ei_x_buff x;
ei_x_new_with_version(&x);
ei_x_encode_atom(&x, "ok");
if (ei_reg_send(&ec, fd, x.buff, x.index) < 0)
handle_error();
int ei_reg_send_tmo(ei_cnode* ec, int fd, char* server_name, char* buf, int
len, unsigned timeout_ms)
Types:
ei_cnode
Equivalent to ei_reg_send with an optional time-out argument, see the description at the beginning of this manual
page.
int ei_rpc(ei_cnode *ec, int fd, char *mod, char *fun, const char *argbuf,
int argbuflen, ei_x_buff *x)
int ei_rpc_to(ei_cnode *ec, int fd, char *mod, char *fun, const char *argbuf,
int argbuflen)
int ei_rpc_from(ei_cnode *ec, int fd, int timeout, erlang_msg *msg, ei_x_buff
*x)
Types:
ei_cnode
ei_x_buff
erlang_msg
Supports calling Erlang functions on remote nodes. ei_rpc_to() sends an RPC request to a remote node and
ei_rpc_from() receives the results of such a call. ei_rpc() combines the functionality of these two functions
by sending an RPC request and waiting for the results. See also rpc:call/4 in Kernel.
• ec is the C-node structure previously initiated by a call to ei_connect_init() or ei_connect_xinit().
• fd is an open descriptor to an Erlang connection.
• timeout is the maximum time (in milliseconds) to wait for results. Specify ERL_NO_TIMEOUT to wait forever.
ei_rpc() waits infinitely for the answer, that is, the call will never time out.
• mod is the name of the module containing the function to be run on the remote node.
• fun is the name of the function to run.
• argbuf is a pointer to a buffer with an encoded Erlang list, without a version magic number, containing the
arguments to be passed to the function.
• argbuflen is the length of the buffer containing the encoded Erlang list.
• msg is structure of type erlang_msg and contains information on the message received. For a description of
the erlang_msg format, see ei_receive_msg.
• x points to the dynamic buffer that receives the result. For ei_rpc() this is the result without the version magic
number. For ei_rpc_from() the result returns a version magic number and a 2-tuple {rex,Reply}.
ei_rpc() returns the number of bytes in the result on success and -1 on failure. ei_rpc_from() returns the
number of bytes, otherwise one of ERL_TICK, ERL_TIMEOUT, and ERL_ERROR. When failing, all three functions
set erl_errno to one of the following:
EIO
I/O error.
ETIMEDOUT
Time-out expired.
EAGAIN
Temporary error: Try again.
Example:
Check to see if an Erlang process is alive:
ei_x_new(&result);
ei_x_new(&args);
ei_x_encode_list_header(&args, 1);
ei_x_encode_pid(&args, &check_pid);
ei_x_encode_empty_list(&args);
int ei_send_encoded_tmo(int fd, erlang_pid* to, char* buf, int len, unsigned
timeout_ms)
Types:
erlang_pid
Equivalent to ei_send_encoded with an optional time-out argument, see the description at the beginning of this
manual page.
int ei_send_reg_encoded(int fd, const erlang_pid *from, const char *to, const
char *buf, int len)
Types:
erlang_pid
This function is retained for compatibility with code generated by the interface compiler and with code following
examples in the same application.
The function works as ei_reg_send with one exception. Instead of taking ei_cnode as first argument, it takes
a second argument, an erlang_pid, which is to be the process identifier of the sending process (in the Erlang
distribution protocol).
A suitable erlang_pid can be retrieved from the ei_cnode structure by calling ei_self(cnode_pointer).
int ei_send_tmo(int fd, erlang_pid* to, char* buf, int len, unsigned
timeout_ms)
Types:
erlang_pid
Equivalent to ei_send with an optional time-out argument, see the description at the beginning of this manual page.
Warning:
This function is deprecated and will be removed in a future release.
Debug Information
If a connection attempt fails, the following can be checked:
• erl_errno.
• That the correct cookie was used
• That EPMD is running
• That the remote Erlang node on the other side is running the same version of Erlang as the ei library
• That environment variable ERL_EPMD_PORT is set correctly
The connection attempt can be traced by setting a trace level by either using ei_set_tracelevel or by setting
environment variable EI_TRACELEVEL. The trace levels have the following messages:
• 1: Verbose error messages
• 2: Above messages and verbose warning messages
• 3: Above messages and progress reports for connection handling
• 4: Above messages and progress reports for communication
registry
C Library
Note:
This functionality is deprecated as of OTP 23, and will be removed in OTP 24. Reasonably new gcc compilers
will issue deprecation warnings. In order to disable these warnings, define the macro EI_NO_DEPR_WARN.
This module provides support for storing key-value pairs in a table known as a registry, backing up registries to Mnesia
in an atomic manner, and later restoring the contents of a registry from Mnesia.
Exports
int ei_reg_close(reg)
Types:
ei_reg *reg;
A registry that has previously been created with ei_reg_open() is closed, and all the objects it contains are freed.
reg is the registry to close.
Returns 0.
int ei_reg_delete(reg,key)
Types:
ei_reg *reg;
const char *key;
Deletes an object from the registry. The object is not removed from the registry, it is only marked for later removal so
that on later backups to Mnesia, the corresponding object can be removed from the Mnesia table as well. If another
object is later created with the same key, the object will be reused.
The object is removed from the registry after a call to ei_reg_dump() or ei_reg_purge().
• reg is the registry containing key.
• key is the object to remove.
Returns 0 on success, otherwise -1.
int ei_reg_dump(fd,reg,mntab,flags)
Types:
int fd;
ei_reg *reg;
const char *mntab;
int flags;
Dumps the contents of a registry to a Mnesia table in an atomic manner, that is, either all data or no data is updated.
If any errors are encountered while backing up the data, the entire operation is aborted.
• fd is an open connection to Erlang. Mnesia 3.0 or later must be running on the Erlang node.
• reg is the registry to back up.
• mntab is the name of the Mnesia table where the backed up data is to be placed. If the table does not exist,
it is created automatically using configurable defaults. For information about configuring this behavior, see
Mnesia.
If flags is 0, the backup includes only those objects that have been created, modified, or deleted since the last backup
or restore (that is, an incremental backup). After the backup, any objects that were marked dirty are now clean, and
any objects that had been marked for deletion are deleted.
Alternatively, setting flags to EI_FORCE causes a full backup to be done, and EI_NOPURGE causes the deleted
objects to be left in the registry afterwards. These can be bitwise OR'ed together if both behaviors are desired. If
EI_NOPURGE was specified, ei_reg_purge() can be used to explicitly remove the deleted items from the registry
later.
Returns 0 on success, otherwise -1.
double ei_reg_getfval(reg,key)
Types:
ei_reg *reg;
const char *key;
Gets the value associated with key in the registry. The value must be a floating point type.
• reg is the registry where the object will be looked up.
• key is the name of the object to look up.
On success, the function returns the value associated with key. If the object is not found or if it is not a floating point
object, -1.0 is returned. To avoid problems with in-band error reporting (that is, if you cannot distinguish between
-1.0 and a valid result), use the more general function ei_reg_getval() instead.
int ei_reg_getival(reg,key)
Types:
ei_reg *reg;
const char *key;
Gets the value associated with key in the registry. The value must be an integer.
• reg is the registry where the object will be looked up.
• key is the name of the object to look up.
On success, the function returns the value associated with key. If the object is not found or if it is not an integer object,
-1 is returned. To avoid problems with in-band error reporting (that is, if you cannot distinguish between -1 and a
valid result), use the more general function ei_reg_getval() instead.
On success, the function returns the value associated with key and indicates its length in size. If the object is not
found or if it is not a binary object, NULL is returned. To avoid problems with in-band error reporting (that is, if you
cannot distinguish between NULL and a valid result), use the more general function ei_reg_getval() instead.
int ei_reg_getval(reg,key,flags,v,...)
Types:
ei_reg *reg;
const char *key;
int flags;
void *v (see below)
A general function for retrieving any kind of object from the registry.
• reg is the registry where the object will be looked up.
• key is the name of the object to look up.
• flags indicates the type of object that you are looking for. If flags is 0, any kind of object is returned. If
flags is EI_INT, EI_FLT, EI_STR, or EI_BIN, then only values of that kind are returned.
The buffer pointed to by v must be large enough to hold the return data, that is, it must be a pointer to one of
int, double, char*, or void*, respectively.
If flags is EI_BIN, a fifth argument int *size is required, so that the size of the object can be returned.
On success, v (and size if the object is binary) is initialized with the value associated with key, and the function
returns EI_INT, EI_FLT, EI_STR, or EI_BIN, indicating the type of object. On failure, -1 is returned and the
arguments are not updated.
int ei_reg_markdirty(reg,key)
Types:
ei_reg *reg;
const char *key;
Marks a registry object as dirty. This ensures that it is included in the next backup to Mnesia. Normally this operation
is not necessary, as all of the normal registry 'set' functions do this automatically. However, if you have retrieved
the value of a string or binary object from the registry and modified the contents, then the change is invisible to the
registry and the object is assumed to be unmodified. This function allows you to make such modifications and then
let the registry know about them.
• reg is the registry containing the object.
• key is the name of the object to mark.
ei_reg *ei_reg_open(size)
Types:
int size;
Opens (creates) a registry, which initially is empty. To close the registry later, use ei_reg_close().
size is the approximate number of objects you intend to store in the registry. As the registry uses a hash table with
collision chaining, no absolute upper limit exists on the number of objects that can be stored in it. However, for reasons
of efficiency, it is a good idea to choose a number that is appropriate for your needs. To change the size later, use
ei_reg_resize(). Notice that the number you provide is increased to the nearest larger prime number.
Returns an empty registry on success, otherwise NULL.
int ei_reg_purge(reg)
Types:
ei_reg *reg;
Removes all objects marked for deletion. When objects are deleted with ei_reg_delete() they are not removed
from the registry, only marked for later removal. On a later backup to Mnesia, the objects can also be removed from
the Mnesia table. If you are not backing up to Mnesia, you may wish to remove the objects manually with this
function.
reg is a registry containing objects marked for deletion.
Returns 0 on success, otherwise -1.
int ei_reg_resize(reg,newsize)
Types:
ei_reg *reg;
int newsize;
Changes the size of a registry.
newsize is the new size to make the registry. The number is increased to the nearest larger prime number.
On success, the registry is resized, all contents rehashed, and 0 is returned. On failure, the registry is left unchanged
and -1 is returned.
int ei_reg_restore(fd,reg,mntab)
Types:
int fd;
ei_reg *reg;
const char *mntab;
The contents of a Mnesia table are read into the registry.
• fd is an open connection to Erlang. Mnesia 3.0 or later must be running on the Erlang node.
• reg is the registry where the data is to be placed.
• mntab is the name of the Mnesia table to read data from.
Notice that only tables of a certain format can be restored, that is, those that have been created and backed up to
with ei_reg_dump(). If the registry was not empty before the operation, the contents of the table are added to the
contents of the registry. If the table contains objects with the same keys as those already in the registry, the registry
objects are overwritten with the new values. If the registry contains objects that were not in the table, they are unchanged
by this operation.
After the restore operation, the entire contents of the registry is marked as unmodified. Notice that this includes any
objects that were modified before the restore and not overwritten by the restore.
Returns 0 on success, otherwise -1.
int ei_reg_setfval(reg,key,f)
Types:
ei_reg *reg;
const char *key;
double f;
Creates a key-value pair with the specified key and floating point value f. If an object already exists with the same
key, the new value replaces the old one. If the previous value was a binary or string, it is freed with free().
• reg is the registry where the object is to be placed.
• key is the object name.
• f is the floating point value to assign.
Returns 0 on success, otherwise -1.
int ei_reg_setival(reg,key,i)
Types:
ei_reg *reg;
const char *key;
int i;
Creates a key-value pair with the specified key and integer value i. If an object already exists with the same key,
the new value replaces the old one. If the previous value was a binary or string, it is freed with free().
• reg is the registry where the object is to be placed.
• key is the object name.
• i is the integer value to assign.
Returns 0 on success, otherwise -1.
int ei_reg_setpval(reg,key,p,size)
Types:
ei_reg *reg;
const char *key;
const void *p;
int size;
Creates a key-value pair with the specified key whose "value" is the binary object pointed to by p. If an object already
exists with the same key, the new value replaces the old one. If the previous value was a binary or string, it is freed
with free().
• reg is the registry where the object is to be placed.
• key is the object name.
• p is a pointer to the binary object. The object itself must have been created through a single call to malloc()
or a similar function, so that the registry can later delete it if necessary by calling free().
int ei_reg_setsval(reg,key,s)
Types:
ei_reg *reg;
const char *key;
const char *s;
Creates a key-value pair with the specified key whose "value" is the specified string s. If an object already exists with
the same key, the new value replaces the old one. If the previous value was a binary or string, it is freed with free().
• reg is the registry where the object is to be placed.
• key is the object name.
• s is the string to assign. The string itself must have been created through a single call to malloc() or similar
a function, so that the registry can later delete it if necessary by calling free().
Returns 0 on success, otherwise -1.
int ei_reg_setval(reg,key,flags,v,...)
Types:
ei_reg *reg;
const char *key;
int flags;
v (see below)
Creates a key-value pair with the specified key whose value is specified by v. If an object already exists with the
same key, the new value replaces the old one. If the previous value was a binary or string, it is freed with free().
• reg is the registry where the object is to be placed.
• key is the object name.
• flags indicates the type of the object specified by v. Flags must be one of EI_INT, EI_FLT, EI_STR, and
EI_BIN, indicating whether v is int, double, char*, or void*.
If flags is EI_BIN, a fifth argument size is required, indicating the size in bytes of the object pointed to by v.
If you wish to store an arbitrary pointer in the registry, specify a size of 0. In this case, the object itself is not
transferred by an ei_reg_dump() operation, only the pointer value.
Returns 0 on success, otherwise -1.
int ei_reg_stat(reg,key,obuf)
Types:
ei_reg *reg;
const char *key;
struct ei_reg_stat *obuf;
Returns information about an object.
• reg is the registry containing the object.
• key is the object name.
• obuf is a pointer to an ei_reg_stat structure, defined as follows:
struct ei_reg_stat {
int attr;
int size;
};
In attr the attributes of the object are stored as the logical OR of its type (one of EI_INT, EI_FLT, EI_BIN, and
EI_STR), whether it is marked for deletion (EI_DELET), and whether it has been modified since the last backup
to Mnesia (EI_DIRTY).
Field size indicates the size in bytes required to store EI_STR (including the terminating 0) and EI_BIN objects,
or 0 for EI_INT and EI_FLT.
Returns 0 and initializes obuf on success, otherwise -1.
int ei_reg_tabstat(reg,obuf)
Types:
ei_reg *reg;
struct ei_reg_tabstat *obuf;
Returns information about a registry. Using information returned by this function, you can see whether the size of the
registry is suitable for the amount of data it contains.
• reg is the registry to return information about.
• obuf is a pointer to an ei_reg_tabstat structure, defined as follows:
struct ei_reg_tabstat {
int size;
int nelem;
int npos;
int collisions;
};
Field size indicates the number of hash positions in the registry. This is the number you provided when you created
or last resized the registry, rounded up to the nearest prime number.
• nelem indicates the number of elements stored in the registry. It includes objects that are deleted but not
purged.
• npos indicates the number of unique positions that are occupied in the registry.
• collisions indicates how many elements are sharing positions in the registry.
On success, 0 is returned and obuf is initialized to contain table statistics, otherwise -1 is returned.
ei_global
C Library
This module provides support for registering, looking up, and unregistering names in the global module. For more
information, see kernel:global.
Notice that the functions below perform an RPC using an open file descriptor provided by the caller. This file descriptor
must not be used for other traffic during the global operation, as the function can then receive unexpected data and fail.
Exports
char **ei_global_names(ec,fd,count)
Types:
ei_cnode *ec;
int fd;
int *count;
Retrieves a list of all known global names.
• ec is the ei_cnode representing the current cnode.
• fd is an open descriptor to an Erlang connection.
• count is the address of an integer, or NULL. If count is not NULL, it is set by the function to the number of
names found.
On success, the function returns an array of strings, each containing a single registered name, and sets count to the
number of names found. The array is terminated by a single NULL pointer. On failure, the function returns NULL and
count is not modified.
Note:
It is the caller's responsibility to free the array afterwards. It has been allocated by the function with a single call
to malloc(), so a single free() is all that is necessary.
int ei_global_register(fd,name,pid)
Types:
int fd;
const char *name;
erlang_pid *pid;
Registers a name in global.
• fd is an open descriptor to an Erlang connection.
• name is the name to register in global.
• pid is the pid that is to be associated with name. This value is returned by global when processes request
the location of name.
Returns 0 on success, otherwise -1.
int ei_global_unregister(ec,fd,name)
Types:
ei_cnode *ec;
int fd;
const char *name;
Unregisters a name from global.
• ec is the ei_cnode representing the current cnode.
• fd is an open descriptor to an Erlang connection.
• name is the name to unregister from global.
Returns 0 on success, otherwise -1.
int ei_global_whereis(ec,fd,name,pid,node)
Types:
ei_cnode *ec;
int fd;
const char *name;
erlang_pid *pid;
char *node;
Looks up a name in global.
• ec is the ei_cnode representing the current cnode.
• fd is an open descriptor to an Erlang connection.
• name is the name that is to be looked up in global.
The pid parameter is a pointer to a erlang_pid that the function will update with the pid associated with the global
name, if successful.
If node is not NULL, it is a pointer to a buffer where the function can fill in the name of the node where name is
found. node can be passed directly to ei_connect() if necessary.
On success, the function returns 0, updates the erlang_pid pointed to by the pid parameter, and the node parameter
is initialized to the node name where name is found. On failure, a negative number is returned.
erl_call
Command
erl_call makes it possible to start and/or communicate with a distributed Erlang node. It is built upon the
Erl_Interface library as an example application. Its purpose is to use a Unix shell script to interact with a
distributed Erlang node. It performs all communication with the Erlang rex server, using the standard Erlang RPC
facility. It does not require any special software to be run at the Erlang target node.
The main use is to either start a distributed Erlang node or to make an ordinary function call. However, it is also
possible to pipe an Erlang module to erl_call and have it compiled, or to pipe a sequence of Erlang expressions
to be evaluated (similar to the Erlang shell).
Options, which cause stdin to be read, can be used with advantage, as scripts from within (Unix) shell scripts.
Another nice use of erl_call could be from (HTTP) CGI-bin scripts.
Exports
erl_call <options>
Starts/calls Erlang.
Each option flag is described below with its name, type, and meaning.
-a [Mod [Fun [Args]]]]
(Optional.) Applies the specified function and returns the result. Mod must be specified. However, start
and [] are assumed for unspecified Fun and Args, respectively. Args is to be in the same format as for
erlang:apply/3 in ERTS.
Notice that this flag takes exactly one argument, so quoting can be necessary to group Mod, Fun, and Args in
a manner dependent on the behavior of your command shell.
-address [Hostname:]Port
(One of -n, -name, -sname or -address is required.) Hostname is the hostname of the machine that is
running the peer node that erl_call shall communicate with. The default hostname is the hostname of the
local machine. Port is the port number of the node that erl_call shall communicate with. The -address
flag cannot be combined with any of the flags -n, -name, -sname or -s.
The -address flag is typically useful when one wants to call a node that is running on machine without an
accessible epmd instance.
-c Cookie
(Optional.) Use this option to specify a certain cookie. If no cookie is specified, the ~/.erlang.cookie file is
read and its content is used as cookie. The Erlang node we want to communicate with must have the same cookie.
-d
(Optional.) Debug mode. This causes all I/O to be output to the ~/.erl_call.out.Nodename file, where
Nodename is the node name of the Erlang node in question.
-e
(Optional.) Reads a sequence of Erlang expressions, separated by comma (,) and ended with a full stop (.), from
stdin until EOF (Control-D). Evaluates the expressions and returns the result from the last expression. Returns
{ok,Result} on success.
-h HiddenName
(Optional.) Specifies the name of the hidden node that erl_call represents.
-m
(Optional.) Reads an Erlang module from stdin and compiles it.
-n Node
(One of -n, -name, -sname or -address is required.) Has the same meaning as -name and can still be used
for backward compatibility reasons.
-name Node
(One of -n, -name, -sname or -address is required.) Node is the name of the peer node to be started or
communicated with. It is assumed that Node is started with erl -name, which means that fully qualified long
node names are used. If option -s is specified, an Erlang node will (if necessary) be started with erl -name.
-q
(Optional.) Halts the Erlang node specified with switch -n. This switch overrides switch -s.
-r
(Optional.) Generates a random name of the hidden node that erl_call represents.
-R
(Optional.) Request a dynamic random name, of the hidden node that erl_call represents, from the peer node.
Supported since OTP 23. Prefer -R over -r when doing repeated requests toward the same peer node.
-s
(Optional.) Starts a distributed Erlang node if necessary. This means that in a sequence of calls, where '-s' and
'-n Node' are constant, only the first call starts the Erlang node. This makes the rest of the communication very
fast. This flag is currently only available on Unix-like platforms (Linux, Mac OS X, Solaris, and so on).
-sname Node
(One of -n, -name, -sname or -address is required.) Node is the name of the peer node to be started or
communicated with. It is assumed that Node is started with erl -sname, which means that short node names
are used. If option -s is specified, an Erlang node is started (if necessary) with erl -sname.
-timeout Seconds
(Optional.) Aborts the erl_call process after the timeout expires. Note that this does not abort commands
that have already been started with -a, -e, or similar.
-v
(Optional.) Prints a lot of verbose information. This is only useful for the developer and maintainer of
erl_call.
-x ErlScript
(Optional.) Specifies another name of the Erlang startup script to be used. If not specified, the standard erl
startup script is used.
Examples
To start an Erlang node and call erlang:time/0:
erl_call -s -e -n madonna
statistics(runtime),
X=1,
Y=2,
{_,T}=statistics(runtime),
{X+Y,T}.
^D
{ok,{3,0}}
To compile a module and run it (again, the input ends with EOF (Control-D)):
(In the example, the output has been formatted afterwards.)