From C you can use the SMJobRemove
function. If the job is in the system launchd context (i.e. it's in /Library/LaunchDaemons and is loaded—if not launched—at system startup) then you'll need to use Authorization Services to acquire the kSMRightModifySystemDaemons
right, and pass the authorization reference to this function.
AuthorizationItem authItem = { .name = kSMRightModifySystemDaemons,
.valueLength = 0,
.value = NULL,
.flags = kAuthorizationFlagDefaults };
AuthorizationRights authRights = { .count = 1,
.items = &authItem };
AuthorizationRef authorization = NULL;
OSStatus authResult = AuthorizationCreate(&authRights,
kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights,
&authorization);
if (authResult != errAuthorizationSuccess) {
NSLog(@"couldn't create AuthorizationRef: error %i", authResult);
} else {
CFErrorRef error = NULL;
BOOL removeResult = SMJobRemove(kSMDomainSystemLaunchd, jobLabel, authorization, waitOrNot, &error);
AuthorizationFree(authorization, kAuthorizationFlagDefaults);
// handle either success or failure
}
The waitOrNot
flag should be set to YES
if you want to block until the job has been unloaded—this could potentially take a long time.