コード例 #1
0
int main(int argc, char *argv[]) {
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
        _cleanup_(sd_bus_track_unrefp) sd_bus_track *x = NULL, *y = NULL;
        _cleanup_(sd_bus_unrefp) sd_bus *a = NULL, *b = NULL;
        const char *unique;
        int r;

        r = sd_event_default(&event);
        assert_se(r >= 0);

        r = sd_bus_open_system(&a);
        if (IN_SET(r, -ECONNREFUSED, -ENOENT)) {
                log_info("Failed to connect to bus, skipping tests.");
                return EXIT_TEST_SKIP;
        }
        assert_se(r >= 0);

        r = sd_bus_attach_event(a, event, SD_EVENT_PRIORITY_NORMAL);
        assert_se(r >= 0);

        r = sd_bus_open_system(&b);
        assert_se(r >= 0);

        r = sd_bus_attach_event(b, event, SD_EVENT_PRIORITY_NORMAL);
        assert_se(r >= 0);

        /* Watch b's name from a */
        r = sd_bus_track_new(a, &x, track_cb_x, NULL);
        assert_se(r >= 0);

        r = sd_bus_get_unique_name(b, &unique);
        assert_se(r >= 0);

        r = sd_bus_track_add_name(x, unique);
        assert_se(r >= 0);

        /* Watch's a's own name from a */
        r = sd_bus_track_new(a, &y, track_cb_y, NULL);
        assert_se(r >= 0);

        r = sd_bus_get_unique_name(a, &unique);
        assert_se(r >= 0);

        r = sd_bus_track_add_name(y, unique);
        assert_se(r >= 0);

        /* Now make b's name disappear */
        sd_bus_close(b);

        r = sd_event_loop(event);
        assert_se(r >= 0);

        assert_se(track_cb_called_x);
        assert_se(track_cb_called_y);

        return 0;
}
コード例 #2
0
ファイル: test-bus-proxy.c プロジェクト: ikeberlein/systemd
static void test_proxy_matched(void) {
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *a = NULL;
        _cleanup_free_ char *matchstr = NULL;
        TestProxyMatch match = {};
        const char *me;
        int r;

        /* open bus 'a' */

        r = sd_bus_new(&a);
        assert_se(r >= 0);

        r = sd_bus_set_address(a, "unix:path=/var/run/dbus/system_bus_socket");
        assert_se(r >= 0);

        r = sd_bus_set_bus_client(a, true);
        assert_se(r >= 0);

        r = sd_bus_start(a);
        assert_se(r >= 0);

        r = sd_bus_get_unique_name(a, &me);
        assert_se(r >= 0);

        matchstr = strjoin("type='signal',"
                           "member='NameAcquired',"
                           "destination='",
                           me,
                           "'",
                           NULL);
        assert_se(matchstr);
        r = sd_bus_add_match(a, NULL, matchstr, test_proxy_acquired, &match);
        assert_se(r >= 0);

        r = sd_bus_get_unique_name(a, &match.sender);
        assert_se(r >= 0);

        /* barrier to guarantee proxy/dbus-daemon handled the previous data  */
        r = sd_bus_call_method(a,
                               "org.freedesktop.DBus",
                               "/org/freedesktop/DBus",
                               "org.freedesktop.DBus",
                               "GetId",
                               NULL, NULL, NULL);
        assert_se(r >= 0);

        /* now we can be sure the Name* signals were sent */
        do {
                r = sd_bus_process(a, NULL);
        } while (r > 0);
        assert_se(r == 0);

        assert_se(match.matched_acquired == 1);
}
コード例 #3
0
ファイル: nspawn-register.c プロジェクト: Werkov/systemd
static int append_controller_property(sd_bus *bus, sd_bus_message *m) {
        const char *unique;
        int r;

        assert(bus);
        assert(m);

        r = sd_bus_get_unique_name(bus, &unique);
        if (r < 0)
                return log_error_errno(r, "Failed to get unique name: %m");

        r = sd_bus_message_append(m, "(sv)", "Controller", "s", unique);
        if (r < 0)
                return bus_log_create_error(r);

        return 0;
}
コード例 #4
0
ファイル: run.c プロジェクト: pszewczyk/systemd
static int make_unit_name(sd_bus *bus, UnitType t, char **ret) {
    const char *unique, *id;
    char *p;
    int r;

    assert(bus);
    assert(t >= 0);
    assert(t < _UNIT_TYPE_MAX);

    r = sd_bus_get_unique_name(bus, &unique);
    if (r < 0) {
        sd_id128_t rnd;

        /* We couldn't get the unique name, which is a pretty
         * common case if we are connected to systemd
         * directly. In that case, just pick a random uuid as
         * name */

        r = sd_id128_randomize(&rnd);
        if (r < 0)
            return log_error_errno(r, "Failed to generate random run unit name: %m");

        if (asprintf(ret, "run-r" SD_ID128_FORMAT_STR ".%s", SD_ID128_FORMAT_VAL(rnd), unit_type_to_string(t)) < 0)
            return log_oom();

        return 0;
    }

    /* We managed to get the unique name, then let's use that to
     * name our transient units. */

    id = startswith(unique, ":1.");
    if (!id) {
        log_error("Unique name %s has unexpected format.", unique);
        return -EINVAL;
    }

    p = strjoin("run-u", id, ".", unit_type_to_string(t), NULL);
    if (!p)
        return log_oom();

    *ret = p;
    return 0;
}
コード例 #5
0
int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) {
        _cleanup_free_ char *match = NULL;
        const char *unique;
        int r;

        assert(e);
        assert(bus);
        assert(name);

        /* We unregister the name here and then wait for the
         * NameOwnerChanged signal for this event to arrive before we
         * quit. We do this in order to make sure that any queued
         * requests are still processed before we really exit. */

        r = sd_bus_get_unique_name(bus, &unique);
        if (r < 0)
                return r;

        r = asprintf(&match,
                     "sender='org.freedesktop.DBus',"
                     "type='signal',"
                     "interface='org.freedesktop.DBus',"
                     "member='NameOwnerChanged',"
                     "path='/org/freedesktop/DBus',"
                     "arg0='%s',"
                     "arg1='%s',"
                     "arg2=''", name, unique);
        if (r < 0)
                return -ENOMEM;

        r = sd_bus_add_match(bus, NULL, match, name_owner_change_callback, e);
        if (r < 0)
                return r;

        r = sd_bus_release_name(bus, name);
        if (r < 0)
                return r;

        return 0;
}
コード例 #6
0
ファイル: dbus.c プロジェクト: openbmc/ipmitool
static int ipmi_dbus_setup(struct ipmi_intf *intf)
{
	const char *name;
	int rc;

	rc = sd_bus_default(&bus);
	if (rc < 0) {
		lprintf(LOG_ERR, "Can't connect to session bus: %s\n",
				strerror(-rc));
		return -1;
	}

	sd_bus_add_object_vtable(bus, NULL, object_path, interface,
			dbus_vtable, NULL);

	sd_bus_request_name(bus, bus_name, SD_BUS_NAME_REPLACE_EXISTING);

	sd_bus_flush(bus);
	sd_bus_get_unique_name(bus, &name);
	intf->opened = 1;

	return 0;
}
コード例 #7
0
ファイル: test-bus-chat.c プロジェクト: Distrotech/systemd
static int server_init(sd_bus **_bus) {
        sd_bus *bus = NULL;
        sd_id128_t id;
        int r;
        const char *unique;

        assert_se(_bus);

        r = sd_bus_open_user(&bus);
        if (r < 0) {
                log_error_errno(r, "Failed to connect to user bus: %m");
                goto fail;
        }

        r = sd_bus_get_bus_id(bus, &id);
        if (r < 0) {
                log_error_errno(r, "Failed to get server ID: %m");
                goto fail;
        }

        r = sd_bus_get_unique_name(bus, &unique);
        if (r < 0) {
                log_error_errno(r, "Failed to get unique name: %m");
                goto fail;
        }

        log_info("Peer ID is " SD_ID128_FORMAT_STR ".", SD_ID128_FORMAT_VAL(id));
        log_info("Unique ID: %s", unique);
        log_info("Can send file handles: %i", sd_bus_can_send(bus, 'h'));

        r = sd_bus_request_name(bus, "org.freedesktop.systemd.test", 0);
        if (r < 0) {
                log_error_errno(r, "Failed to acquire name: %m");
                goto fail;
        }

        r = sd_bus_add_fallback(bus, NULL, "/foo/bar", object_callback, NULL);
        if (r < 0) {
                log_error_errno(r, "Failed to add object: %m");
                goto fail;
        }

        r = sd_bus_add_match(bus, NULL, "type='signal',interface='foo.bar',member='Notify'", match_callback, NULL);
        if (r < 0) {
                log_error_errno(r, "Failed to add match: %m");
                goto fail;
        }

        r = sd_bus_add_match(bus, NULL, "type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged'", match_callback, NULL);
        if (r < 0) {
                log_error_errno(r, "Failed to add match: %m");
                goto fail;
        }

        bus_match_dump(&bus->match_callbacks, 0);

        *_bus = bus;
        return 0;

fail:
        if (bus)
                sd_bus_unref(bus);

        return r;
}
コード例 #8
0
int main(int argc, char *argv[]) {
        _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL;
        const char *unique;
        uint8_t *p;
        sd_bus *a, *b;
        int r, bus_ref;
        sd_bus_message *m;
        int f;
        uint64_t sz;
        uint32_t u32;
        size_t i, l;
        char *s;
        _cleanup_close_ int sfd = -1;

        log_set_max_level(LOG_DEBUG);

        assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);

        bus_ref = bus_kernel_create_bus(name, false, &bus_name);
        if (bus_ref == -ENOENT)
                return EXIT_TEST_SKIP;

        assert_se(bus_ref >= 0);

        address = strappend("kernel:path=", bus_name);
        assert_se(address);

        r = sd_bus_new(&a);
        assert_se(r >= 0);

        r = sd_bus_new(&b);
        assert_se(r >= 0);

        r = sd_bus_set_address(a, address);
        assert_se(r >= 0);

        r = sd_bus_set_address(b, address);
        assert_se(r >= 0);

        r = sd_bus_start(a);
        assert_se(r >= 0);

        r = sd_bus_start(b);
        assert_se(r >= 0);

        r = sd_bus_get_unique_name(a, &unique);
        assert_se(r >= 0);

        r = sd_bus_message_new_method_call(b, &m, unique, "/a/path", "an.inter.face", "AMethod");
        assert_se(r >= 0);

        r = sd_bus_message_open_container(m, 'r', "aysay");
        assert_se(r >= 0);

        r = sd_bus_message_append_array_space(m, 'y', FIRST_ARRAY, (void**) &p);
        assert_se(r >= 0);

        p[0] = '<';
        memset(p+1, 'L', FIRST_ARRAY-2);
        p[FIRST_ARRAY-1] = '>';

        f = memfd_new_and_map(NULL, STRING_SIZE, (void**) &s);
        assert_se(f >= 0);

        s[0] = '<';
        for (i = 1; i < STRING_SIZE-2; i++)
                s[i] = '0' + (i % 10);
        s[STRING_SIZE-2] = '>';
        s[STRING_SIZE-1] = 0;
        munmap(s, STRING_SIZE);

        r = memfd_get_size(f, &sz);
        assert_se(r >= 0);
        assert_se(sz == STRING_SIZE);

        r = sd_bus_message_append_string_memfd(m, f, 0, (uint64_t) -1);
        assert_se(r >= 0);

        close(f);

        f = memfd_new_and_map(NULL, SECOND_ARRAY, (void**) &p);
        assert_se(f >= 0);

        p[0] = '<';
        memset(p+1, 'P', SECOND_ARRAY-2);
        p[SECOND_ARRAY-1] = '>';
        munmap(p, SECOND_ARRAY);

        r = memfd_get_size(f, &sz);
        assert_se(r >= 0);
        assert_se(sz == SECOND_ARRAY);

        r = sd_bus_message_append_array_memfd(m, 'y', f, 0, (uint64_t) -1);
        assert_se(r >= 0);

        close(f);

        r = sd_bus_message_close_container(m);
        assert_se(r >= 0);

        r = sd_bus_message_append(m, "u", 4711);
        assert_se(r >= 0);

        assert_se((sfd = memfd_new_and_map(NULL, 6, (void**) &p)) >= 0);
        memcpy(p, "abcd\0", 6);
        munmap(p, 6);
        assert_se(sd_bus_message_append_string_memfd(m, sfd, 1, 4) >= 0);

        r = bus_message_seal(m, 55, 99*USEC_PER_SEC);
        assert_se(r >= 0);

        bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);

        r = sd_bus_send(b, m, NULL);
        assert_se(r >= 0);

        sd_bus_message_unref(m);

        r = sd_bus_process(a, &m);
        assert_se(r > 0);

        bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
        sd_bus_message_rewind(m, true);

        r = sd_bus_message_enter_container(m, 'r', "aysay");
        assert_se(r > 0);

        r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
        assert_se(r > 0);
        assert_se(l == FIRST_ARRAY);

        assert_se(p[0] == '<');
        for (i = 1; i < l-1; i++)
                assert_se(p[i] == 'L');
        assert_se(p[l-1] == '>');

        r = sd_bus_message_read(m, "s", &s);
        assert_se(r > 0);

        assert_se(s[0] == '<');
        for (i = 1; i < STRING_SIZE-2; i++)
                assert_se(s[i] == (char) ('0' + (i % 10)));
        assert_se(s[STRING_SIZE-2] == '>');
        assert_se(s[STRING_SIZE-1] == 0);

        r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
        assert_se(r > 0);
        assert_se(l == SECOND_ARRAY);

        assert_se(p[0] == '<');
        for (i = 1; i < l-1; i++)
                assert_se(p[i] == 'P');
        assert_se(p[l-1] == '>');

        r = sd_bus_message_exit_container(m);
        assert_se(r > 0);

        r = sd_bus_message_read(m, "u", &u32);
        assert_se(r > 0);
        assert_se(u32 == 4711);

        r = sd_bus_message_read(m, "s", &s);
        assert_se(r > 0);
        assert_se(streq_ptr(s, "bcd"));

        sd_bus_message_unref(m);

        sd_bus_unref(a);
        sd_bus_unref(b);

        return 0;
}
コード例 #9
0
ファイル: test-bus-benchmark.c プロジェクト: arthur-c/systemd
int main(int argc, char *argv[]) {
        enum {
                MODE_BISECT,
                MODE_CHART,
        } mode = MODE_BISECT;
        Type type = TYPE_KDBUS;
        int i, pair[2] = { -1, -1 };
        _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL, *server_name = NULL;
        _cleanup_close_ int bus_ref = -1;
        const char *unique;
        cpu_set_t cpuset;
        size_t result;
        sd_bus *b;
        pid_t pid;
        int r;

        for (i = 1; i < argc; i++) {
                if (streq(argv[i], "chart")) {
                        mode = MODE_CHART;
                        continue;
                } else if (streq(argv[i], "legacy")) {
                        type = TYPE_LEGACY;
                        continue;
                } else if (streq(argv[i], "direct")) {
                        type = TYPE_DIRECT;
                        continue;
                }

                assert_se(parse_sec(argv[i], &arg_loop_usec) >= 0);
        }

        assert_se(!MODE_BISECT || TYPE_KDBUS);

        assert_se(arg_loop_usec > 0);

        if (type == TYPE_KDBUS) {
                assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);

                bus_ref = bus_kernel_create_bus(name, false, &bus_name);
                if (bus_ref == -ENOENT)
                        exit(EXIT_TEST_SKIP);

                assert_se(bus_ref >= 0);

                address = strappend("kernel:path=", bus_name);
                assert_se(address);
        } else if (type == TYPE_LEGACY) {
                const char *e;

                e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
                assert_se(e);

                address = strdup(e);
                assert_se(address);
        }

        r = sd_bus_new(&b);
        assert_se(r >= 0);

        if (type == TYPE_DIRECT) {
                assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) >= 0);

                r = sd_bus_set_fd(b, pair[0], pair[0]);
                assert_se(r >= 0);

                r = sd_bus_set_server(b, true, SD_ID128_NULL);
                assert_se(r >= 0);
        } else {
                r = sd_bus_set_address(b, address);
                assert_se(r >= 0);

                r = sd_bus_set_bus_client(b, true);
                assert_se(r >= 0);
        }

        r = sd_bus_start(b);
        assert_se(r >= 0);

        if (type != TYPE_DIRECT) {
                r = sd_bus_get_unique_name(b, &unique);
                assert_se(r >= 0);

                server_name = strdup(unique);
                assert_se(server_name);
        }

        sync();
        setpriority(PRIO_PROCESS, 0, -19);

        pid = fork();
        assert_se(pid >= 0);

        if (pid == 0) {
                CPU_ZERO(&cpuset);
                CPU_SET(0, &cpuset);
                pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);

                safe_close(bus_ref);
                sd_bus_unref(b);

                switch (mode) {
                case MODE_BISECT:
                        client_bisect(address, server_name);
                        break;

                case MODE_CHART:
                        client_chart(type, address, server_name, pair[1]);
                        break;
                }

                _exit(0);
        }

        CPU_ZERO(&cpuset);
        CPU_SET(1, &cpuset);
        pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);

        server(b, &result);

        if (mode == MODE_BISECT)
                printf("Copying/memfd are equally fast at %zu bytes\n", result);

        assert_se(waitpid(pid, NULL, 0) == pid);

        safe_close(pair[1]);
        sd_bus_unref(b);

        return 0;
}
コード例 #10
0
ファイル: test-bus-kernel.c プロジェクト: AOSC-Dev/systemd
int main(int argc, char *argv[]) {
        _cleanup_close_ int bus_ref = -1;
        _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL, *bname = NULL;
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        const char *ua = NULL, *ub = NULL, *the_string = NULL;
        sd_bus *a, *b;
        int r, pipe_fds[2];
        const char *nn;

        log_set_max_level(LOG_DEBUG);

        assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);

        bus_ref = bus_kernel_create_bus(name, false, &bus_name);
        if (bus_ref == -ENOENT)
                return EXIT_TEST_SKIP;

        assert_se(bus_ref >= 0);

        address = strappend("kernel:path=", bus_name);
        assert_se(address);

        r = sd_bus_new(&a);
        assert_se(r >= 0);

        r = sd_bus_new(&b);
        assert_se(r >= 0);

        r = sd_bus_set_description(a, "a");
        assert_se(r >= 0);

        r = sd_bus_set_address(a, address);
        assert_se(r >= 0);

        r = sd_bus_set_address(b, address);
        assert_se(r >= 0);

        assert_se(sd_bus_negotiate_timestamp(a, 1) >= 0);
        assert_se(sd_bus_negotiate_creds(a, true, _SD_BUS_CREDS_ALL) >= 0);

        assert_se(sd_bus_negotiate_timestamp(b, 0) >= 0);
        assert_se(sd_bus_negotiate_creds(b, true, 0) >= 0);

        r = sd_bus_start(a);
        assert_se(r >= 0);

        r = sd_bus_start(b);
        assert_se(r >= 0);

        assert_se(sd_bus_negotiate_timestamp(b, 1) >= 0);
        assert_se(sd_bus_negotiate_creds(b, true, _SD_BUS_CREDS_ALL) >= 0);

        r = sd_bus_get_unique_name(a, &ua);
        assert_se(r >= 0);
        printf("unique a: %s\n", ua);

        r = sd_bus_get_description(a, &nn);
        assert_se(r >= 0);
        printf("name of a: %s\n", nn);

        r = sd_bus_get_unique_name(b, &ub);
        assert_se(r >= 0);
        printf("unique b: %s\n", ub);

        r = sd_bus_get_description(b, &nn);
        assert_se(r >= 0);
        printf("name of b: %s\n", nn);

        assert_se(bus_kernel_get_bus_name(b, &bname) >= 0);
        assert_se(endswith(bname, name));

        r = sd_bus_call_method(a, "this.doesnt.exist", "/foo", "meh.mah", "muh", &error, NULL, "s", "yayayay");
        assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_SERVICE_UNKNOWN));
        assert_se(r == -EHOSTUNREACH);

        r = sd_bus_add_match(b, NULL, "interface='waldo.com',member='Piep'", NULL, NULL);
        assert_se(r >= 0);

        r = sd_bus_emit_signal(a, "/foo/bar/waldo", "waldo.com", "Piep", "sss", "I am a string", "/this/is/a/path", "and.this.a.domain.name");
        assert_se(r >= 0);

        r = sd_bus_try_close(b);
        assert_se(r == -EBUSY);

        r = sd_bus_process_priority(b, -10, &m);
        assert_se(r == 0);

        r = sd_bus_process(b, &m);
        assert_se(r > 0);
        assert_se(m);

        bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
        assert_se(sd_bus_message_rewind(m, true) >= 0);

        r = sd_bus_message_read(m, "s", &the_string);
        assert_se(r >= 0);
        assert_se(streq(the_string, "I am a string"));

        sd_bus_message_unref(m);
        m = NULL;

        r = sd_bus_request_name(a, "net.x0pointer.foobar", 0);
        assert_se(r >= 0);

        r = sd_bus_message_new_method_call(b, &m, "net.x0pointer.foobar", "/a/path", "an.inter.face", "AMethod");
        assert_se(r >= 0);

        assert_se(pipe2(pipe_fds, O_CLOEXEC) >= 0);

        assert_se(write(pipe_fds[1], "x", 1) == 1);

        pipe_fds[1] = safe_close(pipe_fds[1]);

        r = sd_bus_message_append(m, "h", pipe_fds[0]);
        assert_se(r >= 0);

        pipe_fds[0] = safe_close(pipe_fds[0]);

        r = sd_bus_send(b, m, NULL);
        assert_se(r >= 0);

        for (;;) {
                sd_bus_message_unref(m);
                m = NULL;
                r = sd_bus_process(a, &m);
                assert_se(r > 0);
                assert_se(m);

                bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
                assert_se(sd_bus_message_rewind(m, true) >= 0);

                if (sd_bus_message_is_method_call(m, "an.inter.face", "AMethod")) {
                        int fd;
                        char x;

                        r = sd_bus_message_read(m, "h", &fd);
                        assert_se(r >= 0);

                        assert_se(read(fd, &x, 1) == 1);
                        assert_se(x == 'x');
                        break;
                }
        }

        r = sd_bus_release_name(a, "net.x0pointer.foobar");
        assert_se(r >= 0);

        r = sd_bus_release_name(a, "net.x0pointer.foobar");
        assert_se(r == -ESRCH);

        r = sd_bus_try_close(a);
        assert_se(r >= 0);

        sd_bus_unref(a);
        sd_bus_unref(b);

        return 0;
}
コード例 #11
0
ファイル: test-bus-kernel.c プロジェクト: prodigeni/systemd
int main(int argc, char *argv[]) {
        _cleanup_close_ int bus_ref = -1;
        _cleanup_free_ char *bus_name = NULL, *address = NULL;
        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
        const char *ua = NULL, *ub = NULL, *the_string = NULL;
        sd_bus *a, *b;
        int r, pipe_fds[2];

        log_set_max_level(LOG_DEBUG);

        bus_ref = bus_kernel_create("deine-mutter", &bus_name);
        if (bus_ref == -ENOENT)
                return EXIT_TEST_SKIP;

        assert_se(bus_ref >= 0);

        address = strappend("kernel:path=", bus_name);
        assert_se(address);

        r = sd_bus_new(&a);
        assert_se(r >= 0);

        r = sd_bus_new(&b);
        assert_se(r >= 0);

        r = sd_bus_set_address(a, address);
        assert_se(r >= 0);

        r = sd_bus_set_address(b, address);
        assert_se(r >= 0);

        assert_se(sd_bus_negotiate_attach_comm(a, 1) >= 0);
        assert_se(sd_bus_negotiate_attach_exe(a, 1) >= 0);
        assert_se(sd_bus_negotiate_attach_cmdline(a, 1) >= 0);
        assert_se(sd_bus_negotiate_attach_cgroup(a, 1) >= 0);
        assert_se(sd_bus_negotiate_attach_caps(a, 1) >= 0);
        assert_se(sd_bus_negotiate_attach_selinux_context(a, 1) >= 0);
        assert_se(sd_bus_negotiate_attach_audit(a, 1) >= 0);

        assert_se(sd_bus_negotiate_attach_comm(b, 1) >= 0);
        assert_se(sd_bus_negotiate_attach_exe(b, 1) >= 0);
        assert_se(sd_bus_negotiate_attach_cmdline(b, 1) >= 0);
        assert_se(sd_bus_negotiate_attach_cgroup(b, 1) >= 0);
        assert_se(sd_bus_negotiate_attach_caps(b, 1) >= 0);
        assert_se(sd_bus_negotiate_attach_selinux_context(b, 1) >= 0);
        assert_se(sd_bus_negotiate_attach_audit(b, 1) >= 0);

        r = sd_bus_start(a);
        assert_se(r >= 0);

        r = sd_bus_start(b);
        assert_se(r >= 0);

        r = sd_bus_get_unique_name(a, &ua);
        assert_se(r >= 0);

        printf("unique a: %s\n", ua);

        r = sd_bus_get_unique_name(b, &ub);
        assert_se(r >= 0);

        printf("unique b: %s\n", ub);

        r = sd_bus_add_match(b, "interface='waldo.com',member='Piep'", NULL, NULL);
        assert_se(r >= 0);

        r = sd_bus_emit_signal(a, "/foo/bar/waldo", "waldo.com", "Piep", "sss", "I am a string", "/this/is/a/path", "and.this.a.domain.name");
        assert_se(r >= 0);

        r = sd_bus_process(b, &m);
        assert_se(r > 0);
        assert_se(m);

        bus_message_dump(m);
        assert_se(sd_bus_message_rewind(m, true) >= 0);

        r = sd_bus_message_read(m, "s", &the_string);
        assert_se(r >= 0);
        assert_se(streq(the_string, "I am a string"));

        sd_bus_message_unref(m);
        m = NULL;

        r = sd_bus_request_name(a, "net.x0pointer.foobar", 0);
        assert_se(r >= 0);

        r = sd_bus_message_new_method_call(b, "net.x0pointer.foobar", "/a/path", "an.inter.face", "AMethod", &m);
        assert_se(r >= 0);

        assert_se(pipe2(pipe_fds, O_CLOEXEC) >= 0);

        assert_se(write(pipe_fds[1], "x", 1) == 1);

        close_nointr_nofail(pipe_fds[1]);
        pipe_fds[1] = -1;

        r = sd_bus_message_append(m, "h", pipe_fds[0]);
        assert_se(r >= 0);

        close_nointr_nofail(pipe_fds[0]);
        pipe_fds[0] = -1;

        r = sd_bus_send(b, m, NULL);
        assert_se(r >= 0);

        for (;;) {
                sd_bus_message_unref(m);
                m = NULL;
                r = sd_bus_process(a, &m);
                assert_se(r > 0);
                assert_se(m);

                bus_message_dump(m);
                assert_se(sd_bus_message_rewind(m, true) >= 0);

                if (sd_bus_message_is_method_call(m, "an.inter.face", "AMethod")) {
                        int fd;
                        char x;

                        r = sd_bus_message_read(m, "h", &fd);
                        assert_se(r >= 0);

                        assert_se(read(fd, &x, 1) == 1);
                        assert_se(x == 'x');
                        break;
                }
        }

        r = sd_bus_release_name(a, "net.x0pointer.foobar");
        assert_se(r >= 0);

        r = sd_bus_release_name(a, "net.x0pointer.foobar");
        assert_se(r == -ESRCH);

        sd_bus_unref(a);
        sd_bus_unref(b);

        return 0;
}
コード例 #12
0
ファイル: test-bus-kernel.c プロジェクト: kwirk/systemd
int main(int argc, char *argv[]) {
        _cleanup_close_ int bus_ref = -1;
        _cleanup_free_ char *bus_name = NULL, *address = NULL;
        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
        const char *ua = NULL, *ub = NULL, *the_string = NULL;
        sd_bus *a, *b;
        int r, pipe_fds[2];

        bus_ref = bus_kernel_create("deine-mutter", &bus_name);
        if (bus_ref == -ENOENT)
                return EXIT_TEST_SKIP;

        assert_se(bus_ref >= 0);

        address = strappend("kernel:path=", bus_name);
        assert_se(address);

        r = sd_bus_new(&a);
        assert_se(r >= 0);

        r = sd_bus_new(&b);
        assert_se(r >= 0);

        r = sd_bus_set_address(a, address);
        assert_se(r >= 0);

        r = sd_bus_set_address(b, address);
        assert_se(r >= 0);

        r = sd_bus_start(a);
        assert_se(r >= 0);

        r = sd_bus_start(b);
        assert_se(r >= 0);

        r = sd_bus_get_unique_name(a, &ua);
        assert_se(r >= 0);

        printf("unique a: %s\n", ua);

        r = sd_bus_get_unique_name(b, &ub);
        assert_se(r >= 0);

        printf("unique b: %s\n", ub);

        {
                //FIXME:
                struct kdbus_cmd_match cmd_match;

                cmd_match.size = sizeof(cmd_match);
                cmd_match.src_id = KDBUS_MATCH_SRC_ID_ANY;

                r = ioctl(sd_bus_get_fd(a), KDBUS_CMD_MATCH_ADD, &cmd_match);
                assert_se(r >= 0);

                r = ioctl(sd_bus_get_fd(b), KDBUS_CMD_MATCH_ADD, &cmd_match);
                assert_se(r >= 0);
        }

        r = sd_bus_emit_signal(a, "/foo/bar/waldo", "waldo.com", "Piep", "sss", "I am a string", "/this/is/a/path", "and.this.a.domain.name");
        assert_se(r >= 0);

        r = sd_bus_process(b, &m);
        assert_se(r > 0);
        assert_se(m);

        bus_message_dump(m);
        assert_se(sd_bus_message_rewind(m, true) >= 0);

        r = sd_bus_message_read(m, "s", &the_string);
        assert_se(r >= 0);
        assert_se(streq(the_string, "I am a string"));

        sd_bus_message_unref(m);
        m = NULL;

        r = sd_bus_request_name(a, "net.x0pointer.foobar", 0);
        assert_se(r >= 0);

        r = sd_bus_message_new_method_call(b, "net.x0pointer.foobar", "/a/path", "an.inter.face", "AMethod", &m);
        assert_se(r >= 0);

        assert_se(pipe2(pipe_fds, O_CLOEXEC) >= 0);

        assert_se(write(pipe_fds[1], "x", 1) == 1);

        close_nointr_nofail(pipe_fds[1]);
        pipe_fds[1] = -1;

        r = sd_bus_message_append(m, "h", pipe_fds[0]);
        assert_se(r >= 0);

        close_nointr_nofail(pipe_fds[0]);
        pipe_fds[0] = -1;

        r = sd_bus_send(b, m, NULL);
        assert_se(r >= 0);

        for (;;) {
                sd_bus_message_unref(m);
                m = NULL;
                r = sd_bus_process(a, &m);
                assert_se(r > 0);
                assert_se(m);

                bus_message_dump(m);
                assert_se(sd_bus_message_rewind(m, true) >= 0);

                if (sd_bus_message_is_method_call(m, "an.inter.face", "AMethod")) {
                        int fd;
                        char x;

                        r = sd_bus_message_read(m, "h", &fd);
                        assert_se(r >= 0);

                        assert_se(read(fd, &x, 1) == 1);
                        assert_se(x == 'x');
                        break;
                }
        }

        r = sd_bus_release_name(a, "net.x0pointer.foobar");
        assert_se(r >= 0);

        r = sd_bus_release_name(a, "net.x0pointer.foobar");
        assert_se(r == -ESRCH);

        sd_bus_unref(a);
        sd_bus_unref(b);

        return 0;
}