Index: trunk/zoo-project/zoo-kernel/Makefile.in
===================================================================
--- trunk/zoo-project/zoo-kernel/Makefile.in	(revision 651)
+++ trunk/zoo-project/zoo-kernel/Makefile.in	(revision 652)
@@ -93,10 +93,10 @@
 	g++ -g -O2 ${XML2CFLAGS} ${CFLAGS} ${SAGA_CFLAGS} ${OTBCFLAGS} ${PYTHONCFLAGS} ${JAVACFLAGS} ${JSCFLAGS} ${PERLCFLAGS} ${PHPCFLAGS} ${SAGA_ENABLED} ${OTB_ENABLED} ${PYTHON_ENABLED} ${JS_ENABLED} ${PHP_ENABLED} ${PERL_ENABLED} ${JAVA_ENABLED} -c zoo_service_loader.c  -fno-common -DPIC -o zoo_service_loader.o
 
-libzoo_service.${EXT}: version.h service_internal.o service.o 
-	gcc -shared  ${GDAL_CFLAGS} ${DEFAULT_OPTS} -fpic -o libzoo_service.${EXT} ${CFLAGS}  service_internal.o service.o -lfcgi ${GDAL_LIBS}
+libzoo_service.${EXT}: version.h service_internal.o service.o sqlapi.o
+	gcc -shared  ${GDAL_CFLAGS} ${DEFAULT_OPTS} -fpic -o libzoo_service.${EXT} ${CFLAGS}  service_internal.o service.o sqlapi.o -lfcgi ${GDAL_LIBS}
 
-zoo_loader.cgi: version.h libzoo_service.${EXT} zoo_loader.c zoo_service_loader.o  ulinet.o service.h lex.sr.o service_conf.tab.o service_conf.y ulinet.o main_conf_read.tab.o lex.cr.o request_parser.o sqlapi.o response_print.o server_internal.o caching.o ${MS_FILE} ${PYTHON_FILE} ${PHP_FILE} ${JAVA_FILE} ${JS_FILE} ${PERL_FILE} ${RUBY_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE}
+zoo_loader.cgi: version.h libzoo_service.${EXT} zoo_loader.c zoo_service_loader.o  ulinet.o service.h lex.sr.o service_conf.tab.o service_conf.y ulinet.o main_conf_read.tab.o lex.cr.o request_parser.o response_print.o server_internal.o caching.o ${MS_FILE} ${PYTHON_FILE} ${PHP_FILE} ${JAVA_FILE} ${JS_FILE} ${PERL_FILE} ${RUBY_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE}
 	g++ -g -O2 ${JSCFLAGS} ${PHPCFLAGS}  ${PERLCFLAGS} ${RUBYCFLAGS}  ${JAVACFLAGS} ${XML2CFLAGS} ${PYTHONCFLAGS} ${CFLAGS} -c zoo_loader.c  -fno-common -DPIC -o zoo_loader.o
-	g++  ${JSCFLAGS} ${SAGA_CFLAGS} ${OTBCFLAGS} ${GDAL_CFLAGS} ${XML2CFLAGS} ${PHPCFLAGS} ${PERLCFLAGS} ${JAVACFLAGS} ${PYTHONCFLAGS} ${CFLAGS} zoo_loader.o zoo_service_loader.o ${MS_FILE} ${PYTHON_FILE}  ${PERL_FILE} ${PHP_FILE}  ${JS_FILE} ${JAVA_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE} sqlapi.o response_print.o server_internal.o caching.o request_parser.o  ulinet.o lex.cr.o lex.sr.o service_conf.tab.o main_conf_read.tab.o -o zoo_loader.cgi -L. ${LDFLAGS}
+	g++  ${JSCFLAGS} ${SAGA_CFLAGS} ${OTBCFLAGS} ${GDAL_CFLAGS} ${XML2CFLAGS} ${PHPCFLAGS} ${PERLCFLAGS} ${JAVACFLAGS} ${PYTHONCFLAGS} ${CFLAGS} zoo_loader.o zoo_service_loader.o ${MS_FILE} ${PYTHON_FILE}  ${PERL_FILE} ${PHP_FILE}  ${JS_FILE} ${JAVA_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE} response_print.o server_internal.o caching.o request_parser.o ulinet.o lex.cr.o lex.sr.o service_conf.tab.o main_conf_read.tab.o -o zoo_loader.cgi -L. ${LDFLAGS}
 
 zcfg2yaml: zcfg2yaml.c service.h lex.sr.o service_conf.tab.o service_conf.y main_conf_read.tab.o lex.cr.o response_print.o server_internal.o service_internal.o ${MS_FILE} ${YAML_FILE}
Index: trunk/zoo-project/zoo-kernel/ZOOMakefile.opts.in
===================================================================
--- trunk/zoo-project/zoo-kernel/ZOOMakefile.opts.in	(revision 651)
+++ trunk/zoo-project/zoo-kernel/ZOOMakefile.opts.in	(revision 652)
@@ -90,5 +90,5 @@
 SAGA_FILE=@SAGA_FILE@
 
-CFLAGS=@DEB_DEF@ -fpic ${YAML_CFLAGS} ${MACOS_CFLAGS} ${MS_CFLAGS} -I../../thirds/cgic206 -I. -DLINUX_FREE_ISSUE #-DDEBUG #-DDEBUG_SERVICE_CONF
+CFLAGS=@RELY_ON_DB@ @DEB_DEF@ -fpic ${YAML_CFLAGS} ${MACOS_CFLAGS} ${MS_CFLAGS} -I../../thirds/cgic206 -I. -DLINUX_FREE_ISSUE #-DDEBUG #-DDEBUG_SERVICE_CONF
 LDFLAGS=-lzoo_service -lcurl -L../../thirds/cgic206 -lcgic ${GDAL_LIBS} ${XML2LDFLAGS} ${PYTHONLDFLAGS} ${PERLLDFLAGS}  ${PHPLDFLAGS} ${JAVALDFLAGS} ${JSLDFLAGS} -lfcgi -lcrypto -luuid ${MS_LDFLAGS} ${MACOS_LD_FLAGS} ${MACOS_LD_NET_FLAGS} ${YAML_LDFLAGS} ${OTBLDFLAGS} ${SAGA_LDFLAGS}
 
Index: trunk/zoo-project/zoo-kernel/configure.ac
===================================================================
--- trunk/zoo-project/zoo-kernel/configure.ac	(revision 651)
+++ trunk/zoo-project/zoo-kernel/configure.ac	(revision 652)
@@ -47,8 +47,13 @@
 
 AC_ARG_WITH([cgi-dir],
-    [AS_HELP_STRING([--with-cgi-dir=PATH], [specify an alternative cgi directory path ( default: /usr/lib/cgi-bin) ])],
+    [AS_HELP_STRING([--with-cgi-dir=PATH], [Specifies an alternative cgi directory path ( default: /usr/lib/cgi-bin) ])],
     [CGI_DIR="$withval"], [CGI_DIR="/usr/lib/cgi-bin"])
 AC_SUBST([CGI_DIR])
 
+AC_ARG_WITH([db-backend],
+    [AS_HELP_STRING([--with-db-backend], [Relies on a database for storing status messages and response files ])],
+    [RELY_ON_DB="-DRELY_ON_DB"], [RELY_ON_DB=""])
+AC_SUBST([RELY_ON_DB])
+
 
 # ===========================================================================
@@ -57,5 +62,5 @@
 
 AC_ARG_WITH([yaml],
-        [AS_HELP_STRING([--with-yaml=PATH], [specify an alternative location for the yaml library])],
+        [AS_HELP_STRING([--with-yaml=PATH], [Specifies an alternative location for the yaml library])],
         [YAMLPATH="$withval"], [YAMLPATH=""])
 
@@ -94,5 +99,5 @@
 
 AC_ARG_WITH([fastcgi],
-        [AS_HELP_STRING([--with-fastcgi=PATH], [specify an alternative location for the fastcgi library])],
+        [AS_HELP_STRING([--with-fastcgi=PATH], [Specifies an alternative location for the fastcgi library])],
         [FCGIPATH="$withval"], [FCGIPATH="/usr"])
 
@@ -118,5 +123,5 @@
 
 AC_ARG_WITH([xml2config], 
-	[AS_HELP_STRING([--with-xml2config=FILE], [specify an alternative xml2-config file])], 
+	[AS_HELP_STRING([--with-xml2config=FILE], [Specifies an alternative xml2-config file])], 
 	[XML2CONFIG="$withval"], [XML2CONFIG=""])
 
@@ -132,5 +137,5 @@
 	# XML2CONFIG was specified; display a message to the user
 	if test "x$XML2CONFIG" = "xyes"; then
-		AC_MSG_ERROR([you must specify a parameter to --with-xml2config, e.g. --with-xml2config=/path/to/xml2-config])
+		AC_MSG_ERROR([you must Specifies a parameter to --with-xml2config, e.g. --with-xml2config=/path/to/xml2-config])
 	else
 		if test -f $XML2CONFIG; then
@@ -167,5 +172,5 @@
 
 AC_ARG_WITH([xsltconfig], 
-	[AS_HELP_STRING([--with-xsltconfig=FILE], [specify an alternative xslt-config file])], 
+	[AS_HELP_STRING([--with-xsltconfig=FILE], [Specifies an alternative xslt-config file])], 
 	[XSLTCONFIG="$withval"], [XSLTCONFIG=""])
 
@@ -181,5 +186,5 @@
 	# XSLTCONFIG was specified; display a message to the user
 	if test "x$XSLTCONFIG" = "xyes"; then
-		AC_MSG_ERROR([you must specify a parameter to --with-xsltconfig, e.g. --with-xsltconfig=/path/to/xslt-config])
+		AC_MSG_ERROR([you must Specifies a parameter to --with-xsltconfig, e.g. --with-xsltconfig=/path/to/xslt-config])
 	else
 		if test -f $XSLTCONFIG; then
@@ -209,5 +214,5 @@
 
 AC_ARG_WITH([gdal-config], 
-	[AS_HELP_STRING([--with-gdal-config=FILE], [specify an alternative gdal-config file])], 
+	[AS_HELP_STRING([--with-gdal-config=FILE], [Specifies an alternative gdal-config file])], 
 	[GDAL_CONFIG="$withval"], [GDAL_CONFIG=""])
 if test -z $GDAL_CONFIG;
@@ -238,5 +243,5 @@
 
 AC_ARG_WITH([proj],
-        [AS_HELP_STRING([--with-proj=PATH], [specify an alternative location for PROJ4 setup])],
+        [AS_HELP_STRING([--with-proj=PATH], [Specifies an alternative location for PROJ4 setup])],
         [PROJPATH="$withval"], [PROJPATH=""])
 
@@ -259,5 +264,5 @@
 
 AC_ARG_WITH([geosconfig], 
-	[AS_HELP_STRING([--with-geosconfig=FILE], [specify an alternative geos-config file])], 
+	[AS_HELP_STRING([--with-geosconfig=FILE], [Specifies an alternative geos-config file])], 
 	[GEOSCONFIG="$withval"], [GEOSCONFIG=""])
 
@@ -273,5 +278,5 @@
 	# GEOSCONFIG was specified; display a message to the user
 	if test "x$GEOSCONFIG" = "xyes"; then
-		AC_MSG_WARN([you must specify a parameter to --with-geosconfig, e.g. --with-geosconfig=/path/to/geos-config])
+		AC_MSG_WARN([you must Specifies a parameter to --with-geosconfig, e.g. --with-geosconfig=/path/to/geos-config])
 	else
 		if test -f $GEOSCONFIG; then
@@ -301,5 +306,5 @@
 
 AC_ARG_WITH([cgal],
-        [AS_HELP_STRING([--with-cgal=PATH], [specify an alternative location for CGAL setup])],
+        [AS_HELP_STRING([--with-cgal=PATH], [Specifies an alternative location for CGAL setup])],
         [CGALPATH="$withval"], [CGALPATH="/usr"])
 
@@ -323,5 +328,5 @@
 
 AC_ARG_WITH([mapserver], 
-       [AS_HELP_STRING([--with-mapserver=PATH], [specify the path for MapServer compiled source tree])], 
+       [AS_HELP_STRING([--with-mapserver=PATH], [Specifies the path for MapServer compiled source tree])], 
        [MS_SRC_PATH="$withval"], [MS_SRC_PATH=""])
 
@@ -362,5 +367,5 @@
 
 AC_ARG_WITH([python], 
-	[AS_HELP_STRING([--with-python=PATH], [To enable python support or specify an alternative directory for python installation,  disabled by default])], 
+	[AS_HELP_STRING([--with-python=PATH], [To enable python support or Specifies an alternative directory for python installation,  disabled by default])], 
 	[PYTHON_PATH="$withval"; PYTHON_ENABLED="-DUSE_PYTHON"], [PYTHON_ENABLED=""])
 
@@ -417,5 +422,5 @@
 
 AC_ARG_WITH([js], 
-	[AS_HELP_STRING([--with-js=PATH], [specify --with-js=path-to-js to enable js support, specify --with-js on linux debian like, js support is disabled by default ])], 
+	[AS_HELP_STRING([--with-js=PATH], [Specifies --with-js=path-to-js to enable js support, specify --with-js on linux debian like, js support is disabled by default ])], 
 	[JSHOME="$withval";JS_ENABLED="-DUSE_JS"], [JS_ENABLED=""])
 
@@ -666,13 +671,13 @@
 
 AC_ARG_WITH([itk],
-        [AS_HELP_STRING([--with-itk=PATH], [specify an alternative location for the itk library])],
+        [AS_HELP_STRING([--with-itk=PATH], [Specifies an alternative location for the itk library])],
         [ITKPATH="$withval"], [ITKPATH=""])
 
 AC_ARG_WITH([itk-version],
-        [AS_HELP_STRING([--with-itk-version=VERSION], [specify an alternative version for the itk library])],
+        [AS_HELP_STRING([--with-itk-version=VERSION], [Specifies an alternative version for the itk library])],
         [ITKVERS="$withval"], [ITKVERS=""])
 
 AC_ARG_WITH([otb],
-        [AS_HELP_STRING([--with-otb=PATH], [specify an alternative location for the otb library])],
+        [AS_HELP_STRING([--with-otb=PATH], [Specifies an alternative location for the otb library])],
         [OTBPATH="$withval"], [OTBPATH=""])
 
@@ -720,9 +725,9 @@
 
 AC_ARG_WITH([wx-config],
-        [AS_HELP_STRING([--with-wx-config=PATH], [specify an alternative path for the wx-config tool])],
+        [AS_HELP_STRING([--with-wx-config=PATH], [Specifies an alternative path for the wx-config tool])],
         [WXCFG="$withval"], [WXCFG=""])
 
 AC_ARG_WITH([saga],
-        [AS_HELP_STRING([--with-saga=PATH], [specify an alternative location for the SAGA-GIS library])],
+        [AS_HELP_STRING([--with-saga=PATH], [Specifies an alternative location for the SAGA-GIS library])],
         [SAGAPATH="$withval"], [SAGAPATH=""])
 
Index: trunk/zoo-project/zoo-kernel/response_print.c
===================================================================
--- trunk/zoo-project/zoo-kernel/response_print.c	(revision 651)
+++ trunk/zoo-project/zoo-kernel/response_print.c	(revision 652)
@@ -1515,12 +1515,11 @@
       int lpid;
       map* tmpm2=getMapFromMaps(m,"lenv","usid");
-      lpid=atoi(tmpm2->value);
-      tmpm2=getMap(tmp_maps->content,"tmpUrl");
-      if(tmpm1!=NULL && tmpm2!=NULL){
-	if( strncasecmp( tmpm2->value, "http://", 7) == 0 ||
-	    strncasecmp( tmpm2->value, "https://", 8 ) == 0 ){
-	  sprintf(url,"%s/%s_%i.xml",tmpm2->value,service,lpid);
+      map* tmpm3=getMap(tmp_maps->content,"tmpUrl");
+      if(tmpm1!=NULL && tmpm3!=NULL){
+	if( strncasecmp( tmpm3->value, "http://", 7) == 0 ||
+	    strncasecmp( tmpm3->value, "https://", 8 ) == 0 ){
+	  sprintf(url,"%s/%s_%s.xml",tmpm3->value,service,tmpm2->value);
 	}else
-	  sprintf(url,"%s/%s/%s_%i.xml",tmpm1->value,tmpm2->value,service,lpid);
+	  sprintf(url,"%s/%s_%s.xml",tmpm1->value,service,tmpm2->value);
       }
     }
@@ -1529,8 +1528,7 @@
     }
     int lpid;
-    tmpm1=getMapFromMaps(m,"lenv","usid");
-    lpid=atoi(tmpm1->value);
+    map* tmpm2=getMapFromMaps(m,"lenv","usid");
     tmpm1=getMapFromMaps(m,"main","TmpPath");
-    sprintf(stored_path,"%s/%s_%i.xml",tmpm1->value,service,lpid);
+    sprintf(stored_path,"%s/%s_%s.xml",tmpm1->value,service,tmpm2->value);
   }
 
@@ -1683,6 +1681,7 @@
   }
 
-  if(hasStoredExecuteResponse==true && status!=SERVICE_STARTED){
-    semid lid=getShmLockId(m,1);
+  if(hasStoredExecuteResponse==true && status!=SERVICE_STARTED && status!=SERVICE_ACCEPTED){
+#ifndef RELY_ON_DB
+    semid lid=acquireLock(m);//,1);
     if(lid<0){
       /* If the lock failed */
@@ -1694,8 +1693,5 @@
     }
     else{
-#ifdef DEBUG
-      fprintf(stderr,"LOCK %s %d !\n",__FILE__,__LINE__);
 #endif
-      lockShm(lid);
       /* We need to write the ExecuteResponse Document somewhere */
       FILE* output=fopen(stored_path,"w");
@@ -1709,5 +1705,7 @@
 	xmlCleanupParser();
 	zooXmlCleanupNs();
+#ifndef RELY_ON_DB
 	unlockShm(lid);
+#endif
 	return;
       }
@@ -1718,13 +1716,16 @@
       xmlFree(xmlbuff);
       fclose(output);
+#ifndef RELY_ON_DB
 #ifdef DEBUG
       fprintf(stderr,"UNLOCK %s %d !\n",__FILE__,__LINE__);
 #endif
       unlockShm(lid);
-      map* test1=getMap(request,"status");
-      if(test1==NULL || strcasecmp(test1->value,"true")!=0){
-	removeShmLock(m,1);
-      }
-    }
+      map* v=getMapFromMaps(m,"lenv","sid");
+      // Remove the lock when running as a normal task
+      if(getpid()==atoi(v->value)){
+	removeShmLock (m, 1);
+      }
+    }
+#endif
   }
   printDocument(m,doc,pid);
@@ -2316,5 +2317,6 @@
 #endif
     maps* tmpI=request_outputs;
-
+    map* usid=getMapFromMaps(m,"lenv","usid");
+    int itn=0;
     while(tmpI!=NULL){
 #ifdef USE_MS
@@ -2354,5 +2356,5 @@
 	  }
 
-	  if(gfile==NULL){
+	  if(gfile==NULL) {
 	    map *ext=getMap(tmpI->content,"extension");
 	    char *file_path;
@@ -2369,11 +2371,11 @@
 	    }
 		
-	    file_name=(char*)malloc((strlen(s->name)+strlen(file_ext)+strlen(tmpI->name)+1024)*sizeof(char));
-	    int cpid0=cpid+time(NULL);	    
-	    sprintf(file_name,"%s_%s_%i.%s",s->name,tmpI->name,cpid0,file_ext);
+	    file_name=(char*)malloc((strlen(s->name)+strlen(usid->value)+strlen(file_ext)+strlen(tmpI->name)+45)*sizeof(char));
+	    sprintf(file_name,"%s_%s_%s_%d.%s",s->name,tmpI->name,usid->value,itn,file_ext);
+	    itn++;
 	    file_path=(char*)malloc((strlen(tmp1->value)+strlen(file_name)+2)*sizeof(char));
 	    sprintf(file_path,"%s/%s",tmp1->value,file_name);
     
-		FILE *ofile=fopen(file_path,"wb");
+	    FILE *ofile=fopen(file_path,"wb");
 	    if(ofile==NULL){
 	      char tmpMsg[1024];
Index: trunk/zoo-project/zoo-kernel/server_internal.c
===================================================================
--- trunk/zoo-project/zoo-kernel/server_internal.c	(revision 651)
+++ trunk/zoo-project/zoo-kernel/server_internal.c	(revision 652)
@@ -29,4 +29,5 @@
 #include <dlfcn.h>
 #endif
+#include <uuid/uuid.h>
 
 int getVersionId(const char* version){
@@ -37,4 +38,21 @@
   }
   return 0;
+}
+
+/**
+ * Generate a UUID.
+ * ref: https://www.ietf.org/rfc/rfc4122.txt / 4.2 
+ *
+ * @return a new char* containing the UUID, make sure to free the returned 
+ *  ressource once used.
+ */
+char *get_uuid(){
+  char *res=(char*)malloc(37*sizeof(char));
+  uuid_t uuid;
+  uuid_generate_time(uuid);
+  char * rest;
+  uuid_unparse(uuid,rest);
+  sprintf(res,"%s", rest);
+  return res;
 }
 
@@ -325,5 +343,5 @@
 void dumpMapsValuesToFiles(maps** main_conf,maps** in){
   map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
-  map* tmpSid=getMapFromMaps(*main_conf,"lenv","sid");
+  map* tmpSid=getMapFromMaps(*main_conf,"lenv","usid");
   maps* inputs=*in;
   int length=0;
Index: trunk/zoo-project/zoo-kernel/server_internal.h
===================================================================
--- trunk/zoo-project/zoo-kernel/server_internal.h	(revision 651)
+++ trunk/zoo-project/zoo-kernel/server_internal.h	(revision 652)
@@ -39,5 +39,6 @@
 #include <libxml/parser.h>
 #include <libxml/xpath.h>
-  
+
+  char *get_uuid();  
   char *base64(const char*,int);
   char *base64d(const char*,int,int*);
Index: trunk/zoo-project/zoo-kernel/service.c
===================================================================
--- trunk/zoo-project/zoo-kernel/service.c	(revision 651)
+++ trunk/zoo-project/zoo-kernel/service.c	(revision 652)
@@ -93,11 +93,12 @@
   FILE* file=fopen(file_path,"w");
   maps* tmp=m;
-  if(tmp!=NULL){
+  while(tmp!=NULL){
     fprintf(file,"[%s]\n",tmp->name);
-      dumpMapToFile(tmp->content,file);
-      fflush(file);
-    }
-    fclose(file);
-  }
+    dumpMapToFile(tmp->content,file);
+    fflush(file);
+    tmp=tmp->next;
+  }
+  fclose(file);
+}
   
 /**
Index: trunk/zoo-project/zoo-kernel/service_internal.c
===================================================================
--- trunk/zoo-project/zoo-kernel/service_internal.c	(revision 651)
+++ trunk/zoo-project/zoo-kernel/service_internal.c	(revision 652)
@@ -39,17 +39,45 @@
 
 #define ERROR_MSG_MAX_LENGTH 1024
-
-/**
- * Get the ongoing status of a running service 
+#ifndef RELY_ON_DB
+#include <dirent.h>
+
+/**
+ * Read the sid file attached of a service if any
  *
  * @param conf the maps containing the setting of the main.cfg file
  * @param pid the service identifier (usid key from the [lenv] section)
- * @return the reported status char* (MESSAGE|POURCENTAGE)
- */
-char* _getStatus(maps* conf,int pid){
-  char lid[1024];
-  sprintf(lid,"%d",pid);
-  setMapInMaps(conf,"lenv","lid",lid);
-  semid lockid=getShmLockId(conf,1);
+ * @return the reported status char* (temporary/final result)
+ */
+char* getStatusId(maps* conf,char* pid){
+  map* r_inputs = getMapFromMaps (conf, "main", "tmpPath");
+  char* fbkpid =
+    (char *)
+    malloc ((strlen (r_inputs->value) + strlen (pid) + 7) * sizeof (char));
+  sprintf (fbkpid, "%s/%s.sid", r_inputs->value, pid);
+  FILE* f0 = fopen (fbkpid, "r");
+  if(f0!=NULL){
+    fseek (f0, 0, SEEK_END);
+    long flen = ftell (f0);
+    fseek (f0, 0, SEEK_SET);
+    char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char));
+    fread(tmps1,flen,1,f0);
+    tmps1[flen]=0;
+    fclose(f0);
+    return tmps1;
+  }else
+    return NULL;
+}
+
+/**
+ * Acquire the global lock
+ * 
+ * @param conf the maps containing the setting of the main.cfg file
+ * @return a semid 
+ */
+semid acquireLock(maps* conf){
+  semid lockid;
+  int itn=0;
+ toRetry1:
+  lockid=getShmLockId(conf,1);
   if(
 #ifdef WIN32
@@ -59,22 +87,200 @@
 #endif
      ){
-	char* tmp = (char*) malloc(3*sizeof(char));
-    sprintf(tmp,"%d",ZOO_LOCK_CREATE_FAILED);
-    return tmp;
+#ifdef WIN32
+    return NULL
+#else
+    return -1;
+#endif
   }
   if(lockShm(lockid)<0){
-    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
-    fflush(stderr);    
-	char* tmp = (char*) malloc(3*sizeof(char));
-    sprintf(tmp,"%d",ZOO_LOCK_ACQUIRE_FAILED);
-    return tmp;
-  }
-  char *tmp=getStatus(pid);
-  unlockShm(lockid);
-  if(tmp==NULL || strncmp(tmp,"-1",2)==0){
+    if(itn<ZOO_LOCK_MAX_RETRY){
+      itn++;
+      goto toRetry1;
+    }else
+#ifdef WIN32
+    return NULL
+#else
+    return -1;
+#endif
+  }else
+    return lockid;
+}
+
+/**
+ * Read the cache file of a running service 
+ *
+ * @param conf the maps containing the setting of the main.cfg file
+ * @param pid the service identifier (usid key from the [lenv] section)
+ * @return the reported status char* (temporary/final result)
+ */
+char* _getStatusFile(maps* conf,char* pid){
+  map* tmpTmap = getMapFromMaps (conf, "main", "tmpPath");
+
+  struct dirent *dp;
+  DIR *dirp = opendir(tmpTmap->value);
+  char fileName[1024];
+  int hasFile=-1;
+  if(dirp!=NULL){
+    char tmp[128];
+    sprintf(tmp,"_%s.xml",pid);
+    while ((dp = readdir(dirp)) != NULL){
+#ifdef DEBUG
+      fprintf(stderr,"File : %s searched : %s\n",dp->d_name,tmp);
+#endif
+      if(strstr(dp->d_name,"final_")==0 && strstr(dp->d_name,tmp)!=0){
+	sprintf(fileName,"%s/%s",tmpTmap->value,dp->d_name);
+	hasFile=1;
+	break;
+      }
+    }
+  }
+  if(hasFile>0){
+    semid lockid;
+    char* stat=getStatusId(conf,pid);
+    if(stat!=NULL){
+      setMapInMaps(conf,"lenv","lid",stat);
+      lockid=acquireLock(conf);
+      if(lockid<0)
+	return NULL;
+    }
+
+    FILE* f0 = fopen (fileName, "r");
+    if(f0!=NULL){
+      fseek (f0, 0, SEEK_END);
+      long flen = ftell (f0);
+      fseek (f0, 0, SEEK_SET);
+      char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char));
+      fread(tmps1,flen,1,f0);
+      tmps1[flen]=0;
+      fclose(f0);
+      if(stat!=NULL){
+	unlockShm(lockid);
+	free(stat);
+      }
+
+      return tmps1;
+    }
+    else{
+      if(stat!=NULL){
+	unlockShm(lockid);
+	free(stat);
+      }
+      return NULL;
+    }
+  }
+  else
+    return NULL;
+}
+
+/**
+ * Get the ongoing status of a running service 
+ *
+ * @param conf the maps containing the setting of the main.cfg file
+ * @param pid the service identifier (usid key from the [lenv] section)
+ * @return the reported status char* (MESSAGE|POURCENTAGE)
+ */
+char* _getStatus(maps* conf,char* lid){
+  map* r_inputs = getMapFromMaps (conf, "main", "tmpPath");
+  char* fbkpid =
+    (char *)
+    malloc ((strlen (r_inputs->value) + strlen (lid) + 9) * sizeof (char));
+  sprintf (fbkpid, "%s/%s.status", r_inputs->value, lid);
+  FILE* f0 = fopen (fbkpid, "r");
+  if(f0!=NULL){
+    char* stat=getStatusId(conf,lid);
+    if(stat!=NULL){
+      setMapInMaps(conf,"lenv","lid",stat);
+      semid lockid=acquireLock(conf);
+      if(lockid<0)
+	return NULL;
+    }
+    fseek (f0, 0, SEEK_END);
+    long flen = ftell (f0);
+    if(flen>0){
+      fseek (f0, 0, SEEK_SET);
+      char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char));
+      fread(tmps1,flen,1,f0);
+      tmps1[flen]=0;
+      fclose(f0);
+      free(fbkpid);
+      if(stat!=NULL){
+	removeShmLock(conf,1);
+	free(stat);
+      }
+      return tmps1;
+    }
+    fclose(f0);
+    free(fbkpid);
+    if(stat!=NULL){
+      removeShmLock(conf,1);
+      free(stat);
+    }
+    return NULL;
+  }else{
+    free(fbkpid);
+    char* stat=getStatusId(conf,lid);
+    setMapInMaps(conf,"lenv","lid",stat);
     removeShmLock(conf,1);
-  }
-  return tmp;
-}
+    return NULL;
+  }
+}
+
+/**
+ * Stop handling status repport.
+ *
+ * @param conf the map containing the setting of the main.cfg file
+ */
+void unhandleStatus(maps *conf){
+  map* r_inputs = getMapFromMaps (conf, "main", "tmpPath");
+  map* usid = getMapFromMaps (conf, "lenv", "usid");
+  char* fbkpid =
+    (char *) malloc ((strlen (r_inputs->value) + strlen (usid->value) + 9) 
+		     * sizeof (char));
+  sprintf (fbkpid, "%s/%s.status", r_inputs->value, usid->value);
+  unlink(fbkpid);
+  free(fbkpid);
+}
+
+/**
+ * Update the current status of the running service.
+ *
+ * @see acquireLock, lockShm
+ * @param conf the map containing the setting of the main.cfg file
+ * @return 0 on success, -2 if shmget failed, -1 if shmat failed
+ */
+int _updateStatus(maps *conf){
+  map* r_inputs = getMapFromMaps (conf, "main", "tmpPath");
+  map* sid = getMapFromMaps (conf, "lenv", "usid");
+  char* fbkpid =
+    (char *)
+    malloc ((strlen (r_inputs->value) + strlen (sid->value) + 9) * sizeof (char));
+  sprintf (fbkpid, "%s/%s.status", r_inputs->value, sid->value);
+  map* status=getMapFromMaps(conf,"lenv","status");
+  map* msg=getMapFromMaps(conf,"lenv","message");
+  if(status!=NULL && msg!=NULL &&
+     status->value!=NULL && msg->value!=NULL && 
+     strlen(status->value)>0 && strlen(msg->value)>1){
+    semid lockid;
+    char* stat=getStatusId(conf,sid->value);
+    if(stat!=NULL){
+      lockid=acquireLock(conf);
+      if(lockid<0)
+	return ZOO_LOCK_ACQUIRE_FAILED;
+    }
+    FILE* fstatus=fopen(fbkpid,"w");
+    if(fstatus!=NULL){
+      fprintf(fstatus,"%s|%s\n",status->value,msg->value);
+      fflush(fstatus);
+      fclose(fstatus);
+    }
+    if(stat!=NULL){
+      unlockShm(lockid);
+      free(stat);
+    }
+  }
+  return 0;
+}
+
+#endif
 
 #ifdef WIN32
@@ -96,5 +302,5 @@
   map *tmpMap=getMapFromMaps(conf,"lenv","lid");
   if(tmpMap==NULL)
-	tmpMap=getMapFromMaps(conf,"lenv","usid");
+	tmpMap=getMapFromMaps(conf,"lenv","osid");
 
   if(tmpMap!=NULL){
@@ -130,5 +336,7 @@
   semid sem_id=getShmLockId(conf,1);
   if (CloseHandle(sem_id) == 0) {
+#ifdef DEBUG
     fprintf(stderr,"Unable to remove semaphore: %s\n", getLastErrorMessage());
+#endif
     return -1;
   }
@@ -165,68 +373,4 @@
 static HANDLE hMapObjectG = NULL;  // handle to file mapping
 
-int _updateStatus(maps *conf){
-  LPWSTR lpszTmp;
-  BOOL fInit;
-  char *final_string=NULL;
-  char *s=NULL;
-  map *tmpMap1;
-  map *tmpMap=getMapFromMaps(conf,"lenv","usid");
-  semid lockid=getShmLockId(conf,1);
-  if(lockid==NULL){
-#ifdef DEBUG
-    fprintf(stderr,"Unable to create semaphore on line %d!! \n",__LINE__);
-#endif
-    return ZOO_LOCK_CREATE_FAILED;
-  }
-  if(lockShm(lockid)<0){
-#ifdef DEBUG
-    fprintf(stderr,"Unable to create semaphore on line %d!! \n",__LINE__);
-#endif
-    return ZOO_LOCK_ACQUIRE_FAILED;
-  }
-  
-  if(hMapObjectG==NULL)
-    hMapObjectG = CreateFileMapping( 
-				    INVALID_HANDLE_VALUE,   // use paging file
-				    NULL,                   // default security attributes
-				    PAGE_READWRITE,         // read/write access
-				    0,                      // size: high 32-bits
-				    SHMEMSIZE,              // size: low 32-bits
-				    TEXT(tmpMap->value));   // name of map object
-  if (hMapObjectG == NULL){
-#ifdef DEBUG
-    fprintf(stderr,"Unable to create shared memory segment: %s\n", getLastErrorMessage());
-#endif
-    return -2;
-  }
-  fInit = (GetLastError() != ERROR_ALREADY_EXISTS); 
-  if(lpvMemG==NULL)
-    lpvMemG = MapViewOfFile( 
-			    hMapObjectG,     // object to map view of
-			    FILE_MAP_WRITE, // read/write access
-			    0,              // high offset:  map from
-			    0,              // low offset:   beginning
-			    0);             // default: map entire file
-  if (lpvMemG == NULL){
-#ifdef DEBUG
-    fprintf(stderr,"Unable to create or access the shared memory segment %s !! \n",tmpMap->value);
-#endif
-    return -1;
-  } 
-  memset(lpvMemG, '\0', SHMEMSIZE);
-  tmpMap=getMapFromMaps(conf,"lenv","status");
-  tmpMap1=NULL;
-  tmpMap1=getMapFromMaps(conf,"lenv","message");
-  lpszTmp = (LPWSTR) lpvMemG;
-  final_string=(char*)malloc((strlen(tmpMap1->value)+strlen(tmpMap->value)+2)*sizeof(char));
-  sprintf(final_string,"%s|%s",tmpMap->value,tmpMap1->value);
-  for(s=final_string;*s!='\0';*s++){
-    *lpszTmp++ = *s;
-  }
-  *lpszTmp++ = '\0';
-  free(final_string);
-  unlockShm(lockid);
-  return 0;
-}
 
 char* getStatus(int pid){
@@ -287,10 +431,4 @@
 }
 
-void unhandleStatus(maps *conf){
-  BOOL fIgnore;
-  fIgnore = UnmapViewOfFile(lpvMemG); 
-  fIgnore = CloseHandle(hMapObjectG);
-}
-
 #else
 /**
@@ -312,6 +450,6 @@
 
 /**
- * Set in the pre-allocated key the zoo_sem_[SID] string 
- * where [SID] is the lid (if any) or usid value from the [lenv] section.
+ * Set in the pre-allocated key the zoo_sem_[OSID] string 
+ * where [OSID] is the lid (if any) or osid value from the [lenv] section.
  *
  * @param conf the map containing the setting of the main.cfg file
@@ -322,5 +460,5 @@
   map *tmpMap=getMapFromMaps(conf,"lenv","lid");
   if(tmpMap==NULL)
-    tmpMap=getMapFromMaps(conf,"lenv","usid");
+    tmpMap=getMapFromMaps(conf,"lenv","osid");
   int key=-1;
   if(tmpMap!=NULL)
@@ -380,5 +518,5 @@
 	      fprintf(stderr,"Retry to access the semaphore later ...\n");
 #endif
-	      sleep(1);
+	      zSleep(1);
             }
         }
@@ -411,7 +549,12 @@
   int sem_id=getShmLockId(conf,nsems);
   if (semctl(sem_id, 0, IPC_RMID, arg) == -1) {
-    perror("semctl");
+#ifdef DEBUG
+    perror("semctl remove");
+#endif
     return -1;
   }
+#ifdef DEBUG
+  fprintf(stderr,"Semaphore removed!\n");
+#endif
   return 0;
 }
@@ -429,5 +572,7 @@
   sb.sem_flg = SEM_UNDO;
   if (semop(id, &sb, 1) == -1){
-    perror("semop");
+#ifdef DEBUG
+    perror("semop lock");
+#endif
     return -1;
   }
@@ -447,5 +592,7 @@
   sb.sem_flg = SEM_UNDO;
   if (semop(id, &sb, 1) == -1) {
-    perror("semop");
+#ifdef DEBUG
+    perror("semop unlock");
+#endif
     return -1;
   }
@@ -454,89 +601,5 @@
 
 /**
- * Stop handling status repport.
- *
- * @param conf the map containing the setting of the main.cfg file
- */
-void unhandleStatus(maps *conf){
-  int shmid;
-  key_t key;
-  void *shm;
-  struct shmid_ds shmids;
-  map *tmpMap=getMapFromMaps(conf,"lenv","usid");
-  if(tmpMap!=NULL){
-    key=atoi(tmpMap->value);
-    if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
-#ifdef DEBUG
-      fprintf(stderr,"shmget failed to update value\n");
-#endif
-    }else{
-      if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
-#ifdef DEBUG
-	fprintf(stderr,"shmat failed to update value\n");
-#endif
-      }else{
-	shmdt(shm);
-	shmctl(shmid,IPC_RMID,&shmids);
-      }
-    }
-  }
-}
-
-/**
- * Update the current of the running service.
- *
- * @see getKeyValue, getShmLockId, lockShm
- * @param conf the map containing the setting of the main.cfg file
- * @return 0 on success, -2 if shmget failed, -1 if shmat failed
- */
-int _updateStatus(maps *conf){
-  int shmid;
-  char *shm,*s,*s1;
-  map *tmpMap=NULL;
-  key_t key=getKeyValue(conf);
-  if(key!=-1){
-    semid lockid=getShmLockId(conf,1);
-    if(lockid<0)
-      return ZOO_LOCK_CREATE_FAILED;
-    if(lockShm(lockid)<0){
-      return ZOO_LOCK_ACQUIRE_FAILED;
-    }
-    if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
-#ifdef DEBUG
-      fprintf(stderr,"shmget failed to create new Shared memory segment\n");
-#endif
-      unlockShm(lockid);
-      return -2;
-    }else{
-      if ((shm = (char*) shmat(shmid, NULL, 0)) == (char *) -1) {
-#ifdef DEBUG
-	fprintf(stderr,"shmat failed to update value\n");
-#endif
-	unlockShm(lockid);
-	return -1;
-      }
-      else{
-	tmpMap=getMapFromMaps(conf,"lenv","status");
-	s1=shm;
-	for(s=tmpMap->value;*s!=NULL && *s!=0;s++){
-	  *s1++=*s;
-	}
-	*s1++='|';
-	tmpMap=getMapFromMaps(conf,"lenv","message");
-	if(tmpMap!=NULL)
-	  for(s=tmpMap->value;*s!=NULL && *s!=0;s++){
-	    *s1++=*s;
-	}
-	*s1=NULL;
-	shmdt((void *)shm);
-	unlockShm(lockid);
-      }
-    }
-  }
-  return 0;
-}
-
-/**
- * Update the current of the running service.
+ * Get the current status of the running service.
  *
  * @see getKeyValue, getShmLockId, lockShm
Index: trunk/zoo-project/zoo-kernel/service_internal.h
===================================================================
--- trunk/zoo-project/zoo-kernel/service_internal.h	(revision 651)
+++ trunk/zoo-project/zoo-kernel/service_internal.h	(revision 652)
@@ -60,4 +60,8 @@
  */
 #define ZOO_LOCK_RELEASE_FAILED -6
+/**
+ * Number of time the ZOO-Kernel will try to acquire lock
+ */
+#define ZOO_LOCK_MAX_RETRY 10
 
 #include <sys/stat.h>
@@ -100,6 +104,8 @@
   void unhandleStatus(maps*);
   int _updateStatus(maps*);
-  char* _getStatus(maps*,int);
+  char* _getStatus(maps*,char*);
+  char* _getStatusFile(maps*,char*);
   char* getStatus(int);
+
   int updateStatus( maps*,const int,const char*);
   int removeShmLock(maps*, int);
@@ -112,11 +118,9 @@
 #define semid HANDLE
 #endif
+  semid acquireLock(maps*);
   semid getShmLockId(maps*,int);
   int lockShm(semid);
   int unlockShm(semid);
 
-#ifdef USE_JS
-#endif
-  
 #ifdef __cplusplus
 }
Index: trunk/zoo-project/zoo-kernel/service_internal_saga.c
===================================================================
--- trunk/zoo-project/zoo-kernel/service_internal_saga.c	(revision 651)
+++ trunk/zoo-project/zoo-kernel/service_internal_saga.c	(revision 652)
@@ -102,5 +102,5 @@
 
 maps* SagaWatcher::m_conf;
-char* SagaWatcher::message=zStrdup("");
+char* SagaWatcher::message=zStrdup("No message left");
 int SagaWatcher::status=1;
 
@@ -121,11 +121,21 @@
     {
     default:
-      res = 0;
+      return 0;
       break;
 
-    case CALLBACK_PROCESS_GET_OKAY:
+    case CALLBACK_DLG_ERROR:
+      return 1;
       break;
 
+    case CALLBACK_DLG_PARAMETERS:
     case CALLBACK_PROCESS_SET_OKAY:
+    case CALLBACK_DATAOBJECT_COLORS_GET:
+    case CALLBACK_DATAOBJECT_COLORS_SET:
+    case CALLBACK_DATAOBJECT_PARAMS_GET:
+    case CALLBACK_DATAOBJECT_PARAMS_SET:
+    case CALLBACK_DATAOBJECT_UPDATE:
+    case CALLBACK_DATAOBJECT_SHOW:
+    case CALLBACK_DLG_CONTINUE:
+    case CALLBACK_PROCESS_SET_READY:
       break;
 
@@ -139,5 +149,5 @@
       break;
 
-    case CALLBACK_PROCESS_SET_READY:
+    case CALLBACK_PROCESS_GET_OKAY:
       status=1;
       break;
@@ -161,10 +171,4 @@
     case CALLBACK_DLG_MESSAGE:
       SetMessage((param2.String + ": " + param1.String).b_str());
-      break;
-
-    case CALLBACK_DLG_CONTINUE:
-      break;
-
-    case CALLBACK_DLG_ERROR:
       break;
 
@@ -174,25 +178,4 @@
       else
 	res = 0;
-      break;
-
-    case CALLBACK_DATAOBJECT_UPDATE:
-      break;
-
-    case CALLBACK_DATAOBJECT_SHOW:
-      break;
-
-    case CALLBACK_DATAOBJECT_COLORS_GET:
-      break;
-
-    case CALLBACK_DATAOBJECT_COLORS_SET:
-      break;
-
-    case CALLBACK_DATAOBJECT_PARAMS_GET:
-      break;
-
-    case CALLBACK_DATAOBJECT_PARAMS_SET:
-      break;
-
-    case CALLBACK_DLG_PARAMETERS:
       break;
 
Index: trunk/zoo-project/zoo-kernel/sqlapi.c
===================================================================
--- trunk/zoo-project/zoo-kernel/sqlapi.c	(revision 651)
+++ trunk/zoo-project/zoo-kernel/sqlapi.c	(revision 652)
@@ -1,6 +1,6 @@
 /*
  * Author : David Saggiorato
- *
- *  Copyright 2008-2009 GeoLabs SARL. All rights reserved.
+ *          Gérald Fenoy
+ *  Copyright 2015 GeoLabs SARL. All rights reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -23,23 +23,312 @@
  */
 
+#ifdef RELY_ON_DB
+#include "ogr_api.h"
+#include "ogrsf_frmts.h"
+#include "ogr_p.h"
+
 #include "sqlapi.h"
 #include <fcgi_stdio.h>
-#include <uuid/uuid.h>
 #include <stdlib.h>
 
 /**
- * Generate a UUID.
- * ref: https://www.ietf.org/rfc/rfc4122.txt / 4.2 
- *
- * @return a new char* containing the UUID, make sure to free the returned ressource
- * once used.
- */
-char *get_uuid(){
-  char *res=(char*)malloc(37*sizeof(char));
-  uuid_t uuid;
-  uuid_generate_time(uuid);
-  char * rest;
-  uuid_unparse(uuid,rest);
-  sprintf(res,"%s", rest);
+ * Global GDALDataset pointer
+ */
+OGRDataSource *zoo_DS = NULL;
+
+/**
+ * Global OGRLayer pointer pointing to the lastest result set
+ */
+OGRLayer *zoo_ResultSet = NULL;
+
+/**
+ * Create a GDAL / OGR string for connecting to the db backend
+ * (do not support active_schema)
+ * 
+ * @param conf the maps containing the setting of the main.cfg file
+ */
+char* createInitString(maps* conf){
+  char* res=NULL;
+  char keywords[5][9]={
+    "dbname",
+    "host",
+    "port",
+    "user",
+    "password"
+  };
+  int i=0;
+  maps* cconf=getMaps(conf,"database");
+  int len=0;
+  for(i=0;i<5;i++){
+    map* tmp=getMap(cconf->content,keywords[i]);
+    if(tmp!=NULL){
+      if(res==NULL){
+	res=(char*)malloc((strlen(keywords[i])+strlen(tmp->value)+4)*sizeof(char));
+	sprintf(res,"%s='%s'",keywords[i],tmp->value);
+	len+=strlen(res);
+      }else{
+	char* res1=(char*)malloc((strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char));
+	sprintf(res1," %s='%s'",keywords[i],tmp->value);
+	res=(char*)realloc(res,(len+strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char));
+	memcpy(res+len,res1,(strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char));
+	len+=strlen(res1);
+	res[len]=0;
+	free(res1);
+      }
+    }
+  }
+  map* tmp=getMap(cconf->content,"type");
+  if(tmp!=NULL){
+    char* fres=(char*)malloc((strlen(res)+strlen(tmp->value)+2)*sizeof(char));
+    sprintf(fres,"%s:%s",tmp->value,res);
+    free(res);
+    return fres;
+  }
   return res;
 }
+
+/**
+ * Connect to the db backend.
+ * 
+ * @param conf the maps containing the setting of the main.cfg file
+ * @see createInitString
+ */
+void init_sql(maps* conf){
+  char* sqlInitString=createInitString(conf);
+  OGRSFDriver *poDriver = NULL;
+  OGRRegisterAll();
+  zoo_DS = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver);
+  if( zoo_DS == NULL ){
+#ifdef DEBUG
+    fprintf(stderr,"sqlInitString: %s FAILED !\n",sqlInitString);
+    fflush(stderr);
+#endif
+    free(sqlInitString);
+    setMapInMaps(conf,"lenv","message","Failed to connect to the database backend");
+    return;
+  }
+#ifdef DEBUG
+  fprintf(stderr,"sqlInitString: %s SUCEED !\n",sqlInitString);
+  fflush(stderr);
+#endif
+  free(sqlInitString);
+}
+
+/**
+ * Close any connection to the db backend.
+ * 
+ * @param conf the maps containing the setting of the main.cfg file
+ */
+void close_sql(maps* conf){
+  if( zoo_ResultSet != NULL )
+    zoo_DS->ReleaseResultSet( zoo_ResultSet );
+  if(zoo_DS!=NULL){
+    OGRDataSource::DestroyDataSource( zoo_DS );
+    zoo_DS=NULL;
+  }
+}
+
+/**
+ * Call OGRCleanupAll.
+ * 
+ */
+void end_sql(){
+  OGRCleanupAll();
+}
+
+/**
+ * Execute a SQL query to the SQL Database Backend.
+ * 
+ * @param conf the maps containing the setting of the main.cfg file
+ * @param sqlQuery the SQL query to run
+ * @return -1 in case of failure and 1 if the query succeed.
+ */
+int execSql(maps* conf,const char* sqlQuery){
+  zoo_ResultSet = zoo_DS->ExecuteSQL( sqlQuery, NULL, NULL);
+  if( zoo_ResultSet != NULL ){
+    return 1;
+  }
+  return -1;
+}
+
+/**
+ * Clean any memory allocated by executing a request
+ * 
+ * @param conf the maps containing the setting of the main.cfg file
+ * @param sqlQuery the SQL query to run
+ * @return -1 in case of failure and 1 if the query succeed.
+ */
+void cleanUpResultSet(const maps* conf){
+  if( zoo_ResultSet != NULL ){
+    zoo_DS->ReleaseResultSet( zoo_ResultSet );
+    zoo_ResultSet=NULL;
+  }
+}
+
+/**
+ * Record a file stored during ZOO-Kernel execution
+ * 
+ * @param conf the maps containing the setting of the main.cfg file
+ * @param filename the file's name
+ * @param type the type (Intput,Output,Response)
+ * @param name the maps containing the setting of the main.cfg file
+ */
+void recordStoredFile(maps* conf,const char* filename,const char* type,const char* name){
+  map *uusid=getMapFromMaps(conf,"lenv","usid");
+  map *schema=getMapFromMaps(conf,"database","schema");
+  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(uusid->value)+strlen(filename)+strlen(type)+(name!=NULL?strlen(name):2)+68+1)*sizeof(char));
+  if(name!=NULL)
+    sprintf(sqlQuery,"INSERT INTO %s.files (uuid,filename,nature,name) VALUES ('%s','%s','%s','%s');",schema->value,uusid->value,filename,type,name);
+  else
+    sprintf(sqlQuery,"INSERT INTO %s.files (uuid,filename,nature,name) VALUES ('%s','%s','%s',NULL);",schema->value,uusid->value,filename,type);
+  execSql(conf,sqlQuery);
+  cleanUpResultSet(conf);
+}
+
+/**
+ * Insert the reference tuple corresponding to the running service
+ * 
+ * @param conf the maps containing the setting of the main.cfg file
+ */
+void recordServiceStatus(maps* conf){
+  map *sid=getMapFromMaps(conf,"lenv","sid");
+  map *osid=getMapFromMaps(conf,"lenv","osid");
+  map *uusid=getMapFromMaps(conf,"lenv","usid");
+  map *schema=getMapFromMaps(conf,"database","schema");
+  char *sqlQuery=(char*)malloc((strlen(schema->value)+
+				strlen(uusid->value)+
+				strlen(osid->value)+
+				strlen(sid->value)+56+1)*sizeof(char));
+  sprintf(sqlQuery,
+	  "INSERT INTO %s.services (uuid,sid,osid)"
+	  "VALUES ('%s','%s','%s');",
+	  schema->value,
+	  uusid->value,sid->value,osid->value);
+  execSql(conf,sqlQuery);
+  cleanUpResultSet(conf);
+}
+
+/**
+ * Store the content of the result file
+ * 
+ * @param conf the maps containing the setting of the main.cfg file
+ * @param filename the file's name
+ */
+void recordResponse(maps* conf,char* filename){
+  FILE *file = fopen (filename, "rb");
+  fseek (file, 0, SEEK_END);
+  long flen = ftell (file);
+  fseek (file, 0, SEEK_SET);
+  char *tmps = (char *) malloc ((flen + 1) * sizeof (char));
+  fread (tmps, flen, 1, file);
+  tmps[flen]=0;
+  fclose(file);
+  map *sid=getMapFromMaps(conf,"lenv","usid");
+  map *schema=getMapFromMaps(conf,"database","schema");
+  char *sqlQuery=(char*)malloc((strlen(schema->value)+flen+strlen(sid->value)+51+1)*sizeof(char));
+  sprintf(sqlQuery,"UPDATE %s.services set response=$$%s$$ where uuid=$$%s$$;",schema->value,tmps,sid->value);
+  execSql(conf,sqlQuery);
+  cleanUpResultSet(conf);
+}
+
+/**
+ * Update the current status of the running service.
+ *
+ * @param conf the map containing the setting of the main.cfg file
+ * @return 0 on success, -2 if shmget failed, -1 if shmat failed
+ */
+int _updateStatus(maps* conf){
+  map *sid=getMapFromMaps(conf,"lenv","usid");
+  map *p=getMapFromMaps(conf,"lenv","status");
+  map *msg=getMapFromMaps(conf,"lenv","message");
+  map *schema=getMapFromMaps(conf,"database","schema");
+  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(msg->value)+strlen(p->value)+strlen(sid->value)+64+1)*sizeof(char));
+  sprintf(sqlQuery,"UPDATE %s.services set status=$$%s$$,message=$$%s$$ where uuid=$$%s$$;",schema->value,p->value,msg->value,sid->value);
+  fflush(stderr);
+  if( zoo_DS == NULL )
+    init_sql(conf);
+  execSql(conf,sqlQuery);
+  cleanUpResultSet(conf);
+  return 0;
+}
+
+/**
+ * Get the ongoing status of a running service 
+ *
+ * @param conf the maps containing the setting of the main.cfg file
+ * @param pid the service identifier (usid key from the [lenv] section)
+ * @return the reported status char* (MESSAGE|POURCENTAGE)
+ */
+char* _getStatus(maps* conf,char* pid){
+  map *schema=getMapFromMaps(conf,"database","schema");
+  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
+  sprintf(sqlQuery,"select status||'|'||message from %s.services where uuid=$$%s$$;",schema->value,pid);
+  if( zoo_DS == NULL )
+    init_sql(conf);
+  execSql(conf,sqlQuery);
+  OGRFeature  *poFeature = NULL;
+  const char *tmp1;
+  while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
+    for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
+      if( poFeature->IsFieldSet( iField ) ){
+	tmp1=strdup(poFeature->GetFieldAsString( iField ));
+      }
+      else
+	tmp1=NULL;
+    }
+    OGRFeature::DestroyFeature( poFeature );
+  }
+  cleanUpResultSet(conf);
+  return (char*)tmp1;
+}
+
+/**
+ * Read the cache file of a running service 
+ *
+ * @param conf the maps containing the setting of the main.cfg file
+ * @param pid the service identifier (usid key from the [lenv] section)
+ * @return the reported status char* (temporary/final result)
+ */
+char* _getStatusFile(maps* conf,char* pid){
+  map *schema=getMapFromMaps(conf,"database","schema");
+  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
+  sprintf(sqlQuery,"select response from %s.services where uuid=$$%s$$;",schema->value,pid);
+  if( zoo_DS == NULL )
+    init_sql(conf);
+  execSql(conf,sqlQuery);
+  OGRFeature  *poFeature = NULL;
+  const char *tmp1;
+  int hasRes=-1;
+  while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
+    for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
+      if( poFeature->IsFieldSet( iField ) ){
+	tmp1=strdup(poFeature->GetFieldAsString( iField ));
+	hasRes=1;
+      }
+      else
+	tmp1=NULL;
+    }
+    OGRFeature::DestroyFeature( poFeature );
+  }
+  if(hasRes<0)
+    tmp1=NULL;
+  cleanUpResultSet(conf);
+  return (char*)tmp1;
+}
+
+/**
+ * Stop handling status repport.
+ *
+ * @param conf the map containing the setting of the main.cfg file
+ */
+void unhandleStatus(maps* conf){
+  map *sid=getMapFromMaps(conf,"lenv","usid");
+  char *sqlQuery=(char*)malloc((strlen(sid->value)+52+1)*sizeof(char));
+  sprintf(sqlQuery,"UPDATE services set end_time=now() where uuid=$$%s$$;",sid->value);
+  execSql(conf,sqlQuery);
+  cleanUpResultSet(conf);
+  close_sql(conf);
+  end_sql();
+}
+
+#endif
Index: trunk/zoo-project/zoo-kernel/sqlapi.h
===================================================================
--- trunk/zoo-project/zoo-kernel/sqlapi.h	(revision 651)
+++ trunk/zoo-project/zoo-kernel/sqlapi.h	(revision 652)
@@ -30,8 +30,14 @@
 #endif
 
-char *get_uuid();
-void init_sql(const char* host,const char *user, const char *passwd, const char * bdd,int port);
-int add_job(const char * uuid);
-int start_job(const char *uuid);
+#ifdef RELY_ON_DB
+#include "service.h"
+#include "service_internal.h"
+  void init_sql(maps*);
+  void close_sql(maps*);
+  int execSql(maps*,const char*);
+  void recordStoredFile(maps*,const char*,const char*,const char*);
+  void recordServiceStatus(maps*);
+  void recordResponse(maps*,char*,bool);
+#endif
 
 #ifdef __cplusplus
Index: trunk/zoo-project/zoo-kernel/zoo_service_loader.c
===================================================================
--- trunk/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 651)
+++ trunk/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 652)
@@ -1669,4 +1669,6 @@
   }
 
+
+  // Define each env variable in runing environment
   maps *curs = getMaps (m, "env");
   if (curs != NULL)
@@ -1728,7 +1730,5 @@
 #endif
 
-  /**
-   * Need to check if we need to fork to load a status enabled 
-   */
+  // Need to check if we need to fork to load a status enabled 
   r_inputs = NULL;
   map *store = getMap (request_inputs, "storeExecuteResponse");
@@ -1787,6 +1787,4 @@
   _tmpMaps->name = zStrdup ("lenv");
   char tmpBuff[100];
-  semid lid = getShmLockId (NULL, 1);
-  lockShm (lid);
   struct ztimeval tp;
   if (zGettimeofday (&tp, NULL) == 0)
@@ -1794,7 +1792,5 @@
   else
     sprintf (tmpBuff, "%i", (cpid + (int) time (NULL)));
-  unlockShm (lid);
-  removeShmLock (NULL, 1);
-  _tmpMaps->content = createMap ("usid", tmpBuff);
+  _tmpMaps->content = createMap ("osid", tmpBuff);
   _tmpMaps->next = NULL;
   sprintf (tmpBuff, "%i", cpid);
@@ -1802,4 +1798,5 @@
   char* tmpUuid=get_uuid();
   addToMap (_tmpMaps->content, "uusid", tmpUuid);
+  addToMap (_tmpMaps->content, "usid", tmpUuid);
   free(tmpUuid);
   addToMap (_tmpMaps->content, "status", "0");
@@ -1910,5 +1907,5 @@
     }
 #endif
-  char *fbkp, *fbkp1;
+  char *fbkp, *fbkpid, *fbkp1, *flog;
   FILE *f0, *f1;
   if (status != NULL)
@@ -1977,25 +1974,47 @@
 	   * process answer to http client.
 	   */
-#ifndef WIN32
-          zSleep (1);
-#endif
-          r_inputs = getMapFromMaps (m, "lenv", "usid");
+	  map* usid = getMapFromMaps (m, "lenv", "uusid");
+          r_inputs = getMapFromMaps (m, "lenv", "osid");
           int cpid = atoi (r_inputs->value);
           r_inputs = getMapFromMaps (m, "main", "tmpPath");
-          //map *r_inputs1 = getMap (s1->content, "ServiceProvider");
-		  map* r_inputs1 = createMap("ServiceName", s1->name);
-
+	  map* r_inputs1 = createMap("ServiceName", s1->name);
+
+	  // Create PID file referencing the OS process identifier
+          fbkpid =
+            (char *)
+            malloc ((strlen (r_inputs->value) +
+                     strlen (usid->value) + 7) * sizeof (char));
+          sprintf (fbkpid, "%s/%s.pid", r_inputs->value, usid->value);
+
+          f0 = freopen (fbkpid, "w+", stdout);
+	  fprintf(stdout,"%d",getpid());
+	  fflush(stdout);
+
+	  // Create SID file referencing the semaphore name
           fbkp =
             (char *)
             malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
-                     1024) * sizeof (char));
-          sprintf (fbkp, "%s/%s_%d.xml", r_inputs->value, r_inputs1->value,
-                   cpid);
-          char *flog =
+                     strlen (usid->value) + 7) * sizeof (char));
+          sprintf (fbkp, "%s/%s.sid", r_inputs->value, usid->value);
+
+          FILE* f2 = fopen (fbkp, "w+");
+	  map* tmpm=getMapFromMaps (m, "lenv", "osid");
+	  fprintf(f2,"%s",tmpm->value);
+	  fflush(f2);
+	  fclose(f2);
+	  free(fbkp);
+
+          fbkp =
             (char *)
             malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
-                     1024) * sizeof (char));
-          sprintf (flog, "%s/%s_%d_error.log", r_inputs->value,
-                   r_inputs1->value, cpid);
+                     strlen (usid->value) + 7) * sizeof (char));
+          sprintf (fbkp, "%s/%s_%s.xml", r_inputs->value, r_inputs1->value,
+                   usid->value);
+          flog =
+            (char *)
+            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
+                     strlen (usid->value) + 13) * sizeof (char));
+          sprintf (flog, "%s/%s_%s_error.log", r_inputs->value,
+                   r_inputs1->value, usid->value);
 #ifdef DEBUG
           fprintf (stderr, "RUN IN BACKGROUND MODE \n");
@@ -2005,19 +2024,5 @@
 #endif
           freopen (flog, "w+", stderr);
-          semid lid = getShmLockId (m, 1);
           fflush (stderr);
-          if (lid < 0)
-            {
-              return errorException (m, _("Lock failed"),
-			      "InternalError", NULL);
-            }
-          else
-            {
-              if (lockShm (lid) < 0)
-                {
-		  return errorException (m, _("Lock failed"),
-					 "InternalError", NULL);
-                }
-            }
           f0 = freopen (fbkp, "w+", stdout);
           rewind (stdout);
@@ -2036,15 +2041,18 @@
                                 request_output_real_format);
           fflush (stdout);
-          unlockShm (lid);
+#ifdef RELY_ON_DB
+	  init_sql(m);
+	  recordServiceStatus(m);
+	  recordResponse(m,fbkp);
+#endif
           fflush (stderr);
           fbkp1 =
             (char *)
             malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
-                     1024) * sizeof (char));
-          sprintf (fbkp1, "%s/%s_final_%d.xml", r_inputs->value,
-                   r_inputs1->value, cpid);
+                     strlen (usid->value) + 13) * sizeof (char));
+          sprintf (fbkp1, "%s/%s_final_%s.xml", r_inputs->value,
+                   r_inputs1->value, usid->value);
 
           f1 = freopen (fbkp1, "w+", stdout);
-          free (flog);
 
 	  if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
@@ -2062,6 +2070,6 @@
 	    free (tmpmaps);
 	    fflush (stdout);
-	    unlockShm (lid);
 	    fflush (stderr);
+	    unhandleStatus (m);
 	    return -1;
 	  }
@@ -2073,8 +2081,8 @@
       else
         {
-      /**
-       * error server don't accept the process need to output a valid 
-       * error response here !!!
-       */
+	  /**
+	   * error server don't accept the process need to output a valid 
+	   * error response here !!!
+	   */
           eres = -1;
           errorException (m, _("Unable to run the child process properly"),
@@ -2109,14 +2117,16 @@
       fclose (stdout);
       fclose (stderr);
-    /**
-     * Dump back the final file fbkp1 to fbkp
-     */
+      /**
+       * Dump back the final file fbkp1 to fbkp
+       */
       fclose (f0);
       fclose (f1);
       FILE *f2 = fopen (fbkp1, "rb");
+#ifndef RELY_ON_DB
       semid lid = getShmLockId (m, 1);
       if (lid < 0)
         return -1;
       lockShm (lid);
+#endif
       FILE *f3 = fopen (fbkp, "wb+");
       free (fbkp);
@@ -2129,9 +2139,17 @@
       fclose (f2);
       fclose (f3);
-      unlockShm (lid);
+      unlink (fbkpid);
+#ifndef RELY_ON_DB
+      removeShmLock (m, 1);
+#else
+      recordResponse(m,fbkp1);
+#endif
       unlink (fbkp1);
+      unlink (flog);
+      unhandleStatus (m);
+      free(fbkpid);
+      free (flog);
       free (fbkp1);
       free (tmps1);
-      unhandleStatus (m);
     }
 
