sd_varlink_serialize, sd_varlink_deserialize, sd_varlink_get_connection_id, sd_varlink_set_serialize_pending, sd_varlink_server_serialize, sd_varlink_server_deserialize, sd_varlink_server_claim_connection, sd_varlink_server_disconnect_unclaimed_connections — Serialize and deserialize Varlink connections
#include <systemd/sd-varlink.h>
int sd_varlink_serialize( | sd_varlink *link, |
| void **ret_data, | |
| size_t *ret_size, | |
| int **ret_fds, | |
size_t *ret_n_fds); |
int sd_varlink_deserialize( | sd_varlink **ret, |
| const void *data, | |
| size_t size, | |
| int *fds, | |
size_t n_fds); |
int sd_varlink_get_connection_id( | sd_varlink *link, |
const char **ret); |
int sd_varlink_set_serialize_pending( | sd_varlink *link, |
int b); |
int sd_varlink_server_serialize( | sd_varlink_server *server, |
| void **ret_data, | |
| size_t *ret_size, | |
| int **ret_fds, | |
size_t *ret_n_fds); |
int sd_varlink_server_deserialize( | sd_varlink_server *server, |
| const void *data, | |
| size_t size, | |
| int *fds, | |
size_t n_fds); |
int sd_varlink_server_claim_connection( | sd_varlink_server *server, |
| const char *id, | |
sd_varlink **ret); |
int sd_varlink_server_disconnect_unclaimed_connections( | sd_varlink_server *server); |
These functions allow the state of Varlink connections (either of individual client side connections, or of all connections of a server object) to be serialized into an opaque data blob and an array of file descriptors, and subsequently deserialized again into new connection objects. The connections themselves, any buffered input and output, and any ongoing method calls are preserved, so that the peers do not notice. The listening sockets of a server object are not covered. This may be used by programs that reexecute themselves (i.e. invoke execve(2) on their own binary) to keep their Varlink connections across the reexecution, or to hand connections over to a different process, for example a worker process.
sd_varlink_serialize() serializes the transport state of a client side
Varlink connection object: the input that has been read from the connection but not processed yet, the
output that has been generated but not written yet, the file descriptors passed along with either,
which replies are still expected, and the connection file descriptors themselves. It
returns an opaque, freshly allocated data blob in ret_data and
ret_size, and an array of file descriptors in ret_fds and
ret_n_fds. The file descriptors are duplicates owned by the caller, and need to
be passed on along with the data blob to wherever the connection shall be deserialized, in the order
they are returned in.
The caller is responsible for freeing the data blob and the array, and for closing the file
descriptors. Settings the program chooses itself, such as the userdata pointer, the callbacks, the
event loop attachment, the description, the timeout, whether file descriptor passing is allowed, or
whether input is security sensitive, are not serialized and need to be configured again by the program
after deserialization. Peer credentials are not serialized either, unless they have been explicitly
specified when the connection was established, since they can be acquired from the kernel
again. The peer's PID file descriptor is passed on, however, if it has been acquired already (for
example via sd_varlink_get_peer_pidfd()), since it cannot be acquired again once
the peer has exited.
sd_varlink_deserialize() is the inverse of
sd_varlink_serialize(), and allocates a new Varlink connection object from the
data blob in data and size and the file descriptors in
fds and n_fds. On success, it takes possession of all
passed file descriptors: those it needs are duplicated, and all of them are closed and their array
entries set to -EBADF. On failure the file descriptors are left untouched. This
function may only be used for client side connections, server side connections need to be restored via
sd_varlink_server_deserialize().
Server side connections cannot be serialized individually with
sd_varlink_serialize(), since they can only be restored together with the server
object they belong to. Use sd_varlink_server_serialize() (see below) to serialize
them.
Serialization is refused while a method call or reply is being dispatched, i.e. it may not be invoked from a method or reply callback for the connection being serialized, or while a method call implemented as fiber is suspended. Connections that have been disconnected already cannot be serialized, and neither can connections to programs forked off via sd_varlink_connect_exec(3).
A server side connection with a pending method call (i.e. one that has not been replied to yet,
with the connection object referenced by the program in order to reply later) is only serialized if
sd_varlink_set_serialize_pending() has been called on it with a true
b parameter while the method call was dispatched. Otherwise, the connection is
disconnected on serialization, since the program would not be able to reply to the method call after
deserialization. The setting is reset whenever a method call is completed, and defaults to off. The program
is expected to remember an identifier of the connection, as returned by
sd_varlink_get_connection_id(), in its own serialization, so that it can claim the
connection again after deserialization. Server side connections that are in the process of being upgraded
to a different protocol are always disconnected on serialization.
sd_varlink_get_connection_id() returns an identifier string for the
connection that remains stable across serialization. The string should be considered opaque. It is
owned by the connection object and remains valid until the connection is closed, hence the caller needs
to copy it if it shall be used beyond that. The identifier is derived from the underlying socket or
pipe, and is unique among all such objects existing at the same time. It might be reused once the
underlying object has been destroyed, though.
sd_varlink_server_serialize() serializes all connections of a Varlink
server object, in the same way as sd_varlink_serialize() does for client side
connections, including the method call currently pending on each connection. Connections with
pending method calls that have not been marked with
sd_varlink_set_serialize_pending() are disconnected first. The listening sockets
of the server are not included in the serialization, and need to be passed on by the program
itself.
sd_varlink_server_deserialize() restores the connections serialized with
sd_varlink_server_serialize() into the specified server object. Before this
function is called, the server object should be fully set up already, i.e. its flags set, its method
callbacks bound, and the event loop attached. It does not have to be configured identically to the
server object the connections were serialized from: the restored connections are configured
according to the settings of the server object they are restored into, as for any new connection. For
example, a newer version of a program may enable file descriptor passing on its server object, which
then applies to the restored connections too, even if the older version did not allow it. Similarly, if
SD_VARLINK_SERVER_INHERIT_USERDATA is set, the restored connections inherit the
userdata pointer of the server object they are restored into (and not the one of the server object
they were serialized from). Restored connections with buffered input are processed once
the event loop runs. Ownership of the file descriptors is handled the same way as for
sd_varlink_deserialize().
Restored connections with a pending method call are put in a table of unclaimed connections,
keyed by their identifier. If two such connections share the same identifier (which is only possible
for identifiers derived from inode numbers), the second one is disconnected right away.
sd_varlink_server_claim_connection() looks up the connection with the specified
identifier (as previously returned by sd_varlink_get_connection_id()) in this
table, and removes it from there. The connection is returned in ret, without
taking a reference to it, and the program should take a reference before replying, the same way it
would while dispatching a method call. Once the program has deserialized all of its state,
sd_varlink_server_disconnect_unclaimed_connections() should be called to disconnect
all connections with pending method calls that no one claimed, so that their peers do not wait for a
reply that never comes.
On success, sd_varlink_server_disconnect_unclaimed_connections() returns the
number of disconnected connections, and all other functions return a non-negative integer. On failure,
they return a negative errno-style error code.
Returned errors may indicate the following problems:
-EINVAL¶An argument is invalid, or a field of the serialized data has an invalid value.
-ENXIO¶A mandatory field is missing from the serialized data, for example because
data serialized with sd_varlink_serialize() is passed to
sd_varlink_server_deserialize(), or vice versa.
-EBUSY¶The connection is currently dispatching a method call or a reply, and hence cannot be serialized.
-ENOTCONN¶The connection has been disconnected.
-EOPNOTSUPP¶The connection is to a program forked off via
sd_varlink_connect_exec(), or is a server side connection passed to
sd_varlink_serialize(), and hence cannot be serialized
individually.
-EBADMSG¶The serialized data is not valid.
-ESRCH¶No unclaimed connection with the specified identifier exists.
-ENOMEM¶Memory allocation failed.
Functions described here are available as a shared
library, which can be compiled against and linked to with the
libsystemd pkg-config(1)
file.
The code described here uses
getenv(3),
which is declared to be not multi-thread-safe. This means that the code calling the functions described
here must not call
setenv(3)
from a parallel thread. It is recommended to only do calls to setenv()
from an early phase of the program when no other threads have been started.
sd_varlink_serialize(),
sd_varlink_deserialize(),
sd_varlink_get_connection_id(),
sd_varlink_set_serialize_pending(),
sd_varlink_server_serialize(),
sd_varlink_server_deserialize(),
sd_varlink_server_claim_connection(), and
sd_varlink_server_disconnect_unclaimed_connections() were added in version
263.