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);
205 LOGI("Exec is %s", executable_name);
208 if (path_len < /* '/' */ 1 + 10 + strlen("/bin/") + exec_len)
210 LOGI("Improper path %s", path);
214 strncpy(appid, p - strlen("bin/") - 10, 10);
216 LOGI("appId is %s", appid);
223 main(int argc, char* argv[])
225 void* real_handle = NULL;
227 char appid[MAX_APPID];
228 char executable_name[MAX_APP_EXECUTABLE_NAME];
230 void (*pAppInfoInit)(const char*, const char*, int, char* [], int) = NULL;
231 int (*pRealMain)(int, char* []) = NULL;
233 memset(appid, 0, sizeof(appid));
234 memset(executable_name, 0, sizeof(executable_name));
236 LOGI("Initializing.");
237 LOGI("argc %d, argv 0x%x.", argc, argv);
238 print_args(argc, argv);
240 // convert package path to appId
241 get_appid_executable_name_from_path(argv[0], appid, executable_name);
242 LOGI("Osp appId %s.", appid);
248 osp_do_pre_exe(argv[0]);
260 snprintf(buffer, 1024, "%s.exe", argv[0]);
263 real_handle = dlopen(buffer, RTLD_LAZY);
266 LOGE("Failed to open %s : %s.", buffer, dlerror());
270 pAppInfoInit = (void (*)(const char*, const char*, int, char*[], int)) dlsym(real_handle, "InitAppInfo");
271 errormsg = dlerror();
272 if (errormsg != NULL)
274 LOGE("Failed to find InitAppInfo() : %s.", errormsg);
275 dlclose(real_handle);
276 fprintf(stderr, "executable does not have proper osp library dependency.\n");
280 pRealMain = (int (*)(int, char*[])) dlsym(real_handle, "OspMain");
281 errormsg = dlerror();
282 if (errormsg != NULL)
284 LOGE("Failed to find OspMain() : %s.", errormsg);
285 dlclose(real_handle);
289 // actual initialization
290 (*pAppInfoInit)(appid, executable_name, argc, argv, -1);
291 (*pRealMain)(argc, argv);
293 LOGI("Osp application terminates.");
295 //dlclose(real_handle);
296 LOGI("Osp cleanup finished for %s.", argv[0]);