]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/runloop.c
Fix .PHONY declaration
[thirdparty/cups.git] / backend / runloop.c
CommitLineData
ed486911 1/*
5a1d7a17 2 * Common run loop APIs for CUPS backends.
ed486911 3 *
7e86f2f6 4 * Copyright 2007-2014 by Apple Inc.
5a1d7a17 5 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
ed486911 6 *
5a1d7a17
MS
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/".
ed486911 12 *
5a1d7a17 13 * This file is subject to the Apple OS-Developed Software exception.
ed486911 14 */
15
16/*
17 * Include necessary headers.
18 */
19
20#include "backend-private.h"
568fa3fa 21#include <limits.h>
5a1d7a17 22#include <sys/select.h>
ed486911 23
24
09a101d6 25/*
26 * 'backendDrainOutput()' - Drain pending print data to the device.
27 */
28
29int /* O - 0 on success, -1 on error */
30backendDrainOutput(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 {
f3c17241
MS
82 fprintf(stderr, "DEBUG: Read failed: %s\n", strerror(errno));
83 _cupsLangPrintFilter(stderr, "ERROR", _("Unable to read print data."));
09a101d6 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 {
7e86f2f6 103 if ((bytes = write(device_fd, print_ptr, (size_t)print_bytes)) < 0)
09a101d6 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 {
0837b7e8 112 _cupsLangPrintError("ERROR", _("Unable to write print data"));
09a101d6 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
ed486911 128/*
129 * 'backendRunLoop()' - Read and write print and back-channel data.
130 */
131
132ssize_t /* O - Total bytes on success, -1 on error */
f7deaa1a 133backendRunLoop(
c8fef167
MS
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 */
ed486911 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 */
8ca02f3c 150 int offline; /* "Off-line" status */
ed486911 151 char print_buffer[8192], /* Print data buffer */
152 *print_ptr, /* Pointer into print data buffer */
153 bc_buffer[1024]; /* Back-channel data buffer */
568fa3fa
MS
154 struct timeval timeout; /* Timeout for select() */
155 time_t curtime, /* Current time */
156 snmp_update = 0;
ed486911 157#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
158 struct sigaction action; /* Actions for POSIX signals */
159#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
160
161
c0e1af83 162 fprintf(stderr,
568fa3fa
MS
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);
8ca02f3c 166
ed486911 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 }
4e6f60f0
MS
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 }
ed486911 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
b94498cf 207 for (print_bytes = 0, print_ptr = print_buffer, offline = -1,
208 paperout = -1, total_bytes = 0;;)
ed486911 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);
dd1abb6b 219 if (!print_bytes && side_cb)
f7deaa1a 220 FD_SET(CUPS_SC_FD, &input);
ed486911 221
222 FD_ZERO(&output);
91c84a35 223 if (print_bytes || (!use_bc && !side_cb))
ed486911 224 FD_SET(device_fd, &output);
225
f7deaa1a 226 if (use_bc || side_cb)
8ca02f3c 227 {
568fa3fa
MS
228 timeout.tv_sec = 5;
229 timeout.tv_usec = 0;
230
231 if (select(nfds, &input, &output, NULL, &timeout) < 0)
8ca02f3c 232 {
233 /*
234 * Pause printing to clear any pending errors...
235 */
236
ef55b745 237 if (errno == ENXIO && offline != 1 && update_state)
8ca02f3c 238 {
c5571a1d 239 fputs("STATE: +offline-report\n", stderr);
0837b7e8 240 _cupsLangPrintFilter(stderr, "INFO",
f3c17241 241 _("The printer is not connected."));
8ca02f3c 242 offline = 1;
243 }
b94498cf 244 else if (errno == EINTR && total_bytes == 0)
245 {
246 fputs("DEBUG: Received an interrupt before any bytes were "
c8fef167 247 "written, aborting.\n", stderr);
b94498cf 248 return (0);
249 }
8ca02f3c 250
251 sleep(1);
252 continue;
253 }
254 }
ed486911 255
f7deaa1a 256 /*
257 * Check if we have a side-channel request ready...
258 */
259
260 if (side_cb && FD_ISSET(CUPS_SC_FD, &input))
dd1abb6b
MS
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
18ecb428
MS
267 if ((*side_cb)(print_fd, device_fd, snmp_fd, addr, use_bc))
268 side_cb = NULL;
dd1abb6b
MS
269 continue;
270 }
f7deaa1a 271
ed486911 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,
4d301e69 281 "DEBUG: Received " CUPS_LLFMT " bytes of back-channel data\n",
ed486911 282 CUPS_LLCAST bc_bytes);
7e86f2f6 283 cupsBackChannelWrite(bc_buffer, (size_t)bc_bytes, 1.0);
ed486911 284 }
4b3f67ff
MS
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 }
7cf5915e
MS
291 else if (bc_bytes == 0)
292 use_bc = 0;
ed486911 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 {
f3c17241
MS
310 fprintf(stderr, "DEBUG: Read failed: %s\n", strerror(errno));
311 _cupsLangPrintFilter(stderr, "ERROR",
312 _("Unable to read print data."));
ed486911 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;
8ca02f3c 328
329 fprintf(stderr, "DEBUG: Read %d bytes of print data...\n",
330 (int)print_bytes);
ed486911 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 {
7e86f2f6 340 if ((bytes = write(device_fd, print_ptr, (size_t)print_bytes)) < 0)
ed486911 341 {
342 /*
343 * Write error - bail if we don't see an error we can retry...
344 */
345
346 if (errno == ENOSPC)
347 {
ef55b745 348 if (paperout != 1 && update_state)
ed486911 349 {
c5571a1d 350 fputs("STATE: +media-empty-warning\n", stderr);
0837b7e8 351 fputs("DEBUG: Out of paper\n", stderr);
ed486911 352 paperout = 1;
353 }
354 }
8ca02f3c 355 else if (errno == ENXIO)
356 {
ef55b745 357 if (offline != 1 && update_state)
8ca02f3c 358 {
c5571a1d 359 fputs("STATE: +offline-report\n", stderr);
0837b7e8 360 _cupsLangPrintFilter(stderr, "INFO",
f3c17241 361 _("The printer is not connected."));
8ca02f3c 362 offline = 1;
363 }
364 }
ed486911 365 else if (errno != EAGAIN && errno != EINTR && errno != ENOTTY)
366 {
0837b7e8 367 _cupsLangPrintError("ERROR", _("Unable to write print data"));
ed486911 368 return (-1);
369 }
370 }
371 else
372 {
ef55b745 373 if (paperout && update_state)
ed486911 374 {
c5571a1d 375 fputs("STATE: -media-empty-warning\n", stderr);
ed486911 376 paperout = 0;
377 }
378
ef55b745 379 if (offline && update_state)
8ca02f3c 380 {
c5571a1d 381 fputs("STATE: -offline-report\n", stderr);
f3c17241
MS
382 _cupsLangPrintFilter(stderr, "INFO",
383 _("The printer is now connected."));
8ca02f3c 384 offline = 0;
385 }
386
387 fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes);
ed486911 388
389 print_bytes -= bytes;
390 print_ptr += bytes;
391 total_bytes += bytes;
392 }
393 }
568fa3fa
MS
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 }
ed486911 406 }
407
408 /*
409 * Return with success...
410 */
411
412 return (total_bytes);
413}
414
415
416/*
c8fef167
MS
417 * 'backendWaitLoop()' - Wait for input from stdin while handling side-channel
418 * queries.
419 */
420
421int /* O - 1 if data is ready, 0 if not */
422backendWaitLoop(
423 int snmp_fd, /* I - SNMP socket or -1 if none */
424 http_addr_t *addr, /* I - Address of device */
f14324a7 425 int use_bc, /* I - Use back-channel? */
c8fef167
MS
426 _cups_sccb_t side_cb) /* I - Side-channel callback */
427{
82cc1f9a
MS
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() */
c8fef167
MS
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
82cc1f9a
MS
442 if (snmp_fd >= 0)
443 snmp_update = time(NULL) + 5;
444
c8fef167
MS
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
82cc1f9a
MS
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)
c8fef167
MS
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
f14324a7 502 if ((*side_cb)(0, -1, snmp_fd, addr, use_bc))
c8fef167
MS
503 side_cb = NULL;
504 continue;
505 }
506
507 /*
508 * Do SNMP updates periodically...
509 */
510
82cc1f9a 511 if (snmp_fd >= 0 && curtime >= snmp_update)
c8fef167
MS
512 {
513 if (backendSNMPSupplies(snmp_fd, addr, NULL, NULL))
82cc1f9a 514 snmp_fd = -1;
c8fef167
MS
515 else
516 snmp_update = curtime + 5;
517 }
518 }
519
520 /*
521 * Return with success...
522 */
523
524 return (1);
525}