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