示例#1
0
void DbusApiInit(int sock)
{
	fd = sock;
	sd_event_source *busSource = NULL;
	sd_bus_slot *slot = NULL;

	int ret = sd_event_default(&event);
	char tmp = '0';
	read(fd, &tmp, sizeof(char));

	ret = sd_bus_open_system(&bus);

	ret = sd_bus_add_object_vtable(bus, &slot, "/org/watchdogd1",
				"org.watchdogd1", watchdogPmon, NULL);

	ret = sd_bus_request_name(bus, "org.watchdogd1", 0);

	if (ret < 0) {
		ReloadDbusDaemon();
		ret = sd_bus_request_name(bus, "org.watchdogd1", 0);
	}

	sd_event_add_io(event, &busSource, sd_bus_get_fd(bus), EPOLLIN, BusHandler, NULL);

	sd_event_loop(event);
}
示例#2
0
static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
    _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
    int r;

    assert(c);
    assert(event);
    assert(_bus);

    r = sd_bus_default_system(&bus);
    if (r < 0)
        return log_error_errno(r, "Failed to get system bus connection: %m");

    r = sd_bus_add_object_vtable(bus, NULL, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", hostname_vtable, c);
    if (r < 0)
        return log_error_errno(r, "Failed to register object: %m");

    r = sd_bus_request_name(bus, "org.freedesktop.hostname1", 0);
    if (r < 0)
        return log_error_errno(r, "Failed to register name: %m");

    r = sd_bus_attach_event(bus, event, 0);
    if (r < 0)
        return log_error_errno(r, "Failed to attach bus to event loop: %m");

    *_bus = bus;
    bus = NULL;

    return 0;
}
示例#3
0
/* create the service */
int afb_api_dbus_add_server(const char *path)
{
	int rc;
	struct api_dbus *api;

	/* get the dbus api object connected */
	api = make_api_dbus(path);
	if (api == NULL)
		goto error;

	/* request the service object name */
	rc = sd_bus_request_name(api->sdbus, api->name, 0);
	if (rc < 0) {
		errno = -rc;
		ERROR("can't register name %s", api->name);
		goto error2;
	}

	/* connect the service to the dbus object */
	rc = sd_bus_add_object(api->sdbus, &api->slot, api->path, api_dbus_server_on_object_called, api);
	if (rc < 0) {
		errno = -rc;
		ERROR("can't add dbus object %s for %s", api->path, api->name);
		goto error3;
	}
	INFO("afb service over dbus installed, name %s, path %s", api->name, api->path);

	ctxClientEventListenerAdd(NULL, (struct afb_event_listener){ .itf = &evitf, .closure = api });
//------------------------------------------------------
// Called by IPMID as part of the start up
// -----------------------------------------------------
int start_host_service(sd_bus *bus, sd_bus_slot *slot)
{
    int rc = 0;

    /* Install the object */
    rc = sd_bus_add_object_vtable(bus,
                                  &slot,
                                  "/org/openbmc/HostServices",  /* object path */
                                  "org.openbmc.HostServices",   /* interface name */
                                  host_services_vtable,
                                  NULL);
    if (rc < 0)
    {
        fprintf(stderr, "Failed to issue method call: %s\n", strerror(-rc));
    }
    else
    {
        /* Take one in OpenBmc */
        rc = sd_bus_request_name(bus, "org.openbmc.HostServices", 0);
        if (rc < 0)
        {
            fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-rc));
        }
    }

    return rc < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
示例#5
0
文件: dbus.c 项目: siiptuo/kirstu
int kirstu_dbus_init(void)
{
    int r = sd_bus_default_user(&bus);
    if (r < 0) {
        fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
        exit(EXIT_FAILURE);
    }

    r = sd_bus_add_object_vtable(bus,
        &slot,
        "/org/freedesktop/Notifications",
        "org.freedesktop.Notifications",
        vtable,
        NULL);
    if (r < 0) {
        fprintf(stderr, "Failed to issue method call: %s\n", strerror(-r));
        exit(EXIT_FAILURE);
    }

    r = sd_bus_request_name(bus, "org.freedesktop.Notifications", 0);
    if (r < 0) {
        fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-r));
        exit(EXIT_FAILURE);
    }

    return sd_bus_get_fd(bus);
}
示例#6
0
/*
 * This is the main loop of the d-bus service.  It won't exit until
 * quit_dbus_main_loop() is called.
 *
 * It is should be invoked as the startup function of a thread or the caller
 * should not expect it to return.
 */
void * multipath_main_loop(void * ap) {
	sd_bus_slot *slot = NULL;
	sd_bus *bus = NULL;
	int r;

	/* Connect to the user bus this time */
	r = sd_bus_open_user(&bus);
	if (r < 0) {
		fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
		goto finish;
	}


	sync_maps(bus, slot);

	/* Take a well-known service name so that clients can find us */
	r = sd_bus_request_name(bus, MULTIPATH_BASE_INTERFACE, 0);

	if (r < 0) {
		fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-r));
		goto finish;
	}

	for (;;) {
		/* Process requests */
		r = sd_bus_process(bus, NULL);
		if (r < 0) {
			fprintf(stderr, "Failed to process bus: %s\n", strerror(-r));
			goto finish;
		}
		if (r > 0) /* we processed a request, try to process another one, right-away */
			continue;

		/* Wait for the next request to process */
		r = sd_bus_wait(bus, (uint64_t) - 1);
		if (r < 0) {
			fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r));
			goto finish;
		}
	}

	finish: sd_bus_slot_unref(slot);
	sd_bus_unref(bus);

	return NULL;
}
示例#7
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;
}
示例#8
0
static int manager_connect_bus(Manager *m) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        int r;

        assert(m);
        assert(!m->bus);

        r = sd_bus_default_system(&m->bus);
        if (r < 0)
                return log_error_errno(r, "Failed to connect to system bus: %m");

        r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable, m);
        if (r < 0)
                return log_error_errno(r, "Failed to add manager object vtable: %m");

        r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/machine1/machine", "org.freedesktop.machine1.Machine", machine_vtable, machine_object_find, m);
        if (r < 0)
                return log_error_errno(r, "Failed to add machine object vtable: %m");

        r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/machine1/machine", machine_node_enumerator, m);
        if (r < 0)
                return log_error_errno(r, "Failed to add machine enumerator: %m");

        r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/machine1/image", "org.freedesktop.machine1.Image", image_vtable, image_object_find, m);
        if (r < 0)
                return log_error_errno(r, "Failed to add image object vtable: %m");

        r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/machine1/image", image_node_enumerator, m);
        if (r < 0)
                return log_error_errno(r, "Failed to add image enumerator: %m");

        r = sd_bus_add_match(m->bus,
                             NULL,
                             "type='signal',"
                             "sender='org.freedesktop.systemd1',"
                             "interface='org.freedesktop.systemd1.Manager',"
                             "member='JobRemoved',"
                             "path='/org/freedesktop/systemd1'",
                             match_job_removed,
                             m);
        if (r < 0)
                return log_error_errno(r, "Failed to add match for JobRemoved: %m");

        r = sd_bus_add_match(m->bus,
                             NULL,
                             "type='signal',"
                             "sender='org.freedesktop.systemd1',"
                             "interface='org.freedesktop.systemd1.Manager',"
                             "member='UnitRemoved',"
                             "path='/org/freedesktop/systemd1'",
                             match_unit_removed,
                             m);
        if (r < 0)
                return log_error_errno(r, "Failed to add match for UnitRemoved: %m");

        r = sd_bus_add_match(m->bus,
                             NULL,
                             "type='signal',"
                             "sender='org.freedesktop.systemd1',"
                             "interface='org.freedesktop.DBus.Properties',"
                             "member='PropertiesChanged',"
                             "arg0='org.freedesktop.systemd1.Unit'",
                             match_properties_changed,
                             m);
        if (r < 0)
                return log_error_errno(r, "Failed to add match for PropertiesChanged: %m");

        r = sd_bus_add_match(m->bus,
                             NULL,
                             "type='signal',"
                             "sender='org.freedesktop.systemd1',"
                             "interface='org.freedesktop.systemd1.Manager',"
                             "member='Reloading',"
                             "path='/org/freedesktop/systemd1'",
                             match_reloading,
                             m);
        if (r < 0)
                return log_error_errno(r, "Failed to add match for Reloading: %m");

        r = sd_bus_call_method(
                        m->bus,
                        "org.freedesktop.systemd1",
                        "/org/freedesktop/systemd1",
                        "org.freedesktop.systemd1.Manager",
                        "Subscribe",
                        &error,
                        NULL, NULL);
        if (r < 0) {
                log_error("Failed to enable subscription: %s", bus_error_message(&error, r));
                return r;
        }

        r = sd_bus_request_name(m->bus, "org.freedesktop.machine1", 0);
        if (r < 0)
                return log_error_errno(r, "Failed to register name: %m");

        r = sd_bus_attach_event(m->bus, m->event, 0);
        if (r < 0)
                return log_error_errno(r, "Failed to attach bus to event loop: %m");

        return 0;
}
示例#9
0
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;
}
int main(int argc, char *argv[]) {
	sd_bus_slot *slot = NULL;
	sd_bus *bus = NULL;
	int r;
	char **acquired = NULL, **activatable = NULL, **i;

	/* Connect to the user bus this time */
	r = sd_bus_open_system(&bus);
	if (r < 0) {
		fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
		goto finish;
	}

	/* Install an object */
	r = sd_bus_add_object_vtable(bus,
			&slot,
			"/org/openbmc/examples/path0/SDBusObj",  /* object path */
			"org.openbmc.examples.Echo",   /* interface name */
			echo_vtable,
			NULL);
	if (r < 0) {
		fprintf(stderr, "Failed to issue method call: %s\n", strerror(-r));
		goto finish;
	}

	/* Install an object */
	r = sd_bus_add_object_vtable(bus,
			&slot,
			"/org/openbmc/examples/path1/SDBusObj",  /* object path */
			"org.openbmc.examples.Echo",   /* interface name */
			echo_vtable,
			NULL);
	if (r < 0) {
		fprintf(stderr, "Failed to issue method call: %s\n", strerror(-r));
		goto finish;
	}

	/* Take a well-known service name so that clients can find us */
	r = sd_bus_request_name(bus, "org.openbmc.examples.SDBusService", 0);
	if (r < 0) {
		fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-r));
		goto finish;
	}

	for (;;) {
		/* Process requests */
		r = sd_bus_process(bus, NULL);
		if (r < 0) {
			fprintf(stderr, "Failed to process bus: %s\n", strerror(-r));
			goto finish;
		}
		if (r > 0) /* we processed a request, try to process another one, right-away */
			continue;

		/* Wait for the next request to process */
		r = sd_bus_wait(bus, (uint64_t) -1);
		if (r < 0) {
			fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r));
			goto finish;
		}
	}

finish:
	sd_bus_slot_unref(slot);
	sd_bus_unref(bus);

	return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
示例#11
0
/*
 * ------------------------------------------------
 * Called as part of setting up skeleton services.
 * -----------------------------------------------
 */
int start_led_services()
{
    /* Generic error reporter. */
    int rc = -1;
    int num_leds = 0;
    int count_leds = 0;

    /* Bus and slot where we are offering the LED dbus service. */
    sd_bus *bus_type = NULL;
    sd_bus_slot *led_slot = NULL;

    /* For walking '/sys/class/leds/' looking for names of LED.*/
    struct dirent **led_list;

    /* Get a hook onto system bus. */
    rc = sd_bus_open_system(&bus_type);
    if(rc < 0)
    {
        fprintf(stderr,"Error opening system bus.\n");
        return rc;
    }

    count_leds = num_leds = scandir("/sys/class/leds/", 
                                    &led_list, led_select, alphasort);
    if(num_leds <= 0)
    {
        fprintf(stderr,"No LEDs present in the system\n");

        sd_bus_slot_unref(led_slot);
        sd_bus_unref(bus_type);
        return rc;
    }

    /* Fully qualified Dbus object for a particular LED */
    char led_object[128] = {0};
    int len = 0;

    /* For each led present, announce the service on dbus. */
    while(num_leds--)
    {
        memset(led_object, 0x0, sizeof(led_object));

        len = snprintf(led_object, sizeof(led_object), "%s%s",
                "/org/openbmc/controller/led/", led_list[num_leds]->d_name);

        if(len >= sizeof(led_object))
        {
            fprintf(stderr, "Error. LED object is too long:[%d]\n",len);
            rc = -1;
            break;
        }

        /* Install the object */
        rc = sd_bus_add_object_vtable(bus_type,
                                      &led_slot,
                                      led_object,                     /* object path */
                                      "org.openbmc.controller.led",   /* interface name */
                                      led_control_vtable,
                                      NULL);

        if (rc < 0)
        {
            fprintf(stderr, "Failed to add object to dbus: %s\n", strerror(-rc));
            break;
        }
    }

    /* Done with all registration. */
    while (count_leds > 0)
    {
        free(led_list[--count_leds]);
        if(count_leds == 0)
        {
            free(led_list);
        }
    }

    /* If we had success in adding the providers, request for a bus name. */
    if(rc == 0)
    {
        /* Take one in OpenBmc */
        rc = sd_bus_request_name(bus_type, "org.openbmc.controller.led", 0);
        if (rc < 0)
        {
            fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-rc));
        }
        else
        {
            for (;;)
            {
                /* Process requests */
                rc = sd_bus_process(bus_type, NULL);
                if (rc < 0)
                {
                    fprintf(stderr, "Failed to process bus: %s\n", strerror(-rc));
                    break;
                }
                if (rc > 0)
                {
                    continue;
                }

                rc = sd_bus_wait(bus_type, (uint64_t) - 1);
                if (rc < 0)
                {
                    fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-rc));
                    break;
                }
            }
        }
    }
    sd_bus_slot_unref(led_slot);
    sd_bus_unref(bus_type);

    return rc;
}
示例#12
0
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;
}
示例#13
0
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;
}
示例#14
0
int main(int argc, char *argv[]) {
    sd_bus_slot *slot = NULL;
    int r;
    char *mode = NULL;

    if (argc != 2) {
        fprintf(stderr, "syntax: %s [server|client]\n", argv[0]);
        return 1;
    }

    mode = argv[1];

    /* Connect to system bus */
    r = sd_bus_open_system(&bus);
    if (r < 0) {
        fprintf(stderr, "Failed to connect to system bus: %s\n",
                strerror(-r));
        goto finish;
    }

    if (!strcmp("server", mode)) {
        r = sd_bus_add_object_vtable(bus,
                &slot,
                OBJ, /* object path */
                INT, /* interface name */
                signal_vtable,
                NULL);
        if (r < 0) {
            fprintf(stderr, "Failed to issue method call: %s\n",
                    strerror(-r));
            goto finish;
        }

        /* Take a well-known service name so that clients can find us */
        r = sd_bus_request_name(bus, INT, 0);
        if (r < 0) {
            fprintf(stderr, "Failed to acquire service name: %s\n",
                    strerror(-r));
            goto finish;
        }

    } else if (!strcmp("client", mode)) {
        r = sd_bus_add_match(bus, &slot, FILTER, bus_signal_cb, NULL);
        if (r < 0) {
            fprintf(stderr, "Failed: sd_bus_add_match: %s\n", strerror(-r));
            goto finish;
        }
    } else {
        fprintf(stderr, "Invalid operating mode %s", mode);
        return 1;
    }

    for (;;) {
        /* Process requests */
        r = sd_bus_process(bus, NULL);
        if (r < 0) {
            fprintf(stderr, "Failed to process bus: %s\n", strerror(-r));
            goto finish;
        }
        if (r > 0) {
            continue;
        }

        r = sd_bus_wait(bus, (uint64_t) - 1);
        if (r < 0) {
            fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r));
            goto finish;
        }
    }

finish:
    sd_bus_slot_unref(slot);
    sd_bus_unref(bus);
    return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
示例#15
0
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;
}