acquire-worker.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Acquire Worker - Worker process manager
  5. Each worker class is associated with exaclty one subprocess.
  6. ##################################################################### */
  7. /*}}}*/
  8. /** \addtogroup acquire
  9. * @{
  10. *
  11. * \file acquire-worker.h
  12. */
  13. #ifndef PKGLIB_ACQUIRE_WORKER_H
  14. #define PKGLIB_ACQUIRE_WORKER_H
  15. #include <apt-pkg/acquire.h>
  16. #include <apt-pkg/weakptr.h>
  17. #include <sys/types.h>
  18. #include <string>
  19. #include <vector>
  20. /** \brief A fetch subprocess.
  21. *
  22. * A worker process is responsible for one stage of the fetch. This
  23. * class encapsulates the communications protocol between the master
  24. * process and the worker, from the master end.
  25. *
  26. * Each worker is intrinsically placed on two linked lists. The
  27. * Queue list (maintained in the #NextQueue variable) is maintained
  28. * by the pkgAcquire::Queue class; it represents the set of workers
  29. * assigned to a particular queue. The Acquire list (maintained in
  30. * the #NextAcquire variable) is maintained by the pkgAcquire class;
  31. * it represents the set of active workers for a particular
  32. * pkgAcquire object.
  33. *
  34. * \todo Like everything else in the Acquire system, this has way too
  35. * many protected items.
  36. *
  37. * \sa pkgAcqMethod, pkgAcquire::Item, pkgAcquire
  38. */
  39. class pkgAcquire::Worker : public WeakPointable
  40. {
  41. /** \brief dpointer placeholder (for later in case we need it) */
  42. void * const d;
  43. friend class pkgAcquire;
  44. protected:
  45. friend class Queue;
  46. /** \brief The next link on the Queue list.
  47. *
  48. * \todo This is always NULL; is it just for future use?
  49. */
  50. Worker *NextQueue;
  51. /** \brief The next link on the Acquire list. */
  52. Worker *NextAcquire;
  53. /** \brief The Queue with which this worker is associated. */
  54. Queue *OwnerQ;
  55. /** \brief The download progress indicator to which progress
  56. * messages should be sent.
  57. */
  58. pkgAcquireStatus *Log;
  59. /** \brief The configuration of this method. On startup, the
  60. * target of this pointer is filled in with basic data about the
  61. * method, as reported by the worker.
  62. */
  63. MethodConfig *Config;
  64. /** \brief The access method to be used by this worker.
  65. *
  66. * \todo Doesn't this duplicate Config->Access?
  67. */
  68. std::string Access;
  69. /** \brief The PID of the subprocess. */
  70. pid_t Process;
  71. /** \brief A file descriptor connected to the standard output of
  72. * the subprocess.
  73. *
  74. * Used to read messages and data from the subprocess.
  75. */
  76. int InFd;
  77. /** \brief A file descriptor connected to the standard input of the
  78. * subprocess.
  79. *
  80. * Used to send commands and configuration data to the subprocess.
  81. */
  82. int OutFd;
  83. /** \brief The socket to send SCM_RIGHTS message through
  84. */
  85. int PrivSepSocketFd;
  86. int PrivSepSocketFdChild;
  87. /** \brief Set to \b true if the worker is in a state in which it
  88. * might generate data or command responses.
  89. *
  90. * \todo Is this right? It's a guess.
  91. */
  92. bool InReady;
  93. /** \brief Set to \b true if the worker is in a state in which it
  94. * is legal to send commands to it.
  95. *
  96. * \todo Is this right?
  97. */
  98. bool OutReady;
  99. /** If \b true, debugging output will be sent to std::clog. */
  100. bool Debug;
  101. /** \brief The raw text values of messages received from the
  102. * worker, in sequence.
  103. */
  104. std::vector<std::string> MessageQueue;
  105. /** \brief Buffers pending writes to the subprocess.
  106. *
  107. * \todo Wouldn't a std::dequeue be more appropriate?
  108. */
  109. std::string OutQueue;
  110. /** \brief Common code for the constructor.
  111. *
  112. * Initializes NextQueue and NextAcquire to NULL; Process, InFd,
  113. * and OutFd to -1, OutReady and InReady to \b false, and Debug
  114. * from _config.
  115. */
  116. void Construct();
  117. /** \brief Retrieve any available messages from the subprocess.
  118. *
  119. * The messages are retrieved as in \link strutl.h ReadMessages()\endlink, and
  120. * #MethodFailure() is invoked if an error occurs; in particular,
  121. * if the pipe to the subprocess dies unexpectedly while a message
  122. * is being read.
  123. *
  124. * \return \b true if the messages were successfully read, \b
  125. * false otherwise.
  126. */
  127. bool ReadMessages();
  128. /** \brief Parse and dispatch pending messages.
  129. *
  130. * This dispatches the message in a manner appropriate for its
  131. * type.
  132. *
  133. * \todo Several message types lack separate handlers.
  134. *
  135. * \sa Capabilities(), SendConfiguration(), MediaChange()
  136. */
  137. bool RunMessages();
  138. /** \brief Read and dispatch any pending messages from the
  139. * subprocess.
  140. *
  141. * \return \b false if the subprocess died unexpectedly while a
  142. * message was being transmitted.
  143. */
  144. bool InFdReady();
  145. /** \brief Send any pending commands to the subprocess.
  146. *
  147. * This method will fail if there is no pending output.
  148. *
  149. * \return \b true if all commands were succeeded, \b false if an
  150. * error occurred (in which case MethodFailure() will be invoked).
  151. */
  152. bool OutFdReady();
  153. /** \brief Handle a 100 Capabilities response from the subprocess.
  154. *
  155. * \param Message the raw text of the message from the subprocess.
  156. *
  157. * The message will be parsed and its contents used to fill
  158. * #Config. If #Config is NULL, this routine is a NOP.
  159. *
  160. * \return \b true.
  161. */
  162. bool Capabilities(std::string Message);
  163. /** \brief Send a 601 Configuration message (containing the APT
  164. * configuration) to the subprocess.
  165. *
  166. * The APT configuration will be send to the subprocess in a
  167. * message of the following form:
  168. *
  169. * <pre>
  170. * 601 Configuration
  171. * Config-Item: Fully-Qualified-Item=Val
  172. * Config-Item: Fully-Qualified-Item=Val
  173. * ...
  174. * </pre>
  175. *
  176. * \return \b true if the command was successfully sent, \b false
  177. * otherwise.
  178. */
  179. bool SendConfiguration();
  180. /** \brief Handle a 403 Media Change message.
  181. *
  182. * \param Message the raw text of the message; the Media field
  183. * indicates what type of media should be changed, and the Drive
  184. * field indicates where the media is located.
  185. *
  186. * Invokes pkgAcquireStatus::MediaChange(Media, Drive) to ask the
  187. * user to swap disks; informs the subprocess of the result (via
  188. * 603 Media Changed, with the Failed field set to \b true if the
  189. * user cancelled the media change).
  190. */
  191. bool MediaChange(std::string Message);
  192. /** \brief Invoked when the worked process dies unexpectedly.
  193. *
  194. * Waits for the subprocess to terminate and generates an error if
  195. * it terminated abnormally, then closes and blanks out all file
  196. * descriptors. Discards all pending messages from the
  197. * subprocess.
  198. *
  199. * \return \b false.
  200. */
  201. bool MethodFailure();
  202. /** \brief Invoked when a fetch job is completed, either
  203. * successfully or unsuccessfully.
  204. *
  205. * Resets the status information for the worker process.
  206. */
  207. void ItemDone();
  208. public:
  209. /** \brief The queue entry that is currently being downloaded. */
  210. pkgAcquire::Queue::QItem *CurrentItem;
  211. /** \brief The most recent status string received from the
  212. * subprocess.
  213. */
  214. std::string Status;
  215. /** \brief How many bytes of the file have been downloaded. Zero
  216. * if the current progress of the file cannot be determined.
  217. */
  218. unsigned long long CurrentSize;
  219. /** \brief The total number of bytes to be downloaded. Zero if the
  220. * total size of the final is unknown.
  221. */
  222. unsigned long long TotalSize;
  223. /** \brief How much of the file was already downloaded prior to
  224. * starting this worker.
  225. */
  226. unsigned long long ResumePoint;
  227. /** \brief Tell the subprocess to download the given item.
  228. *
  229. * \param Item the item to queue up.
  230. * \return \b true if the item was successfully enqueued.
  231. *
  232. * Queues up a 600 URI Acquire message for the given item to be
  233. * sent at the next possible moment. Does \e not flush the output
  234. * queue.
  235. */
  236. bool QueueItem(pkgAcquire::Queue::QItem *Item);
  237. /** \brief Start up the worker and fill in #Config.
  238. *
  239. * Reads the first message from the worker, which is assumed to be
  240. * a 100 Capabilities message.
  241. *
  242. * \return \b true if all operations completed successfully.
  243. */
  244. bool Start();
  245. /** \brief Update the worker statistics (CurrentSize, TotalSize,
  246. * etc).
  247. */
  248. void Pulse();
  249. /** \return The fetch method configuration. */
  250. inline const MethodConfig *GetConf() const {return Config;};
  251. /** \brief Create a new Worker to download files.
  252. *
  253. * \param OwnerQ The queue into which this worker should be
  254. * placed.
  255. *
  256. * \param Config A location in which to store information about
  257. * the fetch method.
  258. *
  259. * \param Log The download progress indicator that should be used
  260. * to report the progress of this worker.
  261. */
  262. Worker(Queue *OwnerQ,MethodConfig *Config,pkgAcquireStatus *Log);
  263. /** \brief Create a new Worker that should just retrieve
  264. * information about the fetch method.
  265. *
  266. * Nothing in particular forces you to refrain from actually
  267. * downloading stuff, but the various status callbacks won't be
  268. * invoked.
  269. *
  270. * \param Config A location in which to store information about
  271. * the fetch method.
  272. */
  273. explicit Worker(MethodConfig *Config);
  274. /** \brief Clean up this worker.
  275. *
  276. * Closes the file descriptors; if MethodConfig::NeedsCleanup is
  277. * \b false, also rudely interrupts the worker with a SIGINT.
  278. */
  279. virtual ~Worker();
  280. private:
  281. APT_HIDDEN void PrepareFiles(char const * const caller, pkgAcquire::Queue::QItem const * const Itm);
  282. };
  283. /** @} */
  284. #endif