예제 #1
0
파일: systemd.c 프로젝트: tradej/pacemaker
static char *
systemd_unit_by_name(const gchar * arg_name, svc_action_t *op)
{
    DBusMessage *msg;
    DBusMessage *reply = NULL;
    DBusPendingCall* pending = NULL;
    char *name = NULL;

/*
  Equivalent to GetUnit if its already loaded
  <method name="LoadUnit">
   <arg name="name" type="s" direction="in"/>
   <arg name="unit" type="o" direction="out"/>
  </method>
 */

    if (systemd_init() == FALSE) {
        return FALSE;
    }

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

    name = systemd_service_name(arg_name);
    CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
    free(name);

    if(op == NULL || op->synchronous) {
        const char *unit = NULL;
        char *munit = NULL;
        DBusError error;

        dbus_error_init(&error);
        reply = pcmk_dbus_send_recv(msg, systemd_proxy, &error, op? op->timeout : DBUS_TIMEOUT_USE_DEFAULT);
        dbus_message_unref(msg);

        unit = systemd_loadunit_result(reply, op);
        if(unit) {
            munit = strdup(unit);
        }
        if(reply) {
            dbus_message_unref(reply);
        }
        return munit;
    }

    pending = pcmk_dbus_send(msg, systemd_proxy, systemd_loadunit_cb, op, op->timeout);
    if(pending) {
        services_set_op_pending(op, pending);
    }

    dbus_message_unref(msg);
    return NULL;
}
예제 #2
0
gboolean
systemd_unit_exec(svc_action_t * op, gboolean synchronous)
{
    char *unit = NULL;
    GError *error = NULL;
    gboolean pass = FALSE;
    GVariant *_ret = NULL;
    const char *action = op->action;
    char *name = systemd_service_name(op->agent);

    op->rc = PCMK_EXECRA_UNKNOWN_ERROR;
    CRM_ASSERT(systemd_init());

    crm_debug("Performing %ssynchronous %s op on systemd unit %s named '%s'",
              synchronous ? "" : "a", op->action, op->agent, op->rsc);

    if (safe_str_eq(op->action, "meta-data")) {
        op->stdout_data = systemd_unit_metadata(op->agent);
        op->rc = PCMK_EXECRA_OK;
        goto cleanup;
    }

    pass = systemd_unit_by_name(systemd_proxy, op->agent, &unit, NULL, &error);
    if (error || pass == FALSE) {
        crm_debug("Could not obtain unit named '%s': %s", op->agent,
                  error ? error->message : "unknown");
        if (error && strstr(error->message, "systemd1.NoSuchUnit")) {
            op->rc = PCMK_EXECRA_NOT_INSTALLED;
        }
        g_error_free(error);
        goto cleanup;
    }

    if (safe_str_eq(op->action, "monitor") || safe_str_eq(action, "status")) {
        char *state = systemd_unit_property(unit, BUS_NAME ".Unit", "ActiveState");

        if (g_strcmp0(state, "active") == 0) {
            op->rc = PCMK_EXECRA_OK;
        } else {
            op->rc = PCMK_EXECRA_NOT_RUNNING;
        }

        free(state);
        goto cleanup;

    } else if (g_strcmp0(action, "start") == 0) {
        action = "StartUnit";
    } else if (g_strcmp0(action, "stop") == 0) {
        action = "StopUnit";
    } else if (g_strcmp0(action, "restart") == 0) {
        action = "RestartUnit";
    } else {
        op->rc = PCMK_EXECRA_UNIMPLEMENT_FEATURE;
        goto cleanup;
    }

    crm_debug("Calling %s for %s: %s", action, op->rsc, unit);
    if (synchronous == FALSE) {
        g_dbus_proxy_call(systemd_proxy, action, g_variant_new("(ss)", name, "replace"),
                          G_DBUS_CALL_FLAGS_NONE, op->timeout, NULL, systemd_unit_exec_done, op);
        free(unit);
        free(name);
        return TRUE;
    }

    _ret = g_dbus_proxy_call_sync(systemd_proxy, action, g_variant_new("(ss)", name, "replace"),
                                  G_DBUS_CALL_FLAGS_NONE, op->timeout, NULL, &error);

    if (error) {
        /* ignore "already started" or "not running" errors */
        if (safe_str_eq(op->action, "stop")
            && strstr(error->message, "systemd1.InvalidName")) {
            crm_trace("Masking Stop failure for %s: unknown services are stopped", op->rsc);
            op->rc = PCMK_EXECRA_OK;
        } else {
            crm_err("Could not issue %s for %s: %s (%s)", action, op->rsc, error->message, unit);
        }
        g_error_free(error);

    } else if(g_variant_is_of_type (_ret, G_VARIANT_TYPE("(o)"))) {
        char *path = NULL;

        g_variant_get(_ret, "(o)", &path);
        crm_info("Call to %s passed: type '%s' %s", op->action, g_variant_get_type_string(_ret),
                 path);
        op->rc = PCMK_EXECRA_OK;

    } else {
        crm_err("Call to %s passed but return type was '%s' not '(o)'", op->action, g_variant_get_type_string(_ret));
        op->rc = PCMK_EXECRA_OK;
    }

  cleanup:
    free(unit);
    free(name);

    if (_ret) {
        g_variant_unref(_ret);
    }
    if (synchronous == FALSE) {
        operation_finalize(op);
        return TRUE;
    }
    return op->rc == PCMK_EXECRA_OK;
}
예제 #3
0
파일: systemd.c 프로젝트: urusha/pacemaker
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;
}
예제 #4
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;
}