dpkgpm.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. #ifdef __GNUG__
  10. #pragma implementation "apt-pkg/dpkgpm.h"
  11. #endif
  12. #include <apt-pkg/dpkgpm.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/configuration.h>
  15. #include <apt-pkg/depcache.h>
  16. #include <apt-pkg/strutl.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include <fcntl.h>
  20. #include <sys/types.h>
  21. #include <sys/wait.h>
  22. #include <signal.h>
  23. #include <errno.h>
  24. #include <stdio.h>
  25. #include <iostream>
  26. /*}}}*/
  27. using namespace std;
  28. // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
  29. // ---------------------------------------------------------------------
  30. /* */
  31. pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) : pkgPackageManager(Cache)
  32. {
  33. }
  34. /*}}}*/
  35. // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* */
  38. pkgDPkgPM::~pkgDPkgPM()
  39. {
  40. }
  41. /*}}}*/
  42. // DPkgPM::Install - Install a package /*{{{*/
  43. // ---------------------------------------------------------------------
  44. /* Add an install operation to the sequence list */
  45. bool pkgDPkgPM::Install(PkgIterator Pkg,string File)
  46. {
  47. if (File.empty() == true || Pkg.end() == true)
  48. return _error->Error("Internal Error, No file name for %s",Pkg.Name());
  49. List.push_back(Item(Item::Install,Pkg,File));
  50. return true;
  51. }
  52. /*}}}*/
  53. // DPkgPM::Configure - Configure a package /*{{{*/
  54. // ---------------------------------------------------------------------
  55. /* Add a configure operation to the sequence list */
  56. bool pkgDPkgPM::Configure(PkgIterator Pkg)
  57. {
  58. if (Pkg.end() == true)
  59. return false;
  60. List.push_back(Item(Item::Configure,Pkg));
  61. return true;
  62. }
  63. /*}}}*/
  64. // DPkgPM::Remove - Remove a package /*{{{*/
  65. // ---------------------------------------------------------------------
  66. /* Add a remove operation to the sequence list */
  67. bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge)
  68. {
  69. if (Pkg.end() == true)
  70. return false;
  71. if (Purge == true)
  72. List.push_back(Item(Item::Purge,Pkg));
  73. else
  74. List.push_back(Item(Item::Remove,Pkg));
  75. return true;
  76. }
  77. /*}}}*/
  78. // DPkgPM::RunScripts - Run a set of scripts /*{{{*/
  79. // ---------------------------------------------------------------------
  80. /* This looks for a list of script sto run from the configuration file,
  81. each one is run with system from a forked child. */
  82. bool pkgDPkgPM::RunScripts(const char *Cnf)
  83. {
  84. Configuration::Item const *Opts = _config->Tree(Cnf);
  85. if (Opts == 0 || Opts->Child == 0)
  86. return true;
  87. Opts = Opts->Child;
  88. // Fork for running the system calls
  89. pid_t Child = ExecFork();
  90. // This is the child
  91. if (Child == 0)
  92. {
  93. if (chdir("/tmp/") != 0)
  94. _exit(100);
  95. unsigned int Count = 1;
  96. for (; Opts != 0; Opts = Opts->Next, Count++)
  97. {
  98. if (Opts->Value.empty() == true)
  99. continue;
  100. if (system(Opts->Value.c_str()) != 0)
  101. _exit(100+Count);
  102. }
  103. _exit(0);
  104. }
  105. // Wait for the child
  106. int Status = 0;
  107. while (waitpid(Child,&Status,0) != Child)
  108. {
  109. if (errno == EINTR)
  110. continue;
  111. return _error->Errno("waitpid","Couldn't wait for subprocess");
  112. }
  113. // Restore sig int/quit
  114. signal(SIGQUIT,SIG_DFL);
  115. signal(SIGINT,SIG_DFL);
  116. // Check for an error code.
  117. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  118. {
  119. unsigned int Count = WEXITSTATUS(Status);
  120. if (Count > 100)
  121. {
  122. Count -= 100;
  123. for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--);
  124. _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str());
  125. }
  126. return _error->Error("Sub-process returned an error code");
  127. }
  128. return true;
  129. }
  130. /*}}}*/
  131. // DPkgPM::SendV2Pkgs - Send version 2 package info /*{{{*/
  132. // ---------------------------------------------------------------------
  133. /* This is part of the helper script communication interface, it sends
  134. very complete information down to the other end of the pipe.*/
  135. bool pkgDPkgPM::SendV2Pkgs(FILE *F)
  136. {
  137. fprintf(F,"VERSION 2\n");
  138. /* Write out all of the configuration directives by walking the
  139. configuration tree */
  140. const Configuration::Item *Top = _config->Tree(0);
  141. for (; Top != 0;)
  142. {
  143. if (Top->Value.empty() == false)
  144. {
  145. fprintf(F,"%s=%s\n",
  146. QuoteString(Top->FullTag(),"=\"\n").c_str(),
  147. QuoteString(Top->Value,"\n").c_str());
  148. }
  149. if (Top->Child != 0)
  150. {
  151. Top = Top->Child;
  152. continue;
  153. }
  154. while (Top != 0 && Top->Next == 0)
  155. Top = Top->Parent;
  156. if (Top != 0)
  157. Top = Top->Next;
  158. }
  159. fprintf(F,"\n");
  160. // Write out the package actions in order.
  161. for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
  162. {
  163. pkgDepCache::StateCache &S = Cache[I->Pkg];
  164. fprintf(F,"%s ",I->Pkg.Name());
  165. // Current version
  166. if (I->Pkg->CurrentVer == 0)
  167. fprintf(F,"- ");
  168. else
  169. fprintf(F,"%s ",I->Pkg.CurrentVer().VerStr());
  170. // Show the compare operator
  171. // Target version
  172. if (S.InstallVer != 0)
  173. {
  174. int Comp = 2;
  175. if (I->Pkg->CurrentVer != 0)
  176. Comp = S.InstVerIter(Cache).CompareVer(I->Pkg.CurrentVer());
  177. if (Comp < 0)
  178. fprintf(F,"> ");
  179. if (Comp == 0)
  180. fprintf(F,"= ");
  181. if (Comp > 0)
  182. fprintf(F,"< ");
  183. fprintf(F,"%s ",S.InstVerIter(Cache).VerStr());
  184. }
  185. else
  186. fprintf(F,"> - ");
  187. // Show the filename/operation
  188. if (I->Op == Item::Install)
  189. {
  190. // No errors here..
  191. if (I->File[0] != '/')
  192. fprintf(F,"**ERROR**\n");
  193. else
  194. fprintf(F,"%s\n",I->File.c_str());
  195. }
  196. if (I->Op == Item::Configure)
  197. fprintf(F,"**CONFIGURE**\n");
  198. if (I->Op == Item::Remove ||
  199. I->Op == Item::Purge)
  200. fprintf(F,"**REMOVE**\n");
  201. if (ferror(F) != 0)
  202. return false;
  203. }
  204. return true;
  205. }
  206. /*}}}*/
  207. // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/
  208. // ---------------------------------------------------------------------
  209. /* This looks for a list of scripts to run from the configuration file
  210. each one is run and is fed on standard input a list of all .deb files
  211. that are due to be installed. */
  212. bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf)
  213. {
  214. Configuration::Item const *Opts = _config->Tree(Cnf);
  215. if (Opts == 0 || Opts->Child == 0)
  216. return true;
  217. Opts = Opts->Child;
  218. unsigned int Count = 1;
  219. for (; Opts != 0; Opts = Opts->Next, Count++)
  220. {
  221. if (Opts->Value.empty() == true)
  222. continue;
  223. // Determine the protocol version
  224. string OptSec = Opts->Value;
  225. string::size_type Pos;
  226. if ((Pos = OptSec.find(' ')) == string::npos || Pos == 0)
  227. Pos = OptSec.length();
  228. OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos);
  229. unsigned int Version = _config->FindI(OptSec+"::Version",1);
  230. // Create the pipes
  231. int Pipes[2];
  232. if (pipe(Pipes) != 0)
  233. return _error->Errno("pipe","Failed to create IPC pipe to subprocess");
  234. SetCloseExec(Pipes[0],true);
  235. SetCloseExec(Pipes[1],true);
  236. // Purified Fork for running the script
  237. pid_t Process = ExecFork();
  238. if (Process == 0)
  239. {
  240. // Setup the FDs
  241. dup2(Pipes[0],STDIN_FILENO);
  242. SetCloseExec(STDOUT_FILENO,false);
  243. SetCloseExec(STDIN_FILENO,false);
  244. SetCloseExec(STDERR_FILENO,false);
  245. const char *Args[4];
  246. Args[0] = "/bin/sh";
  247. Args[1] = "-c";
  248. Args[2] = Opts->Value.c_str();
  249. Args[3] = 0;
  250. execv(Args[0],(char **)Args);
  251. _exit(100);
  252. }
  253. close(Pipes[0]);
  254. FILE *F = fdopen(Pipes[1],"w");
  255. if (F == 0)
  256. return _error->Errno("fdopen","Faild to open new FD");
  257. // Feed it the filenames.
  258. bool Die = false;
  259. if (Version <= 1)
  260. {
  261. for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
  262. {
  263. // Only deal with packages to be installed from .deb
  264. if (I->Op != Item::Install)
  265. continue;
  266. // No errors here..
  267. if (I->File[0] != '/')
  268. continue;
  269. /* Feed the filename of each package that is pending install
  270. into the pipe. */
  271. fprintf(F,"%s\n",I->File.c_str());
  272. if (ferror(F) != 0)
  273. {
  274. Die = true;
  275. break;
  276. }
  277. }
  278. }
  279. else
  280. Die = !SendV2Pkgs(F);
  281. fclose(F);
  282. // Clean up the sub process
  283. if (ExecWait(Process,Opts->Value.c_str()) == false)
  284. return _error->Error("Failure running script %s",Opts->Value.c_str());
  285. }
  286. return true;
  287. }
  288. /*}}}*/
  289. // DPkgPM::Go - Run the sequence /*{{{*/
  290. // ---------------------------------------------------------------------
  291. /* This globs the operations and calls dpkg */
  292. bool pkgDPkgPM::Go()
  293. {
  294. unsigned int MaxArgs = _config->FindI("Dpkg::MaxArgs",350);
  295. unsigned int MaxArgBytes = _config->FindI("Dpkg::MaxArgBytes",8192);
  296. if (RunScripts("DPkg::Pre-Invoke") == false)
  297. return false;
  298. if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
  299. return false;
  300. for (vector<Item>::iterator I = List.begin(); I != List.end();)
  301. {
  302. vector<Item>::iterator J = I;
  303. for (; J != List.end() && J->Op == I->Op; J++);
  304. // Generate the argument list
  305. const char *Args[MaxArgs + 50];
  306. if (J - I > (signed)MaxArgs)
  307. J = I + MaxArgs;
  308. unsigned int n = 0;
  309. unsigned long Size = 0;
  310. string Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
  311. Args[n++] = Tmp.c_str();
  312. Size += strlen(Args[n-1]);
  313. // Stick in any custom dpkg options
  314. Configuration::Item const *Opts = _config->Tree("DPkg::Options");
  315. if (Opts != 0)
  316. {
  317. Opts = Opts->Child;
  318. for (; Opts != 0; Opts = Opts->Next)
  319. {
  320. if (Opts->Value.empty() == true)
  321. continue;
  322. Args[n++] = Opts->Value.c_str();
  323. Size += Opts->Value.length();
  324. }
  325. }
  326. switch (I->Op)
  327. {
  328. case Item::Remove:
  329. Args[n++] = "--force-depends";
  330. Size += strlen(Args[n-1]);
  331. Args[n++] = "--force-remove-essential";
  332. Size += strlen(Args[n-1]);
  333. Args[n++] = "--remove";
  334. Size += strlen(Args[n-1]);
  335. break;
  336. case Item::Purge:
  337. Args[n++] = "--force-depends";
  338. Size += strlen(Args[n-1]);
  339. Args[n++] = "--force-remove-essential";
  340. Size += strlen(Args[n-1]);
  341. Args[n++] = "--purge";
  342. Size += strlen(Args[n-1]);
  343. break;
  344. case Item::Configure:
  345. Args[n++] = "--configure";
  346. Size += strlen(Args[n-1]);
  347. break;
  348. case Item::Install:
  349. Args[n++] = "--unpack";
  350. Size += strlen(Args[n-1]);
  351. break;
  352. }
  353. // Write in the file or package names
  354. if (I->Op == Item::Install)
  355. {
  356. for (;I != J && Size < MaxArgBytes; I++)
  357. {
  358. if (I->File[0] != '/')
  359. return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str());
  360. Args[n++] = I->File.c_str();
  361. Size += strlen(Args[n-1]);
  362. }
  363. }
  364. else
  365. {
  366. for (;I != J && Size < MaxArgBytes; I++)
  367. {
  368. Args[n++] = I->Pkg.Name();
  369. Size += strlen(Args[n-1]);
  370. }
  371. }
  372. Args[n] = 0;
  373. J = I;
  374. if (_config->FindB("Debug::pkgDPkgPM",false) == true)
  375. {
  376. for (unsigned int k = 0; k != n; k++)
  377. clog << Args[k] << ' ';
  378. clog << endl;
  379. continue;
  380. }
  381. cout << flush;
  382. clog << flush;
  383. cerr << flush;
  384. /* Mask off sig int/quit. We do this because dpkg also does when
  385. it forks scripts. What happens is that when you hit ctrl-c it sends
  386. it to all processes in the group. Since dpkg ignores the signal
  387. it doesn't die but we do! So we must also ignore it */
  388. sighandler_t old_SIGQUIT = signal(SIGQUIT,SIG_IGN);
  389. sighandler_t old_SIGINT = signal(SIGINT,SIG_IGN);
  390. // Fork dpkg
  391. pid_t Child = ExecFork();
  392. // This is the child
  393. if (Child == 0)
  394. {
  395. if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
  396. _exit(100);
  397. if (_config->FindB("DPkg::FlushSTDIN",true) == true && isatty(STDIN_FILENO))
  398. {
  399. int Flags,dummy;
  400. if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0)
  401. _exit(100);
  402. // Discard everything in stdin before forking dpkg
  403. if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0)
  404. _exit(100);
  405. while (read(STDIN_FILENO,&dummy,1) == 1);
  406. if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
  407. _exit(100);
  408. }
  409. /* No Job Control Stop Env is a magic dpkg var that prevents it
  410. from using sigstop */
  411. putenv("DPKG_NO_TSTP=yes");
  412. execvp(Args[0],(char **)Args);
  413. cerr << "Could not exec dpkg!" << endl;
  414. _exit(100);
  415. }
  416. // Wait for dpkg
  417. int Status = 0;
  418. while (waitpid(Child,&Status,0) != Child)
  419. {
  420. if (errno == EINTR)
  421. continue;
  422. RunScripts("DPkg::Post-Invoke");
  423. // Restore sig int/quit
  424. signal(SIGQUIT,old_SIGQUIT);
  425. signal(SIGINT,old_SIGINT);
  426. return _error->Errno("waitpid","Couldn't wait for subprocess");
  427. }
  428. // Restore sig int/quit
  429. signal(SIGQUIT,old_SIGQUIT);
  430. signal(SIGINT,old_SIGINT);
  431. // Check for an error code.
  432. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  433. {
  434. RunScripts("DPkg::Post-Invoke");
  435. if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV)
  436. return _error->Error("Sub-process %s received a segmentation fault.",Args[0]);
  437. if (WIFEXITED(Status) != 0)
  438. return _error->Error("Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status));
  439. return _error->Error("Sub-process %s exited unexpectedly",Args[0]);
  440. }
  441. }
  442. if (RunScripts("DPkg::Post-Invoke") == false)
  443. return false;
  444. return true;
  445. }
  446. /*}}}*/
  447. // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
  448. // ---------------------------------------------------------------------
  449. /* */
  450. void pkgDPkgPM::Reset()
  451. {
  452. List.erase(List.begin(),List.end());
  453. }
  454. /*}}}*/