Back to index

nordugrid-arc-nox  1.1.0~rc6
main_win32.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #ifdef WIN32
00006 #include <arc/win32.h>
00007 #endif
00008 
00009 #include <fstream>
00010 #include <signal.h>
00011 #include <arc/ArcConfig.h>
00012 #include <arc/message/MCCLoader.h>
00013 #include <arc/XMLNode.h>
00014 #include <arc/Logger.h>
00015 #include "../options.h"
00016 
00017 Arc::Config config;
00018 Arc::MCCLoader *loader = NULL;
00019 Arc::Logger& logger=Arc::Logger::rootLogger;
00020 
00021 static void shutdown(int)
00022 {
00023     logger.msg(Arc::VERBOSE, "shutdown");
00024     delete loader;
00025     _exit(0);
00026 }
00027 
00028 static void merge_options_and_config(Arc::Config& cfg, Arc::ServerOptions& opt)
00029 {   
00030     Arc::XMLNode srv = cfg["Server"];
00031     if (!(bool)srv) {
00032       logger.msg(Arc::ERROR, "No server config part of config file");
00033       return;
00034     }
00035     if (opt.pid_file != "") {
00036         if (!(bool)srv["PidFile"]) {
00037            srv.NewChild("PidFile")=opt.pid_file;
00038         } else {
00039             srv["PidFile"] = opt.pid_file;
00040         }
00041     }
00042     if (opt.foreground == true) {
00043         if (!(bool)srv["Foreground"]) {
00044             srv.NewChild("Foreground");
00045         }
00046     }
00047 }
00048 
00049 static std::string init_logger(Arc::Config& cfg)
00050 {   
00051     /* setup root logger */
00052     Arc::XMLNode log = cfg["Server"]["Logger"];
00053     Arc::LogStream* sd = NULL;
00054     std::string log_file = (std::string)log;
00055     std::string str = (std::string)log.Attribute("level");
00056     if(!str.empty()) {
00057       Arc::LogLevel level = Arc::string_to_level(str);
00058       Arc::Logger::rootLogger.setThreshold(level); 
00059     }
00060 
00061 
00062     Arc::Logger::rootLogger.addDestination(*sd);
00063     if(!log_file.empty()) {
00064       std::fstream *dest = new std::fstream(log_file.c_str(), std::fstream::out | std::fstream::app);
00065       if(!(*dest)) {
00066         logger.msg(Arc::ERROR,"Failed to open log file: %s",log_file);
00067         _exit(1);
00068       }
00069       sd = new Arc::LogStream(*dest);
00070     }
00071     Arc::Logger::rootLogger.removeDestinations();
00072     if(sd) Arc::Logger::rootLogger.addDestination(*sd);
00073     if ((bool)cfg["Server"]["Foreground"]) {
00074       logger.msg(Arc::INFO, "Start foreground");
00075       Arc::LogStream *err = new Arc::LogStream(std::cerr);
00076       Arc::Logger::rootLogger.addDestination(*err);
00077     }
00078     return log_file;
00079 }
00080 
00081 int main(int argc, char **argv)
00082 {
00083     signal(SIGTTOU, SIG_IGN);
00084     // Temporary stderr destination for error messages
00085     Arc::LogStream logcerr(std::cerr);
00086     Arc::Logger::getRootLogger().addDestination(logcerr);
00087     /* Create options parser */
00088     Arc::ServerOptions options;
00089       
00090     try {
00091         std::list<std::string> params = options.Parse(argc, argv);
00092         if (params.size() == 0) {
00093             /* Load and parse config file */
00094             config.parse(options.xml_config_file.c_str());
00095             if(!config) {
00096                 logger.msg(Arc::ERROR, "Failed to load service configuration from file %s",options.xml_config_file);
00097                 exit(1);
00098             };
00099 
00100             if (options.version) {
00101                 std::cout << Arc::IString("%s version %s", "arched", VERSION) << std::endl;
00102                 exit(0);
00103             }
00104 
00105             if(!MatchXMLName(config,"ArcConfig")) {
00106               logger.msg(Arc::ERROR, "Configuration root element is not <ArcConfig>");
00107               exit(1);
00108             }
00109 
00110             /* overwrite config variables by cmdline options */
00111             merge_options_and_config(config, options);
00112             std::string pid_file = (std::string)config["Server"]["PidFile"];
00113             /* initalize logger infrastucture */
00114             std::string root_log_file = init_logger(config);
00115             
00116             // set signal handlers 
00117             signal(SIGTERM, shutdown);
00118             signal(SIGINT, shutdown);
00119 
00120             // bootstrap
00121             loader = new Arc::MCCLoader(config);
00122             if(!*loader) {
00123                 logger.msg(Arc::ERROR, "Failed to load service side MCCs");
00124             } else {
00125                 logger.msg(Arc::INFO, "Service side MCCs are loaded");
00126                 // sleep forever
00127                 for (;;) {
00128                     sleep(INT_MAX);
00129                 }
00130             }
00131         } else {
00132             logger.msg(Arc::ERROR, "Unexpected arguments supplied");
00133         }
00134     } catch (const Glib::Error& error) {
00135       logger.msg(Arc::ERROR, error.what());
00136     }
00137     
00138     return 0;
00139 }