我正在尝试从我的 Mac 应用程序加载 LaunchDaemon plist。当我尝试在终端中加载它时,它会成功加载,但是当我尝试通过我的代码加载它时,它不起作用。
这是我的代码:
OSStatus status;
AuthorizationRef authorizationRef;
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authorizationRef);
if (status != errAuthorizationSuccess)
{
NSString *err = [NSString stringWithFormat:@"Error Creating Initial Authorization : %d", status];
[self ShowErrorAndExit:err];
}
//kAuthorizationRightExecute == "system.privilege.admin"
AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights rights = {1, &right};
AuthorizationFlags flags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;
// Call AuthorizationCopyRights to determine or extend the allowable rights.
status = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);
if (status != errAuthorizationSuccess)
{
NSString *err = [NSString stringWithFormat:@"Sorry, we need Administrator priviliges to Continue"];
[self ShowErrorAndExit:err];
}
// After getting the Authorization Reference, executing the launchctl to load my LaunchDaemon
char *daemonplist="/Library/LaunchDaemons/com.myappdaemon.plist";
char *launchctl="/bin/launchctl";
char *launchdArgs[]={"load",daemonplist,NULL};
FILE *ldpipe=NULL;
OSStatus;
status=AuthorizationExecuteWithPrivileges(authorizationRef,launchctl,kAuthorizationFlagDefaults,launchdArgs,&ldpipe);
if(status!=errAuthorizationSuccess)
{
NSLog(@"Error:%d",status);
}
if(status==errAuthorizationSuccess)
{
NSLog(@" succesfully loaded the daemon plist);
}
status=AuthorizationFree(authorizationRef,kAuthorizationFlagDestroyRights);
**
我的清单:
**
<key> Label</key>
<string>com.myappdaemon</string>
<key>Program</key>
<string>/Applications/myapplication.app/Contents/MacOS/myappdaemon</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/myapplication.app/Content/MacOs/myappdaemon</string>
</array>
<key>WorkingDirectory</key>
<string>/Library/myapplication</string>
<key>StandardOutPath</key>
<string>/dev/null</string>
<key>StandardErrorPath</key>
<string>/dev/null</string>
<key>RunAtLoad</key>
</true>
<key>keepAlive</key>
<true/>
Thing is it shows the daemon plist is loaded successfully, but it's not.
并且在系统重新启动后,守护程序启动正常。plist 在终端中加载良好。我什至尝试使用“-w”和“-F”来强制加载守护进程,但它根本不加载。奇怪的是它只是一直说守护程序已加载。现在我在这里做错了什么..?