本文介绍了Mac OS X:使用其PID获取任意运行的应用程序的详细过程信息(特别是其启动参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



目前我使用 NSWorkspace 注册启动应用程序通知。我还使用 runningApplications 方法获取当应用程序启动时当前正在运行的应用程序。



对于大多数应用程序,应用程序包的名称就足够了。我有一个已知的应用程序,我交叉检查通过的通知中的名称。



这很好,直到你遇到一个应用程序,使用命令行参数启动另一个应用程序的代理。



示例:Mac上新发布的Portal没有专用的应用程序包。 Steam可以创建一个快捷方式,只需使用 -game 参数启动 hl2_osx code> portal 作为参数。



由于更多的源代码游戏正在转向Mac,我想他们会使用同样的方法启动,用 -game 参数有效地运行 hl2_osx 应用程序。



有一个很好的方法来使用Cocoa API获取参数列表(及其参数)?



NSProcessInfo 关闭,提供一个`-arguments'方法,但只提供自己的进程的信息...



NSRunningApplication 提供使用PID获取有关任意应用程序的信息,但没有命令行参数的能力...



有什么填充两者之间的差距?



我试图不沿着产生一个 NSTask ps -p [pid] 并解析输出...我更喜欢更高的级别。

解决方案

你可以使用任何 ps 使用,虽然它不是基于可可。根据, ps 是基于kvm和sysctl调用。通过,优先调用似乎是,和 kvm_getargv 。要获得命令行参数,首先调用 kvm_openfiles 以访问内核内存空间,然后使用 kvm_getprocs 内核进程信息,然后 kvm_getargv



ps 中使用 sysctl 到你的目标它用于获取其他信息,例如组ID和父进程ID。所使用的特定系统名称是 {CTL_KERN,KERN_PROC,KERN_PROC_ flags } ,其中指定过程过滤器(例如 ALL PID )和 (详情请参阅手册页)。



OS X没有支持procfs,但Singh开发了,在GPLv2下发布。如果你将它与你的应用程序捆绑在一起,你也必须在GPLv2下释放它。大多数MacFUSE是根据应该是有用的,因为它有使用kvm和sysctl的示例代码。 也可能对您的使用。


I am trying to detect when particular applications are launched.

Currently I am using NSWorkspace, registering for the "did launch application" notification. I also use the runningApplications method to get apps that are currently running when my app starts.

For most apps, the name of the app bundle is enough. I have a plist of "known apps" that I cross check with the name of that passed in the notification.

This works fine until you come across an app that acts as a proxy for launching another application using command line arguments.

Example: The newly released Portal on the Mac doesn't have a dedicated app bundle. Steam can create a shortcut, which serves as nothing more than to launch the hl2_osx app with the -game argument and portal as it's parameter.

Since more Source based games are heading to the Mac, I imagine they'll use the same method to launch, effectively running the hl2_osx app with the -game argument.

Is there a nice way to get a list of the arguments (and their parameters) using a Cocoa API?

NSProcessInfo comes close, offering an `-arguments' method, but only provides information for its own process...

NSRunningApplication offers the ability to get information about arbitrary apps using a PID, but no command line args...

Is there anything that fills the gap between the two?

I'm trying not to go down the route of spawning an NSTask to run ps -p [pid] and parsing the output... I'd prefer something more high level.

解决方案

You could use whatever ps uses, though it isn't cocoa based. According to Singh, ps is based on kvm and sysctl calls. Pouring over the source, the pertinant calls seem to be kvm_openfiles, kvm_getprocs and kvm_getargv. To get the command line arguments, first call kvm_openfiles to get access to the kernel memory space, then use kvm_getprocs to get kernel process info, then kvm_getargv.

The use of sysctl in ps seems less relevant to your goal; it's used to get other information, such as the group ID and parent proces ID. The particular sysctl name used is {CTL_KERN, KERN_PROC, KERN_PROC_which, flags}, where which specifies a process filter (e.g. ALL, PID) and flags are arguments for the filter (the details are in the sysctl man page).

OS X doesn't have support procfs, but Singh developed a FUSE based version, released under GPLv2. If you bundle it with your application, you'll have to release it under GPLv2 as well. Most of MacFUSE is released under a BSD-style license, so it can be distributed with your app without making it open source (fusefs/fuse_nodehash.c is released under Apple's open source license, but it also allows linking to closed source apps).

The question "Get other process' argv in OS X using C" should be of use, as it has sample code using kvm and sysctl. TN 2050 "Observing Process Lifetimes Without Polling" may also be of use to you.

这篇关于Mac OS X:使用其PID获取任意运行的应用程序的详细过程信息(特别是其启动参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:18