summaryrefslogtreecommitdiff
path: root/Documentation/kdbus/kdbus.message.xml
blob: 0115d9d50db3469cb1ae7f69d94f4a0e375abeff (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
<?xml version='1.0'?> <!--*-nxml-*-->
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
        "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<refentry id="kdbus.message">

  <refentryinfo>
    <title>kdbus.message</title>
    <productname>kdbus.message</productname>
  </refentryinfo>

  <refmeta>
    <refentrytitle>kdbus.message</refentrytitle>
    <manvolnum>7</manvolnum>
  </refmeta>

  <refnamediv>
    <refname>kdbus.message</refname>
    <refpurpose>kdbus message</refpurpose>
  </refnamediv>

  <refsect1>
    <title>Description</title>

    <para>
      A kdbus message is used to exchange information between two connections
      on a bus, or to transport notifications from the kernel to one or many
      connections. This document describes the layout of messages, how payload
      is added to them and how they are sent and received.
    </para>
  </refsect1>

  <refsect1>
    <title>Message layout</title>

    <para>The layout of a message is shown below.</para>

    <programlisting>
  +-------------------------------------------------------------------------+
  | Message                                                                 |
  | +---------------------------------------------------------------------+ |
  | | Header                                                              | |
  | | size:          overall message size, including the data records     | |
  | | destination:   connection ID of the receiver                        | |
  | | source:        connection ID of the sender (set by kernel)          | |
  | | payload_type:  "DBusDBus" textual identifier stored as uint64_t     | |
  | +---------------------------------------------------------------------+ |
  | +---------------------------------------------------------------------+ |
  | | Data Record                                                         | |
  | | size:  overall record size (without padding)                        | |
  | | type:  type of data                                                 | |
  | | data:  reference to data (address or file descriptor)               | |
  | +---------------------------------------------------------------------+ |
  | +---------------------------------------------------------------------+ |
  | | padding bytes to the next 8 byte alignment                          | |
  | +---------------------------------------------------------------------+ |
  | +---------------------------------------------------------------------+ |
  | | Data Record                                                         | |
  | | size:  overall record size (without padding)                        | |
  | | ...                                                                 | |
  | +---------------------------------------------------------------------+ |
  | +---------------------------------------------------------------------+ |
  | | padding bytes to the next 8 byte alignment                          | |
  | +---------------------------------------------------------------------+ |
  | +---------------------------------------------------------------------+ |
  | | Data Record                                                         | |
  | | size:  overall record size                                          | |
  | | ...                                                                 | |
  | +---------------------------------------------------------------------+ |
  |   ... further data records ...                                          |
  +-------------------------------------------------------------------------+
    </programlisting>
  </refsect1>

  <refsect1>
    <title>Message payload</title>

    <para>
      When connecting to the bus, receivers request a memory pool of a given
      size, large enough to carry all backlog of data enqueued for the
      connection. The pool is internally backed by a shared memory file which
      can be <function>mmap()</function>ed by the receiver. See
      <citerefentry>
        <refentrytitle>kdbus.pool</refentrytitle>
        <manvolnum>7</manvolnum>
      </citerefentry>
      for more information.
    </para>

    <para>
      Message payload must be described in items attached to a message when
      it is sent. A receiver can access the payload by looking at the items
      that are attached to a message in its pool. The following items are used.
    </para>

    <variablelist>
      <varlistentry>
        <term><constant>KDBUS_ITEM_PAYLOAD_VEC</constant></term>
        <listitem>
          <para>
            This item references a piece of memory on the sender side which is
            directly copied into the receiver's pool. This way, two peers can
            exchange data by effectively doing a single-copy from one process
            to another; the kernel will not buffer the data anywhere else.
            This item is never found in a message received by a connection.
          </para>
        </listitem>
      </varlistentry>

      <varlistentry>
        <term><constant>KDBUS_ITEM_PAYLOAD_OFF</constant></term>
        <listitem>
          <para>
            This item is attached to messages on the receiving side and points
            to a memory area inside the receiver's pool. The
            <varname>offset</varname> variable in the item denotes the memory
            location relative to the message itself.
          </para>
        </listitem>
      </varlistentry>

      <varlistentry>
        <term><constant>KDBUS_ITEM_PAYLOAD_MEMFD</constant></term>
        <listitem>
          <para>
            Messages can reference <emphasis>memfd</emphasis> files which
            contain the data. memfd files are tmpfs-backed files that allow
            sealing of the content of the file, which prevents all writable
            access to the file content.
          </para>
          <para>
            Only memfds that have
            <constant>(F_SEAL_SHRINK|F_SEAL_GROW|F_SEAL_WRITE|F_SEAL_SEAL)
            </constant>
            set are accepted as payload data, which enforces reliable passing of
            data. The receiver can assume that neither the sender nor anyone
            else can alter the content after the message is sent. If those
            seals are not set on the memfd, the ioctl will fail with
            <errorcode>-1</errorcode>, and <varname>errno</varname> will be
            set to <constant>ETXTBUSY</constant>.
          </para>
        </listitem>
      </varlistentry>

      <varlistentry>
        <term><constant>KDBUS_ITEM_FDS</constant></term>
        <listitem>
          <para>
            Messages can transport regular file descriptors via
            <constant>KDBUS_ITEM_FDS</constant>. This item carries an array
            of <type>int</type> values in <varname>item.fd</varname>. The
            maximum number of file descriptors in the item is
            <constant>253</constant>, and only one item of this type is
            accepted per message. All passed values must be valid file
            descriptors; the open count of each file descriptors is increased
            by installing it to the receiver's task. This item can only be
            used for directed messages, not for broadcasts, and only to
            remote peers that have opted-in for receiving file descriptors
            at connection time (<constant>KDBUS_HELLO_ACCEPT_FD</constant>).
          </para>
        </listitem>
      </varlistentry>
    </variablelist>

    <para>
      The sender must not make any assumptions on the type in which data is
      received by the remote peer. The kernel is free to re-pack multiple
      <constant>KDBUS_ITEM_PAYLOAD_VEC</constant> and
      <constant>KDBUS_ITEM_PAYLOAD_MEMFD</constant> payloads. For instance, the
      kernel may decide to merge multiple <constant>VECs</constant> into a
      single <constant>VEC</constant>, inline <constant>MEMFD</constant>
      payloads into memory, or merge all passed <constant>VECs</constant> into a
      single <constant>MEMFD</constant>. However, the kernel preserves the order
      of passed data. This means that the order of all <constant>VEC</constant>
      and <constant>MEMFD</constant> items is not changed in respect to each
      other. In other words: All passed <constant>VEC</constant> and
      <constant>MEMFD</constant> data payloads are treated as a single stream
      of data that may be received by the remote peer in a different set of
      chunks than it was sent as.
    </para>
  </refsect1>

  <refsect1>
    <title>Sending messages</title>

    <para>
      Messages are passed to the kernel with the
      <constant>KDBUS_CMD_SEND</constant> ioctl. Depending on the destination
      address of the message, the kernel delivers the message to the specific
      destination connection, or to some subset of all connections on the same
      bus. Sending messages across buses is not possible. Messages are always
      queued in the memory pool of the destination connection (see above).
    </para>

    <para>
      The <constant>KDBUS_CMD_SEND</constant> ioctl uses a
      <type>struct kdbus_cmd_send</type> to describe the message
      transfer.
    </para>
    <programlisting>
struct kdbus_cmd_send {
  __u64 size;
  __u64 flags;
  __u64 return_flags;
  __u64 msg_address;
  struct kdbus_msg_info reply;
  struct kdbus_item items[0];
};
    </programlisting>

    <para>The fields in this struct are described below.</para>

    <variablelist>
      <varlistentry>
        <term><varname>size</varname></term>
        <listitem><para>
          The overall size of the struct, including its items.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>flags</varname></term>
        <listitem><para>Flags for message delivery</para>
          <variablelist>
            <varlistentry>
              <term><constant>KDBUS_SEND_SYNC_REPLY</constant></term>
              <listitem>
                <para>
                  By default, all calls to kdbus are considered asynchronous,
                  non-blocking. However, as there are many use cases that need
                  to wait for a remote peer to answer a method call, there's a
                  way to send a message and wait for a reply in a synchronous
                  fashion. This is what the
                  <constant>KDBUS_SEND_SYNC_REPLY</constant> controls. The
                  <constant>KDBUS_CMD_SEND</constant> ioctl will block until the
                  reply has arrived, the timeout limit is reached, in case the
                  remote connection was shut down, or if interrupted by a signal
                  before any reply; see
                  <citerefentry>
                    <refentrytitle>signal</refentrytitle>
                    <manvolnum>7</manvolnum>
                  </citerefentry>.

                  The offset of the reply message in the sender's pool is stored
                  in <varname>reply</varname> when the ioctl has returned without
                  error. Hence, there is no need for another
                  <constant>KDBUS_CMD_RECV</constant> ioctl or anything else to
                  receive the reply.
                </para>
              </listitem>
            </varlistentry>

            <varlistentry>
              <term><constant>KDBUS_FLAG_NEGOTIATE</constant></term>
              <listitem>
                <para>
                  Request a set of valid flags for this ioctl. When this bit is
                  set, no action is taken; the ioctl will fail with
                  <errorcode>-1</errorcode>, <varname>errno</varname>
                  is set to <constant>EPROTO</constant>.
                  Once the ioctl returned, the <varname>flags</varname>
                  field will have all bits set that the kernel recognizes as
                  valid for this command.
                  The <constant>KDBUS_FLAG_NEGOTIATE</constant> bit will be
                  cleared by the operation.
                </para>
              </listitem>
            </varlistentry>
          </variablelist>
        </listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>return_flags</varname></term>
        <listitem><para>
          Flags returned by the kernel. Currently unused and always set to
          <constant>0</constant> by the kernel.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>msg_address</varname></term>
        <listitem><para>
          In this field, users have to provide a pointer to a message
          (<type>struct kdbus_msg</type>) to send. See below for a
          detailed description.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>reply</varname></term>
        <listitem><para>
          Only used for synchronous replies. See description of
          <type>struct kdbus_cmd_recv</type> for more details.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>items</varname></term>
        <listitem>
          <para>
            The following items are currently recognized.
          </para>
          <variablelist>
            <varlistentry>
              <term><constant>KDBUS_ITEM_CANCEL_FD</constant></term>
              <listitem>
                <para>
                  When this optional item is passed in, and the call is
                  executed as SYNC call, the passed in file descriptor can be
                  used as alternative cancellation point. The kernel will call
                  <citerefentry>
                    <refentrytitle>poll</refentrytitle>
                    <manvolnum>2</manvolnum>
                  </citerefentry>
                  on this file descriptor, and once it reports any incoming
                  bytes, the blocking send operation will be canceled; the
                  blocking, synchronous ioctl call will return
                  <errorcode>-1</errorcode>, and <varname>errno</varname> will
                  be set to <errorname>ECANCELED</errorname>.
                  Any type of file descriptor on which
                  <citerefentry>
                    <refentrytitle>poll</refentrytitle>
                    <manvolnum>2</manvolnum>
                  </citerefentry>
                  can be called on can be used as payload to this item; for
                  example, an eventfd can be used for this purpose, see
                  <citerefentry>
                    <refentrytitle>eventfd</refentrytitle>
                    <manvolnum>2</manvolnum>
                  </citerefentry>.
                  For asynchronous message sending, this item is allowed but
                  ignored.
                </para>
              </listitem>
            </varlistentry>
          </variablelist>
          <para>
            Unrecognized items are rejected, and the ioctl will fail with
            <varname>errno</varname> set to <constant>EINVAL</constant>.
          </para>
        </listitem>
      </varlistentry>
    </variablelist>

    <para>
      The message referenced by the <varname>msg_address</varname> above has
      the following layout.
    </para>

    <programlisting>
struct kdbus_msg {
  __u64 size;
  __u64 flags;
  __s64 priority;
  __u64 dst_id;
  __u64 src_id;
  __u64 payload_type;
  __u64 cookie;
  __u64 timeout_ns;
  __u64 cookie_reply;
  struct kdbus_item items[0];
};
    </programlisting>

    <para>The fields in this struct are described below.</para>

    <variablelist>
      <varlistentry>
        <term><varname>size</varname></term>
        <listitem><para>
          The overall size of the struct, including its items.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>flags</varname></term>
        <listitem><para>Flags to describe message details.</para>
          <variablelist>
            <varlistentry>
              <term><constant>KDBUS_MSG_EXPECT_REPLY</constant></term>
              <listitem>
                <para>
                  Expect a reply to this message from the remote peer. With
                  this bit set, the timeout_ns field must be set to a non-zero
                  number of nanoseconds in which the receiving peer is expected
                  to reply. If such a reply is not received in time, the sender
                  will be notified with a timeout message (see below). The
                  value must be an absolute value, in nanoseconds and based on
                  <constant>CLOCK_MONOTONIC</constant>.
                </para><para>
                  For a message to be accepted as reply, it must be a direct
                  message to the original sender (not a broadcast and not a
                  signal message), and its
                  <varname>kdbus_msg.cookie_reply</varname> must match the
                  previous message's <varname>kdbus_msg.cookie</varname>.
                </para><para>
                  Expected replies also temporarily open the policy of the
                  sending connection, so the other peer is allowed to respond
                  within the given time window.
                </para>
              </listitem>
            </varlistentry>

            <varlistentry>
              <term><constant>KDBUS_MSG_NO_AUTO_START</constant></term>
              <listitem>
                <para>
                  By default, when a message is sent to an activator
                  connection, the activator is notified and will start an
                  implementer. This flag inhibits that behavior. With this bit
                  set, and the remote being an activator, the ioctl will fail
                  with <varname>errno</varname> set to
                  <constant>EADDRNOTAVAIL</constant>.
                </para>
              </listitem>
            </varlistentry>

            <varlistentry>
              <term><constant>KDBUS_FLAG_NEGOTIATE</constant></term>
              <listitem>
                <para>
                  Requests a set of valid flags for this ioctl. When this bit is
                  set, no action is taken; the ioctl will return
                  <errorcode>0</errorcode>, and the <varname>flags</varname>
                  field will have all bits set that are valid for this command.
                  The <constant>KDBUS_FLAG_NEGOTIATE</constant> bit will be
                  cleared by the operation.
                </para>
              </listitem>
            </varlistentry>
          </variablelist>
        </listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>priority</varname></term>
        <listitem><para>
          The priority of this message. Receiving messages (see below) may
          optionally be constrained to messages of a minimal priority. This
          allows for use cases where timing critical data is interleaved with
          control data on the same connection. If unused, the priority field
          should be set to <constant>0</constant>.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>dst_id</varname></term>
        <listitem><para>
          The numeric ID of the destination connection, or
          <constant>KDBUS_DST_ID_BROADCAST</constant>
          (~0ULL) to address every peer on the bus, or
          <constant>KDBUS_DST_ID_NAME</constant> (0) to look
          it up dynamically from the bus' name registry.
          In the latter case, an item of type
          <constant>KDBUS_ITEM_DST_NAME</constant> is mandatory.
          Also see
          <citerefentry>
            <refentrytitle>kdbus.name</refentrytitle>
            <manvolnum>7</manvolnum>
          </citerefentry>
          .
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>src_id</varname></term>
        <listitem><para>
          Upon return of the ioctl, this member will contain the sending
          connection's numerical ID. Should be 0 at send time.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>payload_type</varname></term>
        <listitem><para>
          Type of the payload in the actual data records. Currently, only
          <constant>KDBUS_PAYLOAD_DBUS</constant> is accepted as input value
          of this field. When receiving messages that are generated by the
          kernel (notifications), this field will contain
          <constant>KDBUS_PAYLOAD_KERNEL</constant>.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>cookie</varname></term>
        <listitem><para>
          Cookie of this message, for later recognition. Also, when replying
          to a message (see above), the <varname>cookie_reply</varname>
          field must match this value.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>timeout_ns</varname></term>
        <listitem><para>
          If the message sent requires a reply from the remote peer (see above),
          this field contains the timeout in absolute nanoseconds based on
          <constant>CLOCK_MONOTONIC</constant>. Also see
          <citerefentry>
            <refentrytitle>clock_gettime</refentrytitle>
            <manvolnum>2</manvolnum>
          </citerefentry>.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>cookie_reply</varname></term>
        <listitem><para>
          If the message sent is a reply to another message, this field must
          match the cookie of the formerly received message.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>items</varname></term>
        <listitem>
          <para>
            A dynamically sized list of items to contain additional information.
            The following items are expected/valid:
          </para>
          <variablelist>
            <varlistentry>
              <term><constant>KDBUS_ITEM_PAYLOAD_VEC</constant></term>
              <term><constant>KDBUS_ITEM_PAYLOAD_MEMFD</constant></term>
              <term><constant>KDBUS_ITEM_FDS</constant></term>
              <listitem>
                <para>
                  Actual data records containing the payload. See section
                  "Message payload".
                </para>
              </listitem>
            </varlistentry>

            <varlistentry>
              <term><constant>KDBUS_ITEM_BLOOM_FILTER</constant></term>
              <listitem>
                <para>
                  Bloom filter for matches (see below).
                </para>
              </listitem>
            </varlistentry>

            <varlistentry>
              <term><constant>KDBUS_ITEM_DST_NAME</constant></term>
              <listitem>
                <para>
                  Well-known name to send this message to. Required if
                  <varname>dst_id</varname> is set to
                  <constant>KDBUS_DST_ID_NAME</constant>.
                  If a connection holding the given name can't be found,
                  the ioctl will fail with <varname>errno</varname> set to
                  <constant>ESRCH</constant> is returned.
                </para>
                <para>
                  For messages to a unique name (ID), this item is optional. If
                  present, the kernel will make sure the name owner matches the
                  given unique name. This allows programs to tie the message
                  sending to the condition that a name is currently owned by a
                  certain unique name.
                </para>
              </listitem>
            </varlistentry>
          </variablelist>
        </listitem>
      </varlistentry>
    </variablelist>

    <para>
      The message will be augmented by the requested metadata items when
      queued into the receiver's pool. See
      <citerefentry>
        <refentrytitle>kdbus.connection</refentrytitle>
        <manvolnum>7</manvolnum>
      </citerefentry>
      and
      <citerefentry>
        <refentrytitle>kdbus.item</refentrytitle>
        <manvolnum>7</manvolnum>
      </citerefentry>
      for more information on metadata.
    </para>
  </refsect1>

  <refsect1>
    <title>Receiving messages</title>

    <para>
      Messages are received by the client with the
      <constant>KDBUS_CMD_RECV</constant> ioctl. The endpoint file of the bus
      supports <function>poll()/epoll()/select()</function>; when new messages
      are available on the connection's file descriptor,
      <constant>POLLIN</constant> is reported. For compatibility reasons,
      <constant>POLLOUT</constant> is always reported as well. Note, however,
      that the latter does not guarantee that a message can in fact be sent, as
      this depends on how many pending messages the receiver has in its pool.
    </para>

    <para>
      With the <constant>KDBUS_CMD_RECV</constant> ioctl, a
      <type>struct kdbus_cmd_recv</type> is used.
    </para>

    <programlisting>
struct kdbus_cmd_recv {
  __u64 size;
  __u64 flags;
  __u64 return_flags;
  __s64 priority;
  __u64 dropped_msgs;
  struct kdbus_msg_info msg;
  struct kdbus_item items[0];
};
    </programlisting>

    <para>The fields in this struct are described below.</para>

    <variablelist>
      <varlistentry>
        <term><varname>size</varname></term>
        <listitem><para>
          The overall size of the struct, including its items.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>flags</varname></term>
        <listitem><para>Flags to control the receive command.</para>
          <variablelist>
            <varlistentry>
              <term><constant>KDBUS_RECV_PEEK</constant></term>
              <listitem>
                <para>
                  Just return the location of the next message. Do not install
                  file descriptors or anything else. This is usually used to
                  determine the sender of the next queued message.
                </para>
              </listitem>
            </varlistentry>

            <varlistentry>
              <term><constant>KDBUS_RECV_DROP</constant></term>
              <listitem>
                <para>
                  Drop the next message without doing anything else with it,
                  and free the pool slice. This a short-cut for
                  <constant>KDBUS_RECV_PEEK</constant> and
                  <constant>KDBUS_CMD_FREE</constant>.
                </para>
              </listitem>
            </varlistentry>

            <varlistentry>
              <term><constant>KDBUS_RECV_USE_PRIORITY</constant></term>
              <listitem>
                <para>
                  Dequeue the messages ordered by their priority, and filtering
                  them with the priority field (see below).
                </para>
              </listitem>
            </varlistentry>

            <varlistentry>
              <term><constant>KDBUS_FLAG_NEGOTIATE</constant></term>
              <listitem>
                <para>
                  Request a set of valid flags for this ioctl. When this bit is
                  set, no action is taken; the ioctl will fail with
                  <errorcode>-1</errorcode>, <varname>errno</varname>
                  is set to <constant>EPROTO</constant>.
                  Once the ioctl returned, the <varname>flags</varname>
                  field will have all bits set that the kernel recognizes as
                  valid for this command.
                </para>
              </listitem>
            </varlistentry>
          </variablelist>
        </listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>return_flags</varname></term>
        <listitem><para>
          Flags returned by the kernel. If the <varname>dropped_msgs</varname>
          field is non-zero, <constant>KDBUS_RECV_RETURN_DROPPED_MSGS</constant>
          is set. If a file descriptor could not be installed, the
          <constant>KDBUS_RECV_RETURN_INCOMPLETE_FDS</constant> flag is set.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>priority</varname></term>
        <listitem><para>
          With <constant>KDBUS_RECV_USE_PRIORITY</constant> set in
          <varname>flags</varname>, messages will be dequeued ordered by their
          priority, starting with the highest value. Also, messages will be
          filtered by the value given in this field, so the returned message
          will at least have the requested priority. If no such message is
          waiting in the queue, the ioctl will fail, and
          <varname>errno</varname> will be set to <constant>EAGAIN</constant>.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>dropped_msgs</varname></term>
        <listitem><para>
          Whenever a message with <constant>KDBUS_MSG_SIGNAL</constant> is sent
          but cannot be queued on a peer (e.g., as it contains FDs but the peer
          does not support FDs, or there is no space left in the peer's pool)
          the 'dropped_msgs' counter of the peer is incremented. On the next
          RECV ioctl, the 'dropped_msgs' field is copied into the ioctl struct
          and cleared on the peer. If it was non-zero, the
          <constant>KDBUS_RECV_RETURN_DROPPED_MSGS</constant> flag will be set
          in <varname>return_flags</varname>. Note that this will only happen
          if the ioctl succeeded or failed with <constant>EAGAIN</constant>. In
          other error cases, the 'dropped_msgs' field of the peer is left
          untouched.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>msg</varname></term>
        <listitem><para>
          Embedded struct containing information on the received message when
          this command succeeded (see below).
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>items</varname></term>
        <listitem><para>
          Items to specify further details for the receive command.
          Currently unused, and all items will be rejected with
          <varname>errno</varname> set to <constant>EINVAL</constant>.
        </para></listitem>
      </varlistentry>
    </variablelist>

    <para>
      Both <type>struct kdbus_cmd_recv</type> and
      <type>struct kdbus_cmd_send</type> embed
      <type>struct kdbus_msg_info</type>.
      For the <constant>KDBUS_CMD_SEND</constant> ioctl, it is used to catch
      synchronous replies, if one was requested, and is unused otherwise.
    </para>

    <programlisting>
struct kdbus_msg_info {
  __u64 offset;
  __u64 msg_size;
  __u64 return_flags;
};
    </programlisting>

    <para>The fields in this struct are described below.</para>

    <variablelist>
      <varlistentry>
        <term><varname>offset</varname></term>
        <listitem><para>
          Upon return of the ioctl, this field contains the offset in the
          receiver's memory pool. The memory must be freed with
          <constant>KDBUS_CMD_FREE</constant>. See
          <citerefentry>
            <refentrytitle>kdbus.pool</refentrytitle>
            <manvolnum>7</manvolnum>
          </citerefentry>
          for further details.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>msg_size</varname></term>
        <listitem><para>
          Upon successful return of the ioctl, this field contains the size of
          the allocated slice at offset <varname>offset</varname>.
          It is the combination of the size of the stored
          <type>struct kdbus_msg</type> object plus all appended VECs.
          You can use it in combination with <varname>offset</varname> to map
          a single message, instead of mapping the entire pool. See
          <citerefentry>
            <refentrytitle>kdbus.pool</refentrytitle>
            <manvolnum>7</manvolnum>
          </citerefentry>
          for further details.
        </para></listitem>
      </varlistentry>

      <varlistentry>
        <term><varname>return_flags</varname></term>
        <listitem>
          <para>
            Kernel-provided return flags. Currently, the following flags are
            defined.
          </para>
          <variablelist>
            <varlistentry>
              <term><constant>KDBUS_RECV_RETURN_INCOMPLETE_FDS</constant></term>
              <listitem>
                <para>
                  The message contained memfds or file descriptors, and the
                  kernel failed to install one or more of them at receive time.
                  Most probably that happened because the maximum number of
                  file descriptors for the receiver's task were exceeded.
                  In such cases, the message is still delivered, so this is not
                  a fatal condition. File descriptors numbers inside the
                  <constant>KDBUS_ITEM_FDS</constant> item or memfd files
                  referenced by <constant>KDBUS_ITEM_PAYLOAD_MEMFD</constant>
                  items which could not be installed will be set to
                  <constant>-1</constant>.
                </para>
              </listitem>
            </varlistentry>
          </variablelist>
        </listitem>
      </varlistentry>
    </variablelist>

    <para>
      Unless <constant>KDBUS_RECV_DROP</constant> was passed, the
      <varname>offset</varname> field contains the location of the new message
      inside the receiver's pool after the <constant>KDBUS_CMD_RECV</constant>
      ioctl was employed. The message is stored as <type>struct kdbus_msg</type>
      at this offset, and can be interpreted with the semantics described above.
    </para>
    <para>
      Also, if the connection allowed for file descriptor to be passed
      (<constant>KDBUS_HELLO_ACCEPT_FD</constant>), and if the message contained
      any, they will be installed into the receiving process when the
      <constant>KDBUS_CMD_RECV</constant> ioctl is called.
      <emphasis>memfds</emphasis> may always be part of the message payload.
      The receiving task is obliged to close all file descriptors appropriately
      once no longer needed. If <constant>KDBUS_RECV_PEEK</constant> is set, no
      file descriptors are installed. This allows for peeking at a message,
      looking at its metadata only and dropping it via
      <constant>KDBUS_RECV_DROP</constant>, without installing any of the file
      descriptors into the receiving process.
    </para>
    <para>
      The caller is obliged to call the <constant>KDBUS_CMD_FREE</constant>
      ioctl with the returned offset when the memory is no longer needed.
    </para>
  </refsect1>

  <refsect1>
    <title>Notifications</title>
    <para>
      A kernel notification is a regular kdbus message with the following
      details.
    </para>

    <itemizedlist>
      <listitem><para>
          kdbus_msg.src_id == <constant>KDBUS_SRC_ID_KERNEL</constant>
      </para></listitem>
      <listitem><para>
        kdbus_msg.dst_id == <constant>KDBUS_DST_ID_BROADCAST</constant>
      </para></listitem>
      <listitem><para>
        kdbus_msg.payload_type == <constant>KDBUS_PAYLOAD_KERNEL</constant>
      </para></listitem>
      <listitem><para>
        Has exactly one of the items attached that are described below.
      </para></listitem>
      <listitem><para>
        Always has a timestamp item (<constant>KDBUS_ITEM_TIMESTAMP</constant>)
        attached.
      </para></listitem>
    </itemizedlist>

    <para>
      The kernel will notify its users of the following events.
    </para>

    <itemizedlist>
      <listitem><para>
        When connection <emphasis>A</emphasis> is terminated while connection
        <emphasis>B</emphasis> is waiting for a reply from it, connection
        <emphasis>B</emphasis> is notified with a message with an item of
        type <constant>KDBUS_ITEM_REPLY_DEAD</constant>.
      </para></listitem>

      <listitem><para>
        When connection <emphasis>A</emphasis> does not receive a reply from
        connection <emphasis>B</emphasis> within the specified timeout window,
        connection <emphasis>A</emphasis> will receive a message with an
        item of type <constant>KDBUS_ITEM_REPLY_TIMEOUT</constant>.
      </para></listitem>

      <listitem><para>
        When an ordinary connection (not a monitor) is created on or removed
        from a bus, messages with an item of type
        <constant>KDBUS_ITEM_ID_ADD</constant> or
        <constant>KDBUS_ITEM_ID_REMOVE</constant>, respectively, are delivered
        to all bus members that match these messages through their match
        database. Eavesdroppers (monitor connections) do not cause such
        notifications to be sent. They are invisible on the bus.
      </para></listitem>

      <listitem><para>
        When a connection gains or loses ownership of a name, messages with an
        item of type <constant>KDBUS_ITEM_NAME_ADD</constant>,
        <constant>KDBUS_ITEM_NAME_REMOVE</constant> or
        <constant>KDBUS_ITEM_NAME_CHANGE</constant> are delivered to all bus
        members that match these messages through their match database.
      </para></listitem>
    </itemizedlist>
  </refsect1>

  <refsect1>
    <title>Return value</title>
    <para>
      On success, all mentioned ioctl commands return <errorcode>0</errorcode>;
      on error, <errorcode>-1</errorcode> is returned, and
      <varname>errno</varname> is set to indicate the error.
      If the issued ioctl is illegal for the file descriptor used,
      <varname>errno</varname> will be set to <constant>ENOTTY</constant>.
    </para>

    <refsect2>
      <title>
        <constant>KDBUS_CMD_SEND</constant> may fail with the following
        errors
      </title>

      <variablelist>
        <varlistentry>
          <term><constant>EOPNOTSUPP</constant></term>
          <listitem><para>
            The connection is not an ordinary connection, or the passed
            file descriptors in <constant>KDBUS_ITEM_FDS</constant> item are
            either kdbus handles or unix domain sockets. Both are currently
            unsupported.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EINVAL</constant></term>
          <listitem><para>
            The submitted payload type is
            <constant>KDBUS_PAYLOAD_KERNEL</constant>,
            <constant>KDBUS_MSG_EXPECT_REPLY</constant> was set without timeout
            or cookie values, <constant>KDBUS_SEND_SYNC_REPLY</constant> was
            set without <constant>KDBUS_MSG_EXPECT_REPLY</constant>, an invalid
            item was supplied, <constant>src_id</constant> was non-zero and was
            different from the current connection's ID, a supplied memfd had a
            size of 0, or a string was not properly null-terminated.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>ENOTUNIQ</constant></term>
          <listitem><para>
            The supplied destination is
            <constant>KDBUS_DST_ID_BROADCAST</constant> and either
            file descriptors were passed, or
            <constant>KDBUS_MSG_EXPECT_REPLY</constant> was set,
            or a timeout was given.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>E2BIG</constant></term>
          <listitem><para>
            Too many items.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EMSGSIZE</constant></term>
          <listitem><para>
            The size of the message header and items or the payload vector
            is excessive.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EEXIST</constant></term>
          <listitem><para>
            Multiple <constant>KDBUS_ITEM_FDS</constant>,
            <constant>KDBUS_ITEM_BLOOM_FILTER</constant> or
            <constant>KDBUS_ITEM_DST_NAME</constant> items were supplied.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EBADF</constant></term>
          <listitem><para>
            The supplied <constant>KDBUS_ITEM_FDS</constant> or
            <constant>KDBUS_ITEM_PAYLOAD_MEMFD</constant> items
            contained an illegal file descriptor.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EMEDIUMTYPE</constant></term>
          <listitem><para>
            The supplied memfd is not a sealed kdbus memfd.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EMFILE</constant></term>
          <listitem><para>
            Too many file descriptors inside a
            <constant>KDBUS_ITEM_FDS</constant>.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EBADMSG</constant></term>
          <listitem><para>
            An item had illegal size, both a <constant>dst_id</constant> and a
            <constant>KDBUS_ITEM_DST_NAME</constant> was given, or both a name
            and a bloom filter was given.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>ETXTBSY</constant></term>
          <listitem><para>
            The supplied kdbus memfd file cannot be sealed or the seal
            was removed, because it is shared with other processes or
            still mapped with
            <citerefentry>
              <refentrytitle>mmap</refentrytitle>
              <manvolnum>2</manvolnum>
            </citerefentry>.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>ECOMM</constant></term>
          <listitem><para>
            A peer does not accept the file descriptors addressed to it.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EFAULT</constant></term>
          <listitem><para>
            The supplied bloom filter size was not 64-bit aligned, or supplied
            memory could not be accessed by the kernel.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EDOM</constant></term>
          <listitem><para>
            The supplied bloom filter size did not match the bloom filter
            size of the bus.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EDESTADDRREQ</constant></term>
          <listitem><para>
            <constant>dst_id</constant> was set to
            <constant>KDBUS_DST_ID_NAME</constant>, but no
            <constant>KDBUS_ITEM_DST_NAME</constant> was attached.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>ESRCH</constant></term>
          <listitem><para>
            The name to look up was not found in the name registry.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EADDRNOTAVAIL</constant></term>
          <listitem><para>
            <constant>KDBUS_MSG_NO_AUTO_START</constant> was given but the
            destination connection is an activator.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>ENXIO</constant></term>
          <listitem><para>
            The passed numeric destination connection ID couldn't be found,
            or is not connected.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>ECONNRESET</constant></term>
          <listitem><para>
            The destination connection is no longer active.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>ETIMEDOUT</constant></term>
          <listitem><para>
            Timeout while synchronously waiting for a reply.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EINTR</constant></term>
          <listitem><para>
            Interrupted system call while synchronously waiting for a reply.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EPIPE</constant></term>
          <listitem><para>
            When sending a message, a synchronous reply from the receiving
            connection was expected but the connection died before answering.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>ENOBUFS</constant></term>
          <listitem><para>
            Too many pending messages on the receiver side.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EREMCHG</constant></term>
          <listitem><para>
            Both a well-known name and a unique name (ID) was given, but
            the name is not currently owned by that connection.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EXFULL</constant></term>
          <listitem><para>
            The memory pool of the receiver is full.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EREMOTEIO</constant></term>
          <listitem><para>
            While synchronously waiting for a reply, the remote peer
            failed with an I/O error.
          </para></listitem>
        </varlistentry>
      </variablelist>
    </refsect2>

    <refsect2>
      <title>
        <constant>KDBUS_CMD_RECV</constant> may fail with the following
        errors
      </title>

      <variablelist>
        <varlistentry>
          <term><constant>EOPNOTSUPP</constant></term>
          <listitem><para>
            The connection is not an ordinary connection, or the passed
            file descriptors are either kdbus handles or unix domain
            sockets. Both are currently unsupported.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EINVAL</constant></term>
          <listitem><para>
            Invalid flags or offset.
          </para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>EAGAIN</constant></term>
          <listitem><para>
            No message found in the queue.
          </para></listitem>
        </varlistentry>
      </variablelist>
    </refsect2>
  </refsect1>

  <refsect1>
    <title>See Also</title>
    <simplelist type="inline">
      <member>
        <citerefentry>
          <refentrytitle>kdbus</refentrytitle>
          <manvolnum>7</manvolnum>
        </citerefentry>
      </member>
      <member>
        <citerefentry>
          <refentrytitle>kdbus.bus</refentrytitle>
          <manvolnum>7</manvolnum>
        </citerefentry>
      </member>
      <member>
        <citerefentry>
          <refentrytitle>kdbus.connection</refentrytitle>
          <manvolnum>7</manvolnum>
        </citerefentry>
      </member>
      <member>
        <citerefentry>
          <refentrytitle>kdbus.endpoint</refentrytitle>
          <manvolnum>7</manvolnum>
        </citerefentry>
      </member>
      <member>
        <citerefentry>
          <refentrytitle>kdbus.fs</refentrytitle>
          <manvolnum>7</manvolnum>
        </citerefentry>
      </member>
      <member>
        <citerefentry>
          <refentrytitle>kdbus.item</refentrytitle>
          <manvolnum>7</manvolnum>
        </citerefentry>
      </member>
      <member>
        <citerefentry>
          <refentrytitle>kdbus.name</refentrytitle>
          <manvolnum>7</manvolnum>
        </citerefentry>
      </member>
      <member>
        <citerefentry>
          <refentrytitle>kdbus.pool</refentrytitle>
          <manvolnum>7</manvolnum>
        </citerefentry>
      </member>
      <member>
        <citerefentry>
          <refentrytitle>clock_gettime</refentrytitle>
          <manvolnum>2</manvolnum>
        </citerefentry>
      </member>
      <member>
        <citerefentry>
          <refentrytitle>ioctl</refentrytitle>
          <manvolnum>2</manvolnum>
        </citerefentry>
      </member>
      <member>
        <citerefentry>
          <refentrytitle>poll</refentrytitle>
          <manvolnum>2</manvolnum>
        </citerefentry>
      </member>
      <member>
        <citerefentry>
          <refentrytitle>select</refentrytitle>
          <manvolnum>2</manvolnum>
        </citerefentry>
      </member>
      <member>
        <citerefentry>
          <refentrytitle>epoll</refentrytitle>
          <manvolnum>7</manvolnum>
        </citerefentry>
      </member>
      <member>
        <citerefentry>
          <refentrytitle>eventfd</refentrytitle>
          <manvolnum>2</manvolnum>
        </citerefentry>
      </member>
      <member>
        <citerefentry>
          <refentrytitle>memfd_create</refentrytitle>
          <manvolnum>2</manvolnum>
        </citerefentry>
      </member>
    </simplelist>
  </refsect1>
</refentry>