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

ebtables-tiny: Fix lockfile function and reduce wait on lock #275

Open
wants to merge 2 commits into
base: main
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
17 changes: 14 additions & 3 deletions net/ebtables-tiny/src/libebtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <time.h>

static void decrease_chain_jumps(struct ebt_u_replace *replace);
static int iterate_entries(struct ebt_u_replace *replace, int type);
Expand Down Expand Up @@ -127,6 +128,7 @@ void ebt_list_extensions()
static int lock_file()
{
int fd, try = 0;
int ret = 0;

retry:
fd = open(LOCKFILE, O_CREAT, 00600);
Expand All @@ -136,13 +138,18 @@ static int lock_file()
try = 1;
goto retry;
}
return flock(fd, LOCK_EX);
ret = flock(fd, LOCK_EX | LOCK_NB);
if (ret)
close(fd);
return ret;
}

/* Get the table from the kernel or from a binary file */
int ebt_get_kernel_table(struct ebt_u_replace *replace)
{
int ret;
struct timespec req = {0};
int errcount = 0;

if (!ebt_find_table(replace->name)) {
ebt_print_error("Bad table name '%s'", replace->name);
Expand All @@ -155,8 +162,12 @@ int ebt_get_kernel_table(struct ebt_u_replace *replace)
* this file locking is disabled by default. */
ebt_print_error2("Unable to create lock file "LOCKFILE);
}
fprintf(stderr, "Trying to obtain lock %s\n", LOCKFILE);
sleep(1);
if (errcount++ % 20 == 0) {
fprintf(stderr, "Trying to obtain lock %s\n", LOCKFILE);
}
req.tv_sec = 0;
req.tv_nsec = 50*1000*1000; //50ms
nanosleep(&req, NULL);
}
/* Get the kernel's information */
if (ebt_get_table(replace))
Expand Down