Simple C or C++ API for controlling systemd services

时间秒杀一切 提交于 2019-12-08 16:31:55

问题


Goal

I'd like to write C/C++ applications and provide an API for them to communicate directly with systemd-managed services. Effectively, I'd like my applications to be able to do the equivalent of systemctl start service_name@unit_number.service without using system(), popen(), or exec() calls (hard requirement).


Question

Is there a simple C/C++ API for communicating with systemd, assuming systemd version 219 (i.e. CentOS v7.4, also a hard requirement)?


Work So Far

  1. I've scoured online for examples of controlling systemd-managed services in C/C++ and found the odd discussion on the topic, but haven't found any good examples.
  2. I could always reverse-engineer systemctl version 219 from source, but then I potentially run afoul of GPL licensing.
  3. Since I'm stuck with CentOS v7.4, which means I can't get at version 221 or later of systemd, I can't use the newer "SD Bus" API. Nobody would allow manually upgrading libsystemd just for an application-specific benefit, and I'm apparently not the first person to raise this concern.
  4. I'm hesitant to use the low-level C API for DBUS, since the maintainers themselves state "If you use this low-level API directly, you're signing up for some pain.".

Hard Requirements

  • systemd v219.
  • CentOS v7.4
  • C/C++.
  • No system()/popen()/exec() calls to the systemctl CLI utility.
  • Non-propagating license (i.e. LGPLv2 is OK, though BSD/MIT is preferred).

Question (Redux)

Is there a simpler API that I could use on CentOS v7.4 (even if I have to supply the shared library myself) that can be used to communicate with systemd in a simple, reliable manner directly via C/C++ code? If the existing libdbus-1.so API is complicated but still reliable, and I can create a reliable/stable wrapper library around it, I'm open to exploring that option. Also, if it's possible to manually build and deploy the SD-BUS components independent of systemd and use them without having to modify/upgrade the systemd library/daemon already on the system, I could potentially go that route too.


Edits & Comments

  • I would be open to using a mature C++ library, so long as it doesn't require total release of all source.

Thank you.


回答1:


As you already figured out, you should only interact with systemd using a dbus library, there is no other supported way to do so. Even if you lifted the requirement of no execution of binaries, it will remain frowned upon, as the *ctl tools are command line user interfaces not intended or designed to be called from other programs.



来源:https://stackoverflow.com/questions/50780221/simple-c-or-c-api-for-controlling-systemd-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!