[go: up one dir, main page]

Menu

[r1170]: / clp / trunk / src / omcclp.cpp  Maximize  Restore  History

Download this file

480 lines (416 with data), 13.7 kB

  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
/****************************************************************************
|
| Copyright (c) 2006 Novell, Inc.
| All Rights Reserved.
|
| This program is free software; you can redistribute it and/or
| modify it under the terms of version 2 of the GNU General Public License as
| published by the Free Software Foundation.
|
| This program 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 this program; if not, contact Novell, Inc.
|
| To contact Novell about this file by physical or electronic mail,
| you may find current contact information at www.novell.com
|
|***************************************************************************
/**
@author Brad Nicholes
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "omcclp.h"
#include "omcclpc.h"
#include "omcclpprogram.h"
#include "omcclpcommand.h"
#include "omcclpcommon.h"
#include "omcclpcmdalias.h"
#include "omcclpdenv.h"
#include <readline/readline.h>
#include <readline/history.h>
#include <signal.h>
#include <pthread.h>
#include <openwbem/OW_Assertion.hpp>
#include <openwbem/OW_CmdLineParser.hpp>
#include <openwbem/OW_CerrLogger.hpp>
#include <openwbem/OW_Format.hpp>
#include <openwbem/OW_Format.hpp>
#include <openwbem/OW_Select.hpp>
using namespace OW_NAMESPACE;
using namespace OMCCLP;
void init_rl ();
UnnamedPipeRef sigPipe(UnnamedPipe::createUnnamedPipe());
enum tag_OPTIONS
{
E_CMDOPT_HELP,
E_CMDOPT_URL, // specify URL
E_CMDOPT_NS, // specify namespace
E_CMDOPT_CONFIG, // specify configuration file
E_CMDOPT_CMD, // specify a single command
E_CMDOPT_DEBUG, // specify a debug mode
};
CmdLineParser::Option g_options[] =
{
{E_CMDOPT_CMD, 'c', "command", CmdLineParser::E_REQUIRED_ARG, 0, "Execute a single command from the command line."},
{E_CMDOPT_CONFIG, 'f', "config", CmdLineParser::E_REQUIRED_ARG, 0, "Alternate configuration file. Default is omcclp.conf."},
{E_CMDOPT_HELP, 'h', "help", CmdLineParser::E_NO_ARG, 0, "Show help about options."},
{E_CMDOPT_NS, 'n', "ns", CmdLineParser::E_REQUIRED_ARG, 0, "Alternate CIM namespace to be used in place of the namespace specified in the configuration file"},
{E_CMDOPT_URL, 'u', "url", CmdLineParser::E_REQUIRED_ARG, 0, "Alternate CIM URL to be used in place of the URL specified in the configuration file."},
{E_CMDOPT_DEBUG, 'd', "debug", CmdLineParser::E_NO_ARG, 0, "Start the CLP application in debug mode."},
{0, 0, 0, CmdLineParser::E_NO_ARG, 0, 0}
};
OMCCLPProgram *gclp = NULL; // global pointer to the main program object
void usage()
{
cerr << PROG_NAME << " " << PROG_VERSION
<< " Server Management Command Line Protocol" << CLPENDL;
cerr << "Copyright (C) 2006 by Novell Inc." << CLPENDL;
cerr << "Usage: " << PROG_NAME << " [OPTIONS]" << CLPENDL;
cout << CLPENDL << CmdLineParser::getUsage(g_options) << CLPENDL;
}
void printCmdLineParserExceptionMessage(CmdLineParserException& e)
{
switch (e.getErrorCode())
{
case CmdLineParser::E_INVALID_OPTION:
cerr << "unknown option: " << e.getMessage() << CLPENDL;
break;
case CmdLineParser::E_MISSING_ARGUMENT:
cerr << "missing argument for option: " << e.getMessage() << CLPENDL;
break;
case CmdLineParser::E_INVALID_NON_OPTION_ARG:
cerr << "invalid non-option argument: " << e.getMessage() << CLPENDL;
break;
case CmdLineParser::E_MISSING_OPTION:
cerr << "missing required option: " << e.getMessage() << CLPENDL;
break;
default:
cerr << "failed parsing command line options: " << e << CLPENDL;
break;
}
}
namespace OMCCLP {
///////////////////////////////////////////////////////////
void OMCCLPServerThread::shutdown()
{
OW_LOG_DEBUG(m_logger, "OMCCLPServer::shuttingDown called...");
if(!m_running)
{
OW_LOG_INFO(m_logger, "OMCCLPServer::shuttingDown called, "
"but OMCCLPServer is not running");
return;
}
m_shuttingDown = true;
// Tell run method to wake up
m_sigPipe->writeInt(1);
OW_LOG_DEBUG(m_logger, "OMCCLPServer::shuttingDown waiting on thread barrier");
m_threadBarrier.wait();
OW_LOG_DEBUG(m_logger, "OMCCLPServer::shuttingDown shutdown complete");
}
/* This thread main function should be roughly
equivalent to the main() function in the
OMCCLPD.cpp file. */
Int32 OMCCLPServerThread::run()
{
int cc;
bool initialized = false;
OMCClpdEnvRef env = OMCClpdEnv::instance();
LoggerRef logger(new CerrLogger);
NonRecursiveMutexLock localLock(m_guard);
localLock.release();
m_sigPipe = env->getSigPipe();
try
{
if (m_data_filename.length())
env->setConfigItem(OMCClpdConfigOpts::CONFIG_FILE_opt,
m_data_filename);
env->init();
m_logger = env->getLogger(OMCClpdEnv::COMPONENT_NAME);
initialized = true;
}
catch (Exception& e)
{
cerr << "* EXCEPTION CAUGHT IN clp server startup!" << CLPENDL;
cerr << Format("* %1", e) << CLPENDL;
}
catch (std::exception& e)
{
cerr << "* EXCEPTION CAUGHT IN clp server startup!" << CLPENDL;
cerr << Format("* Message: %1", e.what()) << CLPENDL;
}
catch (...)
{
cerr << "* UNKNOWN EXCEPTION CAUGHT IN clpd MAIN!" << CLPENDL;
}
if (initialized)
{
OW_LOG_DEBUG(m_logger, "clpd Environment initialized");
try
{
int sig;
env->startServices();
m_running = true;
OW_LOG_INFO(m_logger, "clpd is now running");
m_condition.notifyAll();
while(!m_shuttingDown)
{
// runSelectEngine will only return once something has been put into
// the signal pipe or an error has happened
env->runSelectEngine();
if ((cc = sigPipe->readInt(&sig)) == -1)
{
continue;
}
OW_LOG_INFO(m_logger, "clp server received shutdown notification."
" Initiating shutdown.");
env->shutdown();
}
}
catch (Exception& e)
{
OW_LOG_FATAL_ERROR(m_logger, "* EXCEPTION CAUGHT IN clpd MAIN!");
OW_LOG_FATAL_ERROR(m_logger, Format("* %1", e));
}
catch (std::exception& e)
{
OW_LOG_FATAL_ERROR(m_logger, "* std::exception CAUGHT IN clpd MAIN!");
OW_LOG_FATAL_ERROR(m_logger, Format("* Message: %1", e.what()));
}
catch (...)
{
OW_LOG_FATAL_ERROR(m_logger, "* UNKNOWN EXCEPTION CAUGHT IN clpd MAIN!");
}
m_running = false;
OW_LOG_DEBUG(m_logger, "OMCCLPServer::run waiting to exit the CLP server thread");
m_threadBarrier.wait();
}
else
{
cerr << "Exiting CLP server thread" << CLPENDL;
m_condition.notifyAll();
return -1;
}
return 0;
}
///////////////////////////////////////////////////////////
void OMCCLPClientThread::shutdown()
{
if(!m_running)
{
return;
}
m_shuttingDown = true;
((OMCCLPClient*)gclp)->shutdown();
}
/* This thread main function should be roughly
equivalent to the main() function in the
OMCCLPC.cpp file. */
Int32 OMCCLPClientThread::run()
{
/*XXX Need to create and use a logger here
just like in the clp server thread.
The logger should be passed to the
OMCCLPClient object to be used there.*/
OMCCLPClient clp(cout);
int ret = 0;
// Set the global pointer to the program object so that
// it can be referenced by other objects.
gclp = &clp;
cout << "hello world" << CLPENDL;
/* Initialize the readline library */
init_rl();
try
{
if (clp.initializeCLPSession() &&
clp.createCLPSessionInfo(clp.getCIMClient()))
{
m_running = true;
StringArray cmdScripts = clp.getScriptFilenames();
if (!cmdScripts.empty())
{
for (int i=0; i < cmdScripts.size(); i++)
clp.run(cmdScripts[i]);
}
else
{
clp.run();
}
m_running = false;
}
}
catch (AssertionException& a)
{
cerr << "Caught Assertion: " << a << CLPENDL;
ret = 1;
}
catch (Exception& e)
{
cerr << e << CLPENDL;
ret = 1;
}
//cerr << "Exit recieved. Client returning" << CLPENDL;
/* Signal a shutdown */
sigPipe->writeInt(0);
return ret;
}
}
//////////////////////////////////////////////////////////////////////////////
void sigHandler(int sig)
{
if (sigPipe)
{
sigPipe->writeInt(sig);
}
}
//////////////////////////////////////////////////////////////////////////////
void
ignoreSignal(int sig)
{
signal(sig, SIG_IGN);
}
//////////////////////////////////////////////////////////////////////////////
void
handleSignal(int sig)
{
signal(sig, sigHandler);
}
//////////////////////////////////////////////////////////////////////////////
void
setupSigHandler(bool dbgFlg)
{
ignoreSignal(SIGINT);
handleSignal(SIGTERM);
handleSignal(SIGHUP);
ignoreSignal(SIGTTIN);
ignoreSignal(SIGTTOU);
ignoreSignal(SIGTSTP);
ignoreSignal(SIGPOLL);
ignoreSignal(SIGIO);
ignoreSignal(SIGPIPE);
ignoreSignal(SIGIOT);
ignoreSignal(SIGCONT);
ignoreSignal(SIGURG);
ignoreSignal(SIGXCPU);
ignoreSignal(SIGXFSZ);
ignoreSignal(SIGVTALRM);
ignoreSignal(SIGPROF);
ignoreSignal(SIGPWR);
}
/**
* Main program starting point
*/
int main (int argc, char *argv[])
{
Condition cond;
NonRecursiveMutex lock;
OMCCLPServerThread clpsvrt(cond, lock);
int cc, c;
cout << "hello world" << CLPENDL;
try
{
NonRecursiveMutexLock localLock(lock);
// parse command line, and build the urlList
CmdLineParser parser(argc, argv, g_options, CmdLineParser::E_NON_OPTION_ARGS_ALLOWED);
if (parser.isSet(E_CMDOPT_HELP))
{
usage();
return 0;
}
if (parser.isSet(E_CMDOPT_CONFIG))
{
String val = parser.getOptionValue(E_CMDOPT_CONFIG);
clpsvrt.setConfFilename(val);
}
else
clpsvrt.setConfFilename(OMCCLP_SYSCONF_DIR"/omcclp.conf");
if (parser.isSet(E_CMDOPT_DEBUG))
{
clpsvrt.setDebugMode(true);
}
/* Start the server and wait for it to initialize. */
clpsvrt.start();
cond.wait(localLock);
localLock.release();
if (clpsvrt.running())
{
OMCCLPClientThread clpt;
if (parser.isSet(E_CMDOPT_URL))
{
String val = parser.getOptionValue(E_CMDOPT_URL);
clpt.setCIMURL(val);
}
if (parser.isSet(E_CMDOPT_NS))
{
String val = parser.getOptionValue(E_CMDOPT_NS);
clpt.setCIMNamespace(val);
}
if (parser.isSet(E_CMDOPT_CONFIG))
{
String val = parser.getOptionValue(E_CMDOPT_CONFIG);
clpt.setConfFilename(val);
}
else
clpt.setConfFilename(OMCCLP_SYSCONF_DIR"/omcclp.conf");
if (parser.isSet(E_CMDOPT_CMD))
{
String val = parser.getOptionValue(E_CMDOPT_CMD);
clpt.setExecCmd(val);
}
clpt.setScriptFilenames(parser.getNonOptionArgs());
/* Start the client after the server has completed its startup. */
clpt.start();
setupSigHandler(true);
SelectTypeArray selArray;
selArray.push_back(sigPipe->getSelectObj());
sigPipe->setBlocking(UnnamedPipe::E_NONBLOCKING);
/* This select loop will only return if a signal
has been recieved and the sigpipe has been
written to. */
do {
Select::select(selArray);
if ((cc = sigPipe->readInt(&c)) == -1)
{
continue;
}
break;
} while (true);
/* Call the server and client shutdown to make sure
that everything has been cleaned up. */
clpt.shutdown();
clpsvrt.shutdown();
return 0;
}
}
catch (CmdLineParserException& e)
{
printCmdLineParserExceptionMessage(e);
usage();
}
catch (AssertionException& a)
{
cerr << "Caught Assertion: " << a << CLPENDL;
}
catch (CIMException& e)
{
cerr << "ERROR: CIMException:" << e.getMessage() << CLPENDL;
}
catch (Exception& e)
{
cerr << "ERROR: Exception:" << e.getMessage() << CLPENDL;
}
catch (std::exception& e)
{
cerr << "ERROR: sdtException:" << e.what() << CLPENDL;
}
catch (...)
{
cerr << "ERROR: UnknownException." << CLPENDL;
}
return 1;
}