Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Merge "UPSTREAM: iptables: insist that the lock is held."
Browse files Browse the repository at this point in the history
am: c9c53db

Change-Id: I490a69bfad64a45bdc8736cacd4ced83da8c07f9
  • Loading branch information
lcolitti authored and android-build-merger committed Jun 1, 2017
2 parents 73dffad + c9c53db commit d6c570e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 37 deletions.
7 changes: 1 addition & 6 deletions iptables/ip6tables-restore.c
Expand Up @@ -296,12 +296,7 @@ int ip6tables_restore_main(int argc, char *argv[])
in_table = 0;
} else if ((buffer[0] == '*') && (!in_table)) {
/* Acquire a lock before we create a new table handle */
lock = xtables_lock(wait, &wait_interval);
if (lock == XT_LOCK_BUSY) {
fprintf(stderr, "Another app is currently holding the xtables lock. "
"Perhaps you want to use the -w option?\n");
exit(RESOURCE_PROBLEM);
}
lock = xtables_lock_or_exit(wait, &wait_interval);

/* New table */
char *table;
Expand Down
11 changes: 2 additions & 9 deletions iptables/ip6tables.c
Expand Up @@ -1779,15 +1779,8 @@ int do_command6(int argc, char *argv[], char **table,
generic_opt_check(command, cs.options);

/* Attempt to acquire the xtables lock */
if (!restore && xtables_lock(wait, &wait_interval) == XT_LOCK_BUSY) {
fprintf(stderr, "Another app is currently holding the xtables lock. ");
if (wait == 0)
fprintf(stderr, "Perhaps you want to use the -w option?\n");
else
fprintf(stderr, "Stopped waiting after %ds.\n", wait);
xtables_free_opts(1);
exit(RESOURCE_PROBLEM);
}
if (!restore)
xtables_lock_or_exit(wait, &wait_interval);

/* only allocate handle if we weren't called with a handle */
if (!*handle)
Expand Down
7 changes: 1 addition & 6 deletions iptables/iptables-restore.c
Expand Up @@ -295,12 +295,7 @@ iptables_restore_main(int argc, char *argv[])
in_table = 0;
} else if ((buffer[0] == '*') && (!in_table)) {
/* Acquire a lock before we create a new table handle */
lock = xtables_lock(wait, &wait_interval);
if (lock == XT_LOCK_BUSY) {
fprintf(stderr, "Another app is currently holding the xtables lock. "
"Perhaps you want to use the -w option?\n");
exit(RESOURCE_PROBLEM);
}
lock = xtables_lock_or_exit(wait, &wait_interval);

/* New table */
char *table;
Expand Down
11 changes: 2 additions & 9 deletions iptables/iptables.c
Expand Up @@ -1766,15 +1766,8 @@ int do_command4(int argc, char *argv[], char **table,
generic_opt_check(command, cs.options);

/* Attempt to acquire the xtables lock */
if (!restore && xtables_lock(wait, &wait_interval) == XT_LOCK_BUSY) {
fprintf(stderr, "Another app is currently holding the xtables lock. ");
if (wait == 0)
fprintf(stderr, "Perhaps you want to use the -w option?\n");
else
fprintf(stderr, "Stopped waiting after %ds.\n", wait);
xtables_free_opts(1);
exit(RESOURCE_PROBLEM);
}
if (!restore)
xtables_lock_or_exit(wait, &wait_interval);

/* only allocate handle if we weren't called with a handle */
if (!*handle)
Expand Down
31 changes: 28 additions & 3 deletions iptables/xshared.c
Expand Up @@ -246,7 +246,7 @@ void xs_init_match(struct xtables_match *match)
match->init(match->m);
}

int xtables_lock(int wait, struct timeval *wait_interval)
static int xtables_lock(int wait, struct timeval *wait_interval)
{
struct timeval time_left, wait_time;
int fd, i = 0;
Expand All @@ -255,8 +255,11 @@ int xtables_lock(int wait, struct timeval *wait_interval)
time_left.tv_usec = 0;

fd = open(XT_LOCK_NAME, O_CREAT, 0600);
if (fd < 0)
return XT_LOCK_UNSUPPORTED;
if (fd < 0) {
fprintf(stderr, "Fatal: can't open lock file %s: %s\n",
XT_LOCK_NAME, strerror(errno));
return XT_LOCK_FAILED;
}

if (wait == -1) {
if (flock(fd, LOCK_EX) == 0)
Expand Down Expand Up @@ -291,6 +294,28 @@ void xtables_unlock(int lock)
close(lock);
}

int xtables_lock_or_exit(int wait, struct timeval *wait_interval)
{
int lock = xtables_lock(wait, wait_interval);

if (lock == XT_LOCK_FAILED) {
xtables_free_opts(1);
exit(RESOURCE_PROBLEM);
}

if (lock == XT_LOCK_BUSY) {
fprintf(stderr, "Another app is currently holding the xtables lock. ");
if (wait == 0)
fprintf(stderr, "Perhaps you want to use the -w option?\n");
else
fprintf(stderr, "Stopped waiting after %ds.\n", wait);
xtables_free_opts(1);
exit(RESOURCE_PROBLEM);
}

return lock;
}

int parse_wait_time(int argc, char *argv[])
{
int wait = -1;
Expand Down
7 changes: 3 additions & 4 deletions iptables/xshared.h
Expand Up @@ -93,8 +93,7 @@ extern void xs_init_match(struct xtables_match *);
*
* A value >= 0 indicates the lock filedescriptor. Other values are:
*
* XT_LOCK_UNSUPPORTED : The system does not support locking, execution will
* proceed lockless.
* XT_LOCK_FAILED : The lock could not be acquired.
*
* XT_LOCK_BUSY : The lock was held by another process. xtables_lock only
* returns this value when |wait| == false. If |wait| == true, xtables_lock
Expand All @@ -104,11 +103,11 @@ extern void xs_init_match(struct xtables_match *);
*/
enum {
XT_LOCK_BUSY = -1,
XT_LOCK_UNSUPPORTED = -2,
XT_LOCK_FAILED = -2,
XT_LOCK_NOT_ACQUIRED = -3,
};
extern int xtables_lock(int wait, struct timeval *tv);
extern void xtables_unlock(int lock);
extern int xtables_lock_or_exit(int wait, struct timeval *tv);

int parse_wait_time(int argc, char *argv[]);
void parse_wait_interval(int argc, char *argv[], struct timeval *wait_interval);
Expand Down

0 comments on commit d6c570e

Please sign in to comment.