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