mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-13 13:17:43 +03:00
Merge pull request #22096 from keszybz/networkctl-bus-once
Open the bus once in networkctl
This commit is contained in:
commit
ba3440b9ab
@ -66,6 +66,14 @@
|
||||
#define free(a) FreePool(a)
|
||||
#endif
|
||||
|
||||
/* This passes the argument through after (if asserts are enabled) checking that it is not null. */
|
||||
#define ASSERT_PTR(expr) \
|
||||
({ \
|
||||
typeof(expr) _expr_ = (expr); \
|
||||
assert(_expr_); \
|
||||
_expr_; \
|
||||
})
|
||||
|
||||
#if defined(static_assert)
|
||||
#define assert_cc(expr) \
|
||||
static_assert(expr, #expr)
|
||||
|
@ -79,17 +79,12 @@ static bool arg_full = false;
|
||||
static unsigned arg_lines = 10;
|
||||
static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
|
||||
|
||||
static int get_description(JsonVariant **ret) {
|
||||
static int get_description(sd_bus *bus, JsonVariant **ret) {
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
||||
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
||||
const char *text = NULL;
|
||||
int r;
|
||||
|
||||
r = sd_bus_open_system(&bus);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to connect system bus: %m");
|
||||
|
||||
r = bus_call_method(bus, bus_network_mgr, "Describe", &error, &reply, NULL);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to get description: %s", bus_error_message(&error, r));
|
||||
@ -105,11 +100,11 @@ static int get_description(JsonVariant **ret) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dump_manager_description(void) {
|
||||
static int dump_manager_description(sd_bus *bus) {
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
|
||||
int r;
|
||||
|
||||
r = get_description(&v);
|
||||
r = get_description(bus, &v);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@ -117,14 +112,14 @@ static int dump_manager_description(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dump_link_description(char **patterns) {
|
||||
static int dump_link_description(sd_bus *bus, char **patterns) {
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
|
||||
_cleanup_free_ bool *matched_patterns = NULL;
|
||||
JsonVariant *i;
|
||||
size_t c = 0;
|
||||
int r;
|
||||
|
||||
r = get_description(&v);
|
||||
r = get_description(bus, &v);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@ -618,11 +613,12 @@ static int link_get_property(
|
||||
sd_bus_message **reply,
|
||||
const char *iface,
|
||||
const char *propname) {
|
||||
_cleanup_free_ char *path = NULL, *ifindex_str = NULL;
|
||||
|
||||
char ifindex_str[DECIMAL_STR_MAX(int)];
|
||||
_cleanup_free_ char *path = NULL;
|
||||
int r;
|
||||
|
||||
if (asprintf(&ifindex_str, "%i", link->ifindex) < 0)
|
||||
return -ENOMEM;
|
||||
xsprintf(ifindex_str, "%i", link->ifindex);
|
||||
|
||||
r = sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str, &path);
|
||||
if (r < 0)
|
||||
@ -790,6 +786,7 @@ static int acquire_link_info(sd_bus *bus, sd_netlink *rtnl, char **patterns, Lin
|
||||
}
|
||||
|
||||
static int list_links(int argc, char *argv[], void *userdata) {
|
||||
sd_bus *bus = ASSERT_PTR(userdata);
|
||||
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
|
||||
_cleanup_(link_info_array_freep) LinkInfo *links = NULL;
|
||||
_cleanup_(table_unrefp) Table *table = NULL;
|
||||
@ -798,9 +795,9 @@ static int list_links(int argc, char *argv[], void *userdata) {
|
||||
|
||||
if (arg_json_format_flags != JSON_FORMAT_OFF) {
|
||||
if (arg_all || argc <= 1)
|
||||
return dump_manager_description();
|
||||
return dump_manager_description(bus);
|
||||
else
|
||||
return dump_link_description(strv_skip(argv, 1));
|
||||
return dump_link_description(bus, strv_skip(argv, 1));
|
||||
}
|
||||
|
||||
r = sd_netlink_open(&rtnl);
|
||||
@ -1223,12 +1220,10 @@ static int list_address_labels(int argc, char *argv[], void *userdata) {
|
||||
}
|
||||
|
||||
static int open_lldp_neighbors(int ifindex, FILE **ret) {
|
||||
_cleanup_free_ char *p = NULL;
|
||||
char p[STRLEN("/run/systemd/netif/lldp/") + DECIMAL_STR_MAX(int)];
|
||||
FILE *f;
|
||||
|
||||
if (asprintf(&p, "/run/systemd/netif/lldp/%i", ifindex) < 0)
|
||||
return -ENOMEM;
|
||||
|
||||
xsprintf(p, "/run/systemd/netif/lldp/%i", ifindex);
|
||||
f = fopen(p, "re");
|
||||
if (!f)
|
||||
return -errno;
|
||||
@ -1556,7 +1551,7 @@ static int link_status_one(
|
||||
|
||||
_cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **sip = NULL, **search_domains = NULL, **route_domains = NULL;
|
||||
_cleanup_free_ char *t = NULL, *network = NULL, *iaid = NULL, *duid = NULL,
|
||||
*setup_state = NULL, *operational_state = NULL, *online_state = NULL, *lease_file = NULL, *activation_policy = NULL;
|
||||
*setup_state = NULL, *operational_state = NULL, *online_state = NULL, *activation_policy = NULL;
|
||||
const char *driver = NULL, *path = NULL, *vendor = NULL, *model = NULL, *link = NULL,
|
||||
*on_color_operational, *off_color_operational, *on_color_setup, *off_color_setup, *on_color_online;
|
||||
_cleanup_free_ int *carrier_bound_to = NULL, *carrier_bound_by = NULL;
|
||||
@ -1606,8 +1601,8 @@ static int link_status_one(
|
||||
(void) sd_network_link_get_carrier_bound_to(info->ifindex, &carrier_bound_to);
|
||||
(void) sd_network_link_get_carrier_bound_by(info->ifindex, &carrier_bound_by);
|
||||
|
||||
if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", info->ifindex) < 0)
|
||||
return log_oom();
|
||||
char lease_file[STRLEN("/run/systemd/netif/leases/") + DECIMAL_STR_MAX(int)];
|
||||
xsprintf(lease_file, "/run/systemd/netif/leases/%i", info->ifindex);
|
||||
|
||||
(void) dhcp_lease_load(&lease, lease_file);
|
||||
|
||||
@ -2383,7 +2378,7 @@ static int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) {
|
||||
}
|
||||
|
||||
static int link_status(int argc, char *argv[], void *userdata) {
|
||||
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
||||
sd_bus *bus = ASSERT_PTR(userdata);
|
||||
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
|
||||
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
|
||||
_cleanup_(link_info_array_freep) LinkInfo *links = NULL;
|
||||
@ -2391,17 +2386,13 @@ static int link_status(int argc, char *argv[], void *userdata) {
|
||||
|
||||
if (arg_json_format_flags != JSON_FORMAT_OFF) {
|
||||
if (arg_all || argc <= 1)
|
||||
return dump_manager_description();
|
||||
return dump_manager_description(bus);
|
||||
else
|
||||
return dump_link_description(strv_skip(argv, 1));
|
||||
return dump_link_description(bus, strv_skip(argv, 1));
|
||||
}
|
||||
|
||||
pager_open(arg_pager_flags);
|
||||
|
||||
r = sd_bus_open_system(&bus);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to connect system bus: %m");
|
||||
|
||||
r = sd_netlink_open(&rtnl);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to connect to netlink: %m");
|
||||
@ -2738,14 +2729,10 @@ static int link_renew_one(sd_bus *bus, int index, const char *name) {
|
||||
}
|
||||
|
||||
static int link_renew(int argc, char *argv[], void *userdata) {
|
||||
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
||||
sd_bus *bus = ASSERT_PTR(userdata);
|
||||
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
|
||||
int index, k = 0, r;
|
||||
|
||||
r = sd_bus_open_system(&bus);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to connect system bus: %m");
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
|
||||
if (index < 0)
|
||||
@ -2772,14 +2759,10 @@ static int link_force_renew_one(sd_bus *bus, int index, const char *name) {
|
||||
}
|
||||
|
||||
static int link_force_renew(int argc, char *argv[], void *userdata) {
|
||||
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
||||
sd_bus *bus = ASSERT_PTR(userdata);
|
||||
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
|
||||
int k = 0, r;
|
||||
|
||||
r = sd_bus_open_system(&bus);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to connect system bus: %m");
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
int index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
|
||||
if (index < 0)
|
||||
@ -2794,14 +2777,10 @@ static int link_force_renew(int argc, char *argv[], void *userdata) {
|
||||
}
|
||||
|
||||
static int verb_reload(int argc, char *argv[], void *userdata) {
|
||||
sd_bus *bus = ASSERT_PTR(userdata);
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
||||
int r;
|
||||
|
||||
r = sd_bus_open_system(&bus);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to connect system bus: %m");
|
||||
|
||||
r = bus_call_method(bus, bus_network_mgr, "Reload", &error, NULL, NULL);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to reload network settings: %m");
|
||||
@ -2810,17 +2789,13 @@ static int verb_reload(int argc, char *argv[], void *userdata) {
|
||||
}
|
||||
|
||||
static int verb_reconfigure(int argc, char *argv[], void *userdata) {
|
||||
sd_bus *bus = ASSERT_PTR(userdata);
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
||||
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
|
||||
_cleanup_set_free_ Set *indexes = NULL;
|
||||
int index, r;
|
||||
void *p;
|
||||
|
||||
r = sd_bus_open_system(&bus);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to connect system bus: %m");
|
||||
|
||||
indexes = set_new(NULL);
|
||||
if (!indexes)
|
||||
return log_oom();
|
||||
@ -2968,7 +2943,7 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int networkctl_main(int argc, char *argv[]) {
|
||||
static int networkctl_main(sd_bus *bus, int argc, char *argv[]) {
|
||||
static const Verb verbs[] = {
|
||||
{ "list", VERB_ANY, VERB_ANY, VERB_DEFAULT, list_links },
|
||||
{ "status", VERB_ANY, VERB_ANY, 0, link_status },
|
||||
@ -2984,20 +2959,15 @@ static int networkctl_main(int argc, char *argv[]) {
|
||||
{}
|
||||
};
|
||||
|
||||
return dispatch_verb(argc, argv, verbs, NULL);
|
||||
return dispatch_verb(argc, argv, verbs, bus);
|
||||
}
|
||||
|
||||
static int check_netns_match(void) {
|
||||
static int check_netns_match(sd_bus *bus) {
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
||||
struct stat st;
|
||||
uint64_t id;
|
||||
int r;
|
||||
|
||||
r = sd_bus_open_system(&bus);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to connect system bus: %m");
|
||||
|
||||
r = sd_bus_get_property_trivial(
|
||||
bus,
|
||||
"org.freedesktop.network1",
|
||||
@ -3035,6 +3005,7 @@ static void warn_networkd_missing(void) {
|
||||
}
|
||||
|
||||
static int run(int argc, char* argv[]) {
|
||||
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
||||
int r;
|
||||
|
||||
log_setup();
|
||||
@ -3043,13 +3014,17 @@ static int run(int argc, char* argv[]) {
|
||||
if (r <= 0)
|
||||
return r;
|
||||
|
||||
r = check_netns_match();
|
||||
r = sd_bus_open_system(&bus);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to connect system bus: %m");
|
||||
|
||||
r = check_netns_match(bus);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
warn_networkd_missing();
|
||||
|
||||
return networkctl_main(argc, argv);
|
||||
return networkctl_main(bus, argc, argv);
|
||||
}
|
||||
|
||||
DEFINE_MAIN_FUNCTION(run);
|
||||
|
Loading…
x
Reference in New Issue
Block a user