2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2013-03-20 22:56:25 +01:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2013 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <fcntl.h>
|
2013-03-20 22:56:25 +01:00
|
|
|
#include <pthread.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <stdlib.h>
|
2013-03-21 22:50:25 +01:00
|
|
|
#include <unistd.h>
|
2013-03-20 22:56:25 +01:00
|
|
|
|
|
|
|
#include "sd-bus.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2013-03-21 22:50:25 +01:00
|
|
|
#include "bus-error.h"
|
2013-03-31 16:16:37 +02:00
|
|
|
#include "bus-internal.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "bus-match.h"
|
2015-08-27 16:32:22 +02:00
|
|
|
#include "bus-util.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "fd-util.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "macro.h"
|
|
|
|
#include "util.h"
|
2013-03-31 16:16:37 +02:00
|
|
|
|
2015-04-29 18:35:10 +02:00
|
|
|
static int match_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
|
2013-03-31 16:16:37 +02:00
|
|
|
log_info("Match triggered! interface=%s member=%s", strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member(m)));
|
|
|
|
return 0;
|
|
|
|
}
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2015-04-29 18:35:10 +02:00
|
|
|
static int object_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
|
2013-03-22 05:58:47 +01:00
|
|
|
int r;
|
|
|
|
|
2013-05-16 21:14:56 +02:00
|
|
|
if (sd_bus_message_is_method_error(m, NULL))
|
2013-03-22 05:58:47 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (sd_bus_message_is_method_call(m, "org.object.test", "Foobar")) {
|
|
|
|
log_info("Invoked Foobar() on %s", sd_bus_message_get_path(m));
|
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
r = sd_bus_reply_method_return(m, NULL);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to send reply: %m");
|
2013-03-22 05:58:47 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-20 22:56:25 +01:00
|
|
|
static int server_init(sd_bus **_bus) {
|
|
|
|
sd_bus *bus = NULL;
|
2013-03-22 01:49:56 +01:00
|
|
|
sd_id128_t id;
|
2013-03-20 22:56:25 +01:00
|
|
|
int r;
|
2018-03-29 16:14:11 +02:00
|
|
|
const char *unique, *desc;
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2015-01-22 22:53:42 +01:00
|
|
|
assert_se(_bus);
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2018-03-29 16:14:11 +02:00
|
|
|
r = sd_bus_open_user_with_description(&bus, "my bus!");
|
2013-03-20 22:56:25 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to connect to user bus: %m");
|
2013-03-20 22:56:25 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-11-28 16:38:47 +01:00
|
|
|
r = sd_bus_get_bus_id(bus, &id);
|
2013-03-22 01:49:56 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to get server ID: %m");
|
2013-03-22 01:49:56 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-03-22 03:34:29 +01:00
|
|
|
r = sd_bus_get_unique_name(bus, &unique);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to get unique name: %m");
|
2013-03-22 03:34:29 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-03-29 16:14:11 +02:00
|
|
|
r = sd_bus_get_description(bus, &desc);
|
|
|
|
assert_se(streq(desc, "my bus!"));
|
|
|
|
|
2013-03-22 01:49:56 +01:00
|
|
|
log_info("Peer ID is " SD_ID128_FORMAT_STR ".", SD_ID128_FORMAT_VAL(id));
|
2013-03-22 03:34:29 +01:00
|
|
|
log_info("Unique ID: %s", unique);
|
2013-03-22 01:49:56 +01:00
|
|
|
log_info("Can send file handles: %i", sd_bus_can_send(bus, 'h'));
|
|
|
|
|
2013-03-20 22:56:25 +01:00
|
|
|
r = sd_bus_request_name(bus, "org.freedesktop.systemd.test", 0);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to acquire name: %m");
|
2013-03-20 22:56:25 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
r = sd_bus_add_fallback(bus, NULL, "/foo/bar", object_callback, NULL);
|
2013-03-22 05:58:47 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to add object: %m");
|
2013-03-22 05:58:47 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-12-19 12:29:04 +01:00
|
|
|
r = sd_bus_match_signal(bus, NULL, NULL, NULL, "foo.bar", "Notify", match_callback, NULL);
|
2013-03-31 16:16:37 +02:00
|
|
|
if (r < 0) {
|
2017-12-19 12:29:04 +01:00
|
|
|
log_error_errno(r, "Failed to request match: %m");
|
2013-03-31 16:16:37 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
r = sd_bus_add_match(bus, NULL, "type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged'", match_callback, NULL);
|
2013-03-31 16:16:37 +02:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to add match: %m");
|
2013-03-31 16:16:37 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
bus_match_dump(&bus->match_callbacks, 0);
|
|
|
|
|
2013-03-20 22:56:25 +01:00
|
|
|
*_bus = bus;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2015-08-17 10:45:30 +02:00
|
|
|
sd_bus_unref(bus);
|
2013-03-20 22:56:25 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-03-21 22:50:25 +01:00
|
|
|
static int server(sd_bus *bus) {
|
2013-03-20 22:56:25 +01:00
|
|
|
int r;
|
2013-03-21 22:50:25 +01:00
|
|
|
bool client1_gone = false, client2_gone = false;
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2013-03-21 22:50:25 +01:00
|
|
|
while (!client1_gone || !client2_gone) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
|
2013-03-22 03:36:58 +01:00
|
|
|
pid_t pid = 0;
|
2013-04-15 01:50:46 +02:00
|
|
|
const char *label = NULL;
|
2013-03-20 22:56:25 +01:00
|
|
|
|
|
|
|
r = sd_bus_process(bus, &m);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to process requests: %m");
|
2013-03-20 22:56:25 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2013-03-21 22:50:25 +01:00
|
|
|
|
2013-03-20 22:56:25 +01:00
|
|
|
if (r == 0) {
|
|
|
|
r = sd_bus_wait(bus, (uint64_t) -1);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to wait: %m");
|
2013-03-20 22:56:25 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-03-21 22:50:25 +01:00
|
|
|
if (!m)
|
|
|
|
continue;
|
|
|
|
|
2013-11-28 17:50:02 +01:00
|
|
|
sd_bus_creds_get_pid(sd_bus_message_get_creds(m), &pid);
|
|
|
|
sd_bus_creds_get_selinux_context(sd_bus_message_get_creds(m), &label);
|
2014-04-25 13:45:15 +02:00
|
|
|
log_info("Got message! member=%s pid="PID_FMT" label=%s",
|
2013-04-15 01:50:46 +02:00
|
|
|
strna(sd_bus_message_get_member(m)),
|
2014-04-25 13:45:15 +02:00
|
|
|
pid,
|
2013-04-15 01:50:46 +02:00
|
|
|
strna(label));
|
2013-03-20 22:56:25 +01:00
|
|
|
/* bus_message_dump(m); */
|
|
|
|
/* sd_bus_message_rewind(m, true); */
|
|
|
|
|
|
|
|
if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "LowerCase")) {
|
|
|
|
const char *hello;
|
|
|
|
_cleanup_free_ char *lowercase = NULL;
|
|
|
|
|
|
|
|
r = sd_bus_message_read(m, "s", &hello);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to get parameter: %m");
|
2013-03-20 22:56:25 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
lowercase = strdup(hello);
|
|
|
|
if (!lowercase) {
|
|
|
|
r = log_oom();
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ascii_strlower(lowercase);
|
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
r = sd_bus_reply_method_return(m, "s", lowercase);
|
2013-03-20 22:56:25 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to send reply: %m");
|
2013-03-20 22:56:25 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2013-03-21 22:50:25 +01:00
|
|
|
} else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "ExitClient1")) {
|
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
r = sd_bus_reply_method_return(m, NULL);
|
2013-03-21 22:50:25 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to send reply: %m");
|
2013-03-21 22:50:25 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
client1_gone = true;
|
|
|
|
} else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "ExitClient2")) {
|
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
r = sd_bus_reply_method_return(m, NULL);
|
2013-03-21 22:50:25 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to send reply: %m");
|
2013-03-21 22:50:25 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
client2_gone = true;
|
|
|
|
} else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Slow")) {
|
|
|
|
|
2013-04-05 14:55:28 +02:00
|
|
|
sleep(1);
|
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
r = sd_bus_reply_method_return(m, NULL);
|
2013-03-21 22:50:25 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to send reply: %m");
|
2013-03-21 22:50:25 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-03-24 22:02:05 +01:00
|
|
|
} else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "FileDescriptor")) {
|
|
|
|
int fd;
|
|
|
|
static const char x = 'X';
|
|
|
|
|
|
|
|
r = sd_bus_message_read(m, "h", &fd);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to get parameter: %m");
|
2013-03-24 22:02:05 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-11-06 23:24:16 +01:00
|
|
|
log_info("Received fd=%d", fd);
|
|
|
|
|
2013-03-24 22:02:05 +01:00
|
|
|
if (write(fd, &x, 1) < 0) {
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "Failed to write to fd: %m");
|
2014-03-18 19:22:43 +01:00
|
|
|
safe_close(fd);
|
2013-03-24 22:02:05 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
r = sd_bus_reply_method_return(m, NULL);
|
2013-03-24 22:02:05 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to send reply: %m");
|
2013-03-24 22:02:05 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-03-21 22:50:25 +01:00
|
|
|
} else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2013-04-05 14:49:45 +02:00
|
|
|
r = sd_bus_reply_method_error(
|
2013-11-21 01:51:16 +01:00
|
|
|
m,
|
2013-11-21 02:11:06 +01:00
|
|
|
&SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method."));
|
2013-03-20 22:56:25 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to send reply: %m");
|
2013-03-20 22:56:25 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
|
|
|
|
fail:
|
2013-03-21 22:50:25 +01:00
|
|
|
if (bus) {
|
|
|
|
sd_bus_flush(bus);
|
2013-03-20 22:56:25 +01:00
|
|
|
sd_bus_unref(bus);
|
2013-03-21 22:50:25 +01:00
|
|
|
}
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2013-03-21 22:50:25 +01:00
|
|
|
return r;
|
2013-03-20 22:56:25 +01:00
|
|
|
}
|
|
|
|
|
2013-03-21 22:50:25 +01:00
|
|
|
static void* client1(void*p) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
|
|
|
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
2013-03-20 22:56:25 +01:00
|
|
|
const char *hello;
|
|
|
|
int r;
|
2015-05-14 22:41:19 +02:00
|
|
|
_cleanup_close_pair_ int pp[2] = { -1, -1 };
|
2013-03-24 22:02:05 +01:00
|
|
|
char x;
|
2013-03-20 22:56:25 +01:00
|
|
|
|
|
|
|
r = sd_bus_open_user(&bus);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to connect to user bus: %m");
|
2013-03-20 22:56:25 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2013-04-05 14:49:45 +02:00
|
|
|
r = sd_bus_call_method(
|
2013-03-20 22:56:25 +01:00
|
|
|
bus,
|
|
|
|
"org.freedesktop.systemd.test",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.systemd.test",
|
|
|
|
"LowerCase",
|
2013-04-05 14:49:45 +02:00
|
|
|
&error,
|
|
|
|
&reply,
|
|
|
|
"s",
|
|
|
|
"HELLO");
|
2013-03-20 22:56:25 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to issue method call: %m");
|
2013-03-20 22:56:25 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_bus_message_read(reply, "s", &hello);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to get string: %m");
|
2013-03-20 22:56:25 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2015-01-22 22:53:42 +01:00
|
|
|
assert_se(streq(hello, "hello"));
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2013-03-24 22:02:05 +01:00
|
|
|
if (pipe2(pp, O_CLOEXEC|O_NONBLOCK) < 0) {
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "Failed to allocate pipe: %m");
|
2013-03-24 22:02:05 +01:00
|
|
|
r = -errno;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2013-11-06 23:24:16 +01:00
|
|
|
log_info("Sending fd=%d", pp[1]);
|
|
|
|
|
2013-04-05 14:55:28 +02:00
|
|
|
r = sd_bus_call_method(
|
2013-03-24 22:02:05 +01:00
|
|
|
bus,
|
|
|
|
"org.freedesktop.systemd.test",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.systemd.test",
|
|
|
|
"FileDescriptor",
|
2013-04-05 14:55:28 +02:00
|
|
|
&error,
|
|
|
|
NULL,
|
|
|
|
"h",
|
|
|
|
pp[1]);
|
2013-03-24 22:02:05 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to issue method call: %m");
|
2013-03-24 22:02:05 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if (read(pp[0], &x, 1) <= 0) {
|
|
|
|
log_error("Failed to read from pipe: %s", errno ? strerror(errno) : "early read");
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2013-03-20 22:56:25 +01:00
|
|
|
r = 0;
|
|
|
|
|
|
|
|
finish:
|
|
|
|
if (bus) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *q;
|
2013-03-20 22:56:25 +01:00
|
|
|
|
|
|
|
r = sd_bus_message_new_method_call(
|
|
|
|
bus,
|
2014-02-19 23:54:58 +01:00
|
|
|
&q,
|
2013-03-20 22:56:25 +01:00
|
|
|
"org.freedesktop.systemd.test",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.systemd.test",
|
2014-02-19 23:54:58 +01:00
|
|
|
"ExitClient1");
|
2013-04-05 14:55:28 +02:00
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to allocate method call: %m");
|
2013-04-05 14:55:28 +02:00
|
|
|
else
|
|
|
|
sd_bus_send(bus, q, NULL);
|
2013-03-20 22:56:25 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-21 22:50:25 +01:00
|
|
|
return INT_TO_PTR(r);
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:35:10 +02:00
|
|
|
static int quit_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
|
2013-03-21 22:50:25 +01:00
|
|
|
bool *x = userdata;
|
|
|
|
|
2016-09-12 20:55:34 +02:00
|
|
|
log_error_errno(sd_bus_message_get_errno(m), "Quit callback: %m");
|
2013-03-21 22:50:25 +01:00
|
|
|
|
|
|
|
*x = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* client2(void*p) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
|
|
|
|
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
2013-03-21 22:50:25 +01:00
|
|
|
bool quit = false;
|
2013-03-22 00:08:58 +01:00
|
|
|
const char *mid;
|
|
|
|
int r;
|
2013-03-21 22:50:25 +01:00
|
|
|
|
|
|
|
r = sd_bus_open_user(&bus);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to connect to user bus: %m");
|
2013-03-21 22:50:25 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2013-03-22 05:58:47 +01:00
|
|
|
r = sd_bus_message_new_method_call(
|
|
|
|
bus,
|
2014-02-19 23:54:58 +01:00
|
|
|
&m,
|
2013-03-22 05:58:47 +01:00
|
|
|
"org.freedesktop.systemd.test",
|
|
|
|
"/foo/bar/waldo/piep",
|
|
|
|
"org.object.test",
|
2014-02-19 23:54:58 +01:00
|
|
|
"Foobar");
|
2013-03-22 05:58:47 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to allocate method call: %m");
|
2013-03-22 05:58:47 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_bus_send(bus, m, NULL);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2015-05-14 22:41:19 +02:00
|
|
|
m = sd_bus_message_unref(m);
|
2013-03-22 05:58:47 +01:00
|
|
|
|
2013-03-31 16:16:37 +02:00
|
|
|
r = sd_bus_message_new_signal(
|
|
|
|
bus,
|
2014-02-19 23:54:58 +01:00
|
|
|
&m,
|
2013-03-31 16:16:37 +02:00
|
|
|
"/foobar",
|
|
|
|
"foo.bar",
|
2014-02-19 23:54:58 +01:00
|
|
|
"Notify");
|
2013-03-31 16:16:37 +02:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to allocate signal: %m");
|
2013-03-31 16:16:37 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_bus_send(bus, m, NULL);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to issue signal: %s", bus_error_message(&error, -r));
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2015-05-14 22:41:19 +02:00
|
|
|
m = sd_bus_message_unref(m);
|
2013-03-31 16:16:37 +02:00
|
|
|
|
2013-03-22 00:08:58 +01:00
|
|
|
r = sd_bus_message_new_method_call(
|
|
|
|
bus,
|
2014-02-19 23:54:58 +01:00
|
|
|
&m,
|
2013-03-22 00:08:58 +01:00
|
|
|
"org.freedesktop.systemd.test",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.DBus.Peer",
|
2014-02-19 23:54:58 +01:00
|
|
|
"GetMachineId");
|
2013-03-22 00:08:58 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to allocate method call: %m");
|
2013-03-22 00:08:58 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2013-11-11 18:55:34 +01:00
|
|
|
r = sd_bus_call(bus, m, 0, &error, &reply);
|
2013-03-22 00:08:58 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_bus_message_read(reply, "s", &mid);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to parse machine ID: %m");
|
2013-03-22 00:08:58 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_info("Machine ID is %s.", mid);
|
|
|
|
|
2015-05-14 22:41:19 +02:00
|
|
|
m = sd_bus_message_unref(m);
|
2013-03-22 00:08:58 +01:00
|
|
|
|
2013-03-21 22:50:25 +01:00
|
|
|
r = sd_bus_message_new_method_call(
|
|
|
|
bus,
|
2014-02-19 23:54:58 +01:00
|
|
|
&m,
|
2013-03-21 22:50:25 +01:00
|
|
|
"org.freedesktop.systemd.test",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.systemd.test",
|
2014-02-19 23:54:58 +01:00
|
|
|
"Slow");
|
2013-03-21 22:50:25 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to allocate method call: %m");
|
2013-03-21 22:50:25 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2015-05-14 22:41:19 +02:00
|
|
|
reply = sd_bus_message_unref(reply);
|
2013-03-22 00:08:58 +01:00
|
|
|
|
2013-11-11 18:55:34 +01:00
|
|
|
r = sd_bus_call(bus, m, 200 * USEC_PER_MSEC, &error, &reply);
|
2013-03-21 22:50:25 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_info("Failed to issue method call: %s", bus_error_message(&error, -r));
|
|
|
|
else
|
|
|
|
log_info("Slow call succeed.");
|
|
|
|
|
2015-05-14 22:41:19 +02:00
|
|
|
m = sd_bus_message_unref(m);
|
2013-03-21 22:50:25 +01:00
|
|
|
|
|
|
|
r = sd_bus_message_new_method_call(
|
|
|
|
bus,
|
2014-02-19 23:54:58 +01:00
|
|
|
&m,
|
2013-03-21 22:50:25 +01:00
|
|
|
"org.freedesktop.systemd.test",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.systemd.test",
|
2014-02-19 23:54:58 +01:00
|
|
|
"Slow");
|
2013-03-21 22:50:25 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to allocate method call: %m");
|
2013-03-21 22:50:25 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
r = sd_bus_call_async(bus, NULL, m, quit_callback, &quit, 200 * USEC_PER_MSEC);
|
2013-03-21 22:50:25 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_info("Failed to issue method call: %s", bus_error_message(&error, -r));
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!quit) {
|
|
|
|
r = sd_bus_process(bus, NULL);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to process requests: %m");
|
2013-03-21 22:50:25 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
if (r == 0) {
|
|
|
|
r = sd_bus_wait(bus, (uint64_t) -1);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to wait: %m");
|
2013-03-21 22:50:25 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
|
|
|
|
finish:
|
|
|
|
if (bus) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *q;
|
2013-03-21 22:50:25 +01:00
|
|
|
|
|
|
|
r = sd_bus_message_new_method_call(
|
|
|
|
bus,
|
2014-02-19 23:54:58 +01:00
|
|
|
&q,
|
2013-03-21 22:50:25 +01:00
|
|
|
"org.freedesktop.systemd.test",
|
|
|
|
"/",
|
|
|
|
"org.freedesktop.systemd.test",
|
2014-02-19 23:54:58 +01:00
|
|
|
"ExitClient2");
|
2013-03-21 22:50:25 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to allocate method call: %m");
|
2013-03-21 22:50:25 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2015-05-14 22:41:19 +02:00
|
|
|
(void) sd_bus_send(bus, q, NULL);
|
2013-03-21 22:50:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return INT_TO_PTR(r);
|
2013-03-20 22:56:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2013-03-21 22:50:25 +01:00
|
|
|
pthread_t c1, c2;
|
2013-03-20 22:56:25 +01:00
|
|
|
sd_bus *bus;
|
|
|
|
void *p;
|
|
|
|
int q, r;
|
|
|
|
|
|
|
|
r = server_init(&bus);
|
2013-03-27 02:07:46 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_info("Failed to connect to bus, skipping tests.");
|
|
|
|
return EXIT_TEST_SKIP;
|
|
|
|
}
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2013-03-21 22:50:25 +01:00
|
|
|
log_info("Initialized...");
|
|
|
|
|
|
|
|
r = pthread_create(&c1, NULL, client1, bus);
|
|
|
|
if (r != 0)
|
2013-03-20 22:56:25 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2013-03-21 22:50:25 +01:00
|
|
|
r = pthread_create(&c2, NULL, client2, bus);
|
|
|
|
if (r != 0)
|
|
|
|
return EXIT_FAILURE;
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2013-03-21 22:50:25 +01:00
|
|
|
r = server(bus);
|
|
|
|
|
|
|
|
q = pthread_join(c1, &p);
|
|
|
|
if (q != 0)
|
|
|
|
return EXIT_FAILURE;
|
2013-03-30 15:21:06 +01:00
|
|
|
if (PTR_TO_INT(p) < 0)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2013-03-21 22:50:25 +01:00
|
|
|
q = pthread_join(c2, &p);
|
2013-03-20 22:56:25 +01:00
|
|
|
if (q != 0)
|
|
|
|
return EXIT_FAILURE;
|
2013-03-30 15:21:06 +01:00
|
|
|
if (PTR_TO_INT(p) < 0)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2013-03-20 22:56:25 +01:00
|
|
|
if (r < 0)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|