dpkgpm.cc 36 KB

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