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"
38 #define MAX_PACKAGEID 10
39 #define MAX_APP_EXECUTABLE_NAME 230
40 #define MAX_APPID (MAX_PACKAGEID + MAX_APP_EXECUTABLE_NAME + 1)
45 print_args(int argc, char* argv[])
52 LOGI("%dth arg : [%s]", i, p);
58 adjust_privilege(const char* appid)
60 void* handle = dlopen("libprivilege-control.so.0", RTLD_LAZY | RTLD_LOCAL);
63 fprintf(stderr, "Cannot open libprivilege-control.so.0\n");
70 int (*ppriv_func)(const char*) = NULL;
72 ppriv_func = (int (*)(const char*))dlsym(handle, "set_privilege");
76 fprintf(stderr, "Dlsym error: %s\n", msg);
82 int ret = (*ppriv_func)(appid);
85 fprintf(stderr, "set_privilege() returned %d\n", ret);
91 extern int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
94 adjust_capability(void)
96 cap_user_header_t head = 0;
97 cap_user_data_t data = 0;
99 head = (cap_user_header_t) malloc(sizeof(*head));
102 fprintf(stderr, "Memory allocation failure.\n");
106 head->version = _LINUX_CAPABILITY_VERSION;
107 head->pid = getpid();
109 data = (cap_user_data_t) calloc(sizeof(*data), _LINUX_CAPABILITY_U32S);
112 fprintf(stderr, "Memory allocation failure.\n");
118 data[CAP_TO_INDEX(CAP_NET_RAW)].effective |= CAP_TO_MASK(CAP_NET_RAW);
119 data[CAP_TO_INDEX(CAP_NET_RAW)].permitted |= CAP_TO_MASK(CAP_NET_RAW);
121 if (capset(head, data) < 0)
123 fprintf(stderr, "Capability setting error\n");
136 osp_do_pre_exe(const char* bin_path)
141 int (*do_pre_exec_fn)(const char*, const char*) = NULL;
143 handle = dlopen("libosp-env-config.so", RTLD_LAZY | RTLD_LOCAL);
146 LOGE("Failed to dlopen osp-env-config.so (%s)", dlerror());
149 LOGI("Tizen::App > dlopen() ok");
151 do_pre_exec_fn = (int (*)(const char*, const char*)) dlsym(handle, "do_pre_exec");
152 errormsg = dlerror();
153 if (errormsg != NULL)
155 LOGE("Failed to dlsym() (%s)", errormsg);
161 ret = do_pre_exec_fn(NULL, bin_path);
164 LOGE("Failed to do_pre_exe() (%d)", ret);
166 LOGI("do_pre_exec() ok");
174 get_packageid_executable_name_from_path(const char path[], char appid[], char executable_name[])
176 // path is "/opt/apps/[appid]/bin/[executable_name]"
178 const char* p = strrchr(path, '/');
181 LOGI("Improper path %s", path);
185 const int path_len = strlen(path);
186 const int exec_len = strlen(p);
187 if (exec_len <= 0 || exec_len > PATH_MAX || path_len <= 0 || path_len > PATH_MAX)
189 LOGI("Improper path %s", path);
193 strncpy(executable_name, p + 1, exec_len);
196 LOGI("Exec is %s", executable_name);
199 if (path_len < /* '/' */ 1 + 10 + strlen("/bin/") + exec_len)
201 LOGI("Improper path %s", path);
205 strncpy(appid, p - strlen("bin/") - 10, 10);
207 LOGI("appId is %s", appid);
214 main(int argc, char* argv[])
216 void* real_handle = NULL;
218 char packageid[MAX_PACKAGEID + 1];
219 char executable_name[MAX_APP_EXECUTABLE_NAME];
220 char appid[MAX_APPID];
222 void (*pAppInfoInit)(const char*, const char*, int, char* [], int) = NULL;
223 int (*pRealMain)(int, char* []) = NULL;
225 memset(packageid, 0, sizeof(packageid));
226 memset(executable_name, 0, sizeof(executable_name));
227 memset(appid, 0, sizeof(appid));
229 LOGI("Initializing : argc %d, argv 0x%x.", argc, argv);
230 //print_args(argc, argv);
232 // convert package path to package name
233 get_packageid_executable_name_from_path(argv[0], packageid, executable_name);
234 snprintf(appid, MAX_APPID, "%s.%s", packageid, executable_name);
237 LOGI("app is %s", appid);
243 osp_do_pre_exe(argv[0]);
246 adjust_privilege(appid);
253 appinfo_init(appid, 0);
254 appinfo_set_argv(argc, argv);
259 snprintf(buffer, 1024, "%s.exe", argv[0]);
262 real_handle = dlopen(buffer, RTLD_LAZY);
265 LOGE("Failed to open %s : %s.", buffer, dlerror());
269 pAppInfoInit = (void (*)(const char*, const char*, int, char*[], int)) dlsym(real_handle, "InitAppInfo");
270 errormsg = dlerror();
271 if (errormsg != NULL)
273 LOGE("Failed to find InitAppInfo() : %s.", errormsg);
274 dlclose(real_handle);
275 fprintf(stderr, "executable does not have proper osp library dependency.\n");
279 pRealMain = (int (*)(int, char*[])) dlsym(real_handle, "OspMain");
280 errormsg = dlerror();
281 if (errormsg != NULL)
283 LOGE("Failed to find OspMain() : %s.", errormsg);
284 dlclose(real_handle);
288 // actual initialization
289 (*pAppInfoInit)(packageid, executable_name, argc, argv, -1);
290 (*pRealMain)(argc, argv);
292 LOGI("Osp application terminates.");
294 //dlclose(real_handle);
295 LOGI("Osp cleanup finished for %s.", argv[0]);