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