]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/runloop.c
2 * Common run loop APIs for CUPS backends.
4 * Copyright © 2007-2014 by Apple Inc.
5 * Copyright © 2006-2007 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers.
15 #include "backend-private.h"
17 #include <sys/select.h>
21 * 'backendDrainOutput()' - Drain pending print data to the device.
24 int /* O - 0 on success, -1 on error */
25 backendDrainOutput(int print_fd
, /* I - Print file descriptor */
26 int device_fd
) /* I - Device file descriptor */
28 int nfds
; /* Maximum file descriptor value + 1 */
29 fd_set input
; /* Input set for reading */
30 ssize_t print_bytes
, /* Print bytes read */
31 bytes
; /* Bytes written */
32 char print_buffer
[8192], /* Print data buffer */
33 *print_ptr
; /* Pointer into print data buffer */
34 struct timeval timeout
; /* Timeout for read... */
37 fprintf(stderr
, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n",
41 * Figure out the maximum file descriptor value to use with select()...
44 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
47 * Now loop until we are out of data from print_fd...
53 * Use select() to determine whether we have data to copy around...
57 FD_SET(print_fd
, &input
);
62 if (select(nfds
, &input
, NULL
, NULL
, &timeout
) < 0)
65 if (!FD_ISSET(print_fd
, &input
))
68 if ((print_bytes
= read(print_fd
, print_buffer
,
69 sizeof(print_buffer
))) < 0)
72 * Read error - bail if we don't see EAGAIN or EINTR...
75 if (errno
!= EAGAIN
&& errno
!= EINTR
)
77 fprintf(stderr
, "DEBUG: Read failed: %s\n", strerror(errno
));
78 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to read print data."));
84 else if (print_bytes
== 0)
87 * End of file, return...
93 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
96 for (print_ptr
= print_buffer
; print_bytes
> 0;)
98 if ((bytes
= write(device_fd
, print_ptr
, (size_t)print_bytes
)) < 0)
101 * Write error - bail if we don't see an error we can retry...
104 if (errno
!= ENOSPC
&& errno
!= ENXIO
&& errno
!= EAGAIN
&&
105 errno
!= EINTR
&& errno
!= ENOTTY
)
107 _cupsLangPrintError("ERROR", _("Unable to write print data"));
113 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
115 print_bytes
-= bytes
;
124 * 'backendRunLoop()' - Read and write print and back-channel data.
127 ssize_t
/* O - Total bytes on success, -1 on error */
129 int print_fd
, /* I - Print file descriptor */
130 int device_fd
, /* I - Device file descriptor */
131 int snmp_fd
, /* I - SNMP socket or -1 if none */
132 http_addr_t
*addr
, /* I - Address of device */
133 int use_bc
, /* I - Use back-channel? */
134 int update_state
, /* I - Update printer-state-reasons? */
135 _cups_sccb_t side_cb
) /* I - Side-channel callback */
137 int nfds
; /* Maximum file descriptor value + 1 */
138 fd_set input
, /* Input set for reading */
139 output
; /* Output set for writing */
140 ssize_t print_bytes
, /* Print bytes read */
141 bc_bytes
, /* Backchannel bytes read */
142 total_bytes
, /* Total bytes written */
143 bytes
; /* Bytes written */
144 int paperout
; /* "Paper out" status */
145 int offline
; /* "Off-line" status */
146 char print_buffer
[8192], /* Print data buffer */
147 *print_ptr
, /* Pointer into print data buffer */
148 bc_buffer
[1024]; /* Back-channel data buffer */
149 struct timeval timeout
; /* Timeout for select() */
150 time_t curtime
, /* Current time */
152 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
153 struct sigaction action
; /* Actions for POSIX signals */
154 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
158 "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, snmp_fd=%d, "
159 "addr=%p, use_bc=%d, side_cb=%p)\n",
160 print_fd
, device_fd
, snmp_fd
, addr
, use_bc
, side_cb
);
163 * If we are printing data from a print driver on stdin, ignore SIGTERM
164 * so that the driver can finish out any page data, e.g. to eject the
165 * current page. We only do this for stdin printing as otherwise there
166 * is no way to cancel a raw print job...
171 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
172 sigset(SIGTERM
, SIG_IGN
);
173 #elif defined(HAVE_SIGACTION)
174 memset(&action
, 0, sizeof(action
));
176 sigemptyset(&action
.sa_mask
);
177 action
.sa_handler
= SIG_IGN
;
178 sigaction(SIGTERM
, &action
, NULL
);
180 signal(SIGTERM
, SIG_IGN
);
181 #endif /* HAVE_SIGSET */
183 else if (print_fd
< 0)
186 * Copy print data from stdin, but don't mess with the signal handlers...
193 * Figure out the maximum file descriptor value to use with select()...
196 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
199 * Now loop until we are out of data from print_fd...
202 for (print_bytes
= 0, print_ptr
= print_buffer
, offline
= -1,
203 paperout
= -1, total_bytes
= 0;;)
206 * Use select() to determine whether we have data to copy around...
211 FD_SET(print_fd
, &input
);
213 FD_SET(device_fd
, &input
);
214 if (!print_bytes
&& side_cb
)
215 FD_SET(CUPS_SC_FD
, &input
);
218 if (print_bytes
|| (!use_bc
&& !side_cb
))
219 FD_SET(device_fd
, &output
);
221 if (use_bc
|| side_cb
)
226 if (select(nfds
, &input
, &output
, NULL
, &timeout
) < 0)
229 * Pause printing to clear any pending errors...
232 if (errno
== ENXIO
&& offline
!= 1 && update_state
)
234 fputs("STATE: +offline-report\n", stderr
);
235 _cupsLangPrintFilter(stderr
, "INFO",
236 _("The printer is not connected."));
239 else if (errno
== EINTR
&& total_bytes
== 0)
241 fputs("DEBUG: Received an interrupt before any bytes were "
242 "written, aborting.\n", stderr
);
252 * Check if we have a side-channel request ready...
255 if (side_cb
&& FD_ISSET(CUPS_SC_FD
, &input
))
258 * Do the side-channel request, then start back over in the select
259 * loop since it may have read from print_fd...
262 if ((*side_cb
)(print_fd
, device_fd
, snmp_fd
, addr
, use_bc
))
268 * Check if we have back-channel data ready...
271 if (FD_ISSET(device_fd
, &input
))
273 if ((bc_bytes
= read(device_fd
, bc_buffer
, sizeof(bc_buffer
))) > 0)
276 "DEBUG: Received " CUPS_LLFMT
" bytes of back-channel data\n",
277 CUPS_LLCAST bc_bytes
);
278 cupsBackChannelWrite(bc_buffer
, (size_t)bc_bytes
, 1.0);
280 else if (bc_bytes
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
)
282 fprintf(stderr
, "DEBUG: Error reading back-channel data: %s\n",
286 else if (bc_bytes
== 0)
291 * Check if we have print data ready...
294 if (FD_ISSET(print_fd
, &input
))
296 if ((print_bytes
= read(print_fd
, print_buffer
,
297 sizeof(print_buffer
))) < 0)
300 * Read error - bail if we don't see EAGAIN or EINTR...
303 if (errno
!= EAGAIN
&& errno
!= EINTR
)
305 fprintf(stderr
, "DEBUG: Read failed: %s\n", strerror(errno
));
306 _cupsLangPrintFilter(stderr
, "ERROR",
307 _("Unable to read print data."));
313 else if (print_bytes
== 0)
316 * End of file, break out of the loop...
322 print_ptr
= print_buffer
;
324 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
329 * Check if the device is ready to receive data and we have data to
333 if (print_bytes
&& FD_ISSET(device_fd
, &output
))
335 if ((bytes
= write(device_fd
, print_ptr
, (size_t)print_bytes
)) < 0)
338 * Write error - bail if we don't see an error we can retry...
343 if (paperout
!= 1 && update_state
)
345 fputs("STATE: +media-empty-warning\n", stderr
);
346 fputs("DEBUG: Out of paper\n", stderr
);
350 else if (errno
== ENXIO
)
352 if (offline
!= 1 && update_state
)
354 fputs("STATE: +offline-report\n", stderr
);
355 _cupsLangPrintFilter(stderr
, "INFO",
356 _("The printer is not connected."));
360 else if (errno
!= EAGAIN
&& errno
!= EINTR
&& errno
!= ENOTTY
)
362 _cupsLangPrintError("ERROR", _("Unable to write print data"));
368 if (paperout
&& update_state
)
370 fputs("STATE: -media-empty-warning\n", stderr
);
374 if (offline
&& update_state
)
376 fputs("STATE: -offline-report\n", stderr
);
377 _cupsLangPrintFilter(stderr
, "INFO",
378 _("The printer is now connected."));
382 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
384 print_bytes
-= bytes
;
386 total_bytes
+= bytes
;
391 * Do SNMP updates periodically...
394 if (snmp_fd
>= 0 && time(&curtime
) >= snmp_update
)
396 if (backendSNMPSupplies(snmp_fd
, addr
, NULL
, NULL
))
397 snmp_update
= INT_MAX
;
399 snmp_update
= curtime
+ 5;
404 * Return with success...
407 return (total_bytes
);
412 * 'backendWaitLoop()' - Wait for input from stdin while handling side-channel
416 int /* O - 1 if data is ready, 0 if not */
418 int snmp_fd
, /* I - SNMP socket or -1 if none */
419 http_addr_t
*addr
, /* I - Address of device */
420 int use_bc
, /* I - Use back-channel? */
421 _cups_sccb_t side_cb
) /* I - Side-channel callback */
423 int nfds
; /* Number of file descriptors */
424 fd_set input
; /* Input set for reading */
425 time_t curtime
= 0, /* Current time */
426 snmp_update
= 0;/* Last SNMP status update */
427 struct timeval timeout
; /* Timeout for select() */
430 fprintf(stderr
, "DEBUG: backendWaitLoop(snmp_fd=%d, addr=%p, side_cb=%p)\n",
431 snmp_fd
, addr
, side_cb
);
434 * Now loop until we receive data from stdin...
438 snmp_update
= time(NULL
) + 5;
443 * Use select() to determine whether we have data to copy around...
449 FD_SET(CUPS_SC_FD
, &input
);
453 curtime
= time(NULL
);
454 timeout
.tv_sec
= curtime
>= snmp_update
? 0 : snmp_update
- curtime
;
457 nfds
= select(CUPS_SC_FD
+ 1, &input
, NULL
, NULL
, &timeout
);
460 nfds
= select(CUPS_SC_FD
+ 1, &input
, NULL
, NULL
, NULL
);
465 * Pause printing to clear any pending errors...
470 fputs("DEBUG: Received an interrupt before any bytes were "
471 "written, aborting.\n", stderr
);
480 * Check for input on stdin...
483 if (FD_ISSET(0, &input
))
487 * Check if we have a side-channel request ready...
490 if (side_cb
&& FD_ISSET(CUPS_SC_FD
, &input
))
493 * Do the side-channel request, then start back over in the select
494 * loop since it may have read from print_fd...
497 if ((*side_cb
)(0, -1, snmp_fd
, addr
, use_bc
))
503 * Do SNMP updates periodically...
506 if (snmp_fd
>= 0 && curtime
>= snmp_update
)
508 if (backendSNMPSupplies(snmp_fd
, addr
, NULL
, NULL
))
511 snmp_update
= curtime
+ 5;
516 * Return with success...