]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
7e86f2f6 | 2 | * Backchannel functions for CUPS. |
ef416fc2 | 3 | * |
7e86f2f6 MS |
4 | * Copyright 2007-2014 by Apple Inc. |
5 | * Copyright 1997-2007 by Easy Software Products. | |
ef416fc2 | 6 | * |
7e86f2f6 MS |
7 | * These coded instructions, statements, and computer programs are the |
8 | * property of Apple Inc. and are protected by Federal copyright | |
9 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
10 | * which should have been included with this file. If this file is | |
57b7b66b | 11 | * missing or damaged, see the license at "http://www.cups.org/". |
ef416fc2 | 12 | * |
7e86f2f6 | 13 | * This file is subject to the Apple OS-Developed Software exception. |
ef416fc2 | 14 | */ |
15 | ||
16 | /* | |
17 | * Include necessary headers... | |
18 | */ | |
19 | ||
20 | #include "cups.h" | |
21 | #include <errno.h> | |
22 | #ifdef WIN32 | |
23 | # include <io.h> | |
24 | # include <fcntl.h> | |
25 | #else | |
26 | # include <sys/time.h> | |
27 | #endif /* WIN32 */ | |
28 | ||
29 | ||
30 | /* | |
31 | * Local functions... | |
32 | */ | |
33 | ||
34 | static void cups_setup(fd_set *set, struct timeval *tval, | |
35 | double timeout); | |
36 | ||
37 | ||
38 | /* | |
39 | * 'cupsBackChannelRead()' - Read data from the backchannel. | |
40 | * | |
79e1d494 MS |
41 | * Reads up to "bytes" bytes from the backchannel/backend. The "timeout" |
42 | * parameter controls how many seconds to wait for the data - use 0.0 to | |
43 | * return immediately if there is no data, -1.0 to wait for data indefinitely. | |
ef416fc2 | 44 | * |
8072030b | 45 | * @since CUPS 1.2/macOS 10.5@ |
ef416fc2 | 46 | */ |
47 | ||
ecdc0628 | 48 | ssize_t /* O - Bytes read or -1 on error */ |
79e1d494 | 49 | cupsBackChannelRead(char *buffer, /* I - Buffer to read into */ |
ecdc0628 | 50 | size_t bytes, /* I - Bytes to read */ |
79e1d494 | 51 | double timeout) /* I - Timeout in seconds, typically 0.0 to poll */ |
ef416fc2 | 52 | { |
53 | fd_set input; /* Input set */ | |
54 | struct timeval tval; /* Timeout value */ | |
55 | int status; /* Select status */ | |
56 | ||
57 | ||
58 | /* | |
59 | * Wait for input ready. | |
60 | */ | |
61 | ||
62 | do | |
63 | { | |
64 | cups_setup(&input, &tval, timeout); | |
65 | ||
66 | if (timeout < 0.0) | |
67 | status = select(4, &input, NULL, NULL, NULL); | |
68 | else | |
69 | status = select(4, &input, NULL, NULL, &tval); | |
70 | } | |
e07d4801 | 71 | while (status < 0 && errno != EINTR && errno != EAGAIN); |
ef416fc2 | 72 | |
73 | if (status < 0) | |
74 | return (-1); /* Timeout! */ | |
75 | ||
76 | /* | |
77 | * Read bytes from the pipe... | |
78 | */ | |
79 | ||
b86bc4cf | 80 | #ifdef WIN32 |
536bc2c6 | 81 | return ((ssize_t)_read(3, buffer, (unsigned)bytes)); |
b86bc4cf | 82 | #else |
ef416fc2 | 83 | return (read(3, buffer, bytes)); |
b86bc4cf | 84 | #endif /* WIN32 */ |
ef416fc2 | 85 | } |
86 | ||
87 | ||
88 | /* | |
89 | * 'cupsBackChannelWrite()' - Write data to the backchannel. | |
90 | * | |
79e1d494 | 91 | * Writes "bytes" bytes to the backchannel/filter. The "timeout" parameter |
ef416fc2 | 92 | * controls how many seconds to wait for the data to be written - use |
93 | * 0.0 to return immediately if the data cannot be written, -1.0 to wait | |
94 | * indefinitely. | |
95 | * | |
8072030b | 96 | * @since CUPS 1.2/macOS 10.5@ |
ef416fc2 | 97 | */ |
98 | ||
ecdc0628 | 99 | ssize_t /* O - Bytes written or -1 on error */ |
bd7854cb | 100 | cupsBackChannelWrite( |
ef416fc2 | 101 | const char *buffer, /* I - Buffer to write */ |
ecdc0628 | 102 | size_t bytes, /* I - Bytes to write */ |
79e1d494 | 103 | double timeout) /* I - Timeout in seconds, typically 1.0 */ |
ef416fc2 | 104 | { |
105 | fd_set output; /* Output set */ | |
106 | struct timeval tval; /* Timeout value */ | |
107 | int status; /* Select status */ | |
ecdc0628 | 108 | ssize_t count; /* Current bytes */ |
109 | size_t total; /* Total bytes */ | |
ef416fc2 | 110 | |
111 | ||
112 | /* | |
113 | * Write all bytes... | |
114 | */ | |
115 | ||
116 | total = 0; | |
117 | ||
118 | while (total < bytes) | |
119 | { | |
120 | /* | |
121 | * Wait for write-ready... | |
122 | */ | |
123 | ||
124 | do | |
125 | { | |
126 | cups_setup(&output, &tval, timeout); | |
127 | ||
128 | if (timeout < 0.0) | |
129 | status = select(4, NULL, &output, NULL, NULL); | |
130 | else | |
131 | status = select(4, NULL, &output, NULL, &tval); | |
132 | } | |
e07d4801 | 133 | while (status < 0 && errno != EINTR && errno != EAGAIN); |
ef416fc2 | 134 | |
e07d4801 | 135 | if (status <= 0) |
ef416fc2 | 136 | return (-1); /* Timeout! */ |
137 | ||
138 | /* | |
139 | * Write bytes to the pipe... | |
140 | */ | |
141 | ||
b86bc4cf | 142 | #ifdef WIN32 |
536bc2c6 | 143 | count = (ssize_t)_write(3, buffer, (unsigned)(bytes - total)); |
b86bc4cf | 144 | #else |
ef416fc2 | 145 | count = write(3, buffer, bytes - total); |
b86bc4cf | 146 | #endif /* WIN32 */ |
ef416fc2 | 147 | |
148 | if (count < 0) | |
149 | { | |
150 | /* | |
151 | * Write error - abort on fatal errors... | |
152 | */ | |
153 | ||
e07d4801 | 154 | if (errno != EINTR && errno != EAGAIN) |
ef416fc2 | 155 | return (-1); |
156 | } | |
157 | else | |
158 | { | |
159 | /* | |
160 | * Write succeeded, update buffer pointer and total count... | |
161 | */ | |
162 | ||
163 | buffer += count; | |
7e86f2f6 | 164 | total += (size_t)count; |
ef416fc2 | 165 | } |
166 | } | |
167 | ||
b86bc4cf | 168 | return ((ssize_t)bytes); |
ef416fc2 | 169 | } |
170 | ||
171 | ||
172 | /* | |
f3c17241 | 173 | * 'cups_setup()' - Setup select() |
ef416fc2 | 174 | */ |
175 | ||
176 | static void | |
177 | cups_setup(fd_set *set, /* I - Set for select() */ | |
178 | struct timeval *tval, /* I - Timer value */ | |
179 | double timeout) /* I - Timeout in seconds */ | |
180 | { | |
181 | tval->tv_sec = (int)timeout; | |
182 | tval->tv_usec = (int)(1000000.0 * (timeout - tval->tv_sec)); | |
183 | ||
184 | FD_ZERO(set); | |
185 | FD_SET(3, set); | |
186 | } |