dpkgpm.cc 14 KB

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