summaryrefslogtreecommitdiff
path: root/src/net/htcd.cc
blob: 89729e6ef251323adb2e33a47a3cebcee6b86364 (plain)
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
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <net/thrserv.h>

static fp_stream merr(stderr);

static mstring PATH;
static htcServ* SERV;

const mstring PIDURI="/sys/htcd";

const int BACKLOG=4;
const int SERVERS=9;
const int LOWER=3;
const int UPPER=6;
const int KEEPALIVE=300;
const int SNDBUFSIZE=16384;
const int RCVBUFSIZE=16384;

///////////////////////////////////////////////////////////

static thrServ* server[SERVERS];
static thrCron* crontab;

extern "C" {
  static void* thr_handler(void* p) { ((thrServ*)p)->enter(); return 0; }
  static void* thr_cron(void* p) { ((thrCron*)p)->enter(); return 0; }
  static void onTERM(int sig) { SERV->htcd.running=0; }
  static void onHUP(int sig) { if(crontab) crontab->running=0; }
  typedef void (*disp_t)(int);
}

static void MainProc(isocket& sock)
{
  int keep=1;
  while(SERV->htcd.running) {
    int nvacant=0; for(int i=0;i<SERVERS;i++) if(server[i]->vacant()) nvacant++;
    if(nvacant>UPPER) keep=1; else if(nvacant<=LOWER) keep=0;
    if(!keep) for(int i=0;i<SERVERS;i++) server[i]->shutdown();
    time_t timeout=time(0)-KEEPALIVE;
    for(int i=0;i<SERVERS;i++) if(timeout>server[i]->last) server[i]->disconnect();
    int test=sock.test();
    if(test>0) {
      thrServ* p=0;
      for(int i=0;i<SERVERS;i++) if(server[i]->vacant()) { p=server[i]; break; }
      if(!p) for(int i=0;i<SERVERS;i++) if(!p||server[i]->first<p->first) p=server[i];
      if(p->shutdown()) {
        try {
          isocket* s=sock.accept();
          if(s) p->reset(s,keep);
        }
        catch(const merror_t& e) {
          merr<<"SOCKET: "<<e.desc<<"\n";
        }
      }
    }
    else if(test<0) SERV->htcd.running=0;
  }
  for(int i=0;i<SERVERS;i++) {
    server[i]->term();
    pthread_join(server[i]->ptid,0);
  }
  pthread_kill(crontab->tid,SIGHUP);
  pthread_join(crontab->ptid,0);
}

///////////////////////////////////////////////////////////////

static void httpServer(int overport)
{
  isocket sock(SERV->htcd.node.addr,overport,BACKLOG);
  sock.keepalive(1);
  sock.sendbufsize(SNDBUFSIZE);
  sock.recvbufsize(RCVBUFSIZE);

  sigignore(SIGPIPE);
  disp_t onterm=signal(SIGTERM,onTERM);
  disp_t onint=signal(SIGINT,onTERM);
  disp_t onhup=signal(SIGHUP,onHUP);
  MainProc(sock);
  signal(SIGHUP,onhup);
  signal(SIGINT,onint);
  signal(SIGTERM,onterm);
}

static void Daemon(int overport) try
{
  PROCLOCK(PATH+PIDURI);
  SERV=new htcServ(PATH);
  crontab=new thrCron(*SERV);
  pthread_create(&crontab->ptid,0,thr_cron,crontab);
  for(int i=0;i<SERVERS;i++) {
    server[i]=new thrServ(*SERV);
    pthread_create(&server[i]->ptid,0,thr_handler,server[i]);
  }
  merr<<"htcd: "<<PATH<<" started\n";
  httpServer(overport<0?0:overport==0?SERV->htcd.port:overport);
  merr<<"htcd: "<<PATH<<" terminating\n";
  for(int i=0;i<SERVERS;i++) delete server[i];
  delete crontab;
}
catch(const merror_t& e) {
  if(SERV) {
    SERV->htcd.errlog<<"["<<time(0)<<"][MAIN]["<<e.desc<<"]\n";
    merr<<e.desc<<"\n";
  }
  else if(First(e.desc)!=sref("proclock:")) merr<<e.desc<<"\n";
}
catch(...) {
  if(SERV) SERV->htcd.errlog<<"["<<time(0)<<"][MAIN][FATAL]\n";  
  merr<<"FATAL\n";
}

main(int argc,char* argv[]) try
{
  if(argc<3) {
    merr<<"Usage: "<<argv[0]<<" <path> <overport=-1|0|port> [-1|0|1]\n"; 
    return -1;
  }
  PATH=argv[1]; if(PATH.back()=='/') PATH=PATH.popb();
  setenv("DOCROOT",PATH.c_str(),1);
  int overport=atoi(argv[2]);
  int cmd=argc>3?atoi(argv[3]):0;
  if(cmd) ProcTerminate(PATH+PIDURI);
  if(cmd>=0) { if(fork()==0) Daemon(overport); }
  delete SERV;
  return 0;
}
catch(const merror_t& e) {
  merr<<"ERROR: ["<<e.desc<<"]\n";
  return -1;
}
catch(...) {
  merr<<"ERROR: [FATAL]\n";
  return -1;
}