简体   繁体   English

有没有办法告诉文件描述符值已被重用?

[英]Is there any way to tell that a file descriptor value has been reused?

I've got an API that is called with a file descriptor as an argument, and it internally stores some state associated with the file descriptor. 我有一个以文件描述符作为参数调用的API,它在内部存储与文件描述符关联的一些状态。 Then on subsequent calls with the same file descriptor value, the previously generated state can be consulted. 然后在具有相同文件描述符值的后续调用中,可以查阅先前生成的状态。

This mostly works, except for the case where the calling code calls my API with a file descriptor, then closes the file descriptor, then allocates a new file descriptor (via socket() or accept() or etc) that ends up having the same integer value as the now-closed one, and then passes that new file descriptor to my API. 这主要是有效的,除了调用代码用文件描述符调用我的API,然后关闭文件描述符,然后分配一个新的文件描述符(通过socket()或accept()等),最终具有相同的整数值作为现在关闭的值,然后将该新文件描述符传递给我的API。 At that point, my API does the wrong thing, because it erroneously associates the old socket's state with the new file descriptor. 那时,我的API做错了,因为它错误地将旧套接字的状态与新文件描述符相关联。

One solution to this issue would be to force the calling code to notify my API whenever it closes a socket, so that my API would know to delete the associated state.... but I'd prefer not to force the user to do that, since it would be inconvenient for them and they'd be prone to forget to do it anyway. 这个问题的一个解决方案是强制调用代码在关闭套接字时通知我的API,以便我的API知道删除关联状态....但我不想强迫用户这样做因为它对他们来说不方便,反正他们也很容易忘记这样做。

Therefore, I'm wondering if there is any clever way to tell whether a file descriptor at time T is still associated with the same underlying structures that it was associated with at time (Tx). 因此,我想知道是否有任何聪明的方法来判断时间T的文件描述符是否仍然与它在时间(Tx)关联的相同底层结构相关联。 If I could do that, my API would be smart enough to tell when a file descriptor integer-value had been reused and do the right thing. 如果我能做到这一点,我的API将足够聪明,可以告诉我何时重用文件描述符整数值并做正确的事情。

FWIW this code is intended to run under MacOS/X and Linux primarily, but the more portable the solution, the better. FWIW此代码主要用于在MacOS / X和Linux下运行,但解决方案越便携越好。

I don't believe so. 我不相信。

You're better off providing wrappers for open/close that return an opaque type back to the callers. 你最好提供打开/关闭的包装器,将一个不透明的类型返回给调用者。 (You can also provide a function to get the underlying file descriptor out of that opaque type if necessary.) (如果需要,您还可以提供一个函数来从该opaque类型中获取基础文件描述符。)

If you don't want to wrap open/close, you can still use an opaque structure for your APIs (just make a pair of functions to create (with a file descriptor parameter) and release that structure), but indeed, your users will have to remember to release or the app will leak. 如果您不想打开/关闭,您仍然可以为API使用不透明结构(只需创建一对函数(使用文件描述符参数)并释放该结构),但实际上,您的用户将必须记住发布或应用程序将泄漏。 (But C developer are supposed to know how to do that - malloc / free have been around for a while.) (但C开发人员应该知道如何做到这一点 - malloc / free已经存在了一段时间。)

Depending on exactly what your library provides, there are probably better alternatives, but this is what I believe is generally done for C APIs. 根据您的库提供的确切内容,可能有更好的替代方案,但我认为这通常是针对C API完成的。

Side note: if you expect both your library and the user code to issue reads and writes on those sockets... be careful, that's real tricky. 旁注:如果您希望您的库和用户代码都在这些套接字上发出读写...请注意,这真的很棘手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM