我正在尝试使用launchctl
从OSX应用程序作为root启动Samba服务,但是我收到了错误状态-60031
。我可以在终端中无问题地运行该命令:
sudo launchctl load -F /System/Library/LaunchDaemons/com.apple.smbd.plist
在Objective-C代码中,我正在使用(我知道它已经被弃用了,但这真的不应该是问题)
AuthorizationExecuteWithPrivileges
方法。以下是代码:
NSString *command = @"launchctl";
// Conversion of NSArray args to char** args here (not relevant part of the code)
OSStatus authStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &_authRef);
if (authStatus != errAuthorizationSuccess) {
NSLog(@"Failed to create application authorization: %d", (int)authStatus);
return;
}
FILE* pipe = NULL;
AuthorizationFlags flags = kAuthorizationFlagDefaults;
AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights rights = {1, &right};
// Call AuthorizationCopyRights to determine or extend the allowable rights.
OSStatus stat = AuthorizationCopyRights(_authRef, &rights, NULL, flags, NULL);
if (stat != errAuthorizationSuccess) {
NSLog(@"Copy Rights Unsuccessful: %d", (int)stat);
return;
}
OSStatus status = AuthorizationExecuteWithPrivileges(_authRef,
command.UTF8String,
flags,
args,
&pipe);
if (status != errAuthorizationSuccess) {
NSLog(@"Error executing command %@ with status %d", command, status);
} else {
// some other stuff
}
我还尝试使用不同的标志,而不是 kAuthorizationFlagDefaults,但这导致了相同的问题或错误码 -60011
-> 无效标志。
请问我在这里做错了什么?