从用户上下文中的守护进程启动代理 [英] launch agent from daemon in user context

查看:157
本文介绍了从用户上下文中的守护进程启动代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个启动守护进程在Mac OSX 10.6机器的登录上下文中运行。我想为该代理程序中的每个用户启动代理程序,并使代理程序在用户的登录上下文中运行。但是,我需要非常仔细地控制代理应用程序的精确参数,这就是为什么我不能使用launchd代理,就像我对守护进程一样。



如何我创建一个在登录用户的上下文中运行的进程?我已经修改了 seteuid setuid 调用,但这些不会更改代理应用程序的执行上下文。 / p>

我知道这不是推荐的Apple做事方式,但我真的没有选择 - 守护进程应用程序的设计是相当不灵活(它必须运行在许多不同的系统)。是否有一个解决方法,允许我在登录用户的GUI上下文中运行一个应用程序,从在登录上下文中运行的守护进程?



使用C ++,Carbon& Cocoa。

解决方案

使用launchd代理。所有其他方式将打击你的头脑与无用的细节和隐藏的把戏。一般来说,这是非常复杂的任务与许多角落的情况,是很难正确实施。 Launchd代理将允许您专注于您的任务,并节省大量的时间。



您最好的解决方案是重写代理不使用命令行,但连接to launchd守护进程,并要求正确的选项/设置。



如果不可能或很难你可以写wrapper launchd代理,将启动连接到守护进程,然后使用正确的命令行启动原代理。



如果你认为实现连接到守护进程机制太难了,也许它是,但它比从守护进程在其他会话中启动代理要容易得多。(



但是如果你真的没有什么需要它在脏的方式,你可以玩launchctl bsexec。一些工作示例:
启动/停止所有使用GUI会话的用户的launchd代理。(而不是launchctl load,它可以在会话上下文中启动任何可执行文件)。





通过play with launchctl bsexec我的意思是:



ps aux | grep loginwindow | grep用户| awk'{system(sudo launchctl bsexec$ 2sudo -u user /Applications/TextEdit.app/Contents/MacOS/TextEdit)}'



在您想要的会话中查找一些应用程序,获取其PID并调用launchctl bsexec在同一会话中运行您想要的。以上示例将在登录的用户会话上启动TextEdit,即使该行在另一个登录用户帐户或服务下执行。



但是我在Lion上测试了它不工作。它只适用于Leopards(10.5 / 10.6)对我来说。
我试图说 - 不使用发射代理会导致你的屁股不断疼痛,没有别的。在十几个这样的事情,我们切换发射代理,现在快乐:)


I have a launch daemon that runs in the login context of a Mac OSX 10.6 machine. I want to launch an agent for each user from that agent, and have the agent run in the user's login context. However, I need to control the precise arguments to the agent application very carefully, which is why I cannot use a launchd agent, like I have for the daemon.

How can I create a process that runs in the context of the logged in user? I've trued seteuid and setuid calls, but these don't change the execution context of the agent application.

I'm aware that this is not the recommended Apple way of doing things, but I don't really have a choice in the matter - the design of the daemon application is pretty inflexible (it has to run across many different systems). Is there a workaround that allows me to run an application in the GUI context of a logged in user, from a daemon which is running in the login context?

I'm using C++, Carbon & Cocoa.

解决方案

Use launchd agent. All other ways will blow your mind with tons of useless details and hidden tricks. In general it is very complex task with many corner cases and is very hard to implement properly. Launchd agent will allow you to concentrate on your task and save a lot of time.

The best solution for you is to rewrite agent not to use command line, but to connect to launchd daemon and ask for proper options/settings.

If it is not possible or hard you can write wrapper launchd agent that will on start connect to daemon, ask for options and then launch original agent with proper command line.

If you think that it is too hard to implement "connect to daemon" machinery... maybe it is, but it is much easier than launching agent in other session from daemon (when implemented right with different corner cases support).

But if your really-really-no-matter-what want it in dirty way, you can play with "launchctl bsexec". Some example that work: Starting/stopping a launchd agent for all users with GUI sessions. (instead of "launchctl load" it can launch any executable in session context).

Some update on this.

By "play with launchctl bsexec" I mean something like that:

ps aux | grep loginwindow | grep user | awk '{ system("sudo launchctl bsexec "$2" sudo -u user /Applications/TextEdit.app/Contents/MacOS/TextEdit") }'

Find some app in session you want, take its PID and call "launchctl bsexec" to run what you want in the same session. Example above will launch TextEdit in logged on "user" session even if that line executed under another logged on user acount or from service.

But I tested it on Lion - it doesn't work. It works only for Leopards (10.5 / 10.6) for me. That what I tried to say - not using launch agents will cause constant pain in your ass and nothing else. After dozen of such things we switched entirly on launch agents and are happy now :)

这篇关于从用户上下文中的守护进程启动代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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