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.
 
 
 
 
 
 

2137 lines
68 KiB

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