dpkgpm.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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 <unistd.h>
  15. #include <stdlib.h>
  16. #include <fcntl.h>
  17. #include <sys/types.h>
  18. #include <sys/wait.h>
  19. #include <signal.h>
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <sstream>
  23. #include <map>
  24. #include <config.h>
  25. #include <apti18n.h>
  26. /*}}}*/
  27. using namespace std;
  28. // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
  29. // ---------------------------------------------------------------------
  30. /* */
  31. pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) : pkgPackageManager(Cache)
  32. {
  33. }
  34. /*}}}*/
  35. // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* */
  38. pkgDPkgPM::~pkgDPkgPM()
  39. {
  40. }
  41. /*}}}*/
  42. // DPkgPM::Install - Install a package /*{{{*/
  43. // ---------------------------------------------------------------------
  44. /* Add an install operation to the sequence list */
  45. bool pkgDPkgPM::Install(PkgIterator Pkg,string File)
  46. {
  47. if (File.empty() == true || Pkg.end() == true)
  48. return _error->Error("Internal Error, No file name for %s",Pkg.Name());
  49. List.push_back(Item(Item::Install,Pkg,File));
  50. return true;
  51. }
  52. /*}}}*/
  53. // DPkgPM::Configure - Configure a package /*{{{*/
  54. // ---------------------------------------------------------------------
  55. /* Add a configure operation to the sequence list */
  56. bool pkgDPkgPM::Configure(PkgIterator Pkg)
  57. {
  58. if (Pkg.end() == true)
  59. return false;
  60. List.push_back(Item(Item::Configure,Pkg));
  61. return true;
  62. }
  63. /*}}}*/
  64. // DPkgPM::Remove - Remove a package /*{{{*/
  65. // ---------------------------------------------------------------------
  66. /* Add a remove operation to the sequence list */
  67. bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge)
  68. {
  69. if (Pkg.end() == true)
  70. return false;
  71. if (Purge == true)
  72. List.push_back(Item(Item::Purge,Pkg));
  73. else
  74. List.push_back(Item(Item::Remove,Pkg));
  75. return true;
  76. }
  77. /*}}}*/
  78. // DPkgPM::RunScripts - Run a set of scripts /*{{{*/
  79. // ---------------------------------------------------------------------
  80. /* This looks for a list of script sto run from the configuration file,
  81. each one is run with system from a forked child. */
  82. bool pkgDPkgPM::RunScripts(const char *Cnf)
  83. {
  84. Configuration::Item const *Opts = _config->Tree(Cnf);
  85. if (Opts == 0 || Opts->Child == 0)
  86. return true;
  87. Opts = Opts->Child;
  88. // Fork for running the system calls
  89. pid_t Child = ExecFork();
  90. // This is the child
  91. if (Child == 0)
  92. {
  93. if (chdir("/tmp/") != 0)
  94. _exit(100);
  95. unsigned int Count = 1;
  96. for (; Opts != 0; Opts = Opts->Next, Count++)
  97. {
  98. if (Opts->Value.empty() == true)
  99. continue;
  100. if (system(Opts->Value.c_str()) != 0)
  101. _exit(100+Count);
  102. }
  103. _exit(0);
  104. }
  105. // Wait for the child
  106. int Status = 0;
  107. while (waitpid(Child,&Status,0) != Child)
  108. {
  109. if (errno == EINTR)
  110. continue;
  111. return _error->Errno("waitpid","Couldn't wait for subprocess");
  112. }
  113. // Restore sig int/quit
  114. signal(SIGQUIT,SIG_DFL);
  115. signal(SIGINT,SIG_DFL);
  116. // Check for an error code.
  117. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  118. {
  119. unsigned int Count = WEXITSTATUS(Status);
  120. if (Count > 100)
  121. {
  122. Count -= 100;
  123. for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--);
  124. _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str());
  125. }
  126. return _error->Error("Sub-process returned an error code");
  127. }
  128. return true;
  129. }
  130. /*}}}*/
  131. // DPkgPM::SendV2Pkgs - Send version 2 package info /*{{{*/
  132. // ---------------------------------------------------------------------
  133. /* This is part of the helper script communication interface, it sends
  134. very complete information down to the other end of the pipe.*/
  135. bool pkgDPkgPM::SendV2Pkgs(FILE *F)
  136. {
  137. fprintf(F,"VERSION 2\n");
  138. /* Write out all of the configuration directives by walking the
  139. configuration tree */
  140. const Configuration::Item *Top = _config->Tree(0);
  141. for (; Top != 0;)
  142. {
  143. if (Top->Value.empty() == false)
  144. {
  145. fprintf(F,"%s=%s\n",
  146. QuoteString(Top->FullTag(),"=\"\n").c_str(),
  147. QuoteString(Top->Value,"\n").c_str());
  148. }
  149. if (Top->Child != 0)
  150. {
  151. Top = Top->Child;
  152. continue;
  153. }
  154. while (Top != 0 && Top->Next == 0)
  155. Top = Top->Parent;
  156. if (Top != 0)
  157. Top = Top->Next;
  158. }
  159. fprintf(F,"\n");
  160. // Write out the package actions in order.
  161. for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
  162. {
  163. pkgDepCache::StateCache &S = Cache[I->Pkg];
  164. fprintf(F,"%s ",I->Pkg.Name());
  165. // Current version
  166. if (I->Pkg->CurrentVer == 0)
  167. fprintf(F,"- ");
  168. else
  169. fprintf(F,"%s ",I->Pkg.CurrentVer().VerStr());
  170. // Show the compare operator
  171. // Target version
  172. if (S.InstallVer != 0)
  173. {
  174. int Comp = 2;
  175. if (I->Pkg->CurrentVer != 0)
  176. Comp = S.InstVerIter(Cache).CompareVer(I->Pkg.CurrentVer());
  177. if (Comp < 0)
  178. fprintf(F,"> ");
  179. if (Comp == 0)
  180. fprintf(F,"= ");
  181. if (Comp > 0)
  182. fprintf(F,"< ");
  183. fprintf(F,"%s ",S.InstVerIter(Cache).VerStr());
  184. }
  185. else
  186. fprintf(F,"> - ");
  187. // Show the filename/operation
  188. if (I->Op == Item::Install)
  189. {
  190. // No errors here..
  191. if (I->File[0] != '/')
  192. fprintf(F,"**ERROR**\n");
  193. else
  194. fprintf(F,"%s\n",I->File.c_str());
  195. }
  196. if (I->Op == Item::Configure)
  197. fprintf(F,"**CONFIGURE**\n");
  198. if (I->Op == Item::Remove ||
  199. I->Op == Item::Purge)
  200. fprintf(F,"**REMOVE**\n");
  201. if (ferror(F) != 0)
  202. return false;
  203. }
  204. return true;
  205. }
  206. /*}}}*/
  207. // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/
  208. // ---------------------------------------------------------------------
  209. /* This looks for a list of scripts to run from the configuration file
  210. each one is run and is fed on standard input a list of all .deb files
  211. that are due to be installed. */
  212. bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf)
  213. {
  214. Configuration::Item const *Opts = _config->Tree(Cnf);
  215. if (Opts == 0 || Opts->Child == 0)
  216. return true;
  217. Opts = Opts->Child;
  218. unsigned int Count = 1;
  219. for (; Opts != 0; Opts = Opts->Next, Count++)
  220. {
  221. if (Opts->Value.empty() == true)
  222. continue;
  223. // Determine the protocol version
  224. string OptSec = Opts->Value;
  225. string::size_type Pos;
  226. if ((Pos = OptSec.find(' ')) == string::npos || Pos == 0)
  227. Pos = OptSec.length();
  228. OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos);
  229. unsigned int Version = _config->FindI(OptSec+"::Version",1);
  230. // Create the pipes
  231. int Pipes[2];
  232. if (pipe(Pipes) != 0)
  233. return _error->Errno("pipe","Failed to create IPC pipe to subprocess");
  234. SetCloseExec(Pipes[0],true);
  235. SetCloseExec(Pipes[1],true);
  236. // Purified Fork for running the script
  237. pid_t Process = ExecFork();
  238. if (Process == 0)
  239. {
  240. // Setup the FDs
  241. dup2(Pipes[0],STDIN_FILENO);
  242. SetCloseExec(STDOUT_FILENO,false);
  243. SetCloseExec(STDIN_FILENO,false);
  244. SetCloseExec(STDERR_FILENO,false);
  245. const char *Args[4];
  246. Args[0] = "/bin/sh";
  247. Args[1] = "-c";
  248. Args[2] = Opts->Value.c_str();
  249. Args[3] = 0;
  250. execv(Args[0],(char **)Args);
  251. _exit(100);
  252. }
  253. close(Pipes[0]);
  254. FILE *F = fdopen(Pipes[1],"w");
  255. if (F == 0)
  256. return _error->Errno("fdopen","Faild to open new FD");
  257. // Feed it the filenames.
  258. bool Die = false;
  259. if (Version <= 1)
  260. {
  261. for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
  262. {
  263. // Only deal with packages to be installed from .deb
  264. if (I->Op != Item::Install)
  265. continue;
  266. // No errors here..
  267. if (I->File[0] != '/')
  268. continue;
  269. /* Feed the filename of each package that is pending install
  270. into the pipe. */
  271. fprintf(F,"%s\n",I->File.c_str());
  272. if (ferror(F) != 0)
  273. {
  274. Die = true;
  275. break;
  276. }
  277. }
  278. }
  279. else
  280. Die = !SendV2Pkgs(F);
  281. fclose(F);
  282. // Clean up the sub process
  283. if (ExecWait(Process,Opts->Value.c_str()) == false)
  284. return _error->Error("Failure running script %s",Opts->Value.c_str());
  285. }
  286. return true;
  287. }
  288. /*}}}*/
  289. // DPkgPM::Go - Run the sequence /*{{{*/
  290. // ---------------------------------------------------------------------
  291. /* This globs the operations and calls dpkg
  292. *
  293. * If it is called with "OutStatusFd" set to a valid file descriptor
  294. * apt will report the install progress over this fd. It maps the
  295. * dpkg states a package goes through to human readable (and i10n-able)
  296. * names and calculates a percentage for each step.
  297. */
  298. bool pkgDPkgPM::Go(int OutStatusFd)
  299. {
  300. unsigned int MaxArgs = _config->FindI("Dpkg::MaxArgs",8*1024);
  301. unsigned int MaxArgBytes = _config->FindI("Dpkg::MaxArgBytes",32*1024);
  302. if (RunScripts("DPkg::Pre-Invoke") == false)
  303. return false;
  304. if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
  305. return false;
  306. // prepare the progress reporting
  307. int Done = 0;
  308. int Total = 0;
  309. // map the dpkg states to the operations that are performed
  310. // (this is sorted in the same way as Item::Ops)
  311. static const struct DpkgState DpkgStatesOpMap[][5] = {
  312. // Install operation
  313. {
  314. {"half-installed", N_("Preparing %s")},
  315. {"unpacked", N_("Unpacking %s") },
  316. {NULL, NULL}
  317. },
  318. // Configure operation
  319. {
  320. {"unpacked",N_("Preparing to configure %s") },
  321. {"half-configured", N_("Configuring %s") },
  322. { "installed", N_("Installed %s")},
  323. {NULL, NULL}
  324. },
  325. // Remove operation
  326. {
  327. {"half-configured", N_("Preparing for removal of %s")},
  328. {"half-installed", N_("Removing %s")},
  329. {"config-files", N_("Removed %s")},
  330. {NULL, NULL}
  331. },
  332. // Purge operation
  333. {
  334. {"config-files", N_("Preparing to completely remove %s")},
  335. {"not-installed", N_("Completely removed %s")},
  336. {NULL, NULL}
  337. },
  338. };
  339. // the dpkg states that the pkg will run through, the string is
  340. // the package, the vector contains the dpkg states that the package
  341. // will go through
  342. map<string,vector<struct DpkgState> > PackageOps;
  343. // the dpkg states that are already done; the string is the package
  344. // the int is the state that is already done (e.g. a package that is
  345. // going to be install is already in state "half-installed")
  346. map<string,int> PackageOpsDone;
  347. // init the PackageOps map, go over the list of packages that
  348. // that will be [installed|configured|removed|purged] and add
  349. // them to the PackageOps map (the dpkg states it goes through)
  350. // and the PackageOpsTranslations (human readable strings)
  351. for (vector<Item>::iterator I = List.begin(); I != List.end();I++)
  352. {
  353. string name = (*I).Pkg.Name();
  354. PackageOpsDone[name] = 0;
  355. for(int i=0; (DpkgStatesOpMap[(*I).Op][i]).state != NULL; i++)
  356. {
  357. PackageOps[name].push_back(DpkgStatesOpMap[(*I).Op][i]);
  358. Total++;
  359. }
  360. }
  361. // this loop is runs once per operation
  362. for (vector<Item>::iterator I = List.begin(); I != List.end();)
  363. {
  364. vector<Item>::iterator J = I;
  365. for (; J != List.end() && J->Op == I->Op; J++);
  366. // Generate the argument list
  367. const char *Args[MaxArgs + 50];
  368. if (J - I > (signed)MaxArgs)
  369. J = I + MaxArgs;
  370. unsigned int n = 0;
  371. unsigned long Size = 0;
  372. string Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
  373. Args[n++] = Tmp.c_str();
  374. Size += strlen(Args[n-1]);
  375. // Stick in any custom dpkg options
  376. Configuration::Item const *Opts = _config->Tree("DPkg::Options");
  377. if (Opts != 0)
  378. {
  379. Opts = Opts->Child;
  380. for (; Opts != 0; Opts = Opts->Next)
  381. {
  382. if (Opts->Value.empty() == true)
  383. continue;
  384. Args[n++] = Opts->Value.c_str();
  385. Size += Opts->Value.length();
  386. }
  387. }
  388. char status_fd_buf[20];
  389. int fd[2];
  390. pipe(fd);
  391. Args[n++] = "--status-fd";
  392. Size += strlen(Args[n-1]);
  393. snprintf(status_fd_buf,sizeof(status_fd_buf),"%i", fd[1]);
  394. Args[n++] = status_fd_buf;
  395. Size += strlen(Args[n-1]);
  396. switch (I->Op)
  397. {
  398. case Item::Remove:
  399. Args[n++] = "--force-depends";
  400. Size += strlen(Args[n-1]);
  401. Args[n++] = "--force-remove-essential";
  402. Size += strlen(Args[n-1]);
  403. Args[n++] = "--remove";
  404. Size += strlen(Args[n-1]);
  405. break;
  406. case Item::Purge:
  407. Args[n++] = "--force-depends";
  408. Size += strlen(Args[n-1]);
  409. Args[n++] = "--force-remove-essential";
  410. Size += strlen(Args[n-1]);
  411. Args[n++] = "--purge";
  412. Size += strlen(Args[n-1]);
  413. break;
  414. case Item::Configure:
  415. Args[n++] = "--configure";
  416. Size += strlen(Args[n-1]);
  417. break;
  418. case Item::Install:
  419. Args[n++] = "--unpack";
  420. Size += strlen(Args[n-1]);
  421. Args[n++] = "--auto-deconfigure";
  422. Size += strlen(Args[n-1]);
  423. break;
  424. }
  425. // Write in the file or package names
  426. if (I->Op == Item::Install)
  427. {
  428. for (;I != J && Size < MaxArgBytes; I++)
  429. {
  430. if (I->File[0] != '/')
  431. return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str());
  432. Args[n++] = I->File.c_str();
  433. Size += strlen(Args[n-1]);
  434. }
  435. }
  436. else
  437. {
  438. for (;I != J && Size < MaxArgBytes; I++)
  439. {
  440. Args[n++] = I->Pkg.Name();
  441. Size += strlen(Args[n-1]);
  442. }
  443. }
  444. Args[n] = 0;
  445. J = I;
  446. if (_config->FindB("Debug::pkgDPkgPM",false) == true)
  447. {
  448. for (unsigned int k = 0; k != n; k++)
  449. clog << Args[k] << ' ';
  450. clog << endl;
  451. continue;
  452. }
  453. cout << flush;
  454. clog << flush;
  455. cerr << flush;
  456. /* Mask off sig int/quit. We do this because dpkg also does when
  457. it forks scripts. What happens is that when you hit ctrl-c it sends
  458. it to all processes in the group. Since dpkg ignores the signal
  459. it doesn't die but we do! So we must also ignore it */
  460. sighandler_t old_SIGQUIT = signal(SIGQUIT,SIG_IGN);
  461. sighandler_t old_SIGINT = signal(SIGINT,SIG_IGN);
  462. // Fork dpkg
  463. pid_t Child;
  464. _config->Set("APT::Keep-Fds::",fd[1]);
  465. Child = ExecFork();
  466. // This is the child
  467. if (Child == 0)
  468. {
  469. close(fd[0]); // close the read end of the pipe
  470. if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
  471. _exit(100);
  472. if (_config->FindB("DPkg::FlushSTDIN",true) == true && isatty(STDIN_FILENO))
  473. {
  474. int Flags,dummy;
  475. if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0)
  476. _exit(100);
  477. // Discard everything in stdin before forking dpkg
  478. if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0)
  479. _exit(100);
  480. while (read(STDIN_FILENO,&dummy,1) == 1);
  481. if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
  482. _exit(100);
  483. }
  484. /* No Job Control Stop Env is a magic dpkg var that prevents it
  485. from using sigstop */
  486. putenv("DPKG_NO_TSTP=yes");
  487. execvp(Args[0],(char **)Args);
  488. cerr << "Could not exec dpkg!" << endl;
  489. _exit(100);
  490. }
  491. // clear the Keep-Fd again
  492. _config->Clear("APT::Keep-Fds",fd[1]);
  493. // Wait for dpkg
  494. int Status = 0;
  495. // we read from dpkg here
  496. int _dpkgin = fd[0];
  497. fcntl(_dpkgin, F_SETFL, O_NONBLOCK);
  498. close(fd[1]); // close the write end of the pipe
  499. // the read buffers for the communication with dpkg
  500. char line[1024] = {0,};
  501. char buf[2] = {0,0};
  502. // the result of the waitpid call
  503. int res;
  504. while ((res=waitpid(Child,&Status, WNOHANG)) != Child) {
  505. if(res < 0) {
  506. // FIXME: move this to a function or something, looks ugly here
  507. // error handling, waitpid returned -1
  508. if (errno == EINTR)
  509. continue;
  510. RunScripts("DPkg::Post-Invoke");
  511. // Restore sig int/quit
  512. signal(SIGQUIT,old_SIGQUIT);
  513. signal(SIGINT,old_SIGINT);
  514. return _error->Errno("waitpid","Couldn't wait for subprocess");
  515. }
  516. // read a single char, make sure that the read can't block
  517. // (otherwise we may leave zombies)
  518. int len = read(_dpkgin, buf, 1);
  519. // nothing to read, wait a bit for more
  520. if(len <= 0)
  521. {
  522. usleep(1000);
  523. continue;
  524. }
  525. // sanity check (should never happen)
  526. if(strlen(line) >= sizeof(line)-10)
  527. {
  528. _error->Error("got a overlong line from dpkg: '%s'",line);
  529. line[0]=0;
  530. }
  531. // append to line, check if we got a complete line
  532. strcat(line, buf);
  533. if(buf[0] != '\n')
  534. continue;
  535. if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
  536. std::clog << "got from dpkg '" << line << "'" << std::endl;
  537. // the status we output
  538. ostringstream status;
  539. /* dpkg sends strings like this:
  540. 'status: <pkg>: <pkg qstate>'
  541. errors look like this:
  542. '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
  543. and conffile-prompt like this
  544. 'status: conffile-prompt: conffile : 'current-conffile' 'new-conffile' useredited distedited
  545. */
  546. char* list[5];
  547. // dpkg sends multiline error messages sometimes (see
  548. // #374195 for a example. we should support this by
  549. // either patching dpkg to not send multiline over the
  550. // statusfd or by rewriting the code here to deal with
  551. // it. for now we just ignore it and not crash
  552. TokSplitString(':', line, list, sizeof(list)/sizeof(list[0]));
  553. char *pkg = list[1];
  554. char *action = _strstrip(list[2]);
  555. if( pkg == NULL || action == NULL)
  556. {
  557. if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
  558. std::clog << "ignoring line: not enough ':'" << std::endl;
  559. // reset the line buffer
  560. line[0]=0;
  561. continue;
  562. }
  563. if(strncmp(action,"error",strlen("error")) == 0)
  564. {
  565. status << "pmerror:" << list[1]
  566. << ":" << (Done/float(Total)*100.0)
  567. << ":" << list[3]
  568. << endl;
  569. if(OutStatusFd > 0)
  570. write(OutStatusFd, status.str().c_str(), status.str().size());
  571. line[0]=0;
  572. if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
  573. std::clog << "send: '" << status.str() << "'" << endl;
  574. continue;
  575. }
  576. if(strncmp(action,"conffile",strlen("conffile")) == 0)
  577. {
  578. status << "pmconffile:" << list[1]
  579. << ":" << (Done/float(Total)*100.0)
  580. << ":" << list[3]
  581. << endl;
  582. if(OutStatusFd > 0)
  583. write(OutStatusFd, status.str().c_str(), status.str().size());
  584. line[0]=0;
  585. if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
  586. std::clog << "send: '" << status.str() << "'" << endl;
  587. continue;
  588. }
  589. vector<struct DpkgState> &states = PackageOps[pkg];
  590. const char *next_action = NULL;
  591. if(PackageOpsDone[pkg] < states.size())
  592. next_action = states[PackageOpsDone[pkg]].state;
  593. // check if the package moved to the next dpkg state
  594. if(next_action && (strcmp(action, next_action) == 0))
  595. {
  596. // only read the translation if there is actually a next
  597. // action
  598. const char *translation = _(states[PackageOpsDone[pkg]].str);
  599. char s[200];
  600. snprintf(s, sizeof(s), translation, pkg);
  601. // we moved from one dpkg state to a new one, report that
  602. PackageOpsDone[pkg]++;
  603. Done++;
  604. // build the status str
  605. status << "pmstatus:" << pkg
  606. << ":" << (Done/float(Total)*100.0)
  607. << ":" << s
  608. << endl;
  609. if(OutStatusFd > 0)
  610. write(OutStatusFd, status.str().c_str(), status.str().size());
  611. if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
  612. std::clog << "send: '" << status.str() << "'" << endl;
  613. }
  614. if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
  615. std::clog << "(parsed from dpkg) pkg: " << pkg
  616. << " action: " << action << endl;
  617. // reset the line buffer
  618. line[0]=0;
  619. }
  620. close(_dpkgin);
  621. // Restore sig int/quit
  622. signal(SIGQUIT,old_SIGQUIT);
  623. signal(SIGINT,old_SIGINT);
  624. // Check for an error code.
  625. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  626. {
  627. // if it was set to "keep-dpkg-runing" then we won't return
  628. // here but keep the loop going and just report it as a error
  629. // for later
  630. bool stopOnError = _config->FindB("Dpkg::StopOnError",true);
  631. if(stopOnError)
  632. RunScripts("DPkg::Post-Invoke");
  633. if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV)
  634. _error->Error("Sub-process %s received a segmentation fault.",Args[0]);
  635. else if (WIFEXITED(Status) != 0)
  636. _error->Error("Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status));
  637. else
  638. _error->Error("Sub-process %s exited unexpectedly",Args[0]);
  639. if(stopOnError)
  640. return false;
  641. }
  642. }
  643. if (RunScripts("DPkg::Post-Invoke") == false)
  644. return false;
  645. return true;
  646. }
  647. /*}}}*/
  648. // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
  649. // ---------------------------------------------------------------------
  650. /* */
  651. void pkgDPkgPM::Reset()
  652. {
  653. List.erase(List.begin(),List.end());
  654. }
  655. /*}}}*/