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

Niall Murphy rinvix at gmail.com
Thu Aug 23 17:05:13 UTC 2018


Hi, thanks! You are correct about the signature, I was trying various
things and pasted in some half complete code, sorry. After changing to pass
the fields of the struct as individual parameters it does work correctly.

I guess I would prefer to pass the struct so that you can share a single
definition of the struct with both the client and service so as to avoid
possible errors where you pass the fields in the wrong order. Also if you
have a large struct it's a bit inconvenient to spell out every field on
both ends.

I thought it would be possible for sd-bus to marshall my C struct into a
D-bus struct. Perhaps that is not possible?

Niall

On Thu, 23 Aug 2018 at 18:35, Simon McVittie <smcv at collabora.com> wrote:

> 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
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/systemd-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/systemd-devel/attachments/20180823/2a30ed41/attachment.html>


More information about the systemd-devel mailing list