Blame sysdeps/unix/sysv/linux/gethostid.c

Packit 6c4009
/* Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <alloca.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <netdb.h>
Packit 6c4009
#include <not-cancel.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
Packit 6c4009
#define HOSTIDFILE "/etc/hostid"
Packit 6c4009
Packit 6c4009
#ifdef SET_PROCEDURE
Packit 6c4009
int
Packit 6c4009
sethostid (long int id)
Packit 6c4009
{
Packit 6c4009
  int fd;
Packit 6c4009
  ssize_t written;
Packit 6c4009
  int32_t id32 = id;
Packit 6c4009
Packit 6c4009
  /* Test for appropriate rights to set host ID.  */
Packit 6c4009
  if (__libc_enable_secure)
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EPERM);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Make sure the ID is not too large.  Needed for bi-arch support.   */
Packit 6c4009
  if (id32 != id)
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EOVERFLOW);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Open file for writing.  Everybody is allowed to read this file.  */
Packit 6c4009
  fd = __open_nocancel (HOSTIDFILE, O_CREAT|O_WRONLY|O_TRUNC, 0644);
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  written = __write_nocancel (fd, &id32, sizeof (id32));
Packit 6c4009
Packit 6c4009
  __close_nocancel_nostatus (fd);
Packit 6c4009
Packit 6c4009
  return written != sizeof (id32) ? -1 : 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#else
Packit 6c4009
# include <string.h>
Packit 6c4009
# include <sys/param.h>
Packit 6c4009
# include <resolv/netdb.h>
Packit 6c4009
# include <netinet/in.h>
Packit 6c4009
# include <scratch_buffer.h>
Packit 6c4009
Packit 6c4009
long int
Packit 6c4009
gethostid (void)
Packit 6c4009
{
Packit 6c4009
  char hostname[MAXHOSTNAMELEN + 1];
Packit 6c4009
  struct hostent hostbuf, *hp;
Packit 6c4009
  int32_t id;
Packit 6c4009
  struct in_addr in;
Packit 6c4009
  int herr;
Packit 6c4009
  int fd;
Packit 6c4009
Packit 6c4009
  /* First try to get the ID from a former invocation of sethostid.  */
Packit 6c4009
  fd = __open_nocancel (HOSTIDFILE, O_RDONLY|O_LARGEFILE, 0);
Packit 6c4009
  if (fd >= 0)
Packit 6c4009
    {
Packit 6c4009
      ssize_t n = __read_nocancel (fd, &id, sizeof (id));
Packit 6c4009
Packit 6c4009
      __close_nocancel_nostatus (fd);
Packit 6c4009
Packit 6c4009
      if (n == sizeof (id))
Packit 6c4009
	return id;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Getting from the file was not successful.  An intelligent guess
Packit 6c4009
     for a unique number of a host is its IP address.  To get the IP
Packit 6c4009
     address we need to know the host name.  */
Packit 6c4009
  if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0')
Packit 6c4009
    /* This also fails.  Return and arbitrary value.  */
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  /* Determine the IP address of the host name.  */
Packit 6c4009
  struct scratch_buffer tmpbuf;
Packit 6c4009
  scratch_buffer_init (&tmpbuf);
Packit 6c4009
  while (true)
Packit 6c4009
    {
Packit 6c4009
      int ret = __gethostbyname_r (hostname, &hostbuf,
Packit 6c4009
				   tmpbuf.data, tmpbuf.length, &hp, &herr;;
Packit Service fd8d1c
      if (ret == 0 && hp != NULL)
Packit 6c4009
	break;
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Enlarge the buffer on ERANGE.  */
Packit Service fd8d1c
	  if (ret != 0 && herr == NETDB_INTERNAL && errno == ERANGE)
Packit 6c4009
	    {
Packit 6c4009
	      if (!scratch_buffer_grow (&tmpbuf))
Packit 6c4009
		return 0;
Packit 6c4009
	    }
Packit 6c4009
	  /* Other errors are a failure.  Return an arbitrary value.  */
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      scratch_buffer_free (&tmpbuf);
Packit 6c4009
	      return 0;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  in.s_addr = 0;
Packit 6c4009
  memcpy (&in, hp->h_addr,
Packit 6c4009
	  (int) sizeof (in) < hp->h_length ? (int) sizeof (in) : hp->h_length);
Packit 6c4009
  scratch_buffer_free (&tmpbuf);
Packit 6c4009
  /* For the return value to be not exactly the IP address we do some
Packit 6c4009
     bit fiddling.  */
Packit 6c4009
  return (int32_t) (in.s_addr << 16 | in.s_addr >> 16);
Packit 6c4009
}
Packit 6c4009
#endif