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