Add debug compilation mode
[platform/core/security/nice-lad.git] / src / main.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file        src/main.cpp
18  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
19  * @version     1.0
20  */
21
22 #include <csignal>
23 #include <cstdlib>
24 #include <memory>
25
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>
34 #include <Log/log.h>
35 #include <Utils/Feed.h>
36 #include <Utils/SignalFd.h>
37 #include <Utils/WithMessageException.h>
38
39 int main(int argc, char **argv) {
40     using std::placeholders::_1;
41     using std::placeholders::_2;
42
43     init_log();
44
45     LOGI("Starting nice-lad");
46
47     try {
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 });
56
57         Utils::Feed feed(STDIN_FILENO, auditApi.MAX_AUDIT_MESSAGE_LENGTH_CONST(), sigFd);
58
59         feed.onData.connect(std::bind(&Audit::Parser::feed, &auParser, _1, _2));
60
61         feed.onTimeout.connect(std::bind(&Audit::Parser::flush, &auParser));
62
63         feed.onEod.connect([&auParser] (void) {
64             auParser.flush();
65             LOGI("End of data. Terminating.");
66         });
67
68         feed.onSignal.connect([&feed] (int sigFd) {
69             const auto sigNo = Utils::SignalFd::readSignalNo(sigFd);
70             if (sigNo == SIGTERM) {
71                 LOGI("Got SIGTERM (Terminating)");
72                 feed.stop();
73             } else if (sigNo == SIGHUP) {
74                 LOGI("Got SIGHUP (Reloading configuration)");
75             } else {
76                 LOGW("Unexpected signal (" << sigNo << ")");
77             }
78         });
79
80         Lad::AuditEventHandler eventHandler;
81         auParser.onEvent.connect(std::bind(&Lad::AuditEventHandler::handleEvent, &eventHandler, _1));
82
83         eventHandler.onLogDenial.connect(std::bind(&Lad::DataCollector::log, &dataCollector, _1));
84
85         LOGD("nice-lad up and ready");
86
87        feed.start();
88     } catch (const std::exception &ex) {
89         LOGC(ex.what() << " (Terminating)");
90         return EXIT_FAILURE;
91     } catch (...) {
92         LOGC("Unknown error (Terminating)");
93         return EXIT_FAILURE;
94     }
95
96     return EXIT_SUCCESS;
97 }