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

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

HPC support update. Add inputs for create options in Gdal_Dem.

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