You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1945 lines
59 KiB

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