hostid.c 2.1 KB

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