Post B4sECkR029QYzIMLtA by libreleah@mas.to
(DIR) More posts by libreleah@mas.to
(DIR) Post #B4rQ6BkD6Q5Lx71XmK by libreleah@mas.to
0 likes, 0 repeats
this is how you close a file, correctly, on linux/bsd (or any unix):https://browse.libreboot.org/lbmk.git/commit/?id=63ec707698d956b50e4d16d5364dd8bc7ad83ddbhttps://browse.libreboot.org/lbmk.git/tree/util/libreboot-utils/lib/file.c?id=63ec707698d956b50e4d16d5364dd8bc7ad83ddb#n709taming your libc, one function at a time.edit: and the typo in the comment shall remain. good day.
(DIR) Post #B4rV07cI2IWbBSFIdU by pauldoo@mastodon.scot
0 likes, 0 repeats
@libreleah to be clear then, there’s no reliable and spec compliant way to close file descriptors? If the operation is interrupted you cannot safely retry (you may race and close a different file that reused the same fd number), so the only option is to hope the close was successful, and if it wasn’t you just leak it.And this isn’t some weird corner of the API. This is just “close”…
(DIR) Post #B4sECkR029QYzIMLtA by libreleah@mas.to
0 likes, 0 repeats
@pauldoo my comment there is overly conservative, and technically correct, but in practise: if close is interrupted, chances are the file was in fact closed, on linux and bsd at least. but linux and bsd do not exist, in the code i'm writing, which aims to be unix-agnostic.close() rarely fails. you will likely never see EINTR, ever - but it is specified in POSIX, and linux/bsd manuals. it *can* fail, but probably won't.you could probably just run close() without guards. but i follow the spec.
(DIR) Post #B4sEaDsGk8mAxDJAtk by libreleah@mas.to
0 likes, 0 repeats
@pauldoo btw, look at my code there, for reset_caller_errno() (and with_fallback_errno())i try to guarantee deterministic errno. in POSIX, a syscall *can also set errno on success*. my reset logic detects this and preserves it, otherwise it preserves state at the caller site upon return.so in this case, close() is regarded as successful, but errno would still be set. you could debug this by putting some warn() calls in your code, if you wanted (bsd warn function).posix is a fickle beast.