Repository :
http://git.fedorahosted.org/cgit/firewalld-recode.git
On branch : master
---------------------------------------------------------------
commit 5c8b1fbb1a2e8de694802238708accc85f1599d6
Author: Daniel Kopecek <dkopecek(a)redhat.com>
Date: Tue Oct 14 14:46:37 2014 +0200
Added a firewall backend abstraction class implementation
---------------------------------------------------------------
src/Daemon/Firewall/Backend.cpp | 64 ++++++++++++++++++++
.../Linux/Sysctl.hpp => Firewall/Backend.hpp} | 35 ++++++-----
2 files changed, 83 insertions(+), 16 deletions(-)
diff --git a/src/Daemon/Firewall/Backend.cpp b/src/Daemon/Firewall/Backend.cpp
new file mode 100644
index 0000000..73287b1
--- /dev/null
+++ b/src/Daemon/Firewall/Backend.cpp
@@ -0,0 +1,64 @@
+//
+// Copyright (C) 2014 Red Hat, Inc.
+//
+// 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; either version 2 of the License, or
+// (at your option) any later version.
+//
+// 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, see <
http://www.gnu.org/licenses/>.
+//
+// Authors: Daniel Kopecek <dkopecek(a)redhat.com>
+//
+#include "Common/Types.hpp"
+#include "Daemon/Firewall/Transaction.hpp"
+#include "Backend.hpp"
+#include <mutex>
+
+namespace firewalld
+{
+
+ Pointer<Transaction> Backend::createTransaction()
+ {
+ std::lock_guard<std::mutex> backend_lock(_backend_mutex);
+ Pointer<Transaction> transaction =
makePointer<Transaction>("");
+ _transactions.push_back(transaction);
+ return transaction;
+ }
+
+ void Backend::commitTransaction(Pointer<Transaction> transaction)
+ {
+ std::lock_guard<std::mutex> backend_lock(_backend_mutex);
+
+ try {
+ for (size_t i = 0; i < transaction->countOperations(); ++i) {
+ processOperation(transaction->operation(i));
+ }
+ } catch(const TransactionError& ex) {
+ // Undo completed operations
+ if (ex.rollbackNeeded()) {
+ size_t i = ex.rollbackFromIndex();
+ while (i > 1) {
+ processOperation(transaction->operation(i), /*undo=*/true);
+ --i;
+ }
+ }
+ throw;
+ }
+
+ return;
+ }
+
+ size_t Backend::countTransactions() const
+ {
+ std::lock_guard<std::mutex> backend_lock(_backend_mutex);
+ return _transactions.size();
+ }
+
+} /* namespace firewalld */
diff --git a/src/Daemon/Backends/Linux/Sysctl.hpp b/src/Daemon/Firewall/Backend.hpp
similarity index 56%
copy from src/Daemon/Backends/Linux/Sysctl.hpp
copy to src/Daemon/Firewall/Backend.hpp
index 437be95..58298e8 100644
--- a/src/Daemon/Backends/Linux/Sysctl.hpp
+++ b/src/Daemon/Firewall/Backend.hpp
@@ -16,31 +16,34 @@
//
// Authors: Daniel Kopecek <dkopecek(a)redhat.com>
//
-#ifndef _FIREWALLD_SYSCTL_HPP_
-#define _FIREWALLD_SYSCTL_HPP_
+#ifndef FIREWALLD_FIREWALL_BACKEND_HPP
+#define FIREWALLD_FIREWALL_BACKEND_HPP
#include "Common/Types.hpp"
+#include "Daemon/Firewall/Transaction.hpp"
+
+#include <cstddef>
+#include <mutex>
namespace firewalld
{
-
- class Sysctl
+ class Backend
{
- char _procfs_path[PATH_MAX];
- int _procfs_fd;
- bool _procfs_fd_read_only;
- bool _procfs_fd_close_on_unref;
+ public:
+ virtual const UString& identifier() const = 0;
+ Pointer<Transaction> createTransaction();
+ void commitTransaction(Pointer<Transaction> transaction);
+ void removeTransaction(const UString& identifier);
+ size_t countTransactions() const;
- bool open(bool read_only = true);
- void close(bool force_close = false);
+ protected:
+ virtual void processOperation(const UStringList& op, bool undo = false) = 0;
- public:
- Sysctl(const char *name, bool close_on_unref = false);
- ~Sysctl();
- bool set(const char *value);
- bool get(char *buffer, size_t buflen);
+ private:
+ PointerVector<Transaction> _transactions;
+ mutable std::mutex _backend_mutex;
};
} /* namespace firewalld */
-#endif /* _FIREWALLD_SYSCTL_HPP_ */
+#endif /* FIREWALLD_FIREWALL_BACKEND_HPP */