Merge branch 'ckm' into tizen
[platform/core/test/security-tests.git] / src / common / service_manager.h
1 /*
2  * Copyright (c) 2013-2014 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        service_manager.h
18  * @author      Zbigniew Jasinski <z.jasinski@samsung.com>
19  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
20  * @author      Marcin Niesluchowski <m.niesluchow@samsungcom>
21  * @version     1.1
22  * @brief       Declaration of service control class using dbus interface to communicate with systemd
23  */
24
25 #ifndef COMMON_SERVICE_MANAGER_H
26 #define COMMON_SERVICE_MANAGER_H
27
28 #include <dbus/dbus.h>
29
30 #include <dbus_connection.h>
31 #include <dbus_message_in.h>
32 #include <dbus_message_out.h>
33
34 #include <cstdint>
35 #include <set>
36 #include <string>
37
38 class ServiceManager {
39 public:
40     ServiceManager() = delete;
41     ServiceManager(const std::string &serviceName,
42                    const std::vector<std::string>& socketsNames = {});
43     ~ServiceManager() = default;
44
45     void startService(bool withSockets = false);
46     void stopService(bool withSockets = false);
47     void restartService(bool withSockets = false);
48     pid_t getServicePid();
49     timeval getServiceStartTimestamp();
50     void maskService();
51     void unmaskService();
52
53 private:
54     void addBusMatch(const std::string &member);
55     void subscribeSignals();
56     void reloadDbusManager();
57     void getUnitPath();
58     DBus::MessageOut newMethodCall(const std::string &method);
59     std::string handleObjectPathMsgReply(DBus::MessageIn &messageIn);
60     uint32_t handleVariantUIntMsgReply(DBus::MessageIn &messageIn);
61     uint64_t handleVariantUInt64MsgReply(DBus::MessageIn &messageIn);
62
63     void sendToService(const std::string &method, const std::string &unit);
64     void sendMaskToService();
65     void sendUnmaskToService();
66     DBus::MessageIn sendPropertyGetMsg(const std::string &interface, const std::string &property);
67     uint32_t getUIntProperty(const std::string &interface, const std::string &property);
68     uint64_t getUInt64Property(const std::string &interface, const std::string &property);
69     void sendResetFailedToService();
70
71     static DBusHandlerResult messageHandler(DBusConnection *conn, DBusMessage *msg, void *t);
72     void signalJobRemovedHandler(DBus::MessageIn &messageIn);
73     void signalJobNewHandler(DBus::MessageIn &messageIn);
74     void signalReloadingHandler(DBus::MessageIn &messageIn);
75     void waitForRunningJobsFinish();
76
77     void executeMethod(const std::string &method, const std::string &unit);
78
79     DBus::Connection m_connection;
80
81     const std::string m_serviceName;
82     const std::vector<std::string> m_socketsNames;
83     std::string m_unitPath;
84
85     std::set<std::string> m_runningJobs;
86 };
87
88 #endif // COMMON_SERVICE_MANAGER_H