1 // $Id: getvserverctx.c,v 1.8 2004/06/27 13:02:07 ensc Exp $ --*- c -*--
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);
96 (len=lseek(fd, 0, SEEK_END))==-1 ||
98 (lseek(fd, 0, SEEK_SET)==-1))
106 if (TEMP_FAILURE_RETRY(read(fd, buf, len+1))!=len)
109 res = strtol(buf, &errptr, 10);
110 if (*errptr!='\0' && *errptr!='\n') return VC_NOCTX;
117 vc_getVserverCtx(char const *id, vcCfgStyle style, bool honor_static, bool *is_running)
119 size_t l1 = strlen(id);
120 char buf[sizeof(CONFDIR "//") + l1 + sizeof("/context")];
122 if (style==vcCFG_NONE || style==vcCFG_AUTO)
123 style = vc_getVserverCfgStyle(id);
125 if (is_running) *is_running = false;
128 case vcCFG_NONE : return VC_NOCTX;
130 return extractLegacyXID(DEFAULT_PKGSTATEDIR, id);
131 case vcCFG_RECENT_SHORT :
132 case vcCFG_RECENT_FULL : {
136 if (style==vcCFG_RECENT_SHORT) {
137 memcpy(buf, CONFDIR "/", sizeof(CONFDIR "/")-1);
138 idx = sizeof(CONFDIR "/") - 1;
140 memcpy(buf+idx, id, l1); idx += l1;
141 memcpy(buf+idx, "/run", 5); // appends '\0' too
143 res = getCtxFromFile(buf);
144 if (is_running) *is_running = res!=VC_NOCTX;
146 if (res==VC_NOCTX && honor_static) {
147 memcpy(buf+idx, "/context", 9); // appends '\0' too
149 res = getCtxFromFile(buf);
154 default : return VC_NOCTX;