chiark / gitweb /
sd-rtnl: multi-part message - store as linked-list rather than independent messages
authorTom Gundersen <teg@jklm.no>
Sun, 13 Apr 2014 19:37:53 +0000 (21:37 +0200)
committerTom Gundersen <teg@jklm.no>
Mon, 14 Apr 2014 15:53:21 +0000 (17:53 +0200)
This means the API can stay the same as for single-part messages by simply passing the head message around. Unrefing
the head of the linked list unrefs the whole list.

src/libsystemd/sd-rtnl/rtnl-internal.h
src/libsystemd/sd-rtnl/rtnl-message.c
src/systemd/sd-rtnl.h

index 9d857ed..2f788d0 100644 (file)
@@ -102,6 +102,8 @@ struct sd_rtnl_message {
         size_t *rta_offset_tb[RTNL_CONTAINER_DEPTH];
         unsigned short rta_tb_size[RTNL_CONTAINER_DEPTH];
         bool sealed:1;
+
+        sd_rtnl_message *next; /* next in a chain of multi-part messages */
 };
 
 int message_new(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t type);
index fc71ed9..3a24cb8 100644 (file)
@@ -378,6 +378,8 @@ sd_rtnl_message *sd_rtnl_message_unref(sd_rtnl_message *m) {
                 for (i = 0; i <= m->n_containers; i++)
                         free(m->rta_offset_tb[i]);
 
+                sd_rtnl_message_unref(m->next);
+
                 free(m);
         }
 
@@ -1078,6 +1080,8 @@ int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m) {
  * On failure, a negative error code is returned.
  */
 int socket_read_message(sd_rtnl *rtnl) {
+        _cleanup_rtnl_message_unref_ sd_rtnl_message *first = NULL;
+        sd_rtnl_message *previous = NULL;
         _cleanup_free_ void *buffer = NULL;
         uint8_t cred_buffer[CMSG_SPACE(sizeof(struct ucred))];
         struct iovec iov = {};
@@ -1146,6 +1150,10 @@ int socket_read_message(sd_rtnl *rtnl) {
                 if (new_msg->nlmsg_type == NLMSG_NOOP)
                         continue;
 
+                /* finished reading multi-part message */
+                if (new_msg->nlmsg_type == NLMSG_DONE)
+                        break;
+
                 /* check that we support this message type */
                 r = type_system_get_type(NULL, &nl_type, new_msg->nlmsg_type);
                 if (r < 0) {
@@ -1173,21 +1181,30 @@ int socket_read_message(sd_rtnl *rtnl) {
                 if (r < 0)
                         return r;
 
-                r = rtnl_rqueue_make_room(rtnl);
-                if (r < 0)
-                        return r;
+                if (!first)
+                        first = m;
+                else {
+                        assert(previous);
 
-                rtnl->rqueue[rtnl->rqueue_size ++] = m;
+                        previous->next = m;
+                }
+                previous = m;
                 m = NULL;
+
                 ret += new_msg->nlmsg_len;
 
-                /* reached end of multi-part message, or not a multi-part
-                   message at all */
-                if (new_msg->nlmsg_type == NLMSG_DONE ||
-                    !(new_msg->nlmsg_flags & NLM_F_MULTI))
+                /* not a multi-part message, so stop reading*/
+                if (!(new_msg->nlmsg_flags & NLM_F_MULTI))
                         break;
         }
 
+        r = rtnl_rqueue_make_room(rtnl);
+        if (r < 0)
+                return r;
+
+        rtnl->rqueue[rtnl->rqueue_size ++] = first;
+        first = NULL;
+
         return ret;
 }
 
@@ -1249,3 +1266,9 @@ void rtnl_message_seal(sd_rtnl_message *m) {
 
         m->sealed = true;
 }
+
+sd_rtnl_message *sd_rtnl_message_next(sd_rtnl_message *m) {
+        assert_return(m, NULL);
+
+        return m->next;
+}
index c32c5e2..6fbaee0 100644 (file)
@@ -123,6 +123,8 @@ int sd_rtnl_message_exit_container(sd_rtnl_message *m);
 
 int sd_rtnl_message_rewind(sd_rtnl_message *m);
 
+sd_rtnl_message *sd_rtnl_message_next(sd_rtnl_message *m);
+
 _SD_END_DECLARATIONS;
 
 #endif