minor cleanups
[util-vserver.git] / util-vserver / src / vunify-init.hc
1 // $Id$    --*- 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 #include "wrappers-io.h"
19 #include "pathconfig.h"
20
21 static void
22 readExcludeListFD(int fd,
23                   char ***files,  size_t *size,
24                   char **buf)
25 {
26   off_t         len;
27   size_t        lines = 0;
28   char          *ptr;
29   
30   if (fd==-1) return; // todo: message on verbose?
31
32   len = Elseek(fd, 0, SEEK_END);
33   Elseek(fd, 0, SEEK_SET);
34
35   *buf = Emalloc(sizeof(*buf) * (len+1));
36   EreadAll(fd, *buf, len);
37   (*buf)[len] = '\0';
38
39   ptr = *buf;
40   while ((ptr=strchr(ptr, '\n'))) {
41     ++lines;
42     ++ptr;
43   }
44
45   ++lines;
46   *files = Emalloc(sizeof(**files) * lines);
47
48   *size = 0;
49   ptr   = *buf;
50   while (*ptr) {
51     char        *end_ptr = strchr(ptr, '\n');
52
53     assert(*size<lines);
54     if (end_ptr==0) break;
55
56     if (*ptr!='#') {
57       char      *tmp = end_ptr;
58       do {
59         *tmp-- = '\0';
60       } while (tmp>ptr && *tmp==' ');
61       
62       if (tmp>ptr) (*files)[(*size)++] = ptr;
63     }
64
65     ptr = end_ptr+1;
66   }
67 }
68
69 static void
70 readExcludeList(char const *filename,
71                 char ***files,  size_t *size,
72                 char **buf)
73 {
74   int           fd = open(filename, O_RDONLY);
75   if (fd==-1) return; // todo: message on verbose?
76
77   readExcludeListFD(fd, files, size, buf);
78   Eclose(fd);
79 }
80
81 static void
82 getConfigfileList(char const *vserver,
83                   char ***files, size_t *size,
84                   char **buf)
85 {
86   char          tmpname[] = "/tmp/vunify.XXXXXX";
87   pid_t         pid;
88   int           fd = Emkstemp(tmpname);
89
90   Eunlink(tmpname);
91   pid = Efork();
92
93   if (pid==0) {
94     char        *args[10];
95     char const  **ptr = (char const **)(args)+0;
96     
97     Edup2(fd, 1);
98     //Eclose(0);
99     if (fd!=1) Eclose(fd);
100
101     *ptr++  = VPKG_PROG;
102     *ptr++  = vserver;
103     *ptr++  = "get-conffiles";
104     *ptr    = 0;
105
106     Eexecv(args[0], args);
107   }
108   else {
109     int         status;
110     
111     if (TEMP_FAILURE_RETRY(wait4(pid, &status, 0,0))==-1) {
112       perror("wait4()");
113       exit(1);
114     }
115
116     if (!WIFEXITED(status) || WEXITSTATUS(status)!=0) {
117       WRITE_MSG(2, "failed to determine configfiles\n");
118       exit(1);
119     }
120
121     readExcludeListFD(fd, files, size, buf);
122     Eclose(fd);
123   }
124 }
125
126 static void
127 initMatchList(struct MatchList *list, char const *vserver,
128               char const *vdir, char const *exclude_file)
129 {
130   char                  *buf[2] = { 0,0 };
131   
132   char                  **fixed_files = 0;
133   size_t                fixed_count   = 0;
134
135   char                  **expr_files  = 0;
136   size_t                expr_count    = 0;
137
138   if (global_args->verbosity>3) {
139     WRITE_MSG(1, "Initializing exclude-list for ");
140     WRITE_STR(1, vdir);
141     if (vserver!=0) {
142       WRITE_MSG(1, " (");
143       WRITE_STR(1, vserver);
144       WRITE_MSG(1, ")");
145     }
146     WRITE_MSG(1, "\n");
147   }
148   if (vserver && global_args->do_renew) {
149     if (global_args->verbosity>4)
150       WRITE_MSG(1, "  Fetching configuration-file list from packagemanagement\n");
151     getConfigfileList(vserver, &fixed_files, &fixed_count, buf+0);
152   }
153
154   // abuse special values (NULL, empty string) to skip the next step
155   if (exclude_file && *exclude_file) {
156     if (global_args->verbosity>4) WRITE_MSG(1, "  Reading exclude file\n");
157     readExcludeList(exclude_file,
158                     &expr_files,  &expr_count,
159                     buf+1);
160   }
161
162   MatchList_init(list, vdir, fixed_count + expr_count);
163   list->buf       = Emalloc(sizeof(void *) * 3);
164   list->buf[0]    = buf[0];
165   list->buf[1]    = buf[1];
166   list->buf[2]    = vdir;
167   list->buf_count = 3;
168
169   MatchList_appendFiles(list, 0,           fixed_files, fixed_count, false);
170   MatchList_appendFiles(list, fixed_count, expr_files,  expr_count,  true);
171
172   free(expr_files);
173   free(fixed_files);
174 }
175
176 static bool
177 initMatchListByVserver(struct MatchList *list, char const *vserver,
178                        char const **res_appdir)
179 {
180   vcCfgStyle    style;
181   char const    *vdir;
182   char const    *appdir;
183
184   style  = vc_getVserverCfgStyle(vserver);
185   vdir   = vc_getVserverVdir(  vserver, style, true);
186   appdir = vc_getVserverAppDir(vserver, style, "vunify");
187
188   if (vdir==0 || appdir==0) {
189     free((char *)appdir);
190     free((char *)vdir);
191     return false;
192   }
193
194   {
195     size_t              l1 = strlen(appdir);
196     char                tmp[l1 + sizeof("/exclude")];
197     char const *        excl_list;
198
199     memcpy(tmp,    appdir, l1);
200     memcpy(tmp+l1, "/exclude", 9);
201
202     excl_list = tmp;
203     if (access(excl_list, R_OK)==-1) excl_list = CONFDIR   "/.defaults/apps/vunify/exclude";
204     if (access(excl_list, R_OK)==-1) excl_list = PKGLIBDIR "/defaults/vunify-exclude";
205
206       // 'vdir' is transferred to matchlist and must not be free'ed here
207     initMatchList(list, vserver, vdir, excl_list);
208   }
209
210   if (res_appdir!=0)
211     *res_appdir = appdir;
212   else
213     free((char *)appdir);
214   
215   return true;
216 }
217
218
219 static void
220 initModeManually(struct Arguments const UNUSED *args, int argc, char *argv[])
221 {
222   int           i, count=argc/2;
223   
224   if (argc%2) {
225     WRITE_MSG(2, "Odd number of (path,excludelist) arguments\n");
226     exit(1);
227   }
228
229   if (count<2) {
230     WRITE_MSG(2, "No reference path(s) given\n");
231     exit(1);
232   }
233
234   initMatchList(&global_info.dst_list, 0, argv[0], argv[1]);
235
236   --count;
237   global_info.src_lists.v = Emalloc(sizeof(struct MatchList) * count);
238   global_info.src_lists.l = count;
239
240   for (i=0; i<count; ++i)
241     initMatchList(global_info.src_lists.v+i, 0,
242                   strdup(argv[2 + i*2]), argv[3 + i*2]);
243 }
244
245 static int
246 selectRefserver(struct dirent const *ent)
247 {
248   return strncmp(ent->d_name, "refserver.", 10)==0;
249 }
250
251 static void
252 initModeVserver(struct Arguments const UNUSED *args, int argc, char *argv[])
253 {
254   char const    *appdir;
255   int           cur_dir = Eopen(".", O_RDONLY, 0);
256   struct dirent **entries;
257   int           count, i;
258   
259   if (argc!=1) {
260     WRITE_MSG(2, "More than one vserver is not supported\n");
261     exit(1);
262   }
263
264   if (!initMatchListByVserver(&global_info.dst_list, argv[0], &appdir)) {
265     WRITE_MSG(2, "unification not configured for this vserver\n");
266     exit(1);
267   }
268
269   Echdir(appdir);
270   count = scandir(".", &entries, selectRefserver, alphasort);
271   if (count==-1) {
272     perror("scandir()");
273     exit(1);
274   }
275
276   if (count==0) {
277     WRITE_MSG(2, "no reference vserver configured\n");
278     exit(1);
279   }
280
281   global_info.src_lists.v = Emalloc(sizeof(struct MatchList) * count);
282   global_info.src_lists.l = count;
283   for (i=0; i<count; ++i) {
284     char const          *tmp   = entries[i]->d_name;
285     size_t              l      = strlen(tmp);
286     char                vname[sizeof("./") + l];
287
288     memcpy(vname,   "./", 2);
289     memcpy(vname+2, tmp,  l+1);
290     
291     if (!initMatchListByVserver(global_info.src_lists.v+i, vname, 0)) {
292       WRITE_MSG(2, "unification for reference vserver not configured\n");
293       exit(1);
294     }
295
296     free(entries[i]);
297   }
298   free(entries);
299   free(const_cast(char *)(appdir));
300
301   Efchdir(cur_dir);
302   Eclose(cur_dir);
303 }