Ejemplo n.º 1
0
gboolean
systemd_unit_exec_with_unit(svc_action_t * op, const char *unit)
{
    const char *method = op->action;
    DBusMessage *msg = NULL;
    DBusMessage *reply = NULL;

    CRM_ASSERT(unit);

    if (unit == NULL) {
        crm_debug("Could not obtain unit named '%s'", op->agent);
        op->rc = PCMK_OCF_NOT_INSTALLED;
        op->status = PCMK_LRM_OP_NOT_INSTALLED;
        goto cleanup;
    }

    if (safe_str_eq(op->action, "monitor") || safe_str_eq(method, "status")) {
        char *state = pcmk_dbus_get_property(systemd_proxy, BUS_NAME, unit, BUS_NAME ".Unit", "ActiveState",
                                             op->synchronous?NULL:systemd_unit_check, op);
        if (op->synchronous) {
            systemd_unit_check("ActiveState", state, op);
            free(state);
            return op->rc == PCMK_OCF_OK;
        }
        return TRUE;

    } else if (g_strcmp0(method, "start") == 0) {
        FILE *file_strm = NULL;
        char *override_dir = g_strdup_printf("%s/%s", SYSTEMD_OVERRIDE_ROOT, unit);
        char *override_file = g_strdup_printf("%s/%s/50-pacemaker.conf", SYSTEMD_OVERRIDE_ROOT, unit);

        method = "StartUnit";
        crm_build_path(override_dir, 0755);

        file_strm = fopen(override_file, "w");
        if (file_strm != NULL) {
            int rc = fprintf(file_strm, "[Service]\nRestart=no");
            if (rc < 0) {
                crm_perror(LOG_ERR, "Cannot write to systemd override file %s", override_file);
            }

        } else {
            crm_err("Cannot open systemd override file %s for writing", override_file);
        }

        if (file_strm != NULL) {
            fflush(file_strm);
            fclose(file_strm);
        }
        systemd_daemon_reload();
        free(override_file);
        free(override_dir);

    } else if (g_strcmp0(method, "stop") == 0) {
        char *override_file = g_strdup_printf("%s/%s/50-pacemaker.conf", SYSTEMD_OVERRIDE_ROOT, unit);

        method = "StopUnit";
        unlink(override_file);
        free(override_file);
        systemd_daemon_reload();

    } else if (g_strcmp0(method, "restart") == 0) {
        method = "RestartUnit";

    } else {
        op->rc = PCMK_OCF_UNIMPLEMENT_FEATURE;
        goto cleanup;
    }

    crm_debug("Calling %s for %s: %s", method, op->rsc, unit);

    msg = systemd_new_method(BUS_NAME".Manager", method);
    CRM_ASSERT(msg != NULL);

    /* (ss) */
    {
        const char *replace_s = "replace";
        char *name = systemd_service_name(op->agent);

        CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
        CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &replace_s, DBUS_TYPE_INVALID));

        free(name);
    }

    if (op->synchronous == FALSE) {
        return pcmk_dbus_send(msg, systemd_proxy, systemd_async_dispatch, op);

    } else {
        DBusError error;

        reply = pcmk_dbus_send_recv(msg, systemd_proxy, &error);
        systemd_exec_result(reply, op);
        if(reply) {
            dbus_message_unref(reply);
        }
    }

    if(msg) {
        dbus_message_unref(msg);
    }

  cleanup:
    if (op->synchronous == FALSE) {
        operation_finalize(op);
        return TRUE;
    }

    return op->rc == PCMK_OCF_OK;
}
Ejemplo n.º 2
0
static gboolean
systemd_unit_by_name(GDBusProxy * proxy,
                     const gchar * arg_name,
                     gchar ** out_unit, GCancellable * cancellable, GError ** error)
{
    GError *reload_error = NULL;
    GVariant *_ret = NULL;
    char *name = NULL;
    int retry = 0;

/*
  "  <method name=\"GetUnit\">\n"                                 \
  "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
  "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
  "  </method>\n"                                                 \
*/

    name = systemd_service_name(arg_name);
    crm_debug("Calling GetUnit");
    _ret = g_dbus_proxy_call_sync(proxy, "GetUnit", g_variant_new("(s)", name),
                                  G_DBUS_CALL_FLAGS_NONE, -1, cancellable, error);

    if (_ret) {
        crm_debug("Checking output");
        g_variant_get(_ret, "(o)", out_unit);
        crm_debug("%s = %s", arg_name, *out_unit);
        g_variant_unref(_ret);
        goto done;
    }

    crm_debug("Reloading the systemd manager configuration");
    systemd_daemon_reload(proxy, &reload_error);
    retry++;

    if (reload_error) {
        crm_err("Cannot reload the systemd manager configuration: %s", reload_error->message);
        g_error_free(reload_error);
        goto done;
    }

    if (*error) {
        crm_debug("Cannot find %s: %s", name, (*error)->message);
        g_error_free(*error);
        *error = NULL;
    }

/*
  <method name="LoadUnit">
   <arg name="name" type="s" direction="in"/>
   <arg name="unit" type="o" direction="out"/>
  </method>
 */
    crm_debug("Calling LoadUnit");
    _ret = g_dbus_proxy_call_sync(proxy, "LoadUnit", g_variant_new("(s)", name),
                                  G_DBUS_CALL_FLAGS_NONE, -1, cancellable, error);

    if (_ret) {
        crm_debug("Checking output");
        g_variant_get(_ret, "(o)", out_unit);
        crm_debug("%s = %s", arg_name, *out_unit);
        g_variant_unref(_ret);
    }

  done:
    free(name);
    return _ret != NULL;
}