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