From 7f4d5396c2268df02068fef37b92cd0328a2b232 Mon Sep 17 00:00:00 2001 From: Leonardo Grasso Date: Tue, 22 Sep 2020 17:45:13 +0200 Subject: [PATCH] new(userspace/falco): program output C++ impl Signed-off-by: Leonardo Grasso --- userspace/falco/falco_outputs_program.cpp | 66 +++++++++++++++++++++++ userspace/falco/falco_outputs_program.h | 44 +++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 userspace/falco/falco_outputs_program.cpp create mode 100644 userspace/falco/falco_outputs_program.h diff --git a/userspace/falco/falco_outputs_program.cpp b/userspace/falco/falco_outputs_program.cpp new file mode 100644 index 00000000..838d5d60 --- /dev/null +++ b/userspace/falco/falco_outputs_program.cpp @@ -0,0 +1,66 @@ +/* +Copyright (C) 2020 The Falco Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "falco_outputs_program.h" +#include +#include "banned.h" // This raises a compilation error when certain functions are used + +void falco::outputs::output_program::open_pfile() +{ + + if(m_pfile == nullptr) + { + m_pfile = popen(m_oc.options["program"].c_str(), "w"); + // todo(leogr): handle errno + } + + // if(!m_buffered) + // { + // m_pipe.rdbuf()->pubsetbuf(0, 0); + // } +} + +void falco::outputs::output_program::output_event(gen_event *evt, std::string &rule, std::string &source, + falco_common::priority_type priority, std::string &format, std::string &msg) +{ + output_msg(priority, msg); +} + +void falco::outputs::output_program::output_msg(falco_common::priority_type priority, std::string &msg) +{ + open_pfile(); + fprintf(m_pfile, "%s\n", msg.c_str()); + + if(m_oc.options["keep_alive"] != "true") + { + cleanup(); + } +} + +void falco::outputs::output_program::cleanup() +{ + if(m_pfile != nullptr) + { + fflush(m_pfile); + fclose(m_pfile); + } +} + +void falco::outputs::output_program::reopen() +{ + cleanup(); + open_pfile(); +} diff --git a/userspace/falco/falco_outputs_program.h b/userspace/falco/falco_outputs_program.h new file mode 100644 index 00000000..402b1e0d --- /dev/null +++ b/userspace/falco/falco_outputs_program.h @@ -0,0 +1,44 @@ +/* +Copyright (C) 2020 The Falco Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#pragma once + +#include "falco_output.h" + +namespace falco +{ +namespace outputs +{ + +class output_program : public output +{ + void output_event(gen_event *evt, std::string &rule, std::string &source, + falco_common::priority_type priority, std::string &format, std::string &msg); + + void output_msg(falco_common::priority_type priority, std::string &msg); + + void cleanup(); + + void reopen(); + +private: + void open_pfile(); + + FILE *m_pfile; +}; + +} // namespace outputs +} // namespace falco