Ignore:
Timestamp:
Feb 2, 2015, 10:04:56 AM (9 years ago)
Author:
david
Message:
  • Adding Process Management
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_loader.c

    r512 r549  
    3030 */
    3131#ifndef WIN32
     32/*
    3233#include "fcgio.h"
    3334#include "fcgi_config.h"
    3435#include "fcgi_stdio.h"
    35 #endif
     36*/
     37#include <unistd.h>
     38#include <fcgiapp.h>
     39#endif
     40#include <sys/wait.h>
     41#include <pthread.h>
    3642#include <sys/types.h>
    3743#include <unistd.h>
     
    5662#include <glib.h>
    5763#include <sys/stat.h>
     64}
     65
    5866#include "service_zcfg.h"
    59 }
    60 
    61 #include "service_internal.h"
     67//#include "service_internal.h"
    6268
    6369xmlXPathObjectPtr extractFromDoc (xmlDocPtr, const char *);
    64 int runRequest (map **);
     70int runRequest (map **,struct cgi_env **,FCGX_Request *);
    6571
    6672using namespace std;
     
    7480
    7581
    76 int
    77 main (int argc, char *argv[])
     82static void PrintEnv(FCGX_Stream *out, char *label, char **envp)
    7883{
    79   maps *m;
    80   m = (maps *) malloc (MAP_SIZE);
    81   conf_read ("main.cfg", m);
    82   char ntmp[1024];
    83 #ifndef WIN32
    84   getcwd (ntmp, 1024);
    85 #else
    86   _getcwd (ntmp, 1024);
    87 #endif
    88   char *rootDir = "/var/www/zoo-wps/cgi-bin";
    89   init_services_conf (rootDir);
    90 
    91 
    92   while (FCGI_Accept () >= 0)
    93     {
    94       cgiMain_init (argc, argv);
    95 /**
    96    * We'll use cgiOut as the default output (stdout) to produce plain text
    97    * response.
    98    */
    99       dup2 (fileno (cgiOut), fileno (stdout));
    100 
    101 #ifdef DEBUG
    102       fprintf (cgiOut,
    103                "Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
    104       fprintf (cgiOut, "Welcome on ZOO verbose debuging mode \r\n\r\n");
    105       fflush (cgiOut);
    106       fprintf (stderr, "Addr:%s\n", cgiRemoteAddr);
    107       fprintf (stderr, "RequestMethod: (%s) %d %d\n", cgiRequestMethod,
    108                strncasecmp (cgiRequestMethod, "post", 4),
    109                strncmp (cgiContentType, "text/xml", 8) == 0
    110                || strncasecmp (cgiRequestMethod, "post", 4) == 0);
    111       fprintf (stderr, "Request: %s\n", cgiQueryString);
    112       fprintf (stderr, "ContentType: %s\n", cgiContentType);
    113       fprintf (stderr, "ContentLength: %d\n", cgiContentLength);
    114       fflush (stderr);
    115 #endif
    116 
    117 
     84    FCGX_FPrintF(out, "%s:<br>\n<pre>\n", label);
     85    for( ; *envp != NULL; envp++) {
     86        FCGX_FPrintF(out, "%s\n", *envp);
     87    }
     88    FCGX_FPrintF(out, "</pre><p>\n");
     89}
     90
     91#define PATH_SOCKET "/tmp/zoo.sock"
     92#define THREAD_COUNT 50
     93static int counts[THREAD_COUNT];
     94
     95
     96int process(FCGX_Request *request){
     97
     98      int pid = getpid();
     99      struct cgi_env *cgi;
     100      //PrintEnv(request.err, "Request environment", request.envp);
     101      cgi = (struct cgi_env*)malloc(sizeof(struct cgi_env));
     102      cgiMain_init (NULL, NULL,&cgi,request);
    118103      char *strQuery = NULL;
    119       if (cgiQueryString != NULL)
    120         strQuery = zStrdup (cgiQueryString);
     104      if (cgi->cgiQueryString != NULL)
     105        strQuery = zStrdup (cgi->cgiQueryString);
    121106      map *tmpMap = NULL;
    122107
    123       if (strncmp (cgiContentType, "text/xml", 8) == 0 ||
    124           strncasecmp (cgiRequestMethod, "post", 4) == 0)
     108      if (strncmp (cgi->cgiContentType, "text/xml", 8) == 0 ||
     109          strncasecmp (cgi->cgiRequestMethod, "post", 4) == 0)
    125110        {
    126           if (cgiContentLength == 0)
    127             {
    128               char *buffer = new char[2];
    129               char *res = NULL;
    130               int r = 0;
    131               while ((r = fread (buffer, sizeof (char), 1, cgiIn)))
    132                 {
    133                   buffer[1] = 0;
    134                   cgiContentLength += r;
    135                   if (res == NULL)
    136                     {
    137                       res = (char *) malloc (2 * sizeof (char));
    138                       sprintf (res, "%s", buffer);
     111          if (cgi->cgiContentLength == 0)
     112            {
     113              char *post_data = NULL;
     114              int i = 0;
     115              int ch = FCGX_GetChar(request->in);
     116              while (ch != -1){
     117                {
     118                  i++;
     119                  if (post_data == NULL)
     120                    {
     121                    post_data=(char*)malloc(sizeof(char));
     122                    post_data[i-1] = (char) ch;
    139123                    }
    140124                  else
    141125                    {
    142                       char *tmp = zStrdup (res);
    143                       res =
    144                         (char *) realloc (res,
    145                                           (strlen (tmp) + 2) * sizeof (char));
    146                       sprintf (res, "%s%s", tmp, buffer);
    147                       free (tmp);
    148                     }
    149                 }
    150               delete[]buffer;
    151               if (res == NULL && (strQuery == NULL || strlen (strQuery) == 0))
     126                    post_data=(char*)realloc(post_data,i*sizeof(char));
     127                    post_data[i-1] = (char) ch;
     128                    }
     129                  ch = FCGX_GetChar(request->in);
     130                  if (ch == -1 ){
     131                    post_data=(char*)realloc(post_data,(i + 1)*sizeof(char));
     132                    post_data[i] = '\0';
     133                    }
     134                }
     135                cgi->cgiContentLength = i;
     136              if (post_data == NULL && (strQuery == NULL || strlen (strQuery) == 0))
    152137                {
    153138                  return errorException (NULL,
    154139                                         "ZOO-Kernel failed to process your request cause the request was emtpty.",
    155                                          "InternalError", NULL);
     140                                         "InternalError", NULL,request->out);
    156141                }
    157142              else
    158143                {
    159144                  if (strQuery == NULL || strlen (strQuery) == 0)
    160                     tmpMap = createMap ("request", res);
    161                 }
    162               if (res != NULL)
    163                 free (res);
     145                    tmpMap = createMap ("request", post_data);
     146                }
     147              if (post_data != NULL)
     148                free (post_data);
     149                }
    164150            }
    165151          else
    166152            {
    167               char *buffer = new char[cgiContentLength + 1];
    168               if (fread (buffer, sizeof (char), cgiContentLength, cgiIn) > 0)
    169                 {
    170                   buffer[cgiContentLength] = 0;
    171                   tmpMap = createMap ("request", buffer);
     153              char *post_data = new char[cgi->cgiContentLength + 1];
     154              int r = FCGX_GetStr(post_data,cgi->cgiContentLength,request->in);
     155              if ( r > 0)
     156                {
     157                  post_data[r] = '\0';
     158                  cgi->cgiContentLength = r;
     159                  tmpMap = createMap ("request", post_data);
    172160                }
    173161              else
    174162                {
    175                   buffer[0] = 0;
     163                  post_data[0] = '\0';
    176164                  char **array, **arrayStep;
    177                   if (cgiFormEntries (&array) != cgiFormSuccess)
     165                  if (cgiFormEntries (&array,&cgi) != cgiFormSuccess)
    178166                    {
    179167                      return 1;
     
    182170                  while (*arrayStep)
    183171                    {
    184                       char *ivalue = new char[cgiContentLength];
     172                      char *ivalue = new char[cgi->cgiContentLength];
    185173                      cgiFormStringNoNewlines (*arrayStep, ivalue,
    186                                                cgiContentLength);
     174                                               cgi->cgiContentLength,&cgi);
    187175                      char *tmpValueFinal =
    188176                        (char *)
     
    190178                                 1) * sizeof (char));
    191179                      sprintf (tmpValueFinal, "%s=%s", *arrayStep, ivalue);
    192                       if (strlen (buffer) == 0)
     180                      if (strlen (post_data) == 0)
    193181                        {
    194                           sprintf (buffer, "%s", tmpValueFinal);
     182                          sprintf (post_data, "%s", tmpValueFinal);
    195183                        }
    196184                      else
    197185                        {
    198                           char *tmp = zStrdup (buffer);
    199                           sprintf (buffer, "%s&%s", tmp, tmpValueFinal);
     186                          char *tmp = zStrdup (post_data);
     187                          sprintf (post_data, "%s&%s", tmp, tmpValueFinal);
    200188                          free (tmp);
    201189                        }
     
    209197                    }
    210198                  if (tmpMap != NULL)
    211                     addToMap (tmpMap, "request", buffer);
     199                    addToMap (tmpMap, "request", post_data);
    212200                  else
    213                     tmpMap = createMap ("request", buffer);
    214                 }
    215               delete[]buffer;
     201                    tmpMap = createMap ("request", post_data);
     202                }
     203              delete[]post_data;
    216204            }
    217205        }
     
    221209          dumpMap (tmpMap);
    222210#endif
    223           char **array, **arrayStep;
    224           if (cgiFormEntries (&array) != cgiFormSuccess)
     211       
     212       char **array, **arrayStep;
     213          if (cgiFormEntries (&array,&cgi) != cgiFormSuccess)
    225214            {
    226215              return 1;
     
    229218          while (*arrayStep)
    230219            {
    231               char *value = new char[cgiContentLength];
    232               cgiFormStringNoNewlines (*arrayStep, value, cgiContentLength);
     220              char *value = new char[cgi->cgiContentLength];
     221              cgiFormStringNoNewlines (*arrayStep, value, cgi->cgiContentLength,&cgi);
    233222#ifdef DEBUG
    234223              fprintf (stderr, "(( \n %s \n %s \n ))", *arrayStep, value);
     
    265254   * one.
    266255   */
    267       if (strncasecmp (cgiRequestMethod, "post", 4) == 0 ||
     256      if (strncasecmp (cgi->cgiRequestMethod, "post", 4) == 0 ||
    268257          (count (tmpMap) == 1 && strncmp (tmpMap->value, "<", 1) == 0)
    269258#ifdef WIN32
     
    280269              addToMap (tmpMap, "xrequest", t1->value);
    281270              xmlInitParser ();
    282               xmlDocPtr doc = xmlParseMemory (t1->value, cgiContentLength);
     271              xmlDocPtr doc = xmlParseMemory (t1->value, cgi->cgiContentLength);
    283272              {
    284273                xmlXPathObjectPtr reqptr = extractFromDoc (doc,
     
    384373                          char *identifiers = NULL;
    385374                          identifiers =
    386                             (char *) calloc (cgiContentLength, sizeof (char));
     375                            (char *) calloc (cgi->cgiContentLength, sizeof (char));
    387376                          identifiers[0] = 0;
    388377                          for (int k = 0; k < id->nodeNr; k++)
     
    427416
    428417          char *token, *saveptr;
    429           token = strtok_r (cgiQueryString, "&", &saveptr);
     418          token = strtok_r (cgi->cgiQueryString, "&", &saveptr);
    430419          while (token != NULL)
    431420            {
     
    452441        }
    453442
    454       if (strncasecmp (cgiContentType, "multipart/form-data", 19) == 0)
     443      if (strncasecmp (cgi->cgiContentType, "multipart/form-data", 19) == 0)
    455444        {
    456445          map *tmp = getMap (tmpMap, "dataInputs");
     
    465454        free (strQuery);
    466455
    467       runRequest (&tmpMap);
     456      runRequest (&tmpMap,&cgi,request);
    468457
    469458  /**
     
    475464          free (tmpMap);
    476465        }
    477       cgiFreeResources ();
     466        // a verifier fait planter
     467      cgiFreeResources (&cgi);
     468
     469      FCGX_Finish_r(request);
     470      return 0;
     471}
     472
     473
     474
     475int
     476main (int argc, char *argv[])
     477{
     478  maps *main_config;
     479  int max_requests, start_servers;
     480  start_servers = 10;
     481  max_requests= 100;
     482  main_config = (maps *) malloc (MAP_SIZE);
     483  conf_read ("main.cfg", main_config);
     484  char ntmp[1024];
     485#ifndef WIN32
     486  getcwd (ntmp, 1024);
     487#else
     488  _getcwd (ntmp, 1024);
     489#endif
     490  char *rootDir = "/var/www/zoo-wps/cgi-bin";
     491  int fork_status = fork();
     492  if (fork_status == 0){
     493    //child
     494    int forker_pid = getpid();
     495    init_services_conf (rootDir);
     496    int sock = FCGX_OpenSocket(PATH_SOCKET, 1000);
     497    FCGX_Init();
     498    FCGX_Request request;
     499    FCGX_InitRequest(&request, sock, 0);
     500    int i;
     501    int count_request = 0;
     502    for (i = 0; i< start_servers; i++){
     503        fork_status = fork();
     504        if (fork_status == 0){
     505            fprintf(stderr,"child %d \n",i);
     506            fflush(stderr);
     507            break;
     508        }
    478509    }
    479   FCGI_Finish ();
     510    while(1){
     511        if (forker_pid != getpid()){
     512            while(FCGX_Accept_r(&request) == 0){
     513                process(&request);
     514                count_request ++;
     515                if (count_request >= max_requests){
     516                    fprintf(stderr,"Max request stop process\n");
     517                    fflush(stderr);
     518                    exit(0);
     519                }
     520            }
     521        }
     522        else {
     523            wait(0);
     524            fprintf(stderr,"new child\n");
     525            fflush(stderr);
     526            fork();
     527        }
     528    }
     529  }
     530  else {
     531 
     532  while(1);
     533
     534
     535
     536  }
     537 
    480538  return 0;
    481 
    482539}
     540 
Note: See TracChangeset for help on using the changeset viewer.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png