]>
Commit | Line | Data |
---|---|---|
c0c0b100 | 1 | // -*- mode: cpp; mode: fold -*- |
03e39e59 | 2 | // Description /*{{{*/ |
7f9a6360 | 3 | // $Id: dpkgpm.cc,v 1.28 2004/01/27 02:25:01 mdz Exp $ |
03e39e59 AL |
4 | /* ###################################################################### |
5 | ||
6 | DPKG Package Manager - Provide an interface to dpkg | |
7 | ||
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
10 | // Includes /*{{{*/ | |
ea542140 DK |
11 | #include <config.h> |
12 | ||
03e39e59 AL |
13 | #include <apt-pkg/dpkgpm.h> |
14 | #include <apt-pkg/error.h> | |
15 | #include <apt-pkg/configuration.h> | |
b2e465d6 | 16 | #include <apt-pkg/depcache.h> |
5e457a93 | 17 | #include <apt-pkg/pkgrecords.h> |
b2e465d6 | 18 | #include <apt-pkg/strutl.h> |
614adaa0 | 19 | #include <apt-pkg/fileutl.h> |
388f2962 | 20 | #include <apt-pkg/cachefile.h> |
590f1923 | 21 | #include <apt-pkg/packagemanager.h> |
af36becc | 22 | #include <apt-pkg/install-progress.h> |
233b185f | 23 | |
03e39e59 AL |
24 | #include <unistd.h> |
25 | #include <stdlib.h> | |
26 | #include <fcntl.h> | |
090c6566 | 27 | #include <sys/select.h> |
96db74ce | 28 | #include <sys/stat.h> |
03e39e59 AL |
29 | #include <sys/types.h> |
30 | #include <sys/wait.h> | |
31 | #include <signal.h> | |
32 | #include <errno.h> | |
2f0d5dea | 33 | #include <string.h> |
db0c350f | 34 | #include <stdio.h> |
f7dec19f DB |
35 | #include <string.h> |
36 | #include <algorithm> | |
75ef8f14 MV |
37 | #include <sstream> |
38 | #include <map> | |
9c76a881 MV |
39 | #include <pwd.h> |
40 | #include <grp.h> | |
a38e023c | 41 | #include <iomanip> |
75ef8f14 | 42 | |
d8cb4aa4 MV |
43 | #include <termios.h> |
44 | #include <unistd.h> | |
45 | #include <sys/ioctl.h> | |
46 | #include <pty.h> | |
47 | ||
75ef8f14 | 48 | #include <apti18n.h> |
b0ebdef5 | 49 | /*}}}*/ |
233b185f AL |
50 | |
51 | using namespace std; | |
03e39e59 | 52 | |
697a1d8a MV |
53 | class pkgDPkgPMPrivate |
54 | { | |
55 | public: | |
dcaa1185 | 56 | pkgDPkgPMPrivate() : stdin_is_dev_null(false), dpkgbuf_pos(0), |
a38e023c | 57 | term_out(NULL), history_out(NULL), |
177296df | 58 | progress(NULL), master(-1), slave(-1) |
697a1d8a | 59 | { |
dcaa1185 | 60 | dpkgbuf[0] = '\0'; |
697a1d8a | 61 | } |
31f97d7b MV |
62 | ~pkgDPkgPMPrivate() |
63 | { | |
31f97d7b | 64 | } |
697a1d8a MV |
65 | bool stdin_is_dev_null; |
66 | // the buffer we use for the dpkg status-fd reading | |
67 | char dpkgbuf[1024]; | |
68 | int dpkgbuf_pos; | |
69 | FILE *term_out; | |
70 | FILE *history_out; | |
71 | string dpkg_error; | |
31f97d7b | 72 | APT::Progress::PackageManager *progress; |
c3045b79 MV |
73 | |
74 | // pty stuff | |
75 | struct termios tt; | |
76 | int master; | |
77 | int slave; | |
78 | ||
79 | // signals | |
80 | sigset_t sigmask; | |
81 | sigset_t original_sigmask; | |
82 | ||
697a1d8a MV |
83 | }; |
84 | ||
f7dec19f DB |
85 | namespace |
86 | { | |
87 | // Maps the dpkg "processing" info to human readable names. Entry 0 | |
88 | // of each array is the key, entry 1 is the value. | |
89 | const std::pair<const char *, const char *> PackageProcessingOps[] = { | |
90 | std::make_pair("install", N_("Installing %s")), | |
91 | std::make_pair("configure", N_("Configuring %s")), | |
92 | std::make_pair("remove", N_("Removing %s")), | |
ac81ae9c | 93 | std::make_pair("purge", N_("Completely removing %s")), |
b3514c56 | 94 | std::make_pair("disappear", N_("Noting disappearance of %s")), |
f7dec19f DB |
95 | std::make_pair("trigproc", N_("Running post-installation trigger %s")) |
96 | }; | |
97 | ||
98 | const std::pair<const char *, const char *> * const PackageProcessingOpsBegin = PackageProcessingOps; | |
99 | const std::pair<const char *, const char *> * const PackageProcessingOpsEnd = PackageProcessingOps + sizeof(PackageProcessingOps) / sizeof(PackageProcessingOps[0]); | |
100 | ||
101 | // Predicate to test whether an entry in the PackageProcessingOps | |
102 | // array matches a string. | |
103 | class MatchProcessingOp | |
104 | { | |
105 | const char *target; | |
106 | ||
107 | public: | |
108 | MatchProcessingOp(const char *the_target) | |
109 | : target(the_target) | |
110 | { | |
111 | } | |
112 | ||
113 | bool operator()(const std::pair<const char *, const char *> &pair) const | |
114 | { | |
115 | return strcmp(pair.first, target) == 0; | |
116 | } | |
117 | }; | |
118 | } | |
09fa2df2 | 119 | |
cebe0287 MV |
120 | /* helper function to ionice the given PID |
121 | ||
122 | there is no C header for ionice yet - just the syscall interface | |
123 | so we use the binary from util-linux | |
124 | */ | |
125 | static bool | |
126 | ionice(int PID) | |
127 | { | |
128 | if (!FileExists("/usr/bin/ionice")) | |
129 | return false; | |
86fc2ca8 | 130 | pid_t Process = ExecFork(); |
cebe0287 MV |
131 | if (Process == 0) |
132 | { | |
133 | char buf[32]; | |
134 | snprintf(buf, sizeof(buf), "-p%d", PID); | |
135 | const char *Args[4]; | |
136 | Args[0] = "/usr/bin/ionice"; | |
137 | Args[1] = "-c3"; | |
138 | Args[2] = buf; | |
139 | Args[3] = 0; | |
140 | execv(Args[0], (char **)Args); | |
141 | } | |
142 | return ExecWait(Process, "ionice"); | |
143 | } | |
144 | ||
6fad3c24 MV |
145 | static std::string getDpkgExecutable() |
146 | { | |
147 | string Tmp = _config->Find("Dir::Bin::dpkg","dpkg"); | |
148 | string const dpkgChrootDir = _config->FindDir("DPkg::Chroot-Directory", "/"); | |
149 | size_t dpkgChrootLen = dpkgChrootDir.length(); | |
150 | if (dpkgChrootDir != "/" && Tmp.find(dpkgChrootDir) == 0) | |
151 | { | |
152 | if (dpkgChrootDir[dpkgChrootLen - 1] == '/') | |
153 | --dpkgChrootLen; | |
154 | Tmp = Tmp.substr(dpkgChrootLen); | |
155 | } | |
156 | return Tmp; | |
157 | } | |
158 | ||
e6ee75af DK |
159 | // dpkgChrootDirectory - chrooting for dpkg if needed /*{{{*/ |
160 | static void dpkgChrootDirectory() | |
161 | { | |
162 | std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory"); | |
163 | if (chrootDir == "/") | |
164 | return; | |
165 | std::cerr << "Chrooting into " << chrootDir << std::endl; | |
166 | if (chroot(chrootDir.c_str()) != 0) | |
167 | _exit(100); | |
f52037d6 MV |
168 | if (chdir("/") != 0) |
169 | _exit(100); | |
e6ee75af DK |
170 | } |
171 | /*}}}*/ | |
172 | ||
a1355481 MV |
173 | |
174 | // FindNowVersion - Helper to find a Version in "now" state /*{{{*/ | |
175 | // --------------------------------------------------------------------- | |
176 | /* This is helpful when a package is no longer installed but has residual | |
177 | * config files | |
178 | */ | |
179 | static | |
180 | pkgCache::VerIterator FindNowVersion(const pkgCache::PkgIterator &Pkg) | |
181 | { | |
182 | pkgCache::VerIterator Ver; | |
69c2ecbd | 183 | for (Ver = Pkg.VersionList(); Ver.end() == false; ++Ver) |
a1355481 MV |
184 | { |
185 | pkgCache::VerFileIterator Vf = Ver.FileList(); | |
186 | pkgCache::PkgFileIterator F = Vf.File(); | |
69c2ecbd | 187 | for (F = Vf.File(); F.end() == false; ++F) |
a1355481 MV |
188 | { |
189 | if (F && F.Archive()) | |
190 | { | |
191 | if (strcmp(F.Archive(), "now")) | |
192 | return Ver; | |
193 | } | |
194 | } | |
195 | } | |
196 | return Ver; | |
197 | } | |
198 | /*}}}*/ | |
199 | ||
03e39e59 AL |
200 | // DPkgPM::pkgDPkgPM - Constructor /*{{{*/ |
201 | // --------------------------------------------------------------------- | |
202 | /* */ | |
09fa2df2 | 203 | pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) |
697a1d8a | 204 | : pkgPackageManager(Cache), PackagesDone(0), PackagesTotal(0) |
03e39e59 | 205 | { |
697a1d8a | 206 | d = new pkgDPkgPMPrivate(); |
03e39e59 AL |
207 | } |
208 | /*}}}*/ | |
209 | // DPkgPM::pkgDPkgPM - Destructor /*{{{*/ | |
210 | // --------------------------------------------------------------------- | |
211 | /* */ | |
212 | pkgDPkgPM::~pkgDPkgPM() | |
213 | { | |
697a1d8a | 214 | delete d; |
03e39e59 AL |
215 | } |
216 | /*}}}*/ | |
217 | // DPkgPM::Install - Install a package /*{{{*/ | |
218 | // --------------------------------------------------------------------- | |
219 | /* Add an install operation to the sequence list */ | |
220 | bool pkgDPkgPM::Install(PkgIterator Pkg,string File) | |
221 | { | |
222 | if (File.empty() == true || Pkg.end() == true) | |
92f21277 | 223 | return _error->Error("Internal Error, No file name for %s",Pkg.FullName().c_str()); |
03e39e59 | 224 | |
05bae55f DK |
225 | // If the filename string begins with DPkg::Chroot-Directory, return the |
226 | // substr that is within the chroot so dpkg can access it. | |
227 | string const chrootdir = _config->FindDir("DPkg::Chroot-Directory","/"); | |
228 | if (chrootdir != "/" && File.find(chrootdir) == 0) | |
229 | { | |
230 | size_t len = chrootdir.length(); | |
231 | if (chrootdir.at(len - 1) == '/') | |
232 | len--; | |
233 | List.push_back(Item(Item::Install,Pkg,File.substr(len))); | |
234 | } | |
235 | else | |
236 | List.push_back(Item(Item::Install,Pkg,File)); | |
237 | ||
03e39e59 AL |
238 | return true; |
239 | } | |
240 | /*}}}*/ | |
241 | // DPkgPM::Configure - Configure a package /*{{{*/ | |
242 | // --------------------------------------------------------------------- | |
243 | /* Add a configure operation to the sequence list */ | |
244 | bool pkgDPkgPM::Configure(PkgIterator Pkg) | |
245 | { | |
246 | if (Pkg.end() == true) | |
247 | return false; | |
3e9c4f70 | 248 | |
5e312de7 DK |
249 | List.push_back(Item(Item::Configure, Pkg)); |
250 | ||
251 | // Use triggers for config calls if we configure "smart" | |
252 | // as otherwise Pre-Depends will not be satisfied, see #526774 | |
253 | if (_config->FindB("DPkg::TriggersPending", false) == true) | |
254 | List.push_back(Item(Item::TriggersPending, PkgIterator())); | |
3e9c4f70 | 255 | |
03e39e59 AL |
256 | return true; |
257 | } | |
258 | /*}}}*/ | |
259 | // DPkgPM::Remove - Remove a package /*{{{*/ | |
260 | // --------------------------------------------------------------------- | |
261 | /* Add a remove operation to the sequence list */ | |
fc4b5c9f | 262 | bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge) |
03e39e59 AL |
263 | { |
264 | if (Pkg.end() == true) | |
265 | return false; | |
266 | ||
fc4b5c9f AL |
267 | if (Purge == true) |
268 | List.push_back(Item(Item::Purge,Pkg)); | |
269 | else | |
270 | List.push_back(Item(Item::Remove,Pkg)); | |
6dd55be7 AL |
271 | return true; |
272 | } | |
273 | /*}}}*/ | |
7a948ec7 | 274 | // DPkgPM::SendPkgInfo - Send info for install-pkgs hook /*{{{*/ |
b2e465d6 AL |
275 | // --------------------------------------------------------------------- |
276 | /* This is part of the helper script communication interface, it sends | |
277 | very complete information down to the other end of the pipe.*/ | |
278 | bool pkgDPkgPM::SendV2Pkgs(FILE *F) | |
279 | { | |
7a948ec7 DK |
280 | return SendPkgsInfo(F, 2); |
281 | } | |
282 | bool pkgDPkgPM::SendPkgsInfo(FILE * const F, unsigned int const &Version) | |
283 | { | |
284 | // This version of APT supports only v3, so don't sent higher versions | |
285 | if (Version <= 3) | |
286 | fprintf(F,"VERSION %u\n", Version); | |
287 | else | |
288 | fprintf(F,"VERSION 3\n"); | |
289 | ||
290 | /* Write out all of the configuration directives by walking the | |
b2e465d6 AL |
291 | configuration tree */ |
292 | const Configuration::Item *Top = _config->Tree(0); | |
293 | for (; Top != 0;) | |
294 | { | |
295 | if (Top->Value.empty() == false) | |
296 | { | |
297 | fprintf(F,"%s=%s\n", | |
298 | QuoteString(Top->FullTag(),"=\"\n").c_str(), | |
299 | QuoteString(Top->Value,"\n").c_str()); | |
300 | } | |
301 | ||
302 | if (Top->Child != 0) | |
303 | { | |
304 | Top = Top->Child; | |
305 | continue; | |
306 | } | |
307 | ||
308 | while (Top != 0 && Top->Next == 0) | |
309 | Top = Top->Parent; | |
310 | if (Top != 0) | |
311 | Top = Top->Next; | |
312 | } | |
313 | fprintf(F,"\n"); | |
314 | ||
315 | // Write out the package actions in order. | |
f7f0d6c7 | 316 | for (vector<Item>::iterator I = List.begin(); I != List.end(); ++I) |
b2e465d6 | 317 | { |
3e9c4f70 DK |
318 | if(I->Pkg.end() == true) |
319 | continue; | |
320 | ||
b2e465d6 AL |
321 | pkgDepCache::StateCache &S = Cache[I->Pkg]; |
322 | ||
323 | fprintf(F,"%s ",I->Pkg.Name()); | |
7a948ec7 DK |
324 | |
325 | // Current version which we are going to replace | |
326 | pkgCache::VerIterator CurVer = I->Pkg.CurrentVer(); | |
327 | if (CurVer.end() == true && (I->Op == Item::Remove || I->Op == Item::Purge)) | |
328 | CurVer = FindNowVersion(I->Pkg); | |
329 | ||
86fdeec2 | 330 | if (CurVer.end() == true) |
7a948ec7 DK |
331 | { |
332 | if (Version <= 2) | |
333 | fprintf(F, "- "); | |
334 | else | |
335 | fprintf(F, "- - none "); | |
336 | } | |
b2e465d6 | 337 | else |
7a948ec7 DK |
338 | { |
339 | fprintf(F, "%s ", CurVer.VerStr()); | |
340 | if (Version >= 3) | |
341 | fprintf(F, "%s %s ", CurVer.Arch(), CurVer.MultiArchType()); | |
342 | } | |
343 | ||
344 | // Show the compare operator between current and install version | |
b2e465d6 AL |
345 | if (S.InstallVer != 0) |
346 | { | |
7a948ec7 | 347 | pkgCache::VerIterator const InstVer = S.InstVerIter(Cache); |
b2e465d6 | 348 | int Comp = 2; |
7a948ec7 DK |
349 | if (CurVer.end() == false) |
350 | Comp = InstVer.CompareVer(CurVer); | |
b2e465d6 AL |
351 | if (Comp < 0) |
352 | fprintf(F,"> "); | |
7a948ec7 | 353 | else if (Comp == 0) |
b2e465d6 | 354 | fprintf(F,"= "); |
7a948ec7 | 355 | else if (Comp > 0) |
b2e465d6 | 356 | fprintf(F,"< "); |
7a948ec7 DK |
357 | fprintf(F, "%s ", InstVer.VerStr()); |
358 | if (Version >= 3) | |
359 | fprintf(F, "%s %s ", InstVer.Arch(), InstVer.MultiArchType()); | |
b2e465d6 AL |
360 | } |
361 | else | |
7a948ec7 DK |
362 | { |
363 | if (Version <= 2) | |
364 | fprintf(F, "> - "); | |
365 | else | |
366 | fprintf(F, "> - - none "); | |
367 | } | |
368 | ||
b2e465d6 AL |
369 | // Show the filename/operation |
370 | if (I->Op == Item::Install) | |
371 | { | |
372 | // No errors here.. | |
373 | if (I->File[0] != '/') | |
374 | fprintf(F,"**ERROR**\n"); | |
375 | else | |
376 | fprintf(F,"%s\n",I->File.c_str()); | |
377 | } | |
7a948ec7 | 378 | else if (I->Op == Item::Configure) |
b2e465d6 | 379 | fprintf(F,"**CONFIGURE**\n"); |
7a948ec7 | 380 | else if (I->Op == Item::Remove || |
b2e465d6 AL |
381 | I->Op == Item::Purge) |
382 | fprintf(F,"**REMOVE**\n"); | |
383 | ||
384 | if (ferror(F) != 0) | |
385 | return false; | |
386 | } | |
387 | return true; | |
388 | } | |
389 | /*}}}*/ | |
db0c350f AL |
390 | // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/ |
391 | // --------------------------------------------------------------------- | |
392 | /* This looks for a list of scripts to run from the configuration file | |
393 | each one is run and is fed on standard input a list of all .deb files | |
394 | that are due to be installed. */ | |
395 | bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf) | |
396 | { | |
397 | Configuration::Item const *Opts = _config->Tree(Cnf); | |
398 | if (Opts == 0 || Opts->Child == 0) | |
399 | return true; | |
400 | Opts = Opts->Child; | |
401 | ||
402 | unsigned int Count = 1; | |
403 | for (; Opts != 0; Opts = Opts->Next, Count++) | |
404 | { | |
405 | if (Opts->Value.empty() == true) | |
406 | continue; | |
b2e465d6 AL |
407 | |
408 | // Determine the protocol version | |
409 | string OptSec = Opts->Value; | |
410 | string::size_type Pos; | |
411 | if ((Pos = OptSec.find(' ')) == string::npos || Pos == 0) | |
412 | Pos = OptSec.length(); | |
b2e465d6 AL |
413 | OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos); |
414 | ||
415 | unsigned int Version = _config->FindI(OptSec+"::Version",1); | |
48498443 | 416 | unsigned int InfoFD = _config->FindI(OptSec + "::InfoFD", STDIN_FILENO); |
b2e465d6 | 417 | |
db0c350f AL |
418 | // Create the pipes |
419 | int Pipes[2]; | |
420 | if (pipe(Pipes) != 0) | |
421 | return _error->Errno("pipe","Failed to create IPC pipe to subprocess"); | |
48498443 DK |
422 | if (InfoFD != (unsigned)Pipes[0]) |
423 | SetCloseExec(Pipes[0],true); | |
424 | else | |
425 | _config->Set("APT::Keep-Fds::", Pipes[0]); | |
db0c350f | 426 | SetCloseExec(Pipes[1],true); |
48498443 | 427 | |
db0c350f | 428 | // Purified Fork for running the script |
48498443 | 429 | pid_t Process = ExecFork(); |
db0c350f AL |
430 | if (Process == 0) |
431 | { | |
432 | // Setup the FDs | |
48498443 | 433 | dup2(Pipes[0], InfoFD); |
db0c350f | 434 | SetCloseExec(STDOUT_FILENO,false); |
48498443 | 435 | SetCloseExec(STDIN_FILENO,false); |
db0c350f | 436 | SetCloseExec(STDERR_FILENO,false); |
90ecbd7d | 437 | |
48498443 DK |
438 | string hookfd; |
439 | strprintf(hookfd, "%d", InfoFD); | |
440 | setenv("APT_HOOK_INFO_FD", hookfd.c_str(), 1); | |
441 | ||
e6ee75af | 442 | dpkgChrootDirectory(); |
90ecbd7d | 443 | const char *Args[4]; |
db0c350f | 444 | Args[0] = "/bin/sh"; |
90ecbd7d AL |
445 | Args[1] = "-c"; |
446 | Args[2] = Opts->Value.c_str(); | |
447 | Args[3] = 0; | |
db0c350f AL |
448 | execv(Args[0],(char **)Args); |
449 | _exit(100); | |
450 | } | |
48498443 DK |
451 | if (InfoFD == (unsigned)Pipes[0]) |
452 | _config->Clear("APT::Keep-Fds", Pipes[0]); | |
db0c350f | 453 | close(Pipes[0]); |
b2e465d6 AL |
454 | FILE *F = fdopen(Pipes[1],"w"); |
455 | if (F == 0) | |
456 | return _error->Errno("fdopen","Faild to open new FD"); | |
457 | ||
db0c350f | 458 | // Feed it the filenames. |
b2e465d6 | 459 | if (Version <= 1) |
db0c350f | 460 | { |
f7f0d6c7 | 461 | for (vector<Item>::iterator I = List.begin(); I != List.end(); ++I) |
db0c350f | 462 | { |
b2e465d6 AL |
463 | // Only deal with packages to be installed from .deb |
464 | if (I->Op != Item::Install) | |
465 | continue; | |
466 | ||
467 | // No errors here.. | |
468 | if (I->File[0] != '/') | |
469 | continue; | |
470 | ||
471 | /* Feed the filename of each package that is pending install | |
472 | into the pipe. */ | |
473 | fprintf(F,"%s\n",I->File.c_str()); | |
474 | if (ferror(F) != 0) | |
b2e465d6 | 475 | break; |
90ecbd7d | 476 | } |
db0c350f | 477 | } |
b2e465d6 | 478 | else |
7a948ec7 | 479 | SendPkgsInfo(F, Version); |
b2e465d6 AL |
480 | |
481 | fclose(F); | |
db0c350f AL |
482 | |
483 | // Clean up the sub process | |
484 | if (ExecWait(Process,Opts->Value.c_str()) == false) | |
90ecbd7d | 485 | return _error->Error("Failure running script %s",Opts->Value.c_str()); |
db0c350f AL |
486 | } |
487 | ||
488 | return true; | |
489 | } | |
ceabc520 MV |
490 | /*}}}*/ |
491 | // DPkgPM::DoStdin - Read stdin and pass to slave pty /*{{{*/ | |
492 | // --------------------------------------------------------------------- | |
493 | /* | |
494 | */ | |
495 | void pkgDPkgPM::DoStdin(int master) | |
496 | { | |
aff87a76 MV |
497 | unsigned char input_buf[256] = {0,}; |
498 | ssize_t len = read(0, input_buf, sizeof(input_buf)); | |
9983591d | 499 | if (len) |
d68d65ad | 500 | FileFd::Write(master, input_buf, len); |
9983591d | 501 | else |
697a1d8a | 502 | d->stdin_is_dev_null = true; |
ceabc520 | 503 | } |
03e39e59 | 504 | /*}}}*/ |
ceabc520 MV |
505 | // DPkgPM::DoTerminalPty - Read the terminal pty and write log /*{{{*/ |
506 | // --------------------------------------------------------------------- | |
507 | /* | |
508 | * read the terminal pty and write log | |
509 | */ | |
1ba38171 | 510 | void pkgDPkgPM::DoTerminalPty(int master) |
ceabc520 | 511 | { |
aff87a76 | 512 | unsigned char term_buf[1024] = {0,0, }; |
ceabc520 | 513 | |
aff87a76 | 514 | ssize_t len=read(master, term_buf, sizeof(term_buf)); |
7052511e MV |
515 | if(len == -1 && errno == EIO) |
516 | { | |
517 | // this happens when the child is about to exit, we | |
518 | // give it time to actually exit, otherwise we run | |
b6ff6913 DK |
519 | // into a race so we sleep for half a second. |
520 | struct timespec sleepfor = { 0, 500000000 }; | |
521 | nanosleep(&sleepfor, NULL); | |
7052511e MV |
522 | return; |
523 | } | |
524 | if(len <= 0) | |
955a6ddb | 525 | return; |
d68d65ad | 526 | FileFd::Write(1, term_buf, len); |
697a1d8a MV |
527 | if(d->term_out) |
528 | fwrite(term_buf, len, sizeof(char), d->term_out); | |
ceabc520 | 529 | } |
03e39e59 | 530 | /*}}}*/ |
6191b008 MV |
531 | // DPkgPM::ProcessDpkgStatusBuf /*{{{*/ |
532 | // --------------------------------------------------------------------- | |
533 | /* | |
534 | */ | |
e6ad8031 | 535 | void pkgDPkgPM::ProcessDpkgStatusLine(char *line) |
6191b008 | 536 | { |
887f5036 | 537 | bool const Debug = _config->FindB("Debug::pkgDPkgProgressReporting",false); |
887f5036 | 538 | if (Debug == true) |
09fa2df2 MV |
539 | std::clog << "got from dpkg '" << line << "'" << std::endl; |
540 | ||
09fa2df2 | 541 | /* dpkg sends strings like this: |
cd4ee27d MV |
542 | 'status: <pkg>: <pkg qstate>' |
543 | 'status: <pkg>:<arch>: <pkg qstate>' | |
fc2d32c0 | 544 | |
34d6563e MV |
545 | 'processing: {install,configure,remove,purge,disappear,trigproc}: pkg' |
546 | 'processing: {install,configure,remove,purge,disappear,trigproc}: trigger' | |
09fa2df2 | 547 | */ |
34d6563e | 548 | |
cd4ee27d MV |
549 | // we need to split on ": " (note the appended space) as the ':' is |
550 | // part of the pkgname:arch information that dpkg sends | |
551 | // | |
552 | // A dpkg error message may contain additional ":" (like | |
553 | // "failed in buffer_write(fd) (10, ret=-1): backend dpkg-deb ..." | |
554 | // so we need to ensure to not split too much | |
7794a688 DK |
555 | std::vector<std::string> list = StringSplit(line, ": ", 4); |
556 | if(list.size() < 3) | |
09fa2df2 | 557 | { |
887f5036 | 558 | if (Debug == true) |
09fa2df2 MV |
559 | std::clog << "ignoring line: not enough ':'" << std::endl; |
560 | return; | |
561 | } | |
dd640f3c | 562 | |
34d6563e MV |
563 | // build the (prefix, pkgname, action) tuple, position of this |
564 | // is different for "processing" or "status" messages | |
565 | std::string prefix = APT::String::Strip(list[0]); | |
566 | std::string pkgname; | |
567 | std::string action; | |
568 | ostringstream status; | |
569 | ||
570 | // "processing" has the form "processing: action: pkg or trigger" | |
571 | // with action = ["install", "configure", "remove", "purge", "disappear", | |
572 | // "trigproc"] | |
573 | if (prefix == "processing") | |
574 | { | |
575 | pkgname = APT::String::Strip(list[2]); | |
576 | action = APT::String::Strip(list[1]); | |
34d6563e MV |
577 | } |
578 | // "status" has the form: "status: pkg: state" | |
579 | // with state in ["half-installed", "unpacked", "half-configured", | |
580 | // "installed", "config-files", "not-installed"] | |
581 | else if (prefix == "status") | |
cd4ee27d | 582 | { |
34d6563e MV |
583 | pkgname = APT::String::Strip(list[1]); |
584 | action = APT::String::Strip(list[2]); | |
585 | } else { | |
dd640f3c | 586 | if (Debug == true) |
34d6563e | 587 | std::clog << "unknown prefix '" << prefix << "'" << std::endl; |
dd640f3c MV |
588 | return; |
589 | } | |
590 | ||
34d6563e MV |
591 | |
592 | /* handle the special cases first: | |
593 | ||
594 | errors look like this: | |
595 | 'status: /var/cache/apt/archives/krecipes_0.8.1-0ubuntu1_i386.deb : error : trying to overwrite `/usr/share/doc/kde/HTML/en/krecipes/krectip.png', which is also in package krecipes-data | |
596 | and conffile-prompt like this | |
597 | 'status: conffile-prompt: conffile : 'current-conffile' 'new-conffile' useredited distedited | |
598 | */ | |
599 | if (prefix == "status") | |
dd640f3c | 600 | { |
34d6563e MV |
601 | if(action == "error") |
602 | { | |
603 | d->progress->Error(list[1], PackagesDone, PackagesTotal, | |
604 | list[3]); | |
605 | pkgFailures++; | |
606 | WriteApportReport(list[1].c_str(), list[3].c_str()); | |
607 | return; | |
608 | } | |
609 | else if(action == "conffile") | |
610 | { | |
611 | d->progress->ConffilePrompt(list[1], PackagesDone, PackagesTotal, | |
612 | list[3]); | |
dd640f3c | 613 | return; |
34d6563e | 614 | } |
cd4ee27d | 615 | } |
7c016f6e | 616 | |
34d6563e MV |
617 | // at this point we know that we should have a valid pkgname, so build all |
618 | // the info from it | |
619 | ||
620 | // dpkg does not send always send "pkgname:arch" so we add it here | |
621 | // if needed | |
622 | if (pkgname.find(":") == std::string::npos) | |
623 | { | |
624 | // find the package in the group that is in a touched by dpkg | |
625 | // if there are multiple dpkg will send us a full pkgname:arch | |
626 | pkgCache::GrpIterator Grp = Cache.FindGrp(pkgname); | |
627 | if (Grp.end() == false) | |
628 | { | |
629 | pkgCache::PkgIterator P = Grp.PackageList(); | |
630 | for (; P.end() != true; P = Grp.NextPkg(P)) | |
631 | { | |
632 | if(Cache[P].Mode != pkgDepCache::ModeKeep) | |
633 | { | |
634 | pkgname = P.FullName(); | |
635 | break; | |
636 | } | |
637 | } | |
638 | } | |
639 | } | |
640 | ||
cd4ee27d | 641 | const char* const pkg = pkgname.c_str(); |
fd6417a6 | 642 | std::string short_pkgname = StringSplit(pkgname, ":")[0]; |
34d6563e | 643 | std::string arch = ""; |
e8022b09 | 644 | if (pkgname.find(":") != string::npos) |
34d6563e MV |
645 | arch = StringSplit(pkgname, ":")[1]; |
646 | std::string i18n_pkgname = pkgname; | |
647 | if (arch.size() != 0) | |
648 | strprintf(i18n_pkgname, "%s (%s)", short_pkgname.c_str(), arch.c_str()); | |
09fa2df2 | 649 | |
fc2d32c0 MV |
650 | // 'processing' from dpkg looks like |
651 | // 'processing: action: pkg' | |
34d6563e | 652 | if(prefix == "processing") |
fc2d32c0 | 653 | { |
f7dec19f DB |
654 | const std::pair<const char *, const char *> * const iter = |
655 | std::find_if(PackageProcessingOpsBegin, | |
656 | PackageProcessingOpsEnd, | |
dd640f3c | 657 | MatchProcessingOp(action.c_str())); |
f7dec19f | 658 | if(iter == PackageProcessingOpsEnd) |
fc2d32c0 | 659 | { |
887f5036 DK |
660 | if (Debug == true) |
661 | std::clog << "ignoring unknown action: " << action << std::endl; | |
fc2d32c0 MV |
662 | return; |
663 | } | |
34d6563e MV |
664 | std::string msg; |
665 | strprintf(msg, _(iter->second), i18n_pkgname.c_str()); | |
666 | d->progress->StatusChanged(pkgname, PackagesDone, PackagesTotal, msg); | |
667 | ||
668 | // FIXME: this needs a muliarch testcase | |
669 | // FIXME2: is "pkgname" here reliable with dpkg only sending us | |
670 | // short pkgnames? | |
671 | if (action == "disappear") | |
dd640f3c | 672 | handleDisappearAction(pkgname); |
fc2d32c0 | 673 | return; |
34d6563e | 674 | } |
09fa2df2 | 675 | |
34d6563e | 676 | if (prefix == "status") |
09fa2df2 | 677 | { |
34d6563e MV |
678 | vector<struct DpkgState> const &states = PackageOps[pkg]; |
679 | const char *next_action = NULL; | |
680 | if(PackageOpsDone[pkg] < states.size()) | |
681 | next_action = states[PackageOpsDone[pkg]].state; | |
682 | // check if the package moved to the next dpkg state | |
683 | if(next_action && (action == next_action)) | |
684 | { | |
685 | // only read the translation if there is actually a next | |
686 | // action | |
687 | const char *translation = _(states[PackageOpsDone[pkg]].str); | |
688 | std::string msg; | |
d274520e MV |
689 | |
690 | // we moved from one dpkg state to a new one, report that | |
691 | PackageOpsDone[pkg]++; | |
692 | PackagesDone++; | |
693 | ||
34d6563e MV |
694 | strprintf(msg, translation, i18n_pkgname.c_str()); |
695 | d->progress->StatusChanged(pkgname, PackagesDone, PackagesTotal, msg); | |
696 | ||
34d6563e MV |
697 | } |
698 | if (Debug == true) | |
699 | std::clog << "(parsed from dpkg) pkg: " << short_pkgname | |
700 | << " action: " << action << endl; | |
dd640f3c | 701 | } |
6191b008 | 702 | } |
887f5036 | 703 | /*}}}*/ |
eb6f9bac DK |
704 | // DPkgPM::handleDisappearAction /*{{{*/ |
705 | void pkgDPkgPM::handleDisappearAction(string const &pkgname) | |
706 | { | |
eb6f9bac DK |
707 | pkgCache::PkgIterator Pkg = Cache.FindPkg(pkgname); |
708 | if (unlikely(Pkg.end() == true)) | |
709 | return; | |
1f467276 | 710 | |
b4017ba7 MV |
711 | // record the package name for display and stuff later |
712 | disappearedPkgs.insert(Pkg.FullName(true)); | |
713 | ||
eb6f9bac DK |
714 | // the disappeared package was auto-installed - nothing to do |
715 | if ((Cache[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) | |
716 | return; | |
75954ae2 | 717 | pkgCache::VerIterator PkgVer = Cache[Pkg].InstVerIter(Cache); |
eb6f9bac DK |
718 | if (unlikely(PkgVer.end() == true)) |
719 | return; | |
720 | /* search in the list of dependencies for (Pre)Depends, | |
721 | check if this dependency has a Replaces on our package | |
722 | and if so transfer the manual installed flag to it */ | |
723 | for (pkgCache::DepIterator Dep = PkgVer.DependsList(); Dep.end() != true; ++Dep) | |
724 | { | |
725 | if (Dep->Type != pkgCache::Dep::Depends && | |
726 | Dep->Type != pkgCache::Dep::PreDepends) | |
727 | continue; | |
728 | pkgCache::PkgIterator Tar = Dep.TargetPkg(); | |
729 | if (unlikely(Tar.end() == true)) | |
730 | continue; | |
731 | // the package is already marked as manual | |
732 | if ((Cache[Tar].Flags & pkgCache::Flag::Auto) != pkgCache::Flag::Auto) | |
733 | continue; | |
75954ae2 DK |
734 | pkgCache::VerIterator TarVer = Cache[Tar].InstVerIter(Cache); |
735 | if (TarVer.end() == true) | |
736 | continue; | |
eb6f9bac DK |
737 | for (pkgCache::DepIterator Rep = TarVer.DependsList(); Rep.end() != true; ++Rep) |
738 | { | |
739 | if (Rep->Type != pkgCache::Dep::Replaces) | |
740 | continue; | |
741 | if (Pkg != Rep.TargetPkg()) | |
742 | continue; | |
743 | // okay, they are strongly connected - transfer manual-bit | |
744 | if (Debug == true) | |
745 | std::clog << "transfer manual-bit from disappeared »" << pkgname << "« to »" << Tar.FullName() << "«" << std::endl; | |
746 | Cache[Tar].Flags &= ~Flag::Auto; | |
747 | break; | |
748 | } | |
749 | } | |
750 | } | |
751 | /*}}}*/ | |
887f5036 | 752 | // DPkgPM::DoDpkgStatusFd /*{{{*/ |
6191b008 MV |
753 | // --------------------------------------------------------------------- |
754 | /* | |
755 | */ | |
e6ad8031 | 756 | void pkgDPkgPM::DoDpkgStatusFd(int statusfd) |
6191b008 MV |
757 | { |
758 | char *p, *q; | |
759 | int len; | |
760 | ||
697a1d8a MV |
761 | len=read(statusfd, &d->dpkgbuf[d->dpkgbuf_pos], sizeof(d->dpkgbuf)-d->dpkgbuf_pos); |
762 | d->dpkgbuf_pos += len; | |
6191b008 MV |
763 | if(len <= 0) |
764 | return; | |
ceabc520 | 765 | |
6191b008 | 766 | // process line by line if we have a buffer |
697a1d8a MV |
767 | p = q = d->dpkgbuf; |
768 | while((q=(char*)memchr(p, '\n', d->dpkgbuf+d->dpkgbuf_pos-p)) != NULL) | |
6191b008 MV |
769 | { |
770 | *q = 0; | |
e6ad8031 | 771 | ProcessDpkgStatusLine(p); |
6191b008 MV |
772 | p=q+1; // continue with next line |
773 | } | |
774 | ||
775 | // now move the unprocessed bits (after the final \n that is now a 0x0) | |
697a1d8a MV |
776 | // to the start and update d->dpkgbuf_pos |
777 | p = (char*)memrchr(d->dpkgbuf, 0, d->dpkgbuf_pos); | |
6191b008 MV |
778 | if(p == NULL) |
779 | return; | |
780 | ||
781 | // we are interessted in the first char *after* 0x0 | |
782 | p++; | |
783 | ||
784 | // move the unprocessed tail to the start and update pos | |
697a1d8a MV |
785 | memmove(d->dpkgbuf, p, p-d->dpkgbuf); |
786 | d->dpkgbuf_pos = d->dpkgbuf+d->dpkgbuf_pos-p; | |
6191b008 MV |
787 | } |
788 | /*}}}*/ | |
d7a4ffd6 | 789 | // DPkgPM::WriteHistoryTag /*{{{*/ |
6cb1060b | 790 | void pkgDPkgPM::WriteHistoryTag(string const &tag, string value) |
d7a4ffd6 | 791 | { |
6cb1060b DK |
792 | size_t const length = value.length(); |
793 | if (length == 0) | |
794 | return; | |
795 | // poor mans rstrip(", ") | |
796 | if (value[length-2] == ',' && value[length-1] == ' ') | |
797 | value.erase(length - 2, 2); | |
697a1d8a | 798 | fprintf(d->history_out, "%s: %s\n", tag.c_str(), value.c_str()); |
d7a4ffd6 | 799 | } /*}}}*/ |
887f5036 | 800 | // DPkgPM::OpenLog /*{{{*/ |
2e1715ea MV |
801 | bool pkgDPkgPM::OpenLog() |
802 | { | |
569cc934 | 803 | string const logdir = _config->FindDir("Dir::Log"); |
7753e468 | 804 | if(CreateAPTDirectoryIfNeeded(logdir, logdir) == false) |
b29c3712 | 805 | // FIXME: use a better string after freeze |
2e1715ea | 806 | return _error->Error(_("Directory '%s' missing"), logdir.c_str()); |
9169c871 MV |
807 | |
808 | // get current time | |
809 | char timestr[200]; | |
569cc934 DK |
810 | time_t const t = time(NULL); |
811 | struct tm const * const tmp = localtime(&t); | |
9169c871 MV |
812 | strftime(timestr, sizeof(timestr), "%F %T", tmp); |
813 | ||
814 | // open terminal log | |
569cc934 | 815 | string const logfile_name = flCombine(logdir, |
2e1715ea MV |
816 | _config->Find("Dir::Log::Terminal")); |
817 | if (!logfile_name.empty()) | |
818 | { | |
697a1d8a MV |
819 | d->term_out = fopen(logfile_name.c_str(),"a"); |
820 | if (d->term_out == NULL) | |
569cc934 | 821 | return _error->WarningE("OpenLog", _("Could not open file '%s'"), logfile_name.c_str()); |
697a1d8a MV |
822 | setvbuf(d->term_out, NULL, _IONBF, 0); |
823 | SetCloseExec(fileno(d->term_out), true); | |
11b126f9 DK |
824 | if (getuid() == 0) // if we aren't root, we can't chown a file, so don't try it |
825 | { | |
826 | struct passwd *pw = getpwnam("root"); | |
827 | struct group *gr = getgrnam("adm"); | |
828 | if (pw != NULL && gr != NULL && chown(logfile_name.c_str(), pw->pw_uid, gr->gr_gid) != 0) | |
829 | _error->WarningE("OpenLog", "chown to root:adm of file %s failed", logfile_name.c_str()); | |
830 | } | |
831 | if (chmod(logfile_name.c_str(), 0640) != 0) | |
832 | _error->WarningE("OpenLog", "chmod 0640 of file %s failed", logfile_name.c_str()); | |
697a1d8a | 833 | fprintf(d->term_out, "\nLog started: %s\n", timestr); |
2e1715ea | 834 | } |
9169c871 | 835 | |
569cc934 DK |
836 | // write your history |
837 | string const history_name = flCombine(logdir, | |
9169c871 MV |
838 | _config->Find("Dir::Log::History")); |
839 | if (!history_name.empty()) | |
840 | { | |
697a1d8a MV |
841 | d->history_out = fopen(history_name.c_str(),"a"); |
842 | if (d->history_out == NULL) | |
569cc934 | 843 | return _error->WarningE("OpenLog", _("Could not open file '%s'"), history_name.c_str()); |
d4621f82 | 844 | SetCloseExec(fileno(d->history_out), true); |
9169c871 | 845 | chmod(history_name.c_str(), 0644); |
697a1d8a | 846 | fprintf(d->history_out, "\nStart-Date: %s\n", timestr); |
97be52d4 | 847 | string remove, purge, install, reinstall, upgrade, downgrade; |
f7f0d6c7 | 848 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I) |
9169c871 | 849 | { |
97be52d4 DK |
850 | enum { CANDIDATE, CANDIDATE_AUTO, CURRENT_CANDIDATE, CURRENT } infostring; |
851 | string *line = NULL; | |
852 | #define HISTORYINFO(X, Y) { line = &X; infostring = Y; } | |
853 | if (Cache[I].NewInstall() == true) | |
854 | HISTORYINFO(install, CANDIDATE_AUTO) | |
855 | else if (Cache[I].ReInstall() == true) | |
856 | HISTORYINFO(reinstall, CANDIDATE) | |
857 | else if (Cache[I].Upgrade() == true) | |
858 | HISTORYINFO(upgrade, CURRENT_CANDIDATE) | |
859 | else if (Cache[I].Downgrade() == true) | |
860 | HISTORYINFO(downgrade, CURRENT_CANDIDATE) | |
861 | else if (Cache[I].Delete() == true) | |
862 | HISTORYINFO((Cache[I].Purge() ? purge : remove), CURRENT) | |
863 | else | |
864 | continue; | |
865 | #undef HISTORYINFO | |
866 | line->append(I.FullName(false)).append(" ("); | |
867 | switch (infostring) { | |
868 | case CANDIDATE: line->append(Cache[I].CandVersion); break; | |
869 | case CANDIDATE_AUTO: | |
870 | line->append(Cache[I].CandVersion); | |
871 | if ((Cache[I].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) | |
872 | line->append(", automatic"); | |
873 | break; | |
874 | case CURRENT_CANDIDATE: line->append(Cache[I].CurVersion).append(", ").append(Cache[I].CandVersion); break; | |
875 | case CURRENT: line->append(Cache[I].CurVersion); break; | |
9169c871 | 876 | } |
97be52d4 | 877 | line->append("), "); |
9169c871 | 878 | } |
2bb25574 DK |
879 | if (_config->Exists("Commandline::AsString") == true) |
880 | WriteHistoryTag("Commandline", _config->Find("Commandline::AsString")); | |
d7a4ffd6 | 881 | WriteHistoryTag("Install", install); |
97be52d4 | 882 | WriteHistoryTag("Reinstall", reinstall); |
d7a4ffd6 MV |
883 | WriteHistoryTag("Upgrade", upgrade); |
884 | WriteHistoryTag("Downgrade",downgrade); | |
885 | WriteHistoryTag("Remove",remove); | |
886 | WriteHistoryTag("Purge",purge); | |
697a1d8a | 887 | fflush(d->history_out); |
9169c871 MV |
888 | } |
889 | ||
2e1715ea MV |
890 | return true; |
891 | } | |
887f5036 DK |
892 | /*}}}*/ |
893 | // DPkg::CloseLog /*{{{*/ | |
2e1715ea MV |
894 | bool pkgDPkgPM::CloseLog() |
895 | { | |
9169c871 MV |
896 | char timestr[200]; |
897 | time_t t = time(NULL); | |
898 | struct tm *tmp = localtime(&t); | |
899 | strftime(timestr, sizeof(timestr), "%F %T", tmp); | |
900 | ||
697a1d8a | 901 | if(d->term_out) |
2e1715ea | 902 | { |
697a1d8a MV |
903 | fprintf(d->term_out, "Log ended: "); |
904 | fprintf(d->term_out, "%s", timestr); | |
905 | fprintf(d->term_out, "\n"); | |
906 | fclose(d->term_out); | |
2e1715ea | 907 | } |
697a1d8a | 908 | d->term_out = NULL; |
9169c871 | 909 | |
697a1d8a | 910 | if(d->history_out) |
9169c871 | 911 | { |
6cb1060b DK |
912 | if (disappearedPkgs.empty() == false) |
913 | { | |
914 | string disappear; | |
915 | for (std::set<std::string>::const_iterator d = disappearedPkgs.begin(); | |
916 | d != disappearedPkgs.end(); ++d) | |
917 | { | |
918 | pkgCache::PkgIterator P = Cache.FindPkg(*d); | |
919 | disappear.append(*d); | |
920 | if (P.end() == true) | |
921 | disappear.append(", "); | |
922 | else | |
923 | disappear.append(" (").append(Cache[P].CurVersion).append("), "); | |
924 | } | |
925 | WriteHistoryTag("Disappeared", disappear); | |
926 | } | |
697a1d8a MV |
927 | if (d->dpkg_error.empty() == false) |
928 | fprintf(d->history_out, "Error: %s\n", d->dpkg_error.c_str()); | |
929 | fprintf(d->history_out, "End-Date: %s\n", timestr); | |
930 | fclose(d->history_out); | |
9169c871 | 931 | } |
697a1d8a | 932 | d->history_out = NULL; |
9169c871 | 933 | |
2e1715ea MV |
934 | return true; |
935 | } | |
887f5036 | 936 | /*}}}*/ |
34d6563e MV |
937 | /*}}}*/ |
938 | /*{{{*/ | |
919e5852 OS |
939 | // This implements a racy version of pselect for those architectures |
940 | // that don't have a working implementation. | |
941 | // FIXME: Probably can be removed on Lenny+1 | |
942 | static int racy_pselect(int nfds, fd_set *readfds, fd_set *writefds, | |
943 | fd_set *exceptfds, const struct timespec *timeout, | |
944 | const sigset_t *sigmask) | |
945 | { | |
946 | sigset_t origmask; | |
947 | struct timeval tv; | |
948 | int retval; | |
949 | ||
f6b37f38 OS |
950 | tv.tv_sec = timeout->tv_sec; |
951 | tv.tv_usec = timeout->tv_nsec/1000; | |
919e5852 | 952 | |
f6b37f38 | 953 | sigprocmask(SIG_SETMASK, sigmask, &origmask); |
919e5852 OS |
954 | retval = select(nfds, readfds, writefds, exceptfds, &tv); |
955 | sigprocmask(SIG_SETMASK, &origmask, 0); | |
956 | return retval; | |
957 | } | |
34d6563e | 958 | /*}}}*/ |
6fad3c24 MV |
959 | |
960 | // DPkgPM::BuildPackagesProgressMap /*{{{*/ | |
961 | void pkgDPkgPM::BuildPackagesProgressMap() | |
962 | { | |
963 | // map the dpkg states to the operations that are performed | |
964 | // (this is sorted in the same way as Item::Ops) | |
965 | static const struct DpkgState DpkgStatesOpMap[][7] = { | |
966 | // Install operation | |
967 | { | |
968 | {"half-installed", N_("Preparing %s")}, | |
969 | {"unpacked", N_("Unpacking %s") }, | |
970 | {NULL, NULL} | |
971 | }, | |
972 | // Configure operation | |
973 | { | |
974 | {"unpacked",N_("Preparing to configure %s") }, | |
975 | {"half-configured", N_("Configuring %s") }, | |
976 | { "installed", N_("Installed %s")}, | |
977 | {NULL, NULL} | |
978 | }, | |
979 | // Remove operation | |
980 | { | |
981 | {"half-configured", N_("Preparing for removal of %s")}, | |
982 | {"half-installed", N_("Removing %s")}, | |
983 | {"config-files", N_("Removed %s")}, | |
984 | {NULL, NULL} | |
985 | }, | |
986 | // Purge operation | |
987 | { | |
988 | {"config-files", N_("Preparing to completely remove %s")}, | |
989 | {"not-installed", N_("Completely removed %s")}, | |
990 | {NULL, NULL} | |
991 | }, | |
992 | }; | |
993 | ||
994 | // init the PackageOps map, go over the list of packages that | |
995 | // that will be [installed|configured|removed|purged] and add | |
996 | // them to the PackageOps map (the dpkg states it goes through) | |
997 | // and the PackageOpsTranslations (human readable strings) | |
998 | for (vector<Item>::const_iterator I = List.begin(); I != List.end(); ++I) | |
999 | { | |
1000 | if((*I).Pkg.end() == true) | |
1001 | continue; | |
1002 | ||
1003 | string const name = (*I).Pkg.FullName(); | |
1004 | PackageOpsDone[name] = 0; | |
1005 | for(int i=0; (DpkgStatesOpMap[(*I).Op][i]).state != NULL; ++i) | |
1006 | { | |
1007 | PackageOps[name].push_back(DpkgStatesOpMap[(*I).Op][i]); | |
1008 | PackagesTotal++; | |
1009 | } | |
1010 | } | |
1011 | } | |
1012 | /*}}}*/ | |
bd5f39b3 MV |
1013 | #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR < 13) |
1014 | bool pkgDPkgPM::Go(int StatusFd) | |
1015 | { | |
1016 | APT::Progress::PackageManager *progress = NULL; | |
1017 | if (StatusFd == -1) | |
1018 | progress = APT::Progress::PackageManagerProgressFactory(); | |
1019 | else | |
1020 | progress = new APT::Progress::PackageManagerProgressFd(StatusFd); | |
1021 | ||
1022 | return GoNoABIBreak(progress); | |
1023 | } | |
1024 | #endif | |
1025 | ||
c3045b79 MV |
1026 | void pkgDPkgPM::StartPtyMagic() |
1027 | { | |
1028 | // setup the pty and stuff | |
1029 | struct winsize win; | |
1030 | ||
1031 | // if tcgetattr does not return zero there was a error | |
1032 | // and we do not do any pty magic | |
1033 | _error->PushToStack(); | |
1034 | if (tcgetattr(STDOUT_FILENO, &d->tt) == 0) | |
1035 | { | |
1036 | ioctl(1, TIOCGWINSZ, (char *)&win); | |
c3045b79 MV |
1037 | if (openpty(&d->master, &d->slave, NULL, &d->tt, &win) < 0) |
1038 | { | |
1039 | _error->Errno("openpty", _("Can not write log (%s)"), _("Is /dev/pts mounted?")); | |
1040 | d->master = d->slave = -1; | |
1041 | } else { | |
1042 | struct termios rtt; | |
1043 | rtt = d->tt; | |
1044 | cfmakeraw(&rtt); | |
1045 | rtt.c_lflag &= ~ECHO; | |
1046 | rtt.c_lflag |= ISIG; | |
1047 | // block SIGTTOU during tcsetattr to prevent a hang if | |
1048 | // the process is a member of the background process group | |
1049 | // http://www.opengroup.org/onlinepubs/000095399/functions/tcsetattr.html | |
1050 | sigemptyset(&d->sigmask); | |
1051 | sigaddset(&d->sigmask, SIGTTOU); | |
1052 | sigprocmask(SIG_BLOCK,&d->sigmask, &d->original_sigmask); | |
1053 | tcsetattr(0, TCSAFLUSH, &rtt); | |
1054 | sigprocmask(SIG_SETMASK, &d->original_sigmask, 0); | |
1055 | } | |
1056 | } | |
1057 | // complain only if stdout is either a terminal (but still failed) or is an invalid | |
1058 | // descriptor otherwise we would complain about redirection to e.g. /dev/null as well. | |
1059 | else if (isatty(STDOUT_FILENO) == 1 || errno == EBADF) | |
1060 | _error->Errno("tcgetattr", _("Can not write log (%s)"), _("Is stdout a terminal?")); | |
1061 | ||
1062 | if (_error->PendingError() == true) | |
1063 | _error->DumpErrors(std::cerr); | |
1064 | _error->RevertToStack(); | |
c3045b79 MV |
1065 | } |
1066 | ||
1067 | void pkgDPkgPM::StopPtyMagic() | |
1068 | { | |
1069 | if(d->slave > 0) | |
1070 | close(d->slave); | |
1071 | if(d->master >= 0) | |
1072 | { | |
1073 | tcsetattr(0, TCSAFLUSH, &d->tt); | |
1074 | close(d->master); | |
1075 | } | |
1076 | } | |
c420fe00 | 1077 | |
03e39e59 AL |
1078 | // DPkgPM::Go - Run the sequence /*{{{*/ |
1079 | // --------------------------------------------------------------------- | |
75ef8f14 | 1080 | /* This globs the operations and calls dpkg |
34d6563e | 1081 | * |
e6ad8031 MV |
1082 | * If it is called with a progress object apt will report the install |
1083 | * progress to this object. It maps the dpkg states a package goes | |
1084 | * through to human readable (and i10n-able) | |
75ef8f14 | 1085 | * names and calculates a percentage for each step. |
34d6563e | 1086 | */ |
bd5f39b3 | 1087 | #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13) |
e6ad8031 | 1088 | bool pkgDPkgPM::Go(APT::Progress::PackageManager *progress) |
bd5f39b3 MV |
1089 | #else |
1090 | bool pkgDPkgPM::GoNoABIBreak(APT::Progress::PackageManager *progress) | |
1091 | #endif | |
03e39e59 | 1092 | { |
b1803e01 | 1093 | pkgPackageManager::SigINTStop = false; |
e6ad8031 | 1094 | d->progress = progress; |
b1803e01 | 1095 | |
86fc2ca8 | 1096 | // Generate the base argument list for dpkg |
86fc2ca8 | 1097 | unsigned long StartSize = 0; |
6fad3c24 MV |
1098 | std::vector<const char *> Args; |
1099 | std::string DpkgExecutable = getDpkgExecutable(); | |
1100 | Args.push_back(DpkgExecutable.c_str()); | |
1101 | StartSize += DpkgExecutable.length(); | |
86fc2ca8 DK |
1102 | |
1103 | // Stick in any custom dpkg options | |
1104 | Configuration::Item const *Opts = _config->Tree("DPkg::Options"); | |
1105 | if (Opts != 0) | |
1106 | { | |
1107 | Opts = Opts->Child; | |
1108 | for (; Opts != 0; Opts = Opts->Next) | |
1109 | { | |
1110 | if (Opts->Value.empty() == true) | |
1111 | continue; | |
1112 | Args.push_back(Opts->Value.c_str()); | |
1113 | StartSize += Opts->Value.length(); | |
1114 | } | |
1115 | } | |
1116 | ||
1117 | size_t const BaseArgs = Args.size(); | |
1118 | // we need to detect if we can qualify packages with the architecture or not | |
1119 | Args.push_back("--assert-multi-arch"); | |
1120 | Args.push_back(NULL); | |
1121 | ||
1122 | pid_t dpkgAssertMultiArch = ExecFork(); | |
1123 | if (dpkgAssertMultiArch == 0) | |
1124 | { | |
e6ee75af | 1125 | dpkgChrootDirectory(); |
67b5d3dc DK |
1126 | // redirect everything to the ultimate sink as we only need the exit-status |
1127 | int const nullfd = open("/dev/null", O_RDONLY); | |
1128 | dup2(nullfd, STDIN_FILENO); | |
1129 | dup2(nullfd, STDOUT_FILENO); | |
1130 | dup2(nullfd, STDERR_FILENO); | |
17019a09 | 1131 | execvp(Args[0], (char**) &Args[0]); |
86fc2ca8 DK |
1132 | _error->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!"); |
1133 | _exit(2); | |
1134 | } | |
1135 | ||
17745b02 MV |
1136 | fd_set rfds; |
1137 | struct timespec tv; | |
17745b02 | 1138 | |
887f5036 DK |
1139 | unsigned int const MaxArgs = _config->FindI("Dpkg::MaxArgs",8*1024); |
1140 | unsigned int const MaxArgBytes = _config->FindI("Dpkg::MaxArgBytes",32*1024); | |
5e312de7 | 1141 | bool const NoTriggers = _config->FindB("DPkg::NoTriggers", false); |
aff4e2f1 | 1142 | |
6dd55be7 AL |
1143 | if (RunScripts("DPkg::Pre-Invoke") == false) |
1144 | return false; | |
db0c350f AL |
1145 | |
1146 | if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false) | |
1147 | return false; | |
fc2d32c0 | 1148 | |
3e9c4f70 DK |
1149 | // support subpressing of triggers processing for special |
1150 | // cases like d-i that runs the triggers handling manually | |
5e312de7 | 1151 | bool const SmartConf = (_config->Find("PackageManager::Configure", "all") != "all"); |
5c23dbcc | 1152 | bool const TriggersPending = _config->FindB("DPkg::TriggersPending", false); |
5e312de7 DK |
1153 | if (_config->FindB("DPkg::ConfigurePending", SmartConf) == true) |
1154 | List.push_back(Item(Item::ConfigurePending, PkgIterator())); | |
3e9c4f70 | 1155 | |
6fad3c24 MV |
1156 | // for the progress |
1157 | BuildPackagesProgressMap(); | |
75ef8f14 | 1158 | |
697a1d8a | 1159 | d->stdin_is_dev_null = false; |
9983591d | 1160 | |
ff56e980 | 1161 | // create log |
2e1715ea | 1162 | OpenLog(); |
ff56e980 | 1163 | |
86fc2ca8 DK |
1164 | bool dpkgMultiArch = false; |
1165 | if (dpkgAssertMultiArch > 0) | |
11bcbdb9 | 1166 | { |
86fc2ca8 DK |
1167 | int Status = 0; |
1168 | while (waitpid(dpkgAssertMultiArch, &Status, 0) != dpkgAssertMultiArch) | |
11bcbdb9 | 1169 | { |
86fc2ca8 | 1170 | if (errno == EINTR) |
11bcbdb9 | 1171 | continue; |
86fc2ca8 DK |
1172 | _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --assert-multi-arch"); |
1173 | break; | |
11bcbdb9 | 1174 | } |
86fc2ca8 DK |
1175 | if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0) |
1176 | dpkgMultiArch = true; | |
11bcbdb9 | 1177 | } |
11bcbdb9 | 1178 | |
c3045b79 MV |
1179 | // start pty magic before the loop |
1180 | StartPtyMagic(); | |
1181 | ||
177296df MV |
1182 | // Tell the progress that its starting and fork dpkg |
1183 | d->progress->Start(); | |
1184 | ||
c3045b79 | 1185 | // this loop is runs once per dpkg operation |
a18456a5 MV |
1186 | vector<Item>::const_iterator I = List.begin(); |
1187 | while (I != List.end()) | |
03e39e59 | 1188 | { |
5c23dbcc | 1189 | // Do all actions with the same Op in one run |
887f5036 | 1190 | vector<Item>::const_iterator J = I; |
5c23dbcc | 1191 | if (TriggersPending == true) |
f7f0d6c7 | 1192 | for (; J != List.end(); ++J) |
5c23dbcc DK |
1193 | { |
1194 | if (J->Op == I->Op) | |
1195 | continue; | |
1196 | if (J->Op != Item::TriggersPending) | |
1197 | break; | |
1198 | vector<Item>::const_iterator T = J + 1; | |
1199 | if (T != List.end() && T->Op == I->Op) | |
1200 | continue; | |
1201 | break; | |
1202 | } | |
1203 | else | |
f7f0d6c7 | 1204 | for (; J != List.end() && J->Op == I->Op; ++J) |
5c23dbcc | 1205 | /* nothing */; |
30e1eab5 | 1206 | |
8e11253d | 1207 | // keep track of allocated strings for multiarch package names |
edca7af0 | 1208 | std::vector<char *> Packages; |
8e11253d | 1209 | |
11bcbdb9 DK |
1210 | // start with the baseset of arguments |
1211 | unsigned long Size = StartSize; | |
1212 | Args.erase(Args.begin() + BaseArgs, Args.end()); | |
1213 | ||
599d6ad5 MV |
1214 | // Now check if we are within the MaxArgs limit |
1215 | // | |
1216 | // this code below is problematic, because it may happen that | |
1217 | // the argument list is split in a way that A depends on B | |
1218 | // and they are in the same "--configure A B" run | |
1219 | // - with the split they may now be configured in different | |
1cecd437 | 1220 | // runs, using Immediate-Configure-All can help prevent this. |
aff4e2f1 | 1221 | if (J - I > (signed)MaxArgs) |
edca7af0 | 1222 | { |
aff4e2f1 | 1223 | J = I + MaxArgs; |
86fc2ca8 DK |
1224 | unsigned long const size = MaxArgs + 10; |
1225 | Args.reserve(size); | |
1226 | Packages.reserve(size); | |
edca7af0 DK |
1227 | } |
1228 | else | |
1229 | { | |
86fc2ca8 DK |
1230 | unsigned long const size = (J - I) + 10; |
1231 | Args.reserve(size); | |
1232 | Packages.reserve(size); | |
edca7af0 DK |
1233 | } |
1234 | ||
75ef8f14 | 1235 | int fd[2]; |
319790f4 DK |
1236 | if (pipe(fd) != 0) |
1237 | return _error->Errno("pipe","Failed to create IPC pipe to dpkg"); | |
edca7af0 DK |
1238 | |
1239 | #define ADDARG(X) Args.push_back(X); Size += strlen(X) | |
1240 | #define ADDARGC(X) Args.push_back(X); Size += sizeof(X) - 1 | |
1241 | ||
1242 | ADDARGC("--status-fd"); | |
1243 | char status_fd_buf[20]; | |
75ef8f14 | 1244 | snprintf(status_fd_buf,sizeof(status_fd_buf),"%i", fd[1]); |
edca7af0 | 1245 | ADDARG(status_fd_buf); |
11b87a08 | 1246 | unsigned long const Op = I->Op; |
007dc9e0 | 1247 | |
03e39e59 AL |
1248 | switch (I->Op) |
1249 | { | |
1250 | case Item::Remove: | |
edca7af0 DK |
1251 | ADDARGC("--force-depends"); |
1252 | ADDARGC("--force-remove-essential"); | |
1253 | ADDARGC("--remove"); | |
03e39e59 AL |
1254 | break; |
1255 | ||
fc4b5c9f | 1256 | case Item::Purge: |
edca7af0 DK |
1257 | ADDARGC("--force-depends"); |
1258 | ADDARGC("--force-remove-essential"); | |
1259 | ADDARGC("--purge"); | |
fc4b5c9f AL |
1260 | break; |
1261 | ||
03e39e59 | 1262 | case Item::Configure: |
edca7af0 | 1263 | ADDARGC("--configure"); |
03e39e59 | 1264 | break; |
3e9c4f70 DK |
1265 | |
1266 | case Item::ConfigurePending: | |
edca7af0 DK |
1267 | ADDARGC("--configure"); |
1268 | ADDARGC("--pending"); | |
3e9c4f70 DK |
1269 | break; |
1270 | ||
5e312de7 | 1271 | case Item::TriggersPending: |
edca7af0 DK |
1272 | ADDARGC("--triggers-only"); |
1273 | ADDARGC("--pending"); | |
5e312de7 DK |
1274 | break; |
1275 | ||
03e39e59 | 1276 | case Item::Install: |
edca7af0 DK |
1277 | ADDARGC("--unpack"); |
1278 | ADDARGC("--auto-deconfigure"); | |
03e39e59 AL |
1279 | break; |
1280 | } | |
3e9c4f70 | 1281 | |
5e312de7 | 1282 | if (NoTriggers == true && I->Op != Item::TriggersPending && |
d5081aee | 1283 | I->Op != Item::ConfigurePending) |
3e9c4f70 | 1284 | { |
edca7af0 | 1285 | ADDARGC("--no-triggers"); |
3e9c4f70 | 1286 | } |
edca7af0 | 1287 | #undef ADDARGC |
3e9c4f70 | 1288 | |
03e39e59 AL |
1289 | // Write in the file or package names |
1290 | if (I->Op == Item::Install) | |
30e1eab5 | 1291 | { |
f7f0d6c7 | 1292 | for (;I != J && Size < MaxArgBytes; ++I) |
30e1eab5 | 1293 | { |
cf544e14 AL |
1294 | if (I->File[0] != '/') |
1295 | return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str()); | |
edca7af0 DK |
1296 | Args.push_back(I->File.c_str()); |
1297 | Size += I->File.length(); | |
30e1eab5 | 1298 | } |
edca7af0 | 1299 | } |
03e39e59 | 1300 | else |
30e1eab5 | 1301 | { |
8e11253d | 1302 | string const nativeArch = _config->Find("APT::Architecture"); |
6f31b247 | 1303 | unsigned long const oldSize = I->Op == Item::Configure ? Size : 0; |
f7f0d6c7 | 1304 | for (;I != J && Size < MaxArgBytes; ++I) |
30e1eab5 | 1305 | { |
3e9c4f70 DK |
1306 | if((*I).Pkg.end() == true) |
1307 | continue; | |
b4017ba7 | 1308 | if (I->Op == Item::Configure && disappearedPkgs.find(I->Pkg.FullName(true)) != disappearedPkgs.end()) |
642ebc1a | 1309 | continue; |
86fc2ca8 | 1310 | // We keep this here to allow "smooth" transitions from e.g. multiarch dpkg/ubuntu to dpkg/debian |
c919ad6e DK |
1311 | if (dpkgMultiArch == false && (I->Pkg.Arch() == nativeArch || |
1312 | strcmp(I->Pkg.Arch(), "all") == 0 || | |
1313 | strcmp(I->Pkg.Arch(), "none") == 0)) | |
edca7af0 DK |
1314 | { |
1315 | char const * const name = I->Pkg.Name(); | |
1316 | ADDARG(name); | |
1317 | } | |
8e11253d SL |
1318 | else |
1319 | { | |
7720666f | 1320 | pkgCache::VerIterator PkgVer; |
3a5ec305 | 1321 | std::string name = I->Pkg.Name(); |
a1355481 MV |
1322 | if (Op == Item::Remove || Op == Item::Purge) |
1323 | { | |
2a2a7ef4 | 1324 | PkgVer = I->Pkg.CurrentVer(); |
a1355481 MV |
1325 | if(PkgVer.end() == true) |
1326 | PkgVer = FindNowVersion(I->Pkg); | |
1327 | } | |
7720666f | 1328 | else |
2a2a7ef4 | 1329 | PkgVer = Cache[I->Pkg].InstVerIter(Cache); |
c919ad6e DK |
1330 | if (strcmp(I->Pkg.Arch(), "none") == 0) |
1331 | ; // never arch-qualify a package without an arch | |
1332 | else if (PkgVer.end() == false) | |
a1355481 MV |
1333 | name.append(":").append(PkgVer.Arch()); |
1334 | else | |
1335 | _error->Warning("Can not find PkgVer for '%s'", name.c_str()); | |
3a5ec305 | 1336 | char * const fullname = strdup(name.c_str()); |
edca7af0 DK |
1337 | Packages.push_back(fullname); |
1338 | ADDARG(fullname); | |
8e11253d | 1339 | } |
6f31b247 DK |
1340 | } |
1341 | // skip configure action if all sheduled packages disappeared | |
1342 | if (oldSize == Size) | |
1343 | continue; | |
1344 | } | |
edca7af0 DK |
1345 | #undef ADDARG |
1346 | ||
30e1eab5 AL |
1347 | J = I; |
1348 | ||
1349 | if (_config->FindB("Debug::pkgDPkgPM",false) == true) | |
1350 | { | |
edca7af0 DK |
1351 | for (std::vector<const char *>::const_iterator a = Args.begin(); |
1352 | a != Args.end(); ++a) | |
1353 | clog << *a << ' '; | |
11bcbdb9 | 1354 | clog << endl; |
30e1eab5 AL |
1355 | continue; |
1356 | } | |
edca7af0 DK |
1357 | Args.push_back(NULL); |
1358 | ||
03e39e59 AL |
1359 | cout << flush; |
1360 | clog << flush; | |
1361 | cerr << flush; | |
1362 | ||
1363 | /* Mask off sig int/quit. We do this because dpkg also does when | |
1364 | it forks scripts. What happens is that when you hit ctrl-c it sends | |
1365 | it to all processes in the group. Since dpkg ignores the signal | |
1366 | it doesn't die but we do! So we must also ignore it */ | |
7f9a6360 | 1367 | sighandler_t old_SIGQUIT = signal(SIGQUIT,SIG_IGN); |
590f1923 | 1368 | sighandler_t old_SIGINT = signal(SIGINT,SigINT); |
1cecd437 CB |
1369 | |
1370 | // Check here for any SIGINT | |
11b87a08 CB |
1371 | if (pkgPackageManager::SigINTStop && (Op == Item::Remove || Op == Item::Purge || Op == Item::Install)) |
1372 | break; | |
1373 | ||
1374 | ||
73e598c3 MV |
1375 | // ignore SIGHUP as well (debian #463030) |
1376 | sighandler_t old_SIGHUP = signal(SIGHUP,SIG_IGN); | |
1377 | ||
a22fdebf | 1378 | pid_t Child = ExecFork(); |
03e39e59 AL |
1379 | // This is the child |
1380 | if (Child == 0) | |
1381 | { | |
af6b4169 | 1382 | |
c3045b79 | 1383 | if(d->slave >= 0 && d->master >= 0) |
a4cf3665 MV |
1384 | { |
1385 | setsid(); | |
c3045b79 MV |
1386 | ioctl(d->slave, TIOCSCTTY, 0); |
1387 | close(d->master); | |
1388 | dup2(d->slave, 0); | |
1389 | dup2(d->slave, 1); | |
1390 | dup2(d->slave, 2); | |
1391 | close(d->slave); | |
a4cf3665 | 1392 | } |
75ef8f14 | 1393 | close(fd[0]); // close the read end of the pipe |
d8cb4aa4 | 1394 | |
e6ee75af | 1395 | dpkgChrootDirectory(); |
4b7cfe96 | 1396 | |
cf544e14 | 1397 | if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0) |
0dbb95d8 | 1398 | _exit(100); |
af6b4169 | 1399 | |
421ff807 | 1400 | if (_config->FindB("DPkg::FlushSTDIN",true) == true && isatty(STDIN_FILENO)) |
8b5fe26c AL |
1401 | { |
1402 | int Flags,dummy; | |
1403 | if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0) | |
1404 | _exit(100); | |
1405 | ||
1406 | // Discard everything in stdin before forking dpkg | |
1407 | if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0) | |
1408 | _exit(100); | |
1409 | ||
1410 | while (read(STDIN_FILENO,&dummy,1) == 1); | |
1411 | ||
1412 | if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0) | |
1413 | _exit(100); | |
1414 | } | |
d8cb4aa4 | 1415 | |
03e39e59 AL |
1416 | /* No Job Control Stop Env is a magic dpkg var that prevents it |
1417 | from using sigstop */ | |
71afbdb5 | 1418 | putenv((char *)"DPKG_NO_TSTP=yes"); |
edca7af0 | 1419 | execvp(Args[0], (char**) &Args[0]); |
03e39e59 | 1420 | cerr << "Could not exec dpkg!" << endl; |
0dbb95d8 | 1421 | _exit(100); |
03e39e59 AL |
1422 | } |
1423 | ||
cebe0287 MV |
1424 | // apply ionice |
1425 | if (_config->FindB("DPkg::UseIoNice", false) == true) | |
1426 | ionice(Child); | |
1427 | ||
5e9458e2 MV |
1428 | // clear the Keep-Fd again |
1429 | _config->Clear("APT::Keep-Fds",fd[1]); | |
1430 | ||
03e39e59 AL |
1431 | // Wait for dpkg |
1432 | int Status = 0; | |
75ef8f14 MV |
1433 | |
1434 | // we read from dpkg here | |
887f5036 | 1435 | int const _dpkgin = fd[0]; |
75ef8f14 MV |
1436 | close(fd[1]); // close the write end of the pipe |
1437 | ||
97efd303 | 1438 | // setups fds |
c3045b79 MV |
1439 | sigemptyset(&d->sigmask); |
1440 | sigprocmask(SIG_BLOCK,&d->sigmask,&d->original_sigmask); | |
7052511e | 1441 | |
edca7af0 DK |
1442 | /* free vectors (and therefore memory) as we don't need the included data anymore */ |
1443 | for (std::vector<char *>::const_iterator p = Packages.begin(); | |
1444 | p != Packages.end(); ++p) | |
1445 | free(*p); | |
1446 | Packages.clear(); | |
8e11253d | 1447 | |
887f5036 DK |
1448 | // the result of the waitpid call |
1449 | int res; | |
090c6566 | 1450 | int select_ret; |
75ef8f14 MV |
1451 | while ((res=waitpid(Child,&Status, WNOHANG)) != Child) { |
1452 | if(res < 0) { | |
1453 | // FIXME: move this to a function or something, looks ugly here | |
1454 | // error handling, waitpid returned -1 | |
1455 | if (errno == EINTR) | |
1456 | continue; | |
1457 | RunScripts("DPkg::Post-Invoke"); | |
1458 | ||
1459 | // Restore sig int/quit | |
1460 | signal(SIGQUIT,old_SIGQUIT); | |
1461 | signal(SIGINT,old_SIGINT); | |
590f1923 | 1462 | |
e306ec47 | 1463 | signal(SIGHUP,old_SIGHUP); |
75ef8f14 MV |
1464 | return _error->Errno("waitpid","Couldn't wait for subprocess"); |
1465 | } | |
d8cb4aa4 MV |
1466 | |
1467 | // wait for input or output here | |
955a6ddb | 1468 | FD_ZERO(&rfds); |
c3045b79 | 1469 | if (d->master >= 0 && !d->stdin_is_dev_null) |
9983591d | 1470 | FD_SET(0, &rfds); |
955a6ddb | 1471 | FD_SET(_dpkgin, &rfds); |
c3045b79 MV |
1472 | if(d->master >= 0) |
1473 | FD_SET(d->master, &rfds); | |
090c6566 | 1474 | tv.tv_sec = 1; |
7052511e | 1475 | tv.tv_nsec = 0; |
c3045b79 MV |
1476 | select_ret = pselect(max(d->master, _dpkgin)+1, &rfds, NULL, NULL, |
1477 | &tv, &d->original_sigmask); | |
919e5852 | 1478 | if (select_ret < 0 && (errno == EINVAL || errno == ENOSYS)) |
c3045b79 MV |
1479 | select_ret = racy_pselect(max(d->master, _dpkgin)+1, &rfds, NULL, |
1480 | NULL, &tv, &d->original_sigmask); | |
da50ba30 MV |
1481 | if (select_ret == 0) |
1482 | continue; | |
1483 | else if (select_ret < 0 && errno == EINTR) | |
1484 | continue; | |
1485 | else if (select_ret < 0) | |
1486 | { | |
1487 | perror("select() returned error"); | |
1488 | continue; | |
1489 | } | |
1490 | ||
c3045b79 MV |
1491 | if(d->master >= 0 && FD_ISSET(d->master, &rfds)) |
1492 | DoTerminalPty(d->master); | |
1493 | if(d->master >= 0 && FD_ISSET(0, &rfds)) | |
1494 | DoStdin(d->master); | |
955a6ddb | 1495 | if(FD_ISSET(_dpkgin, &rfds)) |
e6ad8031 | 1496 | DoDpkgStatusFd(_dpkgin); |
03e39e59 | 1497 | } |
75ef8f14 | 1498 | close(_dpkgin); |
03e39e59 AL |
1499 | |
1500 | // Restore sig int/quit | |
7f9a6360 AL |
1501 | signal(SIGQUIT,old_SIGQUIT); |
1502 | signal(SIGINT,old_SIGINT); | |
590f1923 | 1503 | |
d9ec0fac | 1504 | signal(SIGHUP,old_SIGHUP); |
6dd55be7 AL |
1505 | // Check for an error code. |
1506 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
1507 | { | |
c70496f9 MV |
1508 | // if it was set to "keep-dpkg-runing" then we won't return |
1509 | // here but keep the loop going and just report it as a error | |
1510 | // for later | |
887f5036 | 1511 | bool const stopOnError = _config->FindB("Dpkg::StopOnError",true); |
f956efb4 | 1512 | |
c70496f9 MV |
1513 | if(stopOnError) |
1514 | RunScripts("DPkg::Post-Invoke"); | |
1515 | ||
1516 | if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV) | |
697a1d8a | 1517 | strprintf(d->dpkg_error, "Sub-process %s received a segmentation fault.",Args[0]); |
c70496f9 | 1518 | else if (WIFEXITED(Status) != 0) |
697a1d8a | 1519 | strprintf(d->dpkg_error, "Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status)); |
c70496f9 | 1520 | else |
697a1d8a | 1521 | strprintf(d->dpkg_error, "Sub-process %s exited unexpectedly",Args[0]); |
9169c871 | 1522 | |
697a1d8a | 1523 | if(d->dpkg_error.size() > 0) |
36b8ebbb | 1524 | _error->Error("%s", d->dpkg_error.c_str()); |
c70496f9 | 1525 | |
ff56e980 MV |
1526 | if(stopOnError) |
1527 | { | |
2e1715ea | 1528 | CloseLog(); |
e8022b09 | 1529 | d->progress->Stop(); |
c70496f9 | 1530 | return false; |
ff56e980 | 1531 | } |
6dd55be7 | 1532 | } |
03e39e59 | 1533 | } |
a38e023c | 1534 | // dpkg is done at this point |
e8022b09 | 1535 | d->progress->Stop(); |
c3045b79 | 1536 | StopPtyMagic(); |
177296df | 1537 | CloseLog(); |
af6b4169 | 1538 | |
11b87a08 CB |
1539 | if (pkgPackageManager::SigINTStop) |
1540 | _error->Warning(_("Operation was interrupted before it could finish")); | |
6dd55be7 AL |
1541 | |
1542 | if (RunScripts("DPkg::Post-Invoke") == false) | |
1543 | return false; | |
b462d75a | 1544 | |
388f2962 DK |
1545 | if (_config->FindB("Debug::pkgDPkgPM",false) == false) |
1546 | { | |
1547 | std::string const oldpkgcache = _config->FindFile("Dir::cache::pkgcache"); | |
1548 | if (oldpkgcache.empty() == false && RealFileExists(oldpkgcache) == true && | |
1549 | unlink(oldpkgcache.c_str()) == 0) | |
1550 | { | |
1551 | std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache"); | |
1552 | if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true) | |
1553 | { | |
1554 | _error->PushToStack(); | |
1555 | pkgCacheFile CacheFile; | |
1556 | CacheFile.BuildCaches(NULL, true); | |
1557 | _error->RevertToStack(); | |
1558 | } | |
1559 | } | |
1560 | } | |
1561 | ||
b462d75a | 1562 | Cache.writeStateFile(NULL); |
03e39e59 AL |
1563 | return true; |
1564 | } | |
590f1923 CB |
1565 | |
1566 | void SigINT(int sig) { | |
b1803e01 DK |
1567 | pkgPackageManager::SigINTStop = true; |
1568 | } | |
03e39e59 | 1569 | /*}}}*/ |
281daf46 AL |
1570 | // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/ |
1571 | // --------------------------------------------------------------------- | |
1572 | /* */ | |
1573 | void pkgDPkgPM::Reset() | |
1574 | { | |
1575 | List.erase(List.begin(),List.end()); | |
1576 | } | |
1577 | /*}}}*/ | |
5e457a93 MV |
1578 | // pkgDpkgPM::WriteApportReport - write out error report pkg failure /*{{{*/ |
1579 | // --------------------------------------------------------------------- | |
1580 | /* */ | |
1581 | void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg) | |
1582 | { | |
dd61e64d DK |
1583 | // If apport doesn't exist or isn't installed do nothing |
1584 | // This e.g. prevents messages in 'universes' without apport | |
1585 | pkgCache::PkgIterator apportPkg = Cache.FindPkg("apport"); | |
1586 | if (apportPkg.end() == true || apportPkg->CurrentVer == 0) | |
1587 | return; | |
1588 | ||
5e457a93 MV |
1589 | string pkgname, reportfile, srcpkgname, pkgver, arch; |
1590 | string::size_type pos; | |
1591 | FILE *report; | |
1592 | ||
23c5897c | 1593 | if (_config->FindB("Dpkg::ApportFailureReport", false) == false) |
ff38d63b MV |
1594 | { |
1595 | std::clog << "configured to not write apport reports" << std::endl; | |
5e457a93 | 1596 | return; |
ff38d63b | 1597 | } |
5e457a93 | 1598 | |
d6a4afcb | 1599 | // only report the first errors |
5273f1bf | 1600 | if(pkgFailures > _config->FindI("APT::Apport::MaxReports", 3)) |
ff38d63b MV |
1601 | { |
1602 | std::clog << _("No apport report written because MaxReports is reached already") << std::endl; | |
5e457a93 | 1603 | return; |
ff38d63b | 1604 | } |
5e457a93 | 1605 | |
d6a4afcb MV |
1606 | // check if its not a follow up error |
1607 | const char *needle = dgettext("dpkg", "dependency problems - leaving unconfigured"); | |
1608 | if(strstr(errormsg, needle) != NULL) { | |
1609 | std::clog << _("No apport report written because the error message indicates its a followup error from a previous failure.") << std::endl; | |
1610 | return; | |
1611 | } | |
1612 | ||
2f0d5dea MV |
1613 | // do not report disk-full failures |
1614 | if(strstr(errormsg, strerror(ENOSPC)) != NULL) { | |
1615 | std::clog << _("No apport report written because the error message indicates a disk full error") << std::endl; | |
1616 | return; | |
1617 | } | |
1618 | ||
3024a85e MV |
1619 | // do not report out-of-memory failures |
1620 | if(strstr(errormsg, strerror(ENOMEM)) != NULL) { | |
1621 | std::clog << _("No apport report written because the error message indicates a out of memory error") << std::endl; | |
1622 | return; | |
1623 | } | |
1624 | ||
076c46e5 MZ |
1625 | // do not report dpkg I/O errors |
1626 | // XXX - this message is localized, but this only matches the English version. This is better than nothing. | |
1627 | if(strstr(errormsg, "short read in buffer_copy (")) { | |
1628 | std::clog << _("No apport report written because the error message indicates a dpkg I/O error") << std::endl; | |
1629 | return; | |
1630 | } | |
1631 | ||
5e457a93 MV |
1632 | // get the pkgname and reportfile |
1633 | pkgname = flNotDir(pkgpath); | |
25ffa4e8 | 1634 | pos = pkgname.find('_'); |
5e457a93 | 1635 | if(pos != string::npos) |
25ffa4e8 | 1636 | pkgname = pkgname.substr(0, pos); |
5e457a93 MV |
1637 | |
1638 | // find the package versin and source package name | |
1639 | pkgCache::PkgIterator Pkg = Cache.FindPkg(pkgname); | |
1640 | if (Pkg.end() == true) | |
1641 | return; | |
1642 | pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg); | |
5e457a93 MV |
1643 | if (Ver.end() == true) |
1644 | return; | |
986d97bb | 1645 | pkgver = Ver.VerStr() == NULL ? "unknown" : Ver.VerStr(); |
5e457a93 MV |
1646 | pkgRecords Recs(Cache); |
1647 | pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList()); | |
1648 | srcpkgname = Parse.SourcePkg(); | |
1649 | if(srcpkgname.empty()) | |
1650 | srcpkgname = pkgname; | |
1651 | ||
1652 | // if the file exists already, we check: | |
1653 | // - if it was reported already (touched by apport). | |
1654 | // If not, we do nothing, otherwise | |
1655 | // we overwrite it. This is the same behaviour as apport | |
1656 | // - if we have a report with the same pkgversion already | |
1657 | // then we skip it | |
1658 | reportfile = flCombine("/var/crash",pkgname+".0.crash"); | |
1659 | if(FileExists(reportfile)) | |
1660 | { | |
1661 | struct stat buf; | |
1662 | char strbuf[255]; | |
1663 | ||
1664 | // check atime/mtime | |
1665 | stat(reportfile.c_str(), &buf); | |
1666 | if(buf.st_mtime > buf.st_atime) | |
1667 | return; | |
1668 | ||
1669 | // check if the existing report is the same version | |
1670 | report = fopen(reportfile.c_str(),"r"); | |
1671 | while(fgets(strbuf, sizeof(strbuf), report) != NULL) | |
1672 | { | |
1673 | if(strstr(strbuf,"Package:") == strbuf) | |
1674 | { | |
1675 | char pkgname[255], version[255]; | |
b3c36c6e | 1676 | if(sscanf(strbuf, "Package: %254s %254s", pkgname, version) == 2) |
5e457a93 MV |
1677 | if(strcmp(pkgver.c_str(), version) == 0) |
1678 | { | |
1679 | fclose(report); | |
1680 | return; | |
1681 | } | |
1682 | } | |
1683 | } | |
1684 | fclose(report); | |
1685 | } | |
1686 | ||
1687 | // now write the report | |
1688 | arch = _config->Find("APT::Architecture"); | |
1689 | report = fopen(reportfile.c_str(),"w"); | |
1690 | if(report == NULL) | |
1691 | return; | |
1692 | if(_config->FindB("DPkgPM::InitialReportOnly",false) == true) | |
1693 | chmod(reportfile.c_str(), 0); | |
1694 | else | |
1695 | chmod(reportfile.c_str(), 0600); | |
1696 | fprintf(report, "ProblemType: Package\n"); | |
1697 | fprintf(report, "Architecture: %s\n", arch.c_str()); | |
1698 | time_t now = time(NULL); | |
1699 | fprintf(report, "Date: %s" , ctime(&now)); | |
1700 | fprintf(report, "Package: %s %s\n", pkgname.c_str(), pkgver.c_str()); | |
1701 | fprintf(report, "SourcePackage: %s\n", srcpkgname.c_str()); | |
1702 | fprintf(report, "ErrorMessage:\n %s\n", errormsg); | |
8ecd1fed MV |
1703 | |
1704 | // ensure that the log is flushed | |
697a1d8a MV |
1705 | if(d->term_out) |
1706 | fflush(d->term_out); | |
8ecd1fed MV |
1707 | |
1708 | // attach terminal log it if we have it | |
1709 | string logfile_name = _config->FindFile("Dir::Log::Terminal"); | |
1710 | if (!logfile_name.empty()) | |
1711 | { | |
1712 | FILE *log = NULL; | |
8ecd1fed MV |
1713 | |
1714 | fprintf(report, "DpkgTerminalLog:\n"); | |
1715 | log = fopen(logfile_name.c_str(),"r"); | |
1716 | if(log != NULL) | |
1717 | { | |
69c2ecbd | 1718 | char buf[1024]; |
8ecd1fed MV |
1719 | while( fgets(buf, sizeof(buf), log) != NULL) |
1720 | fprintf(report, " %s", buf); | |
1721 | fclose(log); | |
1722 | } | |
1723 | } | |
76dbdfc7 | 1724 | |
5c8a2aa8 MV |
1725 | // log the ordering |
1726 | const char *ops_str[] = {"Install", "Configure","Remove","Purge"}; | |
1727 | fprintf(report, "AptOrdering:\n"); | |
f7f0d6c7 | 1728 | for (vector<Item>::iterator I = List.begin(); I != List.end(); ++I) |
671b7116 MV |
1729 | if ((*I).Pkg != NULL) |
1730 | fprintf(report, " %s: %s\n", (*I).Pkg.Name(), ops_str[(*I).Op]); | |
1731 | else | |
1732 | fprintf(report, " %s: %s\n", "NULL", ops_str[(*I).Op]); | |
5c8a2aa8 | 1733 | |
76dbdfc7 MV |
1734 | // attach dmesg log (to learn about segfaults) |
1735 | if (FileExists("/bin/dmesg")) | |
1736 | { | |
76dbdfc7 | 1737 | fprintf(report, "Dmesg:\n"); |
69c2ecbd | 1738 | FILE *log = popen("/bin/dmesg","r"); |
76dbdfc7 MV |
1739 | if(log != NULL) |
1740 | { | |
69c2ecbd | 1741 | char buf[1024]; |
76dbdfc7 MV |
1742 | while( fgets(buf, sizeof(buf), log) != NULL) |
1743 | fprintf(report, " %s", buf); | |
23f3cfd0 | 1744 | pclose(log); |
76dbdfc7 MV |
1745 | } |
1746 | } | |
2183a086 MV |
1747 | |
1748 | // attach df -l log (to learn about filesystem status) | |
1749 | if (FileExists("/bin/df")) | |
1750 | { | |
2183a086 MV |
1751 | |
1752 | fprintf(report, "Df:\n"); | |
69c2ecbd | 1753 | FILE *log = popen("/bin/df -l","r"); |
2183a086 MV |
1754 | if(log != NULL) |
1755 | { | |
69c2ecbd | 1756 | char buf[1024]; |
2183a086 MV |
1757 | while( fgets(buf, sizeof(buf), log) != NULL) |
1758 | fprintf(report, " %s", buf); | |
23f3cfd0 | 1759 | pclose(log); |
2183a086 MV |
1760 | } |
1761 | } | |
1762 | ||
5e457a93 | 1763 | fclose(report); |
76dbdfc7 | 1764 | |
5e457a93 MV |
1765 | } |
1766 | /*}}}*/ |