chiark / gitweb /
treewide: a few more log_*_errno + return simplifications
[elogind.git] / src / libsystemd / sd-rtnl / rtnl-util.c
index caa21d60f556e9dd7158b12183f9d27816fe9f57..4521742b8e9a78e51cebdf1a16ff95b78894fb8e 100644 (file)
@@ -19,7 +19,6 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <linux/rtnetlink.h>
 #include <netinet/ether.h>
 
 #include "sd-rtnl.h"
@@ -27,7 +26,7 @@
 #include "rtnl-util.h"
 #include "rtnl-internal.h"
 
-int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) {
+int rtnl_set_link_name(sd_rtnl **rtnl, int ifindex, const char *name) {
         _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
         int r;
 
@@ -35,7 +34,13 @@ int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) {
         assert(ifindex > 0);
         assert(name);
 
-        r = sd_rtnl_message_new_link(RTM_SETLINK, ifindex, &message);
+        if (!*rtnl) {
+                r = sd_rtnl_open(rtnl, 0);
+                if (r < 0)
+                        return r;
+        }
+
+        r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
         if (r < 0)
                 return r;
 
@@ -43,17 +48,16 @@ int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) {
         if (r < 0)
                 return r;
 
-        r = sd_rtnl_call(rtnl, message, 0, NULL);
+        r = sd_rtnl_call(*rtnl, message, 0, NULL);
         if (r < 0)
                 return r;
 
         return 0;
 }
 
-int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias,
+int rtnl_set_link_properties(sd_rtnl **rtnl, int ifindex, const char *alias,
                              const struct ether_addr *mac, unsigned mtu) {
         _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
-        bool need_update = false;
         int r;
 
         assert(rtnl);
@@ -62,7 +66,13 @@ int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias,
         if (!alias && !mac && mtu == 0)
                 return 0;
 
-        r = sd_rtnl_message_new_link(RTM_SETLINK, ifindex, &message);
+        if (!*rtnl) {
+                r = sd_rtnl_open(rtnl, 0);
+                if (r < 0)
+                        return r;
+        }
+
+        r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
         if (r < 0)
                 return r;
 
@@ -70,32 +80,23 @@ int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias,
                 r = sd_rtnl_message_append_string(message, IFLA_IFALIAS, alias);
                 if (r < 0)
                         return r;
-
-                need_update = true;
-
         }
 
         if (mac) {
                 r = sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, mac);
                 if (r < 0)
                         return r;
-
-                need_update = true;
         }
 
         if (mtu > 0) {
                 r = sd_rtnl_message_append_u32(message, IFLA_MTU, mtu);
                 if (r < 0)
                         return r;
-
-                need_update = true;
         }
 
-        if  (need_update) {
-                r = sd_rtnl_call(rtnl, message, 0, NULL);
-                if (r < 0)
-                        return r;
-        }
+        r = sd_rtnl_call(*rtnl, message, 0, NULL);
+        if (r < 0)
+                return r;
 
         return 0;
 }
@@ -106,7 +107,7 @@ int rtnl_message_new_synthetic_error(int error, uint32_t serial, sd_rtnl_message
 
         assert(error <= 0);
 
-        r = message_new(ret, NLMSG_SPACE(sizeof(struct nlmsgerr)));
+        r = message_new(NULL, ret, NLMSG_SPACE(sizeof(struct nlmsgerr)));
         if (r < 0)
                 return r;
 
@@ -155,18 +156,10 @@ bool rtnl_message_type_is_addr(uint16_t type) {
         }
 }
 
-int rtnl_message_link_get_ifname(sd_rtnl_message *message, const char **ret) {
-        unsigned short type;
-        void *name;
-
-        assert(rtnl_message_type_is_link(message->hdr->nlmsg_type));
-
-        while (sd_rtnl_message_read(message, &type, &name)) {
-                if (type == IFLA_IFNAME) {
-                        *ret = name;
-                        return 0;
-                }
-        }
+int rtnl_log_parse_error(int r) {
+        return log_error_errno(r, "Failed to parse netlink message: %m");
+}
 
-        return -ENOENT;
+int rtnl_log_create_error(int r) {
+        return log_error_errno(r, "Failed to create netlink message: %m");
 }