]>
Commit | Line | Data |
---|---|---|
03e39e59 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
fc4b5c9f | 3 | // $Id: dpkgpm.cc,v 1.11 1999/07/09 04:11:34 jgg Exp $ |
03e39e59 AL |
4 | /* ###################################################################### |
5 | ||
6 | DPKG Package Manager - Provide an interface to dpkg | |
7 | ||
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
10 | // Includes /*{{{*/ | |
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "apt-pkg/dpkgpm.h" | |
13 | #endif | |
14 | #include <apt-pkg/dpkgpm.h> | |
15 | #include <apt-pkg/error.h> | |
16 | #include <apt-pkg/configuration.h> | |
17 | ||
18 | #include <unistd.h> | |
19 | #include <stdlib.h> | |
20 | #include <fcntl.h> | |
21 | #include <sys/types.h> | |
22 | #include <sys/wait.h> | |
23 | #include <signal.h> | |
24 | #include <errno.h> | |
25 | /*}}}*/ | |
26 | ||
27 | // DPkgPM::pkgDPkgPM - Constructor /*{{{*/ | |
28 | // --------------------------------------------------------------------- | |
29 | /* */ | |
30 | pkgDPkgPM::pkgDPkgPM(pkgDepCache &Cache) : pkgPackageManager(Cache) | |
31 | { | |
32 | } | |
33 | /*}}}*/ | |
34 | // DPkgPM::pkgDPkgPM - Destructor /*{{{*/ | |
35 | // --------------------------------------------------------------------- | |
36 | /* */ | |
37 | pkgDPkgPM::~pkgDPkgPM() | |
38 | { | |
39 | } | |
40 | /*}}}*/ | |
41 | // DPkgPM::Install - Install a package /*{{{*/ | |
42 | // --------------------------------------------------------------------- | |
43 | /* Add an install operation to the sequence list */ | |
44 | bool pkgDPkgPM::Install(PkgIterator Pkg,string File) | |
45 | { | |
46 | if (File.empty() == true || Pkg.end() == true) | |
47 | return _error->Error("Internal Error, No file name for %s",Pkg.Name()); | |
48 | ||
49 | List.push_back(Item(Item::Install,Pkg,File)); | |
50 | return true; | |
51 | } | |
52 | /*}}}*/ | |
53 | // DPkgPM::Configure - Configure a package /*{{{*/ | |
54 | // --------------------------------------------------------------------- | |
55 | /* Add a configure operation to the sequence list */ | |
56 | bool pkgDPkgPM::Configure(PkgIterator Pkg) | |
57 | { | |
58 | if (Pkg.end() == true) | |
59 | return false; | |
60 | ||
61 | List.push_back(Item(Item::Configure,Pkg)); | |
62 | return true; | |
63 | } | |
64 | /*}}}*/ | |
65 | // DPkgPM::Remove - Remove a package /*{{{*/ | |
66 | // --------------------------------------------------------------------- | |
67 | /* Add a remove operation to the sequence list */ | |
fc4b5c9f | 68 | bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge) |
03e39e59 AL |
69 | { |
70 | if (Pkg.end() == true) | |
71 | return false; | |
72 | ||
fc4b5c9f AL |
73 | if (Purge == true) |
74 | List.push_back(Item(Item::Purge,Pkg)); | |
75 | else | |
76 | List.push_back(Item(Item::Remove,Pkg)); | |
6dd55be7 AL |
77 | return true; |
78 | } | |
79 | /*}}}*/ | |
80 | // DPkgPM::RunScripts - Run a set of scripts /*{{{*/ | |
81 | // --------------------------------------------------------------------- | |
82 | /* This looks for a list of script sto run from the configuration file, | |
83 | each one is run with system from a forked child. */ | |
84 | bool pkgDPkgPM::RunScripts(const char *Cnf) | |
85 | { | |
86 | Configuration::Item const *Opts = _config->Tree(Cnf); | |
87 | if (Opts == 0 || Opts->Child == 0) | |
88 | return true; | |
89 | Opts = Opts->Child; | |
90 | ||
91 | // Fork for running the system calls | |
54676e1a | 92 | pid_t Child = ExecFork(); |
6dd55be7 AL |
93 | |
94 | // This is the child | |
95 | if (Child == 0) | |
96 | { | |
6dd55be7 AL |
97 | if (chdir("/tmp/") != 0) |
98 | _exit(100); | |
99 | ||
6dd55be7 AL |
100 | unsigned int Count = 1; |
101 | for (; Opts != 0; Opts = Opts->Next, Count++) | |
102 | { | |
103 | if (Opts->Value.empty() == true) | |
104 | continue; | |
105 | ||
106 | if (system(Opts->Value.c_str()) != 0) | |
107 | _exit(100+Count); | |
108 | } | |
109 | _exit(0); | |
110 | } | |
111 | ||
112 | // Wait for the child | |
113 | int Status = 0; | |
114 | while (waitpid(Child,&Status,0) != Child) | |
115 | { | |
116 | if (errno == EINTR) | |
117 | continue; | |
118 | return _error->Errno("waitpid","Couldn't wait for subprocess"); | |
119 | } | |
120 | ||
121 | // Restore sig int/quit | |
122 | signal(SIGQUIT,SIG_DFL); | |
123 | signal(SIGINT,SIG_DFL); | |
124 | ||
125 | // Check for an error code. | |
126 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
127 | { | |
128 | unsigned int Count = WEXITSTATUS(Status); | |
129 | if (Count > 100) | |
130 | { | |
131 | Count -= 100; | |
132 | for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--); | |
cf544e14 | 133 | _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str()); |
6dd55be7 AL |
134 | } |
135 | ||
136 | return _error->Error("Sub-process returned an error code"); | |
137 | } | |
138 | ||
03e39e59 AL |
139 | return true; |
140 | } | |
141 | /*}}}*/ | |
142 | // DPkgPM::Go - Run the sequence /*{{{*/ | |
143 | // --------------------------------------------------------------------- | |
144 | /* This globs the operations and calls dpkg */ | |
145 | bool pkgDPkgPM::Go() | |
146 | { | |
6dd55be7 AL |
147 | if (RunScripts("DPkg::Pre-Invoke") == false) |
148 | return false; | |
149 | ||
03e39e59 AL |
150 | for (vector<Item>::iterator I = List.begin(); I != List.end();) |
151 | { | |
152 | vector<Item>::iterator J = I; | |
153 | for (; J != List.end() && J->Op == I->Op; J++); | |
30e1eab5 | 154 | |
03e39e59 AL |
155 | // Generate the argument list |
156 | const char *Args[400]; | |
157 | if (J - I > 350) | |
158 | J = I + 350; | |
159 | ||
30e1eab5 AL |
160 | unsigned int n = 0; |
161 | unsigned long Size = 0; | |
162 | Args[n++] = _config->Find("Dir::Bin::dpkg","dpkg").c_str(); | |
163 | Size += strlen(Args[n-1]); | |
03e39e59 | 164 | |
6dd55be7 AL |
165 | // Stick in any custom dpkg options |
166 | Configuration::Item const *Opts = _config->Tree("DPkg::Options"); | |
167 | if (Opts != 0) | |
168 | { | |
169 | Opts = Opts->Child; | |
170 | for (; Opts != 0; Opts = Opts->Next) | |
171 | { | |
172 | if (Opts->Value.empty() == true) | |
173 | continue; | |
174 | Args[n++] = Opts->Value.c_str(); | |
175 | Size += Opts->Value.length(); | |
176 | } | |
177 | } | |
178 | ||
03e39e59 AL |
179 | switch (I->Op) |
180 | { | |
181 | case Item::Remove: | |
182 | Args[n++] = "--force-depends"; | |
30e1eab5 | 183 | Size += strlen(Args[n-1]); |
03e39e59 | 184 | Args[n++] = "--force-remove-essential"; |
30e1eab5 | 185 | Size += strlen(Args[n-1]); |
03e39e59 | 186 | Args[n++] = "--remove"; |
30e1eab5 | 187 | Size += strlen(Args[n-1]); |
03e39e59 AL |
188 | break; |
189 | ||
fc4b5c9f AL |
190 | case Item::Purge: |
191 | Args[n++] = "--force-depends"; | |
192 | Size += strlen(Args[n-1]); | |
193 | Args[n++] = "--force-remove-essential"; | |
194 | Size += strlen(Args[n-1]); | |
195 | Args[n++] = "--purge"; | |
196 | Size += strlen(Args[n-1]); | |
197 | break; | |
198 | ||
03e39e59 AL |
199 | case Item::Configure: |
200 | Args[n++] = "--configure"; | |
30e1eab5 | 201 | Size += strlen(Args[n-1]); |
03e39e59 AL |
202 | break; |
203 | ||
204 | case Item::Install: | |
205 | Args[n++] = "--unpack"; | |
30e1eab5 | 206 | Size += strlen(Args[n-1]); |
03e39e59 AL |
207 | break; |
208 | } | |
209 | ||
210 | // Write in the file or package names | |
211 | if (I->Op == Item::Install) | |
30e1eab5 AL |
212 | { |
213 | for (;I != J && Size < 1024; I++) | |
214 | { | |
cf544e14 AL |
215 | if (I->File[0] != '/') |
216 | return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str()); | |
03e39e59 | 217 | Args[n++] = I->File.c_str(); |
30e1eab5 AL |
218 | Size += strlen(Args[n-1]); |
219 | } | |
220 | } | |
03e39e59 | 221 | else |
30e1eab5 AL |
222 | { |
223 | for (;I != J && Size < 1024; I++) | |
224 | { | |
03e39e59 | 225 | Args[n++] = I->Pkg.Name(); |
30e1eab5 AL |
226 | Size += strlen(Args[n-1]); |
227 | } | |
228 | } | |
03e39e59 | 229 | Args[n] = 0; |
30e1eab5 AL |
230 | J = I; |
231 | ||
232 | if (_config->FindB("Debug::pkgDPkgPM",false) == true) | |
233 | { | |
234 | for (unsigned int k = 0; k != n; k++) | |
235 | clog << Args[k] << ' '; | |
236 | clog << endl; | |
237 | continue; | |
238 | } | |
03e39e59 | 239 | |
03e39e59 AL |
240 | cout << flush; |
241 | clog << flush; | |
242 | cerr << flush; | |
243 | ||
244 | /* Mask off sig int/quit. We do this because dpkg also does when | |
245 | it forks scripts. What happens is that when you hit ctrl-c it sends | |
246 | it to all processes in the group. Since dpkg ignores the signal | |
247 | it doesn't die but we do! So we must also ignore it */ | |
248 | signal(SIGQUIT,SIG_IGN); | |
249 | signal(SIGINT,SIG_IGN); | |
6dd55be7 | 250 | |
03e39e59 | 251 | // Fork dpkg |
54676e1a | 252 | pid_t Child = ExecFork(); |
6dd55be7 | 253 | |
03e39e59 AL |
254 | // This is the child |
255 | if (Child == 0) | |
256 | { | |
cf544e14 | 257 | if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0) |
0dbb95d8 | 258 | _exit(100); |
03e39e59 | 259 | |
03e39e59 AL |
260 | int Flags,dummy; |
261 | if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0) | |
0dbb95d8 | 262 | _exit(100); |
03e39e59 AL |
263 | |
264 | // Discard everything in stdin before forking dpkg | |
265 | if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0) | |
0dbb95d8 | 266 | _exit(100); |
03e39e59 AL |
267 | |
268 | while (read(STDIN_FILENO,&dummy,1) == 1); | |
269 | ||
270 | if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0) | |
0dbb95d8 | 271 | _exit(100); |
03e39e59 AL |
272 | |
273 | /* No Job Control Stop Env is a magic dpkg var that prevents it | |
274 | from using sigstop */ | |
275 | setenv("DPKG_NO_TSTP","yes",1); | |
d568ed2d | 276 | execvp(Args[0],(char **)Args); |
03e39e59 | 277 | cerr << "Could not exec dpkg!" << endl; |
0dbb95d8 | 278 | _exit(100); |
03e39e59 AL |
279 | } |
280 | ||
281 | // Wait for dpkg | |
282 | int Status = 0; | |
283 | while (waitpid(Child,&Status,0) != Child) | |
284 | { | |
285 | if (errno == EINTR) | |
286 | continue; | |
6dd55be7 | 287 | RunScripts("DPkg::Post-Invoke"); |
03e39e59 AL |
288 | return _error->Errno("waitpid","Couldn't wait for subprocess"); |
289 | } | |
03e39e59 AL |
290 | |
291 | // Restore sig int/quit | |
292 | signal(SIGQUIT,SIG_DFL); | |
293 | signal(SIGINT,SIG_DFL); | |
6dd55be7 AL |
294 | |
295 | // Check for an error code. | |
296 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
297 | { | |
298 | RunScripts("DPkg::Post-Invoke"); | |
f956efb4 AL |
299 | if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV) |
300 | return _error->Error("Sub-process recieved a segmentation fault."); | |
301 | ||
302 | if (WIFEXITED(Status) != 0) | |
303 | return _error->Error("Sub-process returned an error code (%u)",WEXITSTATUS(Status)); | |
304 | ||
305 | return _error->Error("Sub-process exited unexpectedly"); | |
6dd55be7 | 306 | } |
03e39e59 | 307 | } |
6dd55be7 AL |
308 | |
309 | if (RunScripts("DPkg::Post-Invoke") == false) | |
310 | return false; | |
03e39e59 AL |
311 | return true; |
312 | } | |
313 | /*}}}*/ | |
281daf46 AL |
314 | // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/ |
315 | // --------------------------------------------------------------------- | |
316 | /* */ | |
317 | void pkgDPkgPM::Reset() | |
318 | { | |
319 | List.erase(List.begin(),List.end()); | |
320 | } | |
321 | /*}}}*/ |