]>
Commit | Line | Data |
---|---|---|
ed486911 | 1 | /* |
2e4ff8af | 2 | * "$Id: runloop.c 6834 2007-08-22 18:29:25Z mike $" |
ed486911 | 3 | * |
09a101d6 | 4 | * Common run loop APIs for the Common UNIX Printing System (CUPS). |
ed486911 | 5 | * |
91c84a35 | 6 | * Copyright 2007-2008 by Apple Inc. |
f7deaa1a | 7 | * Copyright 2006-2007 by Easy Software Products, all rights reserved. |
ed486911 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
ed486911 | 12 | * "LICENSE" which should have been included with this file. If this |
bc44d920 | 13 | * file is missing or damaged, see the license at "http://www.cups.org/". |
ed486911 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
09a101d6 | 19 | * backendDrainOutput() - Drain pending print data to the device. |
20 | * backendRunLoop() - Read and write print and back-channel data. | |
ed486911 | 21 | */ |
22 | ||
23 | /* | |
24 | * Include necessary headers. | |
25 | */ | |
26 | ||
27 | #include "backend-private.h" | |
568fa3fa | 28 | #include <limits.h> |
d09495fa | 29 | #ifdef __hpux |
30 | # include <sys/time.h> | |
31 | #else | |
32 | # include <sys/select.h> | |
33 | #endif /* __hpux */ | |
ed486911 | 34 | |
35 | ||
09a101d6 | 36 | /* |
37 | * 'backendDrainOutput()' - Drain pending print data to the device. | |
38 | */ | |
39 | ||
40 | int /* O - 0 on success, -1 on error */ | |
41 | backendDrainOutput(int print_fd, /* I - Print file descriptor */ | |
42 | int device_fd) /* I - Device file descriptor */ | |
43 | { | |
44 | int nfds; /* Maximum file descriptor value + 1 */ | |
45 | fd_set input; /* Input set for reading */ | |
46 | ssize_t print_bytes, /* Print bytes read */ | |
47 | bytes; /* Bytes written */ | |
48 | char print_buffer[8192], /* Print data buffer */ | |
49 | *print_ptr; /* Pointer into print data buffer */ | |
50 | struct timeval timeout; /* Timeout for read... */ | |
51 | ||
52 | ||
53 | fprintf(stderr, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n", | |
54 | print_fd, device_fd); | |
55 | ||
56 | /* | |
57 | * Figure out the maximum file descriptor value to use with select()... | |
58 | */ | |
59 | ||
60 | nfds = (print_fd > device_fd ? print_fd : device_fd) + 1; | |
61 | ||
62 | /* | |
63 | * Now loop until we are out of data from print_fd... | |
64 | */ | |
65 | ||
66 | for (;;) | |
67 | { | |
68 | /* | |
69 | * Use select() to determine whether we have data to copy around... | |
70 | */ | |
71 | ||
72 | FD_ZERO(&input); | |
73 | FD_SET(print_fd, &input); | |
74 | ||
75 | timeout.tv_sec = 0; | |
76 | timeout.tv_usec = 0; | |
77 | ||
78 | if (select(nfds, &input, NULL, NULL, &timeout) < 0) | |
79 | return (-1); | |
80 | ||
81 | if (!FD_ISSET(print_fd, &input)) | |
82 | return (0); | |
83 | ||
84 | if ((print_bytes = read(print_fd, print_buffer, | |
85 | sizeof(print_buffer))) < 0) | |
86 | { | |
87 | /* | |
88 | * Read error - bail if we don't see EAGAIN or EINTR... | |
89 | */ | |
90 | ||
91 | if (errno != EAGAIN || errno != EINTR) | |
92 | { | |
93 | perror("ERROR: Unable to read print data"); | |
94 | return (-1); | |
95 | } | |
96 | ||
97 | print_bytes = 0; | |
98 | } | |
99 | else if (print_bytes == 0) | |
100 | { | |
101 | /* | |
102 | * End of file, return... | |
103 | */ | |
104 | ||
105 | return (0); | |
106 | } | |
107 | ||
108 | fprintf(stderr, "DEBUG: Read %d bytes of print data...\n", | |
109 | (int)print_bytes); | |
110 | ||
111 | for (print_ptr = print_buffer; print_bytes > 0;) | |
112 | { | |
113 | if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0) | |
114 | { | |
115 | /* | |
116 | * Write error - bail if we don't see an error we can retry... | |
117 | */ | |
118 | ||
119 | if (errno != ENOSPC && errno != ENXIO && errno != EAGAIN && | |
120 | errno != EINTR && errno != ENOTTY) | |
121 | { | |
db1f069b MS |
122 | _cupsLangPrintf(stderr, _("ERROR: Unable to write print data: %s\n"), |
123 | strerror(errno)); | |
09a101d6 | 124 | return (-1); |
125 | } | |
126 | } | |
127 | else | |
128 | { | |
129 | fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes); | |
130 | ||
131 | print_bytes -= bytes; | |
132 | print_ptr += bytes; | |
133 | } | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | ||
568fa3fa MS |
139 | /* |
140 | * 'backendNetworkSideCB()' - Handle common network side-channel commands. | |
141 | */ | |
142 | ||
143 | void | |
144 | backendNetworkSideCB( | |
145 | int print_fd, /* I - Print file or -1 */ | |
146 | int device_fd, /* I - Device file or -1 */ | |
147 | int snmp_fd, /* I - SNMP socket */ | |
148 | http_addr_t *addr, /* I - Address of device */ | |
149 | int use_bc) /* I - Use back-channel data? */ | |
150 | { | |
151 | cups_sc_command_t command; /* Request command */ | |
152 | cups_sc_status_t status; /* Request/response status */ | |
153 | char data[2048]; /* Request/response data */ | |
154 | int datalen; /* Request/response data size */ | |
155 | const char *device_id; /* 1284DEVICEID env var */ | |
156 | ||
157 | ||
158 | datalen = sizeof(data); | |
159 | ||
160 | if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0)) | |
161 | { | |
162 | _cupsLangPuts(stderr, _("WARNING: Failed to read side-channel request!\n")); | |
163 | return; | |
164 | } | |
165 | ||
166 | switch (command) | |
167 | { | |
168 | case CUPS_SC_CMD_DRAIN_OUTPUT : | |
169 | /* | |
170 | * Our sockets disable the Nagle algorithm and data is sent immediately. | |
171 | */ | |
172 | ||
173 | if (backendDrainOutput(print_fd, device_fd)) | |
174 | status = CUPS_SC_STATUS_IO_ERROR; | |
175 | else | |
176 | status = CUPS_SC_STATUS_OK; | |
177 | ||
178 | datalen = 0; | |
179 | break; | |
180 | ||
181 | case CUPS_SC_CMD_GET_BIDI : | |
182 | data[0] = use_bc; | |
183 | datalen = 1; | |
184 | break; | |
185 | ||
186 | case CUPS_SC_CMD_GET_DEVICE_ID : | |
187 | if ((device_id = getenv("1284DEVICEID")) != NULL) | |
188 | { | |
189 | strlcpy(data, device_id, sizeof(data)); | |
190 | datalen = (int)strlen(data); | |
191 | break; | |
192 | } | |
193 | ||
194 | default : | |
195 | status = CUPS_SC_STATUS_NOT_IMPLEMENTED; | |
196 | datalen = 0; | |
197 | break; | |
198 | } | |
199 | ||
200 | cupsSideChannelWrite(command, status, data, datalen, 1.0); | |
201 | } | |
202 | ||
203 | ||
ed486911 | 204 | /* |
205 | * 'backendRunLoop()' - Read and write print and back-channel data. | |
206 | */ | |
207 | ||
208 | ssize_t /* O - Total bytes on success, -1 on error */ | |
f7deaa1a | 209 | backendRunLoop( |
568fa3fa MS |
210 | int print_fd, /* I - Print file descriptor */ |
211 | int device_fd, /* I - Device file descriptor */ | |
212 | int snmp_fd, /* I - SNMP socket or -1 if none */ | |
213 | http_addr_t *addr, /* I - Address of device */ | |
214 | int use_bc, /* I - Use back-channel? */ | |
215 | void (*side_cb)(int, int, int, http_addr_t *, int)) | |
216 | /* I - Side-channel callback */ | |
ed486911 | 217 | { |
218 | int nfds; /* Maximum file descriptor value + 1 */ | |
219 | fd_set input, /* Input set for reading */ | |
220 | output; /* Output set for writing */ | |
221 | ssize_t print_bytes, /* Print bytes read */ | |
222 | bc_bytes, /* Backchannel bytes read */ | |
223 | total_bytes, /* Total bytes written */ | |
224 | bytes; /* Bytes written */ | |
225 | int paperout; /* "Paper out" status */ | |
8ca02f3c | 226 | int offline; /* "Off-line" status */ |
ed486911 | 227 | char print_buffer[8192], /* Print data buffer */ |
228 | *print_ptr, /* Pointer into print data buffer */ | |
229 | bc_buffer[1024]; /* Back-channel data buffer */ | |
568fa3fa MS |
230 | struct timeval timeout; /* Timeout for select() */ |
231 | time_t curtime, /* Current time */ | |
232 | snmp_update = 0; | |
ed486911 | 233 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
234 | struct sigaction action; /* Actions for POSIX signals */ | |
235 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
236 | ||
237 | ||
c0e1af83 | 238 | fprintf(stderr, |
568fa3fa MS |
239 | "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, snmp_fd=%d, " |
240 | "addr=%p, use_bc=%d, side_cb=%p)\n", | |
241 | print_fd, device_fd, snmp_fd, addr, use_bc, side_cb); | |
8ca02f3c | 242 | |
ed486911 | 243 | /* |
244 | * If we are printing data from a print driver on stdin, ignore SIGTERM | |
245 | * so that the driver can finish out any page data, e.g. to eject the | |
246 | * current page. We only do this for stdin printing as otherwise there | |
247 | * is no way to cancel a raw print job... | |
248 | */ | |
249 | ||
250 | if (!print_fd) | |
251 | { | |
252 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
253 | sigset(SIGTERM, SIG_IGN); | |
254 | #elif defined(HAVE_SIGACTION) | |
255 | memset(&action, 0, sizeof(action)); | |
256 | ||
257 | sigemptyset(&action.sa_mask); | |
258 | action.sa_handler = SIG_IGN; | |
259 | sigaction(SIGTERM, &action, NULL); | |
260 | #else | |
261 | signal(SIGTERM, SIG_IGN); | |
262 | #endif /* HAVE_SIGSET */ | |
263 | } | |
264 | ||
265 | /* | |
266 | * Figure out the maximum file descriptor value to use with select()... | |
267 | */ | |
268 | ||
269 | nfds = (print_fd > device_fd ? print_fd : device_fd) + 1; | |
270 | ||
271 | /* | |
272 | * Now loop until we are out of data from print_fd... | |
273 | */ | |
274 | ||
b94498cf | 275 | for (print_bytes = 0, print_ptr = print_buffer, offline = -1, |
276 | paperout = -1, total_bytes = 0;;) | |
ed486911 | 277 | { |
278 | /* | |
279 | * Use select() to determine whether we have data to copy around... | |
280 | */ | |
281 | ||
282 | FD_ZERO(&input); | |
283 | if (!print_bytes) | |
284 | FD_SET(print_fd, &input); | |
285 | if (use_bc) | |
286 | FD_SET(device_fd, &input); | |
f7deaa1a | 287 | if (side_cb) |
288 | FD_SET(CUPS_SC_FD, &input); | |
ed486911 | 289 | |
290 | FD_ZERO(&output); | |
91c84a35 | 291 | if (print_bytes || (!use_bc && !side_cb)) |
ed486911 | 292 | FD_SET(device_fd, &output); |
293 | ||
f7deaa1a | 294 | if (use_bc || side_cb) |
8ca02f3c | 295 | { |
568fa3fa MS |
296 | timeout.tv_sec = 5; |
297 | timeout.tv_usec = 0; | |
298 | ||
299 | if (select(nfds, &input, &output, NULL, &timeout) < 0) | |
8ca02f3c | 300 | { |
301 | /* | |
302 | * Pause printing to clear any pending errors... | |
303 | */ | |
304 | ||
b94498cf | 305 | if (errno == ENXIO && offline != 1) |
8ca02f3c | 306 | { |
307 | fputs("STATE: +offline-error\n", stderr); | |
080811b1 | 308 | _cupsLangPuts(stderr, _("INFO: Printer is currently offline.\n")); |
8ca02f3c | 309 | offline = 1; |
310 | } | |
b94498cf | 311 | else if (errno == EINTR && total_bytes == 0) |
312 | { | |
313 | fputs("DEBUG: Received an interrupt before any bytes were " | |
314 | "written, aborting!\n", stderr); | |
315 | return (0); | |
316 | } | |
8ca02f3c | 317 | |
318 | sleep(1); | |
319 | continue; | |
320 | } | |
321 | } | |
ed486911 | 322 | |
f7deaa1a | 323 | /* |
324 | * Check if we have a side-channel request ready... | |
325 | */ | |
326 | ||
327 | if (side_cb && FD_ISSET(CUPS_SC_FD, &input)) | |
568fa3fa | 328 | (*side_cb)(print_fd, device_fd, snmp_fd, addr, use_bc); |
f7deaa1a | 329 | |
ed486911 | 330 | /* |
331 | * Check if we have back-channel data ready... | |
332 | */ | |
333 | ||
334 | if (FD_ISSET(device_fd, &input)) | |
335 | { | |
336 | if ((bc_bytes = read(device_fd, bc_buffer, sizeof(bc_buffer))) > 0) | |
337 | { | |
338 | fprintf(stderr, | |
339 | "DEBUG: Received " CUPS_LLFMT " bytes of back-channel data!\n", | |
340 | CUPS_LLCAST bc_bytes); | |
341 | cupsBackChannelWrite(bc_buffer, bc_bytes, 1.0); | |
342 | } | |
343 | } | |
344 | ||
345 | /* | |
346 | * Check if we have print data ready... | |
347 | */ | |
348 | ||
349 | if (FD_ISSET(print_fd, &input)) | |
350 | { | |
351 | if ((print_bytes = read(print_fd, print_buffer, | |
352 | sizeof(print_buffer))) < 0) | |
353 | { | |
354 | /* | |
355 | * Read error - bail if we don't see EAGAIN or EINTR... | |
356 | */ | |
357 | ||
358 | if (errno != EAGAIN || errno != EINTR) | |
359 | { | |
360 | perror("ERROR: Unable to read print data"); | |
361 | return (-1); | |
362 | } | |
363 | ||
364 | print_bytes = 0; | |
365 | } | |
366 | else if (print_bytes == 0) | |
367 | { | |
368 | /* | |
369 | * End of file, break out of the loop... | |
370 | */ | |
371 | ||
372 | break; | |
373 | } | |
374 | ||
375 | print_ptr = print_buffer; | |
8ca02f3c | 376 | |
377 | fprintf(stderr, "DEBUG: Read %d bytes of print data...\n", | |
378 | (int)print_bytes); | |
ed486911 | 379 | } |
380 | ||
381 | /* | |
382 | * Check if the device is ready to receive data and we have data to | |
383 | * send... | |
384 | */ | |
385 | ||
386 | if (print_bytes && FD_ISSET(device_fd, &output)) | |
387 | { | |
388 | if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0) | |
389 | { | |
390 | /* | |
391 | * Write error - bail if we don't see an error we can retry... | |
392 | */ | |
393 | ||
394 | if (errno == ENOSPC) | |
395 | { | |
b94498cf | 396 | if (paperout != 1) |
ed486911 | 397 | { |
b86bc4cf | 398 | fputs("STATE: +media-empty-error\n", stderr); |
db1f069b | 399 | _cupsLangPuts(stderr, _("ERROR: Out of paper!\n")); |
ed486911 | 400 | paperout = 1; |
401 | } | |
402 | } | |
8ca02f3c | 403 | else if (errno == ENXIO) |
404 | { | |
b94498cf | 405 | if (offline != 1) |
8ca02f3c | 406 | { |
407 | fputs("STATE: +offline-error\n", stderr); | |
db1f069b | 408 | _cupsLangPuts(stderr, _("INFO: Printer is currently off-line.\n")); |
8ca02f3c | 409 | offline = 1; |
410 | } | |
411 | } | |
ed486911 | 412 | else if (errno != EAGAIN && errno != EINTR && errno != ENOTTY) |
413 | { | |
c0e1af83 | 414 | fprintf(stderr, _("ERROR: Unable to write print data: %s\n"), |
415 | strerror(errno)); | |
ed486911 | 416 | return (-1); |
417 | } | |
418 | } | |
419 | else | |
420 | { | |
421 | if (paperout) | |
422 | { | |
b86bc4cf | 423 | fputs("STATE: -media-empty-error\n", stderr); |
ed486911 | 424 | paperout = 0; |
425 | } | |
426 | ||
8ca02f3c | 427 | if (offline) |
428 | { | |
429 | fputs("STATE: -offline-error\n", stderr); | |
080811b1 | 430 | _cupsLangPuts(stderr, _("INFO: Printer is now online.\n")); |
8ca02f3c | 431 | offline = 0; |
432 | } | |
433 | ||
434 | fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes); | |
ed486911 | 435 | |
436 | print_bytes -= bytes; | |
437 | print_ptr += bytes; | |
438 | total_bytes += bytes; | |
439 | } | |
440 | } | |
568fa3fa MS |
441 | |
442 | /* | |
443 | * Do SNMP updates periodically... | |
444 | */ | |
445 | ||
446 | if (snmp_fd >= 0 && time(&curtime) >= snmp_update) | |
447 | { | |
448 | if (backendSNMPSupplies(snmp_fd, addr, NULL, NULL)) | |
449 | snmp_update = INT_MAX; | |
450 | else | |
451 | snmp_update = curtime + 5; | |
452 | } | |
ed486911 | 453 | } |
454 | ||
455 | /* | |
456 | * Return with success... | |
457 | */ | |
458 | ||
459 | return (total_bytes); | |
460 | } | |
461 | ||
462 | ||
463 | /* | |
2e4ff8af | 464 | * End of "$Id: runloop.c 6834 2007-08-22 18:29:25Z mike $". |
ed486911 | 465 | */ |