acquire-worker.h 9.7 KB

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