在Mac OS X上以另一个用户身份启动程序

时间:2013-03-31 08:18:53

标签: objective-c macos runas usersession

在具有多个用户的Mac OS X中,是否有任何API或一段代码在Objective C中作为另一个用户运行进程?

1 个答案:

答案 0 :(得分:2)

您需要使用 AuthorizationExecuteWithPrivileges ,请查看例如在http://www.michaelvobrien.com/blog/2009/07/authorizationexecutewithprivileges-a-simple-example/

// Create authorization reference
AuthorizationRef authorizationRef;
OSStatus status;
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
                             kAuthorizationFlagDefaults, &authorizationRef);

// Run the tool using the authorization reference
char *tool = "/sbin/dmesg";
char *args[] = {NULL};
FILE *pipe = NULL;
status = AuthorizationExecuteWithPrivileges(authorizationRef, tool,
                                            kAuthorizationFlagDefaults, args, &pipe);