Start systemd service from C/C++ application or call a D-Bus service

不打扰是莪最后的温柔 提交于 2019-12-18 16:19:13

问题


I have a .service for a process that i don't want to start at boot-time, but to call it somehow from another already running application, at a given time.

The other option would be to put a D-Bus (i'm using glib dbus in my apps ) service file in /usr/share/dbus-1/services and somehow call it from my application. Also, i don't manage to do this either.

Let's say that my dbus service file from /usr/share/dbus-1/services is com.callThis.service and my main service file from /lib/systemd/system is com.startThis.service

If i run a simple introspect from command line:

/home/root # dbus-send --session --type=method_call --print-reply \
--dest=com.callThis  /com/callThis org.freedesktop.DBus.Introspectable.Introspect

the D-Bus service file will get called and it will start what is in the Exec ( com.starThis ). The problem is that i want to achieve this from C/C++ code using D-Bus glib.


回答1:


A combination of g_dbus_connection_send_message with g_dbus_message_new_method_call or g_dbus_message_new_signal should be what you are looking for.




回答2:


I had trouble to do the same thing. The discover of : G_BUS_NAME_WATCHER_FLAGS_AUTO_START solve it.

g_bus_watch_name(G_BUS_TYPE_SYSTEM, "com.mydbus.listen",
      G_BUS_NAME_WATCHER_FLAGS_AUTO_START, xOnNameAppeared, xOnNameVanished,
      this, nullptr);


来源:https://stackoverflow.com/questions/13843093/start-systemd-service-from-c-c-application-or-call-a-d-bus-service

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