1 /* Copyright (C) 1995, 1996, 1998 Free Software Foundation, Inc.
3 The GNU C Library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
8 The GNU C Library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public
14 License along with the GNU C Library; see the file COPYING.LIB. If not,
15 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 Boston, MA 02111-1307, USA. */
24 #define HOSTIDFILE "/var/adm/hostid"
34 /* Test for appropriate rights to set host ID. */
35 if (__libc_enable_secure
)
41 /* Open file for writing. Everybody is allowed to read this file. */
42 fd
= __open (HOSTIDFILE
, O_CREAT
|O_WRONLY
, 0644);
46 written
= __write (fd
, &id
, sizeof (id
));
50 return written
!= sizeof (id
) ? -1 : 0;
55 # include <sys/param.h>
56 # include <resolv/netdb.h>
57 # include <netinet/in.h>
62 char hostname
[MAXHOSTNAMELEN
+ 1];
65 struct hostent hostbuf
, *hp
;
71 /* First try to get the ID from a former invocation of sethostid. */
72 fd
= __open (HOSTIDFILE
, O_RDONLY
);
75 ssize_t n
= __read (fd
, &id
, sizeof (id
));
83 /* Getting from the file was not successful. An intelligent guess for
84 a unique number of a host is its IP address. Return this. */
85 if (__gethostname (hostname
, MAXHOSTNAMELEN
) < 0 || hostname
[0] == '\0')
86 /* This also fails. Return and arbitrary value. */
90 buffer
= __alloca (buflen
);
92 /* To get the IP address we need to know the host name. */
93 while (__gethostbyname_r (hostname
, &hostbuf
, buffer
, buflen
, &hp
, &herr
)
95 if (herr
!= NETDB_INTERNAL
|| errno
!= ERANGE
)
101 buffer
= __alloca (buflen
);
105 memcpy (&in
, hp
->h_addr
,
106 (int) sizeof (in
) < hp
->h_length
? sizeof (in
) : hp
->h_length
);
108 /* For the return value to be not exactly the IP address we do some
110 return in
.s_addr
<< 16 | in
.s_addr
>> 16;