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