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

Last change on this file since 897 was 897, checked in by djay, 5 years ago

Add information abouut semaphore status for future deletion. Make sure to store on disk inputs passed by reference in case no specific memory=load has been set in the [main] section. Fix ticket #170. Add support for publishing raster applying a style definition found in the metadata (zcfg or database).

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