To find if a binary is being used/running

2013-10-04 Thread Nick Rogers
Hi,

I need to know if a binary is executing or not. I just have the path to the 
binary e.g. /Applications/MyApp.app/Contents/MacOS/MyApp

There's NSRunningApplication method runningApplicationsWithBundleIdentifier: , 
but I have other binaries which are embedded in this app which don't have a 
bundle id. And I want to know if they are running or not.

Cocoa doesn't seem have any such specific API for this purpose. NSFileHandle 
provides a valid handle (for writing) even if the binary is executing.

Is there any method like open() or something which would fail if binary is 
executing. Tried open() with flags O_EXLOCK but no luck yet.

Is this even possible?

Best,
Nick

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: To find if a binary is being used/running

2013-10-04 Thread Kyle Sluder
 On Oct 4, 2013, at 12:08 AM, Nick Rogers roger...@mac.com wrote:
 
 Hi,
 
 I need to know if a binary is executing or not. I just have the path to the 
 binary e.g. /Applications/MyApp.app/Contents/MacOS/MyApp

From the OS’s point of view, your question is ill-formed. The user can create a 
hardlink to the same inode at /tmp/someapp and execute that.

 Is this even possible?

I can think of a couple strategies:

1. Have the target app create a resource that goes away when the app 
terminates. Maybe a pipe? Something that the system will destroy when the 
helper app terminates.

2. Move your helper apps to XPC services. (Almost certainly Apple’s preferred 
approach.)

--Kyle Sluder

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: To find if a binary is being used/running

2013-10-04 Thread Jens Alfke

On Oct 4, 2013, at 12:08 AM, Nick Rogers roger...@mac.com wrote:

 Is this even possible?

Basically no. In Unix there is no connection from an executable file to a 
process running that file (or from _any_ file to a process that has that file 
open, really.) The most exhaustive solution is what the `lsof` and `ps` tools 
do, basically iterating over the kernel’s list of open files and running 
processes and looking for a match, which is expensive.

Unix tools often seem to use a strategy of creating a ‘pid’ file at some known 
location, which contains the process ID of the running instance of the tool. 
The tool has to write the file when it starts up, and be careful to delete it 
whenever it exits (including if it crashes or is killed.)

But I agree with Kyle, you should be using XPC or something similar to manage 
helper tasks.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com