hostid.c 2.7 KB

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