Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / unix / sysv / linux / gethostid.c
blob9441bb2f9b313c6d25941928cff95b0d5788fb1c
1 /* Copyright (C) 1995,1996,1998-2001,2003,2004,2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <alloca.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <netdb.h>
24 #include <not-cancel.h>
26 #define HOSTIDFILE "/etc/hostid"
28 #ifdef SET_PROCEDURE
29 int
30 sethostid (id)
31 long int id;
33 int fd;
34 ssize_t written;
35 int32_t id32 = id;
37 /* Test for appropriate rights to set host ID. */
38 if (__libc_enable_secure)
40 __set_errno (EPERM);
41 return -1;
44 /* Make sure the ID is not too large. Needed for bi-arch support. */
45 if (id32 != id)
47 __set_errno (EOVERFLOW);
48 return -1;
51 /* Open file for writing. Everybody is allowed to read this file. */
52 fd = open_not_cancel (HOSTIDFILE, O_CREAT|O_WRONLY|O_TRUNC, 0644);
53 if (fd < 0)
54 return -1;
56 written = write_not_cancel (fd, &id32, sizeof (id32));
58 close_not_cancel_no_status (fd);
60 return written != sizeof (id32) ? -1 : 0;
63 #else
64 # include <string.h>
65 # include <sys/param.h>
66 # include <resolv/netdb.h>
67 # include <netinet/in.h>
69 long int
70 gethostid ()
72 char hostname[MAXHOSTNAMELEN + 1];
73 size_t buflen;
74 char *buffer;
75 struct hostent hostbuf, *hp;
76 int32_t id;
77 struct in_addr in;
78 int herr;
79 int fd;
81 /* First try to get the ID from a former invocation of sethostid. */
82 fd = open_not_cancel (HOSTIDFILE, O_RDONLY|O_LARGEFILE, 0);
83 if (fd >= 0)
85 ssize_t n = read_not_cancel (fd, &id, sizeof (id));
87 close_not_cancel_no_status (fd);
89 if (n == sizeof (id))
90 return id;
93 /* Getting from the file was not successful. An intelligent guess for
94 a unique number of a host is its IP address. Return this. */
95 if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0')
96 /* This also fails. Return and arbitrary value. */
97 return 0;
99 buflen = 1024;
100 buffer = __alloca (buflen);
102 /* To get the IP address we need to know the host name. */
103 while (__gethostbyname_r (hostname, &hostbuf, buffer, buflen, &hp, &herr)
104 != 0
105 || hp == NULL)
106 if (herr != NETDB_INTERNAL || errno != ERANGE)
107 return 0;
108 else
109 /* Enlarge buffer. */
110 buffer = extend_alloca (buffer, buflen, 2 * buflen);
112 in.s_addr = 0;
113 memcpy (&in, hp->h_addr,
114 (int) sizeof (in) < hp->h_length ? (int) sizeof (in) : hp->h_length);
116 /* For the return value to be not exactly the IP address we do some
117 bit fiddling. */
118 return (int32_t) (in.s_addr << 16 | in.s_addr >> 16);
120 #endif