]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/runloop.c
Load cups into easysw/current.
[thirdparty/cups.git] / backend / runloop.c
CommitLineData
ed486911 1/*
f7deaa1a 2 * "$Id: runloop.c 6170 2007-01-02 17:26:41Z mike $"
ed486911 3 *
4 * Common run loop API for the Common UNIX Printing System (CUPS).
5 *
f7deaa1a 6 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
ed486911 7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * backendRunLoop() - Read and write print and back-channel data.
29 */
30
31/*
32 * Include necessary headers.
33 */
34
35#include "backend-private.h"
d09495fa 36#ifdef __hpux
37# include <sys/time.h>
38#else
39# include <sys/select.h>
40#endif /* __hpux */
ed486911 41
42
43/*
44 * 'backendRunLoop()' - Read and write print and back-channel data.
45 */
46
47ssize_t /* O - Total bytes on success, -1 on error */
f7deaa1a 48backendRunLoop(
49 int print_fd, /* I - Print file descriptor */
50 int device_fd, /* I - Device file descriptor */
51 int use_bc, /* I - Use back-channel? */
52 void (*side_cb)(int, int, int)) /* I - Side-channel callback */
ed486911 53{
54 int nfds; /* Maximum file descriptor value + 1 */
55 fd_set input, /* Input set for reading */
56 output; /* Output set for writing */
57 ssize_t print_bytes, /* Print bytes read */
58 bc_bytes, /* Backchannel bytes read */
59 total_bytes, /* Total bytes written */
60 bytes; /* Bytes written */
61 int paperout; /* "Paper out" status */
8ca02f3c 62 int offline; /* "Off-line" status */
ed486911 63 char print_buffer[8192], /* Print data buffer */
64 *print_ptr, /* Pointer into print data buffer */
65 bc_buffer[1024]; /* Back-channel data buffer */
66#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
67 struct sigaction action; /* Actions for POSIX signals */
68#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
69
70
8ca02f3c 71 fprintf(stderr, "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, use_bc=%d)\n",
72 print_fd, device_fd, use_bc);
73
ed486911 74 /*
75 * If we are printing data from a print driver on stdin, ignore SIGTERM
76 * so that the driver can finish out any page data, e.g. to eject the
77 * current page. We only do this for stdin printing as otherwise there
78 * is no way to cancel a raw print job...
79 */
80
81 if (!print_fd)
82 {
83#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
84 sigset(SIGTERM, SIG_IGN);
85#elif defined(HAVE_SIGACTION)
86 memset(&action, 0, sizeof(action));
87
88 sigemptyset(&action.sa_mask);
89 action.sa_handler = SIG_IGN;
90 sigaction(SIGTERM, &action, NULL);
91#else
92 signal(SIGTERM, SIG_IGN);
93#endif /* HAVE_SIGSET */
94 }
95
96 /*
97 * Figure out the maximum file descriptor value to use with select()...
98 */
99
100 nfds = (print_fd > device_fd ? print_fd : device_fd) + 1;
101
102 /*
103 * Now loop until we are out of data from print_fd...
104 */
105
8ca02f3c 106 for (print_bytes = 0, print_ptr = print_buffer, offline = 0, paperout = 0, total_bytes = 0;;)
ed486911 107 {
108 /*
109 * Use select() to determine whether we have data to copy around...
110 */
111
112 FD_ZERO(&input);
113 if (!print_bytes)
114 FD_SET(print_fd, &input);
115 if (use_bc)
116 FD_SET(device_fd, &input);
f7deaa1a 117 if (side_cb)
118 FD_SET(CUPS_SC_FD, &input);
ed486911 119
120 FD_ZERO(&output);
8ca02f3c 121 if (print_bytes || !use_bc)
ed486911 122 FD_SET(device_fd, &output);
123
f7deaa1a 124 if (use_bc || side_cb)
8ca02f3c 125 {
126 if (select(nfds, &input, &output, NULL, NULL) < 0)
127 {
128 /*
129 * Pause printing to clear any pending errors...
130 */
131
132 if (errno == ENXIO && !offline)
133 {
134 fputs("STATE: +offline-error\n", stderr);
135 fputs("INFO: Printer is currently off-line.\n", stderr);
136 offline = 1;
137 }
138
139 sleep(1);
140 continue;
141 }
142 }
ed486911 143
f7deaa1a 144 /*
145 * Check if we have a side-channel request ready...
146 */
147
148 if (side_cb && FD_ISSET(CUPS_SC_FD, &input))
149 (*side_cb)(print_fd, device_fd, use_bc);
150
ed486911 151 /*
152 * Check if we have back-channel data ready...
153 */
154
155 if (FD_ISSET(device_fd, &input))
156 {
157 if ((bc_bytes = read(device_fd, bc_buffer, sizeof(bc_buffer))) > 0)
158 {
159 fprintf(stderr,
160 "DEBUG: Received " CUPS_LLFMT " bytes of back-channel data!\n",
161 CUPS_LLCAST bc_bytes);
162 cupsBackChannelWrite(bc_buffer, bc_bytes, 1.0);
163 }
164 }
165
166 /*
167 * Check if we have print data ready...
168 */
169
170 if (FD_ISSET(print_fd, &input))
171 {
172 if ((print_bytes = read(print_fd, print_buffer,
173 sizeof(print_buffer))) < 0)
174 {
175 /*
176 * Read error - bail if we don't see EAGAIN or EINTR...
177 */
178
179 if (errno != EAGAIN || errno != EINTR)
180 {
181 perror("ERROR: Unable to read print data");
182 return (-1);
183 }
184
185 print_bytes = 0;
186 }
187 else if (print_bytes == 0)
188 {
189 /*
190 * End of file, break out of the loop...
191 */
192
193 break;
194 }
195
196 print_ptr = print_buffer;
8ca02f3c 197
198 fprintf(stderr, "DEBUG: Read %d bytes of print data...\n",
199 (int)print_bytes);
ed486911 200 }
201
202 /*
203 * Check if the device is ready to receive data and we have data to
204 * send...
205 */
206
207 if (print_bytes && FD_ISSET(device_fd, &output))
208 {
209 if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0)
210 {
211 /*
212 * Write error - bail if we don't see an error we can retry...
213 */
214
215 if (errno == ENOSPC)
216 {
217 if (!paperout)
218 {
219 fputs("ERROR: Out of paper!\n", stderr);
b86bc4cf 220 fputs("STATE: +media-empty-error\n", stderr);
ed486911 221 paperout = 1;
222 }
223 }
8ca02f3c 224 else if (errno == ENXIO)
225 {
226 if (!offline)
227 {
228 fputs("STATE: +offline-error\n", stderr);
229 fputs("INFO: Printer is currently off-line.\n", stderr);
230 offline = 1;
231 }
232 }
ed486911 233 else if (errno != EAGAIN && errno != EINTR && errno != ENOTTY)
234 {
235 perror("ERROR: Unable to write print data");
236 return (-1);
237 }
238 }
239 else
240 {
241 if (paperout)
242 {
b86bc4cf 243 fputs("STATE: -media-empty-error\n", stderr);
ed486911 244 paperout = 0;
245 }
246
8ca02f3c 247 if (offline)
248 {
249 fputs("STATE: -offline-error\n", stderr);
250 fputs("INFO: Printer is now on-line.\n", stderr);
251 offline = 0;
252 }
253
254 fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes);
ed486911 255
256 print_bytes -= bytes;
257 print_ptr += bytes;
258 total_bytes += bytes;
259 }
260 }
261 }
262
263 /*
264 * Return with success...
265 */
266
267 return (total_bytes);
268}
269
270
271/*
f7deaa1a 272 * End of "$Id: runloop.c 6170 2007-01-02 17:26:41Z mike $".
ed486911 273 */