[systemd-devel] Returning a struct from an sd-bus method

Simon McVittie smcv at collabora.com
Thu Aug 23 16:34:14 UTC 2018


On Thu, 23 Aug 2018 at 16:52:38 +0200, Niall Murphy wrote:
> struct pack {
>     int x;
>     int y;
> };
...
>     return sd_bus_reply_method_return(m, "(xx)", s);

Is there a reason why you're returning a struct/tuple? D-Bus methods
can return as many things as you want[1], unlike C functions, so a more
conventional return type would be "xx" instead of "(xx)".

You also have a mismatch between your integer types: "x" is a 64-bit signed
integer (mnemonic: the 64-bit signed and unsigned types are "x" and "t",
which are the first letters of "sixty" that weren't already used for
something more important) so they'll expect an int64_t. The type
signatures for 32-bit integers are "i" and "u", depending on signedness.
There is no correct type signature for types like int/long/long long
whose size can vary between platform: integers in D-Bus messages are
always fixed-size.

Finally, I think the message-building API expects struct members as
individual arguments, like

    sd_bus_reply_method_return(m, "xx", (int64_t) s->x, (int64_t) s->y);

although I could be wrong about that.

    smcv

[1] up to arbitrary message size limits measured in megabytes


More information about the systemd-devel mailing list