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