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.
 
 
 
 
 
 

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