dpkgpm.cc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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 <apt-pkg/dpkgpm.h>
  10. #include <apt-pkg/error.h>
  11. #include <apt-pkg/configuration.h>
  12. #include <apt-pkg/depcache.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <apti18n.h>
  15. #include <apt-pkg/fileutl.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <fcntl.h>
  19. #include <sys/select.h>
  20. #include <sys/types.h>
  21. #include <sys/wait.h>
  22. #include <signal.h>
  23. #include <errno.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <algorithm>
  27. #include <sstream>
  28. #include <map>
  29. #include <termios.h>
  30. #include <unistd.h>
  31. #include <sys/ioctl.h>
  32. #include <pty.h>
  33. #include <config.h>
  34. #include <apti18n.h>
  35. /*}}}*/
  36. using namespace std;
  37. namespace
  38. {
  39. // Maps the dpkg "processing" info to human readable names. Entry 0
  40. // of each array is the key, entry 1 is the value.
  41. const std::pair<const char *, const char *> PackageProcessingOps[] = {
  42. std::make_pair("install", N_("Installing %s")),
  43. std::make_pair("configure", N_("Configuring %s")),
  44. std::make_pair("remove", N_("Removing %s")),
  45. std::make_pair("purge", N_("Completely removing %s")),
  46. std::make_pair("trigproc", N_("Running post-installation trigger %s"))
  47. };
  48. const std::pair<const char *, const char *> * const PackageProcessingOpsBegin = PackageProcessingOps;
  49. const std::pair<const char *, const char *> * const PackageProcessingOpsEnd = PackageProcessingOps + sizeof(PackageProcessingOps) / sizeof(PackageProcessingOps[0]);
  50. // Predicate to test whether an entry in the PackageProcessingOps
  51. // array matches a string.
  52. class MatchProcessingOp
  53. {
  54. const char *target;
  55. public:
  56. MatchProcessingOp(const char *the_target)
  57. : target(the_target)
  58. {
  59. }
  60. bool operator()(const std::pair<const char *, const char *> &pair) const
  61. {
  62. return strcmp(pair.first, target) == 0;
  63. }
  64. };
  65. }
  66. /* helper function to ionice the given PID
  67. there is no C header for ionice yet - just the syscall interface
  68. so we use the binary from util-linux
  69. */
  70. static bool
  71. ionice(int PID)
  72. {
  73. if (!FileExists("/usr/bin/ionice"))
  74. return false;
  75. pid_t Process = ExecFork();
  76. if (Process == 0)
  77. {
  78. char buf[32];
  79. snprintf(buf, sizeof(buf), "-p%d", PID);
  80. const char *Args[4];
  81. Args[0] = "/usr/bin/ionice";
  82. Args[1] = "-c3";
  83. Args[2] = buf;
  84. Args[3] = 0;
  85. execv(Args[0], (char **)Args);
  86. }
  87. return ExecWait(Process, "ionice");
  88. }
  89. // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
  90. // ---------------------------------------------------------------------
  91. /* */
  92. pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache)
  93. : pkgPackageManager(Cache), dpkgbuf_pos(0),
  94. term_out(NULL), PackagesDone(0), PackagesTotal(0)
  95. {
  96. }
  97. /*}}}*/
  98. // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
  99. // ---------------------------------------------------------------------
  100. /* */
  101. pkgDPkgPM::~pkgDPkgPM()
  102. {
  103. }
  104. /*}}}*/
  105. // DPkgPM::Install - Install a package /*{{{*/
  106. // ---------------------------------------------------------------------
  107. /* Add an install operation to the sequence list */
  108. bool pkgDPkgPM::Install(PkgIterator Pkg,string File)
  109. {
  110. if (File.empty() == true || Pkg.end() == true)
  111. return _error->Error("Internal Error, No file name for %s",Pkg.Name());
  112. List.push_back(Item(Item::Install,Pkg,File));
  113. return true;
  114. }
  115. /*}}}*/
  116. // DPkgPM::Configure - Configure a package /*{{{*/
  117. // ---------------------------------------------------------------------
  118. /* Add a configure operation to the sequence list */
  119. bool pkgDPkgPM::Configure(PkgIterator Pkg)
  120. {
  121. if (Pkg.end() == true)
  122. return false;
  123. List.push_back(Item(Item::Configure, Pkg));
  124. // Use triggers for config calls if we configure "smart"
  125. // as otherwise Pre-Depends will not be satisfied, see #526774
  126. if (_config->FindB("DPkg::TriggersPending", false) == true)
  127. List.push_back(Item(Item::TriggersPending, PkgIterator()));
  128. return true;
  129. }
  130. /*}}}*/
  131. // DPkgPM::Remove - Remove a package /*{{{*/
  132. // ---------------------------------------------------------------------
  133. /* Add a remove operation to the sequence list */
  134. bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge)
  135. {
  136. if (Pkg.end() == true)
  137. return false;
  138. if (Purge == true)
  139. List.push_back(Item(Item::Purge,Pkg));
  140. else
  141. List.push_back(Item(Item::Remove,Pkg));
  142. return true;
  143. }
  144. /*}}}*/
  145. // DPkgPM::SendV2Pkgs - Send version 2 package info /*{{{*/
  146. // ---------------------------------------------------------------------
  147. /* This is part of the helper script communication interface, it sends
  148. very complete information down to the other end of the pipe.*/
  149. bool pkgDPkgPM::SendV2Pkgs(FILE *F)
  150. {
  151. fprintf(F,"VERSION 2\n");
  152. /* Write out all of the configuration directives by walking the
  153. configuration tree */
  154. const Configuration::Item *Top = _config->Tree(0);
  155. for (; Top != 0;)
  156. {
  157. if (Top->Value.empty() == false)
  158. {
  159. fprintf(F,"%s=%s\n",
  160. QuoteString(Top->FullTag(),"=\"\n").c_str(),
  161. QuoteString(Top->Value,"\n").c_str());
  162. }
  163. if (Top->Child != 0)
  164. {
  165. Top = Top->Child;
  166. continue;
  167. }
  168. while (Top != 0 && Top->Next == 0)
  169. Top = Top->Parent;
  170. if (Top != 0)
  171. Top = Top->Next;
  172. }
  173. fprintf(F,"\n");
  174. // Write out the package actions in order.
  175. for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
  176. {
  177. if(I->Pkg.end() == true)
  178. continue;
  179. pkgDepCache::StateCache &S = Cache[I->Pkg];
  180. fprintf(F,"%s ",I->Pkg.Name());
  181. // Current version
  182. if (I->Pkg->CurrentVer == 0)
  183. fprintf(F,"- ");
  184. else
  185. fprintf(F,"%s ",I->Pkg.CurrentVer().VerStr());
  186. // Show the compare operator
  187. // Target version
  188. if (S.InstallVer != 0)
  189. {
  190. int Comp = 2;
  191. if (I->Pkg->CurrentVer != 0)
  192. Comp = S.InstVerIter(Cache).CompareVer(I->Pkg.CurrentVer());
  193. if (Comp < 0)
  194. fprintf(F,"> ");
  195. if (Comp == 0)
  196. fprintf(F,"= ");
  197. if (Comp > 0)
  198. fprintf(F,"< ");
  199. fprintf(F,"%s ",S.InstVerIter(Cache).VerStr());
  200. }
  201. else
  202. fprintf(F,"> - ");
  203. // Show the filename/operation
  204. if (I->Op == Item::Install)
  205. {
  206. // No errors here..
  207. if (I->File[0] != '/')
  208. fprintf(F,"**ERROR**\n");
  209. else
  210. fprintf(F,"%s\n",I->File.c_str());
  211. }
  212. if (I->Op == Item::Configure)
  213. fprintf(F,"**CONFIGURE**\n");
  214. if (I->Op == Item::Remove ||
  215. I->Op == Item::Purge)
  216. fprintf(F,"**REMOVE**\n");
  217. if (ferror(F) != 0)
  218. return false;
  219. }
  220. return true;
  221. }
  222. /*}}}*/
  223. // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/
  224. // ---------------------------------------------------------------------
  225. /* This looks for a list of scripts to run from the configuration file
  226. each one is run and is fed on standard input a list of all .deb files
  227. that are due to be installed. */
  228. bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf)
  229. {
  230. Configuration::Item const *Opts = _config->Tree(Cnf);
  231. if (Opts == 0 || Opts->Child == 0)
  232. return true;
  233. Opts = Opts->Child;
  234. unsigned int Count = 1;
  235. for (; Opts != 0; Opts = Opts->Next, Count++)
  236. {
  237. if (Opts->Value.empty() == true)
  238. continue;
  239. // Determine the protocol version
  240. string OptSec = Opts->Value;
  241. string::size_type Pos;
  242. if ((Pos = OptSec.find(' ')) == string::npos || Pos == 0)
  243. Pos = OptSec.length();
  244. OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos);
  245. unsigned int Version = _config->FindI(OptSec+"::Version",1);
  246. // Create the pipes
  247. int Pipes[2];
  248. if (pipe(Pipes) != 0)
  249. return _error->Errno("pipe","Failed to create IPC pipe to subprocess");
  250. SetCloseExec(Pipes[0],true);
  251. SetCloseExec(Pipes[1],true);
  252. // Purified Fork for running the script
  253. pid_t Process = ExecFork();
  254. if (Process == 0)
  255. {
  256. // Setup the FDs
  257. dup2(Pipes[0],STDIN_FILENO);
  258. SetCloseExec(STDOUT_FILENO,false);
  259. SetCloseExec(STDIN_FILENO,false);
  260. SetCloseExec(STDERR_FILENO,false);
  261. const char *Args[4];
  262. Args[0] = "/bin/sh";
  263. Args[1] = "-c";
  264. Args[2] = Opts->Value.c_str();
  265. Args[3] = 0;
  266. execv(Args[0],(char **)Args);
  267. _exit(100);
  268. }
  269. close(Pipes[0]);
  270. FILE *F = fdopen(Pipes[1],"w");
  271. if (F == 0)
  272. return _error->Errno("fdopen","Faild to open new FD");
  273. // Feed it the filenames.
  274. bool Die = false;
  275. if (Version <= 1)
  276. {
  277. for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
  278. {
  279. // Only deal with packages to be installed from .deb
  280. if (I->Op != Item::Install)
  281. continue;
  282. // No errors here..
  283. if (I->File[0] != '/')
  284. continue;
  285. /* Feed the filename of each package that is pending install
  286. into the pipe. */
  287. fprintf(F,"%s\n",I->File.c_str());
  288. if (ferror(F) != 0)
  289. {
  290. Die = true;
  291. break;
  292. }
  293. }
  294. }
  295. else
  296. Die = !SendV2Pkgs(F);
  297. fclose(F);
  298. // Clean up the sub process
  299. if (ExecWait(Process,Opts->Value.c_str()) == false)
  300. return _error->Error("Failure running script %s",Opts->Value.c_str());
  301. }
  302. return true;
  303. }
  304. /*}}}*/
  305. // DPkgPM::DoStdin - Read stdin and pass to slave pty /*{{{*/
  306. // ---------------------------------------------------------------------
  307. /*
  308. */
  309. void pkgDPkgPM::DoStdin(int master)
  310. {
  311. unsigned char input_buf[256] = {0,};
  312. ssize_t len = read(0, input_buf, sizeof(input_buf));
  313. if (len)
  314. write(master, input_buf, len);
  315. else
  316. stdin_is_dev_null = true;
  317. }
  318. /*}}}*/
  319. // DPkgPM::DoTerminalPty - Read the terminal pty and write log /*{{{*/
  320. // ---------------------------------------------------------------------
  321. /*
  322. * read the terminal pty and write log
  323. */
  324. void pkgDPkgPM::DoTerminalPty(int master)
  325. {
  326. unsigned char term_buf[1024] = {0,0, };
  327. ssize_t len=read(master, term_buf, sizeof(term_buf));
  328. if(len == -1 && errno == EIO)
  329. {
  330. // this happens when the child is about to exit, we
  331. // give it time to actually exit, otherwise we run
  332. // into a race
  333. usleep(500000);
  334. return;
  335. }
  336. if(len <= 0)
  337. return;
  338. write(1, term_buf, len);
  339. if(term_out)
  340. fwrite(term_buf, len, sizeof(char), term_out);
  341. }
  342. /*}}}*/
  343. // DPkgPM::ProcessDpkgStatusBuf /*{{{*/
  344. // ---------------------------------------------------------------------
  345. /*
  346. */
  347. void pkgDPkgPM::ProcessDpkgStatusLine(int OutStatusFd, char *line)
  348. {
  349. bool const Debug = _config->FindB("Debug::pkgDPkgProgressReporting",false);
  350. // the status we output
  351. ostringstream status;
  352. if (Debug == true)
  353. std::clog << "got from dpkg '" << line << "'" << std::endl;
  354. /* dpkg sends strings like this:
  355. 'status: <pkg>: <pkg qstate>'
  356. errors look like this:
  357. '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
  358. and conffile-prompt like this
  359. 'status: conffile-prompt: conffile : 'current-conffile' 'new-conffile' useredited distedited
  360. Newer versions of dpkg sent also:
  361. 'processing: install: pkg'
  362. 'processing: configure: pkg'
  363. 'processing: remove: pkg'
  364. 'processing: purge: pkg' - but for apt is it a ignored "unknown" action
  365. 'processing: trigproc: trigger'
  366. */
  367. char* list[5];
  368. // dpkg sends multiline error messages sometimes (see
  369. // #374195 for a example. we should support this by
  370. // either patching dpkg to not send multiline over the
  371. // statusfd or by rewriting the code here to deal with
  372. // it. for now we just ignore it and not crash
  373. TokSplitString(':', line, list, sizeof(list)/sizeof(list[0]));
  374. if( list[0] == NULL || list[1] == NULL || list[2] == NULL)
  375. {
  376. if (Debug == true)
  377. std::clog << "ignoring line: not enough ':'" << std::endl;
  378. return;
  379. }
  380. const char* const pkg = list[1];
  381. const char* action = _strstrip(list[2]);
  382. // 'processing' from dpkg looks like
  383. // 'processing: action: pkg'
  384. if(strncmp(list[0], "processing", strlen("processing")) == 0)
  385. {
  386. char s[200];
  387. const char* const pkg_or_trigger = _strstrip(list[2]);
  388. action = _strstrip( list[1]);
  389. const std::pair<const char *, const char *> * const iter =
  390. std::find_if(PackageProcessingOpsBegin,
  391. PackageProcessingOpsEnd,
  392. MatchProcessingOp(action));
  393. if(iter == PackageProcessingOpsEnd)
  394. {
  395. if (Debug == true)
  396. std::clog << "ignoring unknown action: " << action << std::endl;
  397. return;
  398. }
  399. snprintf(s, sizeof(s), _(iter->second), pkg_or_trigger);
  400. status << "pmstatus:" << pkg_or_trigger
  401. << ":" << (PackagesDone/float(PackagesTotal)*100.0)
  402. << ":" << s
  403. << endl;
  404. if(OutStatusFd > 0)
  405. write(OutStatusFd, status.str().c_str(), status.str().size());
  406. if (Debug == true)
  407. std::clog << "send: '" << status.str() << "'" << endl;
  408. return;
  409. }
  410. if(strncmp(action,"error",strlen("error")) == 0)
  411. {
  412. status << "pmerror:" << list[1]
  413. << ":" << (PackagesDone/float(PackagesTotal)*100.0)
  414. << ":" << list[3]
  415. << endl;
  416. if(OutStatusFd > 0)
  417. write(OutStatusFd, status.str().c_str(), status.str().size());
  418. if (Debug == true)
  419. std::clog << "send: '" << status.str() << "'" << endl;
  420. return;
  421. }
  422. else if(strncmp(action,"conffile",strlen("conffile")) == 0)
  423. {
  424. status << "pmconffile:" << list[1]
  425. << ":" << (PackagesDone/float(PackagesTotal)*100.0)
  426. << ":" << list[3]
  427. << endl;
  428. if(OutStatusFd > 0)
  429. write(OutStatusFd, status.str().c_str(), status.str().size());
  430. if (Debug == true)
  431. std::clog << "send: '" << status.str() << "'" << endl;
  432. return;
  433. }
  434. vector<struct DpkgState> const &states = PackageOps[pkg];
  435. const char *next_action = NULL;
  436. if(PackageOpsDone[pkg] < states.size())
  437. next_action = states[PackageOpsDone[pkg]].state;
  438. // check if the package moved to the next dpkg state
  439. if(next_action && (strcmp(action, next_action) == 0))
  440. {
  441. // only read the translation if there is actually a next
  442. // action
  443. const char *translation = _(states[PackageOpsDone[pkg]].str);
  444. char s[200];
  445. snprintf(s, sizeof(s), translation, pkg);
  446. // we moved from one dpkg state to a new one, report that
  447. PackageOpsDone[pkg]++;
  448. PackagesDone++;
  449. // build the status str
  450. status << "pmstatus:" << pkg
  451. << ":" << (PackagesDone/float(PackagesTotal)*100.0)
  452. << ":" << s
  453. << endl;
  454. if(OutStatusFd > 0)
  455. write(OutStatusFd, status.str().c_str(), status.str().size());
  456. if (Debug == true)
  457. std::clog << "send: '" << status.str() << "'" << endl;
  458. }
  459. if (Debug == true)
  460. std::clog << "(parsed from dpkg) pkg: " << pkg
  461. << " action: " << action << endl;
  462. }
  463. /*}}}*/
  464. // DPkgPM::DoDpkgStatusFd /*{{{*/
  465. // ---------------------------------------------------------------------
  466. /*
  467. */
  468. void pkgDPkgPM::DoDpkgStatusFd(int statusfd, int OutStatusFd)
  469. {
  470. char *p, *q;
  471. int len;
  472. len=read(statusfd, &dpkgbuf[dpkgbuf_pos], sizeof(dpkgbuf)-dpkgbuf_pos);
  473. dpkgbuf_pos += len;
  474. if(len <= 0)
  475. return;
  476. // process line by line if we have a buffer
  477. p = q = dpkgbuf;
  478. while((q=(char*)memchr(p, '\n', dpkgbuf+dpkgbuf_pos-p)) != NULL)
  479. {
  480. *q = 0;
  481. ProcessDpkgStatusLine(OutStatusFd, p);
  482. p=q+1; // continue with next line
  483. }
  484. // now move the unprocessed bits (after the final \n that is now a 0x0)
  485. // to the start and update dpkgbuf_pos
  486. p = (char*)memrchr(dpkgbuf, 0, dpkgbuf_pos);
  487. if(p == NULL)
  488. return;
  489. // we are interessted in the first char *after* 0x0
  490. p++;
  491. // move the unprocessed tail to the start and update pos
  492. memmove(dpkgbuf, p, p-dpkgbuf);
  493. dpkgbuf_pos = dpkgbuf+dpkgbuf_pos-p;
  494. }
  495. /*}}}*/
  496. // DPkgPM::OpenLog /*{{{*/
  497. bool pkgDPkgPM::OpenLog()
  498. {
  499. string logdir = _config->FindDir("Dir::Log");
  500. if(not FileExists(logdir))
  501. return _error->Error(_("Directory '%s' missing"), logdir.c_str());
  502. string logfile_name = flCombine(logdir,
  503. _config->Find("Dir::Log::Terminal"));
  504. if (!logfile_name.empty())
  505. {
  506. term_out = fopen(logfile_name.c_str(),"a");
  507. if (term_out == NULL)
  508. return _error->WarningE(_("Could not open file '%s'"), logfile_name.c_str());
  509. chmod(logfile_name.c_str(), 0600);
  510. // output current time
  511. char outstr[200];
  512. time_t t = time(NULL);
  513. struct tm *tmp = localtime(&t);
  514. strftime(outstr, sizeof(outstr), "%F %T", tmp);
  515. fprintf(term_out, "\nLog started: %s\n", outstr);
  516. }
  517. return true;
  518. }
  519. /*}}}*/
  520. // DPkg::CloseLog /*{{{*/
  521. bool pkgDPkgPM::CloseLog()
  522. {
  523. if(term_out)
  524. {
  525. char outstr[200];
  526. time_t t = time(NULL);
  527. struct tm *tmp = localtime(&t);
  528. strftime(outstr, sizeof(outstr), "%F %T", tmp);
  529. fprintf(term_out, "Log ended: ");
  530. fprintf(term_out, "%s", outstr);
  531. fprintf(term_out, "\n");
  532. fclose(term_out);
  533. }
  534. term_out = NULL;
  535. return true;
  536. }
  537. /*}}}*/
  538. /*{{{*/
  539. // This implements a racy version of pselect for those architectures
  540. // that don't have a working implementation.
  541. // FIXME: Probably can be removed on Lenny+1
  542. static int racy_pselect(int nfds, fd_set *readfds, fd_set *writefds,
  543. fd_set *exceptfds, const struct timespec *timeout,
  544. const sigset_t *sigmask)
  545. {
  546. sigset_t origmask;
  547. struct timeval tv;
  548. int retval;
  549. tv.tv_sec = timeout->tv_sec;
  550. tv.tv_usec = timeout->tv_nsec/1000;
  551. sigprocmask(SIG_SETMASK, sigmask, &origmask);
  552. retval = select(nfds, readfds, writefds, exceptfds, &tv);
  553. sigprocmask(SIG_SETMASK, &origmask, 0);
  554. return retval;
  555. }
  556. /*}}}*/
  557. // DPkgPM::Go - Run the sequence /*{{{*/
  558. // ---------------------------------------------------------------------
  559. /* This globs the operations and calls dpkg
  560. *
  561. * If it is called with "OutStatusFd" set to a valid file descriptor
  562. * apt will report the install progress over this fd. It maps the
  563. * dpkg states a package goes through to human readable (and i10n-able)
  564. * names and calculates a percentage for each step.
  565. */
  566. bool pkgDPkgPM::Go(int OutStatusFd)
  567. {
  568. fd_set rfds;
  569. struct timespec tv;
  570. sigset_t sigmask;
  571. sigset_t original_sigmask;
  572. unsigned int const MaxArgs = _config->FindI("Dpkg::MaxArgs",8*1024);
  573. unsigned int const MaxArgBytes = _config->FindI("Dpkg::MaxArgBytes",32*1024);
  574. bool const NoTriggers = _config->FindB("DPkg::NoTriggers", false);
  575. if (RunScripts("DPkg::Pre-Invoke") == false)
  576. return false;
  577. if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
  578. return false;
  579. // support subpressing of triggers processing for special
  580. // cases like d-i that runs the triggers handling manually
  581. bool const SmartConf = (_config->Find("PackageManager::Configure", "all") != "all");
  582. bool const TriggersPending = _config->FindB("DPkg::TriggersPending", false);
  583. if (_config->FindB("DPkg::ConfigurePending", SmartConf) == true)
  584. List.push_back(Item(Item::ConfigurePending, PkgIterator()));
  585. // map the dpkg states to the operations that are performed
  586. // (this is sorted in the same way as Item::Ops)
  587. static const struct DpkgState DpkgStatesOpMap[][7] = {
  588. // Install operation
  589. {
  590. {"half-installed", N_("Preparing %s")},
  591. {"unpacked", N_("Unpacking %s") },
  592. {NULL, NULL}
  593. },
  594. // Configure operation
  595. {
  596. {"unpacked",N_("Preparing to configure %s") },
  597. {"half-configured", N_("Configuring %s") },
  598. { "installed", N_("Installed %s")},
  599. {NULL, NULL}
  600. },
  601. // Remove operation
  602. {
  603. {"half-configured", N_("Preparing for removal of %s")},
  604. {"half-installed", N_("Removing %s")},
  605. {"config-files", N_("Removed %s")},
  606. {NULL, NULL}
  607. },
  608. // Purge operation
  609. {
  610. {"config-files", N_("Preparing to completely remove %s")},
  611. {"not-installed", N_("Completely removed %s")},
  612. {NULL, NULL}
  613. },
  614. };
  615. // init the PackageOps map, go over the list of packages that
  616. // that will be [installed|configured|removed|purged] and add
  617. // them to the PackageOps map (the dpkg states it goes through)
  618. // and the PackageOpsTranslations (human readable strings)
  619. for (vector<Item>::const_iterator I = List.begin(); I != List.end();I++)
  620. {
  621. if((*I).Pkg.end() == true)
  622. continue;
  623. string const name = (*I).Pkg.Name();
  624. PackageOpsDone[name] = 0;
  625. for(int i=0; (DpkgStatesOpMap[(*I).Op][i]).state != NULL; i++)
  626. {
  627. PackageOps[name].push_back(DpkgStatesOpMap[(*I).Op][i]);
  628. PackagesTotal++;
  629. }
  630. }
  631. stdin_is_dev_null = false;
  632. // create log
  633. OpenLog();
  634. // this loop is runs once per operation
  635. for (vector<Item>::const_iterator I = List.begin(); I != List.end();)
  636. {
  637. // Do all actions with the same Op in one run
  638. vector<Item>::const_iterator J = I;
  639. if (TriggersPending == true)
  640. for (; J != List.end(); J++)
  641. {
  642. if (J->Op == I->Op)
  643. continue;
  644. if (J->Op != Item::TriggersPending)
  645. break;
  646. vector<Item>::const_iterator T = J + 1;
  647. if (T != List.end() && T->Op == I->Op)
  648. continue;
  649. break;
  650. }
  651. else
  652. for (; J != List.end() && J->Op == I->Op; J++)
  653. /* nothing */;
  654. // Generate the argument list
  655. const char *Args[MaxArgs + 50];
  656. // Now check if we are within the MaxArgs limit
  657. //
  658. // this code below is problematic, because it may happen that
  659. // the argument list is split in a way that A depends on B
  660. // and they are in the same "--configure A B" run
  661. // - with the split they may now be configured in different
  662. // runs
  663. if (J - I > (signed)MaxArgs)
  664. J = I + MaxArgs;
  665. unsigned int n = 0;
  666. unsigned long Size = 0;
  667. string const Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
  668. Args[n++] = Tmp.c_str();
  669. Size += strlen(Args[n-1]);
  670. // Stick in any custom dpkg options
  671. Configuration::Item const *Opts = _config->Tree("DPkg::Options");
  672. if (Opts != 0)
  673. {
  674. Opts = Opts->Child;
  675. for (; Opts != 0; Opts = Opts->Next)
  676. {
  677. if (Opts->Value.empty() == true)
  678. continue;
  679. Args[n++] = Opts->Value.c_str();
  680. Size += Opts->Value.length();
  681. }
  682. }
  683. char status_fd_buf[20];
  684. int fd[2];
  685. pipe(fd);
  686. Args[n++] = "--status-fd";
  687. Size += strlen(Args[n-1]);
  688. snprintf(status_fd_buf,sizeof(status_fd_buf),"%i", fd[1]);
  689. Args[n++] = status_fd_buf;
  690. Size += strlen(Args[n-1]);
  691. switch (I->Op)
  692. {
  693. case Item::Remove:
  694. Args[n++] = "--force-depends";
  695. Size += strlen(Args[n-1]);
  696. Args[n++] = "--force-remove-essential";
  697. Size += strlen(Args[n-1]);
  698. Args[n++] = "--remove";
  699. Size += strlen(Args[n-1]);
  700. break;
  701. case Item::Purge:
  702. Args[n++] = "--force-depends";
  703. Size += strlen(Args[n-1]);
  704. Args[n++] = "--force-remove-essential";
  705. Size += strlen(Args[n-1]);
  706. Args[n++] = "--purge";
  707. Size += strlen(Args[n-1]);
  708. break;
  709. case Item::Configure:
  710. Args[n++] = "--configure";
  711. Size += strlen(Args[n-1]);
  712. break;
  713. case Item::ConfigurePending:
  714. Args[n++] = "--configure";
  715. Size += strlen(Args[n-1]);
  716. Args[n++] = "--pending";
  717. Size += strlen(Args[n-1]);
  718. break;
  719. case Item::TriggersPending:
  720. Args[n++] = "--triggers-only";
  721. Size += strlen(Args[n-1]);
  722. Args[n++] = "--pending";
  723. Size += strlen(Args[n-1]);
  724. break;
  725. case Item::Install:
  726. Args[n++] = "--unpack";
  727. Size += strlen(Args[n-1]);
  728. Args[n++] = "--auto-deconfigure";
  729. Size += strlen(Args[n-1]);
  730. break;
  731. }
  732. if (NoTriggers == true && I->Op != Item::TriggersPending &&
  733. I->Op != Item::ConfigurePending)
  734. {
  735. Args[n++] = "--no-triggers";
  736. Size += strlen(Args[n-1]);
  737. }
  738. // Write in the file or package names
  739. if (I->Op == Item::Install)
  740. {
  741. for (;I != J && Size < MaxArgBytes; I++)
  742. {
  743. if (I->File[0] != '/')
  744. return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str());
  745. Args[n++] = I->File.c_str();
  746. Size += strlen(Args[n-1]);
  747. }
  748. }
  749. else
  750. {
  751. for (;I != J && Size < MaxArgBytes; I++)
  752. {
  753. if((*I).Pkg.end() == true)
  754. continue;
  755. Args[n++] = I->Pkg.Name();
  756. Size += strlen(Args[n-1]);
  757. }
  758. }
  759. Args[n] = 0;
  760. J = I;
  761. if (_config->FindB("Debug::pkgDPkgPM",false) == true)
  762. {
  763. for (unsigned int k = 0; k != n; k++)
  764. clog << Args[k] << ' ';
  765. clog << endl;
  766. continue;
  767. }
  768. cout << flush;
  769. clog << flush;
  770. cerr << flush;
  771. /* Mask off sig int/quit. We do this because dpkg also does when
  772. it forks scripts. What happens is that when you hit ctrl-c it sends
  773. it to all processes in the group. Since dpkg ignores the signal
  774. it doesn't die but we do! So we must also ignore it */
  775. sighandler_t old_SIGQUIT = signal(SIGQUIT,SIG_IGN);
  776. sighandler_t old_SIGINT = signal(SIGINT,SIG_IGN);
  777. // ignore SIGHUP as well (debian #463030)
  778. sighandler_t old_SIGHUP = signal(SIGHUP,SIG_IGN);
  779. struct termios tt;
  780. struct winsize win;
  781. int master = -1;
  782. int slave = -1;
  783. // if tcgetattr does not return zero there was a error
  784. // and we do not do any pty magic
  785. if (tcgetattr(0, &tt) == 0)
  786. {
  787. ioctl(0, TIOCGWINSZ, (char *)&win);
  788. if (openpty(&master, &slave, NULL, &tt, &win) < 0)
  789. {
  790. const char *s = _("Can not write log, openpty() "
  791. "failed (/dev/pts not mounted?)\n");
  792. fprintf(stderr, "%s",s);
  793. if(term_out)
  794. fprintf(term_out, "%s",s);
  795. master = slave = -1;
  796. } else {
  797. struct termios rtt;
  798. rtt = tt;
  799. cfmakeraw(&rtt);
  800. rtt.c_lflag &= ~ECHO;
  801. // block SIGTTOU during tcsetattr to prevent a hang if
  802. // the process is a member of the background process group
  803. // http://www.opengroup.org/onlinepubs/000095399/functions/tcsetattr.html
  804. sigemptyset(&sigmask);
  805. sigaddset(&sigmask, SIGTTOU);
  806. sigprocmask(SIG_BLOCK,&sigmask, &original_sigmask);
  807. tcsetattr(0, TCSAFLUSH, &rtt);
  808. sigprocmask(SIG_SETMASK, &original_sigmask, 0);
  809. }
  810. }
  811. // Fork dpkg
  812. pid_t Child;
  813. _config->Set("APT::Keep-Fds::",fd[1]);
  814. // send status information that we are about to fork dpkg
  815. if(OutStatusFd > 0) {
  816. ostringstream status;
  817. status << "pmstatus:dpkg-exec:"
  818. << (PackagesDone/float(PackagesTotal)*100.0)
  819. << ":" << _("Running dpkg")
  820. << endl;
  821. write(OutStatusFd, status.str().c_str(), status.str().size());
  822. }
  823. Child = ExecFork();
  824. // This is the child
  825. if (Child == 0)
  826. {
  827. if(slave >= 0 && master >= 0)
  828. {
  829. setsid();
  830. ioctl(slave, TIOCSCTTY, 0);
  831. close(master);
  832. dup2(slave, 0);
  833. dup2(slave, 1);
  834. dup2(slave, 2);
  835. close(slave);
  836. }
  837. close(fd[0]); // close the read end of the pipe
  838. if (_config->FindDir("DPkg::Chroot-Directory","/") != "/")
  839. {
  840. std::cerr << "Chrooting into "
  841. << _config->FindDir("DPkg::Chroot-Directory")
  842. << std::endl;
  843. if (chroot(_config->FindDir("DPkg::Chroot-Directory","/").c_str()) != 0)
  844. _exit(100);
  845. }
  846. if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
  847. _exit(100);
  848. if (_config->FindB("DPkg::FlushSTDIN",true) == true && isatty(STDIN_FILENO))
  849. {
  850. int Flags,dummy;
  851. if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0)
  852. _exit(100);
  853. // Discard everything in stdin before forking dpkg
  854. if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0)
  855. _exit(100);
  856. while (read(STDIN_FILENO,&dummy,1) == 1);
  857. if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
  858. _exit(100);
  859. }
  860. /* No Job Control Stop Env is a magic dpkg var that prevents it
  861. from using sigstop */
  862. putenv((char *)"DPKG_NO_TSTP=yes");
  863. execvp(Args[0],(char **)Args);
  864. cerr << "Could not exec dpkg!" << endl;
  865. _exit(100);
  866. }
  867. // apply ionice
  868. if (_config->FindB("DPkg::UseIoNice", false) == true)
  869. ionice(Child);
  870. // clear the Keep-Fd again
  871. _config->Clear("APT::Keep-Fds",fd[1]);
  872. // Wait for dpkg
  873. int Status = 0;
  874. // we read from dpkg here
  875. int const _dpkgin = fd[0];
  876. close(fd[1]); // close the write end of the pipe
  877. if(slave > 0)
  878. close(slave);
  879. // setups fds
  880. sigemptyset(&sigmask);
  881. sigprocmask(SIG_BLOCK,&sigmask,&original_sigmask);
  882. // the result of the waitpid call
  883. int res;
  884. int select_ret;
  885. while ((res=waitpid(Child,&Status, WNOHANG)) != Child) {
  886. if(res < 0) {
  887. // FIXME: move this to a function or something, looks ugly here
  888. // error handling, waitpid returned -1
  889. if (errno == EINTR)
  890. continue;
  891. RunScripts("DPkg::Post-Invoke");
  892. // Restore sig int/quit
  893. signal(SIGQUIT,old_SIGQUIT);
  894. signal(SIGINT,old_SIGINT);
  895. signal(SIGHUP,old_SIGHUP);
  896. return _error->Errno("waitpid","Couldn't wait for subprocess");
  897. }
  898. // wait for input or output here
  899. FD_ZERO(&rfds);
  900. if (!stdin_is_dev_null)
  901. FD_SET(0, &rfds);
  902. FD_SET(_dpkgin, &rfds);
  903. if(master >= 0)
  904. FD_SET(master, &rfds);
  905. tv.tv_sec = 1;
  906. tv.tv_nsec = 0;
  907. select_ret = pselect(max(master, _dpkgin)+1, &rfds, NULL, NULL,
  908. &tv, &original_sigmask);
  909. if (select_ret < 0 && (errno == EINVAL || errno == ENOSYS))
  910. select_ret = racy_pselect(max(master, _dpkgin)+1, &rfds, NULL,
  911. NULL, &tv, &original_sigmask);
  912. if (select_ret == 0)
  913. continue;
  914. else if (select_ret < 0 && errno == EINTR)
  915. continue;
  916. else if (select_ret < 0)
  917. {
  918. perror("select() returned error");
  919. continue;
  920. }
  921. if(master >= 0 && FD_ISSET(master, &rfds))
  922. DoTerminalPty(master);
  923. if(master >= 0 && FD_ISSET(0, &rfds))
  924. DoStdin(master);
  925. if(FD_ISSET(_dpkgin, &rfds))
  926. DoDpkgStatusFd(_dpkgin, OutStatusFd);
  927. }
  928. close(_dpkgin);
  929. // Restore sig int/quit
  930. signal(SIGQUIT,old_SIGQUIT);
  931. signal(SIGINT,old_SIGINT);
  932. signal(SIGHUP,old_SIGHUP);
  933. if(master >= 0)
  934. {
  935. tcsetattr(0, TCSAFLUSH, &tt);
  936. close(master);
  937. }
  938. // Check for an error code.
  939. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  940. {
  941. // if it was set to "keep-dpkg-runing" then we won't return
  942. // here but keep the loop going and just report it as a error
  943. // for later
  944. bool const stopOnError = _config->FindB("Dpkg::StopOnError",true);
  945. if(stopOnError)
  946. RunScripts("DPkg::Post-Invoke");
  947. if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV)
  948. _error->Error("Sub-process %s received a segmentation fault.",Args[0]);
  949. else if (WIFEXITED(Status) != 0)
  950. _error->Error("Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status));
  951. else
  952. _error->Error("Sub-process %s exited unexpectedly",Args[0]);
  953. if(stopOnError)
  954. {
  955. CloseLog();
  956. return false;
  957. }
  958. }
  959. }
  960. CloseLog();
  961. if (RunScripts("DPkg::Post-Invoke") == false)
  962. return false;
  963. Cache.writeStateFile(NULL);
  964. return true;
  965. }
  966. /*}}}*/
  967. // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
  968. // ---------------------------------------------------------------------
  969. /* */
  970. void pkgDPkgPM::Reset()
  971. {
  972. List.erase(List.begin(),List.end());
  973. }
  974. /*}}}*/