Add time flag for how long to run the program for.

This commit is contained in:
gamemann
2021-12-15 20:51:23 +00:00
parent 21b15badf6
commit 5012fc9ff8
3 changed files with 18 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <getopt.h> #include <getopt.h>
#include "cmdline.h" #include "cmdline.h"
@@ -8,6 +9,7 @@ const struct option opts[] =
{"config", required_argument, NULL, 'c'}, {"config", required_argument, NULL, 'c'},
{"offload", no_argument, NULL, 'o'}, {"offload", no_argument, NULL, 'o'},
{"skb", no_argument, NULL, 's'}, {"skb", no_argument, NULL, 's'},
{"time", required_argument, NULL, 't'},
{"list", no_argument, NULL, 'l'}, {"list", no_argument, NULL, 'l'},
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
@@ -24,7 +26,7 @@ void parsecommandline(struct cmdline *cmd, int argc, char *argv[])
{ {
int c; int c;
while ((c = getopt_long(argc, argv, "c:oslh", opts, NULL)) != -1) while ((c = getopt_long(argc, argv, "c:ost:lh", opts, NULL)) != -1)
{ {
switch (c) switch (c)
{ {
@@ -43,6 +45,11 @@ void parsecommandline(struct cmdline *cmd, int argc, char *argv[])
break; break;
case 't':
cmd->time = atoi(optarg);
break;
case 'l': case 'l':
cmd->list = 1; cmd->list = 1;

View File

@@ -5,6 +5,7 @@ struct cmdline
char *cfgfile; char *cfgfile;
unsigned int offload : 1; unsigned int offload : 1;
unsigned int skb : 1; unsigned int skb : 1;
unsigned int time;
unsigned int list : 1; unsigned int list : 1;
unsigned int help : 1; unsigned int help : 1;
}; };

View File

@@ -311,6 +311,7 @@ int main(int argc, char *argv[])
"--config -c => Config file location (default is /etc/xdpfw/xdpfw.conf).\n" \ "--config -c => Config file location (default is /etc/xdpfw/xdpfw.conf).\n" \
"--offload -o => Tries to load the XDP program in hardware/offload mode." \ "--offload -o => Tries to load the XDP program in hardware/offload mode." \
"--skb -s => Force the XDP program to load with SKB mode instead of DRV." \ "--skb -s => Force the XDP program to load with SKB mode instead of DRV." \
"--time -t => How long to run the program for in seconds before exiting. 0 or not set = infinite.\n" \
"--list -l => Print config details including filters (this will exit program after done).\n" \ "--list -l => Print config details including filters (this will exit program after done).\n" \
"--help -h => Print help menu.\n"); "--help -h => Print help menu.\n");
@@ -471,11 +472,19 @@ int main(int argc, char *argv[])
// Receive CPU count for stats map parsing. // Receive CPU count for stats map parsing.
int cpus = get_nprocs_conf(); int cpus = get_nprocs_conf();
unsigned int endTime = (cmd.time > 0) ? time(NULL) + cmd.time : 0;
while (cont) while (cont)
{ {
// Get current time. // Get current time.
time_t curTime = time(NULL); time_t curTime = time(NULL);
// Check if we should end the program.
if (endTime > 0 && curTime >= endTime)
{
break;
}
// Check for auto-update. // Check for auto-update.
if (cfg.updatetime > 0 && (curTime - lastupdated) > cfg.updatetime) if (cfg.updatetime > 0 && (curTime - lastupdated) > cfg.updatetime)
{ {