]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/runloop.c
d225529dad2ed2f9ed1c48d9e84ccebd5788de85
2 * "$Id: runloop.c 7895 2008-09-02 19:19:43Z mike $"
4 * Common run loop APIs for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * backendDrainOutput() - Drain pending print data to the device.
20 * backendRunLoop() - Read and write print and back-channel data.
24 * Include necessary headers.
27 #include "backend-private.h"
30 # include <sys/time.h>
32 # include <sys/select.h>
37 * 'backendDrainOutput()' - Drain pending print data to the device.
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 */
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... */
53 fprintf(stderr
, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n",
57 * Figure out the maximum file descriptor value to use with select()...
60 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
63 * Now loop until we are out of data from print_fd...
69 * Use select() to determine whether we have data to copy around...
73 FD_SET(print_fd
, &input
);
78 if (select(nfds
, &input
, NULL
, NULL
, &timeout
) < 0)
81 if (!FD_ISSET(print_fd
, &input
))
84 if ((print_bytes
= read(print_fd
, print_buffer
,
85 sizeof(print_buffer
))) < 0)
88 * Read error - bail if we don't see EAGAIN or EINTR...
91 if (errno
!= EAGAIN
|| errno
!= EINTR
)
93 perror("ERROR: Unable to read print data");
99 else if (print_bytes
== 0)
102 * End of file, return...
108 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
111 for (print_ptr
= print_buffer
; print_bytes
> 0;)
113 if ((bytes
= write(device_fd
, print_ptr
, print_bytes
)) < 0)
116 * Write error - bail if we don't see an error we can retry...
119 if (errno
!= ENOSPC
&& errno
!= ENXIO
&& errno
!= EAGAIN
&&
120 errno
!= EINTR
&& errno
!= ENOTTY
)
122 _cupsLangPrintf(stderr
, _("ERROR: Unable to write print data: %s\n"),
129 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
131 print_bytes
-= bytes
;
140 * 'backendRunLoop()' - Read and write print and back-channel data.
143 ssize_t
/* O - Total bytes on success, -1 on error */
145 int print_fd
, /* I - Print file descriptor */
146 int device_fd
, /* I - Device file descriptor */
147 int snmp_fd
, /* I - SNMP socket or -1 if none */
148 http_addr_t
*addr
, /* I - Address of device */
149 int use_bc
, /* I - Use back-channel? */
150 int update_state
, /* I - Update printer-state-reasons? */
151 int (*side_cb
)(int, int, int, http_addr_t
*, int))
152 /* I - Side-channel callback */
154 int nfds
; /* Maximum file descriptor value + 1 */
155 fd_set input
, /* Input set for reading */
156 output
; /* Output set for writing */
157 ssize_t print_bytes
, /* Print bytes read */
158 bc_bytes
, /* Backchannel bytes read */
159 total_bytes
, /* Total bytes written */
160 bytes
; /* Bytes written */
161 int paperout
; /* "Paper out" status */
162 int offline
; /* "Off-line" status */
163 char print_buffer
[8192], /* Print data buffer */
164 *print_ptr
, /* Pointer into print data buffer */
165 bc_buffer
[1024]; /* Back-channel data buffer */
166 struct timeval timeout
; /* Timeout for select() */
167 time_t curtime
, /* Current time */
169 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
170 struct sigaction action
; /* Actions for POSIX signals */
171 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
175 "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, snmp_fd=%d, "
176 "addr=%p, use_bc=%d, side_cb=%p)\n",
177 print_fd
, device_fd
, snmp_fd
, addr
, use_bc
, side_cb
);
180 * If we are printing data from a print driver on stdin, ignore SIGTERM
181 * so that the driver can finish out any page data, e.g. to eject the
182 * current page. We only do this for stdin printing as otherwise there
183 * is no way to cancel a raw print job...
188 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
189 sigset(SIGTERM
, SIG_IGN
);
190 #elif defined(HAVE_SIGACTION)
191 memset(&action
, 0, sizeof(action
));
193 sigemptyset(&action
.sa_mask
);
194 action
.sa_handler
= SIG_IGN
;
195 sigaction(SIGTERM
, &action
, NULL
);
197 signal(SIGTERM
, SIG_IGN
);
198 #endif /* HAVE_SIGSET */
200 else if (print_fd
< 0)
203 * Copy print data from stdin, but don't mess with the signal handlers...
210 * Figure out the maximum file descriptor value to use with select()...
213 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
216 * Now loop until we are out of data from print_fd...
219 for (print_bytes
= 0, print_ptr
= print_buffer
, offline
= -1,
220 paperout
= -1, total_bytes
= 0;;)
223 * Use select() to determine whether we have data to copy around...
228 FD_SET(print_fd
, &input
);
230 FD_SET(device_fd
, &input
);
231 if (!print_bytes
&& side_cb
)
232 FD_SET(CUPS_SC_FD
, &input
);
235 if (print_bytes
|| (!use_bc
&& !side_cb
))
236 FD_SET(device_fd
, &output
);
238 if (use_bc
|| side_cb
)
243 if (select(nfds
, &input
, &output
, NULL
, &timeout
) < 0)
246 * Pause printing to clear any pending errors...
249 if (errno
== ENXIO
&& offline
!= 1 && update_state
)
251 fputs("STATE: +offline-report\n", stderr
);
252 _cupsLangPuts(stderr
, _("INFO: Printer is currently offline.\n"));
255 else if (errno
== EINTR
&& total_bytes
== 0)
257 fputs("DEBUG: Received an interrupt before any bytes were "
258 "written, aborting\n", stderr
);
268 * Check if we have a side-channel request ready...
271 if (side_cb
&& FD_ISSET(CUPS_SC_FD
, &input
))
274 * Do the side-channel request, then start back over in the select
275 * loop since it may have read from print_fd...
278 if ((*side_cb
)(print_fd
, device_fd
, snmp_fd
, addr
, use_bc
))
284 * Check if we have back-channel data ready...
287 if (FD_ISSET(device_fd
, &input
))
289 if ((bc_bytes
= read(device_fd
, bc_buffer
, sizeof(bc_buffer
))) > 0)
292 "DEBUG: Received " CUPS_LLFMT
" bytes of back-channel data\n",
293 CUPS_LLCAST bc_bytes
);
294 cupsBackChannelWrite(bc_buffer
, bc_bytes
, 1.0);
296 else if (bc_bytes
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
)
298 fprintf(stderr
, "DEBUG: Error reading back-channel data: %s\n",
302 else if (bc_bytes
== 0)
307 * Check if we have print data ready...
310 if (FD_ISSET(print_fd
, &input
))
312 if ((print_bytes
= read(print_fd
, print_buffer
,
313 sizeof(print_buffer
))) < 0)
316 * Read error - bail if we don't see EAGAIN or EINTR...
319 if (errno
!= EAGAIN
|| errno
!= EINTR
)
321 perror("ERROR: Unable to read print data");
327 else if (print_bytes
== 0)
330 * End of file, break out of the loop...
336 print_ptr
= print_buffer
;
338 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
343 * Check if the device is ready to receive data and we have data to
347 if (print_bytes
&& FD_ISSET(device_fd
, &output
))
349 if ((bytes
= write(device_fd
, print_ptr
, print_bytes
)) < 0)
352 * Write error - bail if we don't see an error we can retry...
357 if (paperout
!= 1 && update_state
)
359 fputs("STATE: +media-empty-warning\n", stderr
);
360 _cupsLangPuts(stderr
, _("DEBUG: Out of paper\n"));
364 else if (errno
== ENXIO
)
366 if (offline
!= 1 && update_state
)
368 fputs("STATE: +offline-report\n", stderr
);
369 _cupsLangPuts(stderr
, _("INFO: Printer is currently off-line.\n"));
373 else if (errno
!= EAGAIN
&& errno
!= EINTR
&& errno
!= ENOTTY
)
375 fprintf(stderr
, _("ERROR: Unable to write print data: %s\n"),
382 if (paperout
&& update_state
)
384 fputs("STATE: -media-empty-warning\n", stderr
);
388 if (offline
&& update_state
)
390 fputs("STATE: -offline-report\n", stderr
);
391 _cupsLangPuts(stderr
, _("INFO: Printer is now online.\n"));
395 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
397 print_bytes
-= bytes
;
399 total_bytes
+= bytes
;
404 * Do SNMP updates periodically...
407 if (snmp_fd
>= 0 && time(&curtime
) >= snmp_update
)
409 if (backendSNMPSupplies(snmp_fd
, addr
, NULL
, NULL
))
410 snmp_update
= INT_MAX
;
412 snmp_update
= curtime
+ 5;
417 * Return with success...
420 return (total_bytes
);
425 * End of "$Id: runloop.c 7895 2008-09-02 19:19:43Z mike $".