-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprinter.c
332 lines (292 loc) · 9.8 KB
/
printer.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* 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 for querying the printers for queue status information.
*/
static const char rcsid[] = "$Id: printer.c,v 1.7 2000-01-05 22:01:35 ghudson Exp $";
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <syslog.h>
#include <hesiod.h>
#include "larvnetd.h"
#include "timer.h"
#define PRINTER_FALLBACK_PORT 515
struct printer_poll_args {
struct serverstate *state;
struct printer *printer;
};
static void printer_poll(void *arg);
static void printer_hes_callback(void *arg, int status, int timeouts,
unsigned char *abuf, int alen);
static void printer_host_callback(void *arg, int status, int timeouts,
struct hostent *host);
static void *make_pargs(struct serverstate *state, struct printer *printer);
void printer_start_polls(struct serverstate *state)
{
int i;
syslog(LOG_DEBUG, "printer_start_polls");
for (i = 0; i < state->config.nprinters; i++)
printer_poll(make_pargs(state, &state->config.printers[i]));
}
static void printer_poll(void *arg)
{
struct printer_poll_args *pargs = (struct printer_poll_args *) arg;
struct serverstate *state = pargs->state;
struct printer *printer = pargs->printer;
char *hesname;
/* Null out the timer, since it may have just gone off. */
printer->timer = NULL;
hesname = hesiod_to_bind(state->hescontext, printer->name, "pcap");
if (hesname == NULL)
{
syslog(LOG_ERR, "printer_poll: can't convert printer name %s to "
"hesiod name: %m", printer->name);
printer->timer = timer_set_rel(60, printer_poll, pargs);
return;
}
syslog(LOG_DEBUG, "printer_poll: printer %s starting query for %s",
printer->name, hesname);
ares_query(state->channel, hesname, C_IN, T_TXT, printer_hes_callback,
pargs);
hesiod_free_string(state->hescontext, hesname);
}
static void printer_hes_callback(void *arg, int status, int timeouts,
unsigned char *abuf, int alen)
{
struct printer_poll_args *pargs = (struct printer_poll_args *) arg;
struct serverstate *state = pargs->state;
struct printer *printer = pargs->printer;
char **vec = NULL, *p, *q;
if (status == ARES_EDESTRUCTION)
{
syslog(LOG_DEBUG, "printer_hes_callback: printer %s hesiod query "
"halted for channel destruction", printer->name);
free(pargs);
return;
}
if (status != ARES_SUCCESS)
{
syslog(LOG_ERR, "printer_hes_callback: could not resolve Hesiod pcap "
"for printer %s: %s", printer->name,
ares_strerror(status));
goto failure;
}
/* Parse the result buffer into text records. */
vec = hesiod_parse_result(state->hescontext, abuf, alen);
if (!vec || !*vec)
{
syslog(LOG_ERR, "printer_hes_callback: could not parse Hesiod pcap "
"result for printer %s: %m", printer->name);
goto failure;
}
/* Look for the print server name. */
p = strchr(*vec, ':');
while (p)
{
if (strncmp(p + 1, "rm=", 3) == 0)
break;
p = strchr(p + 1, ':');
}
if (!p)
{
syslog(LOG_ERR, "printer_hes_callback: can't find print server name in "
"Hesiod pcap result for printer %s", printer->name);
goto failure;
}
p += 4;
q = p;
while (*q && *q != ':')
q++;
*q = 0;
syslog(LOG_DEBUG, "printer_hes_callback: printer %s starting query for %s",
printer->name, p);
ares_gethostbyname(state->channel, p, AF_INET, printer_host_callback, pargs);
hesiod_free_list(state->hescontext, vec);
return;
failure:
if (vec)
hesiod_free_list(state->hescontext, vec);
printer->timer = timer_set_rel(60, printer_poll, pargs);
}
static void printer_host_callback(void *arg, int status, int timeouts,
struct hostent *host)
{
struct printer_poll_args *pargs = (struct printer_poll_args *) arg;
struct printer *printer = pargs->printer;
int s = -1, lport = IPPORT_RESERVED - 1, flags;
unsigned short port;
struct servent *servent;
struct sockaddr_in sin;
if (status == ARES_EDESTRUCTION)
{
syslog(LOG_DEBUG, "printer_host_callback: printer %s hostname query "
"halted for channel destruction", printer->name);
free(pargs);
return;
}
if (status != ARES_SUCCESS)
{
syslog(LOG_ERR, "printer_host_callback: printer %s can't resolve print "
"server name: %s", printer->name, ares_strerror(status));
goto failure;
}
s = rresvport(&lport);
if (s < 0)
{
syslog(LOG_ERR, "printer_host_callback: printer %s can't get reserved "
"port", printer->name);
goto failure;
}
/* Set s non-blocking so we can do a non-blocking connect. */
flags = fcntl(s, F_GETFL);
fcntl(s, F_SETFL, flags | O_NONBLOCK);
servent = getservbyname("printer", "tcp");
port = (servent) ? servent->s_port : htons(PRINTER_FALLBACK_PORT);
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
memcpy(&sin.sin_addr, host->h_addr, sizeof(sin.sin_addr));
sin.sin_port = port;
if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) == -1
&& errno != EINPROGRESS)
{
syslog(LOG_ERR, "printer_host_callback: printer %s can't connect to "
"print server %s: %m", printer->name, host->h_name);
goto failure;
}
/* Set up the request we want to send; the main loop will call
* printer_handle_output() when the socket selects true for
* writing.
*/
printer->s = s;
printer->to_send = 1;
sprintf(printer->buf, "\3%.*s\n", (int)(sizeof(printer->buf) - 3),
printer->name);
printer->buflen = strlen(printer->buf);
printer->jobs_counted = 0;
printer->up_so_far = 1;
syslog(LOG_DEBUG, "printer_host_callback: printer %s queued %d-byte query",
printer->name, printer->buflen);
free(pargs);
return;
failure:
if (s >= 0)
close(s);
printer->timer = timer_set_rel(60, printer_poll, pargs);
return;
}
void printer_handle_output(struct serverstate *state, struct printer *printer)
{
int count;
count = write(printer->s, printer->buf, printer->buflen);
if (count < 0)
{
syslog(LOG_ERR, "printer_handle_output: printer %s error writing to "
"print server: %m", printer->name);
close(printer->s);
printer->s = -1;
printer->timer = timer_set_rel(60, printer_poll,
make_pargs(state, printer));
return;
}
syslog(LOG_DEBUG, "printer_handle_output: printer %s wrote %d bytes",
printer->name, count);
/* Slide the data to send back in the buffer. */
printer->buflen -= count;
memmove(printer->buf, printer->buf + count, printer->buflen);
/* If we emptied the buffer, we're ready to receive. */
if (printer->buflen == 0)
{
syslog(LOG_DEBUG, "printer_handle_output: printer %s receiving",
printer->name);
printer->to_send = 0;
}
}
void printer_handle_input(struct serverstate *state, struct printer *printer)
{
int count;
char *q;
const char *p;
/* Read in a chunk of data from the printer server. */
count = read(printer->s, printer->buf + printer->buflen,
sizeof(printer->buf) - printer->buflen);
if (count < 0)
{
syslog(LOG_ERR, "printer_handle_input: printer %s error reading from "
"print server: %m", printer->name);
}
if (count <= 0)
{
printer->jobs = printer->jobs_counted;
printer->up = printer->up_so_far;
close(printer->s);
printer->s = -1;
printer->timer = timer_set_rel(60, printer_poll,
make_pargs(state, printer));
syslog(LOG_DEBUG, "printer_handle_input: printer %s ending query, "
"jobs %d up %d", printer->name, printer->jobs, printer->up);
return;
}
printer->buflen += count;
syslog(LOG_DEBUG, "printer_handle_input: printer %s read %d bytes",
printer->name, count);
/* Now look over any complete lines we have. */
p = printer->buf;
q = memchr(p, '\n', printer->buflen);
while (q)
{
*q = 0;
syslog(LOG_DEBUG, "printer_handle_input: printer %s line: %s",
printer->name, p);
if (strncmp(p, "active ", 7) == 0 || isdigit((unsigned char)*p))
printer->jobs_counted++;
else if (strstr(p, "is down") || strstr(p, "Printer Error") ||
strstr(p, "ing disabled"))
printer->up_so_far = 0;
p = q + 1;
q = memchr(p, '\n', printer->buf + printer->buflen - p);
}
/* Copy any leftover partial line back into printer->buf. */
printer->buflen -= (p - printer->buf);
memmove(printer->buf, p, printer->buflen);
/* If the buffer is full and we don't have a complete line, toss the
* buffer so we can make some forward progress. We don't expect
* this to happen when the print server is functioning correctly.
*/
if (printer->buflen == sizeof(printer->buf))
{
syslog(LOG_NOTICE, "printer_handle_input: printer %s input buffer "
"full, flushing", printer->name);
printer->buflen = 0;
}
}
static void *make_pargs(struct serverstate *state, struct printer *printer)
{
struct printer_poll_args *pargs;
pargs = emalloc(sizeof(struct printer_poll_args));
pargs->state = state;
pargs->printer = printer;
return pargs;
}