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>
33 #include <privilege-control.h>
37 #define LOG_TAG "LOADER"
39 #define MAX_PACKAGEID 10
40 #define MAX_APP_EXECUTABLE_NAME 230
41 #define MAX_APPID (MAX_PACKAGEID + MAX_APP_EXECUTABLE_NAME + 1)
46 print_args(int argc, char* argv[])
53 LOGI("%dth arg : [%s]", i, p);
59 adjust_privilege(const char* appid)
61 perm_app_set_privilege(appid, NULL, NULL);
64 extern int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
67 adjust_capability(void)
69 cap_user_header_t head = 0;
70 cap_user_data_t data = 0;
72 head = (cap_user_header_t) malloc(sizeof(*head));
75 fprintf(stderr, "Memory allocation failure.\n");
79 head->version = _LINUX_CAPABILITY_VERSION;
82 data = (cap_user_data_t) calloc(sizeof(*data), _LINUX_CAPABILITY_U32S);
85 fprintf(stderr, "Memory allocation failure.\n");
91 data[CAP_TO_INDEX(CAP_NET_RAW)].effective |= CAP_TO_MASK(CAP_NET_RAW);
92 data[CAP_TO_INDEX(CAP_NET_RAW)].permitted |= CAP_TO_MASK(CAP_NET_RAW);
94 if (capset(head, data) < 0)
96 fprintf(stderr, "Capability setting error\n");
110 osp_do_pre_exe(const char* app_id, const char* bin_path)
115 int (*do_pre_exec_fn)(const char*, const char*) = NULL;
117 handle = dlopen("libosp-env-config.so", RTLD_LAZY | RTLD_LOCAL);
120 LOGE("Failed to dlopen osp-env-config.so (%s)", dlerror());
123 LOGI("Tizen::App > dlopen() ok");
125 do_pre_exec_fn = (int (*)(const char*, const char*)) dlsym(handle, "do_pre_exec");
126 errormsg = dlerror();
127 if (errormsg != NULL)
129 LOGE("Failed to dlsym() (%s)", errormsg);
135 ret = do_pre_exec_fn(app_id, bin_path);
138 LOGE("Failed to do_pre_exe() (%d)", ret);
140 LOGI("do_pre_exec() ok");
148 get_packageid_executable_name_from_path(const char path[], char appid[], char executable_name[])
150 // path is "/opt/apps/[appid]/bin/[executable_name]"
152 const char* p = strrchr(path, '/');
155 LOGI("Improper path %s", path);
159 const int path_len = strlen(path);
160 const int exec_len = strlen(p);
161 if (exec_len <= 0 || exec_len > PATH_MAX || path_len <= 0 || path_len > PATH_MAX)
163 LOGI("Improper path %s", path);
167 strncpy(executable_name, p + 1, exec_len);
170 LOGI("Exec is %s", executable_name);
173 if (path_len < /* '/' */ 1 + 10 + strlen("/bin/") + exec_len)
175 LOGI("Improper path %s", path);
179 strncpy(appid, p - strlen("bin/") - 10, 10);
181 LOGI("appId is %s", appid);
188 main(int argc, char* argv[])
190 void* real_handle = NULL;
192 const char* pid = NULL;
194 LOGI("Initializing : argc %d, argv 0x%x.", argc, argv);
195 //print_args(argc, argv);
197 if (appinfo_is_initialized() == 0)
200 char appid[MAX_APPID];
201 char packageid[MAX_PACKAGEID + 1];
202 char executable_name[MAX_APP_EXECUTABLE_NAME];
204 memset(appid, 0, sizeof(appid));
205 memset(packageid, 0, sizeof(packageid));
206 memset(executable_name, 0, sizeof(executable_name));
208 // convert package path to package name
209 get_packageid_executable_name_from_path(argv[0], packageid, executable_name);
210 snprintf(appid, MAX_APPID, "%s.%s", packageid, executable_name);
213 appinfo_init(appid, 0);
215 LOGI("Non-preloading initialization done.");
218 pid = appinfo_get_appid();
221 LOGI("app is %s", pid);
227 osp_do_pre_exe(pid, argv[0]);
230 adjust_privilege(pid);
236 appinfo_set_argv(argc, argv);
241 snprintf(buffer, 1024, "%s.exe", argv[0]);
244 real_handle = dlopen(buffer, RTLD_LAZY);
247 LOGE("Failed to open %s : %s.", buffer, dlerror());
251 void (*pAppInfoInit)() = NULL;
252 pAppInfoInit = (void (*)()) dlsym(real_handle, "AppInfo_Update");
253 errormsg = dlerror();
254 if (errormsg != NULL)
256 LOGE("Failed to find InitAppInfo() : %s.", errormsg);
257 dlclose(real_handle);
258 fprintf(stderr, "executable does not have proper osp library dependency.\n");
262 int (*pRealMain)(int, char* []) = NULL;
263 pRealMain = (int (*)(int, char*[])) dlsym(real_handle, "OspMain");
264 errormsg = dlerror();
265 if (errormsg != NULL)
267 LOGE("Failed to find OspMain() : %s.", errormsg);
268 dlclose(real_handle);
272 // actual initialization
274 (*pRealMain)(argc, argv);
276 LOGI("Osp application terminates.");
278 //dlclose(real_handle);
279 LOGI("Osp cleanup finished for %s.", argv[0]);