opensock.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* Create socket with an unspecified address family for use with ioctl.
  2. Copyright (C) 1999-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 <errno.h>
  16. #include <sys/socket.h>
  17. /* Return a socket of any type. The socket can be used in subsequent
  18. ioctl calls to talk to the kernel. */
  19. int
  20. __opensock (void)
  21. {
  22. /* SOCK_DGRAM is supported by all address families. */
  23. int type = SOCK_DGRAM | SOCK_CLOEXEC;
  24. int fd;
  25. fd = __socket (AF_UNIX, type, 0);
  26. if (fd >= 0)
  27. return fd;
  28. fd = __socket (AF_INET, type, 0);
  29. if (fd >= 0)
  30. return fd;
  31. fd = __socket (AF_INET6, type, 0);
  32. if (fd >= 0)
  33. return fd;
  34. __set_errno (ENOENT);
  35. return fd;
  36. }