getRecentName(): use realpath(3) instead of errorprone 'chdir()' calls
[util-vserver.git] / util-vserver / lib / getvserverctx.c
index 774c80f..5334a5c 100644 (file)
@@ -45,7 +45,7 @@ getCtxFromFile(char const *pathname)
       (lseek(fd, 0, SEEK_SET)==-1))
     return VC_NOCTX;
 
-  BS;
+  {
   char         buf[len+1];
   char         *errptr;
   xid_t                res;
@@ -57,34 +57,45 @@ getCtxFromFile(char const *pathname)
   if (*errptr!='\0' && *errptr!='\n') return VC_NOCTX;
 
   return res;
-  BE;
+  }
 }
 
 xid_t
-vc_getVserverCtx(char const *id, vcCfgStyle style)
+vc_getVserverCtx(char const *id, vcCfgStyle style, bool honor_static, bool *is_running)
 {
   size_t               l1 = strlen(id);
-  char                 buf[sizeof(CONFDIR "//") + l1 + sizeof("/run")];
+  char                 buf[sizeof(CONFDIR "//") + l1 + sizeof("/context")];
                            
   if (style==vcCFG_NONE || style==vcCFG_AUTO)
     style = vc_getVserverCfgStyle(id);
-  
+
+  if (is_running) *is_running = false;
+
   switch (style) {
     case vcCFG_NONE            :  return VC_NOCTX;
     case vcCFG_LEGACY          :  return VC_NOCTX;     // todo
     case vcCFG_RECENT_SHORT    :
     case vcCFG_RECENT_FULL     : {
       size_t           idx = 0;
+      xid_t            res = 0;
+
       if (style==vcCFG_RECENT_SHORT) {
        memcpy(buf, CONFDIR "/", sizeof(CONFDIR "/")-1);
        idx  = sizeof(CONFDIR "/") - 1;
       }
       memcpy(buf+idx, id, l1);    idx += l1;
-      memcpy(buf+idx, "/run", 4); idx += 4;
-      buf[idx] = '\0';
+      memcpy(buf+idx, "/run", 5);      // appends '\0' too
+      
+      res = getCtxFromFile(buf);
+      if (is_running) *is_running = res!=VC_NOCTX;
       
+      if (res==VC_NOCTX && honor_static) {
+       memcpy(buf+idx, "/context", 9); // appends '\0' too
 
-      return getCtxFromFile(buf);
+       res = getCtxFromFile(buf);
+      }
+      
+      return res;
     }
     default                    :  return VC_NOCTX;
   }