【发布时间】:2014-10-12 08:17:22
【问题描述】:
我正在尝试使用来自 OSX 应用程序的 launchctl 作为 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 -> 标志无效。
请问我这里做错了什么?
【问题讨论】:
标签: objective-c macos cocoa shell