ce116ae88d0c19a01d1d4b04e30db6df17d19422
[util-vserver.git] / lib / getvserverctx.c
1 // $Id: getvserverctx.c,v 1.8 2004/06/27 13:02:07 ensc Exp $    --*- 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 #include "lib_internal/util.h"
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #ifdef VC_ENABLE_API_COMPAT
36 #include <fcntl.h>
37
38 static xid_t
39 extractLegacyXID(char const *dir, char const *basename)
40 {
41   size_t        l1 = strlen(dir);
42   size_t        l2 = strlen(basename);
43   char          path[l1 + l2 + sizeof("/.ctx")];
44   char *        ptr = path;
45   int           fd;
46   ssize_t       len;
47   xid_t         result = VC_NOXID;
48
49   ptr    = Xmemcpy(ptr, dir, l1);
50   *ptr++ = '/';
51   ptr    = Xmemcpy(ptr, basename, l2);
52   ptr    = Xmemcpy(ptr, ".ctx",    5);
53
54   fd = open(path, O_RDONLY);
55   if (fd==-1) return VC_NOXID;
56
57   len = lseek(fd, 0, SEEK_END);
58
59   if (len!=-1 && lseek(fd, 0, SEEK_SET)!=-1) {
60     char        buf[len+2];
61     char const  *pos = 0;
62
63     buf[0] = '\n';
64     
65     if (read(fd, buf+1, len+1)==len) {
66       buf[len+1] = '\0';
67       pos        = strstr(buf, "\nS_CONTEXT=");
68     }
69
70     if (pos) pos += 11;
71     if (*pos>='1' && *pos<='9')
72       result = atoi(pos);
73   }
74
75   close(fd);
76   return result;
77 }
78 #else
79 static xid_t
80 extractLegacyXID(char const UNUSED *dir, char const UNUSED *basename)
81 {
82   return VC_NOXID;
83 }
84 #endif
85
86
87 static xid_t
88 getCtxFromFile(char const *pathname)
89 {
90   int           fd;
91   off_t         len;
92
93   fd = open(pathname, O_RDONLY);
94
95   if (fd==-1 ||
96       (len=lseek(fd, 0, SEEK_END))==-1 ||
97       (len>50) ||
98       (lseek(fd, 0, SEEK_SET)==-1))
99     return VC_NOCTX;
100
101   {
102   char          buf[len+1];
103   char          *errptr;
104   xid_t         res;
105   
106   if (TEMP_FAILURE_RETRY(read(fd, buf, len+1))!=len)
107     return VC_NOCTX;
108
109   res = strtol(buf, &errptr, 10);
110   if (*errptr!='\0' && *errptr!='\n') return VC_NOCTX;
111
112   return res;
113   }
114 }
115
116 xid_t
117 vc_getVserverCtx(char const *id, vcCfgStyle style, bool honor_static, bool *is_running)
118 {
119   size_t                l1 = strlen(id);
120   char                  buf[sizeof(CONFDIR "//") + l1 + sizeof("/context")];
121                             
122   if (style==vcCFG_NONE || style==vcCFG_AUTO)
123     style = vc_getVserverCfgStyle(id);
124
125   if (is_running) *is_running = false;
126
127   switch (style) {
128     case vcCFG_NONE             :  return VC_NOCTX;
129     case vcCFG_LEGACY           :
130       return extractLegacyXID(DEFAULT_PKGSTATEDIR, id);
131     case vcCFG_RECENT_SHORT     :
132     case vcCFG_RECENT_FULL      : {
133       size_t            idx = 0;
134       xid_t             res = 0;
135
136       if (style==vcCFG_RECENT_SHORT) {
137         memcpy(buf, CONFDIR "/", sizeof(CONFDIR "/")-1);
138         idx  = sizeof(CONFDIR "/") - 1;
139       }
140       memcpy(buf+idx, id, l1);    idx += l1;
141       memcpy(buf+idx, "/run", 5);       // appends '\0' too
142       
143       res = getCtxFromFile(buf);
144       if (is_running) *is_running = res!=VC_NOCTX;
145       
146       if (res==VC_NOCTX && honor_static) {
147         memcpy(buf+idx, "/context", 9); // appends '\0' too
148
149         res = getCtxFromFile(buf);
150       }
151       
152       return res;
153     }
154     default                     :  return VC_NOCTX;
155   }
156 }