hostid.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #define __FORCE_GLIBC
  7. #include <features.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <sys/param.h>
  12. #include <netinet/in.h>
  13. #include <netdb.h>
  14. #include <fcntl.h>
  15. #include <unistd.h>
  16. #define HOSTID "/etc/hostid"
  17. #ifdef __USE_BSD
  18. int sethostid(long int new_id)
  19. {
  20. int fd;
  21. int ret;
  22. if (geteuid() || getuid())
  23. return __set_errno(EPERM);
  24. fd = open(HOSTID, O_CREAT|O_WRONLY, 0644);
  25. if (fd < 0)
  26. return fd;
  27. ret = write(fd, &new_id, sizeof(new_id)) == sizeof(new_id) ? 0 : -1;
  28. close(fd);
  29. return ret;
  30. }
  31. #endif
  32. long int gethostid(void)
  33. {
  34. char host[MAXHOSTNAMELEN + 1];
  35. int fd, id;
  36. /* If hostid was already set then we can return that value.
  37. * It is not an error if we cannot read this file. It is not even an
  38. * error if we cannot read all the bytes, we just carry on trying...
  39. */
  40. fd = open(HOSTID, O_RDONLY);
  41. if (fd >= 0 && read(fd, &id, sizeof(id)))
  42. {
  43. close (fd);
  44. return id;
  45. }
  46. if (fd >= 0) close (fd);
  47. /* Try some methods of returning a unique 32 bit id. Clearly IP
  48. * numbers, if on the internet, will have a unique address. If they
  49. * are not on the internet then we can return 0 which means they should
  50. * really set this number via a sethostid() call. If their hostname
  51. * returns the loopback number (i.e. if they have put their hostname
  52. * in the /etc/hosts file with 127.0.0.1) then all such hosts will
  53. * have a non-unique hostid, but it doesn't matter anyway and
  54. * gethostid() will return a non zero number without the need for
  55. * setting one anyway.
  56. * Mitch
  57. */
  58. if (gethostname(host, MAXHOSTNAMELEN) >= 0 && *host) {
  59. struct hostent *hp;
  60. struct in_addr in;
  61. struct hostent ghbn_h;
  62. char ghbn_buf[sizeof(struct in_addr) +
  63. sizeof(struct in_addr *)*2 +
  64. sizeof(char *)*((2 + 5/*MAX_ALIASES*/ +
  65. 1)/*ALIAS_DIM*/) +
  66. 256/*namebuffer*/ + 32/* margin */];
  67. int ghbn_errno;
  68. /* replace gethostbyname() with gethostbyname_r() - ron@zing.net */
  69. /*if ((hp = gethostbyname(host)) == (struct hostent *)NULL)*/
  70. gethostbyname_r(host, &ghbn_h, ghbn_buf, sizeof(ghbn_buf), &hp, &ghbn_errno);
  71. if (hp == NULL) {
  72. /* This is not a error if we get here, as all it means is that
  73. * this host is not on a network and/or they have not
  74. * configured their network properly. So we return the unset
  75. * hostid which should be 0, meaning that they should set it !!
  76. */
  77. return 0;
  78. }
  79. memcpy(&in, hp->h_addr, hp->h_length);
  80. /* Just so it doesn't look exactly like the IP addr */
  81. return (in.s_addr<<16 | in.s_addr>>16);
  82. }
  83. return 0;
  84. }