如何使用SD总线获取服务状态?

问题描述 投票:0回答:1

我需要从C ++应用程序查询,监视并可能更改一些systemd服务的状态。看起来sd-bus是执行此操作的正确方法,但是我很难找到一个例子。

所以,我该如何:

1)通过SD总线查询服务的当前状态,类似于systemctl status foo.service

2)监视服务的状态,以便每当服务更改时都会得到回调?

3)类似于systemctl start/stop/restart更改服务状态?

谢谢!

c++ systemd dbus
1个回答
0
投票

使用sd-bus API是绝对正确的(标题#include <systemd/sd-bus.h>

首先您需要访问总线对象:

我这样做:

Systemctl::Systemctl() :
    m_bus(nullptr)
{
    int r = sd_bus_default_system(&m_bus);

    if (r < 0)
        throw exception("Could not open systemd bus");
}

如果您在打开巴士时遇到问题:

  1. 以root / sudo身份运行
  2. 制定一些polkit策略以授予您的用户/组对此命令的访问权限
  3. 运行_user总线而不是_system总线

完成后别忘了释放总线:

Systemctl::~Systemctl()
{
    sd_bus_unref(m_bus);
}

现在您有3个问题:

  1. 查询状态

[对于每个单元,我都有一个将转义名称(foo_2eservice)保留为m_name的类,并在m_bus中引用了总线。使用任何属性调用此方法。您似乎对"ActiveState""SubState"最感兴趣。

std::string Unit::GetPropertyString(const std::string& property) const
{
    sd_bus_error err = SD_BUS_ERROR_NULL;
    char* msg = nullptr;
    int r;

    r = sd_bus_get_property_string(m_bus,
        "org.freedesktop.systemd1",
        ("/org/freedesktop/systemd1/unit/" + m_unit).c_str(),
        "org.freedesktop.systemd1.Unit",
        property.c_str(),
        &err,
        &msg);

    if (r < 0)
    {
        std::string err_msg(err.message);
        sd_bus_error_free(&err);

        std::string err_str("Failed to get " + property + " for service "
                            + m_name + ". Error: " + err_msg);

        throw exception(err_str);
    }

    sd_bus_error_free(&err);

    // Free memory (avoid leaking)
    std::string ret(msg);
    free (msg);

    return ret;
}
  1. 监视服务状态:

第一步是设置文件描述符以订阅更改。在这种情况下,您有兴趣订购“ PropertiesChanged”信号。请注意,您将获得任何属性更改的信号,而不仅仅是状态。在sd_bus_add_match()调用中,有回调的空间,尽管我还没有尝试过。

void Systemctl::SubscribeToUnitChanges(const std::string& escaped_name)
{
    /* This function is an easier helper, but it as only introduced in systemd 237
     * Stretch is on 232 while buster is on 241 .  Need re replace this as long as
     * we still support stretch
    sd_bus_match_signal(
        m_bus,
        nullptr, // slot
        nullptr, // sender
        std::string("/org/freedesktop/systemd1/unit/" + escaped_name).c_str(), // path
        "org.freedesktop.DBus.Properties", // interface
        "PropertiesChanged", // member
        nullptr, // callback
        nullptr // userdata
    );
    */
    std::string match =  "type='signal'";
        match += ",path='/org/freedesktop/systemd1/unit/" + escaped_name + "'" ;
        match += ",interface='org.freedesktop.DBus.Properties'";
        match += ",member='PropertiesChanged'";

    sd_bus_add_match(
        m_bus,
        nullptr, // slot
        match.c_str(),
        nullptr, // callback
        nullptr // userdata
    );
}

相反,我要做的是定期轮询总线以获取预订的更改并更新每个单元:

bool Systemctl::ProcessBusChanges()
{
    bool changed = false;
    sd_bus_message* msg = nullptr;

    // for each new message
    std::list<std::string> escaped_names;
    while( sd_bus_process(m_bus, &msg) )
    {
        // Note:  Once sd_bus_process returns 0, We are supposed to call
        // sd_bus_wait, or check for changes on sd_bus_get_fd before calling
        // this function again.  We're breaking that rule.  I don't really know
        // the consequences.
        if (msg)
        {
            std::string path = strna( sd_bus_message_get_path(msg) );
            sd_bus_message_unref(msg);

            std::string escaped_name = path.erase(0, path.find_last_of('/')+1 );
            escaped_names.push_back(escaped_name);

            changed = true;
        }
    }

    escaped_names.sort();
    escaped_names.unique();
    for (auto unit : escaped_names)
    {
        auto it = m_units.find(unit);
        if (it != m_units.end())
            it->second.RefreshDynamicProperties();
    }

    return changed;
}

如果它告诉我们总线已更改,那么我继续阅读该总线上所有受监视的单元。

  1. 更改状态

这很容易。我使用以下内容,其中method"StartUnit""StopUnit""RestartUnit"之一。

static void CallMethodSS(sd_bus* bus,
                         const std::string& name,
                         const std::string& method)
{
    sd_bus_error err = SD_BUS_ERROR_NULL;
    sd_bus_message* msg = nullptr;
    int r;

    r = sd_bus_call_method(bus,
        "org.freedesktop.systemd1",         /* <service>   */
        "/org/freedesktop/systemd1",        /* <path>      */
        "org.freedesktop.systemd1.Manager", /* <interface> */
        method.c_str(),                     /* <method>    */
        &err,                               /* object to return error in */
        &msg,                               /* return message on success */
        "ss",                               /* <input_signature (string-string)> */
        name.c_str(),  "replace" );         /* <arguments...> */

    if (r < 0)
    {
        std::string err_str("Could not send " + method +
                            " command to systemd for service: " + name +
                            ". Error: " + err.message );

        sd_bus_error_free(&err);
        sd_bus_message_unref(msg);
        throw exception(err_str);
    }

    // Extra stuff that might be useful:  display the response...
    char* response;
    r = sd_bus_message_read(msg, "o", &response);
    if (r < 0)
    {
      LogError("Failed to parse response message: %s\n", strerror(-r) );
    }

    sd_bus_error_free(&err);
    sd_bus_message_unref(msg);
}
© www.soinside.com 2019 - 2024. All rights reserved.