Skip to content

Commit

Permalink
selftests/bpf: Handle SIGINT when creating netns
Browse files Browse the repository at this point in the history
It's necessary to delete netns during BPF selftests interrupt, otherwise
the next tests run will fail due to unable to create netns.

This patch adds a new SIGINT handle netns_sig_handler() in create_netns(),
and deletes NS_TEST in this handler.

For passing argument to signal handler, a trick mentioned in [1] is
used.

[1]
https://stackoverflow.com/questions/6970224/providing-passing-argument-to-signal-handler

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
  • Loading branch information
Geliang Tang committed May 15, 2024
1 parent e2fb919 commit 1f36138
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tools/testing/selftests/bpf/network_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,16 @@ void cleanup_netns(struct nstoken *token)
close_netns(token);
}

static int netns_sig_handler(const int sig, void *ptr)
{
struct nstoken *token = (struct nstoken *)ptr;

signal(sig, SIG_IGN);
if (sig == SIGINT)
cleanup_netns(token);
return 0;
}

struct nstoken *create_netns(const char *name)
{
struct nstoken *token = NULL;
Expand All @@ -539,6 +549,8 @@ struct nstoken *create_netns(const char *name)
goto fail;
}

signal(SIGINT, (__sighandler_t)netns_sig_handler);
netns_sig_handler(SIGUSR1, (void *)token);
return token;

fail:
Expand Down

0 comments on commit 1f36138

Please sign in to comment.