| 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 |
#include "Sys.h" |
| 27 |
#include "Dispatcher.h" |
| 28 |
#include "HylaFAXServer.h" |
| 29 |
#include "SuperServer.h" |
| 30 |
#include "Socket.h" |
| 31 |
|
| 32 |
#define MAXTRIES 10 |
| 33 |
|
| 34 |
SuperServer::SuperServer(const char* k, int bl) : kind(k) |
| 35 |
{ |
| 36 |
backlog = bl; |
| 37 |
ntries = 0; |
| 38 |
Dispatcher::instance().startTimer(0,1,this); // schedule setup |
| 39 |
} |
| 40 |
SuperServer::~SuperServer() {} |
| 41 |
|
| 42 |
void |
| 43 |
SuperServer::timerExpired(long, long) |
| 44 |
{ |
| 45 |
if (!startServer()) { |
| 46 |
if (++ntries >= MAXTRIES) { |
| 47 |
logError("HylaFAX %s: Unable to init server, " |
| 48 |
"giving up after %u tries.", (const char*) kind, ntries); |
| 49 |
return; |
| 50 |
} |
| 51 |
logNotice("HylaFAX %s: Unable to init server, " |
| 52 |
"trying again in %u seconds.", (const char*) kind, 5*ntries); |
| 53 |
Dispatcher::instance().startTimer(5*ntries,0, this); |
| 54 |
} else |
| 55 |
logNotice("HylaFAX %s Protocol Server: restarted.", (const char*) kind); |
| 56 |
} |
| 57 |
|
| 58 |
int |
| 59 |
SuperServer::inputReady(int fd) |
| 60 |
{ |
| 61 |
Socket::Address addr; |
| 62 |
socklen_t slen = sizeof(addr); |
| 63 |
int c = Socket::accept(fd, &addr, &slen); |
| 64 |
if (c < 0) { |
| 65 |
if (errno == EINTR || errno == ECONNABORTED) |
| 66 |
return (0); |
| 67 |
logError("HylaFAX %s: accept: %m", (const char*) kind); |
| 68 |
_exit(-1); |
| 69 |
} |
| 70 |
#ifdef IPV6_ADDRFORM |
| 71 |
if (Socket::family(addr) == AF_INET6) { |
| 72 |
struct in6_addr& a = addr.in6.sin6_addr; |
| 73 |
if ( (a.s6_addr32[0] == 0 && a.s6_addr32[1] == 0 && a.s6_addr32[2] == htonl(0xFFFF)) || |
| 74 |
(a.s6_addr32[0] == 0 && a.s6_addr32[1] == 0 && a.s6_addr32[2] == 0 && ntohl(a.s6_addr32[3]) > 1) ) { |
| 75 |
logDebug("IPv4 address in AF_INET6, forcing AF_INET"); |
| 76 |
int af = AF_INET; |
| 77 |
setsockopt(c, IPPROTO_IPV6, IPV6_ADDRFORM, &af, sizeof(af)); |
| 78 |
} |
| 79 |
} |
| 80 |
#endif |
| 81 |
pid_t pid = fork(); |
| 82 |
switch (pid) { |
| 83 |
case 0: // child |
| 84 |
/* |
| 85 |
* Child process, setup to process protocol requests. |
| 86 |
* We unlink the dispatcher hook to this code and |
| 87 |
* setup the link to the main handler that processes |
| 88 |
* protocol requests. This routine is dispatched |
| 89 |
* when data is received on stdin (for compatibility |
| 90 |
* with servers started via inetd). |
| 91 |
*/ |
| 92 |
HylaFAXServer* app; app = newChild(); // XXX for __GNUC__ |
| 93 |
HylaFAXServer::closeLogging(); // close any open syslog fd |
| 94 |
HylaFAXServer::closeAllDispatched(); |
| 95 |
Sys::close(STDERR_FILENO); |
| 96 |
if (dup2(c, STDIN_FILENO) < 0 || dup2(c, STDOUT_FILENO) < 0) { |
| 97 |
logError("HylaFAX %s: dup2: %m", (const char*) kind); |
| 98 |
_exit(-1); |
| 99 |
} |
| 100 |
if (c != STDIN_FILENO && c != STDOUT_FILENO) |
| 101 |
Sys::close(c); |
| 102 |
HylaFAXServer::setupLogging(); // reopen syslog before chroot |
| 103 |
app->open(); // opening greeting |
| 104 |
break; |
| 105 |
case -1: // fork failure |
| 106 |
logError("HylaFAX %s: Cannot fork: %m", (const char*) kind); |
| 107 |
break; |
| 108 |
default: // parent |
| 109 |
Sys::close(c); |
| 110 |
Dispatcher::instance().startChild(pid, this); |
| 111 |
break; |
| 112 |
} |
| 113 |
return (0); // indicate data was consumed |
| 114 |
} |
| 115 |
|
| 116 |
void |
| 117 |
SuperServer::childStatus(pid_t, int) |
| 118 |
{ |
| 119 |
/* |
| 120 |
* Nothing to do here - childStatus means it's already been reaped, and |
| 121 |
* thus off the queue from the Dispatcher |
| 122 |
*/ |
| 123 |
} |