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 #define APP_DIR "/opt/apps"
47 typedef struct appid_map_s appid_map_t;
54 struct mrp_resmgr_appid_s {
55 mrp_resmgr_data_t *data;
59 static void map_init(mrp_resmgr_appid_t *, const char *);
60 static void map_add_entry(mrp_resmgr_appid_t *, const char *, const char *);
61 static void map_free_entry(void *, void *);
63 static int pid2exe(const char *, char *, size_t);
66 mrp_resmgr_appid_t *mrp_resmgr_appid_create(mrp_resmgr_data_t *data)
68 mrp_resmgr_appid_t *appid;
69 mrp_htbl_config_t cfg;
72 cfg.comp = mrp_string_comp;
73 cfg.hash = mrp_string_hash;
74 cfg.free = map_free_entry;
75 cfg.nbucket = cfg.nentry / 8;
77 if ((appid = mrp_allocz(sizeof(*appid)))) {
79 appid->map = mrp_htbl_create(&cfg);
81 map_init(appid, APP_DIR);
88 void mrp_resmgr_appid_destroy(mrp_resmgr_appid_t *appid)
91 mrp_htbl_destroy(appid->map, TRUE);
96 const char *mrp_resmgr_appid_find_by_pid(mrp_resmgr_appid_t *appid,
102 if (pid2exe(pid, exe, PATH_MAX) < 0)
105 if (!(entry = mrp_htbl_lookup(appid->map, exe)))
115 static void map_init(mrp_resmgr_appid_t *appid, const char *dir_path)
118 struct dirent *dirent, *binent;
121 char subdir_path[PATH_MAX];
122 char bindir_path[PATH_MAX];
125 if (!(dir = opendir(dir_path))) {
126 mrp_log_error("iiv-resource-manager: can't open directory %s: %s",
127 dir_path, strerror(errno));
131 while ((dirent = readdir(dir))) {
134 snprintf(subdir_path, sizeof(subdir_path), "%s/%s", dir_path, id);
136 if (lstat(subdir_path, &stat) < 0) {
137 mrp_log_error("ivi-resource-manager: can't stat %s: %s",
138 subdir_path, strerror(errno));
142 if (!S_ISDIR(stat.st_mode) || id[0] == '.')
145 snprintf(bindir_path, sizeof(bindir_path), "%s/bin", subdir_path);
147 if (!(bindir = opendir(bindir_path))) {
148 mrp_log_error("ivi-resource-manager: can't open directory %s: %s",
149 bindir_path, strerror(errno));
153 while ((binent = readdir(bindir))) {
154 snprintf(exe, sizeof(exe), "%s/%s", bindir_path, binent->d_name);
156 if (lstat(exe, &stat) < 0) {
157 mrp_log_error("ivi-resource-manager: can't stat %s: %s",
158 exe, strerror(errno));
162 if (!S_ISREG(stat.st_mode) || !(stat.st_mode & 0111))
165 map_add_entry(appid, id, exe);
175 static void map_add_entry(mrp_resmgr_appid_t *appid,
181 if (!(entry = mrp_allocz(sizeof(*entry))) ||
182 !(entry->id = mrp_strdup(id)) ||
183 !(entry->exe = mrp_strdup(exe)))
185 mrp_log_error("ivi-resource-manager: can't allocate memory");
189 mrp_htbl_insert(appid->map, (void *)entry->exe, entry);
191 mrp_log_info("ivi-resource-manager: map exe %s => appid %s",
192 entry->exe, entry->id);
195 static void map_free_entry(void *key, void *object)
197 appid_map_t *me = (appid_map_t *)object;
201 free((void *)me->id);
202 free((void *)me->exe);
206 static int pid2exe(const char *pid, char *buf, size_t len)
213 if (pid && buf && len > 0) {
214 snprintf(path, sizeof(path), "/proc/%s/cmdline", pid);
216 if ((f = fopen(path, "r"))) {
217 if (fgets(buf, len-1, f)) {
218 if ((p = strchr(buf, ' ')))
220 else if ((p = strchr(buf, '\n')))
229 mrp_log_info("ivi-resource-manager: pid2exe(%s) failed", pid);
231 mrp_log_info("ivi-resource-manager: pid %s => exe %s", pid, buf);
240 * indent-tabs-mode: nil