+
+#define ZONE_PREFIX_PATH "/var/lib/lxc"
+#define ZONE_INFIX_PATH "/rootfs"
+
+static inline int __read_proc(const char *path, char *buf, int size)
+{
+ int fd = 0;
+ int ret = 0;
+
+ if (buf == NULL || path == NULL)
+ return -1;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ return -1;
+
+ ret = read(fd, buf, size - 1);
+ if (ret <= 0) {
+ close(fd);
+ return -1;
+ } else
+ buf[ret] = 0;
+
+ close(fd);
+
+ return ret;
+}
+
+static char *__proc_get_cmdline_bypid(int pid)
+{
+ char buf[1024] = {'\0', };
+ int ret = 0;
+
+ snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
+ ret = __read_proc(buf, buf, sizeof(buf));
+ if (ret <= 0)
+ return NULL;
+
+ /* support app launched by shell script*/
+ if (strncmp(buf, "/bin/sh", 7) == 0)
+ return strdup(&buf[7 + 1]);
+ else
+ return strdup(buf);
+}
+
+int __get_appid_func(const pkgmgrinfo_appinfo_h handle, void *user_data)
+{
+ int ret = -1;
+ char *appid;
+ char **return_appid = (char **)user_data;
+ ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
+ ALARM_MGR_LOG_PRINT("appid %s.", appid);
+ if (ret < 0) {
+ ALARM_MGR_EXCEPTION_PRINT("Failed to get appid\n");
+ } else {
+ *return_appid = strdup(appid);
+ ret = 0;
+ }
+
+ return ret;
+}
+
+static int __zone_get_appid_bypid(int pid, const char *zone, char **appid)
+{
+ int ret = -1;
+ char cmdline[PATH_MAX];
+ char *appid_tmp = NULL;
+ int i = 0;
+
+ ret = aul_app_get_cmdline_bypid(pid, cmdline, PATH_MAX);
+ if (ret != 0) {
+ ALARM_MGR_EXCEPTION_PRINT("get_cmdline_bypid is failed\n");
+ return -1;
+ }
+
+ ALARM_MGR_LOG_PRINT("cmdline(%s)", cmdline);
+
+ if (strncmp(zone, "/", 1) == 0) {
+ pkgmgrinfo_pkginfo_set_zone(NULL, NULL, 0);
+ } else {
+ pkgmgrinfo_pkginfo_set_zone(zone, NULL, 0);
+ }
+
+ pkgmgrinfo_appinfo_filter_h handle;
+ ret = pkgmgrinfo_appinfo_filter_create(&handle);
+ if (ret < 0) {
+ ALARM_MGR_EXCEPTION_PRINT("appinfo filter handle create failed\n");
+ return -1;
+ }
+
+ ret = pkgmgrinfo_appinfo_filter_add_string(handle,
+ PMINFO_APPINFO_PROP_APP_EXEC, cmdline);
+ if (ret < 0) {
+ ALARM_MGR_EXCEPTION_PRINT("pkgmgrinfo_appinfo_filter_add_string() failed: exec(%s)", cmdline);
+ goto catch;
+ }
+
+ ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(handle, __get_appid_func, &appid_tmp);
+ if (ret < 0) {
+ ALARM_MGR_EXCEPTION_PRINT("pkgmgrinfo_appinfo_filter_foreach_appinfo() failed\n");
+ goto catch;
+ }
+
+ if (appid_tmp) {
+ ALARM_MGR_LOG_PRINT("appid_tmp(%s)", appid_tmp);
+ ret = 0;
+ } else
+ ret = -1;
+
+ *appid = appid_tmp;
+
+catch:
+ pkgmgrinfo_appinfo_filter_destroy(handle);
+
+ return ret;
+}
+
+int __get_pkgid_func(const pkgmgrinfo_appinfo_h handle, void *user_data)
+{
+ int ret = -1;
+ char *pkgid;
+ char **return_pkgid = (char **)user_data;
+ ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
+ ALARM_MGR_LOG_PRINT("pkgid %s.", pkgid);
+ if (ret < 0) {
+ ALARM_MGR_EXCEPTION_PRINT("Failed to get pkgid\n");
+ } else {
+ *return_pkgid = strdup(pkgid);
+ ret = 0;
+ }
+
+ return ret;
+}
+
+static int __zone_get_pkgid_bypid(int pid, const char *zone, char **pkgid)
+{
+ int ret = -1;
+ char cmdline[PATH_MAX];
+ char *pkgid_tmp = NULL;
+ int i = 0;
+
+ ret = aul_app_get_cmdline_bypid(pid, cmdline, PATH_MAX);
+ if (ret != 0) {
+ ALARM_MGR_EXCEPTION_PRINT("get_cmdline_bypid is failed\n");
+ return -1;
+ }
+
+ ALARM_MGR_LOG_PRINT("cmdline(%s)", cmdline);
+
+ if (strncmp(zone, "/", 1) == 0) {
+ pkgmgrinfo_pkginfo_set_zone(NULL, NULL, 0);
+ } else {
+ pkgmgrinfo_pkginfo_set_zone(zone, NULL, 0);
+ }
+
+ pkgmgrinfo_appinfo_filter_h handle;
+ ret = pkgmgrinfo_appinfo_filter_create(&handle);
+ if (ret < 0) {
+ ALARM_MGR_EXCEPTION_PRINT("appinfo filter handle create failed\n");
+ return -1;
+ }
+
+ ret = pkgmgrinfo_appinfo_filter_add_string(handle,
+ PMINFO_APPINFO_PROP_APP_EXEC, cmdline);
+ if (ret < 0) {
+ ALARM_MGR_EXCEPTION_PRINT("pkgmgrinfo_appinfo_filter_add_string() failed: exec(%s)", cmdline);
+ goto catch;
+ }
+
+ ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(handle, __get_pkgid_func, &pkgid_tmp);
+ if (ret < 0) {
+ ALARM_MGR_EXCEPTION_PRINT("pkgmgrinfo_appinfo_filter_foreach_appinfo() failed\n");
+ goto catch;
+ }
+
+ if (pkgid_tmp) {
+ ALARM_MGR_LOG_PRINT("pkgid_tmp(%s)", pkgid_tmp);
+ ret = 0;
+ } else
+ ret = -1;
+
+ *pkgid = pkgid_tmp;
+
+catch:
+ pkgmgrinfo_appinfo_filter_destroy(handle);
+
+ return ret;
+}
+
+static int __zone_get_pkgid_byappid(const char *appid, const char *zone, char **pkgid)
+{
+ int ret = -1;
+ char *pkgid_tmp = NULL;
+ int i;
+
+ if (strncmp(zone, "/", 1) == 0) {
+ pkgmgrinfo_pkginfo_set_zone(NULL, NULL, 0);
+ } else {
+ pkgmgrinfo_pkginfo_set_zone(zone, NULL, 0);
+ }
+
+ pkgmgrinfo_appinfo_filter_h handle;
+ ret = pkgmgrinfo_appinfo_filter_create(&handle);
+ if (ret < 0) {
+ ALARM_MGR_EXCEPTION_PRINT("appinfo filter handle create failed\n");
+ return -1;
+ }
+
+ ret = pkgmgrinfo_appinfo_filter_add_string(handle,
+ PMINFO_APPINFO_PROP_APP_ID, appid);
+ if (ret < 0) {
+ ALARM_MGR_EXCEPTION_PRINT("pkgmgrinfo_appinfo_filter_add_string() failed : appid(%s)", appid);
+ goto catch;
+ }
+
+ ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(handle, __get_pkgid_func, &pkgid_tmp);
+ if (ret < 0) {
+ ALARM_MGR_EXCEPTION_PRINT("pkgmgrinfo_appinfo_filter_foreach_appinfo() failed\n");
+ goto catch;
+ }
+
+ if (pkgid_tmp) {
+ ALARM_MGR_LOG_PRINT("pkgid_tmp(%s)", pkgid_tmp);
+ ret = 0;
+ } else
+ ret = -1;
+
+ *pkgid = pkgid_tmp;
+catch:
+ pkgmgrinfo_appinfo_filter_destroy(handle);
+ return ret;
+}
+
+bool _get_zone_name(int pid, char *zone_name, int len)
+{
+ vsm_zone_h zone;
+ vsm_context_h ctx = vsm_create_context();
+ const char *zone_name_tmp = NULL;
+ bool ret = true;
+ if (ctx == NULL) {
+ ALARM_MGR_EXCEPTION_PRINT("vsm_create_context failed");
+ return false;
+ }
+ zone = vsm_lookup_zone_by_pid(ctx, pid);
+
+ if (zone != NULL && !vsm_is_host_zone(zone)) {
+ zone_name_tmp = vsm_get_zone_name(zone);
+ if (zone_name_tmp == NULL) {
+ ALARM_MGR_EXCEPTION_PRINT("failed to get zone");
+ ret = false;
+ goto err;
+ }
+ } else if (vsm_is_host_zone(zone)) {
+ zone_name_tmp = "/";
+ } else {
+ ALARM_MGR_EXCEPTION_PRINT("could not get zone name");
+ ret = false;
+ goto err;
+ }
+
+ snprintf(zone_name, len, "%s", zone_name_tmp);
+ ALARM_MGR_EXCEPTION_PRINT("zone name [%s]", zone_name);
+err:
+ if (vsm_cleanup_context(ctx) != 0)
+ ALARM_MGR_EXCEPTION_PRINT("vsm cleanup failed");
+ return ret;
+}
+
+