fd.c 669 B

12345678910111213141516171819202122232425262728293031
  1. // SPDX-License-Identifier: GPL-2.0 or GPL-3.0
  2. // Copyright © 2018-2019 Ariadne Devos
  3. // sHT -- generic file descriptor operations (Unix)
  4. #include "fd.h"
  5. #include <sHT/bugs.h>
  6. #include <sHT/compiler.h>
  7. #include <sHT/test.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10. static const struct sHT_bug_option sHT_close_bug =
  11. { .code = 0, .msg = "not a fd" };
  12. void
  13. sHT_close(int fd)
  14. {
  15. /* close must not be retried on interrupt on:
  16. Linux, FreeBSD, OpenBSD (man pages),
  17. don't know about NetBSD,
  18. I presume the Hurd. */
  19. int ret = close(fd);
  20. if (!sHT_nonzero_p(ret))
  21. return;
  22. int err = errno;
  23. if (sHT_eq(err, EBADFD))
  24. sHT_bug(&sHT_close_bug, err);
  25. return;
  26. }