source: branches/prototype-v0/zoo-project/zoo-kernel/zoo_service_loader.c @ 860

Last change on this file since 860 was 860, checked in by djay, 6 years ago

Add status_code key to the lenv section to support returning a specific HTTP error code from the service code. Fix callback invocation to support inputs arrays at step 1 and 2. Fix issue with cpu usage. Fix issue with mapserver publication when an input is optional. Fix callback invocation at step 7 in case the service has failed on the HPC side.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 74.3 KB
RevLine 
[607]1/*
[1]2 * Author : Gérald FENOY
3 *
[392]4 *  Copyright 2008-2013 GeoLabs SARL. All rights reserved.
[1]5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
[788]24 
[541]25extern "C" int yylex ();
26extern "C" int crlex ();
[9]27
[822]28#ifdef META_DB
29#include "ogrsf_frmts.h"
30#if GDAL_VERSION_MAJOR >= 2
31#include <gdal_priv.h>
32#endif
33#endif
34
[550]35#ifdef USE_OTB
36#include "service_internal_otb.h"
37#endif
38
[822]39#ifdef USE_HPC
40#include "service_internal_hpc.h"
41#endif
42
[376]43#include "cgic.h"
44
[1]45#include <libxml/tree.h>
46#include <libxml/xmlmemory.h>
47#include <libxml/parser.h>
48#include <libxml/xpath.h>
49#include <libxml/xpathInternals.h>
50
51#include "ulinet.h"
52
[34]53#include <libintl.h>
54#include <locale.h>
[1]55#include <string.h>
56
57#include "service.h"
[34]58
[1]59#include "service_internal.h"
[640]60#include "server_internal.h"
61#include "response_print.h"
[621]62#include "request_parser.h"
[640]63#include "sqlapi.h"
[680]64#ifdef WIN32
65#include "caching.h"
66#endif
[33]67
[822]68#ifdef META_DB
69#include "meta_sql.h"
70#endif
71
[33]72#ifdef USE_PYTHON
[1]73#include "service_internal_python.h"
[33]74#endif
[1]75
[634]76#ifdef USE_SAGA
77#include "service_internal_saga.h"
78#endif
79
[1]80#ifdef USE_JAVA
81#include "service_internal_java.h"
82#endif
83
84#ifdef USE_PHP
85#include "service_internal_php.h"
86#endif
87
88#ifdef USE_JS
89#include "service_internal_js.h"
90#endif
91
[453]92#ifdef USE_RUBY
93#include "service_internal_ruby.h"
94#endif
95
[25]96#ifdef USE_PERL
97#include "service_internal_perl.h"
98#endif
[1]99
[794]100#ifdef USE_MONO
101#include "service_internal_mono.h"
102#endif
103
[839]104#include "service_json.h"
105#include "service_callback.h"
106
[1]107#include <dirent.h>
108#include <signal.h>
[854]109#include <execinfo.h>
[1]110#include <unistd.h>
111#ifndef WIN32
112#include <dlfcn.h>
113#include <libgen.h>
114#else
115#include <windows.h>
116#include <direct.h>
[364]117#include <sys/types.h>
118#include <sys/stat.h>
119#include <unistd.h>
120#define pid_t int;
[1]121#endif
122#include <fcntl.h>
123#include <time.h>
124#include <stdarg.h>
125
[854]126#include <libxml/tree.h>
127#include <libxml/parser.h>
128#include <libxml/xpath.h>
129#include <libxml/xpathInternals.h>
130
131#include <libxslt/xslt.h>
132#include <libxslt/xsltInternals.h>
133#include <libxslt/transform.h>
134#include <libxslt/xsltutils.h>
135
[772]136#ifndef WIN32
137extern char **environ;
138#endif
139
[788]140
[364]141#ifdef WIN32
[541]142extern "C"
143{
144  __declspec (dllexport) char *strcasestr (char const *a, char const *b)
[370]145#ifndef USE_MS
[541]146  {
147    char *x = zStrdup (a);
148    char *y = zStrdup (b);
149
150      x = _strlwr (x);
151      y = _strlwr (y);
152    char *pos = strstr (x, y);
153    char *ret = pos == NULL ? NULL : (char *) (a + (pos - x));
154      free (x);
155      free (y);
156      return ret;
157  };
[370]158#else
[541]159   ;
[370]160#endif
[364]161}
162#endif
163
[607]164/**
165 * Translation function for zoo-kernel
166 */
[34]167#define _(String) dgettext ("zoo-kernel",String)
[607]168/**
169 * Translation function for zoo-service
170 */
[376]171#define __(String) dgettext ("zoo-service",String)
[34]172
[582]173#ifdef WIN32
[839]174#ifndef PROGRAMNAME
175#define PROGRAMNAME "zoo_loader.cgi"
[582]176#endif
[839]177#endif
[582]178
[34]179
[607]180/**
181 * Replace a char by another one in a string
182 *
183 * @param str the string to update
184 * @param toReplace the char to replace
185 * @param toReplaceBy the char that will be used
186 */
[541]187void
188translateChar (char *str, char toReplace, char toReplaceBy)
189{
190  int i = 0, len = strlen (str);
191  for (i = 0; i < len; i++)
192    {
193      if (str[i] == toReplace)
194        str[i] = toReplaceBy;
195    }
[34]196}
197
[607]198/**
[765]199 * Dump back the final file fbkp1 to fbkp
200 *
201 * @param m the conf maps containing the main.cfg settings
202 * @param fbkp the string corresponding to the name of the file
203 * @param fbkp1 the string corresponding to the name of the file
204 */
205int dumpBackFinalFile(maps* m,char* fbkp,char* fbkp1)
206{
207  FILE *f2 = fopen (fbkp1, "rb");
208#ifndef RELY_ON_DB
209  semid lid = getShmLockId (m, 1);
210  if (lid < 0)
211    return -1;
212  lockShm (lid);
213#endif
214  FILE *f3 = fopen (fbkp, "wb+");
215  free (fbkp);
216  fseek (f2, 0, SEEK_END);
217  long flen = ftell (f2);
218  fseek (f2, 0, SEEK_SET);
219  char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char));
220  fread (tmps1, flen, 1, f2);
221#ifdef WIN32
222  char *pchr=strrchr(tmps1,'>');
223  flen=strlen(tmps1)-strlen(pchr)+1;
224  tmps1[flen]=0;
225#endif
226  fwrite (tmps1, 1, flen, f3);
227  fclose (f2);
228  fclose (f3);
[839]229  free(tmps1);
[765]230  return 1;
231}
232
233/**
[607]234 * Recursivelly parse zcfg starting from the ZOO-Kernel cwd.
235 * Call the func function given in arguments after parsing the ZCFG file.
236 *
237 * @param m the conf maps containing the main.cfg settings
238 * @param r the registry containing profiles hierarchy
239 * @param n the root XML Node to add the sub-elements
240 * @param conf_dir the location of the main.cfg file (basically cwd)
241 * @param prefix the current prefix if any, or NULL
242 * @param saved_stdout the saved stdout identifier
243 * @param level the current level (number of sub-directories to reach the
244 * current path)
[676]245 * @param func a pointer to a function having 4 parameters
246 *  (registry*, maps*, xmlNodePtr and service*).
[607]247 * @see inheritance, readServiceFile
248 */
249int
[839]250recursReaddirF ( maps * m, registry *r, xmlDocPtr doc, xmlNodePtr n, char *conf_dir, 
[676]251                 char *prefix, int saved_stdout, int level, 
[839]252                 void (func) (registry *, maps *, xmlDocPtr, xmlNodePtr, service *) )
[541]253{
[469]254  struct dirent *dp;
[541]255  int scount = 0;
[469]256
[541]257  if (conf_dir == NULL)
[469]258    return 1;
[541]259  DIR *dirp = opendir (conf_dir);
260  if (dirp == NULL)
261    {
262      if (level > 0)
263        return 1;
264      else
265        return -1;
266    }
[469]267  char tmp1[25];
[541]268  sprintf (tmp1, "sprefix_%d", level);
[469]269  char levels[17];
[541]270  sprintf (levels, "%d", level);
271  setMapInMaps (m, "lenv", "level", levels);
272  while ((dp = readdir (dirp)) != NULL)
273    if ((dp->d_type == DT_DIR || dp->d_type == DT_LNK) && dp->d_name[0] != '.'
274        && strstr (dp->d_name, ".") == NULL)
275      {
[469]276
[541]277        char *tmp =
278          (char *) malloc ((strlen (conf_dir) + strlen (dp->d_name) + 2) *
279                           sizeof (char));
280        sprintf (tmp, "%s/%s", conf_dir, dp->d_name);
[469]281
[541]282        if (prefix != NULL)
283          {
284            prefix = NULL;
285          }
286        prefix = (char *) malloc ((strlen (dp->d_name) + 2) * sizeof (char));
287        sprintf (prefix, "%s.", dp->d_name);
288
289        //map* tmpMap=getMapFromMaps(m,"lenv",tmp1);
290
291        int res;
292        if (prefix != NULL)
293          {
294            setMapInMaps (m, "lenv", tmp1, prefix);
295            char levels1[17];
296            sprintf (levels1, "%d", level + 1);
297            setMapInMaps (m, "lenv", "level", levels1);
298            res =
[839]299              recursReaddirF (m, r, doc, n, tmp, prefix, saved_stdout, level + 1,
[541]300                              func);
301            sprintf (levels1, "%d", level);
302            setMapInMaps (m, "lenv", "level", levels1);
303            free (prefix);
304            prefix = NULL;
305          }
306        else
307          res = -1;
308        free (tmp);
309        if (res < 0)
310          {
311            return res;
312          }
[469]313      }
[541]314    else
315      {
[557]316        char* extn = strstr(dp->d_name, ".zcfg");
[850]317        if(dp->d_name[0] != '.' && extn != NULL && strlen(extn) == 5 && strlen(dp->d_name)>6)
[541]318          {
319            int t;
320            char tmps1[1024];
321            memset (tmps1, 0, 1024);
322            snprintf (tmps1, 1024, "%s/%s", conf_dir, dp->d_name);
[789]323
[850]324            char *tmpsn = (char*)malloc((strlen(dp->d_name)-4)*sizeof(char));//zStrdup (dp->d_name);
325            memset (tmpsn, 0, strlen(dp->d_name)-4);
326            snprintf(tmpsn,strlen(dp->d_name)-4,"%s",dp->d_name);
[789]327           
328            map* import = getMapFromMaps (m, IMPORTSERVICE, tmpsn);
329            if (import == NULL || import->value == NULL || zoo_path_compare(tmps1, import->value) != 0 ) { // service is not in [include] block
330              service *s1 = (service *) malloc (SERVICE_SIZE);
331              if (s1 == NULL)
332                {
333                  dup2 (saved_stdout, fileno (stdout));
334                  errorException (m, _("Unable to allocate memory"),
335                                  "InternalError", NULL);
336                  return -1;
337                }
[839]338  #ifdef DEBUG
[789]339              fprintf (stderr, "#################\n%s\n#################\n",
340                       tmps1);
341  #endif
342              t = readServiceFile (m, tmps1, &s1, tmpsn);
343              free (tmpsn);
344              if (t < 0)
345                {
346                  map *tmp00 = getMapFromMaps (m, "lenv", "message");
347                  char tmp01[1024];
348                  if (tmp00 != NULL)
349                    sprintf (tmp01, _("Unable to parse the ZCFG file: %s (%s)"),
350                             dp->d_name, tmp00->value);
351                  else
352                    sprintf (tmp01, _("Unable to parse the ZCFG file: %s."),
353                             dp->d_name);
354                  dup2 (saved_stdout, fileno (stdout));
355                  errorException (m, tmp01, "InternalError", NULL);
356                  return -1;
357                }
358  #ifdef DEBUG
359              dumpService (s1);
360              fflush (stdout);
361              fflush (stderr);
362  #endif
[822]363              inheritance(r,&s1);
[839]364              func (r, m, doc, n, s1);
[789]365              freeService (&s1);
366              free (s1);
367              scount++;
368            }
[541]369          }
[469]370      }
[541]371  (void) closedir (dirp);
[469]372  return 1;
373}
374
[607]375/**
376 * Signal handling function which simply call exit(0).
377 *
378 * @param sig the signal number
379 */
[541]380void
381donothing (int sig)
382{
[478]383#ifdef DEBUG
[605]384  fprintf (stderr, "Signal %d after the ZOO-Kernel returned result!\n", sig);
[478]385#endif
[541]386  exit (0);
[105]387}
388
[607]389/**
390 * Signal handling function which create an ExceptionReport node containing the
391 * information message corresponding to the signal number.
392 *
393 * @param sig the signal number
394 */
[541]395void
396sig_handler (int sig)
397{
[854]398 
[9]399  char tmp[100];
[114]400  const char *ssig;
[541]401  switch (sig)
402    {
403    case SIGSEGV:
404      ssig = "SIGSEGV";
405      break;
406    case SIGTERM:
407      ssig = "SIGTERM";
408      break;
409    case SIGINT:
410      ssig = "SIGINT";
411      break;
412    case SIGILL:
413      ssig = "SIGILL";
414      break;
415    case SIGFPE:
416      ssig = "SIGFPE";
417      break;
418    case SIGABRT:
419      ssig = "SIGABRT";
420      break;
421    default:
422      ssig = "UNKNOWN";
423      break;
424    }
425  sprintf (tmp,
426           _
[854]427           ("ZOO Kernel failed to process your request, receiving signal %d = %s "),
[541]428           sig, ssig);
429  errorException (NULL, tmp, "InternalError", NULL);
[10]430#ifdef DEBUG
[541]431  fprintf (stderr, "Not this time!\n");
[10]432#endif
[541]433  exit (0);
[1]434}
435
[607]436/**
437 * Load a service provider and run the service function.
438 *
439 * @param myMap the conf maps containing the main.cfg settings
440 * @param s1 the service structure
441 * @param request_inputs map storing all the request parameters
442 * @param inputs the inputs maps
443 * @param ioutputs the outputs maps
444 * @param eres the result returned by the service execution
445 */
[541]446void
447loadServiceAndRun (maps ** myMap, service * s1, map * request_inputs,
448                   maps ** inputs, maps ** ioutputs, int *eres)
449{
[34]450  char tmps1[1024];
451  char ntmp[1024];
[541]452  maps *m = *myMap;
453  maps *request_output_real_format = *ioutputs;
454  maps *request_input_real_format = *inputs;
[34]455  /**
456   * Extract serviceType to know what kind of service should be loaded
457   */
[541]458  map *r_inputs = NULL;
[784]459  map* cwdMap=getMapFromMaps(m,"main","servicePath");
460  if(cwdMap!=NULL){
461    sprintf(ntmp,"%s",cwdMap->value);
462  }else{
[34]463#ifndef WIN32
[784]464    getcwd (ntmp, 1024);
[34]465#else
[784]466    _getcwd (ntmp, 1024);
[34]467#endif
[784]468  }
[541]469  r_inputs = getMap (s1->content, "serviceType");
[34]470#ifdef DEBUG
[541]471  fprintf (stderr, "LOAD A %s SERVICE PROVIDER \n", r_inputs->value);
472  fflush (stderr);
[34]473#endif
[605]474
475  map* libp = getMapFromMaps(m, "main", "libPath");
[712]476
[541]477  if (strlen (r_inputs->value) == 1
478      && strncasecmp (r_inputs->value, "C", 1) == 0)
[605]479  {
480     if (libp != NULL && libp->value != NULL) {
481            r_inputs = getMap (s1->content, "ServiceProvider");
482                sprintf (tmps1, "%s/%s", libp->value, r_inputs->value);
483         }
484     else {     
485        r_inputs = getMap (request_inputs, "metapath");
486        if (r_inputs != NULL)
487          sprintf (tmps1, "%s/%s", ntmp, r_inputs->value);
488        else
489          sprintf (tmps1, "%s/", ntmp);
490         
491        char *altPath = zStrdup (tmps1);
492        r_inputs = getMap (s1->content, "ServiceProvider");
493        sprintf (tmps1, "%s/%s", altPath, r_inputs->value);
494        free (altPath);
495         }
[34]496#ifdef DEBUG
[541]497      fprintf (stderr, "Trying to load %s\n", tmps1);
[34]498#endif
499#ifdef WIN32
[541]500      HINSTANCE so =
501        LoadLibraryEx (tmps1, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
[34]502#else
[541]503      void *so = dlopen (tmps1, RTLD_LAZY);
[34]504#endif
[57]505#ifdef WIN32
[578]506      char* errstr = getLastErrorMessage();
[34]507#else
[541]508      char *errstr;
509      errstr = dlerror ();
[34]510#endif
[478]511#ifdef DEBUG
[578]512          fprintf (stderr, "%s loaded (%s) \n", tmps1, errstr);
[478]513#endif
[541]514      if (so != NULL)
515        {
[34]516#ifdef DEBUG
[541]517          fprintf (stderr, "Library loaded %s \n", errstr);
518          fprintf (stderr, "Service Shared Object = %s\n", r_inputs->value);
[34]519#endif
[541]520          r_inputs = getMap (s1->content, "serviceType");
[34]521#ifdef DEBUG
[541]522          dumpMap (r_inputs);
523          fprintf (stderr, "%s\n", r_inputs->value);
524          fflush (stderr);
[34]525#endif
[541]526          if (strncasecmp (r_inputs->value, "C-FORTRAN", 9) == 0)
527            {
528              r_inputs = getMap (request_inputs, "Identifier");
529              char fname[1024];
530              sprintf (fname, "%s_", r_inputs->value);
[34]531#ifdef DEBUG
[541]532              fprintf (stderr, "Try to load function %s\n", fname);
[34]533#endif
534#ifdef WIN32
[541]535              typedef int (CALLBACK * execute_t) (char ***, char ***,
536                                                  char ***);
537              execute_t execute = (execute_t) GetProcAddress (so, fname);
[34]538#else
[541]539              typedef int (*execute_t) (char ***, char ***, char ***);
540              execute_t execute = (execute_t) dlsym (so, fname);
[34]541#endif
542#ifdef DEBUG
543#ifdef WIN32
[578]544                          errstr = getLastErrorMessage();
[34]545#else
[541]546              errstr = dlerror ();
[34]547#endif
[541]548              fprintf (stderr, "Function loaded %s\n", errstr);
549#endif
[34]550
[541]551              char main_conf[10][30][1024];
552              char inputs[10][30][1024];
553              char outputs[10][30][1024];
554              for (int i = 0; i < 10; i++)
555                {
556                  for (int j = 0; j < 30; j++)
557                    {
558                      memset (main_conf[i][j], 0, 1024);
559                      memset (inputs[i][j], 0, 1024);
560                      memset (outputs[i][j], 0, 1024);
561                    }
562                }
563              mapsToCharXXX (m, (char ***) main_conf);
564              mapsToCharXXX (request_input_real_format, (char ***) inputs);
565              mapsToCharXXX (request_output_real_format, (char ***) outputs);
566              *eres =
567                execute ((char ***) &main_conf[0], (char ***) &inputs[0],
568                         (char ***) &outputs[0]);
[34]569#ifdef DEBUG
[541]570              fprintf (stderr, "Function run successfully \n");
[34]571#endif
[541]572              charxxxToMaps ((char ***) &outputs[0],
573                             &request_output_real_format);
574            }
575          else
576            {
[34]577#ifdef DEBUG
578#ifdef WIN32
[578]579                          errstr = getLastErrorMessage();
580              fprintf (stderr, "Function %s failed to load because of %s\n",
[541]581                       r_inputs->value, errstr);
[34]582#endif
583#endif
[541]584              r_inputs = getMapFromMaps (m, "lenv", "Identifier");
[34]585#ifdef DEBUG
[541]586              fprintf (stderr, "Try to load function %s\n", r_inputs->value);
[34]587#endif
[541]588              typedef int (*execute_t) (maps **, maps **, maps **);
[34]589#ifdef WIN32
[541]590              execute_t execute =
591                (execute_t) GetProcAddress (so, r_inputs->value);
[34]592#else
[541]593              execute_t execute = (execute_t) dlsym (so, r_inputs->value);
[34]594#endif
595
[541]596              if (execute == NULL)
597                {
[469]598#ifdef WIN32
[578]599                                  errstr = getLastErrorMessage();
[469]600#else
[541]601                  errstr = dlerror ();
[469]602#endif
[541]603                  char *tmpMsg =
604                    (char *) malloc (2048 + strlen (r_inputs->value));
605                  sprintf (tmpMsg,
606                           _
[781]607                           ("Error occurred while running the %s function: %s"),
[541]608                           r_inputs->value, errstr);
609                  errorException (m, tmpMsg, "InternalError", NULL);
610                  free (tmpMsg);
[478]611#ifdef DEBUG
[541]612                  fprintf (stderr, "Function %s error %s\n", r_inputs->value,
613                           errstr);
[478]614#endif
[541]615                  *eres = -1;
616                  return;
617                }
[469]618
[34]619#ifdef DEBUG
620#ifdef WIN32
[578]621                          errstr = getLastErrorMessage();
[34]622#else
[541]623              errstr = dlerror ();
[34]624#endif
[541]625              fprintf (stderr, "Function loaded %s\n", errstr);
626#endif
[34]627
628#ifdef DEBUG
[541]629              fprintf (stderr, "Now run the function \n");
630              fflush (stderr);
[34]631#endif
[541]632              *eres =
633                execute (&m, &request_input_real_format,
634                         &request_output_real_format);
[34]635#ifdef DEBUG
[541]636              fprintf (stderr, "Function loaded and returned %d\n", eres);
637              fflush (stderr);
[34]638#endif
[541]639            }
[216]640#ifdef WIN32
[541]641          *ioutputs = dupMaps (&request_output_real_format);
642          FreeLibrary (so);
[216]643#else
[541]644          dlclose (so);
[216]645#endif
[541]646        }
647      else
648        {
[34]649      /**
650       * Unable to load the specified shared library
651       */
[541]652          char tmps[1024];
[34]653#ifdef WIN32
[578]654                  errstr = getLastErrorMessage();
[34]655#else
[578]656              errstr = dlerror ();
[34]657#endif
[605]658          sprintf (tmps, _("Unable to load C Library %s"), errstr);
[576]659          errorException(m,tmps,"InternalError",NULL);
[541]660          *eres = -1;
661        }
[34]662    }
663  else
[550]664
[822]665#ifdef USE_HPC
666  if (strncasecmp (r_inputs->value, "HPC", 3) == 0)
667    {
668      *eres =
669        zoo_hpc_support (&m, request_inputs, s1,
670                            &request_input_real_format,
671                            &request_output_real_format);
672    }
673  else
674#endif
675
[634]676#ifdef USE_SAGA
[822]677  if (strncasecmp (r_inputs->value, "SAGA", 4) == 0)
[634]678    {
679      *eres =
680        zoo_saga_support (&m, request_inputs, s1,
681                            &request_input_real_format,
682                            &request_output_real_format);
683    }
684  else
685#endif
686
[550]687#ifdef USE_OTB
[822]688  if (strncasecmp (r_inputs->value, "OTB", 3) == 0)
[550]689    {
690      *eres =
691        zoo_otb_support (&m, request_inputs, s1,
692                            &request_input_real_format,
693                            &request_output_real_format);
694    }
695  else
696#endif
[34]697#ifdef USE_PYTHON
[541]698  if (strncasecmp (r_inputs->value, "PYTHON", 6) == 0)
[712]699    {     
[541]700      *eres =
701        zoo_python_support (&m, request_inputs, s1,
702                            &request_input_real_format,
703                            &request_output_real_format);
[34]704    }
[541]705  else
[34]706#endif
[541]707
[34]708#ifdef USE_JAVA
[541]709  if (strncasecmp (r_inputs->value, "JAVA", 4) == 0)
710    {
711      *eres =
712        zoo_java_support (&m, request_inputs, s1, &request_input_real_format,
713                          &request_output_real_format);
714    }
715  else
[34]716#endif
717
718#ifdef USE_PHP
[541]719  if (strncasecmp (r_inputs->value, "PHP", 3) == 0)
720    {
721      *eres =
722        zoo_php_support (&m, request_inputs, s1, &request_input_real_format,
[788]723                         &request_output_real_format);         
[541]724    }
725  else
[34]726#endif
[541]727
728
[34]729#ifdef USE_PERL
[541]730  if (strncasecmp (r_inputs->value, "PERL", 4) == 0)
731    {
732      *eres =
733        zoo_perl_support (&m, request_inputs, s1, &request_input_real_format,
734                          &request_output_real_format);
735    }
736  else
[34]737#endif
738
739#ifdef USE_JS
[541]740  if (strncasecmp (r_inputs->value, "JS", 2) == 0)
741    {
742      *eres =
743        zoo_js_support (&m, request_inputs, s1, &request_input_real_format,
744                        &request_output_real_format);
745    }
746  else
[34]747#endif
[453]748
749#ifdef USE_RUBY
[541]750  if (strncasecmp (r_inputs->value, "Ruby", 4) == 0)
751    {
752      *eres =
753        zoo_ruby_support (&m, request_inputs, s1, &request_input_real_format,
754                          &request_output_real_format);
755    }
756  else
[453]757#endif
758
[794]759#ifdef USE_MONO
760  if (strncasecmp (r_inputs->value, "Mono", 4) == 0)
[541]761    {
[794]762      *eres =
763        zoo_mono_support (&m, request_inputs, s1, &request_input_real_format,
764                          &request_output_real_format);
765    }
766  else
767#endif
768
769    {
[541]770      char tmpv[1024];
771      sprintf (tmpv,
772               _
773               ("Programming Language (%s) set in ZCFG file is not currently supported by ZOO Kernel.\n"),
774               r_inputs->value);
[576]775      errorException (m, tmpv, "InternalError", NULL);
[541]776      *eres = -1;
777    }
778  *myMap = m;
779  *ioutputs = request_output_real_format;
[34]780}
781
[384]782
[216]783#ifdef WIN32
784/**
785 * createProcess function: create a new process after setting some env variables
786 */
[541]787void
788createProcess (maps * m, map * request_inputs, service * s1, char *opts,
789               int cpid, maps * inputs, maps * outputs)
790{
[216]791  STARTUPINFO si;
792  PROCESS_INFORMATION pi;
[541]793  ZeroMemory (&si, sizeof (si));
794  si.cb = sizeof (si);
795  ZeroMemory (&pi, sizeof (pi));
796  char *tmp = (char *) malloc ((1024 + cgiContentLength) * sizeof (char));
797  char *tmpq = (char *) malloc ((1024 + cgiContentLength) * sizeof (char));
798  map *req = getMap (request_inputs, "request");
799  map *id = getMap (request_inputs, "identifier");
800  map *di = getMap (request_inputs, "DataInputs");
[216]801
[583]802  // The required size for the dataInputsKVP and dataOutputsKVP buffers
803  // may exceed cgiContentLength, hence a 2 kb extension. However, a
804  // better solution would be to have getMapsAsKVP() determine the required
805  // buffer size before allocating memory.     
806  char *dataInputsKVP = getMapsAsKVP (inputs, cgiContentLength + 2048, 0);
807  char *dataOutputsKVP = getMapsAsKVP (outputs, cgiContentLength + 2048, 1);
[384]808#ifdef DEBUG
[541]809  fprintf (stderr, "DATAINPUTSKVP %s\n", dataInputsKVP);
810  fprintf (stderr, "DATAOUTPUTSKVP %s\n", dataOutputsKVP);
[384]811#endif
[680]812  map *sid = getMapFromMaps (m, "lenv", "osid");
813  map *usid = getMapFromMaps (m, "lenv", "usid");
[541]814  map *r_inputs = getMapFromMaps (m, "main", "tmpPath");
815  map *r_inputs1 = getMap (request_inputs, "metapath");
[605]816 
[541]817  int hasIn = -1;
818  if (r_inputs1 == NULL)
819    {
820      r_inputs1 = createMap ("metapath", "");
821      hasIn = 1;
822    }
823  map *r_inputs2 = getMap (request_inputs, "ResponseDocument");
824  if (r_inputs2 == NULL)
825    r_inputs2 = getMap (request_inputs, "RawDataOutput");
826  map *tmpPath = getMapFromMaps (m, "lenv", "cwd");
[216]827
[541]828  map *tmpReq = getMap (request_inputs, "xrequest");
[790]829
[587]830  if(r_inputs2 != NULL && tmpReq != NULL) {
[680]831    const char key[] = "rfile=";
832    char* kvp = (char*) malloc((FILENAME_MAX + strlen(key))*sizeof(char));
833    char* filepath = kvp + strlen(key);
834    strncpy(kvp, key, strlen(key));
835    addToCache(m, tmpReq->value, tmpReq->value, "text/xml", strlen(tmpReq->value), 
[682]836               filepath, FILENAME_MAX);
[587]837    if (filepath == NULL) {
[680]838      errorException( m, _("Unable to cache HTTP POST Execute request."), "InternalError", NULL); 
839      return;
[587]840    }   
[680]841    sprintf(tmp,"\"metapath=%s&%s&cgiSid=%s&usid=%s\"",
842            r_inputs1->value,kvp,sid->value,usid->value);
[587]843    sprintf(tmpq,"metapath=%s&%s",
[680]844            r_inputs1->value,kvp);
845    free(kvp); 
[587]846  }
847  else if (r_inputs2 != NULL)
[541]848    {
849      sprintf (tmp,
[680]850               "\"metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&%s=%s&cgiSid=%s&usid=%s\"",
[541]851               r_inputs1->value, req->value, id->value, dataInputsKVP,
[680]852               r_inputs2->name, dataOutputsKVP, sid->value, usid->value);
[541]853      sprintf (tmpq,
854               "metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&%s=%s",
855               r_inputs1->value, req->value, id->value, dataInputsKVP,
[587]856               r_inputs2->name, dataOutputsKVP);                   
[541]857    }
858  else
859    {
860      sprintf (tmp,
[680]861               "\"metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&cgiSid=%s&usid=%s\"",
[541]862               r_inputs1->value, req->value, id->value, dataInputsKVP,
[680]863               sid->value, usid->value);
[541]864      sprintf (tmpq,
865               "metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s",
866               r_inputs1->value, req->value, id->value, dataInputsKVP,
[587]867               sid->value);   
[541]868    }
869
870  if (hasIn > 0)
871    {
872      freeMap (&r_inputs1);
873      free (r_inputs1);
874    }
875  char *tmp1 = zStrdup (tmp);
[587]876  sprintf (tmp, "\"%s\" %s \"%s\"", PROGRAMNAME, tmp1, sid->value); 
[541]877  free (dataInputsKVP);
878  free (dataOutputsKVP);
[384]879#ifdef DEBUG
[541]880  fprintf (stderr, "REQUEST IS : %s \n", tmp);
[384]881#endif
[554]882
[680]883  usid = getMapFromMaps (m, "lenv", "usid");
[554]884  if (usid != NULL && usid->value != NULL) {
885    SetEnvironmentVariable("USID", TEXT (usid->value));
886  }
[541]887  SetEnvironmentVariable ("CGISID", TEXT (sid->value));
888  SetEnvironmentVariable ("QUERY_STRING", TEXT (tmpq));
[682]889  // knut: Prevent REQUEST_METHOD=POST in background process call to cgic:main
890  // (process hangs when reading cgiIn):
[587]891  SetEnvironmentVariable("REQUEST_METHOD", "GET");
[682]892  SetEnvironmentVariable("CONTENT_TYPE", "text/plain");
[587]893 
[216]894  char clen[1000];
[541]895  sprintf (clen, "%d", strlen (tmpq));
896  SetEnvironmentVariable ("CONTENT_LENGTH", TEXT (clen));
897
[682]898  // ref. https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863%28v=vs.85%29.aspx
[541]899  if (!CreateProcess (NULL,     // No module name (use command line)
900                      TEXT (tmp),       // Command line
901                      NULL,     // Process handle not inheritable
902                      NULL,     // Thread handle not inheritable
903                      FALSE,    // Set handle inheritance to FALSE
904                      CREATE_NO_WINDOW, // Apache won't wait until the end
905                      NULL,     // Use parent's environment block
906                      NULL,     // Use parent's starting directory
907                      &si,      // Pointer to STARTUPINFO struct
908                      &pi)      // Pointer to PROCESS_INFORMATION struct
909    )
910    {
[384]911#ifdef DEBUG
[541]912      fprintf (stderr, "CreateProcess failed (%d).\n", GetLastError ());
[384]913#endif
[587]914      if (tmp != NULL) {
915        free(tmp);
916      }
917      if (tmpq != NULL) {
918        free(tmpq);
919      }         
[541]920      return;
921    }
922  else
923    {
[384]924#ifdef DEBUG
[587]925      fprintf (stderr, "CreateProcess successful (%d).\n\n\n\n",
[541]926               GetLastError ());
[384]927#endif
[541]928    }
929  CloseHandle (pi.hProcess);
930  CloseHandle (pi.hThread);
[587]931 
932  if (tmp != NULL) {
933    free(tmp);
934  }
935  if (tmpq != NULL) {
936    free(tmpq);
937  }
938 
[384]939#ifdef DEBUG
[541]940  fprintf (stderr, "CreateProcess finished !\n");
[384]941#endif
[216]942}
943#endif
944
[607]945/**
946 * Process the request.
947 *
948 * @param inputs the request parameters map
949 * @return 0 on sucess, other value on failure
950 * @see conf_read,recursReaddirF
951 */
[541]952int
953runRequest (map ** inputs)
[1]954{
[788]955 
[53]956#ifndef USE_GDB
[554]957#ifndef WIN32
[541]958  signal (SIGCHLD, SIG_IGN);
[554]959#endif 
[541]960  signal (SIGSEGV, sig_handler);
961  signal (SIGTERM, sig_handler);
962  signal (SIGINT, sig_handler);
963  signal (SIGILL, sig_handler);
964  signal (SIGFPE, sig_handler);
965  signal (SIGABRT, sig_handler);
[53]966#endif
[9]967
[839]968 
[541]969  map *r_inputs = NULL;
970  map *request_inputs = *inputs;
[839]971  //fprintf(stderr,"%s \n",json_object_to_json_string_ext(mapToJson(request_inputs),JSON_C_TO_STRING_PLAIN));
972 
[605]973#ifdef IGNORE_METAPATH
974  addToMap(request_inputs, "metapath", "");
975#endif 
[541]976  maps *m = NULL;
977  char *REQUEST = NULL;
[1]978  /**
979   * Parsing service specfic configuration file
980   */
[541]981  m = (maps *) malloc (MAPS_SIZE);
982  if (m == NULL)
983    {
[790]984      return errorException (NULL, _("Unable to allocate memory"),
[541]985                             "InternalError", NULL);
986    }
[790]987  m->child=NULL;
[1]988  char ntmp[1024];
[784]989#ifndef ETC_DIR
[1]990#ifndef WIN32
[541]991  getcwd (ntmp, 1024);
[1]992#else
[541]993  _getcwd (ntmp, 1024);
[1]994#endif
[784]995#else
996  sprintf(ntmp,"%s",ETC_DIR);
997#endif
[541]998  r_inputs = getMapOrFill (&request_inputs, "metapath", "");
[282]999
[9]1000  char conf_file[10240];
[541]1001  snprintf (conf_file, 10240, "%s/%s/main.cfg", ntmp, r_inputs->value);
[784]1002#ifdef ETC_DIR
1003#ifndef WIN32
[854]1004  getcwd (ntmp, 1024);
[784]1005#else
[854]1006  _getcwd (ntmp, 1024);
[784]1007#endif
1008#endif
[788]1009
[541]1010  if (conf_read (conf_file, m) == 2)
1011    {
1012      errorException (NULL, _("Unable to load the main.cfg file."),
1013                      "InternalError", NULL);
1014      free (m);
1015      return 1;
1016    }
[9]1017#ifdef DEBUG
[541]1018  fprintf (stderr, "***** BEGIN MAPS\n");
1019  dumpMaps (m);
1020  fprintf (stderr, "***** END MAPS\n");
[9]1021#endif
1022
[541]1023  map *getPath = getMapFromMaps (m, "main", "gettextPath");
1024  if (getPath != NULL)
1025    {
1026      bindtextdomain ("zoo-kernel", getPath->value);
1027      bindtextdomain ("zoo-services", getPath->value);
1028    }
1029  else
1030    {
[784]1031      bindtextdomain ("zoo-kernel", LOCALEDIR);
1032      bindtextdomain ("zoo-services", LOCALEDIR);
[541]1033    }
[364]1034
1035  /**
1036   * Manage our own error log file (usefull to separate standard apache debug
1037   * messages from the ZOO-Kernel ones but also for IIS users to avoid wrong
1038   * headers messages returned by the CGI due to wrong redirection of stderr)
1039   */
[541]1040  FILE *fstde = NULL;
1041  map *fstdem = getMapFromMaps (m, "main", "logPath");
1042  if (fstdem != NULL)
1043    fstde = freopen (fstdem->value, "a+", stderr);
[364]1044
[541]1045  r_inputs = getMap (request_inputs, "language");
1046  if (r_inputs == NULL)
[640]1047    r_inputs = getMap (request_inputs, "AcceptLanguages");
1048  if (r_inputs == NULL)
[541]1049    r_inputs = getMapFromMaps (m, "main", "language");
1050  if (r_inputs != NULL)
1051    {
1052      if (isValidLang (m, r_inputs->value) < 0)
1053        {
1054          char tmp[1024];
1055          sprintf (tmp,
1056                   _
1057                   ("The value %s is not supported for the <language> parameter"),
1058                   r_inputs->value);
1059          errorException (m, tmp, "InvalidParameterValue", "language");
1060          freeMaps (&m);
1061          free (m);
1062          free (REQUEST);
1063          return 1;
[501]1064
[541]1065        }
1066      char *tmp = zStrdup (r_inputs->value);
1067      setMapInMaps (m, "main", "language", tmp);
[466]1068#ifdef DEB
[541]1069      char tmp2[12];
1070      sprintf (tmp2, "%s.utf-8", tmp);
1071      translateChar (tmp2, '-', '_');
1072      setlocale (LC_ALL, tmp2);
[466]1073#else
[541]1074      translateChar (tmp, '-', '_');
1075      setlocale (LC_ALL, tmp);
[466]1076#endif
[444]1077#ifndef WIN32
[541]1078      setenv ("LC_ALL", tmp, 1);
[444]1079#else
[745]1080      char tmp1[13];
[541]1081      sprintf (tmp1, "LC_ALL=%s", tmp);
1082      putenv (tmp1);
[376]1083#endif
[541]1084      free (tmp);
1085    }
1086  else
1087    {
1088      setlocale (LC_ALL, "en_US");
[444]1089#ifndef WIN32
[541]1090      setenv ("LC_ALL", "en_US", 1);
[444]1091#else
[745]1092      char tmp1[13];
[541]1093      sprintf (tmp1, "LC_ALL=en_US");
1094      putenv (tmp1);
[376]1095#endif
[541]1096      setMapInMaps (m, "main", "language", "en-US");
1097    }
[745]1098  setlocale (LC_NUMERIC, "C");
1099#ifndef WIN32
1100  setenv ("LC_NUMERIC", "C", 1);
1101#else
1102  char tmp1[17];
1103  sprintf (tmp1, "LC_NUMERIC=C");
1104  putenv (tmp1);
1105#endif
[541]1106  bind_textdomain_codeset ("zoo-kernel", "UTF-8");
1107  textdomain ("zoo-kernel");
1108  bind_textdomain_codeset ("zoo-services", "UTF-8");
1109  textdomain ("zoo-services");
[34]1110
[541]1111  map *lsoap = getMap (request_inputs, "soap");
1112  if (lsoap != NULL && strcasecmp (lsoap->value, "true") == 0)
1113    setMapInMaps (m, "main", "isSoap", "true");
[280]1114  else
[541]1115    setMapInMaps (m, "main", "isSoap", "false");
[34]1116
[584]1117  if(strlen(cgiServerName)>0)
[654]1118    {
1119      char tmpUrl[1024];
[584]1120       
[680]1121      if ( getenv("HTTPS") != NULL && strncmp(getenv("HTTPS"), "on", 2) == 0 ) {
1122        // Knut: check if non-empty instead of "on"?           
[654]1123        if ( strncmp(cgiServerPort, "443", 3) == 0 ) { 
1124          sprintf(tmpUrl, "https://%s%s", cgiServerName, cgiScriptName);
[584]1125        }
1126        else {
[654]1127          sprintf(tmpUrl, "https://%s:%s%s", cgiServerName, cgiServerPort, cgiScriptName);
[584]1128        }
[654]1129      }
1130      else {
1131        if ( strncmp(cgiServerPort, "80", 2) == 0 ) { 
1132          sprintf(tmpUrl, "http://%s%s", cgiServerName, cgiScriptName);
1133        }
1134        else {
1135          sprintf(tmpUrl, "http://%s:%s%s", cgiServerName, cgiServerPort, cgiScriptName);
1136        }
1137      }
[445]1138#ifdef DEBUG
[654]1139      fprintf(stderr,"*** %s ***\n",tmpUrl);
[445]1140#endif
[654]1141      setMapInMaps(m,"main","serverAddress",tmpUrl);
1142    }
[381]1143
[752]1144  // CORS Support
1145  if(strncasecmp(cgiRequestMethod,"OPTIONS",7)==0){
1146    map* cors=getMapFromMaps(m,"main","cors");
1147    if(cors!=NULL && strncasecmp(cors->value,"true",4)==0){
1148      char *encoding=getEncoding(m);
1149      printHeaders(m);
1150      printf("Content-Type: text/plain; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
1151      printf(_("CORS is enabled.\r\n"));
1152      freeMaps (&m);
1153      free (m);
1154      fflush (stdout);
1155      return 3;
1156    }
1157  }
1158
[654]1159  //Check for minimum inputs
1160  map* version=getMap(request_inputs,"version");
1161  if(version==NULL)
1162    version=getMapFromMaps(m,"main","version");
1163  setMapInMaps(m,"main","rversion",version->value);
1164  int vid=getVersionId(version->value);
1165  if(vid<0)
1166    vid=0;
[576]1167  map* err=NULL;
[654]1168  const char **vvr=(const char**)requests[vid];
1169  checkValidValue(request_inputs,&err,"request",vvr,1);
[576]1170  const char *vvs[]={
1171    "WPS",
1172    NULL
1173  };
1174  if(err!=NULL){
1175    checkValidValue(request_inputs,&err,"service",(const char**)vvs,1);
1176    printExceptionReportResponse (m, err);
1177    freeMap(&err);
1178    free(err);
1179    if (count (request_inputs) == 1)
1180      {
1181        freeMap (&request_inputs);
1182        free (request_inputs);
1183      }
1184    freeMaps (&m);
1185    free (m);
1186    return 1;
1187  }
1188  checkValidValue(request_inputs,&err,"service",(const char**)vvs,1);
[640]1189
[576]1190  const char *vvv[]={
1191    "1.0.0",
[640]1192    "2.0.0",
[576]1193    NULL
1194  };
[541]1195  r_inputs = getMap (request_inputs, "Request");
[850]1196  fprintf(stderr," ** DEBUG %s %d \n",__FILE__,__LINE__);
1197  fflush(stderr);
[854]1198  //dumpMap(r_inputs);
[850]1199  fprintf(stderr," ** DEBUG %s %d \n",__FILE__,__LINE__);
1200  fflush(stderr);
1201  if(r_inputs!=NULL)
1202    REQUEST = zStrdup (r_inputs->value);
1203  fprintf(stderr," ** DEBUG %s %d \n",__FILE__,__LINE__);
1204  fflush(stderr);
[654]1205  int reqId=-1;
[576]1206  if (strncasecmp (REQUEST, "GetCapabilities", 15) != 0){
1207    checkValidValue(request_inputs,&err,"version",(const char**)vvv,1);
[654]1208    int j=0;
1209    for(j=0;j<nbSupportedRequests;j++){
1210      if(requests[vid][j]!=NULL && requests[vid][j+1]!=NULL){
[850]1211        if(j<nbReqIdentifier && strncasecmp(REQUEST,requests[vid][j+1],strlen(requests[vid][j+1]))==0){
[654]1212          checkValidValue(request_inputs,&err,"identifier",NULL,1);
1213          reqId=j+1;
1214          break;
1215        }
1216        else
1217          if(j>=nbReqIdentifier && j<nbReqIdentifier+nbReqJob && 
[850]1218             strncasecmp(REQUEST,requests[vid][j+1],strlen(requests[vid][j+1]))==0){
[654]1219            checkValidValue(request_inputs,&err,"jobid",NULL,1);
1220            reqId=j+1;
1221            break;
1222          }
1223      }else
1224        break;
1225    }
[576]1226  }else{
1227    checkValidValue(request_inputs,&err,"AcceptVersions",(const char**)vvv,-1);
[658]1228    map* version1=getMap(request_inputs,"AcceptVersions");
1229    if(version1!=NULL){
1230      if(strstr(version1->value,schemas[1][0])!=NULL)
[640]1231        addToMap(request_inputs,"version",schemas[1][0]);
1232      else
[658]1233        addToMap(request_inputs,"version",version1->value);
1234      version=getMap(request_inputs,"version");
1235      setMapInMaps(m,"main","rversion",version->value);
1236      vid=getVersionId(version->value);
[640]1237    }
[576]1238  }
1239  if(err!=NULL){
1240    printExceptionReportResponse (m, err);
1241    freeMap(&err);
1242    free(err);
1243    if (count (request_inputs) == 1)
1244      {
1245        freeMap (&request_inputs);
1246        free (request_inputs);
1247      }
1248    free(REQUEST);
1249    freeMaps (&m);
1250    free (m);
1251    return 1;
1252  }
[1]1253
[541]1254  r_inputs = getMap (request_inputs, "serviceprovider");
1255  if (r_inputs == NULL)
1256    {
1257      addToMap (request_inputs, "serviceprovider", "");
1258    }
[1]1259
[541]1260  maps *request_output_real_format = NULL;
1261  map *tmpm = getMapFromMaps (m, "main", "serverAddress");
1262  if (tmpm != NULL)
1263    SERVICE_URL = zStrdup (tmpm->value);
[1]1264  else
[541]1265    SERVICE_URL = zStrdup (DEFAULT_SERVICE_URL);
[1]1266
[607]1267
1268
[541]1269  service *s1;
1270  int scount = 0;
[1]1271#ifdef DEBUG
[541]1272  dumpMap (r_inputs);
[1]1273#endif
1274  char conf_dir[1024];
1275  int t;
1276  char tmps1[1024];
1277
[541]1278  r_inputs = NULL;
1279  r_inputs = getMap (request_inputs, "metapath");
[784]1280  map* cwdMap0=getMapFromMaps(m,"main","servicePath");
[541]1281  if (r_inputs != NULL)
[784]1282    if(cwdMap0!=NULL)
1283      snprintf (conf_dir, 1024, "%s/%s", cwdMap0->value, r_inputs->value);
1284    else
1285      snprintf (conf_dir, 1024, "%s/%s", ntmp, r_inputs->value);
[9]1286  else
[784]1287    if(cwdMap0!=NULL)
1288      snprintf (conf_dir, 1024, "%s", cwdMap0->value);
1289    else
1290      snprintf (conf_dir, 1024, "%s", ntmp);
[607]1291  map* reg = getMapFromMaps (m, "main", "registry");
1292  registry* zooRegistry=NULL;
1293  if(reg!=NULL){
1294    int saved_stdout = dup (fileno (stdout));
1295    dup2 (fileno (stderr), fileno (stdout));
[676]1296    if(createRegistry (m,&zooRegistry,reg->value)<0){
1297      map *message=getMapFromMaps(m,"lenv","message");
1298      map *type=getMapFromMaps(m,"lenv","type");
1299      dup2 (saved_stdout, fileno (stdout));
1300      errorException (m, message->value,
1301                      type->value, NULL);
1302      return 0;
1303    }
[607]1304    dup2 (saved_stdout, fileno (stdout));
[676]1305    close(saved_stdout);
[607]1306  }
1307
[854]1308  dumpMap(request_inputs);
1309
[541]1310  if (strncasecmp (REQUEST, "GetCapabilities", 15) == 0)
1311    {
[1]1312#ifdef DEBUG
[541]1313      dumpMap (r_inputs);
[1]1314#endif
[541]1315      xmlDocPtr doc = xmlNewDoc (BAD_CAST "1.0");
[656]1316      xmlNodePtr n=printGetCapabilitiesHeader(doc,m,(version!=NULL?version->value:"1.0.0"));
[576]1317      /**
1318       * Here we need to close stdout to ensure that unsupported chars
1319       * has been found in the zcfg and then printed on stdout
1320       */
[541]1321      int saved_stdout = dup (fileno (stdout));
1322      dup2 (fileno (stderr), fileno (stdout));
[789]1323
1324      maps* imports = getMaps(m, IMPORTSERVICE);
1325      if (imports != NULL) {       
1326        map* zcfg = imports->content;
1327       
1328        while (zcfg != NULL) {
1329          if (zcfg->value != NULL) {
1330            service* svc = (service*) malloc(SERVICE_SIZE);
1331            if (svc == NULL || readServiceFile(m, zcfg->value, &svc, zcfg->name) < 0) {
1332              // pass over silently
1333              zcfg = zcfg->next;
1334              continue;
1335            }
1336            inheritance(zooRegistry, &svc);
[839]1337            printGetCapabilitiesForProcess(zooRegistry, m, doc, n, svc);
[789]1338            freeService(&svc);
1339            free(svc);                             
1340          }
1341          zcfg = zcfg->next;
1342        }           
1343      }
1344
[584]1345      if (int res =               
[839]1346          recursReaddirF (m, zooRegistry, doc, n, conf_dir, NULL, saved_stdout, 0,
[541]1347                          printGetCapabilitiesForProcess) < 0)
1348        {
1349          freeMaps (&m);
1350          free (m);
[607]1351          if(zooRegistry!=NULL){
1352            freeRegistry(&zooRegistry);
1353            free(zooRegistry);
1354          }
[541]1355          free (REQUEST);
1356          free (SERVICE_URL);
1357          fflush (stdout);
1358          return res;
1359        }
[656]1360      fflush (stdout);
[541]1361      dup2 (saved_stdout, fileno (stdout));
[822]1362#ifdef META_DB
[839]1363      fetchServicesFromDb(zooRegistry,m,doc,n,printGetCapabilitiesForProcess,1);
1364      close_sql(m,0);
[822]1365#endif     
[541]1366      printDocument (m, doc, getpid ());
1367      freeMaps (&m);
1368      free (m);
[607]1369      if(zooRegistry!=NULL){
1370        freeRegistry(&zooRegistry);
1371        free(zooRegistry);
1372      }
[541]1373      free (REQUEST);
1374      free (SERVICE_URL);
1375      fflush (stdout);
[9]1376      return 0;
[1]1377    }
[541]1378  else
1379    {
[654]1380      r_inputs = getMap (request_inputs, "JobId");
1381      if(reqId>nbReqIdentifier){
[850]1382        if (strncasecmp (REQUEST, "GetStatus", 9) == 0 ||
1383            strncasecmp (REQUEST, "GetResult", 9) == 0){
[654]1384          runGetStatus(m,r_inputs->value,REQUEST);
[850]1385#ifdef RELY_ON_DB
1386          map* dsNb=getMapFromMaps(m,"lenv","ds_nb");
1387          if(dsNb!=NULL && atoi(dsNb->value)>1)
1388            close_sql(m,1);
1389          close_sql(m,0);
1390#endif
1391         
[654]1392          freeMaps (&m);
[839]1393          free(m);
[607]1394          if(zooRegistry!=NULL){
1395            freeRegistry(&zooRegistry);
1396            free(zooRegistry);
1397          }
[654]1398          free (REQUEST);
1399          free (SERVICE_URL);
1400          return 0;
1401        }
1402        else
1403          if (strncasecmp (REQUEST, "Dismiss", strlen(REQUEST)) == 0){
1404            runDismiss(m,r_inputs->value);
1405            freeMaps (&m);
1406            free (m);
1407            if(zooRegistry!=NULL){
1408              freeRegistry(&zooRegistry);
1409              free(zooRegistry);
1410            }
1411            free (REQUEST);
1412            free (SERVICE_URL);
1413            return 0;
1414           
1415          }
1416        return 0;
1417      }
1418      if(reqId<=nbReqIdentifier){
1419        r_inputs = getMap (request_inputs, "Identifier");
[9]1420
[654]1421        struct dirent *dp;
1422        DIR *dirp = opendir (conf_dir);
1423        if (dirp == NULL)
1424          {
[725]1425            errorException (m, _("The specified path does not exist."),
[784]1426                            "InternalError", NULL);
[654]1427            freeMaps (&m);
1428            free (m);
1429            if(zooRegistry!=NULL){
1430              freeRegistry(&zooRegistry);
1431              free(zooRegistry);
1432            }
1433            free (REQUEST);
1434            free (SERVICE_URL);
1435            return 0;
1436          }
1437        if (strncasecmp (REQUEST, "DescribeProcess", 15) == 0)
1438          {
1439            /**
1440             * Loop over Identifier list
1441             */
1442            xmlDocPtr doc = xmlNewDoc (BAD_CAST "1.0");
1443            r_inputs = NULL;
1444            r_inputs = getMap (request_inputs, "version");
[854]1445            fprintf(stderr," ** DEBUG %s %d \n",__FILE__,__LINE__);
1446            fflush(stderr);
[654]1447            xmlNodePtr n = printWPSHeader(doc,m,"DescribeProcess",
[656]1448                                          root_nodes[vid][1],(version!=NULL?version->value:"1.0.0"),1);
[469]1449
[654]1450            r_inputs = getMap (request_inputs, "Identifier");
[503]1451
[654]1452            char *orig = zStrdup (r_inputs->value);
[541]1453
[654]1454            int saved_stdout = dup (fileno (stdout));
1455            dup2 (fileno (stderr), fileno (stdout));
1456            if (strcasecmp ("all", orig) == 0)
1457              {
[789]1458                maps* imports = getMaps(m, IMPORTSERVICE); 
1459                if (imports != NULL) {       
1460                  map* zcfg = imports->content;
1461           
1462                  while (zcfg != NULL) {
1463                    if (zcfg->value != NULL) {
1464                      service* svc = (service*) malloc(SERVICE_SIZE);
1465                      if (svc == NULL || readServiceFile(m, zcfg->value, &svc, zcfg->name) < 0) {
1466                        // pass over silently
1467                        zcfg = zcfg->next;
1468                        continue;
1469                      }
1470                      inheritance(zooRegistry, &svc);
[839]1471#ifdef USE_HPC
1472                      addNestedOutputs(&svc);
1473#endif
1474
1475                      printDescribeProcessForProcess(zooRegistry, m, doc, n, svc);
[789]1476                      freeService(&svc);
1477                      free(svc);                             
1478                    }
1479                    zcfg = zcfg->next;
1480                  }           
1481                }
1482 
[654]1483                if (int res =
[839]1484                    recursReaddirF (m, zooRegistry, doc, n, conf_dir, NULL, saved_stdout, 0,
[654]1485                                    printDescribeProcessForProcess) < 0)
1486                  return res;
[822]1487#ifdef META_DB
[839]1488                fetchServicesFromDb(zooRegistry,m,doc,n,printDescribeProcessForProcess,0);
1489                close_sql(m,0);
[822]1490#endif     
1491
[654]1492              }
1493            else
1494              {
1495                char *saveptr;
1496                char *tmps = strtok_r (orig, ",", &saveptr);
[541]1497
[654]1498                char buff[256];
1499                char buff1[1024];
1500                while (tmps != NULL)
1501                  {
1502                    int hasVal = -1;
1503                    char *corig = zStrdup (tmps);
[789]1504                    map* import = getMapFromMaps (m, IMPORTSERVICE, corig);   
1505                    if (import != NULL && import->value != NULL) 
[654]1506                      {
[822]1507#ifdef META_DB                 
[839]1508                        service* s2=extractServiceFromDb(m,import->name,0);
[822]1509                        if(s2==NULL){
1510#endif
1511                          s1 = (service *) malloc (SERVICE_SIZE);
1512                          t = readServiceFile (m, import->value, &s1, import->name);
[789]1513               
[822]1514                          if (t < 0) // failure reading zcfg
1515                            {
1516                              map *tmp00 = getMapFromMaps (m, "lenv", "message");
1517                              char tmp01[1024];
1518                              if (tmp00 != NULL)
1519                                sprintf (tmp01, _("Unable to parse the ZCFG file: %s (%s)"), import->value, tmp00->value);
1520                              else
1521                                sprintf (tmp01, _("Unable to parse the ZCFG file: %s."), import->value);
1522                             
1523                              dup2 (saved_stdout, fileno (stdout));
1524                              errorException (m, tmp01, "InternalError", NULL);
1525                             
1526                              freeMaps (&m);
1527                              free (m);
[541]1528
[822]1529                              if(zooRegistry!=NULL){
1530                                freeRegistry(&zooRegistry);
1531                                free(zooRegistry);
1532                              }
1533                              free (orig);
1534                              free (REQUEST);
1535                              closedir (dirp);
[854]1536                              //xmlFreeDoc (doc);
[822]1537                              xmlCleanupParser ();
1538                              zooXmlCleanupNs ();
1539                   
1540                              return 1;
[789]1541                            }
1542#ifdef DEBUG
[822]1543                          dumpService (s1);
[789]1544#endif
[822]1545                          inheritance(zooRegistry,&s1);
[839]1546#ifdef USE_HPC
1547                          addNestedOutputs(&s1);
1548#endif
1549                          printDescribeProcessForProcess (zooRegistry, m, doc, n, s1);
[822]1550                          freeService (&s1);
1551                          free (s1);
1552                          s1 = NULL;
1553                          scount++;
1554                          hasVal = 1;               
1555#ifdef META_DB
1556                        }
1557#endif
1558                      }
[789]1559                    else if (strstr (corig, ".") != NULL)
1560                      {
1561
[654]1562                        parseIdentifier (m, conf_dir, corig, buff1);
1563                        map *tmpMap = getMapFromMaps (m, "lenv", "metapath");
1564                        if (tmpMap != NULL)
1565                          addToMap (request_inputs, "metapath", tmpMap->value);
1566                        map *tmpMapI = getMapFromMaps (m, "lenv", "Identifier");
[854]1567                        /**
1568                         * No support for dot in service name stored in metadb!?
[822]1569#ifdef META_DB
[854]1570                        service* s2=extractServiceFromDb(m,tmpMapI->value,0);
[822]1571                        if(s2==NULL){
1572#endif
[854]1573                        */
[822]1574                          s1 = (service *) malloc (SERVICE_SIZE);
1575                          t = readServiceFile (m, buff1, &s1, tmpMapI->value);
1576                          if (t < 0)
1577                            {
1578                              map *tmp00 = getMapFromMaps (m, "lenv", "message");
1579                              char tmp01[1024];
1580                              if (tmp00 != NULL)
1581                                sprintf (tmp01,
1582                                         _
1583                                         ("Unable to parse the ZCFG file for the following ZOO-Service: %s. Message: %s"),
1584                                         tmps, tmp00->value);
1585                              else
1586                                sprintf (tmp01,
1587                                         _
1588                                         ("Unable to parse the ZCFG file for the following ZOO-Service: %s."),
1589                                         tmps);
1590                              dup2 (saved_stdout, fileno (stdout));
1591                              errorException (m, tmp01, "InvalidParameterValue",
1592                                              "identifier");
1593                              freeMaps (&m);
1594                              free (m);
1595                              if(zooRegistry!=NULL){
1596                                freeRegistry(&zooRegistry);
1597                                free(zooRegistry);
1598                              }
1599                              free (REQUEST);
1600                              free (corig);
1601                              free (orig);
1602                              free (SERVICE_URL);
1603                              free (s1);
1604                              closedir (dirp);
[854]1605                              //xmlFreeDoc (doc);
[822]1606                              xmlCleanupParser ();
1607                              zooXmlCleanupNs ();
1608                              return 1;
[654]1609                            }
[9]1610#ifdef DEBUG
[822]1611                          dumpService (s1);
[9]1612#endif
[822]1613                          inheritance(zooRegistry,&s1);
[839]1614#ifdef USE_HPC
1615                          addNestedOutputs(&s1);
1616#endif
1617                          printDescribeProcessForProcess (zooRegistry, m, doc, n, s1);
[822]1618                          freeService (&s1);
1619                          free (s1);
1620                          s1 = NULL;
1621                          scount++;
1622                          hasVal = 1;
1623                          setMapInMaps (m, "lenv", "level", "0");
[854]1624                          /*
[822]1625#ifdef META_DB
1626                        }
1627#endif
[854]1628                          */
[654]1629                      }
1630                    else
1631                      {
[822]1632#ifdef META_DB
1633                        _init_sql(m,"metadb");
[839]1634                        //FAILED CONNECTING DB
1635                        if(getMapFromMaps(m,"lenv","dbIssue")!=NULL){
1636                          fprintf(stderr,"ERROR CONNECTING METADB");
1637                        }
1638                        service* s2=extractServiceFromDb(m,corig,0);
[822]1639                        if(s2!=NULL){
1640                          inheritance(zooRegistry,&s2);
[839]1641#ifdef USE_HPC
1642                          addNestedOutputs(&s2);
1643#endif
1644                          printDescribeProcessForProcess (zooRegistry,m, doc, n, s2);
[822]1645                          freeService (&s2);
1646                          free (s2);
1647                          s2 = NULL;
1648                          hasVal = 1;
1649                        }else /*TOTO*/{
1650#endif
[654]1651                        memset (buff, 0, 256);
1652                        snprintf (buff, 256, "%s.zcfg", corig);
1653                        memset (buff1, 0, 1024);
[469]1654#ifdef DEBUG
[822]1655                          printf ("\n#######%s\n########\n", buff);
[469]1656#endif
[822]1657                          while ((dp = readdir (dirp)) != NULL)
1658                            {
1659                              if (strcasecmp (dp->d_name, buff) == 0)
1660                                {
1661                                  memset (buff1, 0, 1024);
1662                                  snprintf (buff1, 1024, "%s/%s", conf_dir,
1663                                            dp->d_name);
1664                                  s1 = (service *) malloc (SERVICE_SIZE);
1665                                  if (s1 == NULL)
1666                                    {
1667                                      dup2 (saved_stdout, fileno (stdout));
1668                                      return errorException (m,
1669                                                             _
1670                                                             ("Unable to allocate memory"),
1671                                                             "InternalError",
1672                                                             NULL);
1673                                    }
[790]1674#ifdef DEBUG_SERVICE_CONF
[822]1675                                  fprintf
1676                                    (stderr,"#################\n(%s) %s\n#################\n",
1677                                     r_inputs->value, buff1);
[469]1678#endif
[822]1679                                  char *tmp0 = zStrdup (dp->d_name);
1680                                  tmp0[strlen (tmp0) - 5] = 0;
1681                                  t = readServiceFile (m, buff1, &s1, tmp0);
1682                                  free (tmp0);
1683                                  if (t < 0)
1684                                    {
1685                                      map *tmp00 =
1686                                        getMapFromMaps (m, "lenv", "message");
1687                                      char tmp01[1024];
1688                                      if (tmp00 != NULL)
1689                                        sprintf (tmp01,
1690                                                 _
1691                                                 ("Unable to parse the ZCFG file: %s (%s)"),
1692                                                 dp->d_name, tmp00->value);
1693                                      else
1694                                        sprintf (tmp01,
1695                                                 _
1696                                                 ("Unable to parse the ZCFG file: %s."),
1697                                                 dp->d_name);
1698                                      dup2 (saved_stdout, fileno (stdout));
1699                                      errorException (m, tmp01, "InternalError",
1700                                                      NULL);
1701                                      freeMaps (&m);
1702                                      free (m);
1703                                      if(zooRegistry!=NULL){
1704                                        freeRegistry(&zooRegistry);
1705                                        free(zooRegistry);
1706                                      }
1707                                      free (orig);
1708                                      free (REQUEST);
1709                                      closedir (dirp);
[854]1710                                      //xmlFreeDoc (doc);
[822]1711                                      xmlCleanupParser ();
1712                                      zooXmlCleanupNs ();
1713                                      return 1;
[654]1714                                    }
[469]1715#ifdef DEBUG
[822]1716                                  dumpService (s1);
[469]1717#endif
[822]1718                                  inheritance(zooRegistry,&s1);
[839]1719#ifdef USE_HPC
1720                                  addNestedOutputs(&s1);
1721#endif
1722                                  printDescribeProcessForProcess (zooRegistry,m, doc, n, s1);
[822]1723                                  freeService (&s1);
1724                                  free (s1);
1725                                  s1 = NULL;
1726                                  scount++;
1727                                  hasVal = 1;
1728                                }
1729                            }
1730#ifdef META_DB
1731                        }
1732#endif
1733                      }               
[654]1734                    if (hasVal < 0)
1735                      {
1736                        map *tmp00 = getMapFromMaps (m, "lenv", "message");
1737                        char tmp01[1024];
1738                        if (tmp00 != NULL)
1739                          sprintf (tmp01,
1740                                   _("Unable to parse the ZCFG file: %s (%s)"),
1741                                   buff, tmp00->value);
1742                        else
1743                          sprintf (tmp01,
1744                                   _("Unable to parse the ZCFG file: %s."),
1745                                   buff);
1746                        dup2 (saved_stdout, fileno (stdout));
1747                        errorException (m, tmp01, "InvalidParameterValue",
1748                                        "Identifier");
1749                        freeMaps (&m);
1750                        free (m);
1751                        if(zooRegistry!=NULL){
1752                          freeRegistry(&zooRegistry);
1753                          free(zooRegistry);
1754                        }
1755                        free (orig);
1756                        free (REQUEST);
1757                        closedir (dirp);
[854]1758                        //xmlFreeDoc (doc);
[654]1759                        xmlCleanupParser ();
1760                        zooXmlCleanupNs ();
1761                        return 1;
1762                      }
1763                    rewinddir (dirp);
1764                    tmps = strtok_r (NULL, ",", &saveptr);
1765                    if (corig != NULL)
1766                      free (corig);
1767                  }
1768              }
1769            closedir (dirp);
1770            fflush (stdout);
1771            dup2 (saved_stdout, fileno (stdout));
1772            free (orig);
1773            printDocument (m, doc, getpid ());
1774            freeMaps (&m);
1775            free (m);
1776            if(zooRegistry!=NULL){
1777              freeRegistry(&zooRegistry);
1778              free(zooRegistry);
1779            }
1780            free (REQUEST);
1781            free (SERVICE_URL);
1782            fflush (stdout);
[839]1783#ifdef META_DB
1784            close_sql(m,0);
1785      //end_sql();
1786#endif
[654]1787            return 0;
[607]1788          }
[654]1789        else if (strncasecmp (REQUEST, "Execute", strlen (REQUEST)) != 0)
1790          {
1791            map* version=getMapFromMaps(m,"main","rversion");
[738]1792            int vid=getVersionId(version->value);           
1793                int len = 0;
1794                int j = 0;
[654]1795            for(j=0;j<nbSupportedRequests;j++){
1796              if(requests[vid][j]!=NULL)
1797                len+=strlen(requests[vid][j])+2;
1798              else{
1799                len+=4;
1800                break;
1801              }
1802            }
1803            char *tmpStr=(char*)malloc(len*sizeof(char));
1804            int it=0;
1805            for(j=0;j<nbSupportedRequests;j++){
1806              if(requests[vid][j]!=NULL){
1807                if(it==0){
1808                  sprintf(tmpStr,"%s",requests[vid][j]);
1809                  it++;
1810                }else{
1811                  char *tmpS=zStrdup(tmpStr);
1812                  if(j+1<nbSupportedRequests && requests[vid][j+1]==NULL){
1813                    sprintf(tmpStr,"%s and %s",tmpS,requests[vid][j]);
1814                  }else{
1815                    sprintf(tmpStr,"%s, %s",tmpS,requests[vid][j]);
1816                 
1817                  }
1818                  free(tmpS);
1819                }
1820              }
1821              else{
1822                len+=4;
1823                break;
1824              }
1825            }
1826            char* message=(char*)malloc((61+len)*sizeof(char));
1827            sprintf(message,"The <request> value was not recognized. Allowed values are %s.",tmpStr);
1828            errorException (m,_(message),"InvalidParameterValue", "request");
[541]1829#ifdef DEBUG
[654]1830            fprintf (stderr, "No request found %s", REQUEST);
[541]1831#endif
[654]1832            closedir (dirp);
1833            freeMaps (&m);
1834            free (m);
1835            if(zooRegistry!=NULL){
1836              freeRegistry(&zooRegistry);
1837              free(zooRegistry);
1838            }
1839            free (REQUEST);
1840            free (SERVICE_URL);
1841            fflush (stdout);
1842            return 0;
[607]1843          }
[654]1844        closedir (dirp);
1845      }
[1]1846    }
[541]1847
[654]1848  map *postRequest = NULL;
1849  postRequest = getMap (request_inputs, "xrequest");
[790]1850 
[654]1851  if(vid==1 && postRequest==NULL){
1852    errorException (m,_("Unable to run Execute request using the GET HTTP method"),"InvalidParameterValue", "request"); 
1853    freeMaps (&m);
1854    free (m);
1855    if(zooRegistry!=NULL){
1856      freeRegistry(&zooRegistry);
1857      free(zooRegistry);
1858    }
1859    free (REQUEST);
1860    free (SERVICE_URL);
1861    fflush (stdout);
1862    return 0;
1863  }
[541]1864  s1 = NULL;
[854]1865 
[541]1866  r_inputs = getMap (request_inputs, "Identifier");
[789]1867  map* import = getMapFromMaps (m, IMPORTSERVICE, r_inputs->value); 
1868  if (import != NULL && import->value != NULL) { 
1869      strncpy(tmps1, import->value, 1024);
1870      setMapInMaps (m, "lenv", "Identifier", r_inputs->value);
1871      setMapInMaps (m, "lenv", "oIdentifier", r_inputs->value);
1872  } 
1873  else {
1874    snprintf (tmps1, 1024, "%s/%s.zcfg", conf_dir, r_inputs->value);
[1]1875#ifdef DEBUG
[789]1876    fprintf (stderr, "Trying to load %s\n", tmps1);
[1]1877#endif
[789]1878    if (strstr (r_inputs->value, ".") != NULL)
1879      {
1880        char *identifier = zStrdup (r_inputs->value);
1881        parseIdentifier (m, conf_dir, identifier, tmps1);
1882        map *tmpMap = getMapFromMaps (m, "lenv", "metapath");
1883        if (tmpMap != NULL)
1884          addToMap (request_inputs, "metapath", tmpMap->value);
1885        free (identifier);
1886      }
1887    else
1888      {
1889        setMapInMaps (m, "lenv", "Identifier", r_inputs->value);
1890        setMapInMaps (m, "lenv", "oIdentifier", r_inputs->value);
1891      }
1892  }
[539]1893
[541]1894  r_inputs = getMapFromMaps (m, "lenv", "Identifier");
[839]1895 
1896#ifdef META_DB
1897  int metadb_id=_init_sql(m,"metadb");
1898  fprintf(stderr,"CONNECTING METADB!\n");
1899  //FAILED CONNECTING DB
[854]1900  if(getMapFromMaps(m,"lenv","dbIssue")!=NULL || metadb_id<0){
[839]1901    fprintf(stderr,"ERROR CONNECTING METADB\n");
1902  }
[854]1903  if(metadb_id>=0)
1904    s1=extractServiceFromDb(m,r_inputs->value,0);
[839]1905  //close_sql(m,0);
1906  if(s1!=NULL){
1907    inheritance(zooRegistry,&s1);
1908#ifdef USE_HPC
1909    addNestedOutputs(&s1);
1910#endif
1911    if(zooRegistry!=NULL){
1912      freeRegistry(&zooRegistry);
1913      free(zooRegistry);
1914    }
1915  }else /* Not found in MetaDB */{
1916#endif
1917  s1 = (service *) malloc (SERVICE_SIZE);
1918  if (s1 == NULL)
1919    {
1920      freeMaps (&m);
1921      free (m);
1922      if(zooRegistry!=NULL){
1923        freeRegistry(&zooRegistry);
1924        free(zooRegistry);
1925      }
1926      free (REQUEST);
1927      free (SERVICE_URL);
1928      return errorException (m, _("Unable to allocate memory"),
1929                             "InternalError", NULL);
1930    }
1931
[541]1932  int saved_stdout = dup (fileno (stdout));
1933  dup2 (fileno (stderr), fileno (stdout));
1934  t = readServiceFile (m, tmps1, &s1, r_inputs->value);
[607]1935  inheritance(zooRegistry,&s1);
[839]1936#ifdef USE_HPC
1937  addNestedOutputs(&s1);
1938#endif
[607]1939  if(zooRegistry!=NULL){
1940    freeRegistry(&zooRegistry);
1941    free(zooRegistry);
1942  }
[541]1943  fflush (stdout);
1944  dup2 (saved_stdout, fileno (stdout));
1945  if (t < 0)
1946    {
1947      char *tmpMsg = (char *) malloc (2048 + strlen (r_inputs->value));
1948      sprintf (tmpMsg,
1949               _
[605]1950               ("The value for <identifier> seems to be wrong (%s). Please specify one of the processes in the list returned by a GetCapabilities request."),
[541]1951               r_inputs->value);
1952      errorException (m, tmpMsg, "InvalidParameterValue", "identifier");
1953      free (tmpMsg);
1954      free (s1);
1955      freeMaps (&m);
1956      free (m);
1957      free (REQUEST);
1958      free (SERVICE_URL);
1959      return 0;
1960    }
1961  close (saved_stdout);
[839]1962#ifdef META_DB
1963  }
1964#endif
1965 
[1]1966#ifdef DEBUG
[541]1967  dumpService (s1);
[1]1968#endif
1969  int j;
[381]1970
[541]1971
[1]1972  /**
[344]1973   * Create the input and output maps data structure
[1]1974   */
[541]1975  int i = 0;
[1]1976  HINTERNET hInternet;
1977  HINTERNET res;
[541]1978  hInternet = InternetOpen (
[1]1979#ifndef WIN32
[654]1980                            (LPCTSTR)
[1]1981#endif
[654]1982                            "ZooWPSClient\0",
1983                            INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
[1]1984
1985#ifndef WIN32
[541]1986  if (!CHECK_INET_HANDLE (hInternet))
1987    fprintf (stderr, "WARNING : hInternet handle failed to initialize");
[1]1988#endif
[541]1989  maps *request_input_real_format = NULL;
1990  maps *tmpmaps = request_input_real_format;
1991
[621]1992  if(parseRequest(&m,&request_inputs,s1,&request_input_real_format,&request_output_real_format,&hInternet)<0){
1993    freeMaps (&m);
1994    free (m);
1995    free (REQUEST);
1996    free (SERVICE_URL);
1997    InternetCloseHandle (&hInternet);
1998    freeService (&s1);
1999    free (s1);
2000    return 0;
2001  }
[839]2002  //InternetCloseHandle (&hInternet);
2003 
[652]2004  // Define each env variable in runing environment
[541]2005  maps *curs = getMaps (m, "env");
2006  if (curs != NULL)
2007    {
2008      map *mapcs = curs->content;
2009      while (mapcs != NULLMAP)
2010        {
[1]2011#ifndef WIN32
[541]2012          setenv (mapcs->name, mapcs->value, 1);
[1]2013#ifdef DEBUG
[541]2014          fprintf (stderr, "[ZOO: setenv (%s=%s)]\n", mapcs->name,
2015                   mapcs->value);
[1]2016#endif
[767]2017#else
[541]2018          if (mapcs->value[strlen (mapcs->value) - 2] == '\r')
2019            {
[1]2020#ifdef DEBUG
[541]2021              fprintf (stderr, "[ZOO: Env var finish with \r]\n");
[1]2022#endif
[541]2023              mapcs->value[strlen (mapcs->value) - 1] = 0;
2024            }
[1]2025#ifdef DEBUG
[541]2026          if (SetEnvironmentVariable (mapcs->name, mapcs->value) == 0)
2027            {
2028              fflush (stderr);
2029              fprintf (stderr, "setting variable... %s\n", "OK");
2030            }
2031          else
2032            {
2033              fflush (stderr);
2034              fprintf (stderr, "setting variable... %s\n", "OK");
2035            }
[1]2036#else
[541]2037          SetEnvironmentVariable (mapcs->name, mapcs->value);
[1]2038#endif
[541]2039          char *toto =
2040            (char *)
2041            malloc ((strlen (mapcs->name) + strlen (mapcs->value) +
2042                     2) * sizeof (char));
2043          sprintf (toto, "%s=%s", mapcs->name, mapcs->value);
2044          putenv (toto);
[1]2045#ifdef DEBUG
[541]2046          fflush (stderr);
[1]2047#endif
2048#endif
2049#ifdef DEBUG
[541]2050          fprintf (stderr, "[ZOO: setenv (%s=%s)]\n", mapcs->name,
2051                   mapcs->value);
2052          fflush (stderr);
[1]2053#endif
[541]2054          mapcs = mapcs->next;
2055        }
[1]2056    }
[541]2057
[1]2058#ifdef DEBUG
[541]2059  dumpMap (request_inputs);
[1]2060#endif
2061
[541]2062  map *status = getMap (request_inputs, "status");
[654]2063  if(vid==0){
2064    // Need to check if we need to fork to load a status enabled
2065    r_inputs = NULL;
2066    map *store = getMap (request_inputs, "storeExecuteResponse");
2067    /**
2068     * 05-007r7 WPS 1.0.0 page 57 :
2069     * 'If status="true" and storeExecuteResponse is "false" then the service
2070     * shall raise an exception.'
2071     */
2072    if (status != NULL && strcmp (status->value, "true") == 0 &&
2073        store != NULL && strcmp (store->value, "false") == 0)
2074      {
2075        errorException (m,
2076                        _
2077                        ("The status parameter cannot be set to true if storeExecuteResponse is set to false. Please modify your request parameters."),
2078                        "InvalidParameterValue", "storeExecuteResponse");
2079        freeService (&s1);
2080        free (s1);
2081        freeMaps (&m);
2082        free (m);
2083       
2084        freeMaps (&request_input_real_format);
2085        free (request_input_real_format);
2086       
2087        freeMaps (&request_output_real_format);
2088        free (request_output_real_format);
[94]2089
[654]2090        free (REQUEST);
2091        free (SERVICE_URL);
2092        return 1;
2093      }
2094    r_inputs = getMap (request_inputs, "storeExecuteResponse");
2095  }else{
2096    // Define status depending on the WPS 2.0.0 mode attribute
2097    status = getMap (request_inputs, "mode");
2098    map* mode=getMap(s1->content,"mode");
2099    if(strcasecmp(status->value,"async")==0){
2100      if(mode!=NULL && strcasecmp(mode->value,"async")==0)
2101        addToMap(request_inputs,"status","true");
2102      else{
2103        if(mode!=NULL){
2104          // see ref. http://docs.opengeospatial.org/is/14-065/14-065.html#61
2105          errorException (m,_("The process does not permit the desired execution mode."),"NoSuchMode", mode->value); 
2106          fflush (stdout);
2107          freeMaps (&m);
2108          free (m);
2109          if(zooRegistry!=NULL){
2110            freeRegistry(&zooRegistry);
2111            free(zooRegistry);
2112          }
2113          freeMaps (&request_input_real_format);
2114          free (request_input_real_format);
2115          freeMaps (&request_output_real_format);
2116          free (request_output_real_format);
2117          free (REQUEST);
2118          free (SERVICE_URL);
2119          return 0;
2120        }else
2121          addToMap(request_inputs,"status","true");
2122      }
2123    }
2124    else{
2125      if(strcasecmp(status->value,"auto")==0){
2126        if(mode!=NULL){
2127          if(strcasecmp(mode->value,"async")==0)
2128            addToMap(request_inputs,"status","false");
2129          else
2130            addToMap(request_inputs,"status","true");
2131        }
2132        else
2133          addToMap(request_inputs,"status","false");
2134      }else
2135        addToMap(request_inputs,"status","false");
2136    }
2137    status = getMap (request_inputs, "status");
2138  }
[541]2139
2140  int eres = SERVICE_STARTED;
2141  int cpid = getpid ();
2142
[839]2143  // Create a map containing a copy of the request map
2144  maps *_tmpMaps = createMaps("request");
2145  addMapToMap(&_tmpMaps->content,request_inputs);
2146  addMapsToMaps (&m, _tmpMaps);
2147  freeMaps (&_tmpMaps);
2148  free (_tmpMaps);
[453]2149  /**
2150   * Initialize the specific [lenv] section which contains runtime variables:
2151   *
[682]2152   *  - usid : it is an universally unique identifier 
2153   *  - osid : it is an idenfitication number
[453]2154   *  - sid : it is the process idenfitication number (OS)
[682]2155   *  - uusid : it is an universally unique identifier
[453]2156   *  - status : value between 0 and 100 to express the  completude of
2157   * the operations of the running service
2158   *  - message : is a string where you can store error messages, in case
2159   * service is failing, or o provide details on the ongoing operation.
[784]2160   *  - cwd : the current working directory or servicePath if defined
[453]2161   *  - soap : is a boolean value, true if the request was contained in a SOAP
2162   * Envelop
2163   *  - sessid : string storing the session identifier (only when cookie is
2164   * used)
2165   *  - cgiSid : only defined on Window platforms (for being able to identify
2166   * the created process)
2167   *
2168   */
[839]2169  _tmpMaps = createMaps("lenv");
[32]2170  char tmpBuff[100];
[514]2171  struct ztimeval tp;
[541]2172  if (zGettimeofday (&tp, NULL) == 0)
2173    sprintf (tmpBuff, "%i", (cpid + ((int) tp.tv_sec + (int) tp.tv_usec)));
[514]2174  else
[541]2175    sprintf (tmpBuff, "%i", (cpid + (int) time (NULL)));
[652]2176  _tmpMaps->content = createMap ("osid", tmpBuff);
[541]2177  sprintf (tmpBuff, "%i", cpid);
2178  addToMap (_tmpMaps->content, "sid", tmpBuff);
[640]2179  char* tmpUuid=get_uuid();
2180  addToMap (_tmpMaps->content, "uusid", tmpUuid);
[652]2181  addToMap (_tmpMaps->content, "usid", tmpUuid);
[640]2182  free(tmpUuid);
[541]2183  addToMap (_tmpMaps->content, "status", "0");
[784]2184  if(cwdMap0!=NULL){
2185    addToMap (_tmpMaps->content, "cwd", cwdMap0->value);
2186    addToMap (_tmpMaps->content, "rcwd", ntmp);
2187  }
2188  else
2189    addToMap (_tmpMaps->content, "cwd", ntmp);
[541]2190  addToMap (_tmpMaps->content, "message", _("No message provided"));
2191  map *ltmp = getMap (request_inputs, "soap");
2192  if (ltmp != NULL)
2193    addToMap (_tmpMaps->content, "soap", ltmp->value);
[280]2194  else
[541]2195    addToMap (_tmpMaps->content, "soap", "false");
[654]2196
[850]2197  // Parse the session file and add it to the main maps
2198  char* originalCookie=NULL;
[541]2199  if (cgiCookie != NULL && strlen (cgiCookie) > 0)
2200    {
2201      int hasValidCookie = -1;
[854]2202      char *tcook = originalCookie = zStrdup (cgiCookie);
[541]2203      map *testing = getMapFromMaps (m, "main", "cookiePrefix");
[854]2204      parseCookie(&m,originalCookie);
[860]2205      map *sessId=getMapFromMaps(m,"cookies",(testing==NULL?"ID":testing->value));
2206      if (sessId!=NULL)
[541]2207        {
[860]2208          addToMap (_tmpMaps->content, "sessid", sessId->value);
[541]2209          char session_file_path[1024];
2210          map *tmpPath = getMapFromMaps (m, "main", "sessPath");
2211          if (tmpPath == NULL)
2212            tmpPath = getMapFromMaps (m, "main", "tmpPath");
2213          char *tmp1 = strtok (tcook, ";");
2214          if (tmp1 != NULL)
2215            sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value,
[860]2216                     sessId->value);
[541]2217          else
2218            sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value,
[860]2219                     sessId->value);
[541]2220          free (tcook);
2221          maps *tmpSess = (maps *) malloc (MAPS_SIZE);
[790]2222          tmpSess->child=NULL;
[541]2223          struct stat file_status;
2224          int istat = stat (session_file_path, &file_status);
2225          if (istat == 0 && file_status.st_size > 0)
2226            {
[854]2227              int saved_stdout = dup (fileno (stdout));
2228              dup2 (fileno (stderr), fileno (stdout));
[541]2229              conf_read (session_file_path, tmpSess);
2230              addMapsToMaps (&m, tmpSess);
2231              freeMaps (&tmpSess);
[854]2232              fflush(stdout);
2233              dup2 (saved_stdout, fileno (stdout));
2234              close(saved_stdout);
[541]2235            }
[817]2236          free (tmpSess);
[541]2237        }
[92]2238    }
[541]2239  addMapsToMaps (&m, _tmpMaps);
2240  freeMaps (&_tmpMaps);
2241  free (_tmpMaps);
[654]2242  maps* bmap=NULL;
[1]2243#ifdef DEBUG
[541]2244  dumpMap (request_inputs);
[1]2245#endif
[772]2246  int ei = 1;
[839]2247  char **orig = 
[216]2248#ifdef WIN32
[839]2249    GetEnvironmentStrings()
[772]2250#else
[839]2251    environ
[772]2252#endif
[839]2253    ;
2254  char *s=*orig;
[790]2255  _tmpMaps = createMaps("renv");
[772]2256  for (; s; ei++) {
[851]2257    if(strstr(s,"=")!=NULL && strlen(strstr(s,"="))>1){
2258      int len=strlen(s);
2259      char* tmpName=zStrdup(s);
2260      char* tmpValue=strstr(s,"=")+1;
[854]2261      char* tmpName1=(char*)malloc((1+(len-(strlen(tmpValue)+1)))*sizeof(char));
2262      snprintf(tmpName1,(len-strlen(tmpValue)),"%s",tmpName);
[851]2263      if(_tmpMaps->content == NULL)
2264        _tmpMaps->content = createMap (tmpName1,tmpValue);
2265      else
2266        addToMap (_tmpMaps->content,tmpName1,tmpValue);
2267      free(tmpName1);
2268      free(tmpName);
2269    }
[839]2270    s = *(orig+ei);
[772]2271  }
[850]2272  if(_tmpMaps->content!=NULL && getMap(_tmpMaps->content,"HTTP_COOKIE")!=NULL){
2273    addToMap(_tmpMaps->content,"HTTP_COOKIE1",&cgiCookie[0]);
2274  }
[772]2275  addMapsToMaps (&m, _tmpMaps);
2276  freeMaps (&_tmpMaps);
2277  free (_tmpMaps);
[854]2278  if(postRequest!=NULL)
2279    setMapInMaps (m, "renv", "xrequest", postRequest->value);
2280  //dumpMaps(m);
[772]2281#ifdef WIN32
[541]2282  char *cgiSidL = NULL;
2283  if (getenv ("CGISID") != NULL)
2284    addToMap (request_inputs, "cgiSid", getenv ("CGISID"));
[554]2285
2286  char* usidp;
2287  if ( (usidp = getenv("USID")) != NULL ) {
2288    setMapInMaps (m, "lenv", "usid", usidp);
2289  }
2290
[541]2291  map *test1 = getMap (request_inputs, "cgiSid");
[680]2292  if (test1 != NULL){
[682]2293    cgiSid = zStrdup(test1->value);
2294    addToMap (request_inputs, "storeExecuteResponse", "true");
2295    addToMap (request_inputs, "status", "true");
2296    setMapInMaps (m, "lenv", "osid", test1->value);
2297    status = getMap (request_inputs, "status");
2298  }
[680]2299  test1 = getMap (request_inputs, "usid");
2300  if (test1 != NULL){
2301    setMapInMaps (m, "lenv", "usid", test1->value);
2302    setMapInMaps (m, "lenv", "uusid", test1->value);
2303  }
[216]2304#endif
[788]2305
[654]2306  char *fbkp, *fbkpid, *fbkpres, *fbkp1, *flog;
[541]2307  FILE *f0, *f1;
2308  if (status != NULL)
2309    if (strcasecmp (status->value, "false") == 0)
2310      status = NULLMAP;
2311  if (status == NULLMAP)
2312    {
[621]2313      if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
2314        freeService (&s1);
2315        free (s1);
2316        freeMaps (&m);
2317        free (m);
2318        free (REQUEST);
2319        free (SERVICE_URL);
2320        freeMaps (&request_input_real_format);
2321        free (request_input_real_format);
2322        freeMaps (&request_output_real_format);
2323        free (request_output_real_format);
2324        freeMaps (&tmpmaps);
2325        free (tmpmaps);
2326        return -1;
2327      }
[541]2328      loadServiceAndRun (&m, s1, request_inputs, &request_input_real_format,
2329                         &request_output_real_format, &eres);
[854]2330
[851]2331#ifdef META_DB
[854]2332      close_sql(m,0);     
2333#endif     
[541]2334    }
2335  else
2336    {
2337      int pid;
[1]2338#ifdef DEBUG
[541]2339      fprintf (stderr, "\nPID : %d\n", cpid);
[1]2340#endif
[9]2341
[1]2342#ifndef WIN32
[541]2343      pid = fork ();
[1]2344#else
[541]2345      if (cgiSid == NULL)
2346        {
2347          createProcess (m, request_inputs, s1, NULL, cpid,
2348                         request_input_real_format,
2349                         request_output_real_format);
2350          pid = cpid;
2351        }
2352      else
2353        {
2354          pid = 0;
2355          cpid = atoi (cgiSid);
[680]2356          updateStatus(m,0,_("Initializing"));
[541]2357        }
[1]2358#endif
[854]2359      //InternetCloseHandle (&hInternet);
[541]2360      if (pid > 0)
2361        {
[654]2362          /**
2363           * dady :
2364           * set status to SERVICE_ACCEPTED
2365           */
[1]2366#ifdef DEBUG
[541]2367          fprintf (stderr, "father pid continue (origin %d) %d ...\n", cpid,
2368                   getpid ());
[1]2369#endif
[541]2370          eres = SERVICE_ACCEPTED;
2371        }
2372      else if (pid == 0)
2373        {
[621]2374          /**
2375           * son : have to close the stdout, stdin and stderr to let the parent
2376           * process answer to http client.
2377           */
[652]2378          map* usid = getMapFromMaps (m, "lenv", "uusid");
[654]2379          map* tmpm = getMapFromMaps (m, "lenv", "osid");
2380          int cpid = atoi (tmpm->value);
[541]2381          r_inputs = getMapFromMaps (m, "main", "tmpPath");
[839]2382          setMapInMaps (m, "lenv", "async","true");
[652]2383          map* r_inputs1 = createMap("ServiceName", s1->name);
[605]2384
[654]2385          // Create the filename for the result file (.res)
2386          fbkpres =
2387            (char *)
2388            malloc ((strlen (r_inputs->value) +
[788]2389                     strlen (usid->value) + 7) * sizeof (char));                   
[654]2390          sprintf (fbkpres, "%s/%s.res", r_inputs->value, usid->value);
[790]2391          bmap = createMaps("status");
[654]2392          bmap->content=createMap("usid",usid->value);
2393          addToMap(bmap->content,"sid",tmpm->value);
2394          addIntToMap(bmap->content,"pid",getpid());
2395         
[652]2396          // Create PID file referencing the OS process identifier
2397          fbkpid =
2398            (char *)
2399            malloc ((strlen (r_inputs->value) +
2400                     strlen (usid->value) + 7) * sizeof (char));
2401          sprintf (fbkpid, "%s/%s.pid", r_inputs->value, usid->value);
[772]2402          setMapInMaps (m, "lenv", "file.pid", fbkpid);
[652]2403
[680]2404          f0 = freopen (fbkpid, "w+",stdout);
2405          printf("%d",getpid());
[652]2406          fflush(stdout);
2407
2408          // Create SID file referencing the semaphore name
[541]2409          fbkp =
2410            (char *)
2411            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
[652]2412                     strlen (usid->value) + 7) * sizeof (char));
2413          sprintf (fbkp, "%s/%s.sid", r_inputs->value, usid->value);
[772]2414          setMapInMaps (m, "lenv", "file.sid", fbkp);
[680]2415          FILE* f2 = freopen (fbkp, "w+",stdout);
2416          printf("%s",tmpm->value);
[652]2417          fflush(f2);
2418          free(fbkp);
2419
2420          fbkp =
[541]2421            (char *)
2422            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
[652]2423                     strlen (usid->value) + 7) * sizeof (char));
2424          sprintf (fbkp, "%s/%s_%s.xml", r_inputs->value, r_inputs1->value,
2425                   usid->value);
[772]2426          setMapInMaps (m, "lenv", "file.responseInit", fbkp);
[652]2427          flog =
2428            (char *)
2429            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
2430                     strlen (usid->value) + 13) * sizeof (char));
2431          sprintf (flog, "%s/%s_%s_error.log", r_inputs->value,
2432                   r_inputs1->value, usid->value);
[772]2433          setMapInMaps (m, "lenv", "file.log", flog);
[1]2434#ifdef DEBUG
[541]2435          fprintf (stderr, "RUN IN BACKGROUND MODE \n");
2436          fprintf (stderr, "son pid continue (origin %d) %d ...\n", cpid,
2437                   getpid ());
2438          fprintf (stderr, "\nFILE TO STORE DATA %s\n", r_inputs->value);
[1]2439#endif
[541]2440          freopen (flog, "w+", stderr);
2441          fflush (stderr);
[654]2442          f0 = freopen (fbkp, "w+", stdout);
2443          rewind (stdout);
[458]2444#ifndef WIN32
[654]2445          fclose (stdin);
[458]2446#endif
[652]2447#ifdef RELY_ON_DB
2448          init_sql(m);
2449          recordServiceStatus(m);
2450#endif
[850]2451          fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
2452          invokeCallback(m,NULL,NULL,0,0);
2453          fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
[654]2454          if(vid==0){
2455            /**
2456             * set status to SERVICE_STARTED and flush stdout to ensure full
2457             * content was outputed (the file used to store the ResponseDocument).
2458             * The rewind stdout to restart writing from the bgining of the file,
2459             * this way the data will be updated at the end of the process run.
2460             */
2461            printProcessResponse (m, request_inputs, cpid, s1, r_inputs1->value,
2462                                  SERVICE_STARTED, request_input_real_format,
2463                                  request_output_real_format);
2464            fflush (stdout);
2465#ifdef RELY_ON_DB
2466            recordResponse(m,fbkp);
2467#endif
2468          }
2469
[631]2470          fflush (stderr);
[654]2471
[631]2472          fbkp1 =
2473            (char *)
2474            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
[652]2475                     strlen (usid->value) + 13) * sizeof (char));
2476          sprintf (fbkp1, "%s/%s_final_%s.xml", r_inputs->value,
2477                   r_inputs1->value, usid->value);
[772]2478          setMapInMaps (m, "lenv", "file.responseFinal", fbkp1);
[631]2479
2480          f1 = freopen (fbkp1, "w+", stdout);
2481
[839]2482          map* serviceTypeMap=getMap(s1->content,"serviceType");
2483          if(serviceTypeMap!=NULL)
2484            setMapInMaps (m, "lenv", "serviceType", serviceTypeMap->value);
2485
2486          char *flenv =
2487            (char *)
2488            malloc ((strlen (r_inputs->value) + 
2489                     strlen (usid->value) + 12) * sizeof (char));
2490          sprintf (flenv, "%s/%s_lenv.cfg", r_inputs->value, usid->value);
2491          maps* lenvMaps=getMaps(m,"lenv");
2492          dumpMapsToFile(lenvMaps,flenv,0);
2493          free(flenv);
2494
2495          fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
2496          invokeCallback(m,request_input_real_format,NULL,1,0);
2497          fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
[860]2498          //dumpMaps(request_output_real_format);
[621]2499          if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
2500            freeService (&s1);
2501            free (s1);
[854]2502            fflush (stdout);
2503            fflush (stderr);
[765]2504            fclose (f0);
2505            fclose (f1);
2506            if(dumpBackFinalFile(m,fbkp,fbkp1)<0)
2507              return -1;
[854]2508            //dumpBackFinalFile(m,fbkp,fbkp1);
2509#ifndef RELY_ON_DB
2510            dumpMapsToFile(bmap,fbkpres,1);
2511            removeShmLock (m, 1);
2512#else
2513            recordResponse(m,fbkp1);
2514            fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
2515            invokeCallback(m,NULL,NULL,7,0);
2516            fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
2517#endif
[765]2518            unlink (fbkpid);
2519            unhandleStatus (m);
[854]2520#ifdef RELY_ON_DB
2521#ifdef META_DB
2522            cleanupCallbackThreads();
2523            close_sql(m,1);
2524            //end_sql();
2525#endif
2526            close_sql(m,0);
2527#endif
[621]2528            freeMaps (&m);
2529            free (m);
2530            free (REQUEST);
2531            free (SERVICE_URL);
2532            freeMaps (&request_input_real_format);
2533            free (request_input_real_format);
2534            freeMaps (&request_output_real_format);
2535            free (request_output_real_format);
2536            freeMaps (&tmpmaps);
2537            free (tmpmaps);
2538            return -1;
2539          }
[854]2540          if(getMapFromMaps(m,"lenv","mapError")!=NULL){
2541                setMapInMaps(m,"lenv","message",_("Issue with geographic data"));
2542                invokeCallback(m,NULL,NULL,7,0);
2543                eres=-1;//SERVICE_FAILED;
2544          }else{
2545            setMapInMaps(m,"lenv","ds_nb","0");
2546            close_sql(m,0);
2547            loadServiceAndRun (&m, s1, request_inputs,
2548                               &request_input_real_format,
2549                               &request_output_real_format, &eres);
2550            setMapInMaps(m,"lenv","ds_nb","2");
2551          }
[541]2552        }
2553      else
2554        {
[652]2555          /**
2556           * error server don't accept the process need to output a valid
2557           * error response here !!!
2558           */
[541]2559          eres = -1;
2560          errorException (m, _("Unable to run the child process properly"),
2561                          "InternalError", NULL);
2562        }
[1]2563    }
2564
[839]2565#ifdef DEBUG
2566  fprintf (stderr, "RUN IN BACKGROUND MODE %s %d \n",__FILE__,__LINE__);
[541]2567  dumpMaps (request_output_real_format);
[839]2568  fprintf (stderr, "RUN IN BACKGROUND MODE %s %d \n",__FILE__,__LINE__);
2569#endif
[860]2570  fflush(stdout);
2571  rewind(stdout);
2572
[541]2573  if (eres != -1)
2574    outputResponse (s1, request_input_real_format,
2575                    request_output_real_format, request_inputs,
2576                    cpid, m, eres);
2577  fflush (stdout);
[788]2578
[105]2579  /**
2580   * Ensure that if error occurs when freeing memory, no signal will return
2581   * an ExceptionReport document as the result was already returned to the
2582   * client.
2583   */
2584#ifndef USE_GDB
[541]2585  signal (SIGSEGV, donothing);
2586  signal (SIGTERM, donothing);
2587  signal (SIGINT, donothing);
2588  signal (SIGILL, donothing);
2589  signal (SIGFPE, donothing);
2590  signal (SIGABRT, donothing);
[105]2591#endif
[788]2592
[541]2593  if (((int) getpid ()) != cpid || cgiSid != NULL)
2594    {
[854]2595      if (eres == SERVICE_SUCCEEDED)
2596        invokeCallback(m,NULL,request_output_real_format,5,1);
2597
[851]2598      fflush(stderr);
[854]2599      fflush(stdout);
[851]2600
[541]2601      fclose (stdout);
[765]2602
[541]2603      fclose (f0);
2604      fclose (f1);
[654]2605
[765]2606      if(dumpBackFinalFile(m,fbkp,fbkp1)<0)
2607        return -1;
[652]2608      unlink (fbkpid);
[654]2609      switch(eres){
2610      default:
2611      case SERVICE_FAILED:
2612        setMapInMaps(bmap,"status","status",wpsStatus[1]);
2613        setMapInMaps(m,"lenv","fstate",wpsStatus[1]);
2614        break;
2615      case SERVICE_SUCCEEDED:
2616        setMapInMaps(bmap,"status","status",wpsStatus[0]);
2617        setMapInMaps(m,"lenv","fstate",wpsStatus[0]);
2618        break;
[854]2619      }
[652]2620#ifndef RELY_ON_DB
[682]2621      dumpMapsToFile(bmap,fbkpres,1);
[652]2622      removeShmLock (m, 1);
2623#else
2624      recordResponse(m,fbkp1);
[854]2625      if (eres == SERVICE_SUCCEEDED)
2626        invokeCallback(m,NULL,request_output_real_format,6,0);
[652]2627#endif
[654]2628      freeMaps(&bmap);
2629      free(bmap);
[541]2630      unlink (fbkp1);
[652]2631      unhandleStatus (m);
[851]2632#ifdef RELY_ON_DB
[839]2633#ifdef META_DB
[851]2634      cleanupCallbackThreads();
2635      close_sql(m,1);
[839]2636#endif
[851]2637      close_sql(m,0);
[854]2638      end_sql();
[851]2639#endif
[652]2640      free(fbkpid);
[788]2641      free(fbkpres); 
[541]2642      free (fbkp1);
[788]2643      // free (tmps1); // tmps1 is stack memory and should not be freed
[682]2644      if(cgiSid!=NULL)
2645        free(cgiSid);
[851]2646      //InternetCloseHandle (&hInternet);
[839]2647      fprintf (stderr, "RUN IN BACKGROUND MODE %s %d \n",__FILE__,__LINE__);
2648      fflush(stderr);
2649      fclose (stderr);
[854]2650      unlink (flog);
2651      free (flog);
[541]2652    }
[839]2653  else{
2654    //InternetCloseHandle (&hInternet); 
2655#ifdef META_DB
2656    //close_sql(m,0);
2657#endif
2658  }
2659 
[541]2660  freeService (&s1);
2661  free (s1);
2662  freeMaps (&m);
2663  free (m);
2664
2665  freeMaps (&request_input_real_format);
2666  free (request_input_real_format);
2667
2668  freeMaps (&request_output_real_format);
2669  free (request_output_real_format);
2670
2671  free (REQUEST);
2672  free (SERVICE_URL);
[1]2673#ifdef DEBUG
[541]2674  fprintf (stderr, "Processed response \n");
2675  fflush (stdout);
2676  fflush (stderr);
[1]2677#endif
2678
[541]2679  if (((int) getpid ()) != cpid || cgiSid != NULL)
2680    {
2681      exit (0);
2682    }
[516]2683
[1]2684  return 0;
2685}
Note: See TracBrowser for help on using the repository browser.

Search

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