Update.
[glibc.git] / sysdeps / unix / sysv / linux / gethostid.c
blob6f501e391dd5d1b8c21299df62d910bc5042dac0
1 /* Copyright (C) 1995, 1996, 1998, 1999 Free Software Foundation, Inc.
3 The GNU C Library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
8 The GNU C Library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public
14 License along with the GNU C Library; see the file COPYING.LIB. If not,
15 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 Boston, MA 02111-1307, USA. */
18 #include <alloca.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <netdb.h>
24 #define HOSTIDFILE "/var/adm/hostid"
26 #ifdef SET_PROCEDURE
27 int
28 sethostid (id)
29 long int id;
31 int fd;
32 ssize_t written;
34 /* Test for appropriate rights to set host ID. */
35 if (__libc_enable_secure)
37 __set_errno (EPERM);
38 return -1;
41 /* Open file for writing. Everybody is allowed to read this file. */
42 fd = __open (HOSTIDFILE, O_CREAT|O_WRONLY, 0644);
43 if (fd < 0)
44 return -1;
46 written = __write (fd, &id, sizeof (id));
48 __close (fd);
50 return written != sizeof (id) ? -1 : 0;
53 #else
54 # include <string.h>
55 # include <sys/param.h>
56 # include <resolv/netdb.h>
57 # include <netinet/in.h>
59 long int
60 gethostid ()
62 char hostname[MAXHOSTNAMELEN + 1];
63 size_t buflen;
64 char *buffer;
65 struct hostent hostbuf, *hp;
66 unsigned long int id;
67 struct in_addr in;
68 int herr;
69 int fd;
71 /* First try to get the ID from a former invocation of sethostid. */
72 fd = __open (HOSTIDFILE, O_RDONLY);
73 if (fd >= 0)
75 ssize_t n = __read (fd, &id, sizeof (id));
77 __close (fd);
79 if (n == sizeof (id))
80 return id;
83 /* Getting from the file was not successful. An intelligent guess for
84 a unique number of a host is its IP address. Return this. */
85 if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0')
86 /* This also fails. Return and arbitrary value. */
87 return 0;
89 buflen = 1024;
90 buffer = __alloca (buflen);
92 /* To get the IP address we need to know the host name. */
93 while (__gethostbyname_r (hostname, &hostbuf, buffer, buflen, &hp, &herr)
94 != 0
95 || hp == NULL)
96 if (herr != NETDB_INTERNAL || errno != ERANGE)
97 return 0;
98 else
100 /* Enlarge buffer. */
101 buflen *= 2;
102 buffer = __alloca (buflen);
105 in.s_addr = 0;
106 memcpy (&in, hp->h_addr,
107 (int) sizeof (in) < hp->h_length ? sizeof (in) : hp->h_length);
109 /* For the return value to be not exactly the IP address we do some
110 bit fiddling. */
111 return in.s_addr << 16 | in.s_addr >> 16;
113 #endif