2 * "$Id: runloop.c 6834 2007-08-22 18:29:25Z mike $"
4 * Common run loop APIs for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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 * 'backendNetworkSideCB()' - Handle common network side-channel commands.
144 backendNetworkSideCB(
145 int print_fd
, /* I - Print file or -1 */
146 int device_fd
, /* I - Device file or -1 */
147 int snmp_fd
, /* I - SNMP socket */
148 http_addr_t
*addr
, /* I - Address of device */
149 int use_bc
) /* I - Use back-channel data? */
151 cups_sc_command_t command
; /* Request command */
152 cups_sc_status_t status
; /* Request/response status */
153 char data
[2048]; /* Request/response data */
154 int datalen
; /* Request/response data size */
155 const char *device_id
; /* 1284DEVICEID env var */
158 datalen
= sizeof(data
);
160 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
162 _cupsLangPuts(stderr
, _("WARNING: Failed to read side-channel request!\n"));
168 case CUPS_SC_CMD_DRAIN_OUTPUT
:
170 * Our sockets disable the Nagle algorithm and data is sent immediately.
173 if (backendDrainOutput(print_fd
, device_fd
))
174 status
= CUPS_SC_STATUS_IO_ERROR
;
176 status
= CUPS_SC_STATUS_OK
;
181 case CUPS_SC_CMD_GET_BIDI
:
186 case CUPS_SC_CMD_GET_DEVICE_ID
:
187 if ((device_id
= getenv("1284DEVICEID")) != NULL
)
189 strlcpy(data
, device_id
, sizeof(data
));
190 datalen
= (int)strlen(data
);
195 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
200 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);
205 * 'backendRunLoop()' - Read and write print and back-channel data.
208 ssize_t
/* O - Total bytes on success, -1 on error */
210 int print_fd
, /* I - Print file descriptor */
211 int device_fd
, /* I - Device file descriptor */
212 int snmp_fd
, /* I - SNMP socket or -1 if none */
213 http_addr_t
*addr
, /* I - Address of device */
214 int use_bc
, /* I - Use back-channel? */
215 void (*side_cb
)(int, int, int, http_addr_t
*, int))
216 /* I - Side-channel callback */
218 int nfds
; /* Maximum file descriptor value + 1 */
219 fd_set input
, /* Input set for reading */
220 output
; /* Output set for writing */
221 ssize_t print_bytes
, /* Print bytes read */
222 bc_bytes
, /* Backchannel bytes read */
223 total_bytes
, /* Total bytes written */
224 bytes
; /* Bytes written */
225 int paperout
; /* "Paper out" status */
226 int offline
; /* "Off-line" status */
227 char print_buffer
[8192], /* Print data buffer */
228 *print_ptr
, /* Pointer into print data buffer */
229 bc_buffer
[1024]; /* Back-channel data buffer */
230 struct timeval timeout
; /* Timeout for select() */
231 time_t curtime
, /* Current time */
233 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
234 struct sigaction action
; /* Actions for POSIX signals */
235 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
239 "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, snmp_fd=%d, "
240 "addr=%p, use_bc=%d, side_cb=%p)\n",
241 print_fd
, device_fd
, snmp_fd
, addr
, use_bc
, side_cb
);
244 * If we are printing data from a print driver on stdin, ignore SIGTERM
245 * so that the driver can finish out any page data, e.g. to eject the
246 * current page. We only do this for stdin printing as otherwise there
247 * is no way to cancel a raw print job...
252 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
253 sigset(SIGTERM
, SIG_IGN
);
254 #elif defined(HAVE_SIGACTION)
255 memset(&action
, 0, sizeof(action
));
257 sigemptyset(&action
.sa_mask
);
258 action
.sa_handler
= SIG_IGN
;
259 sigaction(SIGTERM
, &action
, NULL
);
261 signal(SIGTERM
, SIG_IGN
);
262 #endif /* HAVE_SIGSET */
266 * Figure out the maximum file descriptor value to use with select()...
269 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
272 * Now loop until we are out of data from print_fd...
275 for (print_bytes
= 0, print_ptr
= print_buffer
, offline
= -1,
276 paperout
= -1, total_bytes
= 0;;)
279 * Use select() to determine whether we have data to copy around...
284 FD_SET(print_fd
, &input
);
286 FD_SET(device_fd
, &input
);
288 FD_SET(CUPS_SC_FD
, &input
);
291 if (print_bytes
|| (!use_bc
&& !side_cb
))
292 FD_SET(device_fd
, &output
);
294 if (use_bc
|| side_cb
)
299 if (select(nfds
, &input
, &output
, NULL
, &timeout
) < 0)
302 * Pause printing to clear any pending errors...
305 if (errno
== ENXIO
&& offline
!= 1)
307 fputs("STATE: +offline-error\n", stderr
);
308 _cupsLangPuts(stderr
, _("INFO: Printer is currently offline.\n"));
311 else if (errno
== EINTR
&& total_bytes
== 0)
313 fputs("DEBUG: Received an interrupt before any bytes were "
314 "written, aborting!\n", stderr
);
324 * Check if we have a side-channel request ready...
327 if (side_cb
&& FD_ISSET(CUPS_SC_FD
, &input
))
328 (*side_cb
)(print_fd
, device_fd
, snmp_fd
, addr
, use_bc
);
331 * Check if we have back-channel data ready...
334 if (FD_ISSET(device_fd
, &input
))
336 if ((bc_bytes
= read(device_fd
, bc_buffer
, sizeof(bc_buffer
))) > 0)
339 "DEBUG: Received " CUPS_LLFMT
" bytes of back-channel data!\n",
340 CUPS_LLCAST bc_bytes
);
341 cupsBackChannelWrite(bc_buffer
, bc_bytes
, 1.0);
346 * Check if we have print data ready...
349 if (FD_ISSET(print_fd
, &input
))
351 if ((print_bytes
= read(print_fd
, print_buffer
,
352 sizeof(print_buffer
))) < 0)
355 * Read error - bail if we don't see EAGAIN or EINTR...
358 if (errno
!= EAGAIN
|| errno
!= EINTR
)
360 perror("ERROR: Unable to read print data");
366 else if (print_bytes
== 0)
369 * End of file, break out of the loop...
375 print_ptr
= print_buffer
;
377 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
382 * Check if the device is ready to receive data and we have data to
386 if (print_bytes
&& FD_ISSET(device_fd
, &output
))
388 if ((bytes
= write(device_fd
, print_ptr
, print_bytes
)) < 0)
391 * Write error - bail if we don't see an error we can retry...
398 fputs("STATE: +media-empty-error\n", stderr
);
399 _cupsLangPuts(stderr
, _("ERROR: Out of paper!\n"));
403 else if (errno
== ENXIO
)
407 fputs("STATE: +offline-error\n", stderr
);
408 _cupsLangPuts(stderr
, _("INFO: Printer is currently off-line.\n"));
412 else if (errno
!= EAGAIN
&& errno
!= EINTR
&& errno
!= ENOTTY
)
414 fprintf(stderr
, _("ERROR: Unable to write print data: %s\n"),
423 fputs("STATE: -media-empty-error\n", stderr
);
429 fputs("STATE: -offline-error\n", stderr
);
430 _cupsLangPuts(stderr
, _("INFO: Printer is now online.\n"));
434 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
436 print_bytes
-= bytes
;
438 total_bytes
+= bytes
;
443 * Do SNMP updates periodically...
446 if (snmp_fd
>= 0 && time(&curtime
) >= snmp_update
)
448 if (backendSNMPSupplies(snmp_fd
, addr
, NULL
, NULL
))
449 snmp_update
= INT_MAX
;
451 snmp_update
= curtime
+ 5;
456 * Return with success...
459 return (total_bytes
);
464 * End of "$Id: runloop.c 6834 2007-08-22 18:29:25Z mike $".