merged with 0.26.90
[util-vserver.git] / util-vserver / lib / getvserverctx.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "vserver.h"
24 #include "pathconfig.h"
25 #include "compat-c99.h"
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 static xid_t
35 getCtxFromFile(char const *pathname)
36 {
37   int           fd;
38   off_t         len;
39
40   fd = open(pathname, O_RDONLY);
41
42   if (fd==-1 ||
43       (len=lseek(fd, 0, SEEK_END))==-1 ||
44       (len>50) ||
45       (lseek(fd, 0, SEEK_SET)==-1))
46     return VC_NOCTX;
47
48   BS;
49   char          buf[len+1];
50   char          *errptr;
51   xid_t         res;
52   
53   if (TEMP_FAILURE_RETRY(read(fd, buf, len+1))!=len)
54     return VC_NOCTX;
55
56   res = strtol(buf, &errptr, 10);
57   if (*errptr!='\0' && *errptr!='\n') return VC_NOCTX;
58
59   return res;
60   BE;
61 }
62
63 xid_t
64 vc_getVserverCtx(char const *id, vcCfgStyle style)
65 {
66   size_t                l1 = strlen(id);
67   char                  buf[sizeof(CONFDIR "//") + l1 + sizeof("/run")];
68                             
69   if (style==vcCFG_NONE || style==vcCFG_AUTO)
70     style = vc_getVserverCfgStyle(id);
71   
72   switch (style) {
73     case vcCFG_NONE             :  return VC_NOCTX;
74     case vcCFG_LEGACY           :  return VC_NOCTX;     // todo
75     case vcCFG_RECENT_SHORT     :
76     case vcCFG_RECENT_FULL      : {
77       size_t            idx = 0;
78       if (style==vcCFG_RECENT_SHORT) {
79         memcpy(buf, CONFDIR "/", sizeof(CONFDIR "/")-1);
80         idx  = sizeof(CONFDIR "/") - 1;
81       }
82       memcpy(buf+idx, id, l1);    idx += l1;
83       memcpy(buf+idx, "/run", 4); idx += 4;
84       buf[idx] = '\0';
85       
86
87       return getCtxFromFile(buf);
88     }
89     default                     :  return VC_NOCTX;
90   }
91 }