"Fossies" - the Fresh Open Source Software Archive

Member "xstress/src/xstress.cc" (19 Feb 2009, 3991 Bytes) of package /linux/privat/old/xstress-src-0.375-beta-20090219.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "xstress.cc" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * xstress - xk0derz SMTP Stress Tester
    3  *
    4  * (c) Amit Singh amit@xkoder.com
    5  * http://xkoder.com
    6  *
    7  * This software and related files are licensed under GNU GPL version 2
    8  * Please visit the following webpage for more details
    9  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
   10  *
   11  * View the README file for usage details
   12  * and the LICENSE file for licensing terms.
   13  *
   14  */
   15 
   16 #include <sys/types.h>
   17 #include <unistd.h>
   18 #include <sys/types.h>
   19 #include <sys/wait.h>
   20 #include <string.h>
   21 #include <stdlib.h>
   22 #include <signal.h>
   23 
   24 #include "common.h"
   25 #include "sendmail.h"
   26 #include "config.h"
   27 #include "thread.h"
   28 #include "logger.h"
   29 #include "userinterface.h"
   30 
   31 using namespace std;
   32 
   33 int bDebug = false;
   34 
   35 Logger logger;
   36 
   37 unsigned int *pidList;
   38 unsigned int pidCount;
   39 unsigned int totThreads;
   40 int threadForceQuit = false;
   41 
   42 void sigHandler(int iSig)
   43 {
   44   unsigned int ii;
   45  
   46   threadForceQuit = true;
   47 
   48   if(pidCount>0 && pidList)
   49   {
   50     for(ii=0;ii<totThreads;ii++)
   51     {
   52       if(pidList[ii]) kill(pidList[ii], SIGHUP);
   53       pidList[ii] = 0;
   54     }
   55   }
   56 }
   57 
   58 void threadSigHandler(int iSig)
   59 {
   60   threadForceQuit = true;
   61 }
   62 
   63 int main(int argc, const char *argv[])
   64 {
   65 
   66   char buffer[1024];
   67   int  fdSet;
   68   sockaddr_in addr;
   69   int Done = 0;
   70   unsigned int ii;
   71   int rv;
   72 
   73   pidList = NULL;
   74   pidCount = 0;
   75  
   76   UserInterface uiObj;
   77   
   78   signal(SIGINT, sigHandler);
   79   
   80   if(uiObj.processOptions(argc, argv)==false)
   81   {
   82     return 0;
   83   }
   84   
   85   cout << VERSION << endl;
   86   cout << COPYRIGHT << endl;
   87   
   88   Config configObj(uiObj.sConfigFile);
   89   if(!configObj.okay())
   90   {
   91     cout << "Config file " << uiObj.sConfigFile << " Not found " << endl;
   92     cout << "Please provide complete path to the xstress configuration file :";
   93     cin >> uiObj.sConfigFile;
   94     if(uiObj.sConfigFile.empty()) return 0;
   95     Config configObj(uiObj.sConfigFile);
   96     if(!configObj.okay()) 
   97     {
   98       cout << "Config file " << uiObj.sConfigFile << " Not found " << endl;
   99       cout << "Exiting!" << endl;
  100       return 1;
  101     }
  102   }
  103   
  104 
  105   if(uiObj.override())
  106   {
  107     uiObj.setConfig(configObj);
  108   }
  109 
  110   debug("Debugging ON.");
  111   
  112   logger.setLogFile(configObj.sLogFile);
  113 
  114 
  115   pidList = new unsigned int[configObj.uiThreads];
  116   memset(pidList, sizeof(unsigned int), configObj.uiThreads);
  117   totThreads = configObj.uiThreads;
  118   pidCount = 0;
  119 
  120   if(!pidList)
  121   {
  122     cout << "Error: Memory allocation error at" << __FUNCTION__ << " : " << __LINE__ << endl;
  123     return 1;
  124   }
  125 
  126   for(ii=0;ii<configObj.uiThreads;ii++)
  127   {
  128     socket(AF_INET, SOCK_STREAM, 0);
  129     
  130     if(!threadForceQuit) rv = fork();
  131     else break;
  132 
  133     if(rv==0) /*This is the child*/
  134     {
  135 
  136       Thread threadObj(ii, configObj.uiMailsPerThread, &configObj);
  137       if(threadObj.okay()) 
  138       {
  139         signal(SIGHUP, threadSigHandler);
  140         cout << "Thread " << threadObj.getid() << " started!" << endl;
  141         threadForceQuit = false;
  142         while(!threadObj.finished() && !threadForceQuit)
  143         {
  144           threadObj.process();
  145           if(!threadObj.okay())
  146           {
  147             ;
  148           }
  149         }
  150       }
  151       if(threadForceQuit)
  152       cout << "Thread " << threadObj.getid() << " forced to stop!" << endl;
  153       else
  154       cout << "Thread " << threadObj.getid() << " finished!" << endl;
  155       return 0;
  156     }
  157     else if(rv!=-1)
  158     {
  159       pidList[ii] = rv;
  160       pidCount++;
  161     }
  162     else
  163     {
  164       cout << "Error creating thread with id " << ii  << endl;
  165       totThreads--;
  166     }
  167   }
  168 
  169   if(totThreads>0)
  170   {
  171     cout << "Waiting for " << pidCount << " threads to finish " << endl; 
  172 
  173     for(ii=0;ii<totThreads;ii++)
  174     {
  175       if(pidList[ii]) waitpid(pidList[ii], NULL, 0);
  176       pidList[ii] = 0;
  177       cout << "Number of threads remaining " << pidCount - (ii+1) << endl;
  178     }
  179 
  180     cout << "All Threads finished, exiting!" << endl;
  181   }
  182   else
  183   {
  184     cout << "FATAL Error: Unable to spwan even a single thread!!" << endl;
  185   }
  186 
  187   return 0;
  188 }
  189 
  190 void debug(string _msg)
  191 {
  192     if(bDebug)
  193     {
  194         cout << "* " << _msg << endl;
  195     }
  196 }