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