1 | /* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc. |
---|
2 | This file is part of the GNU C Library. |
---|
3 | |
---|
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. |
---|
8 | |
---|
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. |
---|
13 | |
---|
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. */ |
---|
18 | |
---|
19 | #include <alloca.h> |
---|
20 | #include <errno.h> |
---|
21 | #include <fcntl.h> |
---|
22 | #include <unistd.h> |
---|
23 | #include <netdb.h> |
---|
24 | |
---|
25 | #define HOSTIDFILE "/etc/hostid" |
---|
26 | #define OLD_HOSTIDFILE "/etc/hostid" |
---|
27 | |
---|
28 | #if !defined(_ELIX_LEVEL) || _ELIX_LEVEL >= 2 |
---|
29 | # define OPEN __open64 |
---|
30 | #else |
---|
31 | # define OPEN __open |
---|
32 | #endif |
---|
33 | |
---|
34 | #ifdef SET_PROCEDURE |
---|
35 | int |
---|
36 | sethostid (id) |
---|
37 | long int id; |
---|
38 | { |
---|
39 | int fd; |
---|
40 | ssize_t written; |
---|
41 | |
---|
42 | /* Open file for writing. Everybody is allowed to read this file. */ |
---|
43 | fd = OPEN (HOSTIDFILE, O_CREAT|O_WRONLY|O_TRUNC, 0644); |
---|
44 | if (fd < 0) |
---|
45 | return -1; |
---|
46 | |
---|
47 | written = __write (fd, &id, sizeof (id)); |
---|
48 | |
---|
49 | __close (fd); |
---|
50 | |
---|
51 | return written != sizeof (id) ? -1 : 0; |
---|
52 | } |
---|
53 | |
---|
54 | #else |
---|
55 | # include <string.h> |
---|
56 | # include <sys/param.h> |
---|
57 | # include <netdb.h> |
---|
58 | # include <netinet/in.h> |
---|
59 | |
---|
60 | long int |
---|
61 | gethostid () |
---|
62 | { |
---|
63 | char hostname[MAXHOSTNAMELEN + 1]; |
---|
64 | size_t buflen; |
---|
65 | char *buffer; |
---|
66 | struct hostent hostbuf, *hp; |
---|
67 | unsigned long int id; |
---|
68 | struct in_addr in; |
---|
69 | int herr; |
---|
70 | int fd; |
---|
71 | |
---|
72 | /* First try to get the ID from a former invocation of sethostid. */ |
---|
73 | fd = OPEN (HOSTIDFILE, O_RDONLY); |
---|
74 | if (fd >= 0) |
---|
75 | { |
---|
76 | ssize_t n = __read (fd, &id, sizeof (id)); |
---|
77 | |
---|
78 | __close (fd); |
---|
79 | |
---|
80 | if (n == sizeof (id)) |
---|
81 | return id; |
---|
82 | } |
---|
83 | |
---|
84 | /* Getting from the file was not successful. An intelligent guess for |
---|
85 | a unique number of a host is its IP address. Return this. */ |
---|
86 | if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0') |
---|
87 | /* This also fails. Return and arbitrary value. */ |
---|
88 | return 0; |
---|
89 | |
---|
90 | buflen = 1024; |
---|
91 | buffer = alloca (buflen); |
---|
92 | |
---|
93 | /* To get the IP address we need to know the host name. */ |
---|
94 | while (__gethostbyname_r (hostname, &hostbuf, buffer, buflen, &hp, &herr) |
---|
95 | != 0 |
---|
96 | || hp == NULL) |
---|
97 | if (herr != NETDB_INTERNAL || errno != ERANGE) |
---|
98 | return 0; |
---|
99 | else |
---|
100 | { |
---|
101 | /* Enlarge buffer. */ |
---|
102 | buflen *= 2; |
---|
103 | buffer = alloca (buflen); |
---|
104 | } |
---|
105 | |
---|
106 | in.s_addr = 0; |
---|
107 | memcpy (&in, hp->h_addr, |
---|
108 | (int) sizeof (in) < hp->h_length ? sizeof (in) : hp->h_length); |
---|
109 | |
---|
110 | /* For the return value to be not exactly the IP address we do some |
---|
111 | bit fiddling. */ |
---|
112 | return in.s_addr << 16 | in.s_addr >> 16; |
---|
113 | } |
---|
114 | #endif |
---|