2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FApp_AppEntry.cpp
19 * @brief This is the entry point for the application.
27 #include <sys/types.h>
29 #include <sys/types.h>
34 #include <sys/prctl.h>
35 #include <linux/capability.h>
37 #include <unique_ptr.h>
40 #include <privilege-control.h>
41 #include <pkgmgr-info.h>
43 #include <FOspConfig.h>
46 #define LOG_IO_TAG "LOADER"
47 #define MAX_PACKAGE_ID 20
48 #define MAX_APP_EXECUTABLE_NAME 230
49 #define MAX_PACKAGE_NAME 100
50 #define MAX_PR_NAME 16
59 extern int OspMain(int argc, char* pArgv[]) __attribute__((weak));
60 //extern void Osp_Initialize();
61 extern void InitAppInfo(const char* appId, const char* exeName, int argc, char* pArgv[], int fd);
64 AdjustPrivilege(const char* pkgname)
66 void* handle = dlopen("libprivilege-control.so.0", RTLD_LAZY | RTLD_LOCAL);
69 fprintf(stderr, "Cannot open libprivilege-control.so.0\n");
76 int (* ppriv_func)(const char*) = NULL;
78 ppriv_func = reinterpret_cast <int (*)(const char*)>(dlsym(handle, "set_privilege"));
82 fprintf(stderr, "Dlsym error: %s\n", msg);
88 int ret = (*ppriv_func)(pkgname);
91 fprintf(stderr, "set_privilege() returned %d\n", ret);
97 extern "C" int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
100 AdjustCapability(void)
102 cap_user_header_t head = 0;
103 cap_user_data_t data = 0;
105 head = static_cast<cap_user_header_t>(malloc(sizeof(*head)));
108 fprintf(stderr, "Memory allocation failure.\n");
112 head->version = _LINUX_CAPABILITY_VERSION;
113 head->pid = getpid();
115 data = static_cast<cap_user_data_t>(calloc(sizeof(*data), _LINUX_CAPABILITY_U32S));
118 fprintf(stderr, "Memory allocation failure.\n");
124 data[CAP_TO_INDEX(CAP_NET_RAW)].effective |= CAP_TO_MASK(CAP_NET_RAW);
125 data[CAP_TO_INDEX(CAP_NET_RAW)].permitted |= CAP_TO_MASK(CAP_NET_RAW);
127 if (capset(head, data) < 0)
129 fprintf(stderr, "Capability setting error\n");
144 GetAppIdAppExecutableNameFromPathNew(const char appName[], char* appId, char* exeName)
146 char buffer[PATH_MAX];
148 // path is ".../[appid]/bin/[executable_name]"
150 memset(buffer, 0, PATH_MAX);
151 const char* pRet = realpath(appName, buffer);
154 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, Improper appname %s", appName);
158 const char* p = strrchr(buffer, '/');
161 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, Improper appname %s", appName);
165 const size_t pathLen = strlen(buffer);
166 const int execLen = strlen(p);
167 if (execLen <= 0 || execLen > PATH_MAX || pathLen > PATH_MAX)
169 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, Improper appname %s", appName);
173 strncpy(exeName, p + 1, execLen);
174 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, exeName is %s", exeName);
176 if (pathLen < 1 + 10 + strlen("/bin/") + execLen)
178 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, Improper appname %s", appName);
182 strncpy(appId, p - strlen("bin/") - 10, 10);
183 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, package is %s", appId);
189 GetAppIdAppExecutableNameFromPath(const char appName[], char* appId, char* exeName)
191 const char* begin = NULL;
192 const char* end = NULL;
193 const int path_len = strlen(appName);
195 // Calculate the header
196 const char* p = strstr(appName, "/apps/org.tizen.");
199 return GetAppIdAppExecutableNameFromPathNew(appName, appId, exeName);
204 end = strchr(begin + 1, '/');
207 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, Improper appname %s", appName);
211 int len = end - begin - 1;
213 if (len > MAX_PACKAGE_ID)
215 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, Improper appname %s with length %d", appName, len);
216 len = MAX_PACKAGE_ID;
220 strncpy(appId, begin + 1, len);
221 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, app is %s", appId);
225 if (path_len > len + 21)
227 exe_len = std::min(MAX_APP_EXECUTABLE_NAME - 1, path_len - len - 21);
228 strncpy(exeName, end + 5, exe_len);
231 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, exeName is %s", exeName);
238 PrintArgs(int argc, char* argv[])
240 const char* p = NULL;
245 LOG(LOG_DEBUG, LOG_IO_TAG, "%dth arg : [%s]", i, p);
252 DoPreExec(const char* packageName, const char* bin_path)
256 int (* DoPreExecFunc)(const char*, const char*) = NULL;
259 handle = dlopen("libosp-env-config.so", RTLD_LAZY | RTLD_GLOBAL);
262 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io > Failed to dlopen libosp-env-config.so (%s)", dlerror());
265 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io > dlopen() ok");
267 DoPreExecFunc = reinterpret_cast <int (*)(const char*, const char*)>(dlsym(handle, "do_pre_exec"));
268 errormsg = dlerror();
269 if (errormsg != NULL)
271 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io > Failed to dlsym() (%s)", errormsg);
275 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io > dlsym() ok");
277 ret = DoPreExecFunc(packageName, bin_path);
280 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io > Failed to do_pre_exe() (%d)", ret);
282 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io > do_pre_exe() ok");
290 ChangeProcessName(const char* fullPath, int argc, char** const argv)
292 // http://stupefydeveloper.blogspot.com/2008/10/linux-change-process-name.html
293 // [INFO] While this implementation may not be portable, most program loader does similar work.
295 // fullPath should be less than original size
296 const int size = strlen(argv[0]);
297 char* args = argv[0];
298 memset(args, '\0', size);
299 strncpy(args, fullPath, size - 1);
301 const char* fileName = NULL;
302 fileName = strrchr(fullPath, '/') + 1;
305 char procName[MAX_PR_NAME];
306 memset(procName, '\0', MAX_PR_NAME);
308 snprintf(procName, MAX_PR_NAME - 1, "%s", fileName);
309 prctl(PR_SET_NAME, procName);
310 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, %s, %d > Process name %s.", __func__, __LINE__, procName);
316 AdjustHwAccInfo(const char* appId)
318 pkgmgrinfo_appinfo_h appHandle = NULL;
319 int ret = pkgmgrinfo_appinfo_get_appinfo(appId, &appHandle);
320 if (ret != PMINFO_R_OK)
322 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::App, %s, %d > Getting package info failed for %s.", __func__, __LINE__, appId);
326 pkgmgrinfo_app_hwacceleration hwAcceleration;
328 ret = pkgmgrinfo_appinfo_get_hwacceleration(appHandle, &hwAcceleration);
329 if (ret != PMINFO_R_OK)
331 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::App, %s, %d > Getting hwaccel info failed for %s.", __func__, __LINE__, appId);
332 pkgmgrinfo_appinfo_destroy_appinfo(appHandle);
335 switch(hwAcceleration)
337 case PMINFO_HWACCELERATION_NOT_USE_GL:
338 setenv("HWACC", "NOT_USE", 1);
340 case PMINFO_HWACCELERATION_USE_GL:
341 setenv("HWACC", "USE", 1);
343 case PMINFO_HWACCELERATION_USE_SYSTEM_SETTING:
346 // no need to handle default
350 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::App, %s, %d > HWACC sets for %s with %d.", __func__, __LINE__, appId, hwAcceleration);
351 pkgmgrinfo_appinfo_destroy_appinfo(appHandle);
356 * The entry function of SLP application called by the operating system.
359 main(int argc, char* pArgv[])
361 bool bCommand = false;
363 char packageId[MAX_PACKAGE_ID];
364 char exeName[MAX_APP_EXECUTABLE_NAME];
365 char fullPath[PATH_MAX];
367 memset(packageId, 0, MAX_PACKAGE_ID);
368 memset(exeName, 0, MAX_APP_EXECUTABLE_NAME);
369 memset(fullPath, 0, PATH_MAX);
371 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, %s, %d > executable binary path: %s", __func__, __LINE__, pArgv[0]);
372 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, %s, %d > uid: %d, euid: %d", __func__, __LINE__, getuid(), geteuid());
374 const int len = strlen(pArgv[0]);
375 bCommand = (len > 4 && (strncmp(pArgv[0] + len - 4, ".exe", 4) == 0));
378 const int path_size = std::min(PATH_MAX - 1, len - 4);
379 strncpy(fullPath, pArgv[0], path_size);
383 strncpy(fullPath, pArgv[0], PATH_MAX - 1);
385 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, %s, %d > processed binary path: %s", __func__, __LINE__, fullPath);
387 // convert package path to packageId
388 GetAppIdAppExecutableNameFromPath(fullPath, packageId, exeName);
390 // acquire appId from packageId and exec
391 char appId[MAX_PACKAGE_ID + MAX_APP_EXECUTABLE_NAME + 2] = {0, };
392 strncpy(appId, packageId, 10);
394 strncpy(appId + 11, exeName, MAX_APP_EXECUTABLE_NAME);
400 DoPreExec(packageId, fullPath);
403 AdjustPrivilege(packageId);
408 PrintArgs(argc, pArgv);
412 ChangeProcessName(fullPath, argc, pArgv);
413 pArgv[0] = strdup(fullPath);
417 // dlopen will load Osp_Initialize() internally as __attribute__((constructor))
419 InitAppInfo(packageId, exeName, argc, pArgv, -1);
421 AdjustHwAccInfo(appId);
423 int r = OspMain(argc, pArgv);
425 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, %s, %d > OSP application is terminated.", __func__, __LINE__);
426 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, %s, %d > uid: %d, euid: %d", __func__, __LINE__, getuid(), geteuid());
433 #endif // __cplusplus