gethostid.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright (C) 1995-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <alloca.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include <netdb.h>
  19. #include <not-cancel.h>
  20. #include <stdbool.h>
  21. #define HOSTIDFILE "/etc/hostid"
  22. #ifdef SET_PROCEDURE
  23. int
  24. sethostid (long int id)
  25. {
  26. int fd;
  27. ssize_t written;
  28. int32_t id32 = id;
  29. /* Test for appropriate rights to set host ID. */
  30. if (__libc_enable_secure)
  31. {
  32. __set_errno (EPERM);
  33. return -1;
  34. }
  35. /* Make sure the ID is not too large. Needed for bi-arch support. */
  36. if (id32 != id)
  37. {
  38. __set_errno (EOVERFLOW);
  39. return -1;
  40. }
  41. /* Open file for writing. Everybody is allowed to read this file. */
  42. fd = __open_nocancel (HOSTIDFILE, O_CREAT|O_WRONLY|O_TRUNC, 0644);
  43. if (fd < 0)
  44. return -1;
  45. written = __write_nocancel (fd, &id32, sizeof (id32));
  46. __close_nocancel_nostatus (fd);
  47. return written != sizeof (id32) ? -1 : 0;
  48. }
  49. #else
  50. # include <string.h>
  51. # include <sys/param.h>
  52. # include <resolv/netdb.h>
  53. # include <netinet/in.h>
  54. # include <scratch_buffer.h>
  55. long int
  56. gethostid (void)
  57. {
  58. char hostname[MAXHOSTNAMELEN + 1];
  59. struct hostent hostbuf, *hp;
  60. int32_t id;
  61. struct in_addr in;
  62. int herr;
  63. int fd;
  64. /* First try to get the ID from a former invocation of sethostid. */
  65. fd = __open_nocancel (HOSTIDFILE, O_RDONLY|O_LARGEFILE, 0);
  66. if (fd >= 0)
  67. {
  68. ssize_t n = __read_nocancel (fd, &id, sizeof (id));
  69. __close_nocancel_nostatus (fd);
  70. if (n == sizeof (id))
  71. return id;
  72. }
  73. /* Getting from the file was not successful. An intelligent guess
  74. for a unique number of a host is its IP address. To get the IP
  75. address we need to know the host name. */
  76. if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0')
  77. /* This also fails. Return and arbitrary value. */
  78. return 0;
  79. /* Determine the IP address of the host name. */
  80. struct scratch_buffer tmpbuf;
  81. scratch_buffer_init (&tmpbuf);
  82. while (true)
  83. {
  84. int ret = __gethostbyname_r (hostname, &hostbuf,
  85. tmpbuf.data, tmpbuf.length, &hp, &herr);
  86. if (ret == 0 && hp != NULL)
  87. break;
  88. else
  89. {
  90. /* Enlarge the buffer on ERANGE. */
  91. if (ret != 0 && herr == NETDB_INTERNAL && errno == ERANGE)
  92. {
  93. if (!scratch_buffer_grow (&tmpbuf))
  94. return 0;
  95. }
  96. /* Other errors are a failure. Return an arbitrary value. */
  97. else
  98. {
  99. scratch_buffer_free (&tmpbuf);
  100. return 0;
  101. }
  102. }
  103. }
  104. in.s_addr = 0;
  105. memcpy (&in, hp->h_addr,
  106. (int) sizeof (in) < hp->h_length ? (int) sizeof (in) : hp->h_length);
  107. scratch_buffer_free (&tmpbuf);
  108. /* For the return value to be not exactly the IP address we do some
  109. bit fiddling. */
  110. return (int32_t) (in.s_addr << 16 | in.s_addr >> 16);
  111. }
  112. #endif