Creating netdev
[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 #include <sys/types.h>
30
31 // fwd declaration of lxc internals
32 struct lxc_container;
33
34 namespace vasum {
35 namespace lxc {
36
37
38 /**
39  * A class wrapping lxc container
40  */
41 class LxcZone {
42 public:
43     enum class State {
44         STOPPED,
45         STARTING,
46         RUNNING,
47         STOPPING,
48         ABORTING,
49         FREEZING,
50         FROZEN,
51         THAWED
52     };
53
54     /**
55      * LxcZone constructor
56      * @param lxcPath path where zones lives
57      * @param zoneName name of zone
58      */
59     LxcZone(const std::string& lxcPath, const std::string& zoneName);
60     ~LxcZone();
61
62     LxcZone(const LxcZone&) = delete;
63     LxcZone& operator=(const LxcZone&) = delete;
64
65     /**
66      * Get zone name
67      */
68     std::string getName() const;
69
70     /**
71      * Get item from lxc config file
72      * @throw LxcException if key not found
73      */
74     std::string getConfigItem(const std::string& key);
75
76     /**
77      * Is zone defined (created)?
78      */
79     bool isDefined();
80
81     /**
82      * String representation of state
83      */
84     static std::string toString(State state);
85
86     /**
87      * Get zone state
88      */
89     State getState();
90
91     /**
92      * Wait till zone is in specified state
93      * @return false on timeout
94      */
95     bool waitForState(State state, int timeout);
96
97     /**
98      * Create zone
99      * @param templatePath template from which zone will be created
100      * @param argv additional template arguments
101      */
102     bool create(const std::string& templatePath, const char* const* argv);
103
104     /**
105      * Destroy zone
106      */
107     bool destroy();
108
109     /**
110      * Start zone
111      * @param argv init process with arguments
112      */
113     bool start(const char* const* argv);
114
115     /**
116      * Immediate stop the zone
117      * It kills all processes within this zone
118      */
119     bool stop();
120
121     /**
122      * Reboot zone
123      */
124     bool reboot();
125
126     /**
127      * Gracefully shutdown zone.
128      */
129     bool shutdown(int timeout);
130
131     /**
132      * Freeze (pause/lock) zone
133      */
134     bool freeze();
135
136     /**
137      * Unfreeze zone
138      */
139     bool unfreeze();
140
141     /**
142      * Get pid of init process
143      */
144     pid_t getInitPid() const;
145
146 private:
147     lxc_container* mLxcContainer;
148
149     bool setRunLevel(int runLevel);
150     void refresh();
151 };
152
153
154 } // namespace lxc
155 } // namespace vasum
156
157
158 #endif // COMMON_LXC_ZONE_HPP