我正在尝试从我的Mac应用程序加载Launch 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.
系统重启后,守护进程正常启动。钳子在终端装载良好。 我甚至试过'-w'和amp; '-F'强制加载守护进程,但它不加载 一点都不奇怪的是它只是说守护进程被加载了。 现在我在这里做错了什么..?
答案 0 :(得分:-1)
AuthorizationExecuteWithPrivileges
已弃用。您可以使用applescript
do shell script "launchctl load /Library/LaunchDaemons/com.yourcompany.app.plist" with administrator privileges