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_AulServer.cpp
19 * @brief This is the implementation for the _AulServer.cpp class.
25 #include <sys/prctl.h>
27 #include <unique_ptr.h>
31 #include <appsvc/appsvc.h>
33 #include <FBaseObject.h>
34 #include <FBaseString.h>
35 #include <FBaseUtil.h>
36 #include <FBaseSysLog.h>
37 #include <FBaseColHashMapT.h>
38 #include <FAppPkgPackageInfo.h>
40 #include <FBaseRt_Process.h>
41 #include <FBase_StringConverter.h>
42 #include "FAppPkg_PackageManagerImpl.h"
43 #include "FApp_Types.h"
44 #include "FApp_AulServer.h"
45 #include "FApp_TemplateUtil.h"
48 using namespace Tizen::App::Package;
49 using namespace Tizen::Base;
50 using namespace Tizen::Base::Collection;
51 using namespace Tizen::Base::Runtime;
52 using namespace Tizen::Base::Utility;
57 const int _MAX_CATEGORY = 12;
58 const int _MAX_PACKAGE_ID_LENGTH = 10;
60 const char _APP_PATH_FORMAT[] = "/opt/usr/apps/0000000000/bin/%ls";
61 const char _APP_PATH_FORMAT2[] = "/opt/apps/0000000000/bin/%ls";
62 const char PATH_ROOT[] = "/opt/usr/apps/";
63 const char PATH_ROOT2[] = "/opt/apps/";
67 namespace Tizen { namespace App
72 const char category[_MAX_CATEGORY];
76 static const _CategoryList _CATEGORY_LIST[] =
78 {"home-screen", _APP_TYPE_HOME_APP},
79 {"lock-screen", _APP_TYPE_LOCK_APP},
80 {"ime", _APP_TYPE_IME_APP},
83 static const int _NUM_CATEGORY = sizeof(_CATEGORY_LIST) / sizeof(_CategoryList);
87 _AulServer::SendResult(bundle* b, appsvc_result_val res)
91 int ret = appsvc_send_result(b, res);
97 SysLogException(NID_APP, r, "Invalid result bundle.");
102 SysLogException(NID_APP, r, "Internal IPC error.");
107 SysLogException(NID_APP, r, "General error.");
114 SysLog(NID_APP, "SendResult() ok");
121 _AulServer::IsRunning(const AppId& appId, const String& exeName)
123 char slpPackageName[MAX_SLP_PACKAGE_ID] = {0, };
125 _PackageManagerImpl* pPackageManagerImpl = _PackageManagerImpl::GetInstance();
126 SysTryReturn(NID_APP, pPackageManagerImpl != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Invalid package instance.");
128 pPackageManagerImpl->GetPackageName(appId, &exeName, slpPackageName, MAX_SLP_PACKAGE_ID);
130 const bool isRunning = (aul_app_is_running(slpPackageName) > 0);
132 SysLog(NID_APP, "'%s' %s running now.", slpPackageName, (isRunning) ? "is" : "is NOT");
138 _AulServer::IsRunning(const String& packageName)
140 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageName));
142 const bool isRunning = (aul_app_is_running(pPackageId.get()) > 0);
144 SysLog(NID_APP, "'%ls' %s running now.", packageName.GetPointer(), (isRunning) ? "is" : "is NOT");
150 _AulServer::Launch(const String& appId)
152 std::unique_ptr<char[]> pPackageName(_StringConverter::CopyToCharArrayN(appId));
154 int ret = aul_launch_app(pPackageName.get(), NULL);
161 r = E_ILLEGAL_ACCESS;
170 SysLogException(NID_APP, r, "[%s] Error occurred.", GetErrorMessage(r));
179 _AulServer::SetOnAppTerminatedCb(int (* pf_app_dead_handler)(int pid, void* pData), void* pData)
181 aul_listen_app_dead_signal(pf_app_dead_handler, pData);
182 SysLog(NID_APP, "'app_dead_handler is set.");
187 _AulServer::TerminateApplicationByPid(int pid)
189 int ret_aul = aul_terminate_pid(pid);
190 result r = E_SUCCESS;
196 SysLogException(NID_APP, r, "invaild pid.\n");
201 SysLogException(NID_APP, r, "internal AUL IPC error.\n");
206 SysLogException(NID_APP, r, "general error.\n");
210 SysLog(NID_APP, "'%d' is terminated.", pid);
218 _AulServer::SetOomAdj(int pid, int adj)
220 // set oom_adj to -17 for system service
221 result r = E_SUCCESS;
222 char buf[FILENAME_MAX];
225 snprintf(buf, FILENAME_MAX, "/proc/%d/oom_adj", pid);
226 fP = fopen(buf, "w");
227 SysTryReturnResult(NID_APP, fP != NULL, E_SYSTEM, "oom_adj change failed with %s.", strerror(errno));
229 fprintf(fP, "%d", adj);
236 _AulServer::GetAppType(const String& category)
240 HashMapT<String, int> map;
243 StringTokenizer strTok(category, L';');
246 while (strTok.HasMoreTokens())
248 result r = strTok.GetNextToken(token);
255 SysLog(NID_APP, "%d category items .", map.GetCount());
259 for (int i = 0; i < _NUM_CATEGORY; i++)
262 key = _CATEGORY_LIST[i].category;
263 result r = map.ContainsKey(key, b);
264 if (r == E_SUCCESS && b)
266 ret |= _CATEGORY_LIST[i].type;
273 bool _AulServer::IsInstalled(const AppId& appId)
276 packageId = _PackageManagerImpl::GetPackageIdByAppId(appId);
278 return _PackageManagerImpl::GetInstance()->IsPackageInstalled(packageId);
281 int _AulServer::CreateProcess(const AppId& appId)
283 SysTryReturn(NID_APP, appId.GetLength() > 10 && appId[10] == L'.', -1, E_INVALID_ARG, "[E_INVALID_ARG] Wrong appId %ls.", appId.GetPointer());
293 char path[FILENAME_MAX];
294 memset(path, '\0', FILENAME_MAX);
297 snprintf(path, FILENAME_MAX, _APP_PATH_FORMAT2, appId.GetPointer() + 11);
299 int ret = wcstombs(path + strlen(PATH_ROOT2), appId.GetPointer(), 10);
302 SysLogException(NID_APP, E_SYSTEM, "Launching service (%ls)(%s) failed with [%s].", appId.GetPointer(), path, strerror(errno));
306 snprintf(path, FILENAME_MAX, _APP_PATH_FORMAT2, appId.GetPointer() + 11);
308 int ret = wcstombs(path + strlen(PATH_ROOT2), appId.GetPointer(), 10);
311 SysLogException(NID_APP, E_SYSTEM, "Launching service (%ls)(%s) failed with [%s].", appId.GetPointer(), path, strerror(errno));
315 if (euidaccess(path, R_OK) != 0)
317 snprintf(path, FILENAME_MAX, _APP_PATH_FORMAT, appId.GetPointer() + 11);
319 ret = wcstombs(path + strlen(PATH_ROOT), appId.GetPointer(), 10);
322 SysLogException(NID_APP, E_SYSTEM, "Launching service (%ls)(%s) failed with [%s].", appId.GetPointer(), path, strerror(errno));
328 SysLog(NID_APP, "Launching %s.", path);
330 int currentPid = getpid();
332 //SetOomAdj(currentPid, -17); // set oom_adj to -17 for system service
334 prctl(PR_SET_PDEATHSIG, SIGTERM);
336 setpgid(currentPid, currentPid);
338 ret = execl(path, path, static_cast<char*>(NULL));
341 SysLogException(NID_APP, E_SYSTEM, "Launching service (%ls)(%s) failed with [%s].", appId.GetPointer(), path, strerror(errno));
351 _AulServer::IsUserPreferredAppForAppControlResolution(const AppId& appId)
353 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
355 int ret = appsvc_is_defapp(pAppId.get());
356 SysTryReturn(NID_APP, ret == 1, false, E_SUCCESS,"%ls is not an UserPreferredAppForAppControlResolution. ret(%d)", appId.GetPointer(), ret);
358 SysLog(NID_APP, "%ls is an UserPreferredAppForAppControlResolution.", appId.GetPointer());
363 _AulServer::ClearUserPreferenceForAppControlResolution(const AppId& appId)
365 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
367 int ret_aul = appsvc_unset_defapp(pAppId.get());
368 SysTryReturnResult(NID_APP, ret_aul == APPSVC_RET_OK, E_SYSTEM, "Fail to clear UserPreferredAppForAppControlResolution of %ls. ret_aul(%d)", appId.GetPointer(), ret_aul);
370 SysLog(NID_APP, "Succeed to clear UserPreferredAppForAppControlResolution of %ls.", appId.GetPointer());