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 =======
|
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 #if defined(PEGASUS_OS_TYPE_WINDOWS)
84 # include "cimserver_windows.cpp"
85 #elif defined(PEGASUS_OS_TYPE_UNIX)
86 # include "cimserver_unix.cpp"
87 #else
88 # error "Unsupported platform"
89 #endif
90
|
91 mike 1.8 PEGASUS_USING_PEGASUS;
92 PEGASUS_USING_STD;
|
93 mike 1.1
94 void GetEnvironmentVariables(
95 const char* arg0,
96 String& pegasusHome)
97 {
98 // Get environment variables:
99
100 const char* tmp = getenv("PEGASUS_HOME");
101
102 if (!tmp)
103 {
104 cerr << arg0 << ": PEGASUS_HOME environment variable undefined" << endl;
105 exit(1);
106 }
107
108 pegasusHome = tmp;
109 FileSystem::translateSlashes(pegasusHome);
110 }
111
|
112 karl 1.13 /** GetOptions function - This function defines the Options Table
113 and sets up the options from that table using the option manager.
114 */
|
115 mike 1.1 void GetOptions(
116 OptionManager& om,
|
117 karl 1.13 int& argc,
118 char** argv,
|
119 mike 1.1 const String& pegasusHome)
120 {
|
121 karl 1.14 static struct OptionRow optionsTable[] =
|
122 mike 1.1 {
|
123 karl 1.14 {"port", "8888", false, Option::WHOLE_NUMBER, 0, 0, "port",
124 "specifies port number to listen on" },
125 {"trace", "false", false, Option::BOOLEAN, 0, 0, "t",
126 "turns on trace of Client IO to console "},
127 {"logtrace", "false", false, Option::BOOLEAN, 0, 0, "l",
128 "Turns on trace of Client IO to trace log "},
129 {"options", "false", false, Option::BOOLEAN, 0, 0, "options",
130 " Displays the settings of the Options "},
131 {"severity", "ALL", false, Option::STRING, 0, 0, "s",
|
132 mday 1.17
|
133 karl 1.14 "Sets the severity level that will be logged "},
134 {"logs", "ALL", false, Option::STRING, 0, 0, "X",
135 "Not Used "},
|
136 mday 1.17 {"daemon", "false", false, Option::BOOLEAN, 0, 0, "d",
137 "Detach Pegasus from the console and run it in the background "},
|
138 karl 1.15 {"logdir", "./logs", false, Option::STRING, 0, 0, "logdir",
139 "Directory for log files"},
|
140 karl 1.16 {"cleanlogs", "false", false, Option::BOOLEAN, 0, 0, "clean",
|
141 karl 1.15 "Clears the log files at startup"},
142 {"daemon", "false", false, Option::BOOLEAN, 0, 0, "d",
|
143 karl 1.14 "Not Used "},
144 {"version", "false", false, Option::BOOLEAN, 0, 0, "v",
145 "Displays Pegasus Version "},
146 {"help", "false", false, Option::BOOLEAN, 0, 0, "h",
147 "Prints help message with command line options "},
|
148 mday 1.17 {"install", "false", false, Option::BOOLEAN, 0, 0, "install",
149 "Installs Pegasus as a Windows NT Service "},
150 {"remove", "false", false, Option::BOOLEAN, 0, 0, "remove",
151 "Removes Pegasus as a Windows NT Service "},
|
152 karl 1.14 {"debug", "false", false, Option::BOOLEAN, 0, 0, "d",
153 "Not Used "}
|
154 mike 1.1 };
|
155 karl 1.14 const Uint32 NUM_OPTIONS = sizeof(optionsTable) / sizeof(optionsTable[0]);
|
156 mike 1.1
|
157 karl 1.14 om.registerOptions(optionsTable, NUM_OPTIONS);
|
158 mike 1.1
159 String configFile = pegasusHome + "/cimserver.conf";
160
|
161 karl 1.14 cout << "Config file from " << configFile << endl;
162
|
163 mike 1.3 if (FileSystem::exists(configFile))
164 om.mergeFile(configFile);
|
165 mday 1.17 if(argc && argv != NULL)
166 om.mergeCommandLine(argc, argv);
|
167 mike 1.1
168 om.checkRequiredOptions();
169 }
170
|
171 karl 1.13 /* PrintHelp - This is temporary until we expand the options manager to allow
172 options help to be defined with the OptionRow entries and presented from
173 those entries.
174 */
|
175 mike 1.2 void PrintHelp(const char* arg0)
176 {
177 cout << '\n';
|
178 karl 1.6 cout << PEGASUS_NAME << PEGASUS_VERSION << endl;
|
179 mike 1.2 cout << '\n';
|
180 karl 1.14 cout << "Usage: " << arg0 << endl;
|
181 mike 1.2 cout << endl;
182 }
183
|
184 karl 1.13 //////////////////////////////////////////////////////////////////////////
185 // MAIN
186 //////////////////////////////////////////////////////////////////////////
|
187 mike 1.1 int main(int argc, char** argv)
188 {
|
189 mday 1.17
190 // on Windows NT if there are no command-line options, run as a service
191 if (argc == 1 )
192 cim_server_service(argc, argv) ;
193
|
194 mike 1.1 // Get environment variables:
195
196 String pegasusHome;
|
197 mday 1.17
|
198 mike 1.1 GetEnvironmentVariables(argv[0], pegasusHome);
199
|
200 karl 1.13 // Get options (from command line and from configuration file); this
|
201 mike 1.1 // removes corresponding options and their arguments fromt he command
202 // line.
203
204 OptionManager om;
205
206 try
207 {
208 GetOptions(om, argc, argv, pegasusHome);
209 // om.print();
210 }
211 catch (Exception& e)
212 {
213 cerr << argv[0] << ": " << e.getMessage() << endl;
214 exit(1);
215 }
216
217 // At this point, all options should have been extracted; print an
218 // error if there are any remaining:
219
220 if (argc != 1)
221 {
222 mike 1.1 cerr << argv[0] << ": unrecognized options: ";
223
|
224 mike 1.4 for (int i = 1; i < argc; i++)
|
225 mike 1.1 cerr << argv[i] << ' ';
226 cout << endl;
227 exit(1);
228 }
229
|
230 mday 1.17 // Check to see if we should (can) install as a NT service
231
232 String installOption;
233 if(om.lookupValue("install", installOption) && installOption == "true")
234 {
235 if( 0 != cimserver_install_nt_service( pegasusHome ))
236 cout << "\nPegasus installed as NT Service";
237 exit(0);
238 }
239
240 // Check to see if we should (can) remove Pegasus as an NT service
241
242 String removeOption;
243 if(om.lookupValue("remove", removeOption) && removeOption == "true")
244 {
245 if( 0 != cimserver_remove_nt_service() )
246 cout << "\nPegasus removed as NT Service";
247 exit(0);
248
249 }
250
|
251 mike 1.1 // Check to see if user asked for the version (-v otpion):
252
253 String versionOption;
254
255 if (om.lookupValue("version", versionOption) && versionOption == "true")
256 {
257 cerr << PEGASUS_VERSION << endl;
258 exit(0);
259 }
260
261 // Check to see if user asked for help (-h otpion):
262 String helpOption;
263
264 if (om.lookupValue("help", helpOption) && helpOption == "true")
265 {
|
266 mike 1.2 PrintHelp(argv[0]);
|
267 karl 1.14 om.printHelp();
|
268 mike 1.1 exit(0);
269 }
270
|
271 karl 1.9 // Check the trace options and set global variable
|
272 karl 1.13 Boolean pegasusIOTrace = false;
|
273 karl 1.9 if (om.valueEquals("trace", "true"))
274 {
|
275 karl 1.16 Handler::setMessageTrace(true);
276 pegasusIOTrace = true;
|
277 karl 1.9 }
|
278 karl 1.13
|
279 karl 1.14 Boolean pegasusIOLog = false;
280 if (om.valueEquals("logtrace", "true"))
281 {
282 Handler::setMessageLogTrace(true);
|
283 karl 1.16 pegasusIOLog = true;
|
284 karl 1.14 }
285
|
286 mike 1.1 // Grab the port otpion:
287
288 String portOption;
289 om.lookupValue("port", portOption);
290
|
291 karl 1.15 // Get the log file directory definition.
292 // We put String into Cstring because
293 // Directory functions only handle Cstring.
294 // ATTN-KS: create String based directory functions.
295 String logsDirectory;
296 om.lookupValue("logdir", logsDirectory);
297
|
298 karl 1.16 // Set up the Logger. This does not open the logs
299 // Might be more logical to clean before set.
300 // ATTN: Need tool to completely disable logging.
301 Logger::setHomeDirectory(logsDirectory);
302
303 if (om.valueEquals("cleanlogs", "true"))
304 {
305 Logger::clean(logsDirectory);;
306 }
|
307 karl 1.15
|
308 karl 1.16 // Leave this in until people get familiar with the logs.
|
309 karl 1.15 cout << "Logs Directory = " << logsDirectory << endl;
310
311
|
312 karl 1.13 char* address = portOption.allocateCString();
313
314 // Put out startup up message.
315 cout << PEGASUS_NAME << PEGASUS_VERSION <<
316 " on port " << address << endl;
317 cout << "Built " << __DATE__ << " " << __TIME__ << endl;
318 cout <<"Started..."
|
319 karl 1.14 << (pegasusIOTrace ? " Tracing to Display ": " ")
320 << (pegasusIOLog ? " Tracing to Log ": " ")
321 << endl;
322
323 // Option to Display the options table. Primarily
324 // a diagnostic tool.
325 if (om.valueEquals("options", "true"))
326 om.print();
|
327 karl 1.13
328 // Put server start message to the logger
329 Logger::put(Logger::STANDARD_LOG, "CIMServer", Logger::INFORMATION,
|
330 karl 1.16 "Start $0 $1 port $2 $3 ",
331 PEGASUS_NAME,
332 PEGASUS_VERSION,
333 address,
334 (pegasusIOTrace ? " Tracing": " "));
|
335 karl 1.13
|
336 mday 1.17 // do we need to run as a daemon ?
337 String daemonOption;
338 if(om.lookupValue("daemon", daemonOption) && daemonOption == "true")
339 {
340 if(-1 == cimserver_fork())
341 exit(-1);
342 }
343
344
|
345 karl 1.13 // try loop to bind the address, and run the server
|
346 mike 1.1 try
347 {
348 Selector selector;
349 CIMServer server(&selector, pegasusHome);
|
350 karl 1.6
|
351 karl 1.15 // bind throws an exception of the bind fails
|
352 mike 1.1 server.bind(address);
353 delete [] address;
354 server.runForever();
|
355 karl 1.16
356 Logger::put(Logger::STANDARD_LOG, "CIMServer", Logger::INFORMATION,
357 "Normal Termination");
358
|
359 mike 1.1 }
360 catch(Exception& e)
361 {
|
362 karl 1.16 Logger::put(Logger::STANDARD_LOG, "CIMServer", Logger::INFORMATION,
363 "Abnormal Termination $0", e.getMessage());
364
|
365 mike 1.8 PEGASUS_STD(cerr) << "Error: " << e.getMessage() << PEGASUS_STD(endl);
|
366 mike 1.1 }
367
368 return 0;
369 }
|
370 mday 1.17
|