我正在开发一个将更新/etc/hosts的应用程序。为此,我发现评估权限的首选方法是通过SMJobBless安装帮助工具。下面的代码可以正常工作:
BOOL result = NO;
AuthorizationItem authItem = { kSMRightBlessPrivilegedHelper, 0, NULL, 0 };
AuthorizationRights authRights = { 1, &authItem };
AuthorizationFlags flags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;
AuthorizationRef authRef = NULL;
CFErrorRef error = NULL;
OSStatus status = AuthorizationCreate(&authRights, kAuthorizationEmptyEnvironment, flags, &authRef);
if (status == errAuthorizationSuccess) {
result = SMJobBless(kSMDomainSystemLaunchd, (CFStringRef)@"com.fictitiousnonsense.MeddoHelper", authRef, &error);
} else {
NSLog(@"Failed to authorize");
}
if (error != NULL) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"I think it worked");
}我的助手工具LaunchDaemons安装在/Library/PrivilegedHelperTools/com.fictitiousnonsense.MeddoHelper上,它的plist文件安装在/ com.fictitiousnonsense.MeddoHelper /LaunchDaemons上,所有这些都是根据文档进行的。问题是,然后在我的控制台中会出现以下内容:
9/24/12 11:04:41.237 PM launchdadd[9082]: Could not open /Library/PrivilegedHelperTools/com.fictitiousnonsense.MeddoHelper (open() error 2: No such file or directory)
9/24/12 11:04:41.237 PM launchdadd[9082]: FAILURE: The path /Library/PrivilegedHelperTools/com.fictitiousnonsense.MeddoHelper does not exist on-disk.我可以从终端手动运行帮助程序,它可以工作。为什么启动不能运行它?我已经运行了苹果提供的SMJobBlessApp示例应用程序,它工作得很好,但我不会,我找不到不同的。
作为参考,完整的代码在这里:https://github.com/varikin/meddo。
发布于 2012-10-06 01:22:35
您可以在命令行上使用dtruss来跟踪启动。它将向您显示由launchd执行的所有syscall,其中一个应该尝试访问您的特权助手。跟踪应该准确地显示哪个syscall失败了,这会让launchd认为助手不在那里。我猜这是某种许可问题,顺便说一句。
https://stackoverflow.com/questions/12576162
复制相似问题