]> TLD Linux GIT Repositories - rc-scripts.git/blob - src/genhostid.c
- from PLD
[rc-scripts.git] / src / genhostid.c
1 /* Copyright (C) 2003 Red Hat, Inc.
2  *
3  * This software may be freely redistributed under the terms of the GNU
4  * public license.
5  *
6  * You should have received a copy of the GNU General Public License
7  * along with this program; if not, write to the Free Software
8  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
9  *         
10  */
11
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #include <time.h>
15 #include <unistd.h>
16 #include <sys/stat.h>
17 int
18 main (void)
19 {
20   struct stat st;
21   long int n;
22   if (stat ("/etc/hostid", &st) == 0 && S_ISREG (st.st_mode)
23       && st.st_size >= sizeof (n))
24     return 0;
25   int fd = open ("/dev/random", O_RDONLY);
26   if (fd == -1 || read (fd, &n, sizeof (n)) != sizeof (n))
27     {
28       srand48 ((long int) time (NULL) ^ (long int) getpid ());
29       n = lrand48 ();
30     }
31   return sethostid (n);
32 }