OSX launchctl programmatically as root
Posted
by
Lukas1
on Stack Overflow
See other posts from Stack Overflow
or by Lukas1
Published on 2014-08-19T09:26:20Z
Indexed on
2014/08/19
10:20 UTC
Read the original article
Hit count: 185
I'm trying to start samba service using launchctl
from OSX app as root, but I get error status -60031
. I can run without problems the command in Terminal:
sudo launchctl load -F /System/Library/LaunchDaemons/com.apple.AppleFileServer.plist`
In the objective-c code, I'm using (I know it's deprecated, but that really shouldn't be the issue here) AuthorizationExecuteWithPrivileges
method.
Here's the code:
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
}
I have also tried using different flags then kAuthorizationFlagDefaults, but that led to either the same problem or error code -60011
-> invalid flags.
What am I doing wrong here, please?
© Stack Overflow or respective owner