summaryrefslogtreecommitdiffstats
path: root/rasmgr/rasmgr_main.cc
blob: 6280cbfc4996255e0f7509cbddff92a8c9e05820 (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
/*
* 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>.
/
/**
 * SOURCE: rasmgr_main.cc
 *
 * MODULE: rasmgr
 * CLASS:  
 *
 * PURPOSE:
 *   management of rasserver executables
 *    
 * COMMENTS:
 * - FIXME: looks like a rasmgr slave still uses old comm scheme -- compatible?
 *
*/

#include <iostream>
 
#include "rasmgr.hh"
#include "rasmgr_config.hh"
#include "rasmgr_host.hh"
#include "rasmgr_dbm.hh"
#include "rasmgr_srv.hh"
#include "rasmgr_master.hh" 
#include "rasmgr_rascontrol.hh"
#include "rasmgr_users.hh"
#include "ras_crypto.hh"
#include "rasmgr_localsrv.hh"
#include "rasmgr_error.hh"

#ifndef COMPDATE
#error "Please specify the COMPDATE variable!"
/*
COMPDATE=`date +"%d.%m.%Y %H:%M:%S"`

and -DCOMPDATE="\"$(COMPDATE)\"" when compiling
*/
#endif

#include "raslib/rmdebug.hh"
#define DEBUG_MAIN
#undef DEBUG_HH
#include "debug.hh"

RMINITGLOBALS('S');

Configuration config;

HostManager         hostmanager;
DatabaseHostManager dbHostManager;
DatabaseManager     dbManager;
RasServerManager    rasManager;
MasterComm          masterCommunicator;
RasControl          rascontrol;
UserManager         userManager;
Authorization       authorization;
LocalServerManager  localServerManager;
RandomGenerator     randomGenerator;

void installSignalHandlers();

int main(int argc, char** argv, char** envp)
{
	SET_OUTPUT( true );	// enable debugging trace, if compiled so

	ENTER( "main." );

	std::cout<< "rasmgr: rasdaman server manager tool. rasdaman v" << RMANVERSION / 1000. << " -- generated on " << COMPDATE << "." << std::endl
        << "Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann, rasdaman GmbH.\n"
        << "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. \n"
             "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. \n\n";

	if(testIsMessageDigestAvailable("MD5")==false) 
	{
		std::cout<<"Error: Message Digest MD5 not available."<<std::endl;
		return RASMGR_RESULT_NO_MD5;
	}
	
	installSignalHandlers();

	bool result  = config.interpretArguments(argc,argv,envp);
	if (result == false)
		 return RASMGR_RESULT_ILL_ARGS;

	if(config.isTestModus())
	{
	  std::cout<<"rasmgr running in test modus ";
	  VLOG <<", listening on port=" << config.getListenPort() << std::endl;
	}
	else
	{   
		 VLOG << " rasmgr running on " << config.getHostName() << ", listening on port " << config.getListenPort();
		 VLOG << " with poll timeout " << config.getPollFrequency() << " seconds. ";
	}	
	
	if(config.isTestModus()==false)
	{
		TALK( "hostname=" << config.getHostName() << ", publicHostname=" << config.getPublicHostName() );
		if(strcmp(config.getHostName(),config.getPublicHostName()) != 0)
		{
			VLOG <<"Advertised host name is "<<config.getPublicHostName()<<std::endl;
		}
		
		 bool resultConfig = config.readConfigFile();
		 TALK( "rasmgr::main: resultConfig=" << resultConfig );
		 rascontrol.setConfigDirty( false );		// all changes to config up to now come from config file, do not require save

		int resultAuth = authorization.readAuthFile();
		TALK( "rasmgr::main: resultAuth=" << resultAuth );
		switch( resultAuth )
		{
			case RC_OK:
				TALK( "rasmgr::main: auth file ok, set state to not dirty." );
				rascontrol.setAuthDirty( false );	// auth file ok, so clean init state
				break;
			case ERRAUTHFNOTF:
				TALK( "rasmgr::main: auth file not found, loading defaults." );
				userManager.loadDefaults();
				// disabled because otherwise tons of new auth files are generated -- PB 2005-jul-02
				// rascontrol.setAuthDirty( true );	// auth file not present, write default
				break;
			case ERRAUTHFCORR:
				LEAVE( "rasmgr::main: auth file corrupt." );
				return RASMGR_RESULT_AUTH_CORRUPT;
				break;
			case ERRAUTHFWRHOST:
				LEAVE( "rasmgr::main: auth file for another host." );
				return RASMGR_RESULT_AUTH_OTHERHOST;
				break;
			case ERRAUTHFVERS:
				LEAVE( "rasmgr::main: auth file version mismatch." );
				return RASMGR_RESULT_AUTH_INCOMPAT;
				break;
			default:				// should not occur, internal enum mismatch
				LEAVE( "rasmgr::main: illegal auth file result code " << resultAuth << ", internal error." );
				return RASMGR_RESULT_INTERNAL;
				break;
			}

		try
		{
			TALK( "launching masterCommunicator.Run()..." );
  			masterCommunicator.Run();		// central request handling loop
			TALK( "masterCommunicator.Run() done." );
		}
		catch(RCError& e)
		{
			char *errorMsg;
			e.getString(errorMsg);
			std::cout<<"Error: "<<errorMsg<<std::endl;
		}                   

// write the config file only on explicit rascontrol request "save"
// (and at that moment), or at rascontrol "exit" to a rescue file -- PB 2003-jun-06
#ifdef NEVER_AGAIN
		if(!config.saveConfigFile())
		{
			std::cout<<"Error saving configuration file."<<std::endl;
		}

		if(!authorization.saveAuthFile())
		{
			std::cout<<"Error saving user authorization file."<<std::endl;
		}
#endif
	}
		
	else if(config.isTestModus())
	{
		hostmanager.insertInternalHost();
		userManager.loadDefaults();
		masterCommunicator.Run();
	}
	
	cout <<"rasmgr terminated."<<std::endl;

	int retval = RASMGR_RESULT_OK;
	LEAVE( "main: leave. retval=" << retval );
	return retval;
} // main()

// danger: cout in interrupt???   
// handler for SIGINT and SIGTERM = call for exit
void SigIntHandler(int sig)
{
	std::cout<<"rasmgr received terminate signal...";
	masterCommunicator.shouldExit();
}
   
void installSignalHandlers()   
{
   signal (SIGINT, SigIntHandler);
   signal (SIGTERM, SigIntHandler);
   signal (SIGHUP, SIG_IGN);
   signal (SIGPIPE,SIG_IGN);
   signal (SIGTTOU,SIG_IGN); // no console, ei si?
}
   
// should be replaced by something cleaner, eventually 
void exitbyerror(const char* text)
{
	perror(text);
	exit( RASMGR_EXIT_FAILURE );
}

char *strtolwr(char *string)// should be somewhere in the C-library, but can't find it
{
	char *t=string;
	for(;*t;t++)
	{
		 if(*t>='A' && *t<='Z') *t|='a'-'A';
	}
	return string;  
}