Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Detecting process (non-child) termination
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Detecting process (non-child) termination



On Tuesday, January 21, 2003, at 11:21 AM, Carl Lindberg wrote:
On Friday, January 17, 2003, at 02:58 PM, Ken Witzke wrote:

I perform cleanup at periodic intervals with code similar to that given below.
I need to keep track of the client PIDs that I need to clean up for.
It would be great if I could simply register with something for
notification of a PID going away.

In one tool, I used task_for_pid(mach_task_self(), pid, &pidMachPort) to get the mach port for the specified pid, then waited on an invalidation notification for the mach port. I used Cocoa to do it -- created an NSMachPort object with it, then listened for NSPortDidBecomeInvalidNotification -- although there are obviously lower-level interfaces too (mach_port_request_notification(), by the looks of it).

This worked pretty well, although task_for_pid() fails if you are a non-privileged process trying to get the port for a privileged process. If your listener daemon is running as root though this shouldn't be a problem. I'm not sure how efficient this is, but it worked fine for the simple needs I had at the time.

And the _only_ safe way to use task_for_pid() is from a privileged process. Even if the client is running as the same effective/real user-id as the caller, there may be situations where we cannot give the task port for security reasons. Don't depend upon it working forever from non-privileged processes.

--Jim
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Detecting process (non-child) termination (From: Carl Lindberg <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.