Ejemplo n.º 1
0
static int get_cgroup_root(char **ret) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        _cleanup_free_ char *unit = NULL, *path = NULL;
        const char *m;
        int r;

        if (arg_root) {
                char *aux;

                aux = strdup(arg_root);
                if (!aux)
                        return log_oom();

                *ret = aux;
                return 0;
        }

        if (!arg_machine) {
                r = cg_get_root_path(ret);
                if (r < 0)
                        return log_error_errno(r, "Failed to get root control group path: %m");

                return 0;
        }

        m = strjoina("/run/systemd/machines/", arg_machine);
        r = parse_env_file(m, NEWLINE, "SCOPE", &unit, NULL);
        if (r < 0)
                return log_error_errno(r, "Failed to load machine data: %m");

        path = unit_dbus_path_from_name(unit);
        if (!path)
                return log_oom();

        r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, false, &bus);
        if (r < 0)
                return log_error_errno(r, "Failed to create bus connection: %m");

        r = sd_bus_get_property_string(
                        bus,
                        "org.freedesktop.systemd1",
                        path,
                        unit_dbus_interface_from_name(unit),
                        "ControlGroup",
                        &error,
                        ret);
        if (r < 0)
                return log_error_errno(r, "Failed to query unit control group path: %s", bus_error_message(&error, r));

        return 0;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[]) {
        int r, output_flags;

        log_parse_environment();
        log_open();

        r = parse_argv(argc, argv);
        if (r <= 0)
                goto finish;

        r = pager_open(arg_no_pager, false);
        if (r > 0 && arg_full < 0)
                arg_full = true;

        output_flags =
                arg_all * OUTPUT_SHOW_ALL |
                (arg_full > 0) * OUTPUT_FULL_WIDTH |
                arg_kernel_threads * OUTPUT_KERNEL_THREADS;

        if (arg_names) {
                _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
                _cleanup_free_ char *root = NULL;
                char **name;

                STRV_FOREACH(name, arg_names) {
                        int q;

                        if (arg_show_unit != SHOW_UNIT_NONE) {
                                /* Command line arguments are unit names */
                                _cleanup_free_ char *cgroup = NULL;

                                if (!bus) {
                                        /* Connect to the bus only if necessary */
                                        r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL,
                                                                          arg_show_unit == SHOW_UNIT_USER,
                                                                          &bus);
                                        if (r < 0) {
                                                log_error_errno(r, "Failed to create bus connection: %m");
                                                goto finish;
                                        }
                                }

                                q = show_cgroup_get_unit_path_and_warn(bus, *name, &cgroup);
                                if (q < 0)
                                        goto failed;

                                if (isempty(cgroup)) {
                                        log_warning("Unit %s not found.", *name);
                                        q = -ENOENT;
                                        goto failed;
                                }

                                printf("Unit %s (%s):\n", *name, cgroup);
                                fflush(stdout);

                                q = show_cgroup_by_path(cgroup, NULL, 0, output_flags);

                        } else if (path_startswith(*name, "/sys/fs/cgroup")) {

                                printf("Directory %s:\n", *name);
                                fflush(stdout);

                                q = show_cgroup_by_path(*name, NULL, 0, output_flags);
                        } else {
                                _cleanup_free_ char *c = NULL, *p = NULL, *j = NULL;
                                const char *controller, *path;

                                if (!root) {
                                        /* Query root only if needed, treat error as fatal */
                                        r = show_cgroup_get_path_and_warn(arg_machine, NULL, &root);
                                        if (r < 0)
                                                goto finish;
                                }

                                q = cg_split_spec(*name, &c, &p);
                                if (q < 0) {
                                        log_error_errno(q, "Failed to split argument %s: %m", *name);
                                        goto failed;
                                }

                                controller = c ?: SYSTEMD_CGROUP_CONTROLLER;
                                if (p) {
                                        j = strjoin(root, "/", p);
                                        if (!j) {
                                                r = log_oom();
                                                goto finish;
                                        }

                                        path_kill_slashes(j);
                                        path = j;
                                } else
                                        path = root;

                                show_cg_info(controller, path);

                                q = show_cgroup(controller, path, NULL, 0, output_flags);
                        }

                failed:
                        if (q < 0 && r >= 0)
                                r = q;
                }

        } else {
Ejemplo n.º 3
0
int main(int argc, char* argv[]) {
    _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
    _cleanup_free_ char *description = NULL, *command = NULL;
    int r, retval = EXIT_SUCCESS;

    log_parse_environment();
    log_open();

    r = parse_argv(argc, argv);
    if (r <= 0)
        goto finish;

    if (argc > optind && arg_transport == BUS_TRANSPORT_LOCAL) {
        /* Patch in an absolute path */

        r = find_binary(argv[optind], &command);
        if (r < 0) {
            log_error_errno(r, "Failed to find executable %s: %m", argv[optind]);
            goto finish;
        }

        argv[optind] = command;
    }

    if (!arg_description) {
        description = strv_join(argv + optind, " ");
        if (!description) {
            r = log_oom();
            goto finish;
        }

        if (arg_unit && isempty(description)) {
            r = free_and_strdup(&description, arg_unit);
            if (r < 0)
                goto finish;
        }

        arg_description = description;
    }

    /* If --wait is used connect via the bus, unconditionally, as ref/unref is not supported via the limited direct
     * connection */
    if (arg_wait)
        r = bus_connect_transport(arg_transport, arg_host, arg_user, &bus);
    else
        r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus);
    if (r < 0) {
        log_error_errno(r, "Failed to create bus connection: %m");
        goto finish;
    }

    if (arg_scope)
        r = start_transient_scope(bus, argv + optind);
    else if (with_timer())
        r = start_transient_timer(bus, argv + optind);
    else
        r = start_transient_service(bus, argv + optind, &retval);

finish:
    strv_free(arg_environment);
    strv_free(arg_property);
    strv_free(arg_timer_property);

    return r < 0 ? EXIT_FAILURE : retval;
}