Remove sys/syscall.h, sys/types.h, linux/posix_types.h, sysdep.h and pthread-function...
[glibc.git] / sysdeps / unix / sysv / linux / gethostid.c
blobc7f894033d1e68764bba10e38c1c758d3ff402a0
1 /* Copyright (C) 1995,1996,1998-2001,2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
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"
27 #define OLD_HOSTIDFILE "/etc/hostid"
29 #ifdef SET_PROCEDURE
30 int
31 sethostid (id)
32 long int id;
34 int fd;
35 ssize_t written;
37 /* Test for appropriate rights to set host ID. */
38 if (__libc_enable_secure)
40 __set_errno (EPERM);
41 return -1;
44 /* Open file for writing. Everybody is allowed to read this file. */
45 fd = open_not_cancel (HOSTIDFILE, O_CREAT|O_WRONLY|O_TRUNC, 0644);
46 if (fd < 0)
47 return -1;
49 written = write_not_cancel (fd, &id, sizeof (id));
51 close_not_cancel_no_status (fd);
53 return written != sizeof (id) ? -1 : 0;
56 #else
57 # include <string.h>
58 # include <sys/param.h>
59 # include <resolv/netdb.h>
60 # include <netinet/in.h>
62 long int
63 gethostid ()
65 char hostname[MAXHOSTNAMELEN + 1];
66 size_t buflen;
67 char *buffer;
68 struct hostent hostbuf, *hp;
69 unsigned long int id;
70 struct in_addr in;
71 int herr;
72 int fd;
74 /* First try to get the ID from a former invocation of sethostid. */
75 fd = open_not_cancel (HOSTIDFILE, O_RDONLY|O_LARGEFILE, 0);
76 if (fd < 0)
77 fd = open_not_cancel (OLD_HOSTIDFILE, O_RDONLY|O_LARGEFILE, 0);
78 if (fd >= 0)
80 ssize_t n = read_not_cancel (fd, &id, sizeof (id));
82 close_not_cancel_no_status (fd);
84 if (n == sizeof (id))
85 return id;
88 /* Getting from the file was not successful. An intelligent guess for
89 a unique number of a host is its IP address. Return this. */
90 if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0')
91 /* This also fails. Return and arbitrary value. */
92 return 0;
94 buflen = 1024;
95 buffer = __alloca (buflen);
97 /* To get the IP address we need to know the host name. */
98 while (__gethostbyname_r (hostname, &hostbuf, buffer, buflen, &hp, &herr)
99 != 0
100 || hp == NULL)
101 if (herr != NETDB_INTERNAL || errno != ERANGE)
102 return 0;
103 else
104 /* Enlarge buffer. */
105 buffer = extend_alloca (buffer, buflen, 2 * buflen);
107 in.s_addr = 0;
108 memcpy (&in, hp->h_addr,
109 (int) sizeof (in) < hp->h_length ? (int) sizeof (in) : hp->h_length);
111 /* For the return value to be not exactly the IP address we do some
112 bit fiddling. */
113 return in.s_addr << 16 | in.s_addr >> 16;
115 #endif