-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathws.c
256 lines (222 loc) · 7.58 KB
/
ws.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* Copyright 1998 by the Massachusetts Institute of Technology.
*
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting
* documentation, and that the name of M.I.T. not be used in
* advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is"
* without express or implied warranty.
*/
/* This file is part of larvnetd, a monitoring server. It implements
* functions to poll and receive notifications of workstation status.
*/
static const char rcsid[] = "$Id: ws.c,v 1.4 2003-09-15 17:08:35 ghudson Exp $";
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <syslog.h>
#include <netdb.h>
#include "larvnetd.h"
#include "larvnet.h"
#include "timer.h"
/* Begin polling this many seconds after the last status notification. */
#define POLLINT 30 * 60
/* After this many polls have gone unanswered, decide that we don't know
* what a machine's status is.
*/
#define POLLTRIES 8
/* After sending this many polls, reschedule for one second later and
* stop scanning machines.
*/
#define POLLMAX 20
/* Retry schedule, in seconds between tries */
static int schedule[] = { 0, 5, 5, 10, 10, 30, 30, 60, 60, 5 * 60, 5 * 60,
10 * 60, 10 * 60, 30 * 60, 30 * 60, 60 * 60,
60 * 60 };
struct wsarg {
struct serverstate *state;
struct machine *machine;
};
static void poll_callback(void *arg, int status, int timeouts,
struct hostent *host);
static int ws_searchcomp(const void *key, const void *elem);
static int ws_sortcomp(const void *elem1, const void *elem2);
void ws_poll(void *arg)
{
struct serverstate *state = (struct serverstate *) arg;
struct config *config = &state->config;
struct machine *machine;
struct wsarg *wsarg;
int i, polls_sent = 0, nexttimeout = POLLINT, nextpoll;
time_t now;
syslog(LOG_DEBUG, "ws_poll: startmachine %d", state->startmachine);
time(&now);
for (i = state->startmachine; i < config->nmachines; i++)
{
machine = &config->machines[i];
if (now - machine->laststatus >= POLLINT
&& now - machine->lastpoll >= schedule[machine->numpolls])
{
syslog(LOG_DEBUG, "ws_poll: machine %s laststatus %d lastpoll "
"%d numpolls %d", machine->name, machine->laststatus,
machine->lastpoll, machine->numpolls);
/* Resolve the machine name so that we can send a poll. */
wsarg = (struct wsarg *) emalloc(sizeof(struct wsarg));
wsarg->state = state;
wsarg->machine = machine;
ares_gethostbyname(state->channel, machine->name, AF_INET,
poll_callback, wsarg);
/* If we've already had POLLTRIES polls go unanswered, decide
* we don't know if the machine is free or not any more.
*/
if (machine->numpolls == POLLTRIES)
{
syslog(LOG_DEBUG, "ws_poll: machine %s busy state set to "
"unknown", machine->name);
machine->busy = UNKNOWN_BUSYSTATE;
}
/* Update the number of polls sent and the time of the last
* poll. */
if (machine->numpolls < (sizeof(schedule) / sizeof(int)) - 1)
machine->numpolls++;
machine->lastpoll = now;
polls_sent++;
if (polls_sent >= POLLMAX)
{
syslog(LOG_DEBUG, "ws_poll: hit POLLMAX, stalling");
nexttimeout = 1;
state->startmachine = i + 1;
break;
}
}
/* Compute the time to the next poll. */
if (now - machine->laststatus < POLLINT)
nextpoll = machine->laststatus + POLLINT - now;
else
nextpoll = machine->lastpoll + schedule[machine->numpolls] - now;
if (nexttimeout > nextpoll)
nexttimeout = nextpoll;
}
if (i == config->nmachines)
state->startmachine = 0;
syslog(LOG_DEBUG, "ws_poll: rescheduling for %d seconds with start "
"machine %d", nexttimeout, state->startmachine);
timer_set_rel(nexttimeout, ws_poll, state);
}
void ws_handle_status(int s, struct config *config)
{
struct sockaddr_in sin;
int sz = sizeof(sin), count;
char buf[LARVNET_MAX_PACKET + 1];
const char *name, *arch;
struct machine *machine;
enum busystate busystate;
/* Read a packet from the server socket. */
count = recvfrom(s, buf, sizeof(buf) - 1, 0, (struct sockaddr *) &sin, &sz);
if (count == -1)
{
syslog(LOG_ERR, "ws_handle_status: recvfrom: %m");
return;
}
if (count == 0)
{
syslog(LOG_NOTICE, "ws_handle_status: empty packet from %s",
inet_ntoa(sin.sin_addr));
return;
}
/* Pull the busy state, name, and arch name out of the packet. */
buf[count] = 0;
busystate = (buf[0] == '1') ? BUSY : (buf[0] == '0') ? FREE
: UNKNOWN_BUSYSTATE;
name = buf + 1;
arch = name + strlen(name) + 1;
if (arch >= buf + count)
{
syslog(LOG_NOTICE, "ws_handle_status: invalid packet from %s",
inet_ntoa(sin.sin_addr));
return;
}
syslog(LOG_DEBUG, "ws_handle_status: addr %s busy %c name %s arch %s",
inet_ntoa(sin.sin_addr), buf[0], name, arch);
/* We could, at this point, resolve the given name and see if
* sin.sin_addr is one of its interface addresses. That would make
* it a little harder to spoof, but not much, so we won't bother.
*/
machine = ws_find(config, name);
if (machine)
{
syslog(LOG_DEBUG, "ws_handle_status: machine %s set to state %s arch %s",
machine->name, (busystate == BUSY) ? "busy"
: (busystate == FREE) ? "free" : "unknown", arch);
machine->busy = busystate;
if (machine->arch)
free(machine->arch);
machine->arch = estrdup(arch);
time(&machine->laststatus);
machine->numpolls = 0;
}
}
struct machine *ws_find(struct config *config, const char *name)
{
return bsearch(name, config->machines, config->nmachines,
sizeof(struct machine), ws_searchcomp);
}
void ws_sort(struct config *config)
{
qsort(config->machines, config->nmachines, sizeof(struct machine),
ws_sortcomp);
}
static void poll_callback(void *arg, int status, int timeouts,
struct hostent *host)
{
struct wsarg *wsarg = (struct wsarg *) arg;
struct serverstate *state = wsarg->state;
struct machine *machine = wsarg->machine;
struct sockaddr_in sin;
char dummy = 0;
free(wsarg);
if (status != ARES_SUCCESS)
{
if (status == ARES_EDESTRUCTION)
{
syslog(LOG_DEBUG, "poll_callback: query for %s halted for channel "
"destruction", machine->name);
}
else
{
syslog(LOG_ERR, "poll_callback: could not resolve ws name %s: %s",
machine->name, ares_strerror(status));
}
return;
}
/* Send a poll. */
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
memcpy(&sin.sin_addr, host->h_addr, sizeof(sin.sin_addr));
sin.sin_port = state->poll_port;
sendto(state->server_socket, &dummy, 1, 0,
(struct sockaddr *) &sin, sizeof(sin));
syslog(LOG_DEBUG, "poll_callback: query for ws name %s yielded %s; poll "
"sent", machine->name, inet_ntoa(sin.sin_addr));
}
static int ws_searchcomp(const void *key, const void *elem)
{
const char *s = (const char *) key;
const struct machine *m = (const struct machine *) elem;
return strcasecmp(s, m->name);
}
static int ws_sortcomp(const void *elem1, const void *elem2)
{
const struct machine *m1 = (const struct machine *) elem1;
const struct machine *m2 = (const struct machine *) elem2;
return strcasecmp(m1->name, m2->name);
}