dpkgpm.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: dpkgpm.cc,v 1.21 2001/05/07 05:35:46 jgg 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. if (Die == true)
  283. {
  284. kill(Process,SIGINT);
  285. ExecWait(Process,Opts->Value.c_str(),true);
  286. return _error->Error("Failure running script %s",Opts->Value.c_str());
  287. }
  288. // Clean up the sub process
  289. if (ExecWait(Process,Opts->Value.c_str()) == false)
  290. return _error->Error("Failure running script %s",Opts->Value.c_str());
  291. }
  292. return true;
  293. }
  294. /*}}}*/
  295. // DPkgPM::Go - Run the sequence /*{{{*/
  296. // ---------------------------------------------------------------------
  297. /* This globs the operations and calls dpkg */
  298. bool pkgDPkgPM::Go()
  299. {
  300. if (RunScripts("DPkg::Pre-Invoke") == false)
  301. return false;
  302. if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
  303. return false;
  304. for (vector<Item>::iterator I = List.begin(); I != List.end();)
  305. {
  306. vector<Item>::iterator J = I;
  307. for (; J != List.end() && J->Op == I->Op; J++);
  308. // Generate the argument list
  309. const char *Args[400];
  310. if (J - I > 350)
  311. J = I + 350;
  312. unsigned int n = 0;
  313. unsigned long Size = 0;
  314. Args[n++] = _config->Find("Dir::Bin::dpkg","dpkg").c_str();
  315. Size += strlen(Args[n-1]);
  316. // Stick in any custom dpkg options
  317. Configuration::Item const *Opts = _config->Tree("DPkg::Options");
  318. if (Opts != 0)
  319. {
  320. Opts = Opts->Child;
  321. for (; Opts != 0; Opts = Opts->Next)
  322. {
  323. if (Opts->Value.empty() == true)
  324. continue;
  325. Args[n++] = Opts->Value.c_str();
  326. Size += Opts->Value.length();
  327. }
  328. }
  329. switch (I->Op)
  330. {
  331. case Item::Remove:
  332. Args[n++] = "--force-depends";
  333. Size += strlen(Args[n-1]);
  334. Args[n++] = "--force-remove-essential";
  335. Size += strlen(Args[n-1]);
  336. Args[n++] = "--remove";
  337. Size += strlen(Args[n-1]);
  338. break;
  339. case Item::Purge:
  340. Args[n++] = "--force-depends";
  341. Size += strlen(Args[n-1]);
  342. Args[n++] = "--force-remove-essential";
  343. Size += strlen(Args[n-1]);
  344. Args[n++] = "--purge";
  345. Size += strlen(Args[n-1]);
  346. break;
  347. case Item::Configure:
  348. Args[n++] = "--configure";
  349. Size += strlen(Args[n-1]);
  350. break;
  351. case Item::Install:
  352. Args[n++] = "--unpack";
  353. Size += strlen(Args[n-1]);
  354. break;
  355. }
  356. // Write in the file or package names
  357. if (I->Op == Item::Install)
  358. {
  359. for (;I != J && Size < 1024; I++)
  360. {
  361. if (I->File[0] != '/')
  362. return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str());
  363. Args[n++] = I->File.c_str();
  364. Size += strlen(Args[n-1]);
  365. }
  366. }
  367. else
  368. {
  369. for (;I != J && Size < 1024; I++)
  370. {
  371. Args[n++] = I->Pkg.Name();
  372. Size += strlen(Args[n-1]);
  373. }
  374. }
  375. Args[n] = 0;
  376. J = I;
  377. if (_config->FindB("Debug::pkgDPkgPM",false) == true)
  378. {
  379. for (unsigned int k = 0; k != n; k++)
  380. clog << Args[k] << ' ';
  381. clog << endl;
  382. continue;
  383. }
  384. cout << flush;
  385. clog << flush;
  386. cerr << flush;
  387. /* Mask off sig int/quit. We do this because dpkg also does when
  388. it forks scripts. What happens is that when you hit ctrl-c it sends
  389. it to all processes in the group. Since dpkg ignores the signal
  390. it doesn't die but we do! So we must also ignore it */
  391. signal(SIGQUIT,SIG_IGN);
  392. signal(SIGINT,SIG_IGN);
  393. // Fork dpkg
  394. pid_t Child = ExecFork();
  395. // This is the child
  396. if (Child == 0)
  397. {
  398. if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
  399. _exit(100);
  400. if (_config->FindB("DPkg::FlushSTDIN",true) == true)
  401. {
  402. int Flags,dummy;
  403. if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0)
  404. _exit(100);
  405. // Discard everything in stdin before forking dpkg
  406. if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0)
  407. _exit(100);
  408. while (read(STDIN_FILENO,&dummy,1) == 1);
  409. if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
  410. _exit(100);
  411. }
  412. /* No Job Control Stop Env is a magic dpkg var that prevents it
  413. from using sigstop */
  414. putenv("DPKG_NO_TSTP=yes");
  415. execvp(Args[0],(char **)Args);
  416. cerr << "Could not exec dpkg!" << endl;
  417. _exit(100);
  418. }
  419. // Wait for dpkg
  420. int Status = 0;
  421. while (waitpid(Child,&Status,0) != Child)
  422. {
  423. if (errno == EINTR)
  424. continue;
  425. RunScripts("DPkg::Post-Invoke");
  426. return _error->Errno("waitpid","Couldn't wait for subprocess");
  427. }
  428. // Restore sig int/quit
  429. signal(SIGQUIT,SIG_DFL);
  430. signal(SIGINT,SIG_DFL);
  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. /*}}}*/