Skip to content

nft-helper: Terminate on errors #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading