Commit | Line | Data |
---|---|---|
c0c0b100 | 1 | // -*- mode: cpp; mode: fold -*- |
03e39e59 | 2 | // Description /*{{{*/ |
c0c0b100 | 3 | // $Id: dpkgpm.cc,v 1.24 2002/04/02 06:42:39 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> | |
b2e465d6 AL |
17 | #include <apt-pkg/depcache.h> |
18 | #include <apt-pkg/strutl.h> | |
233b185f | 19 | |
03e39e59 AL |
20 | #include <unistd.h> |
21 | #include <stdlib.h> | |
22 | #include <fcntl.h> | |
23 | #include <sys/types.h> | |
24 | #include <sys/wait.h> | |
25 | #include <signal.h> | |
26 | #include <errno.h> | |
db0c350f | 27 | #include <stdio.h> |
233b185f | 28 | #include <iostream> |
b0ebdef5 | 29 | /*}}}*/ |
233b185f AL |
30 | |
31 | using namespace std; | |
03e39e59 AL |
32 | |
33 | // DPkgPM::pkgDPkgPM - Constructor /*{{{*/ | |
34 | // --------------------------------------------------------------------- | |
35 | /* */ | |
b2e465d6 | 36 | pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) : pkgPackageManager(Cache) |
03e39e59 AL |
37 | { |
38 | } | |
39 | /*}}}*/ | |
40 | // DPkgPM::pkgDPkgPM - Destructor /*{{{*/ | |
41 | // --------------------------------------------------------------------- | |
42 | /* */ | |
43 | pkgDPkgPM::~pkgDPkgPM() | |
44 | { | |
45 | } | |
46 | /*}}}*/ | |
47 | // DPkgPM::Install - Install a package /*{{{*/ | |
48 | // --------------------------------------------------------------------- | |
49 | /* Add an install operation to the sequence list */ | |
50 | bool pkgDPkgPM::Install(PkgIterator Pkg,string File) | |
51 | { | |
52 | if (File.empty() == true || Pkg.end() == true) | |
53 | return _error->Error("Internal Error, No file name for %s",Pkg.Name()); | |
54 | ||
55 | List.push_back(Item(Item::Install,Pkg,File)); | |
56 | return true; | |
57 | } | |
58 | /*}}}*/ | |
59 | // DPkgPM::Configure - Configure a package /*{{{*/ | |
60 | // --------------------------------------------------------------------- | |
61 | /* Add a configure operation to the sequence list */ | |
62 | bool pkgDPkgPM::Configure(PkgIterator Pkg) | |
63 | { | |
64 | if (Pkg.end() == true) | |
65 | return false; | |
66 | ||
67 | List.push_back(Item(Item::Configure,Pkg)); | |
68 | return true; | |
69 | } | |
70 | /*}}}*/ | |
71 | // DPkgPM::Remove - Remove a package /*{{{*/ | |
72 | // --------------------------------------------------------------------- | |
73 | /* Add a remove operation to the sequence list */ | |
fc4b5c9f | 74 | bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge) |
03e39e59 AL |
75 | { |
76 | if (Pkg.end() == true) | |
77 | return false; | |
78 | ||
fc4b5c9f AL |
79 | if (Purge == true) |
80 | List.push_back(Item(Item::Purge,Pkg)); | |
81 | else | |
82 | List.push_back(Item(Item::Remove,Pkg)); | |
6dd55be7 AL |
83 | return true; |
84 | } | |
85 | /*}}}*/ | |
86 | // DPkgPM::RunScripts - Run a set of scripts /*{{{*/ | |
87 | // --------------------------------------------------------------------- | |
88 | /* This looks for a list of script sto run from the configuration file, | |
89 | each one is run with system from a forked child. */ | |
90 | bool pkgDPkgPM::RunScripts(const char *Cnf) | |
91 | { | |
92 | Configuration::Item const *Opts = _config->Tree(Cnf); | |
93 | if (Opts == 0 || Opts->Child == 0) | |
94 | return true; | |
95 | Opts = Opts->Child; | |
96 | ||
97 | // Fork for running the system calls | |
54676e1a | 98 | pid_t Child = ExecFork(); |
6dd55be7 AL |
99 | |
100 | // This is the child | |
101 | if (Child == 0) | |
102 | { | |
6dd55be7 AL |
103 | if (chdir("/tmp/") != 0) |
104 | _exit(100); | |
105 | ||
6dd55be7 AL |
106 | unsigned int Count = 1; |
107 | for (; Opts != 0; Opts = Opts->Next, Count++) | |
108 | { | |
109 | if (Opts->Value.empty() == true) | |
110 | continue; | |
111 | ||
112 | if (system(Opts->Value.c_str()) != 0) | |
113 | _exit(100+Count); | |
114 | } | |
115 | _exit(0); | |
116 | } | |
117 | ||
118 | // Wait for the child | |
119 | int Status = 0; | |
120 | while (waitpid(Child,&Status,0) != Child) | |
121 | { | |
122 | if (errno == EINTR) | |
123 | continue; | |
124 | return _error->Errno("waitpid","Couldn't wait for subprocess"); | |
125 | } | |
126 | ||
127 | // Restore sig int/quit | |
128 | signal(SIGQUIT,SIG_DFL); | |
129 | signal(SIGINT,SIG_DFL); | |
ddc1d8d0 | 130 | |
6dd55be7 AL |
131 | // Check for an error code. |
132 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
133 | { | |
134 | unsigned int Count = WEXITSTATUS(Status); | |
135 | if (Count > 100) | |
136 | { | |
137 | Count -= 100; | |
138 | for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--); | |
cf544e14 | 139 | _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str()); |
6dd55be7 AL |
140 | } |
141 | ||
142 | return _error->Error("Sub-process returned an error code"); | |
143 | } | |
144 | ||
03e39e59 AL |
145 | return true; |
146 | } | |
db0c350f AL |
147 | |
148 | /*}}}*/ | |
b2e465d6 AL |
149 | // DPkgPM::SendV2Pkgs - Send version 2 package info /*{{{*/ |
150 | // --------------------------------------------------------------------- | |
151 | /* This is part of the helper script communication interface, it sends | |
152 | very complete information down to the other end of the pipe.*/ | |
153 | bool pkgDPkgPM::SendV2Pkgs(FILE *F) | |
154 | { | |
155 | fprintf(F,"VERSION 2\n"); | |
156 | ||
157 | /* Write out all of the configuration directives by walking the | |
158 | configuration tree */ | |
159 | const Configuration::Item *Top = _config->Tree(0); | |
160 | for (; Top != 0;) | |
161 | { | |
162 | if (Top->Value.empty() == false) | |
163 | { | |
164 | fprintf(F,"%s=%s\n", | |
165 | QuoteString(Top->FullTag(),"=\"\n").c_str(), | |
166 | QuoteString(Top->Value,"\n").c_str()); | |
167 | } | |
168 | ||
169 | if (Top->Child != 0) | |
170 | { | |
171 | Top = Top->Child; | |
172 | continue; | |
173 | } | |
174 | ||
175 | while (Top != 0 && Top->Next == 0) | |
176 | Top = Top->Parent; | |
177 | if (Top != 0) | |
178 | Top = Top->Next; | |
179 | } | |
180 | fprintf(F,"\n"); | |
181 | ||
182 | // Write out the package actions in order. | |
183 | for (vector<Item>::iterator I = List.begin(); I != List.end(); I++) | |
184 | { | |
185 | pkgDepCache::StateCache &S = Cache[I->Pkg]; | |
186 | ||
187 | fprintf(F,"%s ",I->Pkg.Name()); | |
188 | // Current version | |
189 | if (I->Pkg->CurrentVer == 0) | |
190 | fprintf(F,"- "); | |
191 | else | |
192 | fprintf(F,"%s ",I->Pkg.CurrentVer().VerStr()); | |
193 | ||
194 | // Show the compare operator | |
195 | // Target version | |
196 | if (S.InstallVer != 0) | |
197 | { | |
198 | int Comp = 2; | |
199 | if (I->Pkg->CurrentVer != 0) | |
200 | Comp = S.InstVerIter(Cache).CompareVer(I->Pkg.CurrentVer()); | |
201 | if (Comp < 0) | |
202 | fprintf(F,"> "); | |
203 | if (Comp == 0) | |
204 | fprintf(F,"= "); | |
205 | if (Comp > 0) | |
206 | fprintf(F,"< "); | |
207 | fprintf(F,"%s ",S.InstVerIter(Cache).VerStr()); | |
208 | } | |
209 | else | |
210 | fprintf(F,"> - "); | |
211 | ||
212 | // Show the filename/operation | |
213 | if (I->Op == Item::Install) | |
214 | { | |
215 | // No errors here.. | |
216 | if (I->File[0] != '/') | |
217 | fprintf(F,"**ERROR**\n"); | |
218 | else | |
219 | fprintf(F,"%s\n",I->File.c_str()); | |
220 | } | |
221 | if (I->Op == Item::Configure) | |
222 | fprintf(F,"**CONFIGURE**\n"); | |
223 | if (I->Op == Item::Remove || | |
224 | I->Op == Item::Purge) | |
225 | fprintf(F,"**REMOVE**\n"); | |
226 | ||
227 | if (ferror(F) != 0) | |
228 | return false; | |
229 | } | |
230 | return true; | |
231 | } | |
232 | /*}}}*/ | |
db0c350f AL |
233 | // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/ |
234 | // --------------------------------------------------------------------- | |
235 | /* This looks for a list of scripts to run from the configuration file | |
236 | each one is run and is fed on standard input a list of all .deb files | |
237 | that are due to be installed. */ | |
238 | bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf) | |
239 | { | |
240 | Configuration::Item const *Opts = _config->Tree(Cnf); | |
241 | if (Opts == 0 || Opts->Child == 0) | |
242 | return true; | |
243 | Opts = Opts->Child; | |
244 | ||
245 | unsigned int Count = 1; | |
246 | for (; Opts != 0; Opts = Opts->Next, Count++) | |
247 | { | |
248 | if (Opts->Value.empty() == true) | |
249 | continue; | |
b2e465d6 AL |
250 | |
251 | // Determine the protocol version | |
252 | string OptSec = Opts->Value; | |
253 | string::size_type Pos; | |
254 | if ((Pos = OptSec.find(' ')) == string::npos || Pos == 0) | |
255 | Pos = OptSec.length(); | |
b2e465d6 AL |
256 | OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos); |
257 | ||
258 | unsigned int Version = _config->FindI(OptSec+"::Version",1); | |
259 | ||
db0c350f AL |
260 | // Create the pipes |
261 | int Pipes[2]; | |
262 | if (pipe(Pipes) != 0) | |
263 | return _error->Errno("pipe","Failed to create IPC pipe to subprocess"); | |
264 | SetCloseExec(Pipes[0],true); | |
265 | SetCloseExec(Pipes[1],true); | |
266 | ||
267 | // Purified Fork for running the script | |
268 | pid_t Process = ExecFork(); | |
269 | if (Process == 0) | |
270 | { | |
271 | // Setup the FDs | |
272 | dup2(Pipes[0],STDIN_FILENO); | |
273 | SetCloseExec(STDOUT_FILENO,false); | |
274 | SetCloseExec(STDIN_FILENO,false); | |
275 | SetCloseExec(STDERR_FILENO,false); | |
90ecbd7d AL |
276 | |
277 | const char *Args[4]; | |
db0c350f | 278 | Args[0] = "/bin/sh"; |
90ecbd7d AL |
279 | Args[1] = "-c"; |
280 | Args[2] = Opts->Value.c_str(); | |
281 | Args[3] = 0; | |
db0c350f AL |
282 | execv(Args[0],(char **)Args); |
283 | _exit(100); | |
284 | } | |
285 | close(Pipes[0]); | |
b2e465d6 AL |
286 | FILE *F = fdopen(Pipes[1],"w"); |
287 | if (F == 0) | |
288 | return _error->Errno("fdopen","Faild to open new FD"); | |
289 | ||
db0c350f | 290 | // Feed it the filenames. |
b2e465d6 AL |
291 | bool Die = false; |
292 | if (Version <= 1) | |
db0c350f | 293 | { |
b2e465d6 | 294 | for (vector<Item>::iterator I = List.begin(); I != List.end(); I++) |
db0c350f | 295 | { |
b2e465d6 AL |
296 | // Only deal with packages to be installed from .deb |
297 | if (I->Op != Item::Install) | |
298 | continue; | |
299 | ||
300 | // No errors here.. | |
301 | if (I->File[0] != '/') | |
302 | continue; | |
303 | ||
304 | /* Feed the filename of each package that is pending install | |
305 | into the pipe. */ | |
306 | fprintf(F,"%s\n",I->File.c_str()); | |
307 | if (ferror(F) != 0) | |
308 | { | |
309 | Die = true; | |
310 | break; | |
311 | } | |
90ecbd7d | 312 | } |
db0c350f | 313 | } |
b2e465d6 AL |
314 | else |
315 | Die = !SendV2Pkgs(F); | |
316 | ||
317 | fclose(F); | |
db0c350f AL |
318 | |
319 | // Clean up the sub process | |
320 | if (ExecWait(Process,Opts->Value.c_str()) == false) | |
90ecbd7d | 321 | return _error->Error("Failure running script %s",Opts->Value.c_str()); |
db0c350f AL |
322 | } |
323 | ||
324 | return true; | |
325 | } | |
326 | ||
03e39e59 AL |
327 | /*}}}*/ |
328 | // DPkgPM::Go - Run the sequence /*{{{*/ | |
329 | // --------------------------------------------------------------------- | |
330 | /* This globs the operations and calls dpkg */ | |
331 | bool pkgDPkgPM::Go() | |
332 | { | |
6dd55be7 AL |
333 | if (RunScripts("DPkg::Pre-Invoke") == false) |
334 | return false; | |
db0c350f AL |
335 | |
336 | if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false) | |
337 | return false; | |
338 | ||
03e39e59 AL |
339 | for (vector<Item>::iterator I = List.begin(); I != List.end();) |
340 | { | |
341 | vector<Item>::iterator J = I; | |
342 | for (; J != List.end() && J->Op == I->Op; J++); | |
30e1eab5 | 343 | |
03e39e59 AL |
344 | // Generate the argument list |
345 | const char *Args[400]; | |
346 | if (J - I > 350) | |
347 | J = I + 350; | |
348 | ||
30e1eab5 AL |
349 | unsigned int n = 0; |
350 | unsigned long Size = 0; | |
43b3b626 | 351 | string Tmp = _config->Find("Dir::Bin::dpkg","dpkg"); |
50914ffa | 352 | Args[n++] = Tmp.c_str(); |
30e1eab5 | 353 | Size += strlen(Args[n-1]); |
03e39e59 | 354 | |
6dd55be7 AL |
355 | // Stick in any custom dpkg options |
356 | Configuration::Item const *Opts = _config->Tree("DPkg::Options"); | |
357 | if (Opts != 0) | |
358 | { | |
359 | Opts = Opts->Child; | |
360 | for (; Opts != 0; Opts = Opts->Next) | |
361 | { | |
362 | if (Opts->Value.empty() == true) | |
363 | continue; | |
364 | Args[n++] = Opts->Value.c_str(); | |
365 | Size += Opts->Value.length(); | |
366 | } | |
367 | } | |
368 | ||
03e39e59 AL |
369 | switch (I->Op) |
370 | { | |
371 | case Item::Remove: | |
372 | Args[n++] = "--force-depends"; | |
30e1eab5 | 373 | Size += strlen(Args[n-1]); |
03e39e59 | 374 | Args[n++] = "--force-remove-essential"; |
30e1eab5 | 375 | Size += strlen(Args[n-1]); |
03e39e59 | 376 | Args[n++] = "--remove"; |
30e1eab5 | 377 | Size += strlen(Args[n-1]); |
03e39e59 AL |
378 | break; |
379 | ||
fc4b5c9f AL |
380 | case Item::Purge: |
381 | Args[n++] = "--force-depends"; | |
382 | Size += strlen(Args[n-1]); | |
383 | Args[n++] = "--force-remove-essential"; | |
384 | Size += strlen(Args[n-1]); | |
385 | Args[n++] = "--purge"; | |
386 | Size += strlen(Args[n-1]); | |
387 | break; | |
388 | ||
03e39e59 AL |
389 | case Item::Configure: |
390 | Args[n++] = "--configure"; | |
30e1eab5 | 391 | Size += strlen(Args[n-1]); |
03e39e59 AL |
392 | break; |
393 | ||
394 | case Item::Install: | |
395 | Args[n++] = "--unpack"; | |
30e1eab5 | 396 | Size += strlen(Args[n-1]); |
03e39e59 AL |
397 | break; |
398 | } | |
399 | ||
400 | // Write in the file or package names | |
401 | if (I->Op == Item::Install) | |
30e1eab5 AL |
402 | { |
403 | for (;I != J && Size < 1024; I++) | |
404 | { | |
cf544e14 AL |
405 | if (I->File[0] != '/') |
406 | return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str()); | |
03e39e59 | 407 | Args[n++] = I->File.c_str(); |
30e1eab5 AL |
408 | Size += strlen(Args[n-1]); |
409 | } | |
410 | } | |
03e39e59 | 411 | else |
30e1eab5 AL |
412 | { |
413 | for (;I != J && Size < 1024; I++) | |
414 | { | |
03e39e59 | 415 | Args[n++] = I->Pkg.Name(); |
30e1eab5 AL |
416 | Size += strlen(Args[n-1]); |
417 | } | |
418 | } | |
03e39e59 | 419 | Args[n] = 0; |
30e1eab5 AL |
420 | J = I; |
421 | ||
422 | if (_config->FindB("Debug::pkgDPkgPM",false) == true) | |
423 | { | |
424 | for (unsigned int k = 0; k != n; k++) | |
425 | clog << Args[k] << ' '; | |
426 | clog << endl; | |
427 | continue; | |
428 | } | |
03e39e59 | 429 | |
03e39e59 AL |
430 | cout << flush; |
431 | clog << flush; | |
432 | cerr << flush; | |
433 | ||
434 | /* Mask off sig int/quit. We do this because dpkg also does when | |
435 | it forks scripts. What happens is that when you hit ctrl-c it sends | |
436 | it to all processes in the group. Since dpkg ignores the signal | |
437 | it doesn't die but we do! So we must also ignore it */ | |
438 | signal(SIGQUIT,SIG_IGN); | |
439 | signal(SIGINT,SIG_IGN); | |
6dd55be7 | 440 | |
03e39e59 | 441 | // Fork dpkg |
54676e1a | 442 | pid_t Child = ExecFork(); |
6dd55be7 | 443 | |
03e39e59 AL |
444 | // This is the child |
445 | if (Child == 0) | |
446 | { | |
cf544e14 | 447 | if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0) |
0dbb95d8 | 448 | _exit(100); |
03e39e59 | 449 | |
8b5fe26c AL |
450 | if (_config->FindB("DPkg::FlushSTDIN",true) == true) |
451 | { | |
452 | int Flags,dummy; | |
453 | if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0) | |
454 | _exit(100); | |
455 | ||
456 | // Discard everything in stdin before forking dpkg | |
457 | if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0) | |
458 | _exit(100); | |
459 | ||
460 | while (read(STDIN_FILENO,&dummy,1) == 1); | |
461 | ||
462 | if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0) | |
463 | _exit(100); | |
464 | } | |
03e39e59 | 465 | |
03e39e59 AL |
466 | /* No Job Control Stop Env is a magic dpkg var that prevents it |
467 | from using sigstop */ | |
101030ab | 468 | putenv("DPKG_NO_TSTP=yes"); |
d568ed2d | 469 | execvp(Args[0],(char **)Args); |
03e39e59 | 470 | cerr << "Could not exec dpkg!" << endl; |
0dbb95d8 | 471 | _exit(100); |
03e39e59 AL |
472 | } |
473 | ||
474 | // Wait for dpkg | |
475 | int Status = 0; | |
476 | while (waitpid(Child,&Status,0) != Child) | |
477 | { | |
478 | if (errno == EINTR) | |
479 | continue; | |
6dd55be7 | 480 | RunScripts("DPkg::Post-Invoke"); |
03e39e59 AL |
481 | return _error->Errno("waitpid","Couldn't wait for subprocess"); |
482 | } | |
03e39e59 AL |
483 | |
484 | // Restore sig int/quit | |
485 | signal(SIGQUIT,SIG_DFL); | |
486 | signal(SIGINT,SIG_DFL); | |
6dd55be7 AL |
487 | |
488 | // Check for an error code. | |
489 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
490 | { | |
491 | RunScripts("DPkg::Post-Invoke"); | |
f956efb4 | 492 | if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV) |
b2e465d6 AL |
493 | return _error->Error("Sub-process %s received a segmentation fault.",Args[0]); |
494 | ||
f956efb4 | 495 | if (WIFEXITED(Status) != 0) |
0410b3d5 | 496 | return _error->Error("Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status)); |
f956efb4 | 497 | |
0410b3d5 | 498 | return _error->Error("Sub-process %s exited unexpectedly",Args[0]); |
6dd55be7 | 499 | } |
03e39e59 | 500 | } |
6dd55be7 AL |
501 | |
502 | if (RunScripts("DPkg::Post-Invoke") == false) | |
503 | return false; | |
03e39e59 AL |
504 | return true; |
505 | } | |
506 | /*}}}*/ | |
281daf46 AL |
507 | // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/ |
508 | // --------------------------------------------------------------------- | |
509 | /* */ | |
510 | void pkgDPkgPM::Reset() | |
511 | { | |
512 | List.erase(List.begin(),List.end()); | |
513 | } | |
514 | /*}}}*/ |