]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/runloop.c
281736197386abdd0e64d3a6940a4f1dd9e92794
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 * 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/".
15 * Include necessary headers.
18 #include "backend-private.h"
20 #include <sys/select.h>
24 * 'backendDrainOutput()' - Drain pending print data to the device.
27 int /* O - 0 on success, -1 on error */
28 backendDrainOutput(int print_fd
, /* I - Print file descriptor */
29 int device_fd
) /* I - Device file descriptor */
31 int nfds
; /* Maximum file descriptor value + 1 */
32 fd_set input
; /* Input set for reading */
33 ssize_t print_bytes
, /* Print bytes read */
34 bytes
; /* Bytes written */
35 char print_buffer
[8192], /* Print data buffer */
36 *print_ptr
; /* Pointer into print data buffer */
37 struct timeval timeout
; /* Timeout for read... */
40 fprintf(stderr
, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n",
44 * Figure out the maximum file descriptor value to use with select()...
47 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
50 * Now loop until we are out of data from print_fd...
56 * Use select() to determine whether we have data to copy around...
60 FD_SET(print_fd
, &input
);
65 if (select(nfds
, &input
, NULL
, NULL
, &timeout
) < 0)
68 if (!FD_ISSET(print_fd
, &input
))
71 if ((print_bytes
= read(print_fd
, print_buffer
,
72 sizeof(print_buffer
))) < 0)
75 * Read error - bail if we don't see EAGAIN or EINTR...
78 if (errno
!= EAGAIN
&& errno
!= EINTR
)
80 fprintf(stderr
, "DEBUG: Read failed: %s\n", strerror(errno
));
81 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to read print data."));
87 else if (print_bytes
== 0)
90 * End of file, return...
96 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
99 for (print_ptr
= print_buffer
; print_bytes
> 0;)
101 if ((bytes
= write(device_fd
, print_ptr
, (size_t)print_bytes
)) < 0)
104 * Write error - bail if we don't see an error we can retry...
107 if (errno
!= ENOSPC
&& errno
!= ENXIO
&& errno
!= EAGAIN
&&
108 errno
!= EINTR
&& errno
!= ENOTTY
)
110 _cupsLangPrintError("ERROR", _("Unable to write print data"));
116 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
118 print_bytes
-= bytes
;
127 * 'backendRunLoop()' - Read and write print and back-channel data.
130 ssize_t
/* O - Total bytes on success, -1 on error */
132 int print_fd
, /* I - Print file descriptor */
133 int device_fd
, /* I - Device file descriptor */
134 int snmp_fd
, /* I - SNMP socket or -1 if none */
135 http_addr_t
*addr
, /* I - Address of device */
136 int use_bc
, /* I - Use back-channel? */
137 int update_state
, /* I - Update printer-state-reasons? */
138 _cups_sccb_t side_cb
) /* I - Side-channel callback */
140 int nfds
; /* Maximum file descriptor value + 1 */
141 fd_set input
, /* Input set for reading */
142 output
; /* Output set for writing */
143 ssize_t print_bytes
, /* Print bytes read */
144 bc_bytes
, /* Backchannel bytes read */
145 total_bytes
, /* Total bytes written */
146 bytes
; /* Bytes written */
147 int paperout
; /* "Paper out" status */
148 int offline
; /* "Off-line" status */
149 char print_buffer
[8192], /* Print data buffer */
150 *print_ptr
, /* Pointer into print data buffer */
151 bc_buffer
[1024]; /* Back-channel data buffer */
152 struct timeval timeout
; /* Timeout for select() */
153 time_t curtime
, /* Current time */
155 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
156 struct sigaction action
; /* Actions for POSIX signals */
157 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
161 "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, snmp_fd=%d, "
162 "addr=%p, use_bc=%d, side_cb=%p)\n",
163 print_fd
, device_fd
, snmp_fd
, addr
, use_bc
, side_cb
);
166 * If we are printing data from a print driver on stdin, ignore SIGTERM
167 * so that the driver can finish out any page data, e.g. to eject the
168 * current page. We only do this for stdin printing as otherwise there
169 * is no way to cancel a raw print job...
174 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
175 sigset(SIGTERM
, SIG_IGN
);
176 #elif defined(HAVE_SIGACTION)
177 memset(&action
, 0, sizeof(action
));
179 sigemptyset(&action
.sa_mask
);
180 action
.sa_handler
= SIG_IGN
;
181 sigaction(SIGTERM
, &action
, NULL
);
183 signal(SIGTERM
, SIG_IGN
);
184 #endif /* HAVE_SIGSET */
186 else if (print_fd
< 0)
189 * Copy print data from stdin, but don't mess with the signal handlers...
196 * Figure out the maximum file descriptor value to use with select()...
199 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
202 * Now loop until we are out of data from print_fd...
205 for (print_bytes
= 0, print_ptr
= print_buffer
, offline
= -1,
206 paperout
= -1, total_bytes
= 0;;)
209 * Use select() to determine whether we have data to copy around...
214 FD_SET(print_fd
, &input
);
216 FD_SET(device_fd
, &input
);
217 if (!print_bytes
&& side_cb
)
218 FD_SET(CUPS_SC_FD
, &input
);
221 if (print_bytes
|| (!use_bc
&& !side_cb
))
222 FD_SET(device_fd
, &output
);
224 if (use_bc
|| side_cb
)
229 if (select(nfds
, &input
, &output
, NULL
, &timeout
) < 0)
232 * Pause printing to clear any pending errors...
235 if (errno
== ENXIO
&& offline
!= 1 && update_state
)
237 fputs("STATE: +offline-report\n", stderr
);
238 _cupsLangPrintFilter(stderr
, "INFO",
239 _("The printer is not connected."));
242 else if (errno
== EINTR
&& total_bytes
== 0)
244 fputs("DEBUG: Received an interrupt before any bytes were "
245 "written, aborting.\n", stderr
);
255 * Check if we have a side-channel request ready...
258 if (side_cb
&& FD_ISSET(CUPS_SC_FD
, &input
))
261 * Do the side-channel request, then start back over in the select
262 * loop since it may have read from print_fd...
265 if ((*side_cb
)(print_fd
, device_fd
, snmp_fd
, addr
, use_bc
))
271 * Check if we have back-channel data ready...
274 if (FD_ISSET(device_fd
, &input
))
276 if ((bc_bytes
= read(device_fd
, bc_buffer
, sizeof(bc_buffer
))) > 0)
279 "DEBUG: Received " CUPS_LLFMT
" bytes of back-channel data\n",
280 CUPS_LLCAST bc_bytes
);
281 cupsBackChannelWrite(bc_buffer
, (size_t)bc_bytes
, 1.0);
283 else if (bc_bytes
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
)
285 fprintf(stderr
, "DEBUG: Error reading back-channel data: %s\n",
289 else if (bc_bytes
== 0)
294 * Check if we have print data ready...
297 if (FD_ISSET(print_fd
, &input
))
299 if ((print_bytes
= read(print_fd
, print_buffer
,
300 sizeof(print_buffer
))) < 0)
303 * Read error - bail if we don't see EAGAIN or EINTR...
306 if (errno
!= EAGAIN
&& errno
!= EINTR
)
308 fprintf(stderr
, "DEBUG: Read failed: %s\n", strerror(errno
));
309 _cupsLangPrintFilter(stderr
, "ERROR",
310 _("Unable to read print data."));
316 else if (print_bytes
== 0)
319 * End of file, break out of the loop...
325 print_ptr
= print_buffer
;
327 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
332 * Check if the device is ready to receive data and we have data to
336 if (print_bytes
&& FD_ISSET(device_fd
, &output
))
338 if ((bytes
= write(device_fd
, print_ptr
, (size_t)print_bytes
)) < 0)
341 * Write error - bail if we don't see an error we can retry...
346 if (paperout
!= 1 && update_state
)
348 fputs("STATE: +media-empty-warning\n", stderr
);
349 fputs("DEBUG: Out of paper\n", stderr
);
353 else if (errno
== ENXIO
)
355 if (offline
!= 1 && update_state
)
357 fputs("STATE: +offline-report\n", stderr
);
358 _cupsLangPrintFilter(stderr
, "INFO",
359 _("The printer is not connected."));
363 else if (errno
!= EAGAIN
&& errno
!= EINTR
&& errno
!= ENOTTY
)
365 _cupsLangPrintError("ERROR", _("Unable to write print data"));
371 if (paperout
&& update_state
)
373 fputs("STATE: -media-empty-warning\n", stderr
);
377 if (offline
&& update_state
)
379 fputs("STATE: -offline-report\n", stderr
);
380 _cupsLangPrintFilter(stderr
, "INFO",
381 _("The printer is now connected."));
385 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
387 print_bytes
-= bytes
;
389 total_bytes
+= bytes
;
394 * Do SNMP updates periodically...
397 if (snmp_fd
>= 0 && time(&curtime
) >= snmp_update
)
399 if (backendSNMPSupplies(snmp_fd
, addr
, NULL
, NULL
))
400 snmp_update
= INT_MAX
;
402 snmp_update
= curtime
+ 5;
407 * Return with success...
410 return (total_bytes
);
415 * 'backendWaitLoop()' - Wait for input from stdin while handling side-channel
419 int /* O - 1 if data is ready, 0 if not */
421 int snmp_fd
, /* I - SNMP socket or -1 if none */
422 http_addr_t
*addr
, /* I - Address of device */
423 int use_bc
, /* I - Use back-channel? */
424 _cups_sccb_t side_cb
) /* I - Side-channel callback */
426 int nfds
; /* Number of file descriptors */
427 fd_set input
; /* Input set for reading */
428 time_t curtime
= 0, /* Current time */
429 snmp_update
= 0;/* Last SNMP status update */
430 struct timeval timeout
; /* Timeout for select() */
433 fprintf(stderr
, "DEBUG: backendWaitLoop(snmp_fd=%d, addr=%p, side_cb=%p)\n",
434 snmp_fd
, addr
, side_cb
);
437 * Now loop until we receive data from stdin...
441 snmp_update
= time(NULL
) + 5;
446 * Use select() to determine whether we have data to copy around...
452 FD_SET(CUPS_SC_FD
, &input
);
456 curtime
= time(NULL
);
457 timeout
.tv_sec
= curtime
>= snmp_update
? 0 : snmp_update
- curtime
;
460 nfds
= select(CUPS_SC_FD
+ 1, &input
, NULL
, NULL
, &timeout
);
463 nfds
= select(CUPS_SC_FD
+ 1, &input
, NULL
, NULL
, NULL
);
468 * Pause printing to clear any pending errors...
473 fputs("DEBUG: Received an interrupt before any bytes were "
474 "written, aborting.\n", stderr
);
483 * Check for input on stdin...
486 if (FD_ISSET(0, &input
))
490 * Check if we have a side-channel request ready...
493 if (side_cb
&& FD_ISSET(CUPS_SC_FD
, &input
))
496 * Do the side-channel request, then start back over in the select
497 * loop since it may have read from print_fd...
500 if ((*side_cb
)(0, -1, snmp_fd
, addr
, use_bc
))
506 * Do SNMP updates periodically...
509 if (snmp_fd
>= 0 && curtime
>= snmp_update
)
511 if (backendSNMPSupplies(snmp_fd
, addr
, NULL
, NULL
))
514 snmp_update
= curtime
+ 5;
519 * Return with success...