@@ -444,6 +444,7 @@ char *ping_command(int family)
struct nstoken {
int orig_netns_fd;
+ char *name;
};
struct nstoken *open_netns(const char *name)
@@ -459,6 +460,13 @@ struct nstoken *open_netns(const char *name)
return NULL;
}
+ token->name = strdup(name);
+ if (!token->name) {
+ log_err("Failed to dup name");
+ free(token);
+ return NULL;
+ }
+
token->orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
if (token->orig_netns_fd == -1) {
log_err("Failed to open(/proc/self/ns/net)");
@@ -483,6 +491,7 @@ struct nstoken *open_netns(const char *name)
fail:
if (token->orig_netns_fd != -1)
close(token->orig_netns_fd);
+ free(token->name);
free(token);
return NULL;
}
@@ -494,10 +503,22 @@ void close_netns(struct nstoken *token)
if (setns(token->orig_netns_fd, CLONE_NEWNET))
log_err("Failed to setns(orig_netns_fd)");
+ if (token->name)
+ free(token->name);
close(token->orig_netns_fd);
free(token);
}
+void cleanup_netns(struct nstoken *token)
+{
+ if (!token)
+ return;
+
+ if (SYS_NOFAIL("ip netns del %s", token->name))
+ log_err("del netns %s failed", token->name);
+ close_netns(token);
+}
+
struct nstoken *create_netns(const char *name)
{
struct nstoken *token = NULL;
@@ -81,6 +81,7 @@ struct nstoken;
*/
struct nstoken *open_netns(const char *name);
void close_netns(struct nstoken *token);
+void cleanup_netns(struct nstoken *token);
struct nstoken *create_netns(const char *name);
int send_recv_data(int lfd, int fd, uint32_t total_bytes);
int unshare_netns(void);
@@ -79,14 +79,6 @@ struct mptcp_storage {
char ca_name[TCP_CA_NAME_MAX];
};
-static void cleanup_netns(struct nstoken *nstoken)
-{
- if (nstoken)
- close_netns(nstoken);
-
- SYS_NOFAIL("ip netns del %s", NS_TEST);
-}
-
static int start_mptcp_server(int family, const char *addr_str, __u16 port,
int timeout_ms)
{