separate state and lock files, use state file information to purge ipsets
[project/firewall3.git] / utils.c
diff --git a/utils.c b/utils.c
index 9899d4d..5198305 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -295,70 +295,86 @@ fw3_has_table(bool ipv6, const char *table)
        return seen;
 }
 
        return seen;
 }
 
+
 bool
 bool
-fw3_check_statefile(bool test_exists)
+fw3_lock(void)
 {
 {
-       struct stat s;
+       lock_fd = open(FW3_LOCKFILE, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
 
 
-       if (!stat(FW3_STATEFILE, &s))
+       if (lock_fd < 0)
        {
        {
-               if (test_exists)
-                       return true;
-
-               warn("The firewall appears to be started already. "
-                        "If it is indeed empty, remove the %s file and retry.",
-                        FW3_STATEFILE);
-
+               warn("Cannot create lock file %s: %s", FW3_LOCKFILE, strerror(errno));
                return false;
        }
                return false;
        }
-       else if (test_exists)
+
+       if (flock(lock_fd, LOCK_EX))
        {
        {
-               warn("The firewall appears to stopped already.");
+               warn("Cannot acquire exclusive lock: %s", strerror(errno));
                return false;
        }
 
                return false;
        }
 
-       lock_fd = open(FW3_STATEFILE, O_CREAT | O_RDWR);
+       return true;
+}
 
 
+void
+fw3_unlock(void)
+{
        if (lock_fd < 0)
        if (lock_fd < 0)
+               return;
+
+       if (flock(lock_fd, LOCK_UN))
+               warn("Cannot release exclusive lock: %s", strerror(errno));
+
+       close(lock_fd);
+       unlink(FW3_LOCKFILE);
+
+       lock_fd = -1;
+}
+
+
+bool fw3_has_state(void)
+{
+       struct stat s;
+       return !stat(FW3_STATEFILE, &s);
+}
+
+void fw3_write_state(void *state)
+{
+       int fd;
+       struct fw3_state *s = state;
+       struct fw3_zone *z;
+       struct fw3_ipset *i;
+
+       fd = open(FW3_STATEFILE, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
+
+       if (fd < 0)
        {
        {
-               warn("Unable to create %s file", FW3_STATEFILE);
-               goto fail;
+               warn("Cannot create state %s: %s", FW3_STATEFILE, strerror(errno));
+               return;
        }
 
        }
 
-       if (flock(lock_fd, LOCK_EX))
+       list_for_each_entry(z, &s->zones, list)
        {
        {
-               warn("Unable to acquire exclusive lock on %s file", FW3_STATEFILE);
-               goto fail;
-
+               write(fd, "zone ", 5);
+               write(fd, z->name, strlen(z->name));
+               write(fd, "\n", 1);
        }
 
        }
 
-       return true;
-
-fail:
-       if (lock_fd > -1)
+       list_for_each_entry(i, &s->ipsets, list)
        {
        {
-               close(lock_fd);
-               lock_fd = -1;
+               if (i->external && *i->external)
+                       continue;
+
+               write(fd, "ipset ", 6);
+               write(fd, i->name, strlen(i->name));
+               write(fd, "\n", 1);
        }
 
        }
 
-       return false;
+       close(fd);
 }
 
 }
 
-void
-fw3_remove_statefile(void)
+void fw3_remove_state(void)
 {
 {
-       if (lock_fd > -1)
-               fw3_close_statefile();
-
        if (unlink(FW3_STATEFILE))
        if (unlink(FW3_STATEFILE))
-               warn("Unable to delete %s file", FW3_STATEFILE);
-}
-
-void
-fw3_close_statefile(void)
-{
-       flock(lock_fd, LOCK_UN);
-       close(lock_fd);
-
-       lock_fd = -1;
+               warn("Unable to remove state %s: %s", FW3_STATEFILE, strerror(errno));
 }
 }