Migrate from 2.4 code repo
[platform/core/context/context-service.git] / src / zone_util_impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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 #include <unistd.h>
18 #include <vasum.h>
19 #include <system_info.h>
20 #include <types_internal.h>
21 #include "zone_util_impl.h"
22
23 #define HOST_NAME ""
24
25 static bool container_enabled = false;
26 static vsm_context_h _vsm_ctx = NULL;
27 static ctx::zone_util_impl* _instance = NULL;
28
29 const char* ctx::zone_util_impl::default_zone()
30 {
31         if (container_enabled)
32                 return VSM_DEFAULT_ZONE;
33
34         return HOST_NAME;
35 }
36
37 void* ctx::zone_util_impl::join_by_name(const char* name)
38 {
39         IF_FAIL_RETURN(container_enabled, NULL);
40         IF_FAIL_RETURN_TAG(_vsm_ctx, NULL, _E, "Not initialized");
41
42         if (name == NULL) {
43                 _D("NULL zone name. The default zone will be used.");
44                 name = VSM_DEFAULT_ZONE;
45         }
46
47         vsm_zone_h target_zone = vsm_lookup_zone_by_name(_vsm_ctx, name);
48         IF_FAIL_RETURN_TAG(target_zone, NULL, _E, RED("Zone lookup failed"));
49
50         vsm_zone_h current_zone = vsm_lookup_zone_by_pid(_vsm_ctx, getpid());
51         IF_FAIL_RETURN_TAG(current_zone, NULL, _E, RED("Zone lookup failed"));
52         IF_FAIL_RETURN_TAG(target_zone != current_zone, NULL, _I, YELLOW("Already in the target zone %s"), name);
53
54         _I(YELLOW("Joining to '%s'"), name);
55         return vsm_join_zone(target_zone);
56 }
57
58 void* ctx::zone_util_impl::join_to_zone(void* zone)
59 {
60         IF_FAIL_RETURN(container_enabled, NULL);
61         IF_FAIL_RETURN_TAG(_vsm_ctx, NULL, _E, "Not initialized");
62         IF_FAIL_RETURN(zone, NULL);
63         vsm_zone_h target = static_cast<vsm_zone_h>(zone);
64         _I(YELLOW("Joining to '%s'"), vsm_get_zone_name(target));
65         return vsm_join_zone(target);
66 }
67
68 bool ctx::zone_util::init()
69 {
70         system_info_get_platform_bool("tizen.org/feature/container", &container_enabled);
71         IF_FAIL_RETURN_TAG(_instance == NULL, true, _W, "Re-initialization");
72
73         _instance = new(std::nothrow) zone_util_impl();
74         IF_FAIL_RETURN_TAG(_instance, false, _E, "Memory allocation failed");
75
76         if (container_enabled) {
77                 _vsm_ctx = vsm_create_context();
78                 if (!_vsm_ctx) {
79                         delete _instance;
80                         _E("Memory allocation failed");
81                         return false;
82                 }
83         }
84
85         zone_util::set_instance(_instance);
86         return true;
87 }
88
89 void ctx::zone_util::release()
90 {
91         zone_util::set_instance(NULL);
92
93         delete _instance;
94         _instance = NULL;
95
96         if (_vsm_ctx)
97                 vsm_cleanup_context(_vsm_ctx);
98
99         _vsm_ctx = NULL;
100 }
101
102 const char* ctx::zone_util::get_name_by_pid(pid_t pid)
103 {
104         IF_FAIL_RETURN(container_enabled, HOST_NAME);
105         IF_FAIL_RETURN_TAG(_vsm_ctx, NULL, _E, "Not initialized");
106         vsm_zone_h zn = vsm_lookup_zone_by_pid(_vsm_ctx, pid);
107         return vsm_get_zone_name(zn);
108 }