3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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.
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.
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.
24 #include "pathconfig.h"
25 #include "compat-c99.h"
26 #include "lib_internal/util.h"
28 #include <sys/types.h>
35 #ifdef VC_ENABLE_API_COMPAT
39 extractLegacyXID(char const *dir, char const *basename)
41 size_t l1 = strlen(dir);
42 size_t l2 = strlen(basename);
43 char path[l1 + l2 + sizeof("/.ctx")];
47 xid_t result = VC_NOXID;
49 ptr = Xmemcpy(ptr, dir, l1);
51 ptr = Xmemcpy(ptr, basename, l2);
52 ptr = Xmemcpy(ptr, ".ctx", 5);
54 fd = open(path, O_RDONLY);
55 if (fd==-1) return VC_NOXID;
57 len = lseek(fd, 0, SEEK_END);
59 if (len!=-1 && lseek(fd, 0, SEEK_SET)!=-1) {
65 if (read(fd, buf+1, len+1)==len) {
67 pos = strstr(buf, "\nS_CONTEXT=");
71 if (*pos>='1' && *pos<='9')
80 extractLegacyXID(char const UNUSED *dir, char const UNUSED *basename)
88 getCtxFromFile(char const *pathname)
93 fd = open(pathname, O_RDONLY);
95 if (fd==-1) return VC_NOCTX;
96 if ((len=lseek(fd, 0, SEEK_END))==-1 ||
98 (lseek(fd, 0, SEEK_SET)==-1)) {
108 if (TEMP_FAILURE_RETRY(read(fd, buf, len+1))!=len) res = VC_NOCTX;
112 res = strtol(buf, &errptr, 10);
113 if (*errptr!='\0' && *errptr!='\n') res = VC_NOCTX;
122 vc_getVserverCtx(char const *id, vcCfgStyle style, bool honor_static, bool *is_running)
124 size_t l1 = strlen(id);
125 char buf[sizeof(CONFDIR "//") + l1 + sizeof("/context")];
127 if (style==vcCFG_NONE || style==vcCFG_AUTO)
128 style = vc_getVserverCfgStyle(id);
130 if (is_running) *is_running = false;
133 case vcCFG_NONE : return VC_NOCTX;
135 return extractLegacyXID(DEFAULT_PKGSTATEDIR, id);
136 case vcCFG_RECENT_SHORT :
137 case vcCFG_RECENT_FULL : {
141 if (style==vcCFG_RECENT_SHORT) {
142 memcpy(buf, CONFDIR "/", sizeof(CONFDIR "/")-1);
143 idx = sizeof(CONFDIR "/") - 1;
145 memcpy(buf+idx, id, l1); idx += l1;
146 memcpy(buf+idx, "/run", 5); // appends '\0' too
148 res = getCtxFromFile(buf);
150 // when context information could be read, we have to verify that
151 // it belongs to a running vserver and the both vservers are
155 struct vc_vx_info info;
157 // determine the vserver which is associated with the xid
158 // resp. skip this step when the context does not exist
159 cur_name = (vc_get_vx_info(res, &info)!=-1 ?
160 vc_getVserverByCtx_Internal(res, &style, 0, false) : 0);
162 buf[idx] = '\0'; // cut off the '/run' from the vserver name
163 res = ((cur_name!=0 &&
164 vc_compareVserverById(buf, vcCFG_RECENT_FULL,
165 cur_name, vcCFG_RECENT_FULL)==0)
167 : VC_NOCTX); // correct the value of 'res'
172 if (is_running) // fill 'is_running' information...
173 *is_running = res!=VC_NOCTX;
175 if (res==VC_NOCTX && honor_static) {
176 memcpy(buf+idx, "/context", 9); // appends '\0' too
178 res = getCtxFromFile(buf);
183 default : return VC_NOCTX;