debsystem.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: debsystem.cc,v 1.4 2004/01/26 17:01:53 mdz Exp $
  4. /* ######################################################################
  5. System - Abstraction for running on different systems.
  6. Basic general structure..
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <config.h>
  11. #include <apt-pkg/debsystem.h>
  12. #include <apt-pkg/debversion.h>
  13. #include <apt-pkg/debindexfile.h>
  14. #include <apt-pkg/dpkgpm.h>
  15. #include <apt-pkg/configuration.h>
  16. #include <apt-pkg/error.h>
  17. #include <apt-pkg/fileutl.h>
  18. #include <apt-pkg/pkgcache.h>
  19. #include <apt-pkg/cacheiterators.h>
  20. #include <algorithm>
  21. #include <ctype.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <string>
  25. #include <vector>
  26. #include <unistd.h>
  27. #include <dirent.h>
  28. #include <errno.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <sys/wait.h>
  32. #include <fcntl.h>
  33. #include <apti18n.h>
  34. /*}}}*/
  35. using std::string;
  36. debSystem debSys;
  37. class APT_HIDDEN debSystemPrivate {
  38. public:
  39. debSystemPrivate() : LockFD(-1), LockCount(0), StatusFile(0)
  40. {
  41. }
  42. // For locking support
  43. int LockFD;
  44. unsigned LockCount;
  45. debStatusIndex *StatusFile;
  46. };
  47. // System::debSystem - Constructor /*{{{*/
  48. // ---------------------------------------------------------------------
  49. /* */
  50. debSystem::debSystem() : pkgSystem("Debian dpkg interface", &debVS), d(new debSystemPrivate())
  51. {
  52. }
  53. /*}}}*/
  54. // System::~debSystem - Destructor /*{{{*/
  55. // ---------------------------------------------------------------------
  56. /* */
  57. debSystem::~debSystem()
  58. {
  59. delete d->StatusFile;
  60. delete d;
  61. }
  62. /*}}}*/
  63. // System::Lock - Get the lock /*{{{*/
  64. // ---------------------------------------------------------------------
  65. /* This mirrors the operations dpkg does when it starts up. Note the
  66. checking of the updates directory. */
  67. bool debSystem::Lock()
  68. {
  69. // Disable file locking
  70. if (_config->FindB("Debug::NoLocking",false) == true || d->LockCount > 1)
  71. {
  72. d->LockCount++;
  73. return true;
  74. }
  75. // Create the lockfile
  76. string AdminDir = flNotFile(_config->Find("Dir::State::status"));
  77. d->LockFD = GetLock(AdminDir + "lock");
  78. if (d->LockFD == -1)
  79. {
  80. if (errno == EACCES || errno == EAGAIN)
  81. return _error->Error(_("Unable to lock the administration directory (%s), "
  82. "is another process using it?"),AdminDir.c_str());
  83. else
  84. return _error->Error(_("Unable to lock the administration directory (%s), "
  85. "are you root?"),AdminDir.c_str());
  86. }
  87. // See if we need to abort with a dirty journal
  88. if (CheckUpdates() == true)
  89. {
  90. close(d->LockFD);
  91. d->LockFD = -1;
  92. const char *cmd;
  93. if (getenv("SUDO_USER") != NULL)
  94. cmd = "sudo dpkg --configure -a";
  95. else
  96. cmd = "dpkg --configure -a";
  97. // TRANSLATORS: the %s contains the recovery command, usually
  98. // dpkg --configure -a
  99. return _error->Error(_("dpkg was interrupted, you must manually "
  100. "run '%s' to correct the problem. "), cmd);
  101. }
  102. d->LockCount++;
  103. return true;
  104. }
  105. /*}}}*/
  106. // System::UnLock - Drop a lock /*{{{*/
  107. // ---------------------------------------------------------------------
  108. /* */
  109. bool debSystem::UnLock(bool NoErrors)
  110. {
  111. if (d->LockCount == 0 && NoErrors == true)
  112. return false;
  113. if (d->LockCount < 1)
  114. return _error->Error(_("Not locked"));
  115. if (--d->LockCount == 0)
  116. {
  117. close(d->LockFD);
  118. d->LockCount = 0;
  119. }
  120. return true;
  121. }
  122. /*}}}*/
  123. // System::CheckUpdates - Check if the updates dir is dirty /*{{{*/
  124. // ---------------------------------------------------------------------
  125. /* This does a check of the updates directory (dpkg journal) to see if it has
  126. any entries in it. */
  127. bool debSystem::CheckUpdates()
  128. {
  129. // Check for updates.. (dirty)
  130. string File = flNotFile(_config->Find("Dir::State::status")) + "updates/";
  131. DIR *DirP = opendir(File.c_str());
  132. if (DirP == 0)
  133. return false;
  134. /* We ignore any files that are not all digits, this skips .,.. and
  135. some tmp files dpkg will leave behind.. */
  136. bool Damaged = false;
  137. for (struct dirent *Ent = readdir(DirP); Ent != 0; Ent = readdir(DirP))
  138. {
  139. Damaged = true;
  140. for (unsigned int I = 0; Ent->d_name[I] != 0; I++)
  141. {
  142. // Check if its not a digit..
  143. if (isdigit(Ent->d_name[I]) == 0)
  144. {
  145. Damaged = false;
  146. break;
  147. }
  148. }
  149. if (Damaged == true)
  150. break;
  151. }
  152. closedir(DirP);
  153. return Damaged;
  154. }
  155. /*}}}*/
  156. // System::CreatePM - Create the underlying package manager /*{{{*/
  157. // ---------------------------------------------------------------------
  158. /* */
  159. pkgPackageManager *debSystem::CreatePM(pkgDepCache *Cache) const
  160. {
  161. return new pkgDPkgPM(Cache);
  162. }
  163. /*}}}*/
  164. // System::Initialize - Setup the configuration space.. /*{{{*/
  165. // ---------------------------------------------------------------------
  166. /* These are the Debian specific configuration variables.. */
  167. bool debSystem::Initialize(Configuration &Cnf)
  168. {
  169. /* These really should be jammed into a generic 'Local Database' engine
  170. which is yet to be determined. The functions in pkgcachegen should
  171. be the only users of these */
  172. Cnf.CndSet("Dir::State::extended_states", "extended_states");
  173. Cnf.CndSet("Dir::State::status","/var/lib/dpkg/status");
  174. Cnf.CndSet("Dir::Bin::dpkg","/usr/bin/dpkg");
  175. if (d->StatusFile) {
  176. delete d->StatusFile;
  177. d->StatusFile = 0;
  178. }
  179. return true;
  180. }
  181. /*}}}*/
  182. // System::ArchiveSupported - Is a file format supported /*{{{*/
  183. // ---------------------------------------------------------------------
  184. /* The standard name for a deb is 'deb'.. There are no separate versions
  185. of .deb to worry about.. */
  186. APT_PURE bool debSystem::ArchiveSupported(const char *Type)
  187. {
  188. if (strcmp(Type,"deb") == 0)
  189. return true;
  190. return false;
  191. }
  192. /*}}}*/
  193. // System::Score - Determine how 'Debiany' this sys is.. /*{{{*/
  194. // ---------------------------------------------------------------------
  195. /* We check some files that are sure tell signs of this being a Debian
  196. System.. */
  197. signed debSystem::Score(Configuration const &Cnf)
  198. {
  199. signed Score = 0;
  200. if (FileExists(Cnf.FindFile("Dir::State::status","/var/lib/dpkg/status")) == true)
  201. Score += 10;
  202. if (FileExists(Cnf.Find("Dir::Bin::dpkg","/usr/bin/dpkg")) == true)
  203. Score += 10;
  204. if (FileExists("/etc/debian_version") == true)
  205. Score += 10;
  206. return Score;
  207. }
  208. /*}}}*/
  209. // System::AddStatusFiles - Register the status files /*{{{*/
  210. // ---------------------------------------------------------------------
  211. /* */
  212. bool debSystem::AddStatusFiles(std::vector<pkgIndexFile *> &List)
  213. {
  214. if (d->StatusFile == 0)
  215. d->StatusFile = new debStatusIndex(_config->FindFile("Dir::State::status"));
  216. List.push_back(d->StatusFile);
  217. return true;
  218. }
  219. /*}}}*/
  220. // System::FindIndex - Get an index file for status files /*{{{*/
  221. // ---------------------------------------------------------------------
  222. /* */
  223. bool debSystem::FindIndex(pkgCache::PkgFileIterator File,
  224. pkgIndexFile *&Found) const
  225. {
  226. if (d->StatusFile == 0)
  227. return false;
  228. if (d->StatusFile->FindInCache(*File.Cache()) == File)
  229. {
  230. Found = d->StatusFile;
  231. return true;
  232. }
  233. return false;
  234. }
  235. /*}}}*/
  236. std::string debSystem::GetDpkgExecutable() /*{{{*/
  237. {
  238. string Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
  239. string const dpkgChrootDir = _config->FindDir("DPkg::Chroot-Directory", "/");
  240. size_t dpkgChrootLen = dpkgChrootDir.length();
  241. if (dpkgChrootDir != "/" && Tmp.find(dpkgChrootDir) == 0)
  242. {
  243. if (dpkgChrootDir[dpkgChrootLen - 1] == '/')
  244. --dpkgChrootLen;
  245. Tmp = Tmp.substr(dpkgChrootLen);
  246. }
  247. return Tmp;
  248. }
  249. /*}}}*/
  250. std::vector<std::string> debSystem::GetDpkgBaseCommand() /*{{{*/
  251. {
  252. // Generate the base argument list for dpkg
  253. std::vector<std::string> Args = { GetDpkgExecutable() };
  254. // Stick in any custom dpkg options
  255. Configuration::Item const *Opts = _config->Tree("DPkg::Options");
  256. if (Opts != 0)
  257. {
  258. Opts = Opts->Child;
  259. for (; Opts != 0; Opts = Opts->Next)
  260. {
  261. if (Opts->Value.empty() == true)
  262. continue;
  263. Args.push_back(Opts->Value);
  264. }
  265. }
  266. return Args;
  267. }
  268. /*}}}*/
  269. void debSystem::DpkgChrootDirectory() /*{{{*/
  270. {
  271. std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
  272. if (chrootDir == "/")
  273. return;
  274. std::cerr << "Chrooting into " << chrootDir << std::endl;
  275. if (chroot(chrootDir.c_str()) != 0)
  276. _exit(100);
  277. if (chdir("/") != 0)
  278. _exit(100);
  279. }
  280. /*}}}*/
  281. pid_t debSystem::ExecDpkg(std::vector<std::string> const &sArgs, int * const inputFd, int * const outputFd, bool const DiscardOutput)/*{{{*/
  282. {
  283. std::vector<const char *> Args(sArgs.size(), NULL);
  284. std::transform(sArgs.begin(), sArgs.end(), Args.begin(), [](std::string const &s) { return s.c_str(); });
  285. Args.push_back(NULL);
  286. int external[2] = {-1, -1};
  287. if (inputFd != nullptr || outputFd != nullptr)
  288. if (pipe(external) != 0)
  289. {
  290. _error->WarningE("dpkg", "Can't create IPC pipe for dpkg call");
  291. return -1;
  292. }
  293. pid_t const dpkg = ExecFork();
  294. if (dpkg == 0) {
  295. int const nullfd = open("/dev/null", O_RDONLY);
  296. if (inputFd == nullptr)
  297. dup2(nullfd, STDIN_FILENO);
  298. else
  299. {
  300. close(external[1]);
  301. dup2(external[0], STDIN_FILENO);
  302. }
  303. if (outputFd == nullptr)
  304. dup2(nullfd, STDOUT_FILENO);
  305. else
  306. {
  307. close(external[0]);
  308. dup2(external[1], STDOUT_FILENO);
  309. }
  310. if (DiscardOutput == true)
  311. dup2(nullfd, STDERR_FILENO);
  312. debSystem::DpkgChrootDirectory();
  313. execvp(Args[0], (char**) &Args[0]);
  314. _error->WarningE("dpkg", "Can't execute dpkg!");
  315. _exit(100);
  316. }
  317. if (outputFd != nullptr)
  318. {
  319. close(external[1]);
  320. *outputFd = external[0];
  321. }
  322. else if (inputFd != nullptr)
  323. {
  324. close(external[0]);
  325. *inputFd = external[1];
  326. }
  327. return dpkg;
  328. }
  329. /*}}}*/
  330. bool debSystem::SupportsMultiArch() /*{{{*/
  331. {
  332. std::vector<std::string> Args = GetDpkgBaseCommand();
  333. Args.push_back("--assert-multi-arch");
  334. pid_t const dpkgAssertMultiArch = ExecDpkg(Args, nullptr, nullptr, true);
  335. if (dpkgAssertMultiArch > 0)
  336. {
  337. int Status = 0;
  338. while (waitpid(dpkgAssertMultiArch, &Status, 0) != dpkgAssertMultiArch)
  339. {
  340. if (errno == EINTR)
  341. continue;
  342. _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --assert-multi-arch");
  343. break;
  344. }
  345. if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
  346. return true;
  347. }
  348. return false;
  349. }
  350. /*}}}*/
  351. std::vector<std::string> debSystem::SupportedArchitectures() /*{{{*/
  352. {
  353. std::vector<std::string> archs;
  354. {
  355. string const arch = _config->Find("APT::Architecture");
  356. if (arch.empty() == false)
  357. archs.push_back(std::move(arch));
  358. }
  359. std::vector<std::string> sArgs = GetDpkgBaseCommand();
  360. sArgs.push_back("--print-foreign-architectures");
  361. int outputFd = -1;
  362. pid_t const dpkgMultiArch = ExecDpkg(sArgs, nullptr, &outputFd, true);
  363. if (dpkgMultiArch == -1)
  364. return archs;
  365. FILE *dpkg = fdopen(outputFd, "r");
  366. if(dpkg != NULL) {
  367. char* buf = NULL;
  368. size_t bufsize = 0;
  369. while (getline(&buf, &bufsize, dpkg) != -1)
  370. {
  371. char* tok_saveptr;
  372. char* arch = strtok_r(buf, " ", &tok_saveptr);
  373. while (arch != NULL) {
  374. for (; isspace_ascii(*arch) != 0; ++arch);
  375. if (arch[0] != '\0') {
  376. char const* archend = arch;
  377. for (; isspace_ascii(*archend) == 0 && *archend != '\0'; ++archend);
  378. string a(arch, (archend - arch));
  379. if (std::find(archs.begin(), archs.end(), a) == archs.end())
  380. archs.push_back(a);
  381. }
  382. arch = strtok_r(NULL, " ", &tok_saveptr);
  383. }
  384. }
  385. free(buf);
  386. fclose(dpkg);
  387. }
  388. ExecWait(dpkgMultiArch, "dpkg --print-foreign-architectures", true);
  389. return archs;
  390. }
  391. /*}}}*/