added missing includes
[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
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 static ctx_t
34 getCtxFromFile(char const *pathname)
35 {
36   int           fd;
37   off_t         len;
38
39   fd = open(pathname, O_RDONLY);
40
41   if (fd==-1 ||
42       (len=lseek(fd, 0, SEEK_END))==-1 ||
43       (len>50) ||
44       (lseek(fd, 0, SEEK_SET)==-1))
45     return VC_NOCTX;
46   
47   char  buf[len+1];
48   if (TEMP_FAILURE_RETRY(read(fd, buf, len+1))!=len)
49     return VC_NOCTX;
50
51   char          *errptr;
52   ctx_t         res;
53
54   res = strtol(buf, &errptr, 10);
55   if (*errptr!='\0' && *errptr!='\n') return VC_NOCTX;
56
57   return res;
58 }
59
60 ctx_t
61 vc_getVserverCtx(char const *id, vcCfgStyle style)
62 {
63   size_t                l1 = strlen(id);
64   char                  buf[sizeof(CONFDIR "//") + l1 + sizeof("/run")];
65                             
66   if (style==vcCFG_NONE || style==vcCFG_AUTO)
67     style = vc_getVserverCfgStyle(id);
68   
69   switch (style) {
70     case vcCFG_NONE             :  return VC_NOCTX;
71     case vcCFG_LEGACY           :  return VC_NOCTX;     // todo
72     case vcCFG_RECENT_SHORT     :
73     case vcCFG_RECENT_FULL      : {
74       size_t            idx = 0;
75       if (style==vcCFG_RECENT_SHORT) {
76         memcpy(buf, CONFDIR "/", sizeof(CONFDIR "/")-1);
77         idx  = sizeof(CONFDIR "/") - 1;
78       }
79       memcpy(buf+idx, id, l1);    idx += l1;
80       memcpy(buf+idx, "/run", 4); idx += 4;
81       buf[idx] = '\0';
82       
83
84       return getCtxFromFile(buf);
85     }
86     default                     :  return VC_NOCTX;
87   }
88 }