dpkgpm.cc 58 KB

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