]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/runloop.c
2 * "$Id: runloop.c 5594 2006-05-27 03:14:03Z mike $"
4 * Common run loop API for the Common UNIX Printing System (CUPS).
6 * Copyright 2006 by Easy Software Products, all rights reserved.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * backendRunLoop() - Read and write print and back-channel data.
32 * Include necessary headers.
35 #include "backend-private.h"
36 #include <sys/select.h>
40 * 'backendRunLoop()' - Read and write print and back-channel data.
43 ssize_t
/* O - Total bytes on success, -1 on error */
44 backendRunLoop(int print_fd
, /* I - Print file descriptor */
45 int device_fd
, /* I - Device file descriptor */
46 int use_bc
) /* I - Use back-channel? */
48 int nfds
; /* Maximum file descriptor value + 1 */
49 fd_set input
, /* Input set for reading */
50 output
; /* Output set for writing */
51 ssize_t print_bytes
, /* Print bytes read */
52 bc_bytes
, /* Backchannel bytes read */
53 total_bytes
, /* Total bytes written */
54 bytes
; /* Bytes written */
55 int paperout
; /* "Paper out" status */
56 char print_buffer
[8192], /* Print data buffer */
57 *print_ptr
, /* Pointer into print data buffer */
58 bc_buffer
[1024]; /* Back-channel data buffer */
59 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
60 struct sigaction action
; /* Actions for POSIX signals */
61 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
65 * If we are printing data from a print driver on stdin, ignore SIGTERM
66 * so that the driver can finish out any page data, e.g. to eject the
67 * current page. We only do this for stdin printing as otherwise there
68 * is no way to cancel a raw print job...
73 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
74 sigset(SIGTERM
, SIG_IGN
);
75 #elif defined(HAVE_SIGACTION)
76 memset(&action
, 0, sizeof(action
));
78 sigemptyset(&action
.sa_mask
);
79 action
.sa_handler
= SIG_IGN
;
80 sigaction(SIGTERM
, &action
, NULL
);
82 signal(SIGTERM
, SIG_IGN
);
83 #endif /* HAVE_SIGSET */
87 * Figure out the maximum file descriptor value to use with select()...
90 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
93 * Now loop until we are out of data from print_fd...
96 for (print_bytes
= 0, print_ptr
= print_buffer
, paperout
= 0, total_bytes
= 0;;)
99 * Use select() to determine whether we have data to copy around...
104 FD_SET(print_fd
, &input
);
106 FD_SET(device_fd
, &input
);
110 FD_SET(device_fd
, &output
);
112 if (select(nfds
, &input
, &output
, NULL
, NULL
) < 0)
113 continue; /* Ignore errors here */
116 * Check if we have back-channel data ready...
119 if (FD_ISSET(device_fd
, &input
))
121 if ((bc_bytes
= read(device_fd
, bc_buffer
, sizeof(bc_buffer
))) > 0)
124 "DEBUG: Received " CUPS_LLFMT
" bytes of back-channel data!\n",
125 CUPS_LLCAST bc_bytes
);
126 cupsBackChannelWrite(bc_buffer
, bc_bytes
, 1.0);
131 * Check if we have print data ready...
134 if (FD_ISSET(print_fd
, &input
))
136 if ((print_bytes
= read(print_fd
, print_buffer
,
137 sizeof(print_buffer
))) < 0)
140 * Read error - bail if we don't see EAGAIN or EINTR...
143 if (errno
!= EAGAIN
|| errno
!= EINTR
)
145 perror("ERROR: Unable to read print data");
151 else if (print_bytes
== 0)
154 * End of file, break out of the loop...
160 print_ptr
= print_buffer
;
164 * Check if the device is ready to receive data and we have data to
168 if (print_bytes
&& FD_ISSET(device_fd
, &output
))
170 if ((bytes
= write(device_fd
, print_ptr
, print_bytes
)) < 0)
173 * Write error - bail if we don't see an error we can retry...
180 fputs("ERROR: Out of paper!\n", stderr
);
181 fputs("STATUS: +media-tray-empty-error\n", stderr
);
185 else if (errno
!= EAGAIN
&& errno
!= EINTR
&& errno
!= ENOTTY
)
187 perror("ERROR: Unable to write print data");
195 fputs("STATUS: -media-tray-empty-error\n", stderr
);
199 fprintf(stderr
, "DEBUG: Wrote %d bytes...\n", (int)bytes
);
201 print_bytes
-= bytes
;
203 total_bytes
+= bytes
;
209 * Return with success...
212 return (total_bytes
);
217 * End of "$Id: runloop.c 5594 2006-05-27 03:14:03Z mike $".