]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/runloop.c
Merge changes from CUPS 1.4svn-r7961.
[thirdparty/cups.git] / backend / runloop.c
1 /*
2 * "$Id: runloop.c 7895 2008-09-02 19:19:43Z mike $"
3 *
4 * Common run loop APIs for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
8 *
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/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * backendDrainOutput() - Drain pending print data to the device.
20 * backendRunLoop() - Read and write print and back-channel data.
21 */
22
23 /*
24 * Include necessary headers.
25 */
26
27 #include "backend-private.h"
28 #include <limits.h>
29 #ifdef __hpux
30 # include <sys/time.h>
31 #else
32 # include <sys/select.h>
33 #endif /* __hpux */
34
35
36 /*
37 * 'backendDrainOutput()' - Drain pending print data to the device.
38 */
39
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 */
43 {
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... */
51
52
53 fprintf(stderr, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n",
54 print_fd, device_fd);
55
56 /*
57 * Figure out the maximum file descriptor value to use with select()...
58 */
59
60 nfds = (print_fd > device_fd ? print_fd : device_fd) + 1;
61
62 /*
63 * Now loop until we are out of data from print_fd...
64 */
65
66 for (;;)
67 {
68 /*
69 * Use select() to determine whether we have data to copy around...
70 */
71
72 FD_ZERO(&input);
73 FD_SET(print_fd, &input);
74
75 timeout.tv_sec = 0;
76 timeout.tv_usec = 0;
77
78 if (select(nfds, &input, NULL, NULL, &timeout) < 0)
79 return (-1);
80
81 if (!FD_ISSET(print_fd, &input))
82 return (0);
83
84 if ((print_bytes = read(print_fd, print_buffer,
85 sizeof(print_buffer))) < 0)
86 {
87 /*
88 * Read error - bail if we don't see EAGAIN or EINTR...
89 */
90
91 if (errno != EAGAIN || errno != EINTR)
92 {
93 perror("ERROR: Unable to read print data");
94 return (-1);
95 }
96
97 print_bytes = 0;
98 }
99 else if (print_bytes == 0)
100 {
101 /*
102 * End of file, return...
103 */
104
105 return (0);
106 }
107
108 fprintf(stderr, "DEBUG: Read %d bytes of print data...\n",
109 (int)print_bytes);
110
111 for (print_ptr = print_buffer; print_bytes > 0;)
112 {
113 if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0)
114 {
115 /*
116 * Write error - bail if we don't see an error we can retry...
117 */
118
119 if (errno != ENOSPC && errno != ENXIO && errno != EAGAIN &&
120 errno != EINTR && errno != ENOTTY)
121 {
122 _cupsLangPrintf(stderr, _("ERROR: Unable to write print data: %s\n"),
123 strerror(errno));
124 return (-1);
125 }
126 }
127 else
128 {
129 fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes);
130
131 print_bytes -= bytes;
132 print_ptr += bytes;
133 }
134 }
135 }
136 }
137
138
139 /*
140 * 'backendRunLoop()' - Read and write print and back-channel data.
141 */
142
143 ssize_t /* O - Total bytes on success, -1 on error */
144 backendRunLoop(
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 void (*side_cb)(int, int, int, http_addr_t *, int))
151 /* I - Side-channel callback */
152 {
153 int nfds; /* Maximum file descriptor value + 1 */
154 fd_set input, /* Input set for reading */
155 output; /* Output set for writing */
156 ssize_t print_bytes, /* Print bytes read */
157 bc_bytes, /* Backchannel bytes read */
158 total_bytes, /* Total bytes written */
159 bytes; /* Bytes written */
160 int paperout; /* "Paper out" status */
161 int offline; /* "Off-line" status */
162 char print_buffer[8192], /* Print data buffer */
163 *print_ptr, /* Pointer into print data buffer */
164 bc_buffer[1024]; /* Back-channel data buffer */
165 struct timeval timeout; /* Timeout for select() */
166 time_t curtime, /* Current time */
167 snmp_update = 0;
168 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
169 struct sigaction action; /* Actions for POSIX signals */
170 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
171
172
173 fprintf(stderr,
174 "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, snmp_fd=%d, "
175 "addr=%p, use_bc=%d, side_cb=%p)\n",
176 print_fd, device_fd, snmp_fd, addr, use_bc, side_cb);
177
178 /*
179 * If we are printing data from a print driver on stdin, ignore SIGTERM
180 * so that the driver can finish out any page data, e.g. to eject the
181 * current page. We only do this for stdin printing as otherwise there
182 * is no way to cancel a raw print job...
183 */
184
185 if (!print_fd)
186 {
187 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
188 sigset(SIGTERM, SIG_IGN);
189 #elif defined(HAVE_SIGACTION)
190 memset(&action, 0, sizeof(action));
191
192 sigemptyset(&action.sa_mask);
193 action.sa_handler = SIG_IGN;
194 sigaction(SIGTERM, &action, NULL);
195 #else
196 signal(SIGTERM, SIG_IGN);
197 #endif /* HAVE_SIGSET */
198 }
199
200 /*
201 * Figure out the maximum file descriptor value to use with select()...
202 */
203
204 nfds = (print_fd > device_fd ? print_fd : device_fd) + 1;
205
206 /*
207 * Now loop until we are out of data from print_fd...
208 */
209
210 for (print_bytes = 0, print_ptr = print_buffer, offline = -1,
211 paperout = -1, total_bytes = 0;;)
212 {
213 /*
214 * Use select() to determine whether we have data to copy around...
215 */
216
217 FD_ZERO(&input);
218 if (!print_bytes)
219 FD_SET(print_fd, &input);
220 if (use_bc)
221 FD_SET(device_fd, &input);
222 if (!print_bytes && side_cb)
223 FD_SET(CUPS_SC_FD, &input);
224
225 FD_ZERO(&output);
226 if (print_bytes || (!use_bc && !side_cb))
227 FD_SET(device_fd, &output);
228
229 if (use_bc || side_cb)
230 {
231 timeout.tv_sec = 5;
232 timeout.tv_usec = 0;
233
234 if (select(nfds, &input, &output, NULL, &timeout) < 0)
235 {
236 /*
237 * Pause printing to clear any pending errors...
238 */
239
240 if (errno == ENXIO && offline != 1)
241 {
242 fputs("STATE: +offline-report\n", stderr);
243 _cupsLangPuts(stderr, _("INFO: Printer is currently offline.\n"));
244 offline = 1;
245 }
246 else if (errno == EINTR && total_bytes == 0)
247 {
248 fputs("DEBUG: Received an interrupt before any bytes were "
249 "written, aborting!\n", stderr);
250 return (0);
251 }
252
253 sleep(1);
254 continue;
255 }
256 }
257
258 /*
259 * Check if we have a side-channel request ready...
260 */
261
262 if (side_cb && FD_ISSET(CUPS_SC_FD, &input))
263 {
264 /*
265 * Do the side-channel request, then start back over in the select
266 * loop since it may have read from print_fd...
267 */
268
269 (*side_cb)(print_fd, device_fd, snmp_fd, addr, use_bc);
270 continue;
271 }
272
273 /*
274 * Check if we have back-channel data ready...
275 */
276
277 if (FD_ISSET(device_fd, &input))
278 {
279 if ((bc_bytes = read(device_fd, bc_buffer, sizeof(bc_buffer))) > 0)
280 {
281 fprintf(stderr,
282 "DEBUG: Received " CUPS_LLFMT " bytes of back-channel data!\n",
283 CUPS_LLCAST bc_bytes);
284 cupsBackChannelWrite(bc_buffer, bc_bytes, 1.0);
285 }
286 }
287
288 /*
289 * Check if we have print data ready...
290 */
291
292 if (FD_ISSET(print_fd, &input))
293 {
294 if ((print_bytes = read(print_fd, print_buffer,
295 sizeof(print_buffer))) < 0)
296 {
297 /*
298 * Read error - bail if we don't see EAGAIN or EINTR...
299 */
300
301 if (errno != EAGAIN || errno != EINTR)
302 {
303 perror("ERROR: Unable to read print data");
304 return (-1);
305 }
306
307 print_bytes = 0;
308 }
309 else if (print_bytes == 0)
310 {
311 /*
312 * End of file, break out of the loop...
313 */
314
315 break;
316 }
317
318 print_ptr = print_buffer;
319
320 fprintf(stderr, "DEBUG: Read %d bytes of print data...\n",
321 (int)print_bytes);
322 }
323
324 /*
325 * Check if the device is ready to receive data and we have data to
326 * send...
327 */
328
329 if (print_bytes && FD_ISSET(device_fd, &output))
330 {
331 if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0)
332 {
333 /*
334 * Write error - bail if we don't see an error we can retry...
335 */
336
337 if (errno == ENOSPC)
338 {
339 if (paperout != 1)
340 {
341 fputs("STATE: +media-empty-warning\n", stderr);
342 _cupsLangPuts(stderr, _("ERROR: Out of paper!\n"));
343 paperout = 1;
344 }
345 }
346 else if (errno == ENXIO)
347 {
348 if (offline != 1)
349 {
350 fputs("STATE: +offline-report\n", stderr);
351 _cupsLangPuts(stderr, _("INFO: Printer is currently off-line.\n"));
352 offline = 1;
353 }
354 }
355 else if (errno != EAGAIN && errno != EINTR && errno != ENOTTY)
356 {
357 fprintf(stderr, _("ERROR: Unable to write print data: %s\n"),
358 strerror(errno));
359 return (-1);
360 }
361 }
362 else
363 {
364 if (paperout)
365 {
366 fputs("STATE: -media-empty-warning\n", stderr);
367 paperout = 0;
368 }
369
370 if (offline)
371 {
372 fputs("STATE: -offline-report\n", stderr);
373 _cupsLangPuts(stderr, _("INFO: Printer is now online.\n"));
374 offline = 0;
375 }
376
377 fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes);
378
379 print_bytes -= bytes;
380 print_ptr += bytes;
381 total_bytes += bytes;
382 }
383 }
384
385 /*
386 * Do SNMP updates periodically...
387 */
388
389 if (snmp_fd >= 0 && time(&curtime) >= snmp_update)
390 {
391 if (backendSNMPSupplies(snmp_fd, addr, NULL, NULL))
392 snmp_update = INT_MAX;
393 else
394 snmp_update = curtime + 5;
395 }
396 }
397
398 /*
399 * Return with success...
400 */
401
402 return (total_bytes);
403 }
404
405
406 /*
407 * End of "$Id: runloop.c 7895 2008-09-02 19:19:43Z mike $".
408 */