merge with 0.30.213
[util-vserver.git] / vserver-start / configuration.c
1 // $Id: configuration.c 1746 2004-10-19 21:11:10Z ensc $    --*- c -*--
2
3 // Copyright (C) 2004 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 "configuration.h"
24 #include "interface.h"
25
26 #include <lib_internal/util.h>
27 #include <lib_internal/filecfg.h>
28 #include <ensc_vector/vector.h>
29 #include <lib/internal.h>
30
31 #include <dirent.h>
32 #include <string.h>
33
34 static inline bool
35 getSingleInterface(struct Interface *res,
36                    struct Interface const *tmpl,
37                    PathInfo const *basedir, char const *d_entry)
38 {
39   PathInfo      ent  = { .d = d_entry, .l = strlen(d_entry) };
40   PathInfo      path = *basedir;
41   char          path_buf[ENSC_PI_APPSZ(path, ent)];
42
43   PathInfo_append(&path, &ent, path_buf);
44   if (!utilvserver_isDirectory(path.d, true))
45     return true;        // skip non-directories
46
47   return Iface_read(res, &path, tmpl);
48 }
49
50 static inline bool
51 getInterfaces(struct Configuration *cfg)
52 {
53   ENSC_PI_DECLARE(iface_subdir, "interfaces");
54   PathInfo              ifacepath = cfg->cfgdir;
55   char                  path_buf[ENSC_PI_APPSZ(ifacepath, iface_subdir)];
56   struct Interface      iface_default;
57   DIR                   *dir;
58   bool                  rc = true;
59
60   PathInfo_append(&ifacepath, &iface_subdir, path_buf);
61
62   if (!utilvserver_isDirectory(ifacepath.d, true))
63     return true;        // no interface configuration -> ok
64   
65   Iface_init(&iface_default);
66   if (!Iface_read(&iface_default, &ifacepath, 0))
67     return false;
68
69     // iterate through dir-entries...
70   dir = opendir(ifacepath.d);
71   while (dir!=0) {
72     struct dirent       *ent = readdir(dir);
73     struct Interface    iface;
74     
75     if (ent==0)                 break;
76     if (isDotfile(ent->d_name)) continue;       // skip dot-files
77
78     Iface_init(&iface);
79     if (!getSingleInterface(&iface, &iface_default, &ifacepath, ent->d_name))
80       rc = false;
81     else if (iface.addr.ipv4.ip!=0) {   // HACK: non-directory entries would return true also
82       struct Interface  *new_iface = Vector_pushback(&cfg->interfaces);
83       *new_iface = iface;
84     }
85   }
86
87   if (dir!=0)
88     closedir(dir);
89
90   return rc;
91 }
92
93 static bool
94 initVdir(char const **vdir, PathInfo const *cfgdir)
95 {
96   *vdir = vc_getVserverVdir(cfgdir->d, vcCFG_RECENT_FULL, true);
97   if (*vdir==0) {
98     WRITE_MSG(2, "Can not find root-directory of the vserver\n");
99     return false;
100   }
101
102   return true;
103 }
104
105 static bool
106 setFlag(void *flags_v, char const *str, size_t len)
107 {
108   struct vc_ctx_flags   *flags = flags_v;
109   int                   rc = vc_list2cflag(str,len, 0,flags);
110
111   return rc!=-1;
112 }
113
114 static bool
115 setCCap(void *caps_v, char const *str, size_t len)
116 {
117   struct vc_ctx_caps    *caps = caps_v;
118   int                   rc = vc_list2ccap(str,len, 0,caps);
119
120   return rc!=-1;
121 }
122
123 static bool
124 setBCap(void *caps_v, char const *str, size_t len)
125 {
126   struct vc_ctx_caps    *caps = caps_v;
127   int                   rc = vc_list2bcap(str,len, 0,caps);
128
129   return rc!=-1;
130 }
131
132 static bool
133 readSomething(void *dest, PathInfo const *cfgdir, char const *item,
134               FileCfg_MultiLineHandler handler)
135 {
136   char const    *data = FileCfg_readEntryStr(cfgdir, item, true, 0);
137   bool          res   = false;
138
139   if (!data) return true;
140   if (!FileCfg_iterateOverMultiLine(data, handler, dest)) {
141     WRITE_MSG(2, "Failed to parse '");
142     WRITE_STR(2, item);
143     WRITE_MSG(2, "' configuration\n");
144     goto finish;
145   }
146
147   res = true;
148   finish:
149   free(const_cast(char *)(data));
150   return res;
151 }
152
153 bool
154 getConfiguration(struct Configuration *cfg, PathInfo const *cfgdir)
155 {
156   cfg->cfgdir = *cfgdir;
157   cfg->nice   = FileCfg_readEntryStr(cfgdir, "nice", false, 0);
158   
159   return (initVdir(&cfg->vdir, cfgdir) &&
160           readSomething(&cfg->ctx_flags, cfgdir, "flags", setFlag) &&
161           readSomething(&cfg->ctx_caps,  cfgdir, "ccapabilities", setCCap) &&
162           readSomething(&cfg->ctx_caps,  cfgdir, "bcapabilities", setBCap) &&
163           getInterfaces(cfg));
164 }