2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / gethostid.c
blobde98fb3d7bd988ebd9826c8717e31c66fd2652a1
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <alloca.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <netdb.h>
25 #include <not-cancel.h>
27 #define HOSTIDFILE "/etc/hostid"
29 #ifdef SET_PROCEDURE
30 int
31 sethostid (id)
32 long int id;
34 int fd;
35 ssize_t written;
36 int32_t id32 = id;
38 /* Test for appropriate rights to set host ID. */
39 if (__libc_enable_secure)
41 __set_errno (EPERM);
42 return -1;
45 /* Make sure the ID is not too large. Needed for bi-arch support. */
46 if (id32 != id)
48 __set_errno (EOVERFLOW);
49 return -1;
52 /* Open file for writing. Everybody is allowed to read this file. */
53 fd = open_not_cancel (HOSTIDFILE, O_CREAT|O_WRONLY|O_TRUNC, 0644);
54 if (fd < 0)
55 return -1;
57 written = write_not_cancel (fd, &id32, sizeof (id32));
59 close_not_cancel_no_status (fd);
61 return written != sizeof (id32) ? -1 : 0;
64 #else
65 # include <string.h>
66 # include <sys/param.h>
67 # include <resolv/netdb.h>
68 # include <netinet/in.h>
70 long int
71 gethostid ()
73 char hostname[MAXHOSTNAMELEN + 1];
74 size_t buflen;
75 char *buffer;
76 struct hostent hostbuf, *hp;
77 int32_t id;
78 struct in_addr in;
79 int herr;
80 int fd;
82 /* First try to get the ID from a former invocation of sethostid. */
83 fd = open_not_cancel (HOSTIDFILE, O_RDONLY|O_LARGEFILE, 0);
84 if (fd >= 0)
86 ssize_t n = read_not_cancel (fd, &id, sizeof (id));
88 close_not_cancel_no_status (fd);
90 if (n == sizeof (id))
91 return id;
94 /* Getting from the file was not successful. An intelligent guess for
95 a unique number of a host is its IP address. Return this. */
96 if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0')
97 /* This also fails. Return and arbitrary value. */
98 return 0;
100 buflen = 1024;
101 buffer = __alloca (buflen);
103 /* To get the IP address we need to know the host name. */
104 while (__gethostbyname_r (hostname, &hostbuf, buffer, buflen, &hp, &herr)
105 != 0
106 || hp == NULL)
107 if (herr != NETDB_INTERNAL || errno != ERANGE)
108 return 0;
109 else
110 /* Enlarge buffer. */
111 buffer = extend_alloca (buffer, buflen, 2 * buflen);
113 in.s_addr = 0;
114 memcpy (&in, hp->h_addr,
115 (int) sizeof (in) < hp->h_length ? (int) sizeof (in) : hp->h_length);
117 /* For the return value to be not exactly the IP address we do some
118 bit fiddling. */
119 return (int32_t) (in.s_addr << 16 | in.s_addr >> 16);
121 #endif