简体   繁体   中英

Wrap sd_bus in single file descriptor

I'm writing a library which will use sd_bus under the hood. My goals are

  1. Allow integration into any existing main loop (don't force a choice on clients)
  2. Stable ABI
  3. The ability to add more internal event sources later (more sockets, etc.)
  4. Hide sd_bus as an implementation detail

My idea was to use an epoll file descriptor to which I will add the sd_bus file descriptor and any other file descriptors I will have in the the future. Then my client can hook this epoll file descriptor into their main loop. I will expose a function similar to sd_bus_process which clients can call when the epoll file descriptor becomes active.

One challenge is that sd_bus needs to change its fd, timeout and watched events between every sleep (sd_bus_get_fd, sd_bus_get_events, sd_bus_get_timeout). For inspiration, I'm looking at the sd_event implementation in systemd code base, and this email from Lennart Poettering.

I'm considering to add an extra timerfd to my epoll group. Then I can adjust the timeout on the timerfd before I give back control to the client, and I can adjust the watched events on the bus fd using normal epoll calls. The client will just see a single fd which only needs POLLIN event to be watched.

Is there a better/standard way to do this? Or my idea seems reasonable?

If you're already linking to libsystemd, just use the real sd-event module. It already has correct integration with sd-bus, and is epoll-based internally so you can get a file descriptor representing it via sd_event_get_fd() for use in external event loops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM