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>
34 #include <FBaseObject.h>
35 #include <FBaseString.h>
36 #include <FBaseUtil.h>
37 #include <FBaseSysLog.h>
38 #include <FBaseColHashMapT.h>
39 #include <FAppPkgPackageInfo.h>
41 #include <FBaseRt_Process.h>
42 #include <FBase_StringConverter.h>
43 #include "FAppPkg_PackageManagerImpl.h"
44 #include "FApp_Types.h"
46 #include "FApp_AppArg.h"
47 #include "FApp_TemplateUtil.h"
50 using namespace Tizen::App::Package;
51 using namespace Tizen::Base;
52 using namespace Tizen::Base::Collection;
53 using namespace Tizen::Base::Runtime;
54 using namespace Tizen::Base::Utility;
58 const int _MAX_CATEGORY = 34;
60 // borrowed from app-svc/include/pri_key.h
61 #define APP_SVC_K_RES_VAL "__APP_SVC_K_RES_VAL__"
65 namespace Tizen { namespace App
70 const char category[_MAX_CATEGORY];
74 static const _CategoryList _CATEGORY_LIST[] =
76 {"home-screen", _APP_TYPE_HOME_APP},
77 {"lock-screen", _APP_TYPE_LOCK_APP},
78 {"ime", _APP_TYPE_IME_APP},
79 {"http://tizen.org/category/homeapp", _APP_TYPE_HOME_APP},
80 {"http://tizen.org/category/lockapp", _APP_TYPE_LOCK_APP},
81 {"http://tizen.org/category/ime", _APP_TYPE_IME_APP},
84 static const int _NUM_CATEGORY = sizeof(_CATEGORY_LIST) / sizeof(_CategoryList);
87 _Aul::GetConvertedResult(const int aul_ret, const char* pFunctionName)
95 SysLogException(NID_APP, r, "%s : Invalid argument.", pFunctionName);
100 SysLogException(NID_APP, r, "%s : Internal IPC error.", pFunctionName);
105 SysLogException(NID_APP, r, "%s : General error.", pFunctionName);
109 SysLog(NID_APP, "%s : successed.", pFunctionName);
117 _Aul::SendResult(bundle* b, appsvc_result_val res, bool isSubMode, bool isServiceApp)
119 // to skip error handling, of appsvc_send_result, use aul_send_service_result() directly.
120 //int ret = appsvc_send_result(b, res);
122 char tmp[32] = {0, };
123 snprintf(tmp, 32, "%d", static_cast<int>(res));
124 appsvc_add_data(b, APP_SVC_K_RES_VAL, tmp);
128 _AppArg::UpdateSubMode(b);
133 _AppArg::UpdateServiceApp(b);
136 const int aul_ret = aul_send_service_result(b);
138 result r = GetConvertedResult(aul_ret, "SendResult");
139 if (r == E_INVALID_ARG)
141 SysLog(NID_APP, "Converting internal exception to E_MAX_EXCEEDED.");
149 _Aul::IsRunning(const String& appId)
151 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(appId));
153 const bool isRunning = (aul_app_is_running(pPackageId.get()) > 0);
155 SysLog(NID_APP, "'%ls' %s running now.", appId.GetPointer(), (isRunning) ? "is" : "is NOT");
161 _Aul::TerminateApplicationByPid(int pid)
163 int ret_aul = aul_subapp_terminate_request_pid(pid);
165 return GetConvertedResult(ret_aul, "TerminateApplicationByPid");
169 TerminateApplicationIterFnCb(const aul_app_info* pAppInfo, void* pData)
171 const char* pStr = static_cast<const char*>(pData);
173 if (pStr && strncmp(pStr, pAppInfo->pkg_name, NATIVE_APP_MAX_APPID_LENGTH) == 0)
175 SysLog(NID_APP, "%s(%d) is terminated.", pAppInfo->pkg_name, pAppInfo->pid);
176 int ret_aul = aul_terminate_pid( pAppInfo->pid );
177 SetLastResult(_Aul::GetConvertedResult(ret_aul, "TerminateApplication"));
183 _Aul::TerminateApplication(const AppId& appId)
185 SetLastResult(E_OBJ_NOT_FOUND);
186 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
187 aul_app_get_running_app_info(TerminateApplicationIterFnCb, static_cast<void*>(pAppId.get()));
189 SysLog(NID_APP, "%ls terminated.", appId.GetPointer());
190 return GetLastResult();
194 _Aul::SetOomAdj(int pid, int adj)
196 // set oom_adj to -17 for system service
197 result r = E_SUCCESS;
198 char buf[FILENAME_MAX];
201 snprintf(buf, FILENAME_MAX, "/proc/%d/oom_adj", pid);
202 fP = fopen(buf, "w");
203 SysTryReturnResult(NID_APP, fP != NULL, E_SYSTEM, "oom_adj change failed with %s.", strerror(errno));
205 fprintf(fP, "%d", adj);
211 typedef void (* cbForVconf)(keynode_t* node, void *pData);
214 _Aul::SetPowerOffNotiListener( void (*powerOffCb)(void* node, void *pData), void *pData)
218 int ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, (cbForVconf)powerOffCb, pData);
219 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to set power off");
221 int heyFd = heynoti_init();
222 SysTryReturnResult(NID_APP, heyFd >= 0, E_SYSTEM, "heynoti_init failed.");
224 int ret = heynoti_subscribe(heyFd, "power_off_start", powerOffCb, pData);
225 SysTryReturnResult(NID_APP, ret >= 0, E_SYSTEM, "heynoti_subscribe failed.");
227 ret = heynoti_attach_handler(heyFd);
228 SysTryReturnResult(NID_APP, ret >= 0, E_SYSTEM, "heynoti_attach_handler failed.");
235 // _Aul::GetAppType is provided for installer usage
237 _Aul::GetAppType(const String& category)
241 HashMapT<String, int> map;
244 StringTokenizer strTok(category, L';');
247 while (strTok.HasMoreTokens())
249 result r = strTok.GetNextToken(token);
256 SysLog(NID_APP, "%d category items .", map.GetCount());
260 for (int i = 0; i < _NUM_CATEGORY; i++)
263 key = _CATEGORY_LIST[i].category;
264 result r = map.ContainsKey(key, b);
265 if (r == E_SUCCESS && b)
267 ret |= _CATEGORY_LIST[i].type;
276 _Aul::IsInstalled(const AppId& appId)
279 packageId = _PackageManagerImpl::GetPackageIdByAppId(appId);
281 return _PackageManagerImpl::GetInstance()->IsPackageInstalled(packageId);
286 _Aul::GetRealAppId(const AppId& appId)
289 // [INFO] ugly code for submode callee
290 appId.SubString(11, temp);
291 if (temp == L"_AppControl")
294 appId.SubString(0, 10, id);
295 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(id);
297 const String retVal = id + L'.' + name;
298 SysLog(NID_APP, "Converted caller Id is %ls", retVal.GetPointer());