ustd: unwrite the subbuffer if the put() was unsuccessful and we are going to crash...
authorPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Sat, 27 Feb 2010 17:21:36 +0000 (12:21 -0500)
committerPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Mon, 1 Mar 2010 18:30:52 +0000 (13:30 -0500)
This prevents the subbuffer to be written twice and fixes a FIXME.

ustd/ustd.c
ustd/ustd.h

index d2a9712a20c1ca78cbe99cc6be7337be9c602e31..f3285f58ba534d8c82c7f80677f5eba5f32de85d 100644 (file)
@@ -400,6 +400,25 @@ error:
        return NULL;
 }
 
+int unwrite_last_subbuffer(struct buffer_info *buf)
+{
+       int result;
+
+       result = ftruncate(buf->file_fd, buf->previous_offset);
+       if(result == -1) {
+               PERROR("ftruncate");
+               return -1;
+       }
+
+       result = lseek(buf->file_fd, buf->previous_offset, SEEK_SET);
+       if(result == (int)(off_t)-1) {
+               PERROR("lseek");
+               return -1;
+       }
+
+       return 0;
+}
+
 int write_current_subbuffer(struct buffer_info *buf)
 {
        int result;
@@ -408,11 +427,20 @@ int write_current_subbuffer(struct buffer_info *buf)
 
        size_t cur_sb_size = subbuffer_data_size(subbuf_mem);
 
+       off_t cur_offset = lseek(buf->file_fd, 0, SEEK_CUR);
+       if(cur_offset == (off_t)-1) {
+               PERROR("lseek");
+               return -1;
+       }
+
+       buf->previous_offset = cur_offset;
+       DBG("previous_offset: %ld", cur_offset);
+
        result = patient_write(buf->file_fd, subbuf_mem, cur_sb_size);
        if(result == -1) {
                PERROR("write");
                /* FIXME: maybe drop this trace */
-               return 0;
+               return -1;
        }
 
        return 0;
@@ -445,7 +473,6 @@ int consumer_loop(struct buffer_info *buf)
                /* FIXME: handle return value? */
 
                /* put the subbuffer */
-               /* FIXME: we actually should unput the buffer before consuming... */
                result = put_subbuffer(buf);
                if(result == -1) {
                        ERR("unknown error putting subbuffer (channel=%s)", buf->name);
@@ -457,7 +484,10 @@ int consumer_loop(struct buffer_info *buf)
                }
                else if(result == PUT_SUBBUF_DIED) {
                        WARN("application died while putting subbuffer");
-                       /* FIXME: probably need to skip the first subbuffer in finish_consuming_dead_subbuffer */
+                       /* Skip the first subbuffer. We are not sure it is trustable
+                        * because the put_subbuffer() did not complete.
+                        */
+                       unwrite_last_subbuffer(buf);
                        finish_consuming_dead_subbuffer(buf);
                        break;
                }
index 5be186adb10fcc35ef13eefef341a1f7ec6997f3..d8c4c506facde2b6fe3cfb12dedbde6edccad93d 100644 (file)
@@ -30,6 +30,9 @@ struct buffer_info {
        long consumed_old;
 
        s64 pidunique;
+
+       /* the offset we must truncate to, to unput the last subbuffer */
+       off_t previous_offset;
 };
 
 void finish_consuming_dead_subbuffer(struct buffer_info *buf);
This page took 0.025096 seconds and 4 git commands to generate.