apt-dump-solver.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. dup2(external[0], STDIN_FILENO);
  97. execv(CmdL.FileList[1], const_cast<char**>(CmdL.FileList + 1));
  98. std::cerr << "Failed to execute '" << CmdL.FileList[1] << "'!" << std::endl;
  99. _exit(100);
  100. }
  101. close(external[0]);
  102. if (WaitFd(external[1], true, 5) == false)
  103. return 251;
  104. if (forward.OpenDescriptor(external[1], FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
  105. return 252;
  106. }
  107. DropPrivileges();
  108. FileFd input;
  109. if (input.OpenDescriptor(STDIN_FILENO, FileFd::ReadOnly) == false)
  110. {
  111. std::ostringstream out;
  112. out << "Writing EDSP solver input to file '" << filename << "' failed as stdin couldn't be opened!\n";
  113. return WriteError("ERR_READ_ERROR", out, stdoutfd, Solver);
  114. }
  115. constexpr size_t BufSize = 64 * 1024;
  116. std::unique_ptr<char[]> Buf(new char[BufSize]);
  117. unsigned long long ToRead = 0;
  118. do {
  119. if (input.Read(Buf.get(),BufSize, &ToRead) == false)
  120. {
  121. std::ostringstream out;
  122. out << "Writing EDSP solver input to file '" << filename << "' failed as reading from stdin failed!\n";
  123. return WriteError("ERR_READ_ERROR", out, stdoutfd, Solver);
  124. }
  125. if (ToRead == 0)
  126. break;
  127. if (forward.IsOpen() && forward.Failed() == false && forward.Write(Buf.get(),ToRead) == false)
  128. forward.Close();
  129. if (dump.IsOpen() && dump.Failed() == false && dump.Write(Buf.get(),ToRead) == false)
  130. dump.Close();
  131. } while (true);
  132. input.Close();
  133. forward.Close();
  134. dump.Close();
  135. if (_error->PendingError())
  136. {
  137. std::ostringstream out;
  138. out << "Writing EDSP solver input to file '" << filename << "' failed due to write errors!\n";
  139. return WriteError("ERR_WRITE_ERROR", out, stdoutfd, Solver);
  140. }
  141. if (is_forwarding_dumper)
  142. {
  143. // Wait and collect the error code
  144. int Status;
  145. while (waitpid(Solver, &Status, 0) != Solver)
  146. {
  147. if (errno == EINTR)
  148. continue;
  149. std::ostringstream out;
  150. ioprintf(out, _("Waited for %s but it wasn't there"), CmdL.FileList[1]);
  151. return WriteError("ERR_FORWARD", out, stdoutfd, 0);
  152. }
  153. if (WIFEXITED(Status))
  154. return WEXITSTATUS(Status);
  155. else
  156. return 255;
  157. }
  158. else
  159. EDSP::WriteError("ERR_JUST_DUMPING", "I am too dumb, i can just dump!\nPlease use one of my friends instead!", stdoutfd);
  160. return 0;
  161. }