return seen;
}
+
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;
}
- 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;
}
- lock_fd = open(FW3_STATEFILE, O_CREAT | O_RDWR);
+ return true;
+}
+void
+fw3_unlock(void)
+{
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);
+}
+
+struct list_head *
+fw3_read_state(void)
+{
+ FILE *sf;
+
+ int n;
+ char line[128];
+ const char *p;
+
+ struct list_head *state;
+ struct fw3_statefile_entry *entry;
+
+ state = malloc(sizeof(*state));
+
+ if (!state)
+ return NULL;
+
+ INIT_LIST_HEAD(state);
+
+ sf = fopen(FW3_STATEFILE, "r");
+
+ if (!sf)
{
- warn("Unable to create %s file", FW3_STATEFILE);
- goto fail;
+ warn("Cannot open state %s: %s", FW3_STATEFILE, strerror(errno));
+ free(state);
+
+ return NULL;
}
- if (flock(lock_fd, LOCK_EX))
+ while (fgets(line, sizeof(line), sf))
{
- warn("Unable to acquire exclusive lock on %s file", FW3_STATEFILE);
- goto fail;
+ entry = malloc(sizeof(*entry));
- }
+ if (!entry)
+ continue;
- return true;
+ memset(entry, 0, sizeof(*entry));
-fail:
- if (lock_fd > -1)
- {
- close(lock_fd);
- lock_fd = -1;
+ p = strtok(line, " \t\n");
+
+ if (!p)
+ continue;
+
+ entry->type = strtoul(p, NULL, 10);
+
+ p = strtok(NULL, " \t\n");
+
+ if (!p)
+ continue;
+
+ entry->name = strdup(p);
+
+ for (n = 0, p = strtok(NULL, " \t\n");
+ n < ARRAY_SIZE(entry->flags) && p != NULL;
+ n++, p = strtok(NULL, " \t\n"))
+ {
+ entry->flags[n] = strtoul(p, NULL, 10);
+ }
+
+ list_add_tail(&entry->list, state);
}
- return false;
+ fclose(sf);
+
+ return state;
}
void
-fw3_remove_statefile(void)
+fw3_free_state(struct list_head *statefile)
{
- if (lock_fd > -1)
- fw3_close_statefile();
-
- if (unlink(FW3_STATEFILE))
- warn("Unable to delete %s file", FW3_STATEFILE);
+ fw3_free_list(statefile);
+ free(statefile);
}
void
-fw3_close_statefile(void)
+fw3_write_state(void *state)
{
- flock(lock_fd, LOCK_UN);
- close(lock_fd);
+ FILE *sf;
+ struct fw3_state *s = state;
+ struct fw3_defaults *d = &s->defaults;
+ struct fw3_zone *z;
+ struct fw3_ipset *i;
- lock_fd = -1;
+ sf = fopen(FW3_STATEFILE, "w");
+
+ if (!sf)
+ {
+ warn("Cannot create state %s: %s", FW3_STATEFILE, strerror(errno));
+ return;
+ }
+
+ fprintf(sf, "%u - %u\n", FW3_TYPE_DEFAULTS, d->has_flag);
+
+ list_for_each_entry(z, &s->zones, list)
+ {
+ fprintf(sf, "%u %s %u %u\n", FW3_TYPE_ZONE,
+ z->name, z->has_src_target, z->has_dest_target);
+ }
+
+ list_for_each_entry(i, &s->ipsets, list)
+ {
+ if (i->external && *i->external)
+ continue;
+
+ fprintf(sf, "%u %s\n", FW3_TYPE_IPSET, i->name);
+ }
+
+ fclose(sf);
+}
+
+void
+fw3_remove_state(void)
+{
+ if (unlink(FW3_STATEFILE))
+ warn("Unable to remove state %s: %s", FW3_STATEFILE, strerror(errno));
}