Debian Codegraph @

man/sd_varlink_connect_address.xml

raw
1 <?xml version='1.0'?>
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
4 <!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
5
6 <refentry id="sd_varlink_connect_address" xmlns:xi="http://www.w3.org/2001/XInclude">
7
8 <refentryinfo>
9 <title>sd_varlink_connect_address</title>
10 <productname>systemd</productname>
11 </refentryinfo>
12
13 <refmeta>
14 <refentrytitle>sd_varlink_connect_address</refentrytitle>
15 <manvolnum>3</manvolnum>
16 </refmeta>
17
18 <refnamediv>
19 <refname>sd_varlink_connect_address</refname>
20 <refname>sd_varlink_connect_exec</refname>
21 <refname>sd_varlink_connect_url</refname>
22 <refname>sd_varlink_connect_fd</refname>
23 <refname>sd_varlink_connect_fd_pair</refname>
24
25 <refpurpose>Create a Varlink connection object and connect it to a service</refpurpose>
26 </refnamediv>
27
28 <refsynopsisdiv>
29 <funcsynopsis>
30 <funcsynopsisinfo>#include &lt;systemd/sd-varlink.h&gt;</funcsynopsisinfo>
31
32 <funcsynopsisinfo>struct ucred; /* defined in &lt;sys/socket.h&gt; */</funcsynopsisinfo>
33
34 <funcprototype>
35 <funcdef>int <function>sd_varlink_connect_address</function></funcdef>
36 <paramdef>sd_varlink **<parameter>ret</parameter></paramdef>
37 <paramdef>const char *<parameter>address</parameter></paramdef>
38 </funcprototype>
39
40 <funcprototype>
41 <funcdef>int <function>sd_varlink_connect_exec</function></funcdef>
42 <paramdef>sd_varlink **<parameter>ret</parameter></paramdef>
43 <paramdef>const char *<parameter>command</parameter></paramdef>
44 <paramdef>char **<parameter>argv</parameter></paramdef>
45 </funcprototype>
46
47 <funcprototype>
48 <funcdef>int <function>sd_varlink_connect_url</function></funcdef>
49 <paramdef>sd_varlink **<parameter>ret</parameter></paramdef>
50 <paramdef>const char *<parameter>url</parameter></paramdef>
51 </funcprototype>
52
53 <funcprototype>
54 <funcdef>int <function>sd_varlink_connect_fd</function></funcdef>
55 <paramdef>sd_varlink **<parameter>ret</parameter></paramdef>
56 <paramdef>int <parameter>fd</parameter></paramdef>
57 </funcprototype>
58
59 <funcprototype>
60 <funcdef>int <function>sd_varlink_connect_fd_pair</function></funcdef>
61 <paramdef>sd_varlink **<parameter>ret</parameter></paramdef>
62 <paramdef>int <parameter>input_fd</parameter></paramdef>
63 <paramdef>int <parameter>output_fd</parameter></paramdef>
64 <paramdef>const struct ucred *<parameter>override_ucred</parameter></paramdef>
65 </funcprototype>
66 </funcsynopsis>
67 </refsynopsisdiv>
68
69 <refsect1>
70 <title>Description</title>
71
72 <para>These five functions allocate a new client-side Varlink connection object and connect it to a
73 Varlink service. They differ only in how the service to talk to is specified. On success, a reference to
74 the new connection object is returned in <parameter>ret</parameter>; the caller owns this reference and
75 must eventually release it with
76 <citerefentry><refentrytitle>sd_varlink_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
77 <citerefentry><refentrytitle>sd_varlink_close_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>
78 or
79 <citerefentry><refentrytitle>sd_varlink_flush_close_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
80
81 <para>All five functions return immediately; none of them blocks waiting for the connection to be fully
82 established. For socket-based connections the actual <function>connect()</function> may still be in
83 progress when the function returns (see below); the connection object handles the completion
84 transparently.</para>
85
86 <para>The returned connection object is not attached to any event loop. There are three ways to drive it:
87 attach it to an
88 <citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry> loop with
89 <citerefentry><refentrytitle>sd_varlink_attach_event</refentrytitle><manvolnum>3</manvolnum></citerefentry>;
90 run its I/O manually via
91 <citerefentry><refentrytitle>sd_varlink_process</refentrytitle><manvolnum>3</manvolnum></citerefentry> and
92 <citerefentry><refentrytitle>sd_varlink_wait</refentrytitle><manvolnum>3</manvolnum></citerefentry>; or
93 simply issue blocking method calls. In particular it is perfectly fine to follow any of these connection
94 functions directly with a synchronous, blocking call such as
95 <citerefentry><refentrytitle>sd_varlink_call</refentrytitle><manvolnum>3</manvolnum></citerefentry>, which
96 internally drives the connection (including completing a still-pending
97 <function>connect()</function>) and returns once the reply has been received. In that case no explicit
98 event loop integration or manual processing is required.</para>
99
100 <para><function>sd_varlink_connect_address()</function> connects to an <constant>AF_UNIX</constant>
101 stream socket whose address is given as a string in <parameter>address</parameter>. The address must
102 begin either with <literal>/</literal> (to reference a socket in the file system) or with
103 <literal>@</literal> (to reference a socket in the abstract namespace, with the remainder of the string
104 used as the abstract name). It must be at least two characters long. No other forms are accepted (in
105 particular, relative paths are refused). Abstract namespace names that embed <constant>NUL</constant>
106 bytes cannot be expressed through this interface. If a file system socket path is too long to fit into a
107 <structname>sockaddr_un</structname> structure, the connection is established transparently via an
108 <constant>O_PATH</constant> indirection, so overlong paths work.</para>
109
110 <para><function>sd_varlink_connect_exec()</function> forks off a child process and speaks the Varlink
111 protocol with it over a connected <constant>AF_UNIX</constant> <constant>SOCK_STREAM</constant> socket
112 pair. <parameter>command</parameter> is the program to execute; it is looked up in <varname>$PATH</varname>
113 in the usual way (i.e. via <citerefentry
114 project='man-pages'><refentrytitle>execvp</refentrytitle><manvolnum>3</manvolnum></citerefentry>).
115 <parameter>argv</parameter> is the argument vector (a <constant>NULL</constant>-terminated string array)
116 to pass to the child as its <varname>argv[]</varname>; if it is <constant>NULL</constant> or empty, an
117 argument vector consisting of just <parameter>command</parameter> is synthesized. The connected socket is
118 handed to the child as file descriptor 3 using the
119 <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>
120 protocol, i.e. the child is invoked with <varname>$LISTEN_FDS</varname> set to
121 <literal>1</literal>, <varname>$LISTEN_FDNAMES</varname> set to <literal>varlink</literal>, and the
122 appropriate <varname>$LISTEN_PID</varname> (and, where available, <varname>$LISTEN_PIDFDID</varname>)
123 variables. The <parameter>command</parameter> and <parameter>argv</parameter> strings are copied into the
124 connection object, so the caller may free or modify them once the function returns.</para>
125
126 <para><function>sd_varlink_connect_url()</function> is a higher-level interface that parses a service
127 address string with a leading scheme and dispatches to the appropriate transport. Despite the name, these
128 strings are <emphasis>not</emphasis> Internet URLs in the sense of the relevant RFCs. The following schemes
129 are understood natively:</para>
130
131 <variablelist>
132 <varlistentry>
133 <term><literal>unix:</literal><replaceable>PATH</replaceable></term>
134 <listitem><para>Connects to an <constant>AF_UNIX</constant> socket, equivalent to passing
135 <replaceable>PATH</replaceable> to <function>sd_varlink_connect_address()</function>.
136 <replaceable>PATH</replaceable> must either be an absolute, normalized file system path, or begin with
137 <literal>@</literal> for an abstract namespace socket (for which no path normalization checks are
138 applied).</para></listitem>
139 </varlistentry>
140
141 <varlistentry>
142 <term><literal>exec:</literal><replaceable>PATH</replaceable></term>
143 <listitem><para>Forks off the executable at <replaceable>PATH</replaceable>, equivalent to passing it
144 to <function>sd_varlink_connect_exec()</function> with no extra arguments. <replaceable>PATH</replaceable>
145 must be an absolute, normalized path.</para></listitem>
146 </varlistentry>
147
148 <varlistentry>
149 <term><literal>ssh:</literal><replaceable>HOST</replaceable><literal>:</literal><replaceable>PATH</replaceable></term>
150 <term><literal>ssh-unix:</literal><replaceable>HOST</replaceable><literal>:</literal><replaceable>PATH</replaceable></term>
151 <listitem><para>Connects to an <constant>AF_UNIX</constant> socket at the absolute, normalized path
152 <replaceable>PATH</replaceable> on the remote SSH host <replaceable>HOST</replaceable>. This relies on
153 OpenSSH 9.4 or newer on the server side. Abstract namespace sockets are not supported for this
154 transport. <literal>ssh:</literal> and <literal>ssh-unix:</literal> are synonyms.</para></listitem>
155 </varlistentry>
156
157 <varlistentry>
158 <term><literal>ssh-exec:</literal><replaceable>HOST</replaceable><literal>:</literal><replaceable>COMMAND</replaceable></term>
159 <listitem><para>Invokes <replaceable>COMMAND</replaceable> (a command line, split on whitespace with
160 shell-style quoting and unescaping) on the remote SSH host <replaceable>HOST</replaceable> and speaks
161 the Varlink protocol over its standard input and output.</para></listitem>
162 </varlistentry>
163 </variablelist>
164
165 <para>If the scheme is none of the above but is otherwise a syntactically valid URL scheme,
166 <function>sd_varlink_connect_url()</function> looks for a <emphasis>bridge helper</emphasis> binary of that
167 name in the directory configured via <varname>$SYSTEMD_VARLINK_BRIDGES_DIR</varname> (see below). If found
168 and executable, it is invoked like an <literal>exec:</literal> transport, with the complete, unmodified URL
169 passed as its sole command line argument. This allows additional transports to be plugged in out of tree.</para>
170
171 <para>For the natively supported schemes, URL parameterization using <literal>;</literal>,
172 <literal>?</literal> or <literal>#</literal> is rejected (these are reserved for possible future use). A
173 <literal>vsock:</literal> scheme is not currently supported.</para>
174
175 <para><function>sd_varlink_connect_fd()</function> turns an already existing, already connected file
176 descriptor <parameter>fd</parameter> into a Varlink connection. The descriptor is used for both reading and
177 writing. It may refer to a connected stream socket, but also to a pipe or other bidirectional file
178 descriptor.</para>
179
180 <para><function>sd_varlink_connect_fd_pair()</function> is like
181 <function>sd_varlink_connect_fd()</function>, but accepts a separate <parameter>input_fd</parameter> (used
182 for reading from the peer) and <parameter>output_fd</parameter> (used for writing to the peer). This is
183 useful when the two directions are backed by different descriptors, for example a pair of pipes, or the
184 standard output and standard input of a co-process. The two descriptors may also be identical, which is
185 exactly what <function>sd_varlink_connect_fd()</function> does internally. If
186 <parameter>override_ucred</parameter> is non-<constant>NULL</constant>, the peer credentials reported for
187 the connection (as returned e.g. by
188 <citerefentry><refentrytitle>sd_varlink_get_peer_uid</refentrytitle><manvolnum>3</manvolnum></citerefentry>)
189 are taken from the supplied <structname>ucred</structname> structure instead of being queried from the
190 socket via <constant>SO_PEERCRED</constant>. This is primarily useful when the descriptors are not sockets
191 (and hence carry no kernel-supplied peer credentials), or when the credentials need to be overridden for
192 other reasons. If <parameter>override_ucred</parameter> is <constant>NULL</constant>, peer credentials are
193 determined from the socket as usual.</para>
194
195 <para><function>sd_varlink_connect_fd()</function> and <function>sd_varlink_connect_fd_pair()</function> take
196 over ownership of the descriptors passed to them: the descriptors are closed automatically when the
197 connection object is freed, and the caller must not close them itself. On failure ownership remains with
198 the caller.</para>
199
200 <para>The connection objects created by <function>sd_varlink_connect_exec()</function>, and by the
201 <literal>exec:</literal>, <literal>ssh:</literal>/<literal>ssh-unix:</literal>,
202 <literal>ssh-exec:</literal> and bridge-helper paths of <function>sd_varlink_connect_url()</function>,
203 are bound to the lifetime of the spawned child process. When such a connection object is freed, the
204 associated child process is sent <constant>SIGTERM</constant> and reaped. The child is also configured to
205 receive <constant>SIGTERM</constant> if the calling process dies.</para>
206 </refsect1>
207
208 <refsect1>
209 <title>Return Value</title>
210
211 <para>On success, these functions return a non-negative integer and store a pointer to the new connection
212 object in <parameter>ret</parameter>. On failure, they return a negative errno-style error code and leave
213 <parameter>ret</parameter> unchanged.</para>
214
215 <refsect2>
216 <title>Errors</title>
217
218 <para>Returned errors may indicate the following problems:</para>
219
220 <variablelist>
221 <varlistentry>
222 <term><constant>-EINVAL</constant></term>
223
224 <listitem><para>A required argument is <constant>NULL</constant>, or the supplied address, command,
225 or URL is malformed. This includes addresses that do not begin with <literal>/</literal> or
226 <literal>@</literal>, abstract namespace names that do not fit into a
227 <structname>sockaddr_un</structname> structure, and — for
228 <function>sd_varlink_connect_url()</function> — file system paths that are not absolute or not
229 normalized.</para></listitem>
230 </varlistentry>
231
232 <varlistentry>
233 <term><constant>-EBADF</constant></term>
234
235 <listitem><para>For <function>sd_varlink_connect_fd()</function> and
236 <function>sd_varlink_connect_fd_pair()</function>: a supplied file descriptor is
237 negative.</para></listitem>
238 </varlistentry>
239
240 <varlistentry>
241 <term><constant>-EPROTONOSUPPORT</constant></term>
242
243 <listitem><para>For <function>sd_varlink_connect_url()</function>: the URL contains no
244 <literal>:</literal> separator, uses an unsupported scheme for which no bridge helper binary is
245 available, or makes use of the reserved <literal>;</literal>, <literal>?</literal> or
246 <literal>#</literal> URL parameterization characters with a natively supported
247 scheme.</para></listitem>
248 </varlistentry>
249
250 <varlistentry>
251 <term><constant>-ENOMEM</constant></term>
252
253 <listitem><para>Memory allocation failed.</para></listitem>
254 </varlistentry>
255 </variablelist>
256
257 <para>In addition, these functions may propagate any error returned by the underlying system calls they
258 use, such as <citerefentry
259 project='man-pages'><refentrytitle>socket</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
260 <citerefentry project='man-pages'><refentrytitle>connect</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
261 <citerefentry project='man-pages'><refentrytitle>fork</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
262 <citerefentry project='man-pages'><refentrytitle>pipe</refentrytitle><manvolnum>2</manvolnum></citerefentry>
263 and the various execution helpers.</para>
264 </refsect2>
265 </refsect1>
266
267 <xi:include href="libsystemd-pkgconfig.xml" />
268
269 <refsect1>
270 <title>Environment</title>
271
272 <variablelist class='environment-variables'>
273 <varlistentry>
274 <term><varname>$SYSTEMD_SSH</varname></term>
275
276 <listitem><para>Used by the <literal>ssh:</literal>/<literal>ssh-unix:</literal> and
277 <literal>ssh-exec:</literal> transports of <function>sd_varlink_connect_url()</function> to override
278 the <command>ssh</command> binary to invoke. May be a plain file name (looked up in
279 <varname>$PATH</varname>) or an absolute path.</para>
280
281 <xi:include href="version-info.xml" xpointer="v257"/></listitem>
282 </varlistentry>
283
284 <varlistentry>
285 <term><varname>$SYSTEMD_VARLINK_BRIDGES_DIR</varname></term>
286
287 <listitem><para>Overrides the directory in which
288 <function>sd_varlink_connect_url()</function> looks up bridge helper binaries for non-native URL
289 schemes. Defaults to <filename>/usr/lib/systemd/varlink-bridges/</filename>.</para>
290
291 <xi:include href="version-info.xml" xpointer="v257"/></listitem>
292 </varlistentry>
293 </variablelist>
294 </refsect1>
295
296 <refsect1>
297 <title>Files</title>
298
299 <variablelist>
300 <varlistentry>
301 <term><filename>/usr/lib/systemd/varlink-bridges/</filename></term>
302
303 <listitem><para>Default directory searched for bridge helper binaries when
304 <function>sd_varlink_connect_url()</function> encounters a URL with a scheme that is not natively
305 supported. See
306 <citerefentry><refentrytitle>sd-varlink</refentrytitle><manvolnum>3</manvolnum></citerefentry> for
307 details.</para></listitem>
308 </varlistentry>
309 </variablelist>
310 </refsect1>
311
312 <refsect1>
313 <title>History</title>
314 <para><function>sd_varlink_connect_address()</function>,
315 <function>sd_varlink_connect_exec()</function>, <function>sd_varlink_connect_url()</function>,
316 <function>sd_varlink_connect_fd()</function> and <function>sd_varlink_connect_fd_pair()</function> were
317 added in version 257.</para>
318 </refsect1>
319
320 <refsect1>
321 <title>See Also</title>
322
323 <para><simplelist type="inline">
324 <member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
325 <member><citerefentry><refentrytitle>sd-varlink</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
326 <member><citerefentry><refentrytitle>sd_varlink_attach_event</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
327 <member><citerefentry><refentrytitle>sd_varlink_call</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
328 <member><citerefentry><refentrytitle>sd_varlink_send</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
329 <member><citerefentry><refentrytitle>sd_varlink_is_connected</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
330 <member><citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
331 <member><citerefentry><refentrytitle>varlinkctl</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
332 </simplelist></para>
333 </refsect1>
334
335 </refentry>