8

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.

4

2 に答える 2

12

psココアベースではありませんが、どのような用途でも使用できます。Singhによると、psはkvmとsysctlの呼び出しに基づいています。ソースに注ぐと、関連する呼び出しは、、およびのようにkvm_openfiles見えkvm_getprocsますkvm_getargv。コマンドライン引数を取得するには、最初にを呼び出しkvm_openfilesてカーネルメモリスペースにアクセスし、次にを使用kvm_getprocsしてカーネルプロセス情報を取得し、次にを呼び出しますkvm_getargv

sysctlinの使用はps、目標との関連性が低いようです。これは、グループIDや親プロセスIDなどの他の情報を取得するために使用されます。使用される特定のsysctl名はです。ここで、これはプロセスフィルター(たとえば、)を指定し、フラグはフィルターの引数です(詳細はマニュアルページにあります)。{CTL_KERN, KERN_PROC, KERN_PROC_which, flags}ALLPIDsysctl

OS Xにはサポートprocfsがありませんが、SinghはGPLv2でリリースされたFUSEベースのバージョンを開発しました。アプリケーションにバンドルする場合は、GPLv2でもリリースする必要があります。MacFUSEのほとんどはBSDスタイルのライセンスでリリースされているため、オープンソースにすることなくアプリと一緒に配布できます(fusefs / fuse_nodehash.cはAppleのオープンソースライセンスでリリースされていますが、クローズドソースアプリへのリンクも可能です)。

kvmとsysctlを使用したサンプルコードがあるため、「Cを使用してOSXで他のプロセスのargvを取得する」という質問が役立つはずです。TN2050「ポーリングなしでプロセスの寿命を観察する」も役立つ場合があります。

于 2010-05-14T23:51:01.230 に答える
4

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.

于 2010-05-14T23:42:45.840 に答える