conntrackd: claim lockfile ownership properly
PATCH 1/1 conntrackd: claim lockfile ownership properly
Before this patch, if conntrackd died in an abrupt manner (either by SIGKILL, SIGSEGV or abrupt shutdown), then it would have left a stale lock file that would have prevented any new conntrackd instances from running.
Contrary to abrupt termination, this same bug was not present when conntrackd was terminated with a graceful signal (e.g. SIGTERM). This was because then the lock file would have been removed from the signal handler as expected.
This patch fixes this bug by using POSIX File Locking API instead of opening file in O_EXCL mode. File Locking API ensures that file lock will be released once the process holding it terminates (either gracefully or abruptly).
One side effect of this patch is that daemonization has to happen before the lock file is locked (due to the fact that child processes do not inherit file locks from the parent process). This means that some error messages have to be logged in log file instead of STDOUT.
--- include/Makefile.am | 2 +- include/conntrackd.h | 1 + include/lock_file.h | 7 ++++++ src/Makefile.am | 2 +- src/lock_file.c | 42 ++++++++++++++++++++++++++++++++++ src/main.c | 64 ++++++++++++++++++++++++++-------------------------- src/run.c | 4 +++- 7 files changed, 87 insertions(+), 35 deletions(-) create mode 100644 include/lock_file.h create mode 100644 src/lock_file.c
include/Makefile.am +1 -1
@@ -1,7 +1,7 @@ SUBDIRS = linux noinst_HEADERS = alarm.h jhash.h cache.h linux_list.h linux_rbtree.h \ - sync.h conntrackd.h local.h udp.h tcp.h \ + sync.h conntrackd.h local.h lock_file.h udp.h tcp.h \ debug.h log.h hash.h mcast.h conntrack.h \ network.h filter.h queue.h vector.h cidr.h \ traffic_stats.h netlink.h fds.h event.h bitops.h channel.h \
include/conntrackd.h +1 -0
@@ -149,6 +149,7 @@ struct ct_general_state {struct ct_mode *mode;struct ct_filter *us_filter;struct exp_filter *exp_filter; +int lockfile_fd;struct nfct_handle *event;/* event handler */ struct nfct_filter *filter;/* event filter */
include/lock_file.h new file +7 -0
@@ -0,0 +1,7 @@ +#ifndef _LOCK_FILE_H_ +#define _LOCK_FILE_H_ + +int lock_file_lock (const char *lock_file); +void lock_file_unlock (const char *lock_file,int fd); + +#endif
src/Makefile.am +1 -1
@@ -39,7 +39,7 @@ endif nfct_LDFLAGS = -export-dynamic conntrackd_SOURCES = alarm.c main.c run.c hash.c queue.c rbtree.c \ - local.c log.c mcast.c udp.c netlink.c vector.c \ + local.c lock_file.c log.c mcast.c udp.c netlink.c vector.c \ filter.c fds.c event.c process.c origin.c date.c \ cache.c cache-ct.c cache-exp.c \ cache_timer.c \
src/lock_file.c new file +42 -0
42 lines not on this page · raw message
src/main.c +32 -32
111 lines not on this page · raw message
src/run.c +3 -1
@@ -22,6 +22,7 @@#include "conntrackd.h" #include "netlink.h" #include "filter.h" +#include "lock_file.h" #include "log.h" #include "alarm.h" #include "fds.h"
@@ -60,7 +61,8 @@ void killer(int signo)cthelper_kill ();#endif destroy_fds (STATE (fds)); -unlink (CONFIG (lockfile)); +if (STATE (lockfile_fd) !=-1 ) +lock_file_unlock (CONFIG (lockfile),STATE (lockfile_fd));dlog (LOG_NOTICE ,"---- shutdown received ----" );close_log ();
Signature · 1 line
1.8.1.2
Quoted 15 lines · Ansis Atteka
Before this patch, if conntrackd died in an abrupt manner (either by SIGKILL, SIGSEGV or abrupt shutdown), then it would have left a stale lock file that would have prevented any new conntrackd instances from running.
Contrary to abrupt termination, this same bug was not present when conntrackd was terminated with a graceful signal (e.g. SIGTERM). This was because then the lock file would have been removed from the signal handler as expected.
This patch fixes this bug by using POSIX File Locking API instead of opening file in
O_EXCLmode. File Locking API ensures that file lock will be released once the process holding it terminates (either gracefully or abruptly).
Quoted 6 lines
One side effect of this patch is that daemonization has to happen before the lock file is locked (due to the fact that child processes do not inherit file locks from the parent process). This means that some error messages have to be logged in log file instead of STDOUT.
Signed-off-by Ansis Atteka <[email protected]>
Could anyone look into this patch? Unless I am missing something, this seems like a bug to me that could result into conntrackd not running, if it was abruptly terminated.
Regards,
Ansis
Pablo Neira Ayuso
maintainerQuoted 4 lines · Ansis Atteka
Before this patch, if conntrackd died in an abrupt manner (either by SIGKILL, SIGSEGV or abrupt shutdown), then it would have left a stale lock file that would have prevented any new conntrackd instances from running.
SIGKILL and SIGSEGV need a closer look from the admin, since things are not going right. Sudden (abrupt) shutdown is indeed a problem.
Quoted 10 lines
Contrary to abrupt termination, this same bug was not present when conntrackd was terminated with a graceful signal (e.g. SIGTERM). This was because then the lock file would have been removed from the signal handler as expected.
This patch fixes this bug by using POSIX File Locking API instead of opening file in
O_EXCLmode. File Locking API ensures that file lock will be released once the process holding it terminates (either gracefully or abruptly).
Quoted 4 lines
One side effect of this patch is that daemonization has to happen before the lock file is locked (due to the fact that child processes do not inherit file locks from the parent process). This means that some error messages have to be logged in log file instead of STDOUT.
Can you rework this to avoid converting all those message from stdout to the log file? I like that this daemon bails out to stdout if early problems are found, I find it quite annoying when you launch things and then you have to check ps and log file to see the reason for an early configuration problem. And I don't find a good reason why we need an early daemonization that this patch introduces.
Pablo Neira Ayuso
maintainerQuoted 24 lines · Pablo Neira Ayuso
Quoted 4 lines · Ansis Atteka
Before this patch, if conntrackd died in an abrupt manner (either by SIGKILL, SIGSEGV or abrupt shutdown), then it would have left a stale lock file that would have prevented any new conntrackd instances from running.
SIGKILL and SIGSEGV need a closer look from the admin, since things are not going right. Sudden (abrupt) shutdown is indeed a problem.
Quoted 10 lines
Contrary to abrupt termination, this same bug was not present when conntrackd was terminated with a graceful signal (e.g. SIGTERM). This was because then the lock file would have been removed from the signal handler as expected.
This patch fixes this bug by using POSIX File Locking API instead of opening file in
O_EXCLmode. File Locking API ensures that file lock will be released once the process holding it terminates (either gracefully or abruptly).Quoted 4 lines
One side effect of this patch is that daemonization has to happen before the lock file is locked (due to the fact that child processes do not inherit file locks from the parent process). This means that some error messages have to be logged in log file instead of STDOUT.
Quoted 6 lines
Can you rework this to avoid converting all those message from stdout to the log file? I like that this daemon bails out to stdout if early problems are found, I find it quite annoying when you launch things and then you have to check ps and log file to see the reason for an early configuration problem. And I don't find a good reason why we need an early daemonization that this patch introduces.
Now I noticed after reading my own words, this can be problematic if someone just types 'conntrackd' by mistake instead of 'conntrackd -e' to dump the external cache.
What about using two locks? I mean, one that is grabbed when the process is in non-daemon stage, then another one once one it enters daemon stage. I think that should help to workaround the posix file locking limitation which doesn't allow us to inherit lock file?