Allow abort PLT references in libc.so for SH.
[glibc.git] / sysdeps / unix / sysv / linux / gethostid.c
blobcc108aa2d64b616ae2c3f213b4f5f89eb42855fe
1 /* Copyright (C) 1995-2017 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, see
16 <http://www.gnu.org/licenses/>. */
18 #include <alloca.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <netdb.h>
23 #include <not-cancel.h>
25 #define HOSTIDFILE "/etc/hostid"
27 #ifdef SET_PROCEDURE
28 int
29 sethostid (long int id)
31 int fd;
32 ssize_t written;
33 int32_t id32 = id;
35 /* Test for appropriate rights to set host ID. */
36 if (__libc_enable_secure)
38 __set_errno (EPERM);
39 return -1;
42 /* Make sure the ID is not too large. Needed for bi-arch support. */
43 if (id32 != id)
45 __set_errno (EOVERFLOW);
46 return -1;
49 /* Open file for writing. Everybody is allowed to read this file. */
50 fd = open_not_cancel (HOSTIDFILE, O_CREAT|O_WRONLY|O_TRUNC, 0644);
51 if (fd < 0)
52 return -1;
54 written = write_not_cancel (fd, &id32, sizeof (id32));
56 close_not_cancel_no_status (fd);
58 return written != sizeof (id32) ? -1 : 0;
61 #else
62 # include <string.h>
63 # include <sys/param.h>
64 # include <resolv/netdb.h>
65 # include <netinet/in.h>
67 long int
68 gethostid (void)
70 char hostname[MAXHOSTNAMELEN + 1];
71 size_t buflen;
72 char *buffer;
73 struct hostent hostbuf, *hp;
74 int32_t id;
75 struct in_addr in;
76 int herr;
77 int fd;
79 /* First try to get the ID from a former invocation of sethostid. */
80 fd = open_not_cancel (HOSTIDFILE, O_RDONLY|O_LARGEFILE, 0);
81 if (fd >= 0)
83 ssize_t n = read_not_cancel (fd, &id, sizeof (id));
85 close_not_cancel_no_status (fd);
87 if (n == sizeof (id))
88 return id;
91 /* Getting from the file was not successful. An intelligent guess for
92 a unique number of a host is its IP address. Return this. */
93 if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0')
94 /* This also fails. Return and arbitrary value. */
95 return 0;
97 buflen = 1024;
98 buffer = __alloca (buflen);
100 /* To get the IP address we need to know the host name. */
101 while (__gethostbyname_r (hostname, &hostbuf, buffer, buflen, &hp, &herr)
102 != 0
103 || hp == NULL)
104 if (herr != NETDB_INTERNAL || errno != ERANGE)
105 return 0;
106 else
107 /* Enlarge buffer. */
108 buffer = extend_alloca (buffer, buflen, 2 * buflen);
110 in.s_addr = 0;
111 memcpy (&in, hp->h_addr,
112 (int) sizeof (in) < hp->h_length ? (int) sizeof (in) : hp->h_length);
114 /* For the return value to be not exactly the IP address we do some
115 bit fiddling. */
116 return (int32_t) (in.s_addr << 16 | in.s_addr >> 16);
118 #endif