跳转到主要内容

sd_varlink_connect_address

systemd

3

#include <systemd/sd-varlink.h>

struct ucred; /* defined in <sys/socket.h> */

int

ret

const char *

address

int

ret

const char *

command

char **

argv

int

ret

const char *

url

int

ret

int

fd

int

ret

int

input_fd

int

output_fd

const struct ucred *

override_ucred

Description

These five functions allocate a new client-side Varlink connection object and connect it to a Varlink service. They differ only in how the service to talk to is specified. On success, a reference to the new connection object is returned in ret; the caller owns this reference and must eventually release it with sd_varlink_unref(3), sd_varlink_close_unref(3) or sd_varlink_flush_close_unref(3).

All five functions return immediately; none of them blocks waiting for the connection to be fully established. For socket-based connections the actual connect() may still be in progress when the function returns (see below); the connection object handles the completion transparently.

The returned connection object is not attached to any event loop. There are three ways to drive it: attach it to an sd-event(3) loop with sd_varlink_attach_event(3); run its I/O manually via sd_varlink_process(3) and sd_varlink_wait(3); or simply issue blocking method calls. In particular it is perfectly fine to follow any of these connection functions directly with a synchronous, blocking call such as sd_varlink_call(3), which internally drives the connection (including completing a still-pending connect()) and returns once the reply has been received. In that case no explicit event loop integration or manual processing is required.

unix:<PATH>
Connects to an AF_UNIX socket, equivalent to passing <PATH> to sd_varlink_connect_address(). <PATH> must either be an absolute, normalized file system path, or begin with @ for an abstract namespace socket (for which no path normalization checks are applied).

exec:<PATH>
Forks off the executable at <PATH>, equivalent to passing it to sd_varlink_connect_exec() with no extra arguments. <PATH> must be an absolute, normalized path.

ssh:<HOST>:<PATH>; ssh-unix:<HOST>:<PATH>
Connects to an AF_UNIX socket at the absolute, normalized path <PATH> on the remote SSH host <HOST>. This relies on OpenSSH 9.4 or newer on the server side. Abstract namespace sockets are not supported for this transport. ssh: and ssh-unix: are synonyms.

ssh-exec:<HOST>:<COMMAND>
Invokes <COMMAND> (a command line, split on whitespace with shell-style quoting and unescaping) on the remote SSH host <HOST> and speaks the Varlink protocol over its standard input and output.

If the scheme is none of the above but is otherwise a syntactically valid URL scheme, sd_varlink_connect_url() looks for a bridge helper binary of that name in the directory configured via $SYSTEMD_VARLINK_BRIDGES_DIR (see below). If found and executable, it is invoked like an exec: transport, with the complete, unmodified URL passed as its sole command line argument. This allows additional transports to be plugged in out of tree.

For the natively supported schemes, URL parameterization using ;, ? or # is rejected (these are reserved for possible future use). A vsock: scheme is not currently supported.

The connection objects created by sd_varlink_connect_exec(), and by the exec:, ssh:/ssh-unix:, ssh-exec: and bridge-helper paths of sd_varlink_connect_url(), are bound to the lifetime of the spawned child process. When such a connection object is freed, the associated child process is sent SIGTERM and reaped. The child is also configured to receive SIGTERM if the calling process dies.

Return Value

On success, these functions return a non-negative integer and store a pointer to the new connection object in ret. On failure, they return a negative errno-style error code and leave ret unchanged.

Errors

Returned errors may indicate the following problems:

-EINVAL
A required argument is NULL, or the supplied address, command, or URL is malformed. This includes addresses that do not begin with / or @, abstract namespace names that do not fit into a sockaddr_un structure, and — for sd_varlink_connect_url() — file system paths that are not absolute or not normalized.

-EBADF
For sd_varlink_connect_fd() and sd_varlink_connect_fd_pair(): a supplied file descriptor is negative.

-EPROTONOSUPPORT
For sd_varlink_connect_url(): the URL contains no : separator, uses an unsupported scheme for which no bridge helper binary is available, or makes use of the reserved ;, ? or # URL parameterization characters with a natively supported scheme.

-ENOMEM
Memory allocation failed.

In addition, these functions may propagate any error returned by the underlying system calls they use, such as socket(2), connect(2), fork(2), pipe(2) and the various execution helpers.

Environment

$SYSTEMD_SSH
Used by the ssh:/ssh-unix: and ssh-exec: transports of sd_varlink_connect_url() to override the ssh binary to invoke. May be a plain file name (looked up in $PATH) or an absolute path.

Files

/usr/lib/systemd/varlink-bridges/
Default directory searched for bridge helper binaries when sd_varlink_connect_url() encounters a URL with a scheme that is not natively supported. See sd-varlink(3) for details.

History

See Also

systemd(1), sd-varlink(3), sd_varlink_attach_event(3), sd_varlink_call(3), sd_varlink_send(3), sd_varlink_is_connected(3), sd_listen_fds(3), varlinkctl(1)