1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/*
* Copyright © 2005,2006 Red Hat, Inc. All rights reserved.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions of the
* GNU General Public License v.2. This program is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY expressed or implied,
* including the implied warranties of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat
* trademarks that are incorporated in the source code or documentation are not
* subject to the GNU General Public License and may only be used or replicated
* with the express permission of Red Hat, Inc.
*
* Red Hat Author(s): Nathan Straz <nstraz@redhat.com>
* Dean Jansa <djansa@redhat.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <syslog.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <sys/times.h>
#include "btime_int.h"
static unsigned int get_btime(void);
int
main(int argc, char **argv)
{
int sd;
char inmsg[BTIME_MSGLEN];
char btimeonly[BTIME_MSGLEN];
char cookiemsg[BTIME_MSGLEN];
char *outmsg;
struct sockaddr_in cli_addr;
socklen_t cli_addr_len;
ssize_t nbytes;
unsigned int local_btime;
openlog("btimed", LOG_PID, LOG_USER);
/* Running out of (x)inetd the socket was duped onto stdin. */
sd = fileno(stdin);
/* We want to exit after 30 seconds of inactivity */
alarm(30);
/* Generate the standard btime message */
memset(btimeonly, 0, BTIME_MSGLEN);
local_btime = get_btime();
sprintf(btimeonly, "%u\n", local_btime);
syslog(LOG_INFO, "started with btime = %u", local_btime);
for (;;) {
memset(&cli_addr, 0, sizeof cli_addr);
memset(inmsg, 0, BTIME_MSGLEN);
cli_addr_len = sizeof cli_addr;
nbytes = recvfrom(sd, &inmsg, BTIME_MSGLEN, MSG_WAITALL,
(struct sockaddr *)&cli_addr, &cli_addr_len);
if (nbytes < 0) {
/* Bail, we may have had an alarm(), or other error.
* client side resends request, no need to retry here.
*/
syslog(LOG_INFO, "exitting");
exit(0);
}
if (inmsg[0] == 'B' && inmsg[1] == 'T' ) {
/* New style heartbeat with cookie */
/* Copy cookie to message and append timestamp */
memset(cookiemsg, 0, BTIME_MSGLEN);
memcpy(cookiemsg, inmsg, COOKIE_LEN);
strcpy(cookiemsg + COOKIE_LEN, btimeonly);
outmsg = cookiemsg;
} else {
outmsg = btimeonly;
}
sendto(sd, outmsg, BTIME_MSGLEN, MSG_DONTWAIT,
(struct sockaddr *)&cli_addr, cli_addr_len);
/* We want to exit after 30 seconds of inactivity */
alarm(30);
}
return 0;
}
/*
*---------------------------------------------------------------------------
*
* get_btime --
*
* Return machine's boot time rounded up to the nearest minute.
*
* Returns:
* 0 on failure
* non-zero on success.
*
*---------------------------------------------------------------------------
*/
static unsigned int
get_btime(void)
{
unsigned int btime = 0;
long hertz = sysconf(_SC_CLK_TCK);
struct tms tms;
btime = time(0) - (times(&tms) / hertz);
btime = btime - (btime % 60) + 60;
return btime;
}
|