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"
29 #include <sys/types.h>
36 #ifdef VC_ENABLE_API_COMPAT
40 extractLegacyXID(char const *dir, char const *basename)
42 size_t l1 = strlen(dir);
43 size_t l2 = strlen(basename);
44 char path[l1 + l2 + sizeof("/.ctx")];
48 xid_t result = VC_NOXID;
50 ptr = Xmemcpy(ptr, dir, l1);
52 ptr = Xmemcpy(ptr, basename, l2);
53 ptr = Xmemcpy(ptr, ".ctx", 5);
55 fd = open(path, O_RDONLY);
56 if (fd==-1) return VC_NOXID;
58 len = lseek(fd, 0, SEEK_END);
60 if (len!=-1 && lseek(fd, 0, SEEK_SET)!=-1) {
66 if (read(fd, buf+1, len+1)==len) {
68 pos = strstr(buf, "\nS_CONTEXT=");
72 if (*pos>='1' && *pos<='9')
81 extractLegacyXID(char const UNUSED *dir, char const UNUSED *basename)
89 getCtxFromFile(char const *pathname)
94 fd = open(pathname, O_RDONLY);
96 if (fd==-1) return VC_NOCTX;
97 if ((len=lseek(fd, 0, SEEK_END))==-1 ||
99 (lseek(fd, 0, SEEK_SET)==-1)) {
109 if (TEMP_FAILURE_RETRY(read(fd, buf, len+1))!=len) res = VC_NOCTX;
113 res = strtol(buf, &errptr, 10);
114 if (*errptr!='\0' && *errptr!='\n') res = VC_NOCTX;
123 vc_getVserverCtx(char const *id, vcCfgStyle style, bool honor_static, bool *is_running)
125 size_t l1 = strlen(id);
126 char buf[sizeof(CONFDIR "//") + l1 + sizeof("/context")];
128 if (style==vcCFG_NONE || style==vcCFG_AUTO)
129 style = vc_getVserverCfgStyle(id);
131 if (is_running) *is_running = false;
134 case vcCFG_NONE : return VC_NOCTX;
136 return extractLegacyXID(DEFAULT_PKGSTATEDIR, id);
137 case vcCFG_RECENT_SHORT :
138 case vcCFG_RECENT_FULL : {
142 if (style==vcCFG_RECENT_SHORT) {
143 memcpy(buf, CONFDIR "/", sizeof(CONFDIR "/")-1);
144 idx = sizeof(CONFDIR "/") - 1;
146 memcpy(buf+idx, id, l1); idx += l1;
147 memcpy(buf+idx, "/run", 5); // appends '\0' too
149 res = getCtxFromFile(buf);
151 // when context information could be read, we have to verify that
152 // it belongs to a running vserver and the both vservers are
156 struct vc_vx_info info;
158 // determine the vserver which is associated with the xid resp. skip
159 // this step when the context does not exist. When checking whether
160 // the context exists, do not rely on the success of
161 // vc_get_vx_info() alone but check 'errno' for ESRCH also. Else,
162 // wrong results will be caused e.g. for xid 1 which will fail with
164 cur_name = (vc_get_vx_info(res, &info)!=-1 || errno!=ESRCH ?
165 vc_getVserverByCtx_Internal(res, &style, 0, false) :
168 buf[idx] = '\0'; // cut off the '/run' from the vserver name
170 res = ((cur_name!=0 &&
171 vc_compareVserverById(buf, vcCFG_RECENT_FULL,
172 cur_name, vcCFG_RECENT_FULL)==0)
174 : VC_NOCTX); // correct the value of 'res'
179 if (is_running) // fill 'is_running' information...
180 *is_running = res!=VC_NOCTX;
182 if (res==VC_NOCTX && honor_static) {
183 memcpy(buf+idx, "/context", 9); // appends '\0' too
185 res = getCtxFromFile(buf);
190 default : return VC_NOCTX;