added Vector_zeroEnd() function
[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   {
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   }
61 }
62
63 xid_t
64 vc_getVserverCtx(char const *id, vcCfgStyle style, bool honor_static, bool *is_running)
65 {
66   size_t                l1 = strlen(id);
67   char                  buf[sizeof(CONFDIR "//") + l1 + sizeof("/context")];
68                             
69   if (style==vcCFG_NONE || style==vcCFG_AUTO)
70     style = vc_getVserverCfgStyle(id);
71
72   if (is_running) *is_running = false;
73
74   switch (style) {
75     case vcCFG_NONE             :  return VC_NOCTX;
76     case vcCFG_LEGACY           :  return VC_NOCTX;     // todo
77     case vcCFG_RECENT_SHORT     :
78     case vcCFG_RECENT_FULL      : {
79       size_t            idx = 0;
80       xid_t             res = 0;
81
82       if (style==vcCFG_RECENT_SHORT) {
83         memcpy(buf, CONFDIR "/", sizeof(CONFDIR "/")-1);
84         idx  = sizeof(CONFDIR "/") - 1;
85       }
86       memcpy(buf+idx, id, l1);    idx += l1;
87       memcpy(buf+idx, "/run", 5);       // appends '\0' too
88       
89       res = getCtxFromFile(buf);
90       if (is_running) *is_running = res!=VC_NOCTX;
91       
92       if (res==VC_NOCTX && honor_static) {
93         memcpy(buf+idx, "/context", 9); // appends '\0' too
94
95         res = getCtxFromFile(buf);
96       }
97       
98       return res;
99     }
100     default                     :  return VC_NOCTX;
101   }
102 }