]>
Commit | Line | Data |
---|---|---|
2b778ceb | 1 | /* Copyright (C) 1995-2021 Free Software Foundation, Inc. |
41bdb6e2 | 2 | This file is part of the GNU C Library. |
d2f5be2a | 3 | |
e4cf5070 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
e4cf5070 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
e4cf5070 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
e4cf5070 UD |
17 | |
18 | #include <alloca.h> | |
d2f5be2a UD |
19 | #include <errno.h> |
20 | #include <fcntl.h> | |
21 | #include <unistd.h> | |
b77e6cd6 | 22 | #include <netdb.h> |
2c008571 | 23 | #include <not-cancel.h> |
890c2ced | 24 | #include <stdbool.h> |
d2f5be2a | 25 | |
e0baae12 | 26 | #define HOSTIDFILE "/etc/hostid" |
d2f5be2a UD |
27 | |
28 | #ifdef SET_PROCEDURE | |
29 | int | |
bd2260a2 | 30 | sethostid (long int id) |
d2f5be2a UD |
31 | { |
32 | int fd; | |
33 | ssize_t written; | |
04736140 | 34 | int32_t id32 = id; |
d2f5be2a UD |
35 | |
36 | /* Test for appropriate rights to set host ID. */ | |
b33f91e9 | 37 | if (__libc_enable_secure) |
d2f5be2a | 38 | { |
c4029823 | 39 | __set_errno (EPERM); |
d2f5be2a UD |
40 | return -1; |
41 | } | |
42 | ||
04736140 UD |
43 | /* Make sure the ID is not too large. Needed for bi-arch support. */ |
44 | if (id32 != id) | |
45 | { | |
46 | __set_errno (EOVERFLOW); | |
47 | return -1; | |
48 | } | |
49 | ||
d2f5be2a | 50 | /* Open file for writing. Everybody is allowed to read this file. */ |
c2284574 | 51 | fd = __open_nocancel (HOSTIDFILE, O_CREAT|O_WRONLY|O_TRUNC, 0644); |
d2f5be2a UD |
52 | if (fd < 0) |
53 | return -1; | |
54 | ||
c647fb88 | 55 | written = __write_nocancel (fd, &id32, sizeof (id32)); |
d2f5be2a | 56 | |
c181840c | 57 | __close_nocancel_nostatus (fd); |
d2f5be2a | 58 | |
04736140 | 59 | return written != sizeof (id32) ? -1 : 0; |
d2f5be2a UD |
60 | } |
61 | ||
62 | #else | |
7f811679 | 63 | # include <string.h> |
d2f5be2a UD |
64 | # include <sys/param.h> |
65 | # include <resolv/netdb.h> | |
66 | # include <netinet/in.h> | |
890c2ced | 67 | # include <scratch_buffer.h> |
d2f5be2a UD |
68 | |
69 | long int | |
60d2f8f3 | 70 | gethostid (void) |
d2f5be2a UD |
71 | { |
72 | char hostname[MAXHOSTNAMELEN + 1]; | |
e4cf5070 | 73 | struct hostent hostbuf, *hp; |
04736140 | 74 | int32_t id; |
d2f5be2a | 75 | struct in_addr in; |
e4cf5070 | 76 | int herr; |
d2f5be2a UD |
77 | int fd; |
78 | ||
79 | /* First try to get the ID from a former invocation of sethostid. */ | |
c2284574 | 80 | fd = __open_nocancel (HOSTIDFILE, O_RDONLY|O_LARGEFILE, 0); |
d2f5be2a UD |
81 | if (fd >= 0) |
82 | { | |
a748eb31 | 83 | ssize_t n = __read_nocancel (fd, &id, sizeof (id)); |
d2f5be2a | 84 | |
c181840c | 85 | __close_nocancel_nostatus (fd); |
d2f5be2a UD |
86 | |
87 | if (n == sizeof (id)) | |
88 | return id; | |
89 | } | |
90 | ||
890c2ced FW |
91 | /* Getting from the file was not successful. An intelligent guess |
92 | for a unique number of a host is its IP address. To get the IP | |
93 | address we need to know the host name. */ | |
50304ef0 | 94 | if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0') |
d2f5be2a UD |
95 | /* This also fails. Return and arbitrary value. */ |
96 | return 0; | |
97 | ||
890c2ced FW |
98 | /* Determine the IP address of the host name. */ |
99 | struct scratch_buffer tmpbuf; | |
100 | scratch_buffer_init (&tmpbuf); | |
101 | while (true) | |
102 | { | |
103 | int ret = __gethostbyname_r (hostname, &hostbuf, | |
104 | tmpbuf.data, tmpbuf.length, &hp, &herr); | |
1214ba06 | 105 | if (ret == 0 && hp != NULL) |
890c2ced FW |
106 | break; |
107 | else | |
108 | { | |
109 | /* Enlarge the buffer on ERANGE. */ | |
1214ba06 | 110 | if (ret != 0 && herr == NETDB_INTERNAL && errno == ERANGE) |
890c2ced FW |
111 | { |
112 | if (!scratch_buffer_grow (&tmpbuf)) | |
113 | return 0; | |
114 | } | |
115 | /* Other errors are a failure. Return an arbitrary value. */ | |
116 | else | |
117 | { | |
118 | scratch_buffer_free (&tmpbuf); | |
119 | return 0; | |
120 | } | |
121 | } | |
122 | } | |
d2f5be2a UD |
123 | |
124 | in.s_addr = 0; | |
125 | memcpy (&in, hp->h_addr, | |
57b36a0a | 126 | (int) sizeof (in) < hp->h_length ? (int) sizeof (in) : hp->h_length); |
890c2ced | 127 | scratch_buffer_free (&tmpbuf); |
d2f5be2a UD |
128 | /* For the return value to be not exactly the IP address we do some |
129 | bit fiddling. */ | |
04736140 | 130 | return (int32_t) (in.s_addr << 16 | in.s_addr >> 16); |
d2f5be2a UD |
131 | } |
132 | #endif |