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.
 
 
 
 
 
 

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