ustd: improve error handling
[ust.git] / ustd / ustd.c
index 21da4ebb8785d620018b75e5736ee27cc2265e18..d2a9712a20c1ca78cbe99cc6be7337be9c602e31 100644 (file)
@@ -48,6 +48,7 @@
 #define PUT_SUBBUF_OK 1
 #define PUT_SUBBUF_DIED 0
 #define PUT_SUBBUF_PUSHED 2
+#define PUT_SUBBUF_DONE 3
 
 char *sock_path=NULL;
 char *trace_path=NULL;
@@ -98,6 +99,11 @@ int get_subbuffer(struct buffer_info *buf)
                retval = GET_SUBBUF_DONE;
                goto end_rep;
        }
+       else if(!strcmp(received_msg, "NOTFOUND")) {
+               WARN("For buffer %s, the trace was not found. This likely means it was destroyed by the user.", buf->name);
+               retval = GET_SUBBUF_DONE;
+               goto end_rep;
+       }
        else {
                DBG("error getting subbuffer %s", buf->name);
                retval = -1;
@@ -130,11 +136,18 @@ int put_subbuffer(struct buffer_info *buf)
                retval = PUT_SUBBUF_DIED;
                goto end;
        }
-       if(result < 0) {
+       else if(result < 0) {
                ERR("put_subbuffer: send_message failed");
                retval = -1;
                goto end;
        }
+       else if(result == 0) {
+               /* Program seems finished. However this might not be
+                * the last subbuffer that has to be collected.
+                */
+               retval = PUT_SUBBUF_DIED;
+               goto end;
+       }
 
        result = sscanf(received_msg, "%as", &rep_code);
        if(result != 1) {
@@ -147,6 +160,14 @@ int put_subbuffer(struct buffer_info *buf)
                DBG("subbuffer put %s", buf->name);
                retval = PUT_SUBBUF_OK;
        }
+       else if(!strcmp(received_msg, "NOTFOUND")) {
+               WARN("For buffer %s, the trace was not found. This likely means it was destroyed by the user.", buf->name);
+               /* However, maybe this was not the last subbuffer. So
+                * we return the program died.
+                */
+               retval = PUT_SUBBUF_DIED;
+               goto end_rep;
+       }
        else {
                DBG("put_subbuffer: received error, we were pushed");
                retval = PUT_SUBBUF_PUSHED;
@@ -239,6 +260,9 @@ struct buffer_info *connect_buffer(pid_t pid, const char *bufname)
                ERR("problem in ustcomm_send_request(get_pidunique)");
                return NULL;
        }
+       if(result == 0) {
+               goto error;
+       }
 
        result = sscanf(received_msg, "%lld", &buf->pidunique);
        if(result != 1) {
@@ -256,6 +280,9 @@ struct buffer_info *connect_buffer(pid_t pid, const char *bufname)
                ERR("problem in ustcomm_send_request(get_shmid)");
                return NULL;
        }
+       if(result == 0) {
+               goto error;
+       }
 
        result = sscanf(received_msg, "%d %d", &buf->shmid, &buf->bufstruct_shmid);
        if(result != 2) {
@@ -273,6 +300,9 @@ struct buffer_info *connect_buffer(pid_t pid, const char *bufname)
                ERR("problem in ustcomm_send_request(g_n_subbufs)");
                return NULL;
        }
+       if(result == 0) {
+               goto error;
+       }
 
        result = sscanf(received_msg, "%d", &buf->n_subbufs);
        if(result != 1) {
@@ -284,8 +314,15 @@ struct buffer_info *connect_buffer(pid_t pid, const char *bufname)
 
        /* get subbuf size */
        asprintf(&send_msg, "get_subbuf_size %s", buf->name);
-       ustcomm_send_request(&buf->conn, send_msg, &received_msg);
+       result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
        free(send_msg);
+       if(result == -1) {
+               ERR("problem in ustcomm_send_request(get_subbuf_size)");
+               return NULL;
+       }
+       if(result == 0) {
+               goto error;
+       }
 
        result = sscanf(received_msg, "%d", &buf->subbuf_size);
        if(result != 1) {
@@ -357,6 +394,10 @@ struct buffer_info *connect_buffer(pid_t pid, const char *bufname)
        pthread_mutex_unlock(&active_buffers_mutex);
 
        return buf;
+
+error:
+       free(buf);
+       return NULL;
 }
 
 int write_current_subbuffer(struct buffer_info *buf)
@@ -420,6 +461,15 @@ int consumer_loop(struct buffer_info *buf)
                        finish_consuming_dead_subbuffer(buf);
                        break;
                }
+               else if(result == PUT_SUBBUF_DONE) {
+                       /* Done with this subbuffer */
+                       /* FIXME: add a case where this branch is used? Upon
+                        * normal trace termination, at put_subbuf time, a
+                        * special last-subbuffer code could be returned by
+                        * the listener.
+                        */
+                       break;
+               }
                else if(result == PUT_SUBBUF_OK) {
                }
        }
This page took 0.025366 seconds and 4 git commands to generate.