Debian Codegraph @

hfaxd/FIFO.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1990-1996 Sam Leffler
4 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 #include "port.h"
27 #include "Sys.h"
28 #include "config.h"
29
30 #include "HylaFAXServer.h"
31 #include "Dispatcher.h"
32
33 /*
34 * Support for communication with the HylaFAX queuer via FIFO's.
35 */
36
37 /*
38 * Create the client FIFO and open it for use.
39 */
40 bool
41 HylaFAXServer::initClientFIFO(fxStr& emsg)
42 {
43 clientFIFOName = fxStr::format(FAX_CLIENTDIR "/%u", getpid());
44 if (Sys::mkfifo(clientFIFOName, 0622) < 0 && errno != EEXIST) {
45 emsg = fxStr::format("Could not create %s: %s",
46 (const char*) clientFIFOName, strerror(errno));
47 return (false);
48 }
49 clientFd = Sys::open(clientFIFOName, CONFIG_OPENFIFO|O_NDELAY);
50 if (clientFd == -1) {
51 emsg = fxStr::format("Could not open FIFO file %s: %s",
52 (const char*) clientFIFOName, strerror(errno));
53 return (false);
54 }
55 if (!Sys::isFIFOFile(clientFd)) {
56 emsg = clientFIFOName | " is not a FIFO special file";
57 return (false);
58 }
59 // open should set O_NDELAY, but just to be sure...
60 if (fcntl(clientFd, F_SETFL, fcntl(clientFd, F_GETFL, 0) | O_NDELAY) < 0)
61 logError("initClientFIFO %s: fcntl: %m", (const char*) clientFIFOName);
62 Dispatcher::instance().link(clientFd, Dispatcher::ReadMask, this);
63 return (true);
64 }
65
66 /*
67 * Respond to input on a FIFO file descriptor.
68 */
69 int
70 HylaFAXServer::FIFOInput(int fd)
71 {
72 char buf[2048];
73 int cc;
74 while ((cc = Sys::read(fd, buf, sizeof (buf)-1)) > 0) {
75 if (cc == sizeof(buf)-1)
76 logWarning("FIFO Read full: %d", cc);
77 buf[cc] = '\0';
78 char* bp = &buf[0];
79 do {
80 if (bp[0] == '!') {
81 /*
82 * This is an event message from the scheduler
83 * generated by a previously registered trigger.
84 * Setup the unpacking work and dispatch it.
85 */
86 TriggerMsgHeader h;
87 u_int left = &buf[cc]-bp;
88 bool needy = false;
89 if (left < sizeof(TriggerMsgHeader))
90 {
91 needy = true;
92 } else
93 {
94 memcpy(&h, bp, sizeof (h)); // copy to align fields
95 if (left < h.length)
96 needy = true;
97 }
98 if (needy)
99 {
100 /*
101 * Handle the case where the buffer was read full. This means that
102 * we have a "partial" message at the end of buf, and the rest in
103 * the FIFO.
104 *
105 * buf[n] is NEVER read from the file, it's always 1-past. If we
106 * have reached buf[n], we know we are past the read data.
107 *
108 * We have (left) bytes of the message at the end of the buf.
109 * We move them to the front, and read as much more as we can to
110 * fill buf back up, leaving it in the same state as if this was
111 * the initial read.
112 */
113 memmove(buf, bp, left);
114 cc = Sys::read(fd, buf+left, (sizeof(buf)-1) - left) + left;
115 buf[cc] = '\0';
116 bp = &buf[0];
117 } else {
118 triggerEvent(h, bp+sizeof (h));
119 bp += h.length;
120 }
121 } else {
122 /*
123 * Break up '\0'-separated records and strip
124 * any trailing '\n' so that "echo mumble>FIFO"
125 * works (i.e. echo appends a '\n' character).
126 */
127 char* cp = strchr(bp, '\0');
128
129 /*
130 * Handle the case where the buffer was read full. This means that
131 * we have a "partial" message at the end of buf, and the rest in
132 * the FIFO.
133 *
134 * buf[n] is NEVER read from the file, it's always 1-past. If we
135 * have reached buf[n], we know we are past the read data.
136 *
137 * We have (cp - bp) bytes of the message at the end of the buf.
138 * We move them to the front, and read as much more as we can to
139 * fill buf back up, leaving it in the same state as if this was
140 * the initial read.
141 */
142 if (cp == &buf[sizeof(buf)-1])
143 {
144 memmove(buf, bp, cp-bp);
145 cc = Sys::read(fd, buf+(cp-bp), (sizeof(buf)-1) - (cp-bp)) + (cp-bp);
146 buf[cc] = '\0';
147 bp = &buf[0];
148 cp = strchr(bp, '\0');
149 }
150
151 if (cp > bp) {
152 if (cp[-1] == '\n') {
153 cp[-1] = '\0';
154 FIFOMessage(bp, &cp[-1]-bp);
155 } else
156 FIFOMessage(bp, cp-bp);
157 }
158 bp = cp+1;
159 }
160 } while (bp < &buf[cc]);
161 }
162 return (0);
163 }
164
165 void
166 HylaFAXServer::FIFOMessage(const char* cp, u_int)
167 {
168 if (IS(WAITFIFO)) {
169 /*
170 * Someone is waiting for a response
171 * from the server. Stash the response
172 * and notify them by marking the
173 * response as arrived.
174 */
175 fifoResponse = cp;
176 state &= ~S_WAITFIFO;
177 return;
178 }
179 switch (cp[0]) {
180 case 'H': // HELLO when queuer restarts
181 if (faxqFd >= 0)
182 Sys::close(faxqFd), faxqFd = -1;
183 if (trigSpec != "") { // reload trigger
184 fxStr emsg;
185 (void) loadTrigger(emsg);
186 }
187 break;
188 }
189 }
190
191 /*
192 * Send a message to the central queuer process.
193 */
194 bool
195 HylaFAXServer::sendQueuerMsg(fxStr& emsg, const fxStr& msg)
196 {
197 bool retry = false;
198 again:
199 if (faxqFd == -1) {
200 #ifdef FIFOSELECTBUG
201 /*
202 * We try multiple times to open the appropriate FIFO
203 * file because the system has a kernel bug that forces
204 * the server to close+reopen the FIFO file descriptors
205 * for each message received on the FIFO (yech!).
206 */
207 int tries = 0;
208 do {
209 if (tries > 0)
210 sleep(1);
211 faxqFd = Sys::open(faxqFIFOName, O_WRONLY|O_NDELAY);
212 } while (faxqFd == -1 && errno == ENXIO && ++tries < 5);
213 #else
214 faxqFd = Sys::open(faxqFIFOName, O_WRONLY|O_NDELAY);
215 #endif
216 if (faxqFd == -1) {
217 emsg = fxStr::format("Unable to open scheduler FIFO: %s",
218 strerror(errno));
219 return (false);
220 }
221 /*
222 * Turn off O_NDELAY so that write will block if FIFO is full.
223 */
224 if (fcntl(faxqFd, F_SETFL, fcntl(faxqFd, F_GETFL, 0) &~ O_NDELAY) < 0)
225 logError("fcntl: %m");
226 }
227 ssize_t len = msg.length()+1;
228 if (Sys::write(faxqFd, msg, len) != len) {
229 if (errno == EBADF || errno == EPIPE) {
230 /*
231 * The queuer process is gone. Try again
232 * once in case it has been restarted.
233 */
234 Sys::close(faxqFd), faxqFd = -1;
235 if (!retry) {
236 retry = true;
237 goto again;
238 }
239 }
240 emsg = fxStr::format("FIFO write failed: %s", strerror(errno));
241 logError(emsg);
242 return (false);
243 } else
244 return (true);
245 }
246
247 /*
248 * Send a message to the central queuer process.
249 */
250 bool
251 HylaFAXServer::sendQueuer(fxStr& emsg, const char* fmt ...)
252 {
253 va_list ap;
254 va_start(ap, fmt);
255 bool ok = sendQueuerMsg(emsg, fxStr::vformat(fmt, ap));
256 va_end(ap);
257 return (ok);
258 }
259
260 /*
261 * Send a message to the central queuer process
262 * and wait for a response on our client FIFO.
263 */
264 bool
265 HylaFAXServer::sendQueuerACK(fxStr& emsg, const char* fmt, ...)
266 {
267 va_list ap;
268 va_start(ap, fmt);
269 bool b = vsendQueuerACK(emsg, fmt, ap);
270 va_end(ap);
271 return (b);
272 }
273
274 bool
275 HylaFAXServer::vsendQueuerACK(fxStr& emsg, const char* fmt, va_list ap)
276 {
277 if (clientFd == -1) {
278 emsg = "Bad server state, client FIFO is not open";
279 return (false);
280 }
281 fxStr msg = fxStr::vformat(fmt, ap);
282 if (msg.length() < 2) { // sanity check
283 emsg = "Bad FIFO message, too short to be valid";
284 return (false);
285 }
286 msg.insert(clientFIFOName | ":", 1); // insert FIFO name for reply
287 bool ok = sendQueuerMsg(emsg, msg);
288 if (ok) {
289 Dispatcher& disp = Dispatcher::instance();
290 for (state |= S_WAITFIFO; IS(WAITFIFO); disp.dispatch())
291 ;
292 if (fifoResponse.length() < 2) { // too short to be valid
293 emsg = "Unexpected response from scheduler: \"" |fifoResponse| "\"";
294 ok = false;
295 } else if (fifoResponse[0] == msg[0]) { // response to our request
296 ok = (fifoResponse[1] == '*');
297 if (!ok)
298 emsg = "Unspecified reason (scheduler NAK'd request)";
299 } else // user abort
300 ok = false;
301 }
302 return (ok);
303 }
304
305 /*
306 * Send a message to a modem process via the per-modem FIFO.
307 */
308 bool
309 HylaFAXServer::sendModem(const char* modem, fxStr& emsg, const char* fmt ...)
310 {
311 fxStr fifoName(modem);
312 canonDevID(fifoName); // convert pathname -> devid
313 fifoName.insert("/" FAX_FIFO "."); // prepend /FIFO. string
314 #ifdef FIFOSELECTBUG
315 /*
316 * We try multiple times to open the appropriate FIFO
317 * file because the system has a kernel bug that forces
318 * the server to close+reopen the FIFO file descriptors
319 * for each message received on the FIFO (yech!).
320 */
321 int fd;
322 int tries = 0;
323 do {
324 if (tries > 0)
325 sleep(1);
326 fd = Sys::open(fifoName, O_WRONLY|O_NDELAY);
327 } while (fd == -1 && errno == ENXIO && ++tries < 5);
328 #else
329 int fd = Sys::open(fifoName, O_WRONLY|O_NDELAY);
330 #endif
331 if (fd == -1) {
332 emsg = fxStr::format("Unable to open %s: %s",
333 (const char*) fifoName, strerror(errno));
334 return (false);
335 }
336 va_list ap;
337 va_start(ap, fmt);
338 fxStr msg = fxStr::vformat(fmt, ap);
339 va_end(ap);
340 ssize_t len = msg.length()+1;
341 if (Sys::write(fd, msg, len) != len) {
342 emsg = fxStr::format("write to %s failed: %s",
343 (const char*) fifoName, strerror(errno));
344 logError(emsg);
345 return (false);
346 } else
347 return (true);
348 }