开发者

How to find all the file handles by a process programmatically?

I have a process "x" which uses "system" C function to start ntpd daemon. I observed that ntpd are passed the open file descriptors of "x". ntpd holds on to the file descriptors even after original file is deleted. for ex: Some log files used by "x" are rotated out after sometime, but "ntpd" has file handle opened for these deleted files. Will it cause any problem?

Alternatively I thought of setting "FD_CLOEXEC" flag for all the file descriptors before calling "system" function. But as we are running as an extension library to third process "x"( "x" loads our library based on some condition), there is no easy way to know about all the file descriptors process has ope开发者_如何学Pythonned. One way is to read /proc//fd and set "FD_CLOEXEC" for each file handle and reset it back after "system" function returns. I'm using Linux 2.6.16. Is there any other easy way to find all the file handlers?

Thanks,


Yes, it will cause a problem. The disk space used by the deleted files will not be released for reuse until the last open file descriptor is in fact closed.

Ideally, you would ensure that FD_CLOEXEC is set on all file descriptors; with POSIX 2008, you can do that when the file is opened with the O_CLOEXEC flag, even. But if you are part of another process and not in charge of its code, then it is not at all easy. Your choices are brute force and ignorance. You can cycle through all the descriptors you think might be open and close them - ignoring errors from descriptors that are already closed. That's brute force. Or you can ignore the files that are open, and hope that the system doesn't jam too badly. Maybe you can limit your search by checking how many files you can have open at once.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜