Skip to content
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

net: redirect nftables stdout and stderr to log CRIU's log file #2549

Open
wants to merge 1 commit into
base: criu-dev
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions criu/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -3073,6 +3073,7 @@ static inline int nftables_lock_network_internal(void)
int ret = 0;
char table[32];
char buf[128];
FILE *fp;

if (nftables_get_table(table, sizeof(table)))
return -1;
Expand All @@ -3081,6 +3082,14 @@ static inline int nftables_lock_network_internal(void)
if (!nft)
return -1;

fp = fdopen(log_get_fd(), "w");
if (!fp) {
pr_perror("fdopen() failed");
goto err3;
}
nft_ctx_set_output(nft, fp);
nft_ctx_set_error(nft, fp);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code in nftables_lock_network_internal() and nftables_network_unlock() looks very similar.

Would it make sense to create a function?

static inline int nftables_set_logfd(struct nft_ctx *nft)
{
	FILE *fp;
	fp = fdopen(log_get_fd(), "w");
	if (!fp) {
		pr_perror("fdopen() failed");
		return -1;
	}
	nft_ctx_set_output(nft, fp);
	nft_ctx_set_error(nft, fp);

	fflush(fp);
	fclose(fp);
	return 0;
}

We can use this function in nftables_lock_network_internal():

if (nftables_set_logfd(nft)) {
	goto err2;
}

and in nftables_network_unlock():

if (nftables_set_logfd(nft)) {
	nft_ctx_free(nft);
	return -1;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nft_ctx_set_output() and nft_ctx_set_error() functions set the output or error file pointer in ctx to the value of fp. They return the previous value to aid in temporary file pointer overrides. On error, these functions return NULL. 

nftables_set_logfd can't close fp...


snprintf(buf, sizeof(buf), "create table %s", table);
if (NFT_RUN_CMD(nft, buf))
goto err2;
Expand All @@ -3107,6 +3116,9 @@ static inline int nftables_lock_network_internal(void)
snprintf(buf, sizeof(buf), "delete table %s", table);
NFT_RUN_CMD(nft, buf);
err2:
fflush(fp);
fclose(fp);
err3:
ret = -1;
pr_err("Locking network failed using nftables\n");
out:
Expand Down Expand Up @@ -3171,6 +3183,7 @@ static inline int nftables_network_unlock(void)
struct nft_ctx *nft;
char table[32];
char buf[128];
FILE *fp;

if (nftables_get_table(table, sizeof(table)))
return -1;
Expand All @@ -3179,10 +3192,21 @@ static inline int nftables_network_unlock(void)
if (!nft)
return -1;

fp = fdopen(log_get_fd(), "w");
if (!fp) {
pr_perror("fdopen() failed");
nft_ctx_free(nft);
return -1;
}
nft_ctx_set_output(nft, fp);
nft_ctx_set_error(nft, fp);

snprintf(buf, sizeof(buf), "delete table %s", table);
if (NFT_RUN_CMD(nft, buf))
ret = -1;

fflush(fp);
fclose(fp);
nft_ctx_free(nft);
return ret;
#else
Expand Down
Loading