Welcome to little lamb

Code » anopa » commit 79a95b3

start: Fix possible warning "unable to get longrun info"

author Olivier Brunel
2016-01-09 14:58:46 UTC
committer Olivier Brunel
2016-01-09 14:58:46 UTC
parent 2f0792ab93efeae8c0e02405d8328009d87b69bb

start: Fix possible warning "unable to get longrun info"

Because we might get the same id more than once, but if the last event
was 'U' - and therefore we unsubscribe - we shouldn't try to check() for
that (now invalid) id anymore.

src/libanopa/exec_longrun.c +34 -8

diff --git a/src/libanopa/exec_longrun.c b/src/libanopa/exec_longrun.c
index 482120a..52c7c09 100644
--- a/src/libanopa/exec_longrun.c
+++ b/src/libanopa/exec_longrun.c
@@ -199,38 +199,64 @@ _exec_longrun (int si, aa_mode mode)
     return 0;
 }
 
+static int idx = -1;
+
 int
 aa_get_longrun_info (uint16 *id, char *event)
 {
-    static int i = -1;
     int r;
 
-    if (i == -1)
+    if (idx == -1)
     {
         r = ftrigr_update (&_aa_ft);
         if (r <= 0)
             return -1;
-        i = 0;
+        idx = 0;
     }
 
-    for ( ; i < genalloc_len (uint16, &_aa_ft.list); )
+    for ( ; idx < genalloc_len (uint16, &_aa_ft.list); )
     {
-        *id = genalloc_s (uint16, &_aa_ft.list)[i];
+        *id = genalloc_s (uint16, &_aa_ft.list)[idx];
         r = ftrigr_check (&_aa_ft, *id, event);
-        ++i;
+        ++idx;
         if (r != 0)
             return (r < 0) ? -2 : r;
     }
 
-    i = -1;
+    idx = -1;
     return 0;
 }
 
 int
 aa_unsubscribe_for (uint16 id)
 {
+    int r = 0;
     tain_t deadline;
 
     tain_addsec_g (&deadline, 1);
-    return ftrigr_unsubscribe_g (&_aa_ft, id, &deadline);
+    if (!ftrigr_unsubscribe_g (&_aa_ft, id, &deadline))
+        r = -1;
+    /* if it works, we need to remove any future iteration of that id in the
+     * _aa_ft.list, to avoid EINVAL from ftrigr_check() in aa_get_longrun_info()
+     * above, as more of this id could have been listed */
+    if (r == 0 && idx >= 0)
+    {
+        int i = genalloc_len (uint16, &_aa_ft.list);
+
+        for (--i; i >= idx; --i)
+        {
+            if (genalloc_s (uint16, &_aa_ft.list)[i] == id)
+            {
+                int len = genalloc_len (uint16, &_aa_ft.list);
+                int c = len - i - 1;
+
+                if (c > 0)
+                    byte_copy (genalloc_s (uint16, &_aa_ft.list) + i,
+                            c * sizeof (uint16),
+                            genalloc_s (uint16, &_aa_ft.list) + i + 1);
+                genalloc_setlen (uint16, &_aa_ft.list, len - 1);
+            }
+        }
+    }
+    return r;
 }