0

我正在开发一个将更新 /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");
}

根据文档,我的帮助工具 com.fictitiousnonsense.MeddoHelper 安装到 /Library/PrivilegedHelperTools/com.fictitiousnonsense.MeddoHelper 并且它的 plist 文件安装到 /Library/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.

我可以从终端手动运行助手并且它可以工作。为什么launchd不运行它?我已经运行了 Apple 提供的 SMJobBlessApp 示例应用程序,它运行良好,但我不会,而且我找不到不同的。

作为参考,整个代码在这里:https ://github.com/varikin/meddo 。

4

1 回答 1

0

您可以dtruss在命令行上使用来跟踪 launchd。它将向您显示由 launchd 执行的所有系统调用,其中一个应该尝试访问您的特权助手。跟踪应该显示哪些系统调用准确地失败了,这使得 launchd 认为助手不在那里。我猜这是某种许可问题,顺便说一句。

于 2012-10-05T17:22:34.950 回答