Skip to content

Commit

Permalink
Merge pull request #18 from kernelkit/nft-helper-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
troglobit authored Dec 18, 2024
2 parents 25c6cf9 + 86ebb42 commit e35d3b5
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/nft-helper/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@ int run(char *cmd[])
_exit(execvp(cmd[0], cmd));
}

if (waitpid(pid, &rc, 0))
if (waitpid(pid, &rc, 0) != pid)
return -1;

return WEXITSTATUS(rc);
return rc;
}

void cb(int signo)
{
warnx("got signal %d, calling nft flush ruleset and exit.", signo);
warnx("got signal %d, calling nft flush ruleset and exit", signo);
}

int main(int argc, char *argv[])
{
char *load[] = { "nft", "-f", NULL, NULL };
char *flush[] = { "nft", "flush", "ruleset", NULL };
int rc;

if (argc < 2 || access(argv[1], F_OK))
errx(1, "Missing nft.conf argument.\nUsage:\n\t%s /path/to/nftables.conf", argv[0]);
Expand All @@ -46,7 +47,18 @@ int main(int argc, char *argv[])
signal(SIGHUP, cb);

load[2] = argv[1];
run(load);
rc = run(load);
if (rc == -1) {
err(1, "Internal error while waiting for ruleset to load");
} else if (WIFEXITED(rc)) {
rc = WEXITSTATUS(rc);
if (rc)
errx(rc, "Failed to load ruleset, exited with status %d", rc);
} else if (WIFSIGNALED(rc)) {
errx(rc, "Failed to load ruleset, terminated on signal %d", WTERMSIG(rc));
}

warnx("Ruleset active");
pause();
run(flush);

Expand Down

0 comments on commit e35d3b5

Please sign in to comment.