summaryrefslogtreecommitdiffstats
path: root/man/sd_bus_message_read.xml
blob: aa325f39c29ec2412c81ec2d87dbde8b582ef4e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?xml version='1.0'?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->

<refentry id="sd_bus_message_read"
          xmlns:xi="http://www.w3.org/2001/XInclude">

  <refentryinfo>
    <title>sd_bus_message_read</title>
    <productname>systemd</productname>
  </refentryinfo>

  <refmeta>
    <refentrytitle>sd_bus_message_read</refentrytitle>
    <manvolnum>3</manvolnum>
  </refmeta>

  <refnamediv>
    <refname>sd_bus_message_read</refname>
    <refname>sd_bus_message_readv</refname>
    <refname>sd_bus_message_peek_type</refname>

    <refpurpose>Read a sequence of values from a message</refpurpose>
  </refnamediv>

  <refsynopsisdiv>
    <funcsynopsis>
      <funcsynopsisinfo>#include &lt;systemd/sd-bus.h&gt;</funcsynopsisinfo>

      <funcprototype>
        <funcdef>int <function>sd_bus_message_read</function></funcdef>
        <paramdef>sd_bus_message *<parameter>m</parameter></paramdef>
        <paramdef>const char *<parameter>types</parameter></paramdef>
        <paramdef>...</paramdef>
      </funcprototype>

      <funcprototype>
        <funcdef>int <function>sd_bus_message_readv</function></funcdef>
        <paramdef>sd_bus_message *<parameter>m</parameter></paramdef>
        <paramdef>const char *<parameter>types</parameter></paramdef>
        <paramdef>va_list <parameter>ap</parameter></paramdef>
      </funcprototype>

      <funcprototype>
        <funcdef>int <function>sd_bus_message_peek_type</function></funcdef>
        <paramdef>sd_bus_message *<parameter>m</parameter></paramdef>
        <paramdef>char *<parameter>type</parameter></paramdef>
        <paramdef>const char **<parameter>contents</parameter></paramdef>
      </funcprototype>
    </funcsynopsis>
  </refsynopsisdiv>

  <refsect1>
    <title>Description</title>

    <para><function>sd_bus_message_read()</function> reads a sequence of fields from the D-Bus message object
    <parameter>m</parameter> and advances the read position in the message. The type string
    <parameter>types</parameter> describes the types of items expected in the message and the field arguments
    that follow. The type string may be <constant>NULL</constant> or empty, in which case nothing is read.
    </para>

    <para>The type string is composed of the elements described in
    <citerefentry><refentrytitle>sd_bus_message_append</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
    i.e. basic and container types. It must contain zero or more single "complete types". The type string is
    <constant>NUL</constant>-terminated.</para>

    <para>For each type specified in the type string, one or more arguments need to be specified after the
    <parameter>types</parameter> parameter, in the same order. The arguments must be pointers to appropriate
    types (a pointer to <type>int8_t</type> for a <literal>y</literal> in the type string, a pointer to
    <type>int32_t</type> for an <literal>i</literal>, a pointer to <type>const char*</type> for an
    <literal>s</literal>, ...)  which are set based on the values in the message. As an exception, in case of
    array and variant types, the first argument is an "input" argument that further specifies how the message
    should be read. See the table below for a complete list of allowed arguments and their types. Note that,
    if the basic type is a pointer (e.g., <type>const char *</type> in the case of a string), the argument is
    a pointer to a pointer, and also the pointer value that is written is only borrowed and the contents must
    be copied if they are to be used after the end of the message's lifetime. If the type is
    <literal>h</literal> (UNIX file descriptor), the descriptor is not duplicated by this call and the
    returned descriptor remains in possession of the message object, and needs to be duplicated by the caller
    in order to keep an open reference to it after the message object is freed.</para>

    <para>Each argument may also be <constant>NULL</constant>, in which case the value is read and ignored.
    </para>

    <table>
      <title>Item type specifiers</title>

      <tgroup cols='5'>
        <colspec colname='specifier' />
        <colspec colname='constant' />
        <colspec colname='description' />
        <colspec colname='type1' />
        <colspec colname='type2' />
        <thead>
          <row>
            <entry>Specifier</entry>
            <entry>Constant</entry>
            <entry>Description</entry>
            <entry>Type of the first argument</entry>
            <entry>Types of the subsequent arguments, if any</entry>
          </row>
        </thead>

        <tbody>
          <xi:include href="sd_bus_message_read_basic.xml" xpointer="xpointer(//table[@id='format-specifiers']//tbody/*)" />

          <row>
            <entry><literal>a</literal></entry>
            <entry><constant>SD_BUS_TYPE_ARRAY</constant></entry>
            <entry>array</entry>
            <entry><type>int</type>, which specifies the expected length <parameter>n</parameter> of the array</entry>
            <entry><parameter>n</parameter> sets of arguments appropriate for the array element type</entry>
          </row>

          <row>
            <entry><literal>v</literal></entry>
            <entry><constant>SD_BUS_TYPE_VARIANT</constant></entry>
            <entry>variant</entry>
            <entry>signature string</entry>
            <entry>arguments appropriate for the types specified by the signature</entry>
          </row>

          <row>
            <entry><literal>(</literal></entry>
            <entry><constant>SD_BUS_TYPE_STRUCT_BEGIN</constant></entry>
            <entry>array start</entry>
            <entry morerows="1" namest="type1" nameend="type2">arguments appropriate for the structure elements</entry>
          </row>
          <row>
            <entry><literal>)</literal></entry>
            <entry><constant>SD_BUS_TYPE_STRUCT_END</constant></entry>
            <entry>array end</entry>
          </row>

          <row>
            <entry><literal>{</literal></entry>
            <entry><constant>SD_BUS_TYPE_DICT_ENTRY_BEGIN</constant></entry>
            <entry>dictionary entry start</entry>
            <entry morerows="1">arguments appropriate for the first type in the pair</entry>
            <entry morerows="1">arguments appropriate for the second type in the pair</entry>
            </row>
          <row>
            <entry><literal>}</literal></entry>
            <entry><constant>SD_BUS_TYPE_DICT_ENTRY_END</constant></entry>
            <entry>dictionary entry end</entry>
          </row>
       </tbody>
      </tgroup>
    </table>

    <para>If objects of the specified types are not present at the current position in the message, an error
    is returned.</para>

    <para>The <function>sd_bus_message_readv()</function> is equivalent to the
    <function>sd_bus_message_read()</function>, except that it is called with a <literal>va_list</literal>
    instead of a variable number of arguments. This function does not call the <function>va_end()</function>
    macro. Because it invokes the <function>va_arg()</function> macro, the value of <parameter>ap</parameter>
    is undefined after the call.</para>

    <para><function>sd_bus_message_peek_type()</function> determines the type of the next element in
    <parameter>m</parameter> to be read by <function>sd_bus_message_read()</function> or similar functions.
    On success, the type is stored in <parameter>type</parameter>, if it is not <constant>NULL</constant>.
    If the type is a container type, the type of its elements is stored in <parameter>contents</parameter>,
    if it is not <constant>NULL</constant>. If this function successfully determines the type of the next
    element in <parameter>m</parameter>, it returns a positive integer. If there are no more elements to be
    read, it returns zero.</para>
  </refsect1>

  <refsect1>
    <title>Return Value</title>

    <para>On success, these functions return a non-negative integer. On failure, they return a negative
    errno-style error code.</para>

    <refsect2 id='errors'>
      <title>Errors</title>

      <para>Returned errors may indicate the following problems:</para>

      <variablelist>
        <xi:include href="sd_bus_message_read_basic.xml" xpointer="errors-einval"/>
        <xi:include href="sd_bus_message_read_basic.xml" xpointer="errors-enxio"/>
        <xi:include href="sd_bus_message_read_basic.xml" xpointer="errors-ebadmsg"/>
        <varlistentry>
          <term><constant>-EBUSY</constant></term>

          <listitem><para>When reading from a container, this error will be returned if unread elements
          are left in the container.</para></listitem>
        </varlistentry>
      </variablelist>
    </refsect2>
  </refsect1>

  <xi:include href="libsystemd-pkgconfig.xml" />

  <refsect1>
    <title>Examples</title>

    <para>Read a single basic type (a 64-bit integer):
    </para>

    <programlisting>sd_bus_message *m;
int64_t x;

sd_bus_message_read(m, "x", &amp;x);</programlisting>

    <para>Read a boolean value:</para>

    <programlisting>sd_bus_message *m;
int x; /* Do not use C99 'bool' type here, it's typically smaller
          in memory and would cause memory corruption */

sd_bus_message_read(m, "b", &amp;x);</programlisting>

    <para>Read all types of integers:</para>

    <programlisting>uint8_t y;
int16_t n;
uint16_t q;
int32_t i;
uint32_t u;
int32_t x;
uint32_t t;
double d;

sd_bus_message_read(m, "ynqiuxtd", &amp;y, &amp;n, &amp;q, &amp;i, &amp;u, &amp;x, &amp;t, &amp;d);</programlisting>

     <para>Read a structure composed of a string and a D-Bus path:</para>

     <programlisting>const char *s, *p;

sd_bus_message_read(m, "(so)", &amp;s, &amp;p);
</programlisting>

     <para>Read a variant, with the real type "gt" (signature, unsigned integer):
     </para>

     <programlisting>const char *s;
uint64_t *v;

sd_bus_message_read(m, "v", "gt", &amp;s, &amp;v);</programlisting>

     <para>Read a dictionary containing three pairs of type {integer=>string}:
     </para>

     <programlisting>int i, j, k;
const char *s, *t, *u;

sd_bus_message_read(m, "a{is}", 3, &amp;i, &amp;s, &amp;j, &amp;t, &amp;k, &amp;u);
     </programlisting>

     <para>Read a single file descriptor, and duplicate it in order to keep it open after the message is
     freed.</para>

     <programlisting>sd_bus_message *m;
int fd, fd_copy;

sd_bus_message_read(m, "h", &amp;fd);
fd_copy = fcntl(fd, FD_DUPFD_CLOEXEC, 3);</programlisting>
  </refsect1>

  <refsect1>
    <title>See Also</title>

    <para>
      <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
      <citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
      <citerefentry><refentrytitle>sd_bus_message_read_basic</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
      <citerefentry><refentrytitle>sd_bus_message_skip</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
      <citerefentry><refentrytitle>sd_bus_message_append</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
      <citerefentry><refentrytitle>sd_bus_message_enter_container</refentrytitle><manvolnum>3</manvolnum></citerefentry>
    </para>
  </refsect1>

</refentry>