问题描述
我想编写C / C ++应用程序,并提供API以使其直接与 systemd
-通信管理服务。实际上,我希望我的应用程序能够执行 systemctl start service_name@unit_number.service
的等效功能,而无需使用 system()
, popen()
或 exec()
调用(硬性要求 )。
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).
是否有用于通信的简单 C / C ++ API使用 systemd
,并假定使用 systemd
219版(即CentOS v7.4,也是一个硬要求 >)?
Is there a simple C/C++ API for communicating with systemd
, assuming systemd
version 219 (i.e. CentOS v7.4, also a hard requirement)?
- 我在网上搜索了示例在C / C ++和systemd 管理的服务rel = noreferrer>找到了关于该主题的奇怪讨论,但没有找到任何好的例子。
- 我总是可以对,但随后可能会违反GPL许可。
- ,因为维护者自己会声明 如果您使用此。
- 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. - I could always reverse-engineer
systemctl
version 219 from source, but then I potentially run afoul of GPL licensing. - 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. - 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.".
硬要求
-
systemd
v219。 - CentOS v7.4
- C / C ++。
- 否
system()
/对
/systemctl
CLI实用程序的popen()exec()
调用。 - 非传播许可证(即LGPLv2可以,但最好使用BSD / MIT)。
systemd
v219.- CentOS v7.4
- C/C++.
- No
system()
/popen()
/exec()
calls to thesystemctl
CLI utility. - Non-propagating license (i.e. LGPLv2 is OK, though BSD/MIT is preferred).
- 只要不要求完全发布所有源代码,我愿意使用成熟的C ++库。
Hard Requirements
我可以使用更简单的API吗?在CentOS v7.4上的ld使用(即使我必须自己提供共享库)也可以用于通过C /以简单,可靠的方式与 systemd
进行通信C ++代码? 如果现有的 libdbus-1.so
API很复杂但仍然可靠,并且我可以围绕它创建一个可靠/稳定的包装器库,那么我愿意探索。此外,如果可以手动构建和部署独立于 systemd
的SD-BUS组件并使用它们,而无需修改/升级 systemd
库/守护程序已经在系统上了,我也有可能走这条路线。
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.
谢谢。
推荐答案
如您所知,您只应使用dbus库与systemd进行交互,没有其他支持的方法。
即使您取消了不执行二进制文件的要求,它也仍然会令人皱眉,因为* ctl工具是命令行用户界面,无意或设计为从其他程序调用。
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.
这篇关于简单的C或C ++ API,用于控制systemd服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!