2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file serviceapp_loader.c
20 * @brief This is the implementation for the osp-service-app-loader.
30 #include <linux/capability.h>
36 #define LOG_TAG "LOADER"
39 #define MAX_APP_EXECUTABLE_NAME 230
41 #define MIN(a,b) ((a) < (b) ? (a) : (b))
45 print_args(int argc, char* argv[])
52 LOGI("%dth arg : [%s]", i, p);
59 void* handle = dlopen("libprivilege-control.so.0", RTLD_LAZY | RTLD_LOCAL);
62 fprintf(stderr, "Cannot open libprivilege-control.so.0\n");
69 int (*ppriv_func)(const char*) = NULL;
71 ppriv_func = (int (*)(const char*))dlsym(handle, "set_privilege");
75 fprintf(stderr, "Dlsym error: %s\n", msg);
81 char pkgname[255] = {0, };
82 int ret = aul_app_get_appid_bypid(getpid(), pkgname, 255);
85 fprintf(stderr, "Cannot acquire app from pid with 0x%x.\n", ret);
91 ret = (*ppriv_func)(pkgname);
94 fprintf(stderr, "set_privilege() returned %d\n", ret);
100 extern int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
103 adjust_capability(void)
105 cap_user_header_t head = 0;
106 cap_user_data_t data = 0;
108 head = (cap_user_header_t) malloc(sizeof(*head));
111 fprintf(stderr, "Memory allocation failure.\n");
115 head->version = _LINUX_CAPABILITY_VERSION;
116 head->pid = getpid();
118 data = (cap_user_data_t) calloc(sizeof(*data), _LINUX_CAPABILITY_U32S);
121 fprintf(stderr, "Memory allocation failure.\n");
127 data[CAP_TO_INDEX(CAP_NET_RAW)].effective |= CAP_TO_MASK(CAP_NET_RAW);
128 data[CAP_TO_INDEX(CAP_NET_RAW)].permitted |= CAP_TO_MASK(CAP_NET_RAW);
130 if (capset(head, data) < 0)
132 fprintf(stderr, "Capability setting error\n");
145 osp_do_pre_exe(const char* bin_path)
150 int (*do_pre_exec_fn)(const char*, const char*) = NULL;
152 handle = dlopen("libosp-env-config.so", RTLD_LAZY | RTLD_LOCAL);
155 LOGE("Failed to dlopen osp-env-config.so (%s)", dlerror());
158 LOGI("Tizen::App > dlopen() ok");
160 do_pre_exec_fn = (int (*)(const char*, const char*)) dlsym(handle, "do_pre_exec");
161 errormsg = dlerror();
162 if (errormsg != NULL)
164 LOGE("Failed to dlsym() (%s)", errormsg);
170 ret = do_pre_exec_fn(NULL, bin_path);
173 LOGE("Failed to do_pre_exe() (%d)", ret);
175 LOGI("do_pre_exec() ok");
183 get_appid_executable_name_from_path(const char path[], char appid[], char executable_name[])
185 // path is "/opt/apps/[appid]/bin/[executable_name]"
187 const char* p = strrchr(path, '/');
190 LOGI("Improper path %s", path);
194 const int path_len = strlen(path);
195 const int exec_len = strlen(p);
196 if (exec_len <= 0 || exec_len > PATH_MAX || path_len <= 0 || path_len > PATH_MAX)
198 LOGI("Improper path %s", path);
202 strncpy(executable_name, p + 1, exec_len);
204 LOGI("exeName is %s", executable_name);
206 if (path_len < /* '/' */ 1 + 10 + strlen("/bin/") + exec_len)
208 LOGI("Improper path %s", path);
212 strncpy(appid, p - strlen("bin/") - 10, 10);
214 LOGI("appId is %s", appid);
220 get_dir_fd_from_path(const char* path)
224 const int len = strlen(path);
225 char dname[PATH_MAX];
226 memset(dname, 0, PATH_MAX);
228 const char* p = path + len;
229 for (i = 0; i < len; i++, p--)
242 LOGI("Wrong path format : %s.", path);
247 strncpy(dname, path, p - path);
249 const int fd = open(dname, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
251 LOGI("Directory path : %s, fd : %d.", dname, fd);
257 main(int argc, char* argv[])
259 void* real_handle = NULL;
261 char appid[MAX_APPID];
262 char executable_name[MAX_APP_EXECUTABLE_NAME];
264 void (*pAppInfoInit)(const char*, const char*, int, char* [], int) = NULL;
265 int (*pRealMain)(int, char* []) = NULL;
267 memset(appid, 0, sizeof(appid));
268 memset(executable_name, 0, sizeof(executable_name));
270 LOGI("Initializing.");
271 LOGI("argc %d, argv 0x%x.", argc, argv);
272 print_args(argc, argv);
274 // convert package path to appId
275 get_appid_executable_name_from_path(argv[0], appid, executable_name);
276 LOGI("Osp appId %s.", appid);
282 osp_do_pre_exe(argv[0]);
294 snprintf(buffer, 1024, "%s.exe", argv[0]);
297 real_handle = dlopen(buffer, RTLD_LAZY);
300 LOGE("Failed to open %s : %s.", buffer, dlerror());
304 pAppInfoInit = (void (*)(const char*, const char*, int, char*[], int)) dlsym(real_handle, "InitAppInfo");
305 errormsg = dlerror();
306 if (errormsg != NULL)
308 LOGE("Failed to find InitAppInfo() : %s.", errormsg);
309 dlclose(real_handle);
310 fprintf(stderr, "executable does not have proper osp library dependency.\n");
314 int fd = get_dir_fd_from_path(argv[0]);
317 LOGE("Failed to open path.");
318 fprintf(stderr, "Failed to open %s.\n", argv[0]);
319 dlclose(real_handle);
323 pRealMain = (int (*)(int, char*[])) dlsym(real_handle, "OspMain");
324 errormsg = dlerror();
325 if (errormsg != NULL)
327 LOGE("Failed to find OspMain() : %s.", errormsg);
328 dlclose(real_handle);
332 // actual initialization
333 (*pAppInfoInit)(appid, executable_name, argc, argv, fd);
334 (*pRealMain)(argc, argv);
336 LOGI("Osp application terminates.");
338 //dlclose(real_handle);
339 LOGI("Osp cleanup finished for %s.", argv[0]);