2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Aleksander Zdyb <a.zdyb@samsung.com>
26 #include <Audit/Auditctl.h>
27 #include <Audit/AuditWrapper.h>
28 #include <Audit/AuparseSourceFeedWrapper.h>
29 #include <Audit/ErrorException.h>
30 #include <Audit/Parser.h>
31 #include <Lad/AuditEventHandler.h>
32 #include <Lad/AuditRulesPopulator.h>
33 #include <Lad/Options.h>
35 #include <Utils/Feed.h>
36 #include <Utils/SignalFd.h>
37 #include <Utils/WithMessageException.h>
39 int main(int argc, char **argv) {
40 using std::placeholders::_1;
41 using std::placeholders::_2;
45 LOGI("Starting nice-lad");
48 Audit::AuditWrapper auditApi;
49 Audit::AuparseSourceFeedWrapper auparseApi;
50 Audit::Parser auParser(auparseApi);
51 Audit::Auditctl auditctl(auditApi);
52 auto &dataProvider = Lad::Options::dataProvider();
53 auto &dataCollector = Lad::Options::dataCollector();
54 Lad::AuditRulesPopulator rulesPopulator(auditctl, dataProvider);
55 int sigFd = Utils::SignalFd::createSignalFd({ SIGHUP, SIGTERM });
57 Utils::Feed feed(STDIN_FILENO, auditApi.MAX_AUDIT_MESSAGE_LENGTH_CONST(), sigFd);
59 feed.onData.connect(std::bind(&Audit::Parser::feed, &auParser, _1, _2));
61 feed.onTimeout.connect(std::bind(&Audit::Parser::flush, &auParser));
63 feed.onEod.connect([&auParser] (void) {
65 LOGI("End of data. Terminating.");
68 feed.onSignal.connect([&feed] (int sigFd) {
69 const auto sigNo = Utils::SignalFd::readSignalNo(sigFd);
70 if (sigNo == SIGTERM) {
71 LOGI("Got SIGTERM (Terminating)");
73 } else if (sigNo == SIGHUP) {
74 LOGI("Got SIGHUP (Reloading configuration)");
76 LOGW("Unexpected signal (" << sigNo << ")");
80 Lad::AuditEventHandler eventHandler;
81 auParser.onEvent.connect(std::bind(&Lad::AuditEventHandler::handleEvent, &eventHandler, _1));
83 eventHandler.onLogDenial.connect(std::bind(&Lad::DataCollector::log, &dataCollector, _1));
85 LOGD("nice-lad up and ready");
88 } catch (const std::exception &ex) {
89 LOGC(ex.what() << " (Terminating)");
92 LOGC("Unknown error (Terminating)");