network: introduce reference counter for Network object

This commit is contained in:
Yu Watanabe
2019-05-07 16:55:19 +02:00
parent bdafa22d93
commit 35ac3b7664
4 changed files with 17 additions and 10 deletions
+1 -1
View File
@@ -1458,7 +1458,7 @@ void manager_free(Manager *m) {
m->duids_requesting_uuid = set_free(m->duids_requesting_uuid);
while ((network = m->networks))
network_free(network);
network_unref(network);
hashmap_free(m->networks_by_name);
m->netdevs = hashmap_free_with_destructor(m->netdevs, netdev_unref);
+9 -5
View File
@@ -298,7 +298,7 @@ int network_verify(Network *network) {
int network_load_one(Manager *manager, const char *filename) {
_cleanup_free_ char *fname = NULL, *name = NULL;
_cleanup_(network_freep) Network *network = NULL;
_cleanup_(network_unrefp) Network *network = NULL;
_cleanup_fclose_ FILE *file = NULL;
const char *dropin_dirname;
char *d;
@@ -344,6 +344,8 @@ int network_load_one(Manager *manager, const char *filename) {
.filename = TAKE_PTR(fname),
.name = TAKE_PTR(name),
.n_ref = 1,
.required_for_online = true,
.required_operstate_for_online = LINK_OPERSTATE_DEGRADED,
.dhcp = ADDRESS_FAMILY_NO,
@@ -473,7 +475,7 @@ int network_load(Manager *manager) {
assert(manager);
while ((network = manager->networks))
network_free(network);
network_unref(network);
r = conf_files_list_strv(&files, ".network", NULL, 0, NETWORK_DIRS);
if (r < 0)
@@ -488,7 +490,7 @@ int network_load(Manager *manager) {
return 0;
}
void network_free(Network *network) {
static Network *network_free(Network *network) {
IPv6ProxyNDPAddress *ipv6_proxy_ndp_address;
RoutingPolicyRule *rule;
FdbEntry *fdb_entry;
@@ -499,7 +501,7 @@ void network_free(Network *network) {
Route *route;
if (!network)
return;
return NULL;
free(network->filename);
@@ -586,9 +588,11 @@ void network_free(Network *network) {
set_free_free(network->dnssec_negative_trust_anchors);
free(network);
return mfree(network);
}
DEFINE_TRIVIAL_REF_UNREF_FUNC(Network, network, network_free);
int network_get_by_name(Manager *manager, const char *name, Network **ret) {
Network *network;
+5 -3
View File
@@ -92,6 +92,8 @@ struct Network {
char *filename;
char *name;
unsigned n_ref;
Set *match_mac;
char **match_path;
char **match_driver;
@@ -278,9 +280,9 @@ struct Network {
LIST_FIELDS(Network, networks);
};
void network_free(Network *network);
DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free);
Network *network_ref(Network *network);
Network *network_unref(Network *network);
DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_unref);
int network_load(Manager *manager);
int network_load_one(Manager *manager, const char *filename);
+2 -1
View File
@@ -169,9 +169,10 @@ static void test_config_parse_hwaddr(void) {
}
static void test_config_parse_address_one(const char *rvalue, int family, unsigned n_addresses, const union in_addr_union *u, unsigned char prefixlen) {
_cleanup_(network_freep) Network *network = NULL;
_cleanup_(network_unrefp) Network *network = NULL;
assert_se(network = new0(Network, 1));
network->n_ref = 1;
assert_se(network->filename = strdup("hogehoge.network"));
assert_se(config_parse_ifnames("network", "filename", 1, "section", 1, "Name", 0, "*", &network->match_name, network) == 0);
assert_se(config_parse_address("network", "filename", 1, "section", 1, "Address", 0, rvalue, network, network) == 0);