Back to index

glibc  2.9
gethostid.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995,1996,1998-2001,2003,2004,2006
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <alloca.h>
00021 #include <errno.h>
00022 #include <fcntl.h>
00023 #include <unistd.h>
00024 #include <netdb.h>
00025 #include <not-cancel.h>
00026 
00027 #define HOSTIDFILE "/etc/hostid"
00028 
00029 #ifdef SET_PROCEDURE
00030 int
00031 sethostid (id)
00032      long int id;
00033 {
00034   int fd;
00035   ssize_t written;
00036   int32_t id32 = id;
00037 
00038   /* Test for appropriate rights to set host ID.  */
00039   if (__libc_enable_secure)
00040     {
00041       __set_errno (EPERM);
00042       return -1;
00043     }
00044 
00045   /* Make sure the ID is not too large.  Needed for bi-arch support.   */
00046   if (id32 != id)
00047     {
00048       __set_errno (EOVERFLOW);
00049       return -1;
00050     }
00051 
00052   /* Open file for writing.  Everybody is allowed to read this file.  */
00053   fd = open_not_cancel (HOSTIDFILE, O_CREAT|O_WRONLY|O_TRUNC, 0644);
00054   if (fd < 0)
00055     return -1;
00056 
00057   written = write_not_cancel (fd, &id32, sizeof (id32));
00058 
00059   close_not_cancel_no_status (fd);
00060 
00061   return written != sizeof (id32) ? -1 : 0;
00062 }
00063 
00064 #else
00065 # include <string.h>
00066 # include <sys/param.h>
00067 # include <resolv/netdb.h>
00068 # include <netinet/in.h>
00069 
00070 long int
00071 gethostid ()
00072 {
00073   char hostname[MAXHOSTNAMELEN + 1];
00074   size_t buflen;
00075   char *buffer;
00076   struct hostent hostbuf, *hp;
00077   int32_t id;
00078   struct in_addr in;
00079   int herr;
00080   int fd;
00081 
00082   /* First try to get the ID from a former invocation of sethostid.  */
00083   fd = open_not_cancel (HOSTIDFILE, O_RDONLY|O_LARGEFILE, 0);
00084   if (fd >= 0)
00085     {
00086       ssize_t n = read_not_cancel (fd, &id, sizeof (id));
00087 
00088       close_not_cancel_no_status (fd);
00089 
00090       if (n == sizeof (id))
00091        return id;
00092     }
00093 
00094   /* Getting from the file was not successful.  An intelligent guess for
00095      a unique number of a host is its IP address.  Return this.  */
00096   if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0')
00097     /* This also fails.  Return and arbitrary value.  */
00098     return 0;
00099 
00100   buflen = 1024;
00101   buffer = __alloca (buflen);
00102 
00103   /* To get the IP address we need to know the host name.  */
00104   while (__gethostbyname_r (hostname, &hostbuf, buffer, buflen, &hp, &herr)
00105         != 0
00106         || hp == NULL)
00107     if (herr != NETDB_INTERNAL || errno != ERANGE)
00108       return 0;
00109     else
00110       /* Enlarge buffer.  */
00111       buffer = extend_alloca (buffer, buflen, 2 * buflen);
00112 
00113   in.s_addr = 0;
00114   memcpy (&in, hp->h_addr,
00115          (int) sizeof (in) < hp->h_length ? (int) sizeof (in) : hp->h_length);
00116 
00117   /* For the return value to be not exactly the IP address we do some
00118      bit fiddling.  */
00119   return (int32_t) (in.s_addr << 16 | in.s_addr >> 16);
00120 }
00121 #endif