source: trunk/zoo-project/zoo-kernel/zoo_service_loader.c @ 943

Last change on this file since 943 was 943, checked in by knut, 5 years ago

Fix problem with the parsing of system environment variables (Windows version). Need to verify that problem does not pertain to non-Windows versions also.

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