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.
19 * @brief This is the implementation for the _Aul.cpp class.
25 #include <sys/prctl.h>
27 #include <unique_ptr.h>
31 #include <appsvc/appsvc.h>
33 #include <pkgmgr-info.h>
35 #include <FBaseObject.h>
36 #include <FBaseString.h>
37 #include <FBaseUtil.h>
38 #include <FBaseSysLog.h>
39 #include <FBaseColHashMapT.h>
40 #include <FAppPkgPackageInfo.h>
42 #include <FBaseRt_Process.h>
43 #include <FBase_StringConverter.h>
44 #include "FAppPkg_PackageManagerImpl.h"
45 #include "FApp_Types.h"
47 #include "FApp_AppArg.h"
48 #include "FApp_TemplateUtil.h"
51 using namespace Tizen::App::Package;
52 using namespace Tizen::Base;
53 using namespace Tizen::Base::Collection;
54 using namespace Tizen::Base::Runtime;
55 using namespace Tizen::Base::Utility;
59 const int _MAX_CATEGORY = 34;
61 // borrowed from app-svc/include/pri_key.h
62 #define APP_SVC_K_RES_VAL "__APP_SVC_K_RES_VAL__"
66 namespace Tizen { namespace App
71 const char category[_MAX_CATEGORY];
75 static const _CategoryList _CATEGORY_LIST[] =
77 {"home-screen", _APP_TYPE_HOME_APP},
78 {"lock-screen", _APP_TYPE_LOCK_APP},
79 {"ime", _APP_TYPE_IME_APP},
80 {"http://tizen.org/category/homeapp", _APP_TYPE_HOME_APP},
81 {"http://tizen.org/category/lockapp", _APP_TYPE_LOCK_APP},
82 {"http://tizen.org/category/ime", _APP_TYPE_IME_APP},
85 static const int _NUM_CATEGORY = sizeof(_CATEGORY_LIST) / sizeof(_CategoryList);
88 _Aul::GetConvertedResult(const int aul_ret, const char* pFunctionName)
96 SysLogException(NID_APP, r, "%s : Invalid argument.", pFunctionName);
101 SysLogException(NID_APP, r, "%s : Internal IPC error.", pFunctionName);
106 SysLogException(NID_APP, r, "%s : General error.", pFunctionName);
110 SysLog(NID_APP, "%s : successed.", pFunctionName);
118 _Aul::SendResult(bundle* b, appsvc_result_val res, bool isRaiseMode)
120 // to skip error handling, of appsvc_send_result, use aul_send_service_result() directly.
121 //int ret = appsvc_send_result(b, res);
123 char tmp[32] = {0, };
124 snprintf(tmp, 32, "%d", static_cast<int>(res));
125 appsvc_add_data(b, APP_SVC_K_RES_VAL, tmp);
129 _AppArg::UpdateRaiseMode(b);
132 const int aul_ret = aul_send_service_result(b);
134 result r = GetConvertedResult(aul_ret, "SendResult");
135 if (r == E_INVALID_ARG)
137 SysLog(NID_APP, "Converting internal exception to E_MAX_EXCEEDED.");
145 _Aul::IsRunning(const String& appId)
147 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(appId));
149 const bool isRunning = (aul_app_is_running(pPackageId.get()) > 0);
151 SysLog(NID_APP, "'%ls' %s running now.", appId.GetPointer(), (isRunning) ? "is" : "is NOT");
157 _Aul::TerminateApplicationByPid(int pid)
159 int ret_aul = aul_subapp_terminate_request_pid(pid);
161 return GetConvertedResult(ret_aul, "TerminateApplicationByPid");
165 TerminateApplicationIterFnCb(const aul_app_info* pAppInfo, void* pData)
167 const char* pStr = static_cast<const char*>(pData);
169 if (pStr && strncmp(pStr, pAppInfo->pkg_name, NATIVE_APP_MAX_APPID_LENGTH) == 0)
171 SysLog(NID_APP, "%s(%d) is terminated.", pAppInfo->pkg_name, pAppInfo->pid);
172 int ret_aul = aul_terminate_pid( pAppInfo->pid );
173 SetLastResult(_Aul::GetConvertedResult(ret_aul, "TerminateApplication"));
179 _Aul::TerminateApplication(const AppId& appId)
181 SetLastResult(E_OBJ_NOT_FOUND);
182 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
183 aul_app_get_running_app_info(TerminateApplicationIterFnCb, static_cast<void*>(pAppId.get()));
185 SysLog(NID_APP, "%ls terminated.", appId.GetPointer());
186 return GetLastResult();
190 _Aul::SetOomAdj(int pid, int adj)
192 // set oom_adj to -17 for system service
193 result r = E_SUCCESS;
194 char buf[FILENAME_MAX];
197 snprintf(buf, FILENAME_MAX, "/proc/%d/oom_adj", pid);
198 fP = fopen(buf, "w");
199 SysTryReturnResult(NID_APP, fP != NULL, E_SYSTEM, "oom_adj change failed with %s.", strerror(errno));
201 fprintf(fP, "%d", adj);
207 typedef void (* cbForVconf)(keynode_t* node, void *pData);
210 _Aul::SetPowerOffNotiListener( void (*powerOffCb)(void* node, void *pData), void *pData)
214 int ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, (cbForVconf)powerOffCb, pData);
215 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to set power off");
217 int heyFd = heynoti_init();
218 SysTryReturnResult(NID_APP, heyFd >= 0, E_SYSTEM, "heynoti_init failed.");
220 int ret = heynoti_subscribe(heyFd, "power_off_start", powerOffCb, pData);
221 SysTryReturnResult(NID_APP, ret >= 0, E_SYSTEM, "heynoti_subscribe failed.");
223 ret = heynoti_attach_handler(heyFd);
224 SysTryReturnResult(NID_APP, ret >= 0, E_SYSTEM, "heynoti_attach_handler failed.");
231 // _Aul::GetAppType is provided for installer usage
233 _Aul::GetAppType(const String& category)
237 HashMapT<String, int> map;
240 StringTokenizer strTok(category, L';');
243 while (strTok.HasMoreTokens())
245 result r = strTok.GetNextToken(token);
252 SysLog(NID_APP, "%d category items .", map.GetCount());
256 for (int i = 0; i < _NUM_CATEGORY; i++)
259 key = _CATEGORY_LIST[i].category;
260 result r = map.ContainsKey(key, b);
261 if (r == E_SUCCESS && b)
263 ret |= _CATEGORY_LIST[i].type;
272 _Aul::IsInstalled(const AppId& appId)
275 packageId = _PackageManagerImpl::GetPackageIdByAppId(appId);
277 return _PackageManagerImpl::GetInstance()->IsPackageInstalled(packageId);
282 _Aul::GetMainAppId(const char* appid)
284 pkgmgrinfo_appinfo_h handle = NULL;
285 int ret = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
286 if (ret != PMINFO_R_OK)
292 ret = pkgmgrinfo_appinfo_get_submode_mainid(handle, &mainid);
293 SysTryLog(NID_APP, ret == PMINFO_R_OK, "Cannot get main id for %s.", appid);
295 const String tmp = String(mainid);
296 pkgmgrinfo_appinfo_destroy_appinfo(handle);
302 _Aul::GetRealAppId(const AppId& appId)
305 appId.SubString(11, temp);
306 if (temp == String(SUBMODE_NAME))
308 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
309 const String& main = GetMainAppId(pAppId.get());
312 SysLog(NID_APP, "main Id is %ls", main.GetPointer());