(file) Return to cimserver.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Server

  1 mike  1.25 //%/////////////////////////////////////////////////////////////////////////////
  2            //
  3            // Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM
  4            //
  5            // Permission is hereby granted, free of charge, to any person obtaining a copy
  6            // of this software and associated documentation files (the "Software"), to 
  7            // deal in the Software without restriction, including without limitation the 
  8            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
  9            // sell copies of the Software, and to permit persons to whom the Software is
 10            // furnished to do so, subject to the following conditions:
 11            // 
 12            // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN 
 13            // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 14            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 15            // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 16            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 17            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
 18            // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 19            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 20            //
 21            //==============================================================================
 22 mike  1.25 //
 23            // Author: Mike Brasher (mbrasher@bmc.com)
 24            //
 25            // Modified By: Mike Day (mdday@us.ibm.com) 
 26            // =======
 27            // Modified By:	Karl Schopmeyer (k.schopmeyer@opengroup.org)
 28            //
 29            //%/////////////////////////////////////////////////////////////////////////////
 30            
 31            
 32            //////////////////////////////////////////////////////////////////////
 33            //
 34            // Notes on deamon operation (Unix) and service operation (Win 32):
 35            //
 36            // To run pegasus as a daemon on Unix platforms, use the -d option:
 37            //
 38            // cimserver -d
 39            //
 40            // The -d option has no effect on windows operation. 
 41            //
 42            // To run pegasus as an NT service, there are FOUR  different possibilities:
 43 mike  1.25 //
 44            // To INSTALL the Pegasus service, 
 45            //
 46            // cimserver -install
 47            //
 48            // To REMOVE the Pegasus service, 
 49            //
 50            // cimserver -remove
 51            //
 52            // To START the Pegasus service, 
 53            //
 54            // net start cimserver
 55            //
 56            // To STOP the Pegasus service, 
 57            //
 58            // net stop cimserver
 59            //
 60            // Alternatively, you can use the windows service manager. Pegasus shows up 
 61            // in the service database as "Pegasus CIM Object Manager"
 62            //
 63            // Mike Day, mdday@us.ibm.com
 64 mike  1.25 // 
 65            //////////////////////////////////////////////////////////////////////
 66            
 67            
 68            #include <iostream>
 69            #include <cstdlib>
 70            #include <Pegasus/Common/FileSystem.h>
 71            #include <Pegasus/Common/Selector.h>
 72            #include <Pegasus/Common/OptionManager.h>
 73            #include <Pegasus/Server/CIMServer.h>
 74            #include <Pegasus/Common/PegasusVersion.h>
 75            #include <Pegasus/Protocol/Handler.h>
 76            #include <Pegasus/Common/Logger.h>
 77            #include <Pegasus/Common/System.h>
 78 mday  1.28 #include <slp/slp.h>
 79 mike  1.25 
 80            
 81            
 82            #if defined(PEGASUS_OS_TYPE_WINDOWS)
 83            # include "cimserver_windows.cpp"
 84            #elif defined(PEGASUS_OS_TYPE_UNIX)
 85            # include "cimserver_unix.cpp"
 86            #else
 87            # error "Unsupported platform"
 88            #endif
 89            
 90            PEGASUS_USING_PEGASUS;
 91            PEGASUS_USING_STD;
 92            
 93            void GetEnvironmentVariables(
 94                const char* arg0,
 95                String& pegasusHome)
 96            {
 97                // Get environment variables:
 98            
 99                const char* tmp = getenv("PEGASUS_HOME");
100 mike  1.25 
101                if (!tmp)
102                {
103            	cerr << arg0 << ": PEGASUS_HOME environment variable undefined" << endl;
104            	exit(1);
105                }
106            
107                pegasusHome = tmp;
108                FileSystem::translateSlashes(pegasusHome);
109            }
110            
111            /** GetOptions function - This function defines the Options Table
112                and sets up the options from that table using the option manager.
113            */
114            void GetOptions(
115                OptionManager& om,
116                int& argc,
117                char** argv,
118                const String& pegasusHome)
119            {
120                static struct OptionRow optionsTable[] =
121 mike  1.25     {
122            	{"port", "5988", false, Option::WHOLE_NUMBER, 0, 0, "port",
123            			"specifies port number to listen on" },
124            	{"trace", "false", false, Option::BOOLEAN, 0, 0, "t", 
125            			"turns on trace of Client IO to console "},
126            	{"logtrace", "false", false, Option::BOOLEAN, 0, 0, "l",
127            			"Turns on trace of Client IO to trace log "},
128            	{"options", "false", false, Option::BOOLEAN, 0, 0, "options",
129            			" Displays the settings of the Options "},
130            	{"severity", "ALL", false, Option::STRING, 0, 0, "s",
131            
132            		    "Sets the severity level that will be logged "},
133            	{"logs", "ALL", false, Option::STRING, 0, 0, "X", 
134            			"Not Used "},
135            	{"daemon", "false", false, Option::BOOLEAN, 0, 0, "d", 
136            			"Detach Pegasus from the console and run it in the background "},
137            	{"logdir", "./logs", false, Option::STRING, 0, 0, "logdir", 
138            			"Directory for log files"},
139            	{"cleanlogs", "false", false, Option::BOOLEAN, 0, 0, "clean", 
140            			"Clears the log files at startup"},
141            	{"version", "false", false, Option::BOOLEAN, 0, 0, "v",
142 mike  1.25 			"Displays Pegasus Version "},
143            	{"help", "false", false, Option::BOOLEAN, 0, 0, "h",
144            		    "Prints help message with command line options "},
145            	{"install", "false", false, Option::BOOLEAN, 0, 0, "install",
146            		    "Installs Pegasus as a Windows NT Service "},
147            	{"remove", "false", false, Option::BOOLEAN, 0, 0, "remove",
148            		    "Removes Pegasus as a Windows NT Service "},
149            	{"debug", "false", false, Option::BOOLEAN, 0, 0, "d", 
150            	                "Not Used "},
151 karl  1.27 	{"slp", "false", false, Option::BOOLEAN, 0, 0, "slp", 
152 mike  1.25 			"Register Pegasus as a Service with SLP"}
153                };
154                const Uint32 NUM_OPTIONS = sizeof(optionsTable) / sizeof(optionsTable[0]);
155            
156                om.registerOptions(optionsTable, NUM_OPTIONS);
157            
158                String configFile = pegasusHome + "/testclient.conf";
159            
160                cout << "Config file from " << configFile << endl;
161            
162                if (FileSystem::exists(configFile))
163            	om.mergeFile(configFile);
164                if(argc && argv != NULL)
165                  om.mergeCommandLine(argc, argv);
166            
167                om.checkRequiredOptions();
168            }
169            
170            /* PrintHelp - This is temporary until we expand the options manager to allow
171               options help to be defined with the OptionRow entries and presented from
172               those entries.
173 mike  1.25 */
174            void PrintHelp(const char* arg0)
175            {
176                cout << '\n';
177                cout << PEGASUS_NAME << PEGASUS_VERSION << endl;
178                cout << '\n';
179                cout << "Usage: " << arg0 << endl;
180                cout << endl;
181            }
182            
183            //////////////////////////////////////////////////////////////////////////
184            //  MAIN
185            //////////////////////////////////////////////////////////////////////////
186            int main(int argc, char** argv)
187            {
188                // on Windows NT if there are no command-line options, run as a service
189            
190                if (argc == 1 )
191                  cim_server_service(argc, argv) ;
192              
193                // Get environment variables:
194 mike  1.25 
195                String pegasusHome;
196            
197                GetEnvironmentVariables(argv[0], pegasusHome);
198            
199                // Get options (from command line and from configuration file); this
200                // removes corresponding options and their arguments fromt he command
201                // line.
202            
203                OptionManager om;
204            
205                try
206                {
207            	GetOptions(om, argc, argv, pegasusHome);
208            	// om.print();
209                }
210                catch (Exception& e)
211                {
212            	cerr << argv[0] << ": " << e.getMessage() << endl;
213            	exit(1);
214                }
215 mike  1.25 
216                // At this point, all options should have been extracted; print an
217                // error if there are any remaining:
218            
219                if (argc != 1)
220                {
221            	cerr << argv[0] << ": unrecognized options: ";
222            
223            	for (int i = 1; i < argc; i++)
224            	    cerr << argv[i] << ' ';
225            	cout << endl;
226            	exit(1);
227                }
228            
229                // Check to see if we should (can) install as a NT service
230            
231                String installOption;
232                if(om.lookupValue("install", installOption) && installOption == "true")
233                  {
234            	if( 0 != cimserver_install_nt_service( pegasusHome ))
235            	  cout << "\nPegasus installed as NT Service";
236 mike  1.25 	exit(0);
237                  }
238            
239                // Check to see if we should (can) remove Pegasus as an NT service
240            
241                String removeOption;
242                if(om.lookupValue("remove", removeOption) && removeOption == "true")
243                  {
244            	if( 0 != cimserver_remove_nt_service() )
245            	  cout << "\nPegasus removed as NT Service";
246            	exit(0);
247            	  
248                  }
249            
250                // Check to see if user asked for the version (-v otpion):
251            
252                String versionOption;
253            
254                if (om.lookupValue("version", versionOption) && versionOption == "true")
255                {
256            	cerr << PEGASUS_VERSION << endl;
257 mike  1.25 	exit(0);
258                }
259            
260                // Check to see if user asked for help (-h otpion):
261                String helpOption;
262            
263                if (om.lookupValue("help", helpOption) && helpOption == "true")
264                {
265            	PrintHelp(argv[0]);
266            	om.printHelp();
267            	exit(0);
268                }
269            
270                // Check the trace options and set global variable
271                Boolean pegasusIOTrace = false;
272                if (om.valueEquals("trace", "true"))
273                {
274            	Handler::setMessageTrace(true);
275            	pegasusIOTrace = true;
276                }
277            
278 mike  1.25     Boolean pegasusIOLog = false;
279                if (om.valueEquals("logtrace", "true"))
280                {
281            	Handler::setMessageLogTrace(true);
282            	pegasusIOLog = true;
283                }
284                
285                // Grab the port otpion:
286            
287                String portOption;
288                om.lookupValue("port", portOption);
289            
290                // Get the log file directory definition.
291                // We put String into Cstring because
292                // Directory functions only handle Cstring.
293                // ATTN-KS: create String based directory functions.
294                String logsDirectory;
295                om.lookupValue("logdir", logsDirectory);
296            
297                // Set up the Logger. This does not open the logs
298                // Might be more logical to clean before set.
299 mike  1.25     // ATTN: Need tool to completely disable logging.
300                Logger::setHomeDirectory(logsDirectory);
301                
302                if (om.valueEquals("cleanlogs", "true"))
303                {
304            	Logger::clean(logsDirectory);;
305                }
306            
307 karl  1.27     // Option to Display the options table.  Primarily
308                // a diagnostic tool.
309                if (om.valueEquals("options", "true"))
310            	om.print();
311            
312 mike  1.25     // Leave this in until people get familiar with the logs.
313                cout << "Logs Directory = " << logsDirectory << endl;
314            
315 karl  1.27     Boolean useSLP = (om.valueEquals("slp", "true")) ? true: false;
316 mike  1.25 
317                char* address = portOption.allocateCString();
318            
319                // Put out startup up message.
320                cout << PEGASUS_NAME << PEGASUS_VERSION <<
321            	 " on port " << address << endl;
322                cout << "Built " << __DATE__ << " " << __TIME__ << endl;
323                cout <<"Started..."
324            	 << (pegasusIOTrace ? " Tracing to Display ": " ") 
325                     << (pegasusIOLog ? " Tracing to Log ": " ")
326 karl  1.27 	 << (useSLP ? " SLP reg. " : " No SLP ")
327 mike  1.25 	<< endl;
328            
329                // Put server start message to the logger
330                Logger::put(Logger::STANDARD_LOG, "CIMServer", Logger::INFORMATION,
331 karl  1.27 	"Start $0 $1 port $2 $3 $4",
332 mike  1.25 		PEGASUS_NAME, 
333            		PEGASUS_VERSION,
334            		address,
335 karl  1.26 		(pegasusIOTrace ? " Tracing": " "),
336            		(useSLP ? " SLP on " : " SLP off "));
337 mike  1.25 
338 karl  1.26 // do we need to run as a daemon ?
339 mike  1.25     String daemonOption;
340                if(om.lookupValue("daemon", daemonOption) && daemonOption == "true") 
341                  {
342            	if(-1 == cimserver_fork())
343            	  exit(-1);
344                  }
345            
346            
347                // try loop to bind the address, and run the server
348                try
349                {
350 mday  1.28         String serviceURL;
351            	serviceURL.assign("service:cim.pegasus://");
352            	char *host_name = slp_get_host_name();
353            	serviceURL += host_name;
354            	serviceURL += ":";
355            	serviceURL += address;
356            	char *url = serviceURL.allocateCString();
357            	free(host_name);
358            
359 mike  1.25 	Selector selector;
360            	CIMServer server(&selector, pegasusHome);
361            
362            	// bind throws an exception of the bind fails
363            	server.bind(address);
364            	delete [] address;
365 mday  1.28 
366            	slp_client *discovery = NULL;
367            	if(useSLP)
368            	  discovery = new slp_client();
369            
370            	time_t last = 0;
371            	while( 1 )
372            	{
373            	  if(useSLP  ) 
374            	  {
375            	    int success, failure;
376            	    
377            	    if(  (time(NULL) - last ) > 60 ) 
378            	    {
379            	      if( discovery != NULL && url != NULL )
380            		discovery->srv_reg_all(url,  
381            				       "(namespace=root/cimv20)",
382            				       "service:cim.pegasus", 
383            				       "DEFAULT", 
384            				       3600) ;
385            	      time(&last);
386 mday  1.28 	    }
387            	  }
388            	  server.runForever();
389            	}
390 mike  1.25 
391            	Logger::put(Logger::STANDARD_LOG, "CIMServer", Logger::INFORMATION,
392            	    "Normal Termination");
393                }
394                catch(Exception& e)
395                {
396            	Logger::put(Logger::STANDARD_LOG, "CIMServer", Logger::INFORMATION,
397            	    "Abnormal Termination $0", e.getMessage());
398            	
399            	PEGASUS_STD(cerr) << "Error: " << e.getMessage() << PEGASUS_STD(endl);
400                }
401            
402                return 0;
403            }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2