我将在High Sierra版本10.13.4中的系统目录中创建/更新文件。
对于它,应该获得mac os的root权限。 我尝试使用此代码启动特权进程。
OSStatus LaunchPrivilegedProcess(NSString *path) {
OSStatus status;
AuthorizationRef authorizationRef;
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults, &authorizationRef);
if (status != errAuthorizationSuccess)
{
NSLog(@"Error Creating Initial Authorization: %d", status);
return status;
}
AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights rights = {1, &right};
AuthorizationFlags flags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;
status = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);
if (status != errAuthorizationSuccess)
{
NSLog(@"Copy Rights Unsuccessful: %d", status);
return status;
}
char *tool = path.UTF8String;
char *args[] = {NULL};
FILE *pipe = NULL;
status = AuthorizationExecuteWithPrivileges(authorizationRef, tool,
kAuthorizationFlagDefaults, args, &pipe);
if (status != errAuthorizationSuccess)
{
NSLog(@"Error: %d", status);
return status;
}
status = AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);
return status;
}
但它在AuthorizationExecuteWithPrivileges
失败,错误代码为:-60031(errAuthorizationToolExecuteFailure
)。
我该如何解决这个问题?
是否有其他方法可以获得更新系统文件的root权限?