apt-dump-solver.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* #####################################################################
  4. dummy solver to get quickly a scenario file out of APT
  5. ##################################################################### */
  6. /*}}}*/
  7. // Include Files /*{{{*/
  8. #include <config.h>
  9. #include <apt-pkg/cmndline.h>
  10. #include <apt-pkg/configuration.h>
  11. #include <apt-pkg/edsp.h>
  12. #include <apt-pkg/fileutl.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <apt-private/private-cmndline.h>
  15. #include <cstdio>
  16. #include <iostream>
  17. #include <memory>
  18. #include <sstream>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <apti18n.h>
  24. /*}}}*/
  25. static bool ShowHelp(CommandLine &) /*{{{*/
  26. {
  27. std::cout <<
  28. _("Usage: apt-dump-solver\n"
  29. "\n"
  30. "apt-dump-solver is an interface to store an EDSP scenario in\n"
  31. "a file and optionally forwards it to another solver.\n");
  32. return true;
  33. }
  34. /*}}}*/
  35. static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
  36. {
  37. return {};
  38. }
  39. /*}}}*/
  40. static int WriteError(char const * const uid, std::ostringstream &out, FileFd &stdoutfd, pid_t const &Solver)/*{{{*/
  41. {
  42. _error->DumpErrors(out);
  43. // ensure the solver isn't printing into "our" error message, too
  44. if (Solver != 0)
  45. ExecWait(Solver, "dump", true);
  46. EDSP::WriteError(uid, out.str(), stdoutfd);
  47. return 0;
  48. }
  49. /*}}}*/
  50. int main(int argc,const char *argv[]) /*{{{*/
  51. {
  52. CommandLine CmdL;
  53. ParseCommandLine(CmdL, APT_CMD::APT_DUMP_SOLVER, &_config, nullptr, argc, argv, &ShowHelp, &GetCommands);
  54. _config->Clear("Dir::Log");
  55. bool const is_forwarding_dumper = (CmdL.FileSize() != 0);
  56. FileFd stdoutfd;
  57. if (stdoutfd.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
  58. return 252;
  59. FileFd dump;
  60. char const * const filename = is_forwarding_dumper ? CmdL.FileList[0] : getenv("APT_EDSP_DUMP_FILENAME");
  61. if (filename == nullptr || strlen(filename) == 0)
  62. {
  63. if (is_forwarding_dumper == false)
  64. {
  65. EDSP::WriteError("ERR_NO_FILENAME", "You have to set the environment variable APT_EDSP_DUMP_FILENAME\n"
  66. "to a valid filename to store the dump of EDSP solver input in.\n"
  67. "For example with: export APT_EDSP_DUMP_FILENAME=/tmp/dump.edsp", stdoutfd);
  68. return 0;
  69. }
  70. }
  71. else
  72. {
  73. // ignore errors here as logging isn't really critical
  74. _error->PushToStack();
  75. if (dump.Open(filename, FileFd::WriteOnly | FileFd::Exclusive | FileFd::Create, FileFd::Extension, 0644) == false &&
  76. is_forwarding_dumper == false)
  77. {
  78. _error->MergeWithStack();
  79. std::ostringstream out;
  80. out << "Writing EDSP solver input to file '" << filename << "' failed as it couldn't be created!\n";
  81. return WriteError("ERR_CREATE_FILE", out, stdoutfd, 0);
  82. }
  83. _error->RevertToStack();
  84. }
  85. pid_t Solver = 0;
  86. FileFd forward;
  87. if (is_forwarding_dumper)
  88. {
  89. int external[] = {-1, -1};
  90. if (pipe(external) != 0)
  91. return 250;
  92. for (int i = 0; i < 2; ++i)
  93. SetCloseExec(external[i], true);
  94. Solver = ExecFork();
  95. if (Solver == 0) {
  96. _config->Set("APT::Sandbox::User", _config->Find("APT::Solver::RunAsUser", _config->Find("APT::Sandbox::User")));
  97. DropPrivileges();
  98. dup2(external[0], STDIN_FILENO);
  99. execv(CmdL.FileList[1], const_cast<char**>(CmdL.FileList + 1));
  100. std::cerr << "Failed to execute '" << CmdL.FileList[1] << "'!" << std::endl;
  101. _exit(100);
  102. }
  103. close(external[0]);
  104. if (WaitFd(external[1], true, 5) == false)
  105. return 251;
  106. if (forward.OpenDescriptor(external[1], FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
  107. return 252;
  108. }
  109. DropPrivileges();
  110. FileFd input;
  111. if (input.OpenDescriptor(STDIN_FILENO, FileFd::ReadOnly) == false)
  112. {
  113. std::ostringstream out;
  114. out << "Writing EDSP solver input to file '" << filename << "' failed as stdin couldn't be opened!\n";
  115. return WriteError("ERR_READ_ERROR", out, stdoutfd, Solver);
  116. }
  117. constexpr size_t BufSize = 64 * 1024;
  118. std::unique_ptr<char[]> Buf(new char[BufSize]);
  119. unsigned long long ToRead = 0;
  120. do {
  121. if (input.Read(Buf.get(),BufSize, &ToRead) == false)
  122. {
  123. std::ostringstream out;
  124. out << "Writing EDSP solver input to file '" << filename << "' failed as reading from stdin failed!\n";
  125. return WriteError("ERR_READ_ERROR", out, stdoutfd, Solver);
  126. }
  127. if (ToRead == 0)
  128. break;
  129. if (forward.IsOpen() && forward.Failed() == false && forward.Write(Buf.get(),ToRead) == false)
  130. forward.Close();
  131. if (dump.IsOpen() && dump.Failed() == false && dump.Write(Buf.get(),ToRead) == false)
  132. dump.Close();
  133. } while (true);
  134. input.Close();
  135. forward.Close();
  136. dump.Close();
  137. if (_error->PendingError())
  138. {
  139. std::ostringstream out;
  140. out << "Writing EDSP solver input to file '" << filename << "' failed due to write errors!\n";
  141. return WriteError("ERR_WRITE_ERROR", out, stdoutfd, Solver);
  142. }
  143. if (is_forwarding_dumper)
  144. {
  145. // Wait and collect the error code
  146. int Status;
  147. while (waitpid(Solver, &Status, 0) != Solver)
  148. {
  149. if (errno == EINTR)
  150. continue;
  151. std::ostringstream out;
  152. ioprintf(out, _("Waited for %s but it wasn't there"), CmdL.FileList[1]);
  153. return WriteError("ERR_FORWARD", out, stdoutfd, 0);
  154. }
  155. if (WIFEXITED(Status))
  156. return WEXITSTATUS(Status);
  157. else
  158. return 255;
  159. }
  160. else
  161. EDSP::WriteError("ERR_JUST_DUMPING", "I am too dumb, i can just dump!\nPlease use one of my friends instead!", stdoutfd);
  162. return 0;
  163. }