Re: Identify all open files of current OS X process
Re: Identify all open files of current OS X process
- Subject: Re: Identify all open files of current OS X process
- From: Roland King <email@hidden>
- Date: Sun, 08 Feb 2015 10:59:01 +0800
> On 8 Feb 2015, at 07:26, Jerry Krinock <email@hidden> wrote:
>
> I need a method which will give me the paths of all files which the current OS X process (*) has open. Yes, this is for a benevolent hack and no, I am not sandboxed.
>
> I’m considering using getpid() and passing the result to /usr/sbin/lsof with a -p option. This works very fast in Terminal.app, but I’m hoping for something less fragile (lsof has lots of weird options including a timeout) and more lightweight.
>
> Thanks,
>
> Jerry Krinock
>
> (*) Actually, this code is going in to a binary plug-in in another app, so my plug-in is going to get the open files of its host app. The purpose is to work around a deficiency in the host app’s plug-in API.
> __________
Well if you can figure out which file descriptors are open, or even how many file descriptors you could possibly have and iterate each possible then you can use
char filePath[ PATH_MAX ];
fcntl( fd, F_GETPATH, filePath )
If that returns other than -1 you have some kind of path. Don’t know what it returns for other file descriptors than files, eg sockets, perhaps nothing, perhaps an address, not sure, not documented.
finding out the max number of file handles you could do with
struct rlimit file_limit;
getrlimit( RLIMIT_NOFILE, &file_limit );
don’t know if there’s a better way.
Also, per the discussion we had the other day about guarded filehandles, I don’t know if you could potentially run into trouble inspecting random handles like that, even just to get their file paths (if they are file handles at all and have paths at all), at the least you probably want to be catching exceptions.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden