00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #if defined(_WIN32) && defined(_MSC_VER)
00025 # include <vos/applibs/vos_applibs_config-vc7.h>
00026 #else
00027 # include <vos/applibs/vos_applibs_config.h>
00028 #endif
00029
00030 #ifdef HAVE_GETOPT_LONG
00031 # define _GNU_SOURCE
00032 typedef struct option LongOptionStruct;
00033 #endif
00034
00035 #ifdef HAVE_GETOPT_H
00036 # include <getopt.h>
00037 #endif
00038
00039 #ifdef HAVE_UNISTD_H
00040 # include <unistd.h>
00041 #endif
00042
00043 #ifdef HAVE_SIGNAL
00044 #include <signal.h>
00045 #endif
00046
00047 #include <stdlib.h>
00048
00049 #include <iostream>
00050 #include <fstream>
00051
00052 #include <vos/metaobjects/property/property.hh>
00053
00054 #include "vosdaemon.hh"
00055
00056
00057 #if 0
00058
00059 VOSDaemon* _daemon = 0;
00060
00061 void _VOSDaemon_killsignal(int signal) {
00062 if(_daemon) {
00063 _daemon->save();
00064 delete _daemon;
00065 }
00066 exit(0);
00067 }
00068
00069 void _VOSDaemon_hupsignal(int signal) {
00070 if(_daemon)
00071 _daemon->save();
00072 }
00073
00074 void _VOSDaemon_autosave(void* something) {
00075 VOSDaemon* daemon = (VOSDaemon*)something;
00076 if(daemon) {
00077 daemon->save();
00078 }
00079 }
00080 #endif
00081
00082 VOSDaemon::~VOSDaemon() {
00083 fflush(stdout);
00084 fflush(stderr);
00085 if(logStream) {
00086 logStream->flush();
00087 delete logStream;
00088 }
00089 LOG(programName, 2, "goodbye.");
00090 }
00091
00092
00093
00094 VOSDaemon::VOSDaemon(char* progName, LocalSite* s) :
00095 saveFreq(0), rootObj(0), programName(progName), rootName(0),
00096 mayLoad(false), maySave(false), mayNameRoot(false),
00097 loadfile(0), dontFork(false)
00098 {
00099 int n = strlen("/var/log/") + strlen(programName) + 1;
00100 logFile = (char*) malloc(n * sizeof(char));
00101 snprintf(logFile, n, "/var/log/%s", programName);
00102 if(s)
00103 site = s;
00104 else {
00105 site = new LocalSocketSite(&NoAccessControl::static_);
00106 site->setTimeoutOnSelect(-1);
00107 }
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 void VOSDaemon::setLogFile(char* file) {
00145 logFile = file;
00146 }
00147
00148 void VOSDaemon::setLogLevel(int lev) {
00149 Log::setDefaultLevel(lev);
00150 }
00151
00152
00153
00154 void VOSDaemon::checkArgs(int argc, char** argv) {
00155
00156
00157
00158
00159 string optstring = "-hE:L:N";
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 #ifdef HAVE_GETOPT_LONG
00172 LongOptionStruct *longopts;
00173 longopts = (LongOptionStruct*)malloc( (7 + args.size()) * sizeof(LongOptionStruct));
00174 int l = 0;
00175
00176 longopts[l].name = "help";
00177 longopts[l].has_arg = no_argument;
00178 longopts[l].flag = NULL;
00179 longopts[l].val = 'h';
00180 l++;
00181
00182 longopts[l].name = "log";
00183 longopts[l].has_arg = required_argument;
00184 longopts[l].flag = NULL;
00185 longopts[l].val = 'L';
00186 l++;
00187
00188 longopts[l].name = "loglevel";
00189 longopts[l].has_arg = required_argument;
00190 longopts[l].flag = NULL;
00191 longopts[l].val = 'E';
00192 l++;
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 #endif
00228
00229
00230
00231
00232
00233 for(ArgMap::iterator i = args.begin(); i != args.end(); i++) {
00234
00235
00236 if(i->second.envVar != "") {
00237 char* e = getenv(i->second.envVar.c_str());
00238 if(e) {
00239 i->second.given = true;
00240 if(!(i->second.isFlag)) {
00241 i->second.value = e;
00242 }
00243 }
00244 }
00245
00246
00247 if(i->second.argChar != 0) {
00248 optstring += i->second.argChar;
00249 if(!(i->second.isFlag))
00250 optstring += ":";
00251 }
00252
00253 #ifdef HAVE_GETOPT_LONG
00254
00255 longopts[l].name = i->second.argName.c_str();
00256 if(i->second.isFlag)
00257 longopts[l].has_arg = no_argument;
00258 else
00259 longopts[l].has_arg = required_argument;
00260 longopts[l].flag = NULL;
00261 longopts[l].val = 0;
00262 l++;
00263 #endif
00264
00265 }
00266
00267 #ifdef HAVE_GETOPT_LONG
00268
00269 memset(&longopts[l], 0, sizeof(LongOptionStruct));
00270 #endif
00271
00272
00273 int *indexp = new int;
00274 *indexp = -1;
00275 char o = 0;
00276 while( (o =
00277 #ifdef HAVE_GETOPT_LONG
00278 getopt_long(argc, argv, optstring.c_str(), longopts, indexp)
00279 #else
00280 getopt(argc, argv, optstring.c_str())
00281 #endif
00282 ) != -1 ) {
00283
00284 if(o == 1) {
00285 printHelp();
00286 exit(-1);
00287 }
00288
00289 ArgInfo* ai = 0;
00290 #ifdef HAVE_GETOPT_LONG
00291 if(o == 0) {
00292 assert(indexp);
00293 assert(*indexp > 0);
00294 ai = &(args[longopts[*indexp].name]);
00295 } else
00296 #endif
00297 {
00298 if(checkBuiltinArgs(o, optarg))
00299 continue;
00300 assert(shortArgs.count(o) > 0);
00301 ai = shortArgs[o];
00302 }
00303 assert(ai);
00304 ai->given = true;
00305 if(!(ai->isFlag) && (optarg != 0)) {
00306 ai->value = strdup(optarg);
00307 }
00308 }
00309
00310 delete indexp;
00311 #ifdef HAVE_GETOPT_LONG
00312 free(longopts);
00313 #endif
00314
00315 }
00316
00317 void VOSDaemon::printHelp() {
00318 bool have_long;
00319 #ifdef HAVE_GETOPT_LONG
00320 have_long = true;
00321 #else
00322 have_long = false;
00323 #endif
00324
00325 cout << "Usage:\n\t" << programName << " [options]\nOptions:\n";
00326
00327 #ifdef HAVE_FORK
00328 cout << "\t-N\tDon't run in the background.\n";
00329 #endif
00330
00331 cout << "\t-h " << (have_long?"or --help":"") << "\tPrint a brief help message.\n" <<
00332 "\t-L <FILENAME>" << (have_long?" or --log <FILENAME>":"") << "\tLog to file <FILENAME>. (default is /var/log/" << programName <<") Use \"-L -\" to log to stdout (-N is implied in this case).\n" <<
00333 "\t-E <LEVEL>" << (have_long?" or --loglevel <LEVEL>":"") << "\tSet log level to <LEVEL> (default is " << Log::getDefaultLevel() <<").\n";
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 for(ArgMap::const_iterator i = args.begin(); i != args.end(); i++) {
00348 cout << "\t-" << i->second.argChar;
00349 cout << " " << i->second.valueLabel;
00350 if(have_long) {
00351 cout << " or --" << i->second.argName;
00352 cout << " " << i->second.valueLabel;
00353 }
00354 cout << "\t" << i->second.description << endl;
00355 }
00356 if(have_long)
00357 cout << "\nNote that long argument names may not be available on all platforms.\n";
00358 }
00359
00360 bool VOSDaemon::checkBuiltinArgs(char o, char* val) {
00361 switch(o) {
00362 case '?':
00363 case 'h':
00364 printHelp();
00365 exit(0);
00366 case 'E':
00367 Log::setDefaultLevel( atoi(val) );
00368 return true;
00369 case 'L':
00370 if(logFile)
00371 free(logFile);
00372 logFile = strdup(val);
00373 return true;
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388 case 'N':
00389 dontFork = true;
00390 return true;
00391 }
00392 return false;
00393 }
00394
00395
00396 void VOSDaemon::run() {
00397
00398
00399 if(!strcmp(logFile, "-")) {
00400 Log::setDefaultOutputStream(&cout);
00401 dontFork = true;
00402 } else {
00403 ofstream* l = new ofstream(logFile, ofstream::app);
00404 if(l->bad()) {
00405 cerr << "Warning: could not open log file (" << logFile << "). No logging will occur." << endl;
00406 Log::setDefaultLevel(0);
00407 logStream = 0;
00408 fclose(stdout);
00409 fclose(stderr);
00410 fclose(stdin);
00411 } else {
00412 Log::setDefaultOutputStream(l);
00413
00414 logStream = l;
00415
00416
00417
00418
00419
00420 freopen(logFile, "a", stdout);
00421 freopen(logFile, "a", stderr);
00422 fclose(stdin);
00423 }
00424 }
00425
00426
00427 #if 0
00428 try {
00429
00430 if(loadfile) {
00431 LOG("VOSDaemon", 0, "Oops, loading is not implemented.");
00432
00433
00434
00435
00436
00437 }
00438 } catch(exception& e) {
00439 LOG(programName, 0, "Error: " << e.what() );
00440 exit(-1);
00441 }
00442 #endif
00443
00444 #ifdef HAVE_SIGNAL
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455 #endif
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465 #ifdef HAVE_FORK
00466 if(!dontFork) {
00467
00468 int pid;
00469 pid = fork();
00470 if (pid == -1) {
00471 cerr << "Error forking daemon!\n";
00472 exit(-2);
00473 }
00474 if (pid != 0) {
00475 cerr << "Running in background with PID " << pid << endl;
00476 exit(0);
00477 }
00478 }
00479 #else
00480
00481 #endif
00482
00483
00484
00485 try {
00486 LOG(programName, 3, "Site running... ");
00487 if(rootObj) LOG(programName, 1, "Root object is: " << rootObj->getURL().getString() );
00488 while(true) {
00489 site->flushIncomingBuffers();
00490 loop();
00491 }
00492 } catch(exception& e) {
00493 LOG(programName, 0, "Error: " << e.what() << " (" << typeid(e).name() << ")" );
00494 exit(-1);
00495 }
00496
00497 }
00498
00499
00500 void VOSDaemon::save() {
00501 LOG("VOSDaemon", 0, "Oops, saving is not implemented.");
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522 }
00523
00524
00525
00526 void VOSDaemon::addArg(string argName, string valueLabel, string description, char argChar, string envVar) {
00527 args[argName].argName = argName;
00528 args[argName].valueLabel = valueLabel;
00529 args[argName].description = description;
00530 args[argName].argChar = argChar;
00531 args[argName].isFlag = false;
00532 args[argName].given = false;
00533 args[argName].value = "";
00534 args[argName].envVar = envVar;
00535 if(argChar != 0)
00536 shortArgs[argChar] = &args[argName];
00537 }
00538
00539 void VOSDaemon::addArg(string argName, string valueLabel, string description, string defaultValue, char argChar, string envVar) {
00540 addArg(argName, valueLabel, description, argChar, envVar);
00541 args[argName].value = defaultValue;
00542 args[argName].given = true;
00543 }
00544
00545 void VOSDaemon::addFlag(string name, string descrip, char flagch, string envVar) {
00546 addArg(name, "", descrip, flagch, envVar);
00547 args[name].isFlag = true;
00548 }
00549
00550
00551 string VOSDaemon::getArg(string name) {
00552 if(args.count(name) == 0)
00553 throw runtime_error("No such argument registered. use addArg().");
00554 if(!args[name].given)
00555 throw runtime_error("Argument not given.");
00556 return args[name].value;
00557 }
00558
00559 bool VOSDaemon::argGiven(string argName) {
00560 if(args.count(argName) == 0)
00561 throw runtime_error("No such argument registered. use addArg().");
00562 return args[argName].given;
00563 }
00564
00565 void VOSDaemon::debugArgs() {
00566 for(ArgMap::const_iterator i = args.begin(); i != args.end(); i++) {
00567 cerr << "Argument name=" << i->second.argName << endl;
00568 cerr << "\tvalueLabel=" << i->second.valueLabel << endl;
00569 cerr << "\tvalue=" << i->second.value << endl;
00570 cerr << "\targChar=" << i->second.argChar << endl;
00571 cerr << "\tisFlag=" << ((i->second.isFlag)?"true":"false") << endl;
00572 cerr << "\tdescription=" << i->second.description << endl;
00573 cerr << "\tgiven=" << ((i->second.given)?"true":"false") << endl;
00574 cerr << "\tenvVar=" << i->second.envVar << endl;
00575 }
00576 }
00577
00578
00579
00580