Mac OS X:如果守护程序以root身份运行,则无法通过守护程序使用NSWorkspace启动应用程序 [英] Mac OS X: Launching an app using NSWorkspace from a daemon doesn't work if the daemon is run as root

查看:225
本文介绍了Mac OS X:如果守护程序以root身份运行,则无法通过守护程序使用NSWorkspace启动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Xcode创建了一个命令行工具应用程序. 在该应用程序中,我使用了NSWorkspace来启动另一个应用程序捆绑包(.app),如此处建议的那样.

I created a Command Line Tool app using Xcode. In that app, I used NSWorkspace to launch another application bundle (.app) as suggested here. MacOsX: How to launch an application (.app) from a "Command Line Tool" type of app

在我尝试使用launchctl将该命令行工具应用程序作为守护程序启动之前,所有方法似乎都可以正常工作. 如果守护程序以当前登录的用户身份运行,则命令行工具应用程序可以很好地启动外部应用程序. 如果守护程序以root用户身份运行,则命令行工具应用程序将无法启动外部应用程序. 如果守护程序以root身份运行,则使用NSWorkspace打开应用程序似乎不起作用.

All seem to work fine until I tried to start that Command Line Tool app as a daemon using launchctl. If the daemon is run as the currently logged in user, then the Command Line Tool app launches the external app just fine. If the daemon is run as root, then the Command Line Tool app cannot launch the external app. Using NSWorkspace to open an app doesn't seem to work if the daemon is run as root.

有人知道从以root用户身份运行的守护进程中打开另一个应用程序的正确方法吗?

Does anyone know the correct way to open another app from a daemon that's running as root?

推荐答案

我终于通过使用此链接中提到的代码来获取控制台用户"的uid和gid使其正常工作:
https://superuser.com/questions/180819/您如何找到当前登录的用户在OS-X-GUI中的声音.

I finally got it working by using the code mentioned in this link to get the "console user's" uid and gid:
https://superuser.com/questions/180819/how-can-you-find-out-the-currently-logged-in-user-in-the-os-x-gui.

该链接显示了如何使用SCDynamicStoreCreate()SCDynamicStoreCopyConsoleUser()来获取控制台用户"的uid和gid的示例.

The link shows an example of how to use SCDynamicStoreCreate() and SCDynamicStoreCopyConsoleUser() to get "console user's" uid and gid.

获取uid和gid之后,只需在使用NSWorkspace打开应用程序之前将uid和gid设置为控制台用户的uid和gid即可.

After getting the uid and gid, just set the uid and gid to those of the console user before using NSWorkspace to open an app and that did the trick for me.

这篇关于Mac OS X:如果守护程序以root身份运行,则无法通过守护程序使用NSWorkspace启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆