1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

network/netdev: split out netdev_attach() and netdev_attach_name() from netdev_load_one()

No functional change, just refactoring and preparation for later
commits.
This commit is contained in:
Yu Watanabe 2024-09-09 17:41:00 +09:00
parent 0dc71b9584
commit bfacb8fe1d
2 changed files with 44 additions and 15 deletions

View File

@ -267,6 +267,45 @@ void netdev_drop(NetDev *netdev) {
netdev_detach(netdev);
}
int netdev_attach_name(NetDev *netdev, const char *name) {
int r;
assert(netdev);
assert(netdev->manager);
assert(name);
r = hashmap_ensure_put(&netdev->manager->netdevs, &string_hash_ops, name, netdev);
if (r == -ENOMEM)
return log_oom();
if (r == -EEXIST) {
NetDev *n = hashmap_get(netdev->manager->netdevs, name);
assert(n);
if (!streq(netdev->filename, n->filename))
log_netdev_warning_errno(netdev, r,
"Device \"%s\" was already configured by \"%s\", ignoring %s.",
name, n->filename, netdev->filename);
return -EEXIST;
}
assert(r > 0);
return 0;
}
static int netdev_attach(NetDev *netdev) {
int r;
assert(netdev);
assert(netdev->ifname);
r = netdev_attach_name(netdev, netdev->ifname);
if (r < 0)
return r;
return 0;
}
int netdev_get(Manager *manager, const char *name, NetDev **ret) {
NetDev *netdev;
@ -896,21 +935,9 @@ int netdev_load_one(Manager *manager, const char *filename) {
if (!netdev->filename)
return log_oom();
r = hashmap_ensure_put(&netdev->manager->netdevs, &string_hash_ops, netdev->ifname, netdev);
if (r == -ENOMEM)
return log_oom();
if (r == -EEXIST) {
NetDev *n = hashmap_get(netdev->manager->netdevs, netdev->ifname);
assert(n);
if (!streq(netdev->filename, n->filename))
log_netdev_warning_errno(netdev, r,
"Device was already configured by \"%s\", ignoring %s.",
n->filename, netdev->filename);
return -EEXIST;
}
assert(r > 0);
r = netdev_attach(netdev);
if (r < 0)
return r;
log_netdev_debug(netdev, "loaded \"%s\"", netdev_kind_to_string(netdev->kind));

View File

@ -5,6 +5,7 @@
#include "conf-parser.h"
#include "ether-addr-util.h"
#include "hash-funcs.h"
#include "list.h"
#include "log-link.h"
#include "networkd-link.h"
@ -195,6 +196,7 @@ extern const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX];
/* For casting the various netdev kinds into a netdev */
#define NETDEV(n) (&(n)->meta)
int netdev_attach_name(NetDev *netdev, const char *name);
NetDev* netdev_detach_name(NetDev *netdev, const char *name);
void netdev_detach(NetDev *netdev);