Elseek(), Emkstemp(): added
[util-vserver.git] / util-vserver / src / vunify-init.ic
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 }
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++  = PKGMGMT_INFO_PROG;
101     *ptr++  = vserver;
102     *ptr++  = "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 (vserver && global_args->do_renew) {
138     getConfigfileList(vserver, &fixed_files, &fixed_count, buf+0);
139   }
140
141   // abuse special values (NULL, empty string) to skip the next step
142   if (exclude_file && *exclude_file) {
143     readExcludeList(exclude_file,
144                     &expr_files,  &expr_count,
145                     buf+1);
146   }
147
148   MatchList_init(list, vdir, fixed_count + expr_count);
149   list->buf       = Emalloc(sizeof(void *) * 2);
150   list->buf[0]    = buf[0];
151   list->buf[1]    = buf[1];
152   list->buf_count = 2;
153
154   MatchList_appendFiles(list, 0,           fixed_files, fixed_count, false);
155   MatchList_appendFiles(list, fixed_count, expr_files,  expr_count,  true);
156
157   free(expr_files);
158   free(fixed_files);
159 }
160
161 static bool
162 initMatchListByVserver(struct MatchList *list, char const *vserver,
163               char const **res_appdir)
164 {
165   vcCfgStyle    style;
166   char const    *vdir;
167   char const    *appdir;
168
169   style  = vc_getVserverCfgStyle(vserver);
170   vdir   = vc_getVserverVdir(  vserver, style);
171   appdir = vc_getVserverAppDir(vserver, style, "vunify");
172
173   if (vdir==0 || appdir==0) {
174     free((char *)appdir);
175     free((char *)vdir);
176     return false;
177   }
178
179   {
180     size_t              l1 = strlen(appdir);
181     char                excl_list[l1 + sizeof("/exclude")];
182
183     memcpy(excl_list,    appdir, l1);
184     memcpy(excl_list+l1, "/exclude", 9);
185     
186       // 'vdir' is transferred to matchlist and must not be free'ed here
187     initMatchList(list, vserver, vdir, excl_list);
188   }
189
190   if (res_appdir!=0)
191     *res_appdir = appdir;
192   else
193     free((char *)appdir);
194   
195   return true;
196 }