1 /* Copyright (C) 1995 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
15 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 Boston, MA 02111-1307, USA. */
22 #define HOSTIDFILE "/var/adm/hostid"
32 /* Test for appropriate rights to set host ID. */
33 if (geteuid () || getuid ())
39 /* Open file for writing. Everybody is allowed to read this file. */
40 fd
= __open (HOSTIDFILE
, O_CREAT
|O_WRONLY
, 0644);
44 written
= __write (fd
, &id
, sizeof (id
));
48 return written
!= sizeof (id
) ? -1 : 0;
52 # include <sys/param.h>
53 # include <resolv/netdb.h>
54 # include <netinet/in.h>
59 char hostname
[MAXHOSTNAMELEN
+ 1];
65 /* First try to get the ID from a former invocation of sethostid. */
66 fd
= __open (HOSTIDFILE
, O_RDONLY
);
69 ssize_t n
= __read (fd
, &id
, sizeof (id
));
77 /* Getting from the file was not succesful. An intelligent guess for
78 a unique number of a host is its IP address. Return this. */
79 if (gethostname (hostname
, MAXHOSTNAMELEN
) < 0 || hostname
[0] == '\0')
80 /* This also fails. Return and arbitrary value. */
83 /* To get the IP address we need to knoe the host name. */
84 hp
= gethostbyname (hostname
);
89 memcpy (&in
, hp
->h_addr
,
90 sizeof (in
) < hp
->h_length
? sizeof (in
) : hp
->h_length
);
92 /* For the return value to be not exactly the IP address we do some
94 return in
.s_addr
<< 16 | in
.s_addr
>> 16;