main.cpp

00001 /*   
00002  *   (c) Copyright 2008 Philipp Skadorov (philipp_s@users.sourceforge.net)
00003  *
00004  *   This file is part of FREESECS.
00005  *
00006  *   FREESECS is free software: you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation, either version 3 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   FREESECS is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with FREESECS, see COPYING.
00018  *   If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 #include <unistd.h>
00021 #include <stdlib.h>
00022 #include <getopt.h>
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include "log.h"
00026 #include "registrar.h"
00027 #include "hsms_cnx.h"
00028 #include "hsms_factory.h"
00029 
00030 using namespace freesecs;
00031 
00032 static int daemonize();
00033 static void print_help();
00034 static void print_version();
00035 static void process();
00036 
00037 hsms_factory_t hsms_factory;
00038 
00039 int main(int argc, char *argv[])
00040 {
00041     int c;
00042     int daemonize_flag = 1;
00043     
00044     while (1)
00045     {
00046         int option_index = 0;
00047         static struct option long_options[] = 
00048         {
00049                    {"nodaemon", 0, 0, 0},
00050                    {"help", 0, 0, 0},
00051                    {"version", 0, 0, 0},
00052                    {0, 0, 0, 0}
00053         };
00054 
00055         c = getopt_long (argc, argv, "nhv",
00056                         long_options, &option_index);
00057 
00058         if(c == -1)
00059             break;
00060 
00061         switch(c)
00062         {
00063             case 'n':
00064                 daemonize_flag = 0;
00065             break;
00066             case 'h':
00067                 print_help();
00068                 exit(0);
00069             break;
00070             case 'v':
00071                 print_version();
00072                 exit(0);
00073             case '?':
00074                 fprintf(stderr, "unknown argument: %c\n", optopt);
00075                 print_help();
00076                 exit(1);
00077             break;
00078         }
00079     }
00080 
00081     if(daemonize_flag)
00082     {
00083         daemonize();
00084     }
00085 
00086     process();
00087 
00088     exit(0);
00089 }
00090 
00091 static int daemonize()
00092 {
00093     int res;
00094     if(-1 == (res = daemon(0,0)))
00095     {
00096         TRACE_CRITICAL("failed to daemonize");
00097     }
00098     umask(0);
00099     return res;
00100 }
00101 
00102 static void print_help()
00103 {
00104     fprintf(stdout, "Usage: hsmsd [OPTION]\n");
00105     fprintf(stdout, "Starts HSMS stack daemon process\n");
00106     fprintf(stdout, "\t -h --help\t\t\tdisplay this message\n");
00107     fprintf(stdout, "\t -v --version\t\t\tprint version\n");
00108     fprintf(stdout, "\t -n --nodaemon\t\t\tdon't fork to background\n");
00109 }
00110 
00111 static void print_version()
00112 {
00113     fprintf(stdout, "Version: %s\n", PACKAGE_STRING);
00114 }
00115 
00116 #include "timer.h"
00117 void process()
00118 {
00119     hsmsd_client_registrar_t r;
00120 
00121     if(hsms_factory.NO_ERR != hsms_factory.parse_config("/etc/hsms.xml"))
00122     {
00123         fprintf(stderr, "hsms config parsed with errors:\n");
00124         hsms_factory_t::error_container_t::iterator it = hsms_factory.errors.begin();
00125         while(it < hsms_factory.errors.end())
00126         {
00127             fprintf(stderr,(it++)->c_str());
00128         }
00129         exit(-1);
00130     }
00131     while(1)
00132         r.process();
00133 }

Generated on Fri Oct 3 15:30:01 2008 for FREESECS hsms by  doxygen 1.5.1