fcntl-internal.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Helper functions for translating between O_* and SOCK_* flags.
  2. Copyright (C) 2008-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <fcntl.h>
  16. #include <sys/socket.h>
  17. /* Do some compile-time checks for the SOCK_* constants, which we rely on. */
  18. _Static_assert (SOCK_CLOEXEC == O_CLOEXEC,
  19. "SOCK_CLOEXEC is assumed to be the same as O_CLOEXEC");
  20. _Static_assert (((SOCK_MAX - 1) | SOCK_TYPE_MASK) == SOCK_TYPE_MASK,
  21. "SOCK_TYPE_MASK must contain SOCK_MAX - 1");
  22. _Static_assert ((SOCK_CLOEXEC & SOCK_TYPE_MASK) == 0,
  23. "SOCK_TYPE_MASK must not contain SOCK_CLOEXEC");
  24. _Static_assert ((SOCK_NONBLOCK & SOCK_TYPE_MASK) == 0,
  25. "SOCK_TYPE_MASK must not contain SOCK_NONBLOCK");
  26. /* Convert from SOCK_* flags to O_* flags. */
  27. __extern_always_inline
  28. int
  29. sock_to_o_flags (int in)
  30. {
  31. int out = 0;
  32. if (in & SOCK_NONBLOCK)
  33. out |= O_NONBLOCK;
  34. /* Others are passed through unfiltered. */
  35. out |= in & ~(SOCK_NONBLOCK);
  36. return out;
  37. }
  38. /* Convert from O_* flags to SOCK_* flags. */
  39. __extern_always_inline
  40. int
  41. o_to_sock_flags (int in)
  42. {
  43. int out = 0;
  44. if (in & O_NONBLOCK)
  45. out |= SOCK_NONBLOCK;
  46. /* Others are passed through unfiltered. */
  47. out |= in & ~(O_NONBLOCK);
  48. return out;
  49. }