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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2