mirror of
https://github.com/systemd/systemd.git
synced 2025-01-26 14:04:03 +03:00
bus: implicitly handle peer commands Ping() and GetMachineId()
This commit is contained in:
parent
2522023999
commit
b9bf7e2be9
@ -108,6 +108,7 @@ static inline void bus_unrefp(sd_bus **b) {
|
||||
}
|
||||
|
||||
#define _cleanup_bus_unref_ __attribute__((cleanup(bus_unrefp)))
|
||||
#define _cleanup_bus_error_free_ __attribute__((cleanup(sd_bus_error_free)))
|
||||
|
||||
#define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
|
||||
|
||||
|
@ -1475,6 +1475,57 @@ static int process_timeout(sd_bus *bus) {
|
||||
return r < 0 ? r : 1;
|
||||
}
|
||||
|
||||
static int process_builtin(sd_bus *bus, sd_bus_message *m) {
|
||||
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
||||
int r;
|
||||
|
||||
assert(bus);
|
||||
assert(m);
|
||||
|
||||
if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
|
||||
return 0;
|
||||
|
||||
if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
|
||||
return 0;
|
||||
|
||||
if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
|
||||
return 1;
|
||||
|
||||
if (streq_ptr(m->member, "Ping"))
|
||||
r = sd_bus_message_new_method_return(bus, m, &reply);
|
||||
else if (streq_ptr(m->member, "GetMachineId")) {
|
||||
sd_id128_t id;
|
||||
char sid[33];
|
||||
|
||||
r = sd_id128_get_machine(&id);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = sd_bus_message_new_method_return(bus, m, &reply);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
|
||||
} else {
|
||||
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_INIT;
|
||||
|
||||
sd_bus_error_set(&error,
|
||||
"org.freedesktop.DBus.Error.UnknownMethod",
|
||||
"Unknown method '%s' on interface '%s'.", m->member, m->interface);
|
||||
|
||||
r = sd_bus_message_new_method_error(bus, m, &error, &reply);
|
||||
}
|
||||
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = sd_bus_send(bus, reply, NULL);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int process_message(sd_bus *bus, sd_bus_message *m) {
|
||||
struct filter_callback *l;
|
||||
int r;
|
||||
@ -1482,7 +1533,7 @@ static int process_message(sd_bus *bus, sd_bus_message *m) {
|
||||
assert(bus);
|
||||
assert(m);
|
||||
|
||||
if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL || m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN) {
|
||||
if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN || m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_ERROR) {
|
||||
struct reply_callback *c;
|
||||
|
||||
c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
|
||||
@ -1504,7 +1555,7 @@ static int process_message(sd_bus *bus, sd_bus_message *m) {
|
||||
return r;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return process_builtin(bus, m);
|
||||
}
|
||||
|
||||
int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
|
||||
@ -1599,11 +1650,13 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (sd_bus_message_is_method_call(m, NULL, NULL)) {
|
||||
const sd_bus_error e = SD_BUS_ERROR_INIT_CONST("org.freedesktop.DBus.Error.UnknownObject", "Unknown object.");
|
||||
if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL) {
|
||||
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
||||
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_INIT;
|
||||
|
||||
r = sd_bus_message_new_method_error(bus, m, &e, &reply);
|
||||
sd_bus_error_set(&error, "org.freedesktop.DBus.Error.UnknownObject", "Unknown object '%s'.", m->path);
|
||||
|
||||
r = sd_bus_message_new_method_error(bus, m, &error, &reply);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
@ -36,8 +36,8 @@
|
||||
* - handle NULL strings nicer when appending
|
||||
* - merge busctl into systemctl or so?
|
||||
* - add object handlers
|
||||
* - add peer message handlers
|
||||
* - verify object paths
|
||||
* - implicitly add stub introspection calls
|
||||
*/
|
||||
|
||||
typedef struct sd_bus sd_bus;
|
||||
|
@ -264,8 +264,9 @@ static void* client2(void*p) {
|
||||
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
|
||||
sd_bus *bus = NULL;
|
||||
sd_bus_error error = SD_BUS_ERROR_INIT;
|
||||
int r;
|
||||
bool quit = false;
|
||||
const char *mid;
|
||||
int r;
|
||||
|
||||
r = sd_bus_open_user(&bus);
|
||||
if (r < 0) {
|
||||
@ -273,6 +274,35 @@ static void* client2(void*p) {
|
||||
goto finish;
|
||||
}
|
||||
|
||||
r = sd_bus_message_new_method_call(
|
||||
bus,
|
||||
"org.freedesktop.systemd.test",
|
||||
"/",
|
||||
"org.freedesktop.DBus.Peer",
|
||||
"GetMachineId",
|
||||
&m);
|
||||
if (r < 0) {
|
||||
log_error("Failed to allocate method call: %s", strerror(-r));
|
||||
goto finish;
|
||||
}
|
||||
|
||||
r = sd_bus_send_with_reply_and_block(bus, m, 0, &error, &reply);
|
||||
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) {
|
||||
log_error("Failed to parse machine ID: %s", strerror(-r));
|
||||
goto finish;
|
||||
}
|
||||
|
||||
log_info("Machine ID is %s.", mid);
|
||||
|
||||
sd_bus_message_unref(m);
|
||||
m = NULL;
|
||||
|
||||
r = sd_bus_message_new_method_call(
|
||||
bus,
|
||||
"org.freedesktop.systemd.test",
|
||||
@ -285,6 +315,9 @@ static void* client2(void*p) {
|
||||
goto finish;
|
||||
}
|
||||
|
||||
sd_bus_message_unref(reply);
|
||||
reply = NULL;
|
||||
|
||||
r = sd_bus_send_with_reply_and_block(bus, m, 200 * USEC_PER_MSEC, &error, &reply);
|
||||
if (r < 0)
|
||||
log_info("Failed to issue method call: %s", bus_error_message(&error, -r));
|
||||
|
Loading…
x
Reference in New Issue
Block a user