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: Jerry Krinock <email@hidden>
- Date: Sun, 08 Feb 2015 21:14:57 -0800
Thank you, Roland. I am using your idea, iterating over fd with
> fcntl( fd, F_GETPATH, filePath )
I think that it’s less fragile than what I was thinking of doing, less code, and it works :)
On 2015 Feb 07, at 18:59, Roland King <email@hidden> wrote:
> Well if you can figure out which file descriptors are open, or even how many file descriptors
Since I know the name of the file I’m looking for (but not its full path), and since the host app always has this file open, or my plugin is not going to work anyhow, the first line of defense is to break out of the loop when that known filename is found. Then, for extra robustness, I used your suggestion for an upper limit,
> struct rlimit file_limit;
> getrlimit( RLIMIT_NOFILE, &file_limit );
which gives me an answer of rlim_cur = 16384. Quite a bit but, at least for now, it never happens, and even when it does, it takes only milliseconds, and I only do this once.
> If that returns other than -1 you have some kind of path.
Yes that is correct.
> Don’t know what it returns for other file descriptors than files, eg sockets, perhaps nothing, perhaps an address, not sure, not documented.
It seems to return -1 and set errno to EBADF=9 Bad file descriptor. These are interspersed among the desired path results which return 0.
The behavior is somewhat documented:
"The fcntl() system call will fail if:
…
[EBADF] Fildes is not a valid open file descriptor.”
I’m interpreting that to mean it is OK to give bad file descriptors. Otherwise, it should say “Do not do this” or “behavior is undefined”.
> 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.
OK, I plowed through that whole thread. I don’t think I am likely to be affected by that bug in the system, or whatever, but just in case it might help I followed your advice and enclosed my fcntl() inside a @try.
_______________________________________________
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