[systemd-devel] Returning arrays from sd-bus methods

Lennart Poettering lennart at poettering.net
Tue Jun 27 07:43:50 UTC 2017


On Mon, 26.06.17 12:19, guhan balasubramanian (guhan.sac at gmail.com) wrote:

> > if you use sd_bus_reply_method_return() (or sd_bus_append()) the
> > arguments to pass for an array is the array's size followed by the
> > members. Hence, the following should do what you want:
> >
> >         sd_bus_reply_method_return(m, "ay", 2, 0x01, 0x02);
> >
> 
> This worked for me. But I had the following three questions based on this:
> 
> 1. Would it be a good design to construct a va_list of the array elements
> and pass it as the last argument in sd_bus_reply_method_return?

C doesn't support that in a portable way. But there's really no need
to, if you want to add complex data structures to a bus message, you
can do so easily, by allocating a reply bus message object, and then
adding elements piecemeal. sd_bus_reply_method_return() is really just
a convencience wrapper around sd_bus_message_new_method_return(),
sd_bus_message_append(), sd_bus_send(), and you can invoke those
directly too.

If you want to add set of array entries, you need to invoke
sd_bus_message_open_container(), followed by repeated
sd_bus_message_append(), one for each entry, and then a final
sd_bus_message_close_container(). How to do this precisely is
unfortunately a bit underdocument, but you find tons of examples if
you grep through the systemd codebase.

> 2. I wasn't able to find the definition for sd_bus_append, I think you
> meant *sd_bus_message_append*() right?

Yes, sorry.

> 3. Is there an API to pass the entire container (array in this case) for a
>  method return?

See above. There are also a couple of other helpers, including
sd_bus_message_append_array(), which does the _open_container() +
_append() loop and _close_container() in one go for you.

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list