Skip to content

Commit

Permalink
Merge pull request #27757 from dtardon/bus-locator
Browse files Browse the repository at this point in the history
Convert more DBus calls to BusLocator
  • Loading branch information
yuwata authored May 24, 2023
2 parents fe830b8 + 3ac26d0 commit 8ddc5f7
Show file tree
Hide file tree
Showing 21 changed files with 58 additions and 174 deletions.
11 changes: 2 additions & 9 deletions src/home/homed-home.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "blockdev-util.h"
#include "btrfs-util.h"
#include "bus-common-errors.h"
#include "bus-locator.h"
#include "data-fd-util.h"
#include "env-util.h"
#include "errno-list.h"
Expand Down Expand Up @@ -2117,15 +2118,7 @@ int home_killall(Home *h) {
if (asprintf(&unit, "user-" UID_FMT ".slice", h->uid) < 0)
return log_oom();

r = sd_bus_call_method(
h->manager->bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"KillUnit",
&error,
NULL,
"ssi", unit, "all", SIGKILL);
r = bus_call_method(h->manager->bus, bus_systemd_mgr, "KillUnit", &error, NULL, "ssi", unit, "all", SIGKILL);
if (r < 0)
log_full_errno(sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ? LOG_DEBUG : LOG_WARNING,
r, "Failed to kill login processes of user, ignoring: %s", bus_error_message(&error, r));
Expand Down
28 changes: 4 additions & 24 deletions src/hostname/hostnamectl.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "build.h"
#include "bus-common-errors.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-map-properties.h"
#include "format-table.h"
#include "hostname-setup.h"
Expand Down Expand Up @@ -298,13 +299,7 @@ static int get_one_name(sd_bus *bus, const char* attr, char **ret) {

/* This obtains one string property, and copy it if 'ret' is set, or print it otherwise. */

r = sd_bus_get_property(
bus,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
attr,
&error, &reply, "s");
r = bus_get_property(bus, bus_hostname, attr, &error, &reply, "s");
if (r < 0)
return log_error_errno(r, "Could not get property: %s", bus_error_message(&error, r));

Expand Down Expand Up @@ -408,15 +403,7 @@ static int show_status(int argc, char **argv, void *userdata) {
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
const char *text = NULL;

r = sd_bus_call_method(
bus,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
"Describe",
&error,
&reply,
NULL);
r = bus_call_method(bus, bus_hostname, "Describe", &error, &reply, NULL);
if (r < 0)
return log_error_errno(r, "Could not get description: %s", bus_error_message(&error, r));

Expand Down Expand Up @@ -448,14 +435,7 @@ static int set_simple_string_internal(sd_bus *bus, sd_bus_error *error, const ch
if (!error)
error = &e;

r = sd_bus_call_method(
bus,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
method,
error, NULL,
"sb", value, arg_ask_password);
r = bus_call_method(bus, bus_hostname, method, error, NULL, "sb", value, arg_ask_password);
if (r < 0)
return log_error_errno(r, "Could not set %s: %s", target, bus_error_message(error, r));

Expand Down
10 changes: 2 additions & 8 deletions src/journal-remote/journal-gatewayd.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "alloc-util.h"
#include "build.h"
#include "bus-locator.h"
#include "bus-util.h"
#include "errno-util.h"
#include "fd-util.h"
Expand Down Expand Up @@ -704,14 +705,7 @@ static int get_virtualization(char **v) {
if (r < 0)
return r;

r = sd_bus_get_property_string(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"Virtualization",
NULL,
&b);
r = bus_get_property_string(bus, bus_systemd_mgr, "Virtualization", NULL, &b);
if (r < 0)
return r;

Expand Down
11 changes: 2 additions & 9 deletions src/journal/journalctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "alloc-util.h"
#include "build.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-util.h"
#include "catalog.h"
#include "chase.h"
Expand Down Expand Up @@ -2246,15 +2247,7 @@ static int run(int argc, char *argv[]) {
if (r < 0)
return log_error_errno(r, "Failed to open system bus: %m");

r = sd_bus_call_method(
bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"OpenMachineRootDirectory",
&error,
&reply,
"s", arg_machine);
r = bus_call_method(bus, bus_machine_mgr, "OpenMachineRootDirectory", &error, &reply, "s", arg_machine);
if (r < 0)
return log_error_errno(r, "Failed to open root directory: %s", bus_error_message(&error, r));

Expand Down
21 changes: 3 additions & 18 deletions src/login/inhibit.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "alloc-util.h"
#include "build.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-util.h"
#include "fd-util.h"
#include "format-table.h"
Expand Down Expand Up @@ -41,15 +42,7 @@ static int inhibit(sd_bus *bus, sd_bus_error *error) {
int r;
int fd;

r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"Inhibit",
error,
&reply,
"ssss", arg_what, arg_who, arg_why, arg_mode);
r = bus_call_method(bus, bus_login_mgr, "Inhibit", error, &reply, "ssss", arg_what, arg_who, arg_why, arg_mode);
if (r < 0)
return r;

Expand All @@ -68,15 +61,7 @@ static int print_inhibitors(sd_bus *bus) {

pager_open(arg_pager_flags);

r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"ListInhibitors",
&error,
&reply,
"");
r = bus_call_method(bus, bus_login_mgr, "ListInhibitors", &error, &reply, NULL);
if (r < 0)
return log_error_errno(r, "Could not get active inhibitors: %s", bus_error_message(&error, r));

Expand Down
9 changes: 1 addition & 8 deletions src/login/loginctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -917,14 +917,7 @@ static int show_user(int argc, char *argv[], void *userdata) {
if (r < 0)
return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);

r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"GetUser",
&error, &reply,
"u", (uint32_t) uid);
r = bus_call_method(bus, bus_login_mgr, "GetUser", &error, &reply, "u", (uint32_t) uid);
if (r < 0)
return log_error_errno(r, "Failed to get user: %s", bus_error_message(&error, r));

Expand Down
9 changes: 2 additions & 7 deletions src/login/logind-user.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "alloc-util.h"
#include "bus-common-errors.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-util.h"
#include "cgroup-util.h"
#include "clean-ipc.h"
Expand Down Expand Up @@ -388,13 +389,7 @@ static int user_update_slice(User *u) {
u->user_record->io_weight == UINT64_MAX)
return 0;

r = sd_bus_message_new_method_call(
u->manager->bus,
&m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"SetUnitProperties");
r = bus_message_new_method_call(u->manager->bus, &m, bus_systemd_mgr, "SetUnitProperties");
if (r < 0)
return bus_log_create_error(r);

Expand Down
19 changes: 3 additions & 16 deletions src/login/test-inhibit.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "sd-bus.h"

#include "bus-locator.h"
#include "bus-util.h"
#include "fd-util.h"
#include "macro.h"
Expand All @@ -16,14 +17,7 @@ static int inhibit(sd_bus *bus, const char *what) {
int fd;
int r;

r = sd_bus_call_method(bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"Inhibit",
&error,
&reply,
"ssss", what, who, reason, mode);
r = bus_call_method(bus, bus_login_mgr, "Inhibit", &error, &reply, "ssss", what, who, reason, mode);
assert_se(r >= 0);

r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
Expand All @@ -41,14 +35,7 @@ static void print_inhibitors(sd_bus *bus) {
unsigned n = 0;
int r;

r = sd_bus_call_method(bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"ListInhibitors",
&error,
&reply,
"");
r = bus_call_method(bus, bus_login_mgr, "ListInhibitors", &error, &reply, NULL);
assert_se(r >= 0);

r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
Expand Down
2 changes: 1 addition & 1 deletion src/login/test-session-properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ TEST(set_type) {

/* Type is reset to the original value when we release control of the session */
assert_se(!streq(type, "tty"));
assert_se(bus_call_method(bus, &session, "ReleaseControl", NULL, NULL, "") >= 0);
assert_se(bus_call_method(bus, &session, "ReleaseControl", NULL, NULL, NULL) >= 0);
type = mfree(type);
assert_se(bus_get_property_string(bus, &session, "Type", NULL, &type) >= 0);
assert_se(streq(type, "tty"));
Expand Down
5 changes: 3 additions & 2 deletions src/login/user-runtime-dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "sd-bus.h"

#include "bus-error.h"
#include "bus-locator.h"
#include "dev-setup.h"
#include "format-util.h"
#include "fs-util.h"
Expand Down Expand Up @@ -33,13 +34,13 @@ static int acquire_runtime_dir_properties(uint64_t *size, uint64_t *inodes) {
if (r < 0)
return log_error_errno(r, "Failed to connect to system bus: %m");

r = sd_bus_get_property_trivial(bus, "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "RuntimeDirectorySize", &error, 't', size);
r = bus_get_property_trivial(bus, bus_login_mgr, "RuntimeDirectorySize", &error, 't', size);
if (r < 0) {
log_warning_errno(r, "Failed to acquire runtime directory size, ignoring: %s", bus_error_message(&error, r));
*size = physical_memory_scale(10U, 100U); /* 10% */
}

r = sd_bus_get_property_trivial(bus, "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "RuntimeDirectoryInodesMax", &error, 't', inodes);
r = bus_get_property_trivial(bus, bus_login_mgr, "RuntimeDirectoryInodesMax", &error, 't', inodes);
if (r < 0) {
log_warning_errno(r, "Failed to acquire number of inodes for runtime directory, ignoring: %s", bus_error_message(&error, r));
*inodes = DIV_ROUND_UP(*size, 4096);
Expand Down
6 changes: 2 additions & 4 deletions src/network/networkd-manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,10 @@ static int manager_connect_bus(Manager *m) {
if (r < 0)
return log_error_errno(r, "Failed to request match on Connected signal: %m");

r = sd_bus_match_signal_async(
r = bus_match_signal_async(
m->bus,
NULL,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
bus_login_mgr,
"PrepareForSleep",
match_prepare_for_sleep, NULL, m);
if (r < 0)
Expand Down
9 changes: 2 additions & 7 deletions src/nspawn/nspawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "btrfs-util.h"
#include "build.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-util.h"
#include "cap-list.h"
#include "capability-util.h"
Expand Down Expand Up @@ -2092,13 +2093,7 @@ static int resolved_listening(void) {
if (r == 0)
return 0;

r = sd_bus_get_property_string(bus,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"DNSStubListener",
&error,
&dns_stub_listener_mode);
r = bus_get_property_string(bus, bus_resolve_mgr, "DNSStubListener", &error, &dns_stub_listener_mode);
if (r < 0)
return log_debug_errno(r, "Failed to query DNSStubListener property: %s", bus_error_message(&error, r));

Expand Down
11 changes: 2 additions & 9 deletions src/oom/oomctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "build.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "copy.h"
#include "main-func.h"
#include "pretty-print.h"
Expand Down Expand Up @@ -55,15 +56,7 @@ static int dump_state(int argc, char *argv[], void *userdata) {

pager_open(arg_pager_flags);

r = sd_bus_call_method(
bus,
"org.freedesktop.oom1",
"/org/freedesktop/oom1",
"org.freedesktop.oom1.Manager",
"DumpByFileDescriptor",
&error,
&reply,
NULL);
r = bus_call_method(bus, bus_oom_mgr, "DumpByFileDescriptor", &error, &reply, NULL);
if (r < 0)
return log_error_errno(r, "Failed to dump context: %s", bus_error_message(&error, r));

Expand Down
9 changes: 2 additions & 7 deletions src/portable/portable.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "bus-common-errors.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "chase.h"
#include "conf-files.h"
#include "copy.h"
Expand Down Expand Up @@ -780,13 +781,7 @@ static int unit_file_is_active(
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
const char *at, *prefix, *joined;

r = sd_bus_message_new_method_call(
bus,
&m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ListUnitsByPatterns");
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "ListUnitsByPatterns");
if (r < 0)
return r;

Expand Down
14 changes: 3 additions & 11 deletions src/portable/portablectl.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,7 @@ static int maybe_reload(sd_bus **bus) {
if (r < 0)
return r;

r = sd_bus_message_new_method_call(
*bus,
&m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"Reload");
r = bus_message_new_method_call(*bus, &m, bus_systemd_mgr, "Reload");
if (r < 0)
return bus_log_create_error(r);

Expand Down Expand Up @@ -566,12 +560,10 @@ static int maybe_enable_disable(sd_bus *bus, const char *path, bool enable) {
if (!names)
return log_oom();

r = sd_bus_message_new_method_call(
r = bus_message_new_method_call(
bus,
&m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
bus_systemd_mgr,
enable ? "EnableUnitFilesWithFlags" : "DisableUnitFilesWithFlags");
if (r < 0)
return bus_log_create_error(r);
Expand Down
Loading

0 comments on commit 8ddc5f7

Please sign in to comment.