Debian Codegraph @

hfaxd/Status.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1995-1996 Sam Leffler
4 * Copyright (c) 1995-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
27 /*
28 * Support related to server status.
29 */
30 #include "HylaFAXServer.h"
31 #include "Sys.h"
32 #include "config.h"
33
34 #include <ctype.h>
35 #include <sys/file.h>
36
37 class ModemConfig : public FaxConfig {
38 public:
39 enum { // ClassModem::SpeakerVolume
40 OFF = 0, // nothing
41 QUIET = 1, // somewhere near a dull chirp
42 LOW = 2, // normally acceptable
43 MEDIUM = 3, // heard above a stereo
44 HIGH = 4 // ear splitting
45 };
46 u_int speakerVolume; // volume control
47 u_int tracingLevel; // tracing level w/o session
48 u_int logTracingLevel; // tracing level during session
49 u_int maxRecvPages; // max pages to accept on receive
50 fxStr localIdentifier; // to use in place of FAXNumber
51 fxStr FAXNumber; // phone number
52 fxStr modemName; // canonical modem name
53 bool isGettyRunning; // true if faxgetty responds via FIFO
54 fxStr status; // from status file
55
56 ModemConfig(const char* name);
57 ~ModemConfig() {};
58
59 bool setConfigItem(const char* tag, const char* value);
60 void configError(const char* fmt, ...);
61 void configTrace(const char* fmt, ...);
62
63 void checkGetty(const char* fifoFile);
64 };
65 ModemConfig::ModemConfig(const char* name) : modemName(name)
66 {
67 HylaFAXServer::canonModem(modemName);
68 maxRecvPages = (u_int) -1;
69 tracingLevel = 0;
70 logTracingLevel = 0;
71 speakerVolume = QUIET;
72 }
73 void ModemConfig::configError(const char*, ...) {}
74 void ModemConfig::configTrace(const char*, ...) {}
75
76 void
77 ModemConfig::checkGetty(const char* fifoFile)
78 {
79 int fifo;
80 Sys::close(fifo = Sys::open(fifoFile, O_WRONLY|O_NDELAY));
81 isGettyRunning = (fifo != -1);
82 }
83
84 bool
85 ModemConfig::setConfigItem(const char* tag, const char* value)
86 {
87 if (streq(tag, "faxnumber"))
88 FAXNumber = value;
89 else if (streq(tag, "localidentifier"))
90 localIdentifier = value;
91 else if (streq(tag, "speakervolume")) { // XXX duplicates code elsewhere
92 if (strcasecmp(value, "off") == 0)
93 speakerVolume = OFF;
94 else if (strcasecmp(value, "quiet") == 0)
95 speakerVolume = QUIET;
96 else if (strcasecmp(value, "low") == 0)
97 speakerVolume = LOW;
98 else if (strcasecmp(value, "medium") == 0)
99 speakerVolume = MEDIUM;
100 else
101 speakerVolume = HIGH;
102 } else if (streq(tag, "tracinglevel"))
103 tracingLevel = getNumber(value);
104 else if (streq(tag, "logtracinglevel"))
105 logTracingLevel = getNumber(value);
106 else if (streq(tag, "maxrecvpages"))
107 maxRecvPages = getNumber(value);
108 return (true); // avoid complaints
109 }
110
111 void
112 HylaFAXServer::listStatus(FILE* fd, const SpoolDir& sd, DIR* dir)
113 {
114 KeyStringArray listing;
115 /*
116 * Check scheduler status.
117 */
118 int fifo;
119 Sys::close(fifo = Sys::open("/" FAX_FIFO, O_WRONLY|O_NDELAY));
120 fprintf(fd, "HylaFAX scheduler on %s: %s\r\n"
121 , (const char*) hostname
122 , fifo != -1 ? "Running" : "Not running"
123 );
124 /*
125 * Cross-check entries in the status directory
126 * against the other files required for an operating
127 * modem. There must be a configuration file and,
128 * if there is a faxgetty process running, there must
129 * be a FIFO special file in the root filesystem.
130 */
131 fxStr path(sd.pathname);
132 struct stat sb;
133 fxStr fifoPrefix("/" FAX_FIFO ".");
134 struct dirent* dp;
135 while ((dp = readdir(dir))) {
136 fxStr statusFile(path | dp->d_name);
137 if (!FileCache::update(statusFile, sb) || !S_ISREG(sb.st_mode))
138 continue;
139 // verify there is a modem config file
140 fxStr configFile = fxStr::format("/" FAX_CONFIG ".%s", dp->d_name);
141 if (!FileCache::lookup(configFile, sb) || !S_ISREG(sb.st_mode))
142 continue;
143 fxStr fifoFile(fifoPrefix | dp->d_name);
144 if (!FileCache::lookup(fifoFile, sb) || !S_ISFIFO(sb.st_mode))
145 continue;
146 ModemConfig config(dp->d_name);
147 config.readConfig(configFile); // read config file
148 config.checkGetty(fifoFile); // check for faxgetty
149 getServerStatus(statusFile, config.status); // XXX
150 if (modemSortFormat.length() == 0) {
151 Mprintf(fd, modemFormat, config);
152 fputs("\r\n", fd);
153 } else {
154 fxStackBuffer buf;
155 Mprintf(buf, modemFormat, config);
156 fxStr content(buf, buf.getLength());
157 buf.reset();
158 Mprintf(buf, modemSortFormat, config);
159 fxStr key(buf, buf.getLength());
160 listing.append(KeyString(key, content));
161 }
162 }
163 if (listing.length() > 1)
164 listing.qsort();
165
166 for (u_int i = 0; i < listing.length(); i++)
167 {
168 fwrite(listing[i], listing[i].length(), 1, fd);
169 fputs("\r\n", fd);
170 }
171 }
172
173 void
174 HylaFAXServer::listStatusFile(FILE* fd, const SpoolDir& dir,
175 const char* filename, const struct stat& sb)
176 {
177 listUnixFile(fd, dir, filename, sb);
178 }
179
180 void
181 HylaFAXServer::getServerStatus(const char* fileName, fxStr& status)
182 {
183 int fd = Sys::open(fileName, O_RDONLY);
184 if (fd > 0) {
185 struct stat sb;
186 (void) Sys::fstat(fd, sb);
187 status.resize((u_int) sb.st_size);
188 char* buff = new char[sb.st_size];
189 int n = Sys::read(fd, buff, (size_t) sb.st_size);
190 status = buff;
191 Sys::close(fd);
192 if (n > 0 && status[n-1] == '\n') n--;
193 if (n == 0) {
194 status = "No status (empty file)";
195 } else {
196 status.resize(n);
197 }
198 delete [] buff;
199 } else {
200 status = "No status (cannot open file)";
201 }
202 }
203
204 static const char mformat[] = {
205 'a', // a
206 'b', // b
207 'c', // c
208 'd', // d
209 'e', // e
210 'f', // f
211 'g', // g
212 's', // h (hostname)
213 'i', // i
214 'j', // j
215 'k', // k
216 's', // l (local identifier)
217 's', // m (canonical modem name)
218 's', // n (phone number)
219 'o', // o
220 'p', // p
221 'q', // q
222 's', // r (max recv pages)
223 's', // s (status information)
224 's', // t (server/session tracing level)
225 'x', // u (server tracing level)
226 'c', // v (speaker volume)
227 'w', // w
228 'x', // x
229 'y', // y
230 'c' // z (``*'' if faxgetty is running)
231 };
232
233 /*
234 * Print a formatted string with fields filled in from
235 * the specified modem state. This functionality is
236 * used to permit clients to get modem status listings
237 * in preferred formats.
238 */
239 void
240 HylaFAXServer::Mprintf(fxStackBuffer& buf, const char* fmt, const ModemConfig& config)
241 {
242 for (const char* cp = fmt; *cp; cp++) {
243 if (*cp == '%') {
244 #define MAXSPEC 20
245 char fspec[MAXSPEC];
246 char* fp = fspec;
247 *fp++ = '%';
248 char c = *++cp;
249 if (c == '-')
250 *fp++ = c, c = *++cp;
251 if (isdigit(c)) {
252 do {
253 *fp++ = c;
254 } while (isdigit(c = *++cp) && fp < &fspec[MAXSPEC-3]);
255 }
256 if (c == '.') {
257 do {
258 *fp++ = c;
259 } while (isdigit(c = *++cp) && fp < &fspec[MAXSPEC-2]);
260 }
261 if (!islower(c)) {
262 if (c == '%') // %% -> %
263 buf.put(c);
264 else
265 buf.fput("%.*s%c", fp-fspec, fspec, c);
266 continue;
267 }
268 fp[0] = mformat[c-'a']; // printf format string
269 fp[1] = '\0';
270 switch (c) {
271 case 'h':
272 buf.fput(fspec, (const char*) hostname);
273 break;
274 case 'l':
275 buf.fput(fspec, (const char*) config.localIdentifier);
276 break;
277 case 'm':
278 buf.fput(fspec, (const char*) config.modemName);
279 break;
280 case 'n':
281 buf.fput(fspec, (const char*) config.FAXNumber);
282 break;
283 case 'r':
284 /*
285 * this is not used for some reason ie -1 represents infinite
286 * if (config.maxRecvPages == (u_int) -1) {
287 * tmp = "INF";
288 * } else {
289 * tmp = fxStr::format("%u", config.maxRecvPages);
290 * }
291 */
292 buf.fput(fspec, config.maxRecvPages);
293 break;
294 case 's':
295 buf.fput(fspec, (const char*) config.status);
296 break;
297 case 't': {
298 fxStr tmp = fxStr::format("%05x:%05x",
299 config.tracingLevel&0xfffff,
300 config.logTracingLevel&0xfffff);
301 buf.fput(fspec, (const char*)tmp);
302 }
303 break;
304 case 'v':
305 buf.fput(fspec, " QLMH"[config.speakerVolume]);
306 break;
307 case 'z':
308 buf.fput(fspec, config.isGettyRunning ? '*' : ' ');
309 break;
310 }
311 } else
312 buf.put(*cp);
313 }
314 }
315
316 void
317 HylaFAXServer::Mprintf(FILE* fd, const char* fmt, const ModemConfig& config)
318 {
319 fxStackBuffer buf;
320 Mprintf(buf, fmt, config);
321 fwrite((const char*)buf, buf.getLength(), 1, fd);
322 }
323
324
325 void
326 HylaFAXServer::nlstStatus(FILE* fd, const SpoolDir& sd, DIR* dir)
327 {
328 /*
329 * Cross-check entries in the status directory
330 * against the other files required for an operating
331 * modem. There must be a configuration file and,
332 * if there is a faxgetty process running, there must
333 * be a FIFO special file in the root filesystem.
334 */
335 fxStr path(sd.pathname);
336 struct stat sb;
337 fxStr fifoPrefix("/" FAX_FIFO ".");
338 struct dirent* dp;
339 while ((dp = readdir(dir))) {
340 fxStr statusFile(path | dp->d_name);
341 if (!FileCache::update(statusFile, sb) || !S_ISREG(sb.st_mode))
342 continue;
343 // verify there is a modem config file
344 fxStr configFile = fxStr::format("/" FAX_CONFIG ".%s", dp->d_name);
345 if (!FileCache::lookup(configFile, sb) || !S_ISREG(sb.st_mode))
346 continue;
347 fxStr fifoFile(fifoPrefix | dp->d_name);
348 if (!FileCache::lookup(fifoFile, sb) || !S_ISFIFO(sb.st_mode))
349 continue;
350 ModemConfig config(dp->d_name);
351 config.readConfig(configFile); // read config file
352 config.checkGetty(fifoFile); // check for faxgetty
353 getServerStatus(statusFile, config.status); // XXX
354 Mprintf(fd, "%m\r\n", config);
355 }
356 }