summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2008-09-14 23:02:07 +0200
committerDavid Sommerseth <dazo@users.sourceforge.net>2008-09-14 23:02:07 +0200
commitab726a0092fc2f41c347fa86c48fb5bd4e27fa89 (patch)
treec713136a1feee132aa8749a9e2e73fee175c4ca1
parentad0df81c0cc8f9ef813dae560679f82d2086e1bb (diff)
Moved creation and destruction of semaphores and message queue into an own helper file. eFW_StartFirewall() now creates and eFW_RunFirewall() destructs, using this helper file.alpha_0.5
-rw-r--r--Makefile2
-rw-r--r--eurephiafw.c52
-rw-r--r--eurephiafw_helpers.c96
-rw-r--r--eurephiafw_helpers.h35
-rw-r--r--firewall/iptables/Makefile2
-rw-r--r--firewall/iptables/efw_iptables.c10
6 files changed, 143 insertions, 54 deletions
diff --git a/Makefile b/Makefile
index 116e26a..87a8f7b 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ topdir = .
directories = database/sqlite firewall/iptables
objs = eurephiadb.o eurephia_log.o eurephia_values.o eurephiadb_session.o sha512.o passwd.o \
- eurephia-auth.o eurephia.o certinfo.o eurephia_getsym.o eurephiafw.o
+ eurephia-auth.o eurephia.o certinfo.o eurephia_getsym.o eurephiafw.o eurephiafw_helpers.o
#programs = eurephiadb-testprog
testprog_obj = eurephiadb-testprog.o eurephiadb.o eurephia_log.o sha512.o eurephiadb_session.o
diff --git a/eurephiafw.c b/eurephiafw.c
index a5e665e..16328eb 100644
--- a/eurephiafw.c
+++ b/eurephiafw.c
@@ -35,11 +35,8 @@
#include "eurephiafw_intf.h"
#include "eurephia_getsym.h"
#include "eurephia_nullsafe.h"
-#include "eurephiafw_intf.h"
#include "eurephia_values.h"
-
-#define MQUEUE_NAME "/eurephiaFW"
-
+#include "eurephiafw_helpers.h"
int eFW_unload(eurephiaCTX *ctx) {
if( ctx == NULL ) {
@@ -127,29 +124,14 @@ void eFW_StartFirewall(eurephiaCTX *ctx) {
eurephia_log(ctx, LOG_INFO, 3, "Starting eurephia firewall interface");
- // Initialise the main process' semaphore
- ctx->fwcfg->thrdata.semp_master = sem_open("eurephia_fwmaster", O_CREAT, 0666, 0);
- if( ctx->fwcfg->thrdata.semp_master == SEM_FAILED ) {
- eurephia_log(ctx, LOG_FATAL, 0, "Could not setup semaphore for FW driver: %s", strerror(errno));
- free_nullsafe(ctx->fwcfg);
+ // Setup semaphores we need
+ if( efwSetupSemaphores(ctx, &(*ctx->fwcfg).thrdata) == 0 ) {
+ free_nullsafe(ctx->fwcfg->thrdata.fw_command);
return;
- }
-
- // Initialise the worker process' semaphore
- ctx->fwcfg->thrdata.semp_worker = sem_open("eurephia_fwworker", O_CREAT, 0666, 0);
- if( ctx->fwcfg->thrdata.semp_worker == SEM_FAILED ) {
- eurephia_log(ctx, LOG_FATAL, 0, "Could setup semaphore in eFW_RunFirewall: %s", strerror(errno));
- return;
- }
-
+ };
- // Prepare a POSIX Message Queue
- mqattr.mq_maxmsg = 10;
- mqattr.mq_msgsize = 1024;
- mqattr.mq_flags = 0;
- (*ctx->fwcfg).thrdata.msgq = mq_open(MQUEUE_NAME, O_RDWR | O_CREAT, 0600, &mqattr);
- if( (*ctx->fwcfg).thrdata.msgq < 0 ) {
- eurephia_log(ctx, LOG_FATAL, 0, "Could not open message queue: %s", strerror(errno));
+ // Setup a message queue
+ if( efwSetupMessageQueue(ctx, &(*ctx->fwcfg).thrdata) == 0 ) {
free_nullsafe(ctx->fwcfg);
return;
}
@@ -174,9 +156,6 @@ void eFW_StartFirewall(eurephiaCTX *ctx) {
ctx->fwcfg->fwproc_pid);
}
- // Let child have time to settle
- sleep(2);
-
// Flush the message queue for old messages
if( mq_getattr((*ctx->fwcfg).thrdata.msgq, &mqattr) == 0 ) {
long i;
@@ -235,23 +214,6 @@ void eFW_StopFirewall(eurephiaCTX *ctx) {
kill(ctx->fwcfg->fwproc_pid, SIGABRT);
}
- // Close and remove the message queue used
- if( mq_close((*ctx->fwcfg).thrdata.msgq) != 0 ) {
- eurephia_log(ctx, LOG_WARNING, 0, "Could not do close the message queue used for eFW: %s",
- strerror(errno));
- }
-
- if( mq_unlink(MQUEUE_NAME) != 0 ) {
- eurephia_log(ctx, LOG_WARNING, 0, "Could not do close the message queue used for eFW: %s",
- strerror(errno));
- }
-
- // Take down the semaphore we used
- if( sem_close(ctx->fwcfg->thrdata.semp_master) != 0 ) {
- eurephia_log(ctx, LOG_WARNING, 0, "Could not do destroy the driver semaphore for eFW: %s",
- strerror(errno));
- }
- sem_unlink("eurephia_fwmaster");
free_nullsafe((*ctx->fwcfg).thrdata.fw_command);
free_nullsafe(ctx->fwcfg);
eurephia_log(ctx, LOG_INFO, 3, "eurephia firewall interface is stopped");
diff --git a/eurephiafw_helpers.c b/eurephiafw_helpers.c
new file mode 100644
index 0000000..dd1ff52
--- /dev/null
+++ b/eurephiafw_helpers.c
@@ -0,0 +1,96 @@
+/* eurephiafw_helpers.c --
+ *
+ * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; version 2
+ * of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <signal.h>
+#include <unistd.h>
+#include <semaphore.h>
+#include <mqueue.h>
+
+#include "eurephia_struct.h"
+#include "eurephia_log.h"
+#include "eurephiafw.h"
+#include "eurephiafw_helpers.h"
+
+
+int efwSetupSemaphores(eurephiaCTX *ctx, efw_threaddata *cfg) {
+ // Initialise the main process' semaphore
+ cfg->semp_master = sem_open(SEMPH_MASTER, O_CREAT, 0666, 0);
+ if( cfg->semp_master == SEM_FAILED ) {
+ eurephia_log(ctx, LOG_FATAL, 0, "Could not setup semaphore for eFW master: %s", strerror(errno));
+ return 0;
+ }
+
+ // Initialise the worker process' semaphore
+ cfg->semp_worker = sem_open(SEMPH_WORKER, O_CREAT, 0666, 0);
+ if( cfg->semp_worker == SEM_FAILED ) {
+ eurephia_log(ctx, LOG_FATAL, 0, "Could not setup semaphore for eFW worker: %s", strerror(errno));
+ return 0;
+ }
+ return 1;
+}
+
+int efwRemoveSemaphores(eurephiaCTX *ctx, efw_threaddata *cfg) {
+ if( sem_close(cfg->semp_worker) != 0 ) {
+ eurephia_log(ctx, LOG_CRITICAL, 0,
+ "eFW: Could not destroy semaphore for worker: %s", strerror(errno));
+ }
+ sem_unlink(SEMPH_WORKER);
+
+ if( sem_close(cfg->semp_master) != 0 ) {
+ eurephia_log(ctx, LOG_CRITICAL, 0,
+ "eFW: Could not destroy semaphore for master: %s", strerror(errno));
+ }
+ sem_unlink(SEMPH_MASTER);
+ return 1;
+}
+
+int efwSetupMessageQueue(eurephiaCTX *ctx, efw_threaddata *cfg) {
+ struct mq_attr mqattr;
+
+ // Prepare a POSIX Message Queue
+ mqattr.mq_maxmsg = 10;
+ mqattr.mq_msgsize = EFW_MSG_SIZE;
+ mqattr.mq_flags = 0;
+ cfg->msgq = mq_open(MQUEUE_NAME, O_RDWR | O_CREAT, 0600, &mqattr);
+ if( cfg->msgq < 0 ) {
+ eurephia_log(ctx, LOG_FATAL, 0, "Could not open message queue: %s", strerror(errno));
+ return 0;
+ }
+ return 1;
+}
+
+int efwRemoveMessageQueue(eurephiaCTX *ctx, efw_threaddata *cfg) {
+ // Close and remove the message queue used
+ if( mq_close((*cfg).msgq) != 0 ) {
+ eurephia_log(ctx, LOG_WARNING, 0, "Could not do close the message queue used for eFW: %s",
+ strerror(errno));
+ }
+
+ if( mq_unlink(MQUEUE_NAME) != 0 ) {
+ eurephia_log(ctx, LOG_WARNING, 0, "Could not do close the message queue used for eFW: %s",
+ strerror(errno));
+ }
+ return 1;
+}
+
diff --git a/eurephiafw_helpers.h b/eurephiafw_helpers.h
new file mode 100644
index 0000000..30459b0
--- /dev/null
+++ b/eurephiafw_helpers.h
@@ -0,0 +1,35 @@
+/* eurephiafw_helpers.h --
+ *
+ * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; version 2
+ * of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+
+#ifndef EUREPHIAFW_HELPERS_H_
+#define EUREPHIAFW_HELPERS_H_
+
+#define EFW_MSG_SIZE 1024
+#define MQUEUE_NAME "/eurephiaFW"
+#define SEMPH_MASTER "eurephiafw_master"
+#define SEMPH_WORKER "eurephiafw_worker"
+
+int efwSetupSemaphores(eurephiaCTX *, efw_threaddata *);
+int efwRemoveSemaphores(eurephiaCTX *, efw_threaddata *);
+
+int efwSetupMessageQueue(eurephiaCTX *, efw_threaddata *);
+int efwRemoveMessageQueue(eurephiaCTX *, efw_threaddata *);
+
+#endif /* !EUREPHIAFW_HELPERS_H_ */
diff --git a/firewall/iptables/Makefile b/firewall/iptables/Makefile
index 035c3d1..1491abc 100644
--- a/firewall/iptables/Makefile
+++ b/firewall/iptables/Makefile
@@ -3,7 +3,7 @@ topdir = ../..
INTERFACEVER=1.0
INTERFACEAPIVER=1
-objs = efw_iptables.o ../../eurephia_log.o
+objs = efw_iptables.o ../../eurephia_log.o ../../eurephiafw_helpers.o
interface = efw_iptables.so
diff --git a/firewall/iptables/efw_iptables.c b/firewall/iptables/efw_iptables.c
index 05ac17c..911ecca 100644
--- a/firewall/iptables/efw_iptables.c
+++ b/firewall/iptables/efw_iptables.c
@@ -30,8 +30,8 @@
#include <eurephia_nullsafe.h>
#include <eurephia_log.h>
#include <eurephia_struct.h>
+#include <eurephiafw_helpers.h>
-#define EFW_MSG_SIZE 1024
const char *eFWinterfaceVersion() {
return "eFW-iptables (v"INTERFACEVER") David Sommerseth 2008 (C) GPLv2";
@@ -87,12 +87,8 @@ void eFW_RunFirewall(void *fwargs) {
}
}
- if( sem_close(cfg->semp_worker) != 0 ) {
- eurephia_log(ctx, LOG_CRITICAL, 0,
- "eFW_RunFirewall: Could not destroy semaphore: %s", strerror(errno));
- }
- sem_unlink("eurephia_fwworker");
-
+ efwRemoveSemaphores(ctx, fwargs);
+ efwRemoveMessageQueue(ctx, fwargs);
exit(0);
}