chiark / gitweb /
udev-builtin-keyboard: More useful error message
[elogind.git] / src / libsystemd-rtnl / rtnl-message.c
index 85cf55a3cdcc2df8cad09e0d5445512078f82b89..26da7f2db88c3b3db5cce254dce4afe2224b276d 100644 (file)
@@ -68,6 +68,27 @@ static int message_new(sd_rtnl_message **ret, size_t initial_size) {
         return 0;
 }
 
+int message_new_synthetic_error(int error, uint32_t serial, sd_rtnl_message **ret) {
+        struct nlmsgerr *err;
+        int r;
+
+        assert(error <= 0);
+
+        r = message_new(ret, NLMSG_SPACE(sizeof(struct nlmsgerr)));
+        if (r < 0)
+                return r;
+
+        (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
+        (*ret)->hdr->nlmsg_type = NLMSG_ERROR;
+        (*ret)->hdr->nlmsg_seq = serial;
+
+        err = NLMSG_DATA((*ret)->hdr);
+
+        err->error = error;
+
+        return 0;
+}
+
 int sd_rtnl_message_route_new(uint16_t nlmsg_type, unsigned char rtm_family,
                               unsigned char rtm_dst_len, unsigned char rtm_src_len,
                               unsigned char rtm_tos, unsigned char rtm_table,
@@ -108,7 +129,8 @@ int sd_rtnl_message_link_new(uint16_t nlmsg_type, int index, unsigned int type,
         struct ifinfomsg *ifi;
         int r;
 
-        assert_return(nlmsg_type == RTM_NEWLINK || nlmsg_type == RTM_DELLINK || nlmsg_type == RTM_GETLINK, -EINVAL);
+        assert_return(nlmsg_type == RTM_NEWLINK || nlmsg_type == RTM_DELLINK ||
+                      nlmsg_type == RTM_SETLINK || nlmsg_type == RTM_GETLINK, -EINVAL);
         assert_return(index > 0, -EINVAL);
         assert_return(ret, -EINVAL);
 
@@ -187,6 +209,7 @@ static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data,
         uint32_t rta_length, message_length;
         struct nlmsghdr *new_hdr;
         struct rtattr *rta;
+        char *padding;
 
         assert_return(m, -EINVAL);
         assert_return(m->hdr, -EINVAL);
@@ -194,10 +217,9 @@ static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data,
         assert_return(data, -EINVAL);
         assert_return(data_length > 0, -EINVAL);
 
-        /* get the size of the new rta attribute (without padding at the end) */
+        /* get the size of the new rta attribute (with padding at the end) */
         rta_length = RTA_LENGTH(data_length);
-        /* get the new message size (with padding between the old message and the new attrib,
-         * but no padding after)
+        /* get the new message size (with padding at the end)
          */
         message_length = m->hdr->nlmsg_len + RTA_ALIGN(rta_length);
 
@@ -218,7 +240,9 @@ static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data,
         /* we don't deal with the case where the user lies about the type and gives us
          * too little data (so don't do that)
          */
-        memcpy(RTA_DATA(rta), data, data_length);
+        padding = mempcpy(RTA_DATA(rta), data, data_length);
+        /* make sure also the padding at the end of the message is initialized */
+        memset(padding, '\0', (unsigned char *) m->hdr + m->hdr->nlmsg_len - (unsigned char *) padding);
 
         return 0;
 }
@@ -235,14 +259,15 @@ int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *
 
         switch (rtm_type) {
                 case RTM_NEWLINK:
+                case RTM_SETLINK:
                 case RTM_DELLINK:
                 case RTM_GETLINK:
                         switch (type) {
                                 case IFLA_IFNAME:
+                                case IFLA_IFALIAS:
                                 case IFLA_QDISC:
                                         return add_rtattr(m, type, data, strlen(data) + 1);
                                 case IFLA_MTU:
-                                        return add_rtattr(m, type, data, sizeof(uint32_t));
                                 case IFLA_LINK:
                                         return add_rtattr(m, type, data, sizeof(uint32_t));
                                 case IFLA_STATS:
@@ -329,6 +354,7 @@ int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data)
 
         switch (rtm_type) {
                 case RTM_NEWLINK:
+                case RTM_SETLINK:
                 case RTM_DELLINK:
                 case RTM_GETLINK:
                         if (!m->next_rta) {
@@ -365,16 +391,16 @@ int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data)
         return message_read(m, type, data);
 }
 
-int message_get_serial(sd_rtnl_message *m) {
+uint32_t message_get_serial(sd_rtnl_message *m) {
         assert(m);
 
         return m->hdr->nlmsg_seq;
 }
 
-int message_get_errno(sd_rtnl_message *m) {
+int sd_rtnl_message_get_errno(sd_rtnl_message *m) {
         struct nlmsgerr *err;
 
-        assert(m);
+        assert_return(m, -EINVAL);
 
         if (m->hdr->nlmsg_type != NLMSG_ERROR)
                 return 0;
@@ -489,6 +515,7 @@ int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
                                         k = -EIO;
                                 break;
                         case RTM_NEWLINK:
+                        case RTM_SETLINK:
                         case RTM_DELLINK:
                         case RTM_GETLINK:
                                 if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg)))