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