summaryrefslogtreecommitdiffstats
path: root/rasmgr/rasmgr_config.cc
blob: 6e317a78deb60203a6be0f11d3f20028e278714d (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*
* 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_config.cc
 *
 * MODULE: rasmgr
 * CLASS:  Configuration, RasmgrLicense
 *
 * PURPOSE:
 *   Config info from commandline, environment and license
 *    
 * COMMENTS:
 *   none
 *
*/

using namespace std;

#include "globals.hh"		// DEFAULT_HOSTNAME, RMANHOME_VAR, DEFAULT_PORT, RASMGR_CONF_FILE

#include "rasmgr_config.hh"
#include "rasmgr.hh"
#include "rasmgr_host.hh"
#include "rasmgr_dbm.hh"
#include "rasmgr_srv.hh"
#include "ras_crypto.hh"
#include "rasmgr_users.hh"
#include "rasmgr_comm.hh"
#include "rasmgr_rascontrol.hh"

#include <stdlib.h>		// mkstemp()
#include <iomanip>
#include <time.h>

#include "debug.hh"

extern bool hostCmp( const char *h1, const char *h2);


Configuration::Configuration():
	cmlInter      (CommandLineParser::getInstance()),
	cmlName       (cmlInter.addStringParameter(CommandLineParser::noShortName, "name", "<name> symbolic name of this rasmgr (slave only, default: the host name)")),
	cmlHostName   (cmlInter.addStringParameter(CommandLineParser::noShortName, "hostname", "<name> the advertized host name (master only, default: same as UNIX command 'hostname')")),
	cmlPort       (cmlInter.addLongParameter(CommandLineParser::noShortName, "port", "<port> listen port number", DEFAULT_PORT)),
	cmlPollFrequ  (cmlInter.addLongParameter(CommandLineParser::noShortName, "poll", "<poll> polling timeout (in seconds) for rasmgr listen port", DEFAULT_POLLING_FREQUENCY )),
	cmlMaster     (cmlInter.addStringParameter(CommandLineParser::noShortName, "master", "<name> host of rasmgr master (slave only)")), 
	cmlMasterPort (cmlInter.addLongParameter(CommandLineParser::noShortName, "mport", "<port> listen port number of rasmgr master (slave only)", DEFAULT_PORT)),
	cmlQuiet      (cmlInter.addFlagParameter( 'q', CommandLineParser::noLongName, "quiet: don't log requests (default: log requests to stdout)")),
#ifdef NO_OFFICIAL_RELEASE          
	cmlTest       (cmlInter.addFlagParameter(CommandLineParser::noShortName, "test", "test mode")), 
	cmlDSup       (cmlInter.addFlagParameter(CommandLineParser::noShortName, "dsup", "debug mode")), 
	cmlRandTest   (cmlInter.addFlagParameter(CommandLineParser::noShortName, "rgt",  "random generator test")), 
	cmlRth        (cmlInter.addFlagParameter(CommandLineParser::noShortName, "rth", "disable rthl test")),
	cmlMultiWT    (cmlInter.addFlagParameter(CommandLineParser::noShortName, "amw", "allow multiple write transactions")),
#endif	      	
	cmlHelp       (cmlInter.addFlagParameter('h', RASMGRCMD_HELP, "print this help"))
{
	ENTER( "Configuration::Configuration: enter." );

	int ghnResult = gethostname(hostName, sizeof(hostName) );
	if (ghnResult != 0)	// cannot get hostname?
	{
		int ghnErrno = errno;
		std::cout << "Error: cannot get hostname of my machine: error " << ghnErrno << "; will use '" << DEFAULT_HOSTNAME << "' as heuristic." << endl;
		strcpy( hostName, DEFAULT_HOSTNAME );
	}
	strcpy(slaveName,hostName);
	strcpy(publicHostName,hostName);
	listenPort=DEFAULT_PORT; 
	masterPort=DEFAULT_PORT;
	masterName[0]=0;

	pollFrequency = DEFAULT_POLLING_FREQUENCY;

	configFileName[0]=0;
	slave = false;

	if (sizeof(configFileName) < strlen(CONFDIR) + strlen(RASMGR_CONF_FILE) + 2)
	{
		std::cout << "Error: configuration path length exceeds system limits: '" << CONFDIR << "/" << RASMGR_CONF_FILE << "'" << endl;
		LEAVE( "Configuration::Configuration: leave." );
		return;
	}
	sprintf( configFileName, "%s/%s", CONFDIR, RASMGR_CONF_FILE );
	altConfigFileName[0] = '\0';
 
	testModus    = false;
	debugSupport = false;
	rtHlTest     = true;  // by default RasMgr tests at runtime if it's the only one 
	allowMultiWT = false; // rasmgr doesn't allow multiple write transactions for a db

	LEAVE( "Configuration::Configuration: leave." );
}
   
bool Configuration::readConfigFile()
{
	//insert internal host, it's not in config file
	hostmanager.insertInternalHost();

	char inBuffer[MAXMSG];
	char outBuffer[MAXMSG];
	bool result = true;
	bool fileIsOpen = false;
	
	ENTER( "Configuration::readConfigFile: enter. Looking for config file " << configFileName );

	VLOG << "Inspecting config file " << configFileName << "...";

	std::ifstream ifs(configFileName);		// open config file
	
	if(ifs)
		fileIsOpen = true;
	else
	{
		TALK( "Configuration::readConfigFile: cannot open config file." );
		std::cout << "Warning: cannot open config file " << configFileName << endl;
		fileIsOpen = false;
	}
	result = true;				// was: false, but I want to allow a missing file

	if (fileIsOpen)
	{
		authorization.startConfigFile();
	
		while( ! ifs.eof() )		// was: while(1), I simplified this
						// processRequest() will get an additional empty line at eof, but this is harmless
		{
			ifs.getline(inBuffer,MAXMSG);
			// if(!strlen(inBuffer) && ifs.eof())	// FIXME: what happens if last line in file is empty?
			// {
			// TALK( "Configuration::readConfigFile: strlen(inBuffer)=" << strlen(inBuffer) << ", eof=" << ifs.eof());
			// break;
			// }
	
			TALK( "Configuration::readConfigFile: processing line: " << inBuffer );
		 	rascontrol.processRequest(inBuffer,outBuffer);
		}
	
		authorization.endConfigFile();

		ifs.close();				// close config file handle
	}

	if (result == true && fileIsOpen)
		VLOG << "ok" << endl;

	LEAVE( "Configuration::readConfigFile: leave. result=" << result );
	return true;		
}

// return name of alternate config file;
// takes value from preceding saveAltConfigFile() call.
const char *Configuration::getAltConfigFileName()
{
	return altConfigFileName;
}

// in future this is not used directly, but through saveOrigConfigFile() and saveAltConfigFile() wrappers below
bool Configuration::saveConfigFile()
{
	ENTER( "Configuration::saveConfigFile: enter." );

	std::ofstream ofs(configFileName);
	if(!ofs)
	{
		LEAVE( "Configuration::saveConfigFile: leave. cannot open config file " << configFileName << " for writing." );
		return false;
	}

	ofs << "# rasmgr config file (v1.1)" << std::endl;
	ofs << "# warning: do not edit this file, it may be overwritten by rasmgr!" << std::endl;
	ofs << "#" << std::endl;
	
	int i;
	//serverhosts
	for(i=0;i<hostmanager.countHosts();i++)
	{
		ServerHost &xx=hostmanager[i];
	
		if(i > 0)
			ofs<<"define host "<<xx.getName()<<" -net "<<xx.getNetworkName()<<" -port "<<xx.getListenPort()<<std::endl;
		else
		{
			//by default the master RasMgr is init with the hostname as name => if we have to we change it here
			if( ! hostCmp(xx.getName(),config.getHostName()) )
				ofs << "change host "<<config.getHostName()<<" -name "<<xx.getName()<<std::endl;	

			if(xx.useLocalHost()==false)
				ofs<<"change host "<<xx.getName()<<" -uselocalhost off"<<std::endl;
		}
	}
	//databaseHosts
	for(i=0;i<dbHostManager.countHosts();i++)
	{
		DatabaseHost &xx=dbHostManager[i];
		ofs<<"define dbh "<<xx.getName()<<" -connect "<<xx.getConnectionString()<<std::endl;
	}
	//rasservers
	for(i=0;i<rasManager.countServers();i++)
	{
		RasServer &xx=rasManager[i];
	
		ofs<<"define srv "<<xx.getName()<<" -host "<<xx.getHostName()<<" -type "<<xx.getType();
		if(xx.getType()==SERVERTYPE_FLAG_HTTP)
			ofs<<" -port "<<xx.getPort();
		else
			ofs<<" -port 0x"<<std::hex<<xx.getPort()<<std::dec;
		if(xx.isConnectedToDBHost())
			ofs<<" -dbh "<<xx.getDBHostName();
		ofs<<std::endl;
	
		ofs<<"change srv "<<xx.getName()<<" -countdown "<<xx.getCountDown();
	
		if(strcmp(xx.getExecutableName(),RASEXECUTABLE))
			ofs<<" -exec "<<xx.getExecutableName();
		  
		ofs<<" -autorestart "<<(xx.isAutoRestart() ? "on":"off")<<" -xp "<<xx.getExtraParams()<<std::endl;      
	}
		 
	//databases
	for(i=0;i<dbManager.countDatabases();i++)
	{
		Database &xx=dbManager[i];
	
		for(int j=0;j<xx.countConnectionsToDBHosts();j++)
		{
			ofs<<"define db "<<xx.getName()<<" -dbh "<<xx.getDBHostName(j)<<std::endl;
		} 
	}

	ofs.close();		// this was missing, therefore sometimes the config file was cleared -- PB 2003-jun-06
	LEAVE( "Configuration::saveConfigFile: leave." );
	return true;
} // saveConfigFile()

// save config file at original place, i.e., under the name of configFile
bool Configuration::saveOrigConfigFile()
{
	ENTER( "Configuration::saveOrigConfigFile: enter." );

	bool result = saveConfigFile();

	LEAVE( "Configuration::saveOrigAltConfigFile: leave. result=" << result );
	return result;
}

// save configuration file in another file, same dir as config file
bool Configuration::saveAltConfigFile()
{
	ENTER( "Configuration::saveAltConfigFile()" );

	bool result = true;
	char origFileName[ sizeof(configFileName) ];        // temp copy of origFileName

	// save original file name
	(void) strcpy( origFileName, configFileName );

	// build temp file by appending a unique string
	(void) strcpy( altConfigFileName, configFileName );
	(void) strcat( altConfigFileName, ".XXXXXX" );       // 6 * 'X', see man mkstemp()

	int altFile = mkstemp( altConfigFileName );         // replaces the Xs by unique string
	if (altFile < 0)                                    // error in creating file name
	{
		int tempError = errno;
		TALK( "Configuration::saveAltConfigFile: error creating alternate file name: " << strerror(tempError) );
		result = false;
	}

	if (result == true)
	{
		// now we have a valid + open file, but we can't use it like that, because we open down below.
		// so close it again, being happy that we have a valid file name. bad hack, though.
		int closeResult = close( altFile );
		if (closeResult != 0)
			TALK( "Configuration::saveAltConfigFile: error in temporary closing file, ignoring that." );
	}

	if (result == true)
	{
		(void) strcpy( configFileName, altConfigFileName );	// set file to be written to alternate name
		result = saveConfigFile();                      // save file, name has been substituted successfully
		(void) strcpy( configFileName, origFileName );	// restore original config file name
	}

	LEAVE( "Configuration::saveAltConfigFile: leave. result=" << result );
	return result;
}

bool Configuration::interpretArguments(int argc, char **argv, char **envp)
{
	ENTER( "Configuration::interpretArguments: enter." );

	bool result = true;
	//errorCode=0;
	
	//process command line
	try
	{
		cmlInter.processCommandLine(argc, argv);
	}
	catch(CmlException& err)
	{
		std::cout << "Error parsing command line: " << err.what() << std::endl;
		printHelp();
		result = false;
	}
   
	SET_OUTPUT( true );			// by default, enable trace (debug) output
	verbose = true;			// by default, be verbose
	if( (result==true) && cmlQuiet.isPresent() )
	{
		SET_OUTPUT( false );			// disable trace (debug) output
		// debugOutput = true;			// done via the above macro
		verbose = false;			// only minimum messages
		result = true;
	} 
	
	if( (result==true) && cmlHelp.isPresent() )
	{
		printHelp();
		result = false;
	} 
	
	if( (result==true) && cmlHostName.isPresent() )
	{
		if (sizeof(hostName) > strlen(cmlHostName.getValueAsString()))
			strcpy(publicHostName,cmlHostName.getValueAsString());
		else
		{
			VLOG << "Error: host name exceeds length limit of " << sizeof(hostName) << " characters." << std::endl;
			result = false;
		}
	}
		 
	if( (result==true) && cmlPort.isPresent() )
	{
		try 
		{
			listenPort = cmlPort.getValueAsLong();
		}
		catch(CmlException& err) 
		{
			VLOG << "Error converting port parameter " << cmlPort.getLongName() << " to integer: " << err.what() << std::endl;
			result = false;
		}           
	}
		 
	if( (result==true) && cmlMaster.isPresent() )
	{
	        slave = true;
		strcpy(masterName,cmlMaster.getValueAsString());
	}
	
	if( (result==true) && cmlMasterPort.isPresent() )
	{
		try 
		{
			masterPort = cmlMasterPort.getValueAsLong();
		}
		catch(CmlException& err) 
		{
			VLOG << "Error converting " << cmlMasterPort.getLongName() << " to integer: " << err.what() << std::endl;
			result = false;
		}           
	}

	if( (result==true) && cmlPollFrequ.isPresent() )
	{
		try
		{
			pollFrequency = cmlPollFrequ.getValueAsLong();
		}
		catch(CmlException& err)
		{
			VLOG << "Error converting " << cmlPollFrequ.getLongName() << " to integer: " << err.what() << std::endl;
			result = false;
		}
		if (result == true && pollFrequency <= 0)
		{
			VLOG << "Error: poll frequency must be a positive integer." << std::endl;
			result = false;
		}
	}

	if( (result==true) && cmlName.isPresent() )
	{
		if (sizeof(slaveName) > strlen(cmlName.getValueAsString()))
			strcpy(slaveName,cmlName.getValueAsString());
		else
		{
		      VLOG << "Error: slave name exceeds length limit of " << sizeof(slaveName) << " characters." << std::endl;
		      result = false;
		}
	}       
		 
#ifdef NO_OFFICIAL_RELEASE
	testModus = cmlTest.isPresent();
	debugSupport=cmlDSup.isPresent();
	rtHlTest=cmlRth.isPresent();

	if( (result==true) && cmlRandTest.isPresent() )
	{
		std::cout<<"Random generator test..."<<(randomGenerator.insideTest() ? "PASSED":"FAILED" )<<std::endl;
		result = false;
	}
	
	allowMultiWT = cmlMultiWT.isPresent();

#endif       

	LEAVE( "Configuration::interpretArguments: leave. result=" << result );
	return result;   
}
	
bool Configuration::allowMultipleWriteTransactions()
{
	return allowMultiWT;
}

bool Configuration::isDebugSupport()
{ 
	return debugSupport;
}
	
bool Configuration::isVerbose()
{
	return verbose;
}

bool Configuration::isTestModus()
{
	return testModus;
}
   
const char* Configuration::getHostName()
{
	return hostName;
}

const char* Configuration::getPublicHostName()
{
	return publicHostName;
}

int Configuration::getListenPort()
{
	return listenPort;
}

const char* Configuration::getMasterName()
{
	return masterName;
}
int Configuration::getMasterPort()
{
	return masterPort; 
}

int Configuration::getPollFrequency()
{
	return pollFrequency;
}

const char* Configuration::getSlaveName()
{
	return slaveName[0] ? slaveName:hostName;
}

   
void Configuration::printHelp()
{
	std::cout << "Usage: rasmgr [options]" << std::endl;
	std::cout << "Options:" << std::endl;
	cmlInter.printHelp();
	std::cout << std::endl;
	return;
}

void Configuration::printStatus()
{
	std::cout << "rasmgr configuration parameter settings:" << std::endl;
	std::cout << "   symb name   = " << publicHostName << std::endl;
	std::cout << "   hostname    = " << publicHostName << std::endl;
	std::cout << "   port        = " << listenPort     << std::endl;
	std::cout << "   poll        = " << pollFrequency  << std::endl;
	std::cout << "   quiet     = "   << (!verbose) << std::endl;
	return;
}

//****************************************************************************************************************

// FIXME: this needs refinement -- PB 2003-jun-09
BenchmarkTimer::BenchmarkTimer(const char *text)
{
	this->text = (char*)text;
	time_t now = time(NULL);
	char *n = ctime(&now);
	n[strlen(n)-1] = '\0';		// delete trailing newline char
	// cout << "--- rasmgr timer start for \'" << text << "\' at " << n << "." << endl;    
	gettimeofday(&start,NULL);
}
   
BenchmarkTimer::~BenchmarkTimer()
{
}
   
void BenchmarkTimer::result()
{
	timeval result;
	gettimeofday(&end,NULL);
	int r=timeval_subtract(&result,&end,&start);
	time_t now = time(NULL);
	char *n = ctime(&now);
	n[strlen(n)-1] = '\0';		// delete trailing newline char
	VLOG << "rasmgr timer stop for \'" << text << "\' at " << n << ": " << result.tv_sec << '.' << std::setw(3) << std::setfill('0') << result.tv_usec << " seconds elapsed." << endl;    
}

int BenchmarkTimer::timeval_subtract(timeval *result,timeval *x,timeval *y)
{
#define ONE_MILLION 1000000
	/* Perform the carry for the later subtraction by updating Y. */
	if (x->tv_usec < y->tv_usec)
	{
		int nsec = (y->tv_usec - x->tv_usec) / ONE_MILLION + 1;
		y->tv_usec -= ONE_MILLION * nsec;
		y->tv_sec += nsec;
	}
	if (x->tv_usec - y->tv_usec > ONE_MILLION)
	{
		int nsec = (x->tv_usec - y->tv_usec) / ONE_MILLION;
		y->tv_usec += ONE_MILLION * nsec;
		y->tv_sec -= nsec;
	}
	 
	/* Compute the time remaining to wait.
	   `tv_usec' is certainly positive. */
	result->tv_sec = x->tv_sec - y->tv_sec;
	result->tv_usec = x->tv_usec - y->tv_usec;
	 
	/* Return 1 if result is negative. */
	return x->tv_sec < y->tv_sec;
}

//####################################
	/*/
	
	//read-pacaleala
	hostmanager.insertNewHost("Ras1","martini",8081);
	hostmanager.insertNewHost("Ras2","coconutkiss",8082);
	hostmanager.insertNewHost("Ras3","tequilla",8082);

	dbHostManager.insertNewHost("julep",     "rasdec/rasdec@julep.akglocal.de");    
	dbHostManager.insertNewHost("pinacolada","rasdec/rasdec@pinacolada.akglocal.de");    

	dbManager.insertNewDatabase("RasDemo");
		dbManager[0].connectToDBHost("julep");
		dbManager[0].connectToDBHost("pinacolada");

	dbManager.insertNewDatabase("BLVA");
		dbManager[1].connectToDBHost("pinacolada");
	
	rasManager.insertNewServer("RasServer11","Ras1",SERVERTYPE_FLAG_RPC,0x29999998);
		rasManager[0].connectToDBHost("julep");//,"\\");
	
	rasManager.insertNewServer("RasServer12","Ras1",SERVERTYPE_FLAG_RPC,0x29999998);      
		rasManager[1].connectToDBHost("pinacolada");//,"\\");

	rasManager.insertNewServer("RasServer21","Ras2",SERVERTYPE_FLAG_HTTP,8085);      
		rasManager[2].connectToDBHost("julep");//,"\\");

	rasManager.insertNewServer("RasServer31","Ras3",SERVERTYPE_FLAG_HTTP,8084);      
		rasManager[3].connectToDBHost("julep");//,"\\");
	
	rasManager.insertNewServer("RasServer00","RasMaster",SERVERTYPE_FLAG_HTTP,8083);      
		rasManager[4].connectToDBHost("julep");//,"\\");

	dbManager.insertNewDatabase("RasNew");
		dbManager[2].connectToDBHost("julep");
	 
	return true;
	// */