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