dpkgpm.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. // Description /*{{{*/
  2. // $Id: dpkgpm.cc,v 1.23 2001/05/27 04:42:19 jgg Exp $
  3. /* ######################################################################
  4. DPKG Package Manager - Provide an interface to dpkg
  5. ##################################################################### */
  6. /*}}}*/
  7. // Includes /*{{{*/
  8. #ifdef __GNUG__
  9. #pragma implementation "apt-pkg/dpkgpm.h"
  10. #endif
  11. #include <apt-pkg/dpkgpm.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/configuration.h>
  14. #include <apt-pkg/depcache.h>
  15. #include <apt-pkg/strutl.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <fcntl.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <signal.h>
  22. #include <errno.h>
  23. #include <stdio.h>
  24. #include <iostream>
  25. /*}}}*/
  26. using namespace std;
  27. // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
  28. // ---------------------------------------------------------------------
  29. /* */
  30. pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) : pkgPackageManager(Cache)
  31. {
  32. }
  33. /*}}}*/
  34. // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
  35. // ---------------------------------------------------------------------
  36. /* */
  37. pkgDPkgPM::~pkgDPkgPM()
  38. {
  39. }
  40. /*}}}*/
  41. // DPkgPM::Install - Install a package /*{{{*/
  42. // ---------------------------------------------------------------------
  43. /* Add an install operation to the sequence list */
  44. bool pkgDPkgPM::Install(PkgIterator Pkg,string File)
  45. {
  46. if (File.empty() == true || Pkg.end() == true)
  47. return _error->Error("Internal Error, No file name for %s",Pkg.Name());
  48. List.push_back(Item(Item::Install,Pkg,File));
  49. return true;
  50. }
  51. /*}}}*/
  52. // DPkgPM::Configure - Configure a package /*{{{*/
  53. // ---------------------------------------------------------------------
  54. /* Add a configure operation to the sequence list */
  55. bool pkgDPkgPM::Configure(PkgIterator Pkg)
  56. {
  57. if (Pkg.end() == true)
  58. return false;
  59. List.push_back(Item(Item::Configure,Pkg));
  60. return true;
  61. }
  62. /*}}}*/
  63. // DPkgPM::Remove - Remove a package /*{{{*/
  64. // ---------------------------------------------------------------------
  65. /* Add a remove operation to the sequence list */
  66. bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge)
  67. {
  68. if (Pkg.end() == true)
  69. return false;
  70. if (Purge == true)
  71. List.push_back(Item(Item::Purge,Pkg));
  72. else
  73. List.push_back(Item(Item::Remove,Pkg));
  74. return true;
  75. }
  76. /*}}}*/
  77. // DPkgPM::RunScripts - Run a set of scripts /*{{{*/
  78. // ---------------------------------------------------------------------
  79. /* This looks for a list of script sto run from the configuration file,
  80. each one is run with system from a forked child. */
  81. bool pkgDPkgPM::RunScripts(const char *Cnf)
  82. {
  83. Configuration::Item const *Opts = _config->Tree(Cnf);
  84. if (Opts == 0 || Opts->Child == 0)
  85. return true;
  86. Opts = Opts->Child;
  87. // Fork for running the system calls
  88. pid_t Child = ExecFork();
  89. // This is the child
  90. if (Child == 0)
  91. {
  92. if (chdir("/tmp/") != 0)
  93. _exit(100);
  94. unsigned int Count = 1;
  95. for (; Opts != 0; Opts = Opts->Next, Count++)
  96. {
  97. if (Opts->Value.empty() == true)
  98. continue;
  99. if (system(Opts->Value.c_str()) != 0)
  100. _exit(100+Count);
  101. }
  102. _exit(0);
  103. }
  104. // Wait for the child
  105. int Status = 0;
  106. while (waitpid(Child,&Status,0) != Child)
  107. {
  108. if (errno == EINTR)
  109. continue;
  110. return _error->Errno("waitpid","Couldn't wait for subprocess");
  111. }
  112. // Restore sig int/quit
  113. signal(SIGQUIT,SIG_DFL);
  114. signal(SIGINT,SIG_DFL);
  115. // Check for an error code.
  116. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  117. {
  118. unsigned int Count = WEXITSTATUS(Status);
  119. if (Count > 100)
  120. {
  121. Count -= 100;
  122. for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--);
  123. _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str());
  124. }
  125. return _error->Error("Sub-process returned an error code");
  126. }
  127. return true;
  128. }
  129. /*}}}*/
  130. // DPkgPM::SendV2Pkgs - Send version 2 package info /*{{{*/
  131. // ---------------------------------------------------------------------
  132. /* This is part of the helper script communication interface, it sends
  133. very complete information down to the other end of the pipe.*/
  134. bool pkgDPkgPM::SendV2Pkgs(FILE *F)
  135. {
  136. fprintf(F,"VERSION 2\n");
  137. /* Write out all of the configuration directives by walking the
  138. configuration tree */
  139. const Configuration::Item *Top = _config->Tree(0);
  140. for (; Top != 0;)
  141. {
  142. if (Top->Value.empty() == false)
  143. {
  144. fprintf(F,"%s=%s\n",
  145. QuoteString(Top->FullTag(),"=\"\n").c_str(),
  146. QuoteString(Top->Value,"\n").c_str());
  147. }
  148. if (Top->Child != 0)
  149. {
  150. Top = Top->Child;
  151. continue;
  152. }
  153. while (Top != 0 && Top->Next == 0)
  154. Top = Top->Parent;
  155. if (Top != 0)
  156. Top = Top->Next;
  157. }
  158. fprintf(F,"\n");
  159. // Write out the package actions in order.
  160. for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
  161. {
  162. pkgDepCache::StateCache &S = Cache[I->Pkg];
  163. fprintf(F,"%s ",I->Pkg.Name());
  164. // Current version
  165. if (I->Pkg->CurrentVer == 0)
  166. fprintf(F,"- ");
  167. else
  168. fprintf(F,"%s ",I->Pkg.CurrentVer().VerStr());
  169. // Show the compare operator
  170. // Target version
  171. if (S.InstallVer != 0)
  172. {
  173. int Comp = 2;
  174. if (I->Pkg->CurrentVer != 0)
  175. Comp = S.InstVerIter(Cache).CompareVer(I->Pkg.CurrentVer());
  176. if (Comp < 0)
  177. fprintf(F,"> ");
  178. if (Comp == 0)
  179. fprintf(F,"= ");
  180. if (Comp > 0)
  181. fprintf(F,"< ");
  182. fprintf(F,"%s ",S.InstVerIter(Cache).VerStr());
  183. }
  184. else
  185. fprintf(F,"> - ");
  186. // Show the filename/operation
  187. if (I->Op == Item::Install)
  188. {
  189. // No errors here..
  190. if (I->File[0] != '/')
  191. fprintf(F,"**ERROR**\n");
  192. else
  193. fprintf(F,"%s\n",I->File.c_str());
  194. }
  195. if (I->Op == Item::Configure)
  196. fprintf(F,"**CONFIGURE**\n");
  197. if (I->Op == Item::Remove ||
  198. I->Op == Item::Purge)
  199. fprintf(F,"**REMOVE**\n");
  200. if (ferror(F) != 0)
  201. return false;
  202. }
  203. return true;
  204. }
  205. /*}}}*/
  206. // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/
  207. // ---------------------------------------------------------------------
  208. /* This looks for a list of scripts to run from the configuration file
  209. each one is run and is fed on standard input a list of all .deb files
  210. that are due to be installed. */
  211. bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf)
  212. {
  213. Configuration::Item const *Opts = _config->Tree(Cnf);
  214. if (Opts == 0 || Opts->Child == 0)
  215. return true;
  216. Opts = Opts->Child;
  217. unsigned int Count = 1;
  218. for (; Opts != 0; Opts = Opts->Next, Count++)
  219. {
  220. if (Opts->Value.empty() == true)
  221. continue;
  222. // Determine the protocol version
  223. string OptSec = Opts->Value;
  224. string::size_type Pos;
  225. if ((Pos = OptSec.find(' ')) == string::npos || Pos == 0)
  226. Pos = OptSec.length();
  227. OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos);
  228. unsigned int Version = _config->FindI(OptSec+"::Version",1);
  229. // Create the pipes
  230. int Pipes[2];
  231. if (pipe(Pipes) != 0)
  232. return _error->Errno("pipe","Failed to create IPC pipe to subprocess");
  233. SetCloseExec(Pipes[0],true);
  234. SetCloseExec(Pipes[1],true);
  235. // Purified Fork for running the script
  236. pid_t Process = ExecFork();
  237. if (Process == 0)
  238. {
  239. // Setup the FDs
  240. dup2(Pipes[0],STDIN_FILENO);
  241. SetCloseExec(STDOUT_FILENO,false);
  242. SetCloseExec(STDIN_FILENO,false);
  243. SetCloseExec(STDERR_FILENO,false);
  244. const char *Args[4];
  245. Args[0] = "/bin/sh";
  246. Args[1] = "-c";
  247. Args[2] = Opts->Value.c_str();
  248. Args[3] = 0;
  249. execv(Args[0],(char **)Args);
  250. _exit(100);
  251. }
  252. close(Pipes[0]);
  253. FILE *F = fdopen(Pipes[1],"w");
  254. if (F == 0)
  255. return _error->Errno("fdopen","Faild to open new FD");
  256. // Feed it the filenames.
  257. bool Die = false;
  258. if (Version <= 1)
  259. {
  260. for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
  261. {
  262. // Only deal with packages to be installed from .deb
  263. if (I->Op != Item::Install)
  264. continue;
  265. // No errors here..
  266. if (I->File[0] != '/')
  267. continue;
  268. /* Feed the filename of each package that is pending install
  269. into the pipe. */
  270. fprintf(F,"%s\n",I->File.c_str());
  271. if (ferror(F) != 0)
  272. {
  273. Die = true;
  274. break;
  275. }
  276. }
  277. }
  278. else
  279. Die = !SendV2Pkgs(F);
  280. fclose(F);
  281. if (Die == true)
  282. {
  283. kill(Process,SIGINT);
  284. ExecWait(Process,Opts->Value.c_str(),true);
  285. return _error->Error("Failure running script %s",Opts->Value.c_str());
  286. }
  287. // Clean up the sub process
  288. if (ExecWait(Process,Opts->Value.c_str()) == false)
  289. return _error->Error("Failure running script %s",Opts->Value.c_str());
  290. }
  291. return true;
  292. }
  293. /*}}}*/
  294. // DPkgPM::Go - Run the sequence /*{{{*/
  295. // ---------------------------------------------------------------------
  296. /* This globs the operations and calls dpkg */
  297. bool pkgDPkgPM::Go()
  298. {
  299. if (RunScripts("DPkg::Pre-Invoke") == false)
  300. return false;
  301. if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
  302. return false;
  303. for (vector<Item>::iterator I = List.begin(); I != List.end();)
  304. {
  305. vector<Item>::iterator J = I;
  306. for (; J != List.end() && J->Op == I->Op; J++);
  307. // Generate the argument list
  308. const char *Args[400];
  309. if (J - I > 350)
  310. J = I + 350;
  311. unsigned int n = 0;
  312. unsigned long Size = 0;
  313. string Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
  314. Args[n++] = Tmp.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. /*}}}*/