Main Page   Data Structures   File List   Data Fields   Globals   Related Pages  

pdeppd_main.c

Go to the documentation of this file.
00001 /***************************************************************************
00002                                    main.c
00003                              -------------------
00004     begin                : Tue Okt 30 2001
00005     copyright            : (C) 2001-2002 by Christian Hoenig & Gunter Ohrner
00006     email                : pdepp@CustomCDROM.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00024 #include "common.h"
00025 
00026 #ifdef HAVE_GETOPT_H
00027 # include <getopt.h>
00028 #endif
00029 #include <errno.h>
00030 #include <limits.h>
00031 // logger.h includes stdio.h for us. Even more importantly it includes stdarg.h
00032 // BEFORE including stdio.h which avoids tons of warnings on Solaris... :-(
00033 #include <stdlib.h>
00034 #include <signal.h>
00035 #include <string.h>
00036 #include <sys/stat.h>
00037 #include <sys/types.h>
00038 #include <unistd.h>
00039 
00040 #include "logger.h"
00041 
00042 #include "module_preinit.h"
00043 #include "buffer.h"
00044 #include "conftable.h"
00045 #include "http.h"
00046 #include "mime.h"
00047 #include "reqhandler_file.h"
00048 #include "server.h"
00049 #include "signal_handler.h"
00050 #include "xmalloc.h"
00051 
00052 
00053 //prototypes for following functions
00054 int initServer(void);
00055 
00056 
00060 void printVersionText()
00061 {
00062   printf("%s (%s) Version %s\n", SERVERNAME, PACKAGE, SERVERVERSION);
00063 }
00064 
00065 
00069 void printCopyright()
00070 {
00071   printVersionText();
00072   printf("Copyright (C) 2001-2002 Christian Hoenig & Gunter Ohrner <pdepp@CustomCDROM.de>\n"
00073          "%s comes with ABSOLUTELY NO WARRANTY. This is free software, and\n"
00074          "you are welcome to redistribute it under certain conditions. For more\n"
00075          "details see the GNU General Public License Version 2\n"
00076          "(http://www.gnu.org/copyleft/gpl.html).\n\n",
00077          PACKAGE);
00078 }
00079 
00086 void printHelpText(int briefHelp, char* argv_0)
00087 {
00088   if (briefHelp)
00089   { //print brief info
00090     fprintf(stderr,
00091 #ifdef HAVE_GETOPT_LONG
00092       "Try %s --help\n", argv_0);
00093 #else
00094       "Try %s -h\n", argv_0);
00095 #endif        
00096   }
00097   else
00098   { //print long helptext
00099     printf(
00100       "Usage: %s [options] rootdir\n\n"
00101       "Options:\n"
00102 #ifdef HAVE_GETOPT_LONG
00103       "rootdir                          Sets the root directory of the filetree\n\n"
00104       "--configfile=<FILE> -c <FILE>    Sets <FILE> the configfile\n"
00105       "--help              -h           Prints this text\n"
00106       "--logfile=<FILE>    -l <FILE>    Sets the logfile, whereas <FILE> is the full\n"
00107       "                                   path of the logfile\n"
00108       "--port=<N>          -p <N>       Tells %s to listen on port <N> \n"
00109       "                                   (0 < <N> < 65535)\n"
00110       "--sendbuffer=<N>    -s <N>       Tells %s to use <N> kByte as a sendbuffer\n"
00111       "--version           -v           Prints version information\n"
00112 #else
00113       "rootdir      Sets the root directory of the filetree\n\n"
00114       "-c <FILE>    Sets <FILE> the configfile\n"
00115       "-h           Prints this text\n"
00116       "-l <FILE>    Sets the logfile, whereas <FILE> is the full path of the logfile\n"
00117       "-p <N>       Tells %s to listen on port <N> (0 < <N> < 65535)\n"
00118       "-s <N>       Tells %s to use <N> kByte as a sendbuffer\n"
00119       "-v           Prints version information\n"
00120 #endif
00121       ,
00122       argv_0, SERVERNAME, SERVERNAME);
00123   }
00124 }
00125 
00126 
00130 int main(int argc, char *argv[])
00131 {
00132   int ret_val = EXIT_SUCCESS;
00133   int c;
00134 
00135   //temporary data for the commandline parameters
00136   long int port = 0;
00137   long int sendbuf = 0;
00138   int buf_size;
00139   char *rootdir = NULL;
00140   int rootdir_malloc = false;
00141   char *log_file, *cfg_file;
00142 
00143 #ifndef NDEBUG
00144   fprintf(stderr, "\n\n\n!!! DEBUG mode enabled !!!\n\n\n");
00145 #endif
00146 
00147   // initialize signal handlers
00148   sgnlInit();
00149   // initialize configuration table
00150   initConftable();
00151   // pre-initialise logging subsystem
00152   logfilesInit();
00153 
00154   printCopyright();
00155 
00156 
00157   while (true)
00158   {
00159 #ifdef HAVE_GETOPT_LONG
00160     int option_index = 0;
00161     static struct option long_options[] =
00162     {
00163       {CONFENTRY_KEY_CONFIGFILE , 1, 0, 'c'},
00164       {"help"                   , 0, 0, 'h'},
00165       {CONFENTRY_KEY_LOGFILE    , 1, 0, 'l'},
00166       {CONFENTRY_KEY_PORT       , 1, 0, 'p'},
00167       {CONFENTRY_KEY_SENDBUFFER , 1, 0, 's'},
00168       {"version"                , 0, 0, 'v'},
00169       {0, 0, 0, 0}
00170     };
00171     c = getopt_long(argc, argv, "c:hl:p:s:v", long_options, &option_index);
00172 #else
00173     c = getopt(argc, argv,"c:hl:p:s:v");
00174 #endif
00175     //if no more parameters found, leave...
00176     if (c == -1)
00177       break;
00178 
00179     //check the commandline parameters:
00180     switch (c)
00181     {
00182       case 'c': //configfile
00183         buf_size = strlen(optarg)+1;
00184         cfg_file = (char*)pd_malloc(buf_size);
00185         strcpy(cfg_file, optarg);
00186 
00187         if (strcmp(LOG_NULL_STREAM,cfg_file) != 0)
00188         setEntry(PRIO_CMDLINE, CONFENTRY_KEY_CONFIGFILE, cfg_file);
00189         break;
00190       case 'h': //help text
00191         printHelpText(false,argv[0]);
00192         return EXIT_SUCCESS;
00193       case 'l': //logfile
00194         buf_size = strlen(optarg)+1;
00195         log_file = (char*)pd_malloc(buf_size);
00196         strncpy(log_file, optarg, buf_size);
00197 
00198         if (strcmp(LOG_NULL_STREAM,log_file) != 0)
00199         setEntry(PRIO_CMDLINE, CONFENTRY_KEY_LOGFILE, log_file);
00200         break;
00201       case 'p': //port
00202         port = strtol(optarg, NULL, 10);
00203         if (setEntryL(PRIO_CMDLINE, CONFENTRY_KEY_PORT, port) <0)
00204         {
00205           logger(PDEPP_CORE, CRITICAL, "Invalid portnumber given: %d", port);
00206           printHelpText(true,argv[0]);
00207           return EXIT_FAILURE;
00208         }
00209         break;
00210       case 's': //sendbuffer
00211         sendbuf = strtol(optarg, NULL, 10);
00212         if (setEntryL(PRIO_CMDLINE, CONFENTRY_KEY_SENDBUFFER, sendbuf) <0)
00213         {
00214           logger(PDEPP_CORE, CRITICAL, "Invalid sendbuffer-size given: %d", sendbuf);
00215           printHelpText(true,argv[0]);
00216           return EXIT_FAILURE;
00217         }
00218         break;
00219       case 'v': //version
00220 //        printVersionText();
00221         return EXIT_SUCCESS;
00222       default:
00223         printHelpText(true,argv[0]);
00224         return EXIT_FAILURE;
00225     }
00226   }
00227 
00228   //read the config file if one is found
00229   readConfigFile();
00230 
00231   if (optind != argc - 1)
00232   {
00233     const char* rootdir_const = getEntry(CONFENTRY_KEY_ROOTDIR);
00234     if (rootdir_const == NULL)
00235     {
00236       logger(PDEPP_CORE, CRITICAL, "No rootdir given!");
00237       ret_val = EXIT_FAILURE;
00238     }
00239     else
00240     {
00241       rootdir_malloc = true;
00242       rootdir = pd_malloc(strlen(rootdir_const) + 1);
00243       strcpy(rootdir, rootdir_const);
00244     }
00245   } else {
00246     struct stat buf;
00247 
00248     buf_size = strlen(argv[optind]) + 1;
00249     rootdir_malloc = true;
00250     rootdir = (char*)pd_malloc(buf_size);
00251     strcpy(rootdir, argv[optind]);
00252 
00253     errno = 0;
00254 
00255     if (!(stat(rootdir, &buf)==0 && S_ISDIR(buf.st_mode)))
00256     {
00257       logerr(PDEPP_CORE, CRITICAL, errno, "Invalid rootdir given: %s", rootdir);
00258       printHelpText(true,argv[0]);
00259       ret_val = EXIT_FAILURE;
00260     }
00261   }
00262 
00263   if (ret_val != EXIT_FAILURE)
00264   {
00267     char* abs_path = (char*)pd_malloc(PATH_MAX+1);
00268     if (realpath(rootdir,abs_path) != NULL)
00269     {
00270       // a dir should end with a '/'
00271       strcat(abs_path, "/");
00272       setEntry(PRIO_PRGCALL, CONFENTRY_KEY_ROOTDIR, abs_path);
00273     }
00274     else
00275     {
00276       logerr(PDEPP_CORE, CRITICAL, errno, "Invalid rootdir given: %s", rootdir);
00277       ret_val = EXIT_FAILURE;
00278     }
00279     pd_free(abs_path);
00280   }
00281   if (rootdir_malloc == true)
00282     pd_free(rootdir);
00283 
00284   /* Shut dowhn pre-initialized logging subsystem.
00285      Will be reinitialized immediately using the information just read
00286      from command line and configuration file. */
00287   logfilesTerminate();
00288 
00289   if (ret_val != EXIT_FAILURE)
00290   {
00291     preinitAll();
00292     //the actual server is being started
00293     if (initServer() < 0)
00294       ret_val =  EXIT_FAILURE;
00295 
00296     //kill all
00297     terminateAll();
00298   }
00299 
00300 #ifndef NDEBUG
00301   pd_malloc(1952);
00302 #endif
00303 
00304   return ret_val;
00305 }
00306 
00307 
00308 
00309 
00317 int initServer(void) {
00318   unsigned int port;
00319 
00320   errno = 0;
00321   port = getEntryDeflUI(CONFENTRY_KEY_PORT, STD_CONFENTRY_VAL_PORT);
00322   if (port > 65535)
00323   {
00324     logerr(PDEPP_CORE, CRITICAL, errno,
00325            "initServer: invalid port specified (must be between 1 and 65535)");
00326     return -1;
00327   }
00328 
00329   if (svMain(port)) {
00330     logerr(PDEPP_CORE, CRITICAL, errno, "initServer: could not launch server");
00331     return -1;
00332   }
00333 
00334   return 0;
00335 }

Generated on Fri Jan 25 22:40:31 2002 for PDepp Webserver by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001