2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <system_info.h>
20 #include <types_internal.h>
21 #include "zone_util_impl.h"
25 static bool container_enabled = false;
26 static vsm_context_h _vsm_ctx = NULL;
27 static ctx::zone_util_impl* _instance = NULL;
29 const char* ctx::zone_util_impl::default_zone()
31 if (container_enabled)
32 return VSM_DEFAULT_ZONE;
37 void* ctx::zone_util_impl::join_by_name(const char* name)
39 IF_FAIL_RETURN(container_enabled, NULL);
40 IF_FAIL_RETURN_TAG(_vsm_ctx, NULL, _E, "Not initialized");
43 _D("NULL zone name. The default zone will be used.");
44 name = VSM_DEFAULT_ZONE;
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"));
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);
54 _I(YELLOW("Joining to '%s'"), name);
55 return vsm_join_zone(target_zone);
58 void* ctx::zone_util_impl::join_to_zone(void* zone)
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);
68 bool ctx::zone_util::init()
70 system_info_get_platform_bool("tizen.org/feature/container", &container_enabled);
71 IF_FAIL_RETURN_TAG(_instance == NULL, true, _W, "Re-initialization");
73 _instance = new(std::nothrow) zone_util_impl();
74 IF_FAIL_RETURN_TAG(_instance, false, _E, "Memory allocation failed");
76 if (container_enabled) {
77 _vsm_ctx = vsm_create_context();
80 _E("Memory allocation failed");
85 zone_util::set_instance(_instance);
89 void ctx::zone_util::release()
91 zone_util::set_instance(NULL);
97 vsm_cleanup_context(_vsm_ctx);
102 const char* ctx::zone_util::get_name_by_pid(pid_t pid)
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);