580
社区成员
发帖
与我相关
我的任务
分享OpenHarmony,系统用户,应用,安装
环境:OpenHarmony 3.2 Beta2 及以后
问题现象:系统启动后无法通过bm安装应用到系统用户下
在应用配置app.json5中配置相关设置如下:
{
...
"singleton": true,
...
}
使用系统用户安装应用,执行命令:
bm install -u 0 -p /data/LogMonitor-default-signed.hap
安装应用返回成功信息:
install bundle successfully.
安装应用返回失败信息:
error: failed to install bundle.
error: install failed due to zero user can only install singleton app
删除foundation/bundlemanager/bundle_framework/services/bundlemgr/src/module_profile.cpp文件中的预装应用条件项,只判断当应用为系统应用时,允许系统用户安装应用。修改后源码如下:
bool ToApplicationInfo(const Profile::ModuleJson &moduleJson, ApplicationInfo &applicationInfo,
bool isPreInstallApp, const BundleExtractor &bundleExtractor)
{
...
if (applicationInfo.isSystemApp) {
applicationInfo.keepAlive = app.keepAlive;
applicationInfo.singleton = app.singleton;
applicationInfo.userDataClearable = app.userDataClearable;
if (app.removable.first) {
applicationInfo.removable = app.removable.second;
} else {
applicationInfo.removable = false;
}
applicationInfo.accessible = app.accessible;
}
...
}
通过以上修改后安装的系统,可以使用系统用户安装应用。

foundation/bundlemanager/bundle_framework/services/bundlemgr/src/base_bundle_installer.cpp的InnerProcessBundleInstall方法执行时返回错误信息。ErrCode BaseBundleInstaller::InnerProcessBundleInstall(std::unordered_map<std::string, InnerBundleInfo> &newInfos,
InnerBundleInfo &oldInfo, const InstallParam &installParam, int32_t &uid)
{
···
// singleton app can only be installed in U0 and U0 can only install singleton app.
bool isSingleton = newInfos.begin()->second.IsSingleton();
if ((isSingleton && (userId_ != Constants::DEFAULT_USERID)) ||
(!isSingleton && (userId_ == Constants::DEFAULT_USERID))) {
APP_LOGW("singleton(%{public}d) app(%{public}s) and user(%{public}d) are not matched.",
isSingleton, bundleName_.c_str(), userId_);
return ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON;
}
···
}
通过源码得知,系统用户安装应用需要设置singleton配置为true,才可继续安装。
根据逻辑向上寻找在foundation/bundlemanager/bundle_framework/services/bundlemgr/src/module_profile.cpp的ToApplicationInfo中设置singleton的参数值。
bool ToApplicationInfo(const Profile::ModuleJson &moduleJson, ApplicationInfo &applicationInfo,
bool isPreInstallApp, const BundleExtractor &bundleExtractor)
{
...
if (applicationInfo.isSystemApp && isPreInstallApp) {
applicationInfo.keepAlive = app.keepAlive;
applicationInfo.singleton = app.singleton;
applicationInfo.userDataClearable = app.userDataClearable;
if (app.removable.first) {
applicationInfo.removable = app.removable.second;
} else {
applicationInfo.removable = false;
}
applicationInfo.accessible = app.accessible;
}
...
}
ToApplicationInfo方法,发现只有当应用为系统应用且为预装应用时才会设置singleton参数。应用在签名时设置P7b生成json文件的app-feature项参数为ohos_system_app或hos_system_app后,签名安装的应用会被认定为系统应用。