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