properly deal with only v4 or only v6 start/stop/restart
[project/firewall3.git] / utils.c
diff --git a/utils.c b/utils.c
index 4691fe1..c3dbb3d 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -332,15 +332,8 @@ fw3_unlock(void)
 }
 
 
 }
 
 
-bool
-fw3_has_state(void)
-{
-       struct stat s;
-       return !stat(FW3_STATEFILE, &s);
-}
-
 struct list_head *
 struct list_head *
-fw3_read_state(void)
+fw3_read_statefile(void)
 {
        FILE *sf;
 
 {
        FILE *sf;
 
@@ -351,6 +344,11 @@ fw3_read_state(void)
        struct list_head *state;
        struct fw3_statefile_entry *entry;
 
        struct list_head *state;
        struct fw3_statefile_entry *entry;
 
+       sf = fopen(FW3_STATEFILE, "r");
+
+       if (!sf)
+               return NULL;
+
        state = malloc(sizeof(*state));
 
        if (!state)
        state = malloc(sizeof(*state));
 
        if (!state)
@@ -358,16 +356,6 @@ fw3_read_state(void)
 
        INIT_LIST_HEAD(state);
 
 
        INIT_LIST_HEAD(state);
 
-       sf = fopen(FW3_STATEFILE, "r");
-
-       if (!sf)
-       {
-               warn("Cannot open state %s: %s", FW3_STATEFILE, strerror(errno));
-               free(state);
-
-               return NULL;
-       }
-
        while (fgets(line, sizeof(line), sf))
        {
                entry = malloc(sizeof(*entry));
        while (fgets(line, sizeof(line), sf))
        {
                entry = malloc(sizeof(*entry));
@@ -407,14 +395,7 @@ fw3_read_state(void)
 }
 
 void
 }
 
 void
-fw3_free_state(struct list_head *statefile)
-{
-       fw3_free_list(statefile);
-       free(statefile);
-}
-
-void
-fw3_write_state(void *state)
+fw3_write_statefile(void *state)
 {
        FILE *sf;
        struct fw3_state *s = state;
 {
        FILE *sf;
        struct fw3_state *s = state;
@@ -422,6 +403,17 @@ fw3_write_state(void *state)
        struct fw3_zone *z;
        struct fw3_ipset *i;
 
        struct fw3_zone *z;
        struct fw3_ipset *i;
 
+       int mask = (1 << FW3_DEFAULT_IPV4_LOADED) | (1 << FW3_DEFAULT_IPV6_LOADED);
+
+       if (!(d->has_flag & mask))
+       {
+               if (unlink(FW3_STATEFILE))
+                       warn("Unable to remove state %s: %s",
+                            FW3_STATEFILE, strerror(errno));
+
+               return;
+       }
+
        sf = fopen(FW3_STATEFILE, "w");
 
        if (!sf)
        sf = fopen(FW3_STATEFILE, "w");
 
        if (!sf)
@@ -450,8 +442,19 @@ fw3_write_state(void *state)
 }
 
 void
 }
 
 void
-fw3_remove_state(void)
+fw3_free_statefile(struct list_head *statefile)
 {
 {
-       if (unlink(FW3_STATEFILE))
-               warn("Unable to remove state %s: %s", FW3_STATEFILE, strerror(errno));
+       struct fw3_statefile_entry *e, *tmp;
+
+       if (!statefile)
+               return;
+
+       list_for_each_entry_safe(e, tmp, statefile, list)
+       {
+               list_del(&e->list);
+               free(e->name);
+               free(e);
+       }
+
+       free(statefile);
 }
 }