1573c7d2fad90fb66af560828ee3974bae268119
[platform/core/security/vasum.git] / common / lxc / zone.hpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Piotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19 /**
20  * @file
21  * @author  Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22  * @brief   Lxc zone
23  */
24
25 #ifndef COMMON_LXC_ZONE_HPP
26 #define COMMON_LXC_ZONE_HPP
27
28 #include <string>
29
30 // fwd declaration of lxc internals
31 struct lxc_container;
32
33 namespace vasum {
34 namespace lxc {
35
36
37 /**
38  * A class wrapping lxc container
39  */
40 class LxcZone {
41 public:
42     enum class State {
43         STOPPED,
44         STARTING,
45         RUNNING,
46         STOPPING,
47         ABORTING,
48         FREEZING,
49         FROZEN,
50         THAWED
51     };
52
53     /**
54      * LxcZone constructor
55      * @param lxcPath path where zones lives
56      * @param zoneName name of zone
57      */
58     LxcZone(const std::string& lxcPath, const std::string& zoneName);
59     ~LxcZone();
60
61     LxcZone(const LxcZone&) = delete;
62     LxcZone& operator=(const LxcZone&) = delete;
63
64     /**
65      * Get zone name
66      */
67     std::string getName() const;
68
69     /**
70      * Get item from lxc config file
71      * @throw LxcException if key not found
72      */
73     std::string getConfigItem(const std::string& key);
74
75     /**
76      * Is zone defined (created)?
77      */
78     bool isDefined();
79
80     /**
81      * String representation of state
82      */
83     static std::string toString(State state);
84
85     /**
86      * Get zone state
87      */
88     State getState();
89
90     /**
91      * Wait till zone is in specified state
92      * @return false on timeout
93      */
94     bool waitForState(State state, int timeout);
95
96     /**
97      * Create zone
98      * @param templatePath template from which zone will be created
99      * @param argv additional template arguments
100      */
101     bool create(const std::string& templatePath, const char* const* argv);
102
103     /**
104      * Destroy zone
105      */
106     bool destroy();
107
108     /**
109      * Start zone
110      * @param argv init process with arguments
111      */
112     bool start(const char* const* argv);
113
114     /**
115      * Immediate stop the zone
116      * It kills all processes within this zone
117      */
118     bool stop();
119
120     /**
121      * Reboot zone
122      */
123     bool reboot();
124
125     /**
126      * Gracefully shutdown zone.
127      */
128     bool shutdown(int timeout);
129
130     /**
131      * Freeze (pause/lock) zone
132      */
133     bool freeze();
134
135     /**
136      * Unfreeze zone
137      */
138     bool unfreeze();
139 private:
140     lxc_container* mLxcContainer;
141
142     bool setRunLevel(int runLevel);
143     void refresh();
144 };
145
146
147 } // namespace lxc
148 } // namespace vasum
149
150
151 #endif // COMMON_LXC_ZONE_HPP