hostid.c 2.9 KB

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