File: FIFO.c%2B%2B

package info (click to toggle)
hylafax 1%3A4.1.1-3.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,400 kB
  • ctags: 7,270
  • sloc: sh: 15,895; ansic: 12,661; makefile: 1,439; cpp: 850
file content (296 lines) | stat: -rw-r--r-- 8,318 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*	$Id: FIFO.c++,v 1.2 1999/06/13 07:41:12 robert Exp $ */
/*
 * Copyright (c) 1990-1996 Sam Leffler
 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
 * HylaFAX is a trademark of Silicon Graphics
 *
 * Permission to use, copy, modify, distribute, and sell this software and 
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names of
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
 * publicity relating to the software without the specific, prior written
 * permission of Sam Leffler and Silicon Graphics.
 * 
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
 * OF THIS SOFTWARE.
 */
#include "port.h"
#include "Sys.h"
#include "config.h"

#include "HylaFAXServer.h"
#include "Dispatcher.h"

/*
 * Support for communication with the HylaFAX queuer via FIFO's.
 */

/*
 * Create the client FIFO and open it for use.
 */
bool
HylaFAXServer::initClientFIFO(fxStr& emsg)
{
    clientFIFOName = fxStr::format(FAX_CLIENTDIR "/%u", getpid());
    if (Sys::mkfifo(clientFIFOName, 0622) < 0 && errno != EEXIST) {
	emsg = fxStr::format("Could not create %s: %s",
	    (const char*) clientFIFOName, strerror(errno));
	return (false);
    }
    clientFd = Sys::open(clientFIFOName, CONFIG_OPENFIFO|O_NDELAY);
    if (clientFd == -1) {
	emsg = fxStr::format("Could not open FIFO file %s: %s",
	    (const char*) clientFIFOName, strerror(errno));
	return (false);
    }
    if (!Sys::isFIFOFile(clientFd)) {
	emsg = clientFIFOName | " is not a FIFO special file";
	return (false);
    }
    // open should set O_NDELAY, but just to be sure...
    if (fcntl(clientFd, F_SETFL, fcntl(clientFd, F_GETFL, 0) | O_NDELAY) < 0)
	logError("initClientFIFO %s: fcntl: %m", (const char*) clientFIFOName);
    Dispatcher::instance().link(clientFd, Dispatcher::ReadMask, this);
    return (true);
}

/*
 * Respond to input on a FIFO file descriptor.
 */
int
HylaFAXServer::FIFOInput(int fd)
{
    char buf[2048];
    int cc;
    while ((cc = Sys::read(fd, buf, sizeof (buf)-1)) > 0) {
	buf[cc] = '\0';
	char* bp = &buf[0];
	do {
	    if (bp[0] == '!') {
		/*
		 * This is an event message from the scheduler
		 * generated by a previously registered trigger.
		 * Setup the unpacking work and dispatch it.
		 */
		TriggerMsgHeader h;
		memcpy(&h, bp, sizeof (h));		// copy to align fields
		if (&buf[cc]-bp < h.length) {
		    // XXX need more data to complete msg/should not happen
		} else {
		    triggerEvent(h, bp+sizeof (h));
		}
		bp += h.length;
	    } else {
		/*
		 * Break up '\0'-separated records and strip
		 * any trailing '\n' so that "echo mumble>FIFO"
		 * works (i.e. echo appends a '\n' character).
		 */
		char* cp = strchr(bp, '\0');
		if (cp > bp) {
		    if (cp[-1] == '\n') {
			cp[-1] = '\0';
			FIFOMessage(bp, &cp[-1]-bp);
		    } else
			FIFOMessage(bp, cp-bp);
		}
		bp = cp+1;
	    }
	} while (bp < &buf[cc]);
    }
    return (0);
}

void
HylaFAXServer::FIFOMessage(const char* cp, u_int)
{
    if (IS(WAITFIFO)) {
	/*
	 * Someone is waiting for a response
	 * from the server.  Stash the response
	 * and notify them by marking the
	 * response as arrived.
	 */
	fifoResponse = cp;
	state &= ~S_WAITFIFO;
	return;
    }
    switch (cp[0]) {
    case 'H':				// HELLO when queuer restarts
	if (faxqFd >= 0)
	    Sys::close(faxqFd), faxqFd = -1;
	if (trigSpec != "") {		// reload trigger
	    fxStr emsg;
	    (void) loadTrigger(emsg);
	}
	break;
    }
}

/*
 * Send a message to the central queuer process.
 */
bool
HylaFAXServer::sendQueuerMsg(fxStr& emsg, const fxStr& msg)
{
    bool retry = false;
again:
    if (faxqFd == -1) {
#ifdef FIFOSELECTBUG
	/*
	 * We try multiple times to open the appropriate FIFO
	 * file because the system has a kernel bug that forces
	 * the server to close+reopen the FIFO file descriptors
	 * for each message received on the FIFO (yech!).
	 */
	int tries = 0;
	do {
	    if (tries > 0)
		sleep(1);
	    faxqFd = Sys::open(faxqFIFOName, O_WRONLY|O_NDELAY);
	} while (faxqFd == -1 && errno == ENXIO && ++tries < 5);
#else
	faxqFd = Sys::open(faxqFIFOName, O_WRONLY|O_NDELAY);
#endif
	if (faxqFd == -1) {
	    emsg = fxStr::format("Unable to open scheduler FIFO: %s",
		strerror(errno));
	    return (false);
	}
	/*
	 * Turn off O_NDELAY so that write will block if FIFO is full.
	 */
	if (fcntl(faxqFd, F_SETFL, fcntl(faxqFd, F_GETFL, 0) &~ O_NDELAY) < 0)
	    logError("fcntl: %m");
    }
    u_int len = msg.length()+1;
    if (Sys::write(faxqFd, msg, len) != len) {
	if (errno == EBADF || errno == EPIPE) {
	    /*
	     * The queuer process is gone.  Try again
	     * once in case it has been restarted.
	     */
	    Sys::close(faxqFd), faxqFd = -1;
	    if (!retry) {
		retry = true;
		goto again;
	    }
	}
	emsg = fxStr::format("FIFO write failed: %s", strerror(errno));
	logError(emsg);
	return (false);
    } else
	return (true);
}

/*
 * Send a message to the central queuer process.
 */
bool
HylaFAXServer::sendQueuer(fxStr& emsg, const char* fmt ...)
{
    va_list ap;
    va_start(ap, fmt);
    bool ok = sendQueuerMsg(emsg, fxStr::vformat(fmt, ap));
    va_end(ap);
    return (ok);
}

/*
 * Send a message to the central queuer process
 * and wait for a response on our client FIFO.
 */
bool
HylaFAXServer::sendQueuerACK(fxStr& emsg, const char* fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    bool b = vsendQueuerACK(emsg, fmt, ap);
    va_end(ap);
    return (b);
}

bool
HylaFAXServer::vsendQueuerACK(fxStr& emsg, const char* fmt, va_list ap)
{
    if (clientFd == -1) {
	emsg = "Bad server state, client FIFO is not open";
	return (false);
    }
    fxStr msg = fxStr::vformat(fmt, ap);
    if (msg.length() < 2) {			// sanity check
	emsg = "Bad FIFO message, too short to be valid";
	return (false);
    }
    msg.insert(clientFIFOName | ":", 1);	// insert FIFO name for reply 
    bool ok = sendQueuerMsg(emsg, msg);
    if (ok) {
	Dispatcher& disp = Dispatcher::instance();
	for (state |= S_WAITFIFO; IS(WAITFIFO); disp.dispatch())
	    ;
	if (fifoResponse.length() < 2) {	// too short to be valid
	    emsg = "Unexpected response from scheduler: \"" |fifoResponse| "\"";
	    ok = false;
	} else if (fifoResponse[0] == msg[0]) {	// response to our request
	    ok = (fifoResponse[1] == '*');
	    if (!ok)
		emsg = "Unspecified reason (scheduler NAK'd request)";
	} else					// user abort
	    ok = false;
    }
    return (ok);
}

/*
 * Send a message to a modem process via the per-modem FIFO.
 */
bool
HylaFAXServer::sendModem(const char* modem, fxStr& emsg, const char* fmt ...)
{
    fxStr fifoName(modem);
    canonDevID(fifoName);			// convert pathname -> devid
    fifoName.insert("/" FAX_FIFO ".");		// prepend /FIFO. string
#ifdef FIFOSELECTBUG
    /*
     * We try multiple times to open the appropriate FIFO
     * file because the system has a kernel bug that forces
     * the server to close+reopen the FIFO file descriptors
     * for each message received on the FIFO (yech!).
     */
    int fd;
    int tries = 0;
    do {
	if (tries > 0)
	    sleep(1);
	fd = Sys::open(fifoName, O_WRONLY|O_NDELAY);
    } while (fd == -1 && errno == ENXIO && ++tries < 5);
#else
    int fd = Sys::open(fifoName, O_WRONLY|O_NDELAY);
#endif
    if (fd == -1) {
	emsg = fxStr::format("Unable to open %s: %s",
	    (const char*) fifoName, strerror(errno));
	return (false);
    }
    va_list ap;
    va_start(ap, fmt);
    fxStr msg = fxStr::vformat(fmt, ap);
    va_end(ap);
    u_int len = msg.length()+1;
    if (Sys::write(fd, msg, len) != len) {
	emsg = fxStr::format("write to %s failed: %s",
	    (const char*) fifoName, strerror(errno));
	logError(emsg);
	return (false);
    } else
	return (true);
}