2 * Copyright (c) 2012, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Intel Corporation nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
40 #include <murphy/common.h>
44 #ifdef TZCONFIG_ENABLED
45 # include <tzplatform_config.h>
50 typedef struct appid_map_s appid_map_t;
57 struct mrp_resmgr_appid_s {
58 mrp_resmgr_data_t *data;
62 static void map_init(mrp_resmgr_appid_t *, const char *);
63 static void map_add_entry(mrp_resmgr_appid_t *, const char *, const char *);
64 static void map_free_entry(void *, void *);
66 static int pid2exe(const char *, char *, size_t);
69 mrp_resmgr_appid_t *mrp_resmgr_appid_create(mrp_resmgr_data_t *data)
71 mrp_resmgr_appid_t *appid;
72 mrp_htbl_config_t cfg;
74 #ifdef TZCONFIG_ENABLED
75 char *app_dir = tzplatform_getenv(TZ_USER_APP);
77 char *app_dir = "/usr/app";
81 cfg.comp = mrp_string_comp;
82 cfg.hash = mrp_string_hash;
83 cfg.free = map_free_entry;
84 cfg.nbucket = cfg.nentry / 8;
86 if ((appid = mrp_allocz(sizeof(*appid)))) {
88 appid->map = mrp_htbl_create(&cfg);
90 map_init(appid, app_dir);
97 void mrp_resmgr_appid_destroy(mrp_resmgr_appid_t *appid)
100 mrp_htbl_destroy(appid->map, TRUE);
105 const char *mrp_resmgr_appid_find_by_pid(mrp_resmgr_appid_t *appid,
111 if (pid2exe(pid, exe, PATH_MAX) < 0)
114 if (!(entry = mrp_htbl_lookup(appid->map, exe)))
124 static void map_init(mrp_resmgr_appid_t *appid, const char *dir_path)
127 struct dirent *dirent, *binent;
130 char subdir_path[PATH_MAX];
131 char bindir_path[PATH_MAX];
134 if (!(dir = opendir(dir_path))) {
135 mrp_log_error("ivi-resource-manager: can't open directory %s: %s",
136 dir_path, strerror(errno));
140 while ((dirent = readdir(dir))) {
143 snprintf(subdir_path, sizeof(subdir_path), "%s/%s", dir_path, id);
145 if (lstat(subdir_path, &stat) < 0) {
146 mrp_log_error("ivi-resource-manager: can't stat %s: %s",
147 subdir_path, strerror(errno));
151 if (!S_ISDIR(stat.st_mode) || id[0] == '.')
154 snprintf(bindir_path, sizeof(bindir_path), "%s/bin", subdir_path);
156 if (!(bindir = opendir(bindir_path))) {
157 mrp_log_error("ivi-resource-manager: can't open directory %s: %s",
158 bindir_path, strerror(errno));
162 while ((binent = readdir(bindir))) {
163 snprintf(exe, sizeof(exe), "%s/%s", bindir_path, binent->d_name);
165 if (lstat(exe, &stat) < 0) {
166 mrp_log_error("ivi-resource-manager: can't stat %s: %s",
167 exe, strerror(errno));
171 if (!S_ISREG(stat.st_mode) || !(stat.st_mode & 0111))
174 map_add_entry(appid, id, exe);
184 static void map_add_entry(mrp_resmgr_appid_t *appid,
190 if (!(entry = mrp_allocz(sizeof(*entry))) ||
191 !(entry->id = mrp_strdup(id)) ||
192 !(entry->exe = mrp_strdup(exe)))
194 mrp_log_error("ivi-resource-manager: can't allocate memory");
198 mrp_htbl_insert(appid->map, (void *)entry->exe, entry);
200 mrp_log_info("ivi-resource-manager: map exe %s => appid %s",
201 entry->exe, entry->id);
204 static void map_free_entry(void *key, void *object)
206 appid_map_t *me = (appid_map_t *)object;
210 free((void *)me->id);
211 free((void *)me->exe);
215 static int pid2exe(const char *pid, char *buf, size_t len)
222 if (pid && buf && len > 0) {
223 snprintf(path, sizeof(path), "/proc/%s/cmdline", pid);
225 if ((f = fopen(path, "r"))) {
226 if (fgets(buf, len-1, f)) {
227 if ((p = strchr(buf, ' ')))
229 else if ((p = strchr(buf, '\n')))
238 mrp_log_info("ivi-resource-manager: pid2exe(%s) failed", pid);
240 mrp_log_info("ivi-resource-manager: pid %s => exe %s", pid, buf);
249 * indent-tabs-mode: nil