summaryrefslogtreecommitdiffstats
path: root/httpserver/init.cc
blob: 964f1e2fd9cc7338354f1f4043863374fdf94840 (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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* This file is part of rasdaman community.
*
* Rasdaman community 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 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community 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 rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/

#include "mymalloc/mymalloc.h"
/*------------------------------------------------------------------------*/
/*  init.c - Functions to setup the server.                               */
/*------------------------------------------------------------------------*/

#include <iostream>

#include   "defs.h"
#include   "protos.h"
#include   "server.h"
#include   "http-defs.h"
#include   "http.h"

extern struct ServerBase    Server;


/****** init/Initialize ******************************************************
*                                                                             
*   NAME                                                                      
*       Initialize -- central function for initializing the servers data      
*           structures and environment.                                       
*                                                                             
*   SYNOPSIS                                                                  
*       int Initialize( int argc, char *argv[], struct ServerBase *Server );  
*                                                                             
*   FUNCTION                                                                  
*                                                                             
*   RESULT                                                                    
*                                                                             
*   NOTES                                                                     
*                                                                             
*   BUGS                                                                      
*                                                                             
*   SEE ALSO                                                                  
*                                                                             
******************************************************************************
*
*/

rc_t Initialize( int argc, char *argv[], struct ServerBase *Server )
{

  /*  Make sure that the Server structure is empty.  -------------------- */
  bzero( (char *)Server, sizeof( *Server ) );

  /*  Create Head of ChildList  */
  Server->ChildList = (struct ChildBase*)mymalloc( sizeof( struct ChildBase ) );
  if( Server->ChildList == NULL )
    ErrorMsg( E_SYS, FAIL, "FAIL:  Initialize(): malloc() failed!" );
  else
    {
      //LogMsg( LG_SERVER, DEBUG, "DEBUG:  Creating Head of ChildList" );
      Server->ChildList->next = Server->ChildList;
      Server->ChildList->prev = Server->ChildList;
      Server->ChildList->PId  = 0;
    }

  /* Read the environment variables */
  char* dummy = CONFDIR;
  if( dummy == NULL)
    ErrorMsg( E_SYS, FAIL, "FAIL:  configure error: CONFDIR not provided." );
  Server->Directory = (char*)mymalloc(strlen(dummy) + 2);
  strcpy(Server->Directory, dummy);
  if( Server->Directory[strlen(Server->Directory)] != '/' )
    strcat( Server->Directory, "/" );

  /*  Read Arguments and Server configuration file - and setup  --------- */
  /*      the corresponding data structures.  --------------------------- */

  ReadArgs( Server, argc, argv );
  //ReadConfig( Server );

  ConfigureServer( Server );
  // CheckConfig( Server );  // Check for reasonable configuration

  /*  Set timeout for communication with subservers and client  --------- */
  Server->Client.TimeOut.tv_sec  = DIALOG_TIMEOUT;
  Server->Client.TimeOut.tv_usec = 0;

  /*  Get the process into "daemon"-mode.  ------------------------------ */
  InitDaemon( Server->Log.Mode );
  Server->PId = getpid();

  //SavePId( Server->PidFile );
  /*  Initialize the Server Socket.  ------------------------------------ */
  InitSocket( &Server->SockFD, &Server->Socket, Server->Port );

  /*  Setup and Open the logfiles.  ------------------------------------- */
  OpenLog( &Server->Log, Server->Log.Access.Filename, 
	                 Server->Log.Server.Filename, 
	                 Server->Log.Comm.Filename );


  LogMsg( LG_SERVER, INFO, "INFO:  ========= %s started. ===============", DAEMONNAME );

  // We don't do that so that the one process running can be exited with simple ^C
  /*  Initialize the signal handlers.  ---------------------------------- */
  // InitSigHandler();

  /*  Everything is just great...  -------------------------------------- */
  return( OK );
}


/****** init/InitDaemon ******************************************************
*                                                                             
*   NAME                                                                      
*       InitDaemon -- going into "daemon"-mode: process forks to run in the   
*           background and closes all open file-descriptors.                  
*                                                                             
*   SYNOPSIS                                                                  
*       int InitDaemon( int Mode );                                           
*                                                                             
*   FUNCTION                                                                  
*                                                                             
*   RESULT                                                                    
*                                                                             
*   NOTES                                                                     
*                                                                             
*   BUGS                                                                      
*                                                                             
*   SEE ALSO                                                                  
*                                                                             
******************************************************************************
*
*/

rc_t InitDaemon( int Mode )
{
  int i;
  pid_t pid;
  int openmax;

  // RasDaMan does not go into background!
  chdir( Server.Directory );            /* Arbeitsverzeichnis wechseln */
  umask( 0 );                           /* Dateischutzbitmaske loeschen */
  return( OK );
}


/****** init/InitSocket ******************************************************
*                                                                             
*   NAME                                                                      
*       
*                                                                             
*   SYNOPSIS                                                                  
*       
*                                                                             
*   FUNCTION                                                                  
*       
*                                                                             
*   INPUTS                                                                    
*       
*                                                                             
*   RESULT                                                                    
*       
*                                                                             
*   NOTES                                                                     
*       ###  Minimum Socket Setup. Stay tuned.                                
*                                                                             
*   BUGS                                                                      
*       
*                                                                             
*   SEE ALSO                                                                  
*       
*                                                                             
******************************************************************************
*
*/

rc_t InitSocket( int *SockFD, struct sockaddr_in *Socket, int Port )
{
  int val = 0;
  size_t len;

  Socket->sin_family       = AF_INET;
  Socket->sin_addr.s_addr  = htonl( INADDR_ANY );
  Socket->sin_port         = htons( Port );
  if( ( *SockFD = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
    ErrorMsg( E_SYS, FAIL, "FAIL:  Open socket for server." );

#ifdef SO_REUSEADDR
  val = 1;
  len = sizeof( val );
  if( setsockopt( *SockFD, SOL_SOCKET, SO_REUSEADDR, (char*)&val, len ) < 0 )
    ErrorMsg( E_SYS, WARN, "WARN:  InitSocket(): can't set sockopt REUSEADDR." );
#else
  LogMsg( LG_SERVER, INFO, "INFO:  InitSocket(): sockopt REUSEADDR not available." );
#endif

  if( bind( *SockFD, (struct sockaddr *)Socket, sizeof( *Socket ) ) < 0 )
    ErrorMsg( E_SYS, FAIL, "FAIL:  Bind socket for server." );
  return( OK );
}


/****** init/InitClientSocket ************************************************
*                                                                             
*   NAME                                                                      
*       
*                                                                             
*   SYNOPSIS                                                                  
*       
*                                                                             
*   FUNCTION                                                                  
*       
*                                                                             
*   INPUTS                                                                    
*       
*                                                                             
*   RESULT                                                                    
*       
*                                                                             
*   NOTES                                                                     
*       
*                                                                             
*   BUGS                                                                      
*       
*                                                                             
*   SEE ALSO                                                                  
*       
*                                                                             
******************************************************************************
*
*/

rc_t InitClientSocket( int *SockFD, 
		      struct sockaddr_in *Socket, 
		      char *Hostname, 
		      int Port )
{
  struct hostent *Host;  

  bzero( (char *)Socket, sizeof( *Socket ) );

  Socket->sin_family       = AF_INET;
  Host = gethostbyname( Hostname );
  if( Host == NULL )
    {
      return( ERROR );
    }
  memcpy( (char *)&Socket->sin_addr, (char *)Host->h_addr, Host->h_length );

  Socket->sin_port         = htons( Port );
  if( ( *SockFD = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
    return( ERROR );
  return( OK );
}


/****** init/SavePId *********************************************************
*                                                                             
*   NAME                                                                      
*       SavePId -- store process id in file.                                  
*                                                                             
*   SYNOPSIS                                                                  
*       void SavePId( char *PIdFilename );                                    
*                                                                             
*   FUNCTION                                                                  
*       THis function saves the parent process id in a file, so this file     
*       can be used by for example shell scripts to identify the correct      
*       process to signal it.                                                 
*                                                                             
*   INPUTS                                                                    
*       PIdFilename -- Full pathname of the file which stortes the pid.       
*                                                                             
*   RESULT                                                                    
*       Does not return anything.                                             
*                                                                             
*   NOTES                                                                     
*       This function is called by Initialize(), right after the logs are     
*       opened. So the content of the pidfile is also only valid afterwards.  
*       To ensure, that there is no stale pidfile, it is deleted on normal    
*       termination.                                                          
*                                                                             
*   BUGS                                                                      
*       
*                                                                             
*   SEE ALSO                                                                  
*       
*                                                                             
******************************************************************************
*
*/


void SavePId( char *PIdFilename )
{
  FILE  *PIdFile;

  if( ( PIdFile = fopen( PIdFilename, "w" ) ) != NULL )
    {
      fprintf( PIdFile, "%d", getpid() );
      fclose( PIdFile );
    }
}