X-Git-Url: https://git.archive.openwrt.org/?p=project%2Ffirewall3.git;a=blobdiff_plain;f=utils.c;h=5198305214ccf0435f0c71c5cc131ef016e06454;hp=9899d4d5d507471848cf0f16b6937f6e467bee82;hb=a32e331a11034403df2e26807df9195435b6fb8a;hpb=8fee8f9c520c58d07772cc6bd8f65d9eb1776a56 diff --git a/utils.c b/utils.c index 9899d4d..5198305 100644 --- a/utils.c +++ b/utils.c @@ -295,70 +295,86 @@ fw3_has_table(bool ipv6, const char *table) 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); +} + +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)) - 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)); }