Mac OS X:使用 PID 获取任意正在运行的应用程序的详细进程信息(特别是其启动参数)

发布于 2024-09-01 11:01:47 字数 839 浏览 3 评论 0原文

我正在尝试检测特定应用程序何时启动。

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

对于大多数应用程序来说,应用程序包的名称就足够了。我有一个“已知应用程序”列表,我将其与通知中传递的名称进行交叉检查。

这种方法工作得很好,直到您遇到一个应用程序充当使用命令行参数启动另一个应用程序的代理。

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

由于更多基于 Source 的游戏即将登陆 Mac,我想他们将使用相同的方法来启动,使用 -game 参数有效运行 hl2_osx 应用程序。

有没有一种好的方法可以使用 Cocoa API 获取参数(及其参数)列表?

NSProcessInfo 很接近,提供了一个“-arguments”方法,但仅提供其自身进程的信息...

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

有什么可以填补两者之间的空白吗?

我试图不沿着生成 NSTask 的路线来运行 ps -p [pid] 并解析输出......我更喜欢更高的东西等级。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記柔刀 2024-09-08 11:01:47

您可以使用 ps 使用的任何内容,尽管它不是基于可可的。根据 辛格 , ps 基于 kvm 和 sysctl 调用。仔细查看源代码,相关调用似乎为 kvm_openfileskvm_getprocskvm_getargv。要获取命令行参数,请首先调用 kvm_openfiles 来访问内核内存空间,然后使用 kvm_getprocs 获取内核进程信息,然后使用 kvm_getargv >。

ps 中使用 sysctl 似乎与您的目标不太相关;它用于获取其他信息,例如组 ID 和父进程 ID。使用的特定 sysctl 名称为 {CTL_KERN, KERN_PROC, KERN_PROC_which, flags},其中 which 指定进程过滤器(例如 ALLPID)和 flags 是过滤器的参数(详细信息位于 <代码>sysctl手册页)。

OS X 不支持 procfs,但 Singh 开发了一个 基于 FUSE 的版本,在 GPLv2 下发布。如果您将其与您的应用程序捆绑在一起,则还必须根据 GPLv2 发布它。 MacFUSE 的大部分内容都是在 BSD 风格许可下发布的,因此可以与您的应用程序一起分发,而不将其开源(fusefs/fuse_nodehash.c 在 Apple 的开源许可证下发布,但它也允许链接到闭源应用程序)。

问题“使用 C 在 OS X 中获取其他进程'argv< /a>”应该有用,因为它有使用 kvm 和 sysctl 的示例代码。 TN 2050“无需轮询即可观察进程生命周期” 也可用于你。

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.

似梦非梦 2024-09-08 11:01:47

不 - 运行 ps 是你最好的选择。 OS X 不支持标准进程信息接口(OS X 10.4 中提供了 nooop 版本,但此后被删除),并且私有接口可能会在 OS X 修订版之间发生变化。

如果您愿意将自己锁定在单个 OS X 版本中,则所有源代码都可用,例如 pslibproc;您还需要以 root 身份运行。

Nope - running ps is your best bet. Standard process info interfaces aren't supported on OS X (noop versions were provided in OS X 10.4, but removed thereafter) and the private interfaces are likely to change between OS X revisions.

If you're willing to lock yourself into a single OS X version, all the source is available, for example for ps or libproc; you'll also need to run as root.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文