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>
44 #include <FOspConfig.h>
47 #define LOG_IO_TAG "LOADER"
48 #define MAX_PACKAGE_ID 10
49 #define MAX_APP_EXECUTABLE_NAME 230
50 #define MAX_PACKAGE_NAME 100
51 #define MAX_PR_NAME 16
60 extern int OspMain(int argc, char* pArgv[]) __attribute__((weak));
61 //extern void Osp_Initialize();
62 extern void AppInfo_Update(void);
65 AdjustPrivilege(const char* pkgname)
67 void* handle = dlopen("libprivilege-control.so.0", RTLD_LAZY | RTLD_LOCAL);
70 fprintf(stderr, "Cannot open libprivilege-control.so.0\n");
77 int (* ppriv_func)(const char*) = NULL;
79 ppriv_func = reinterpret_cast <int (*)(const char*)>(dlsym(handle, "set_privilege"));
83 fprintf(stderr, "Dlsym error: %s\n", msg);
89 int ret = (*ppriv_func)(pkgname);
92 fprintf(stderr, "set_privilege() returned %d\n", ret);
98 extern "C" int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
101 AdjustCapability(void)
103 cap_user_header_t head = 0;
104 cap_user_data_t data = 0;
106 head = static_cast<cap_user_header_t>(malloc(sizeof(*head)));
109 fprintf(stderr, "Memory allocation failure.\n");
113 head->version = _LINUX_CAPABILITY_VERSION;
114 head->pid = getpid();
116 data = static_cast<cap_user_data_t>(calloc(sizeof(*data), _LINUX_CAPABILITY_U32S));
119 fprintf(stderr, "Memory allocation failure.\n");
125 data[CAP_TO_INDEX(CAP_NET_RAW)].effective |= CAP_TO_MASK(CAP_NET_RAW);
126 data[CAP_TO_INDEX(CAP_NET_RAW)].permitted |= CAP_TO_MASK(CAP_NET_RAW);
128 if (capset(head, data) < 0)
130 fprintf(stderr, "Capability setting error\n");
145 GetPackageIdAppExecutableNameFromPath(const char appName[], char* appId, char* exeName)
147 char buffer[PATH_MAX];
149 // path is ".../[appid]/bin/[executable_name]"
151 memset(buffer, 0, PATH_MAX);
152 const char* pRet = realpath(appName, buffer);
155 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, Improper appname %s", appName);
159 const char* p = strrchr(buffer, '/');
162 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, Improper appname %s", appName);
166 const size_t pathLen = strlen(buffer);
167 const int execLen = strlen(p);
168 if (execLen <= 0 || execLen > PATH_MAX || pathLen > PATH_MAX)
170 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, Improper appname %s", appName);
174 strncpy(exeName, p + 1, execLen);
175 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, exeName is %s", exeName);
177 if (pathLen < 1 + 10 + strlen("/bin/") + execLen)
179 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, Improper appname %s", appName);
183 strncpy(appId, p - strlen("bin/") - 10, 10);
184 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, package is %s", appId);
191 PrintArgs(int argc, char* argv[])
193 const char* p = NULL;
198 LOG(LOG_DEBUG, LOG_IO_TAG, "%dth arg : [%s]", i, p);
205 DoPreExec(const char* packageName, const char* bin_path)
209 int (* DoPreExecFunc)(const char*, const char*) = NULL;
212 handle = dlopen("libosp-env-config.so", RTLD_LAZY | RTLD_GLOBAL);
215 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io > Failed to dlopen libosp-env-config.so (%s)", dlerror());
218 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io > dlopen() ok");
221 DoPreExecFunc = reinterpret_cast <int (*)(const char*, const char*)>(dlsym(handle, "do_pre_exec"));
222 errormsg = dlerror();
223 if (errormsg != NULL)
225 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io > Failed to dlsym() (%s)", errormsg);
229 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io > dlsym() ok");
231 ret = DoPreExecFunc(packageName, bin_path);
234 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io > Failed to do_pre_exe() (%d)", ret);
236 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io > do_pre_exe() ok");
244 ChangeProcessName(const char* fullPath, int argc, char** const argv)
246 // http://stupefydeveloper.blogspot.com/2008/10/linux-change-process-name.html
247 // [INFO] While this implementation may not be portable, most program loader does similar work.
249 // fullPath should be less than original size
250 const int size = strlen(argv[0]);
251 char* args = argv[0];
252 memset(args, '\0', size);
253 strncpy(args, fullPath, size - 1);
255 const char* fileName = NULL;
256 fileName = strrchr(fullPath, '/') + 1;
259 char procName[MAX_PR_NAME];
260 memset(procName, '\0', MAX_PR_NAME);
262 snprintf(procName, MAX_PR_NAME - 1, "%s", fileName);
263 prctl(PR_SET_NAME, procName);
264 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, %s, %d > Process name %s.", __func__, __LINE__, procName);
270 AdjustHwAccInfo(const char* appId)
272 pkgmgrinfo_appinfo_h appHandle = NULL;
273 int ret = pkgmgrinfo_appinfo_get_appinfo(appId, &appHandle);
274 if (ret != PMINFO_R_OK)
276 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::App, %s, %d > Getting package info failed for %s.", __func__, __LINE__, appId);
280 pkgmgrinfo_app_hwacceleration hwAcceleration;
282 ret = pkgmgrinfo_appinfo_get_hwacceleration(appHandle, &hwAcceleration);
283 if (ret != PMINFO_R_OK)
285 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::App, %s, %d > Getting hwaccel info failed for %s.", __func__, __LINE__, appId);
286 pkgmgrinfo_appinfo_destroy_appinfo(appHandle);
289 switch(hwAcceleration)
291 case PMINFO_HWACCELERATION_NOT_USE_GL:
292 setenv("HWACC", "NOT_USE", 1);
294 case PMINFO_HWACCELERATION_USE_GL:
295 setenv("HWACC", "USE", 1);
297 case PMINFO_HWACCELERATION_USE_SYSTEM_SETTING:
300 // no need to handle default
304 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::App, %s, %d > HWACC sets for %s with %d.", __func__, __LINE__, appId, hwAcceleration);
305 pkgmgrinfo_appinfo_destroy_appinfo(appHandle);
310 * The entry function of SLP application called by the operating system.
313 main(int argc, char* pArgv[])
315 bool bCommand = false;
317 char packageId[MAX_PACKAGE_ID + 1];
318 char exeName[MAX_APP_EXECUTABLE_NAME];
319 char fullPath[PATH_MAX];
321 memset(packageId, 0, sizeof(packageId));
322 memset(exeName, 0, sizeof(exeName));
323 memset(fullPath, 0, PATH_MAX);
325 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, %s, %d > executable binary path: %s", __func__, __LINE__, pArgv[0]);
326 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, %s, %d > uid: %d, euid: %d", __func__, __LINE__, getuid(), geteuid());
328 const int len = strlen(pArgv[0]);
329 bCommand = (len > 4 && (strncmp(pArgv[0] + len - 4, ".exe", 4) == 0));
332 const int path_size = std::min(PATH_MAX - 1, len - 4);
333 strncpy(fullPath, pArgv[0], path_size);
337 strncpy(fullPath, pArgv[0], PATH_MAX - 1);
339 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, %s, %d > processed binary path: %s", __func__, __LINE__, fullPath);
341 // convert package path to packageId
342 GetPackageIdAppExecutableNameFromPath(fullPath, packageId, exeName);
344 // acquire appId from packageId and exec
345 char appId[MAX_PACKAGE_ID + MAX_APP_EXECUTABLE_NAME + 2] = {0, };
346 strncpy(appId, packageId, 10);
348 strncpy(appId + 11, exeName, MAX_APP_EXECUTABLE_NAME);
354 DoPreExec(packageId, fullPath);
357 AdjustPrivilege(appId);
362 PrintArgs(argc, pArgv);
366 ChangeProcessName(fullPath, argc, pArgv);
367 pArgv[0] = strdup(fullPath);
371 appinfo_init(appId, 0);
372 appinfo_set_argv(argc, pArgv);
376 AdjustHwAccInfo(appId);
378 int r = OspMain(argc, pArgv);
380 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, %s, %d > OSP application is terminated.", __func__, __LINE__);
381 LOG(LOG_DEBUG, LOG_IO_TAG, "Tizen::Io, %s, %d > uid: %d, euid: %d", __func__, __LINE__, getuid(), geteuid());
388 #endif // __cplusplus