Optimizing main loop (#35)
* Mem leak fix Memleak fix for strdup() * Typo fix forgot ; * Optimizing Main Loop Filtersmap will only be updated if there is a newer version of our config. * Include sys/stat.h * Added myself in the credits Thanks for the trust
This commit is contained in:
@@ -179,3 +179,4 @@ This XDP project performs basic layer 3/4 forwarding using source port mapping s
|
|||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
* [Christian Deacon](https://github.com/gamemann) - Creator.
|
* [Christian Deacon](https://github.com/gamemann) - Creator.
|
||||||
|
* [Phil](https://github.com/Nasty07) - Contributor.
|
||||||
|
|||||||
29
src/xdpfw.c
29
src/xdpfw.c
@@ -9,6 +9,7 @@
|
|||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <sys/resource.h>
|
#include <sys/resource.h>
|
||||||
#include <sys/sysinfo.h>
|
#include <sys/sysinfo.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
@@ -291,6 +292,7 @@ int attachxdp(int ifidx, int progfd, struct cmdline *cmd)
|
|||||||
return mode;
|
return mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct stat conf_stat;
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
// Parse the command line.
|
// Parse the command line.
|
||||||
@@ -341,8 +343,9 @@ int main(int argc, char *argv[])
|
|||||||
setcfgdefaults(&cfg);
|
setcfgdefaults(&cfg);
|
||||||
|
|
||||||
// Create last updated variable.
|
// Create last updated variable.
|
||||||
time_t lastupdated = time(NULL);
|
time_t lastupdatecheck = time(NULL);
|
||||||
time_t statslastupdated = time(NULL);
|
time_t statslastupdated = time(NULL);
|
||||||
|
time_t lastupdated = time(NULL);
|
||||||
|
|
||||||
// Update config.
|
// Update config.
|
||||||
updateconfig(&cfg, cmd.cfgfile);
|
updateconfig(&cfg, cmd.cfgfile);
|
||||||
@@ -510,20 +513,26 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check for auto-update.
|
// Check for auto-update.
|
||||||
if (cfg.updatetime > 0 && (curTime - lastupdated) > cfg.updatetime)
|
if (cfg.updatetime > 0 && (curTime - lastupdatecheck) > cfg.updatetime)
|
||||||
{
|
{
|
||||||
// Memleak fix for strdup() in updateconfig()
|
// Check if config file have been modified
|
||||||
// Before updating it again, we need to free the old return value
|
if (stat(cmd.cfgfile, &conf_stat) == 0 && conf_stat.st_mtime > lastupdated) {
|
||||||
free(cfg.interface);
|
// Memleak fix for strdup() in updateconfig()
|
||||||
|
// Before updating it again, we need to free the old return value
|
||||||
|
free(cfg.interface);
|
||||||
|
|
||||||
// Update config.
|
// Update config.
|
||||||
updateconfig(&cfg, cmd.cfgfile);
|
updateconfig(&cfg, cmd.cfgfile);
|
||||||
|
|
||||||
// Update BPF maps.
|
// Update BPF maps.
|
||||||
updatefilters(&cfg);
|
updatefilters(&cfg);
|
||||||
|
|
||||||
|
// Update timer
|
||||||
|
lastupdated = time(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
// Update last updated variable.
|
// Update last updated variable.
|
||||||
lastupdated = time(NULL);
|
lastupdatecheck = time(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update stats.
|
// Update stats.
|
||||||
|
|||||||
Reference in New Issue
Block a user