acquire-worker.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-worker.cc,v 1.10 1998/11/05 07:21:39 jgg Exp $
  4. /* ######################################################################
  5. Acquire Worker
  6. The worker process can startup either as a Configuration prober
  7. or as a queue runner. As a configuration prober it only reads the
  8. configuration message and
  9. ##################################################################### */
  10. /*}}}*/
  11. // Include Files /*{{{*/
  12. #ifdef __GNUG__
  13. #pragma implementation "apt-pkg/acquire-worker.h"
  14. #endif
  15. #include <apt-pkg/acquire-worker.h>
  16. #include <apt-pkg/acquire-item.h>
  17. #include <apt-pkg/configuration.h>
  18. #include <apt-pkg/error.h>
  19. #include <apt-pkg/fileutl.h>
  20. #include <strutl.h>
  21. #include <unistd.h>
  22. #include <signal.h>
  23. #include <wait.h>
  24. /*}}}*/
  25. // Worker::Worker - Constructor for Queue startup /*{{{*/
  26. // ---------------------------------------------------------------------
  27. /* */
  28. pkgAcquire::Worker::Worker(Queue *Q,MethodConfig *Cnf)
  29. {
  30. OwnerQ = Q;
  31. Config = Cnf;
  32. Access = Cnf->Access;
  33. CurrentItem = 0;
  34. Construct();
  35. }
  36. /*}}}*/
  37. // Worker::Worker - Constructor for method config startup /*{{{*/
  38. // ---------------------------------------------------------------------
  39. /* */
  40. pkgAcquire::Worker::Worker(MethodConfig *Cnf)
  41. {
  42. OwnerQ = 0;
  43. Config = Cnf;
  44. Access = Cnf->Access;
  45. CurrentItem = 0;
  46. Construct();
  47. }
  48. /*}}}*/
  49. // Worker::Construct - Constructor helper /*{{{*/
  50. // ---------------------------------------------------------------------
  51. /* */
  52. void pkgAcquire::Worker::Construct()
  53. {
  54. NextQueue = 0;
  55. NextAcquire = 0;
  56. Process = -1;
  57. InFd = -1;
  58. OutFd = -1;
  59. OutReady = false;
  60. InReady = false;
  61. Debug = _config->FindB("Debug::pkgAcquire::Worker",false);
  62. }
  63. /*}}}*/
  64. // Worker::~Worker - Destructor /*{{{*/
  65. // ---------------------------------------------------------------------
  66. /* */
  67. pkgAcquire::Worker::~Worker()
  68. {
  69. close(InFd);
  70. close(OutFd);
  71. if (Process > 0)
  72. {
  73. kill(Process,SIGINT);
  74. if (waitpid(Process,0,0) != Process)
  75. _error->Warning("I waited but nothing was there!");
  76. }
  77. }
  78. /*}}}*/
  79. // Worker::Start - Start the worker process /*{{{*/
  80. // ---------------------------------------------------------------------
  81. /* This forks the method and inits the communication channel */
  82. bool pkgAcquire::Worker::Start()
  83. {
  84. // Get the method path
  85. string Method = _config->FindDir("Dir::Bin::Methods") + Access;
  86. if (FileExists(Method) == false)
  87. return _error->Error("The method driver %s could not be found.",Method.c_str());
  88. if (Debug == true)
  89. clog << "Starting method '" << Method << '\'' << endl;
  90. // Create the pipes
  91. int Pipes[4] = {-1,-1,-1,-1};
  92. if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0)
  93. {
  94. _error->Errno("pipe","Failed to create IPC pipe to subprocess");
  95. for (int I = 0; I != 4; I++)
  96. close(Pipes[I]);
  97. return false;
  98. }
  99. for (int I = 0; I != 4; I++)
  100. SetCloseExec(Pipes[0],true);
  101. // Fork off the process
  102. Process = fork();
  103. if (Process < 0)
  104. {
  105. cerr << "FATAL -> Failed to fork." << endl;
  106. exit(100);
  107. }
  108. // Spawn the subprocess
  109. if (Process == 0)
  110. {
  111. // Setup the FDs
  112. dup2(Pipes[1],STDOUT_FILENO);
  113. dup2(Pipes[2],STDIN_FILENO);
  114. dup2(((filebuf *)clog.rdbuf())->fd(),STDERR_FILENO);
  115. SetCloseExec(STDOUT_FILENO,false);
  116. SetCloseExec(STDIN_FILENO,false);
  117. SetCloseExec(STDERR_FILENO,false);
  118. const char *Args[2];
  119. Args[0] = Method.c_str();
  120. Args[1] = 0;
  121. execv(Args[0],(char **)Args);
  122. cerr << "Failed to exec method " << Args[0] << endl;
  123. exit(100);
  124. }
  125. // Fix up our FDs
  126. InFd = Pipes[0];
  127. OutFd = Pipes[3];
  128. SetNonBlock(Pipes[0],true);
  129. SetNonBlock(Pipes[3],true);
  130. close(Pipes[1]);
  131. close(Pipes[2]);
  132. OutReady = false;
  133. InReady = true;
  134. // Read the configuration data
  135. if (WaitFd(InFd) == false ||
  136. ReadMessages() == false)
  137. return _error->Error("Method %s did not start correctly",Method.c_str());
  138. RunMessages();
  139. if (OwnerQ != 0)
  140. SendConfiguration();
  141. return true;
  142. }
  143. /*}}}*/
  144. // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
  145. // ---------------------------------------------------------------------
  146. /* */
  147. bool pkgAcquire::Worker::ReadMessages()
  148. {
  149. if (::ReadMessages(InFd,MessageQueue) == false)
  150. return MethodFailure();
  151. return true;
  152. }
  153. /*}}}*/
  154. // Worker::RunMessage - Empty the message queue /*{{{*/
  155. // ---------------------------------------------------------------------
  156. /* This takes the messages from the message queue and runs them through
  157. the parsers in order. */
  158. bool pkgAcquire::Worker::RunMessages()
  159. {
  160. while (MessageQueue.empty() == false)
  161. {
  162. string Message = MessageQueue.front();
  163. MessageQueue.erase(MessageQueue.begin());
  164. if (Debug == true)
  165. clog << " <- " << Access << ':' << QuoteString(Message,"\n") << endl;
  166. // Fetch the message number
  167. char *End;
  168. int Number = strtol(Message.c_str(),&End,10);
  169. if (End == Message.c_str())
  170. return _error->Error("Invalid message from method %s: %s",Access.c_str(),Message.c_str());
  171. string URI = LookupTag(Message,"URI");
  172. pkgAcquire::Queue::QItem *Itm = 0;
  173. if (URI.empty() == false)
  174. Itm = OwnerQ->FindItem(URI,this);
  175. // Determine the message number and dispatch
  176. switch (Number)
  177. {
  178. // 100 Capabilities
  179. case 100:
  180. if (Capabilities(Message) == false)
  181. return _error->Error("Unable to process Capabilities message from %s",Access.c_str());
  182. break;
  183. // 101 Log
  184. case 101:
  185. if (Debug == true)
  186. clog << " <- (log) " << LookupTag(Message,"Message") << endl;
  187. break;
  188. // 102 Status
  189. case 102:
  190. Status = LookupTag(Message,"Message");
  191. break;
  192. // 200 URI Start
  193. case 200:
  194. {
  195. if (Itm == 0)
  196. {
  197. _error->Error("Method gave invalid 200 URI Start message");
  198. break;
  199. }
  200. CurrentItem = Itm;
  201. CurrentSize = 0;
  202. TotalSize = atoi(LookupTag(Message,"Size","0").c_str());
  203. break;
  204. }
  205. // 201 URI Done
  206. case 201:
  207. {
  208. if (Itm == 0)
  209. {
  210. _error->Error("Method gave invalid 201 URI Done message");
  211. break;
  212. }
  213. pkgAcquire::Item *Owner = Itm->Owner;
  214. OwnerQ->ItemDone(Itm);
  215. Owner->Done(Message,atoi(LookupTag(Message,"Size","0").c_str()),
  216. LookupTag(Message,"MD5-Hash"));
  217. break;
  218. }
  219. // 400 URI Failure
  220. case 400:
  221. {
  222. if (Itm == 0)
  223. {
  224. _error->Error("Method gave invalid 400 URI Failure message");
  225. break;
  226. }
  227. pkgAcquire::Item *Owner = Itm->Owner;
  228. OwnerQ->ItemDone(Itm);
  229. Owner->Failed(Message);
  230. break;
  231. }
  232. // 401 General Failure
  233. case 401:
  234. _error->Error("Method %s General failure: %s",LookupTag(Message,"Message").c_str());
  235. break;
  236. }
  237. }
  238. return true;
  239. }
  240. /*}}}*/
  241. // Worker::Capabilities - 100 Capabilities handler /*{{{*/
  242. // ---------------------------------------------------------------------
  243. /* This parses the capabilities message and dumps it into the configuration
  244. structure. */
  245. bool pkgAcquire::Worker::Capabilities(string Message)
  246. {
  247. if (Config == 0)
  248. return true;
  249. Config->Version = LookupTag(Message,"Version");
  250. Config->SingleInstance = StringToBool(LookupTag(Message,"Single-Instance"),false);
  251. Config->PreScan = StringToBool(LookupTag(Message,"Pre-Scan"),false);
  252. Config->Pipeline = StringToBool(LookupTag(Message,"Pipeline"),false);
  253. Config->SendConfig = StringToBool(LookupTag(Message,"Send-Config"),false);
  254. // Some debug text
  255. if (Debug == true)
  256. {
  257. clog << "Configured access method " << Config->Access << endl;
  258. clog << "Version:" << Config->Version << " SingleInstance:" <<
  259. Config->SingleInstance << " PreScan: " << Config->PreScan <<
  260. " Pipeline:" << Config->Pipeline << " SendConfig:" <<
  261. Config->SendConfig << endl;
  262. }
  263. return true;
  264. }
  265. /*}}}*/
  266. // Worker::SendConfiguration - Send the config to the method /*{{{*/
  267. // ---------------------------------------------------------------------
  268. /* */
  269. bool pkgAcquire::Worker::SendConfiguration()
  270. {
  271. if (Config->SendConfig == false)
  272. return true;
  273. if (OutFd == -1)
  274. return false;
  275. string Message = "601 Configuration\n";
  276. Message.reserve(2000);
  277. /* Write out all of the configuration directives by walking the
  278. configuration tree */
  279. const Configuration::Item *Top = _config->Tree(0);
  280. for (; Top != 0;)
  281. {
  282. if (Top->Value.empty() == false)
  283. {
  284. string Line = "Config-Item: " + Top->FullTag() + "=";
  285. Line += QuoteString(Top->Value,"\n") + '\n';
  286. Message += Line;
  287. }
  288. if (Top->Child != 0)
  289. {
  290. Top = Top->Child;
  291. continue;
  292. }
  293. while (Top != 0 && Top->Next == 0)
  294. Top = Top->Parent;
  295. if (Top != 0)
  296. Top = Top->Next;
  297. }
  298. Message += '\n';
  299. if (Debug == true)
  300. clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
  301. OutQueue += Message;
  302. OutReady = true;
  303. return true;
  304. }
  305. /*}}}*/
  306. // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
  307. // ---------------------------------------------------------------------
  308. /* Send a URI Acquire message to the method */
  309. bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem *Item)
  310. {
  311. if (OutFd == -1)
  312. return false;
  313. string Message = "600 URI Acquire\n";
  314. Message.reserve(300);
  315. Message += "URI: " + Item->URI;
  316. Message += "\nFilename: " + Item->Owner->DestFile;
  317. Message += Item->Owner->Custom600Headers();
  318. Message += "\n\n";
  319. if (Debug == true)
  320. clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
  321. OutQueue += Message;
  322. OutReady = true;
  323. return true;
  324. }
  325. /*}}}*/
  326. // Worker::OutFdRead - Out bound FD is ready /*{{{*/
  327. // ---------------------------------------------------------------------
  328. /* */
  329. bool pkgAcquire::Worker::OutFdReady()
  330. {
  331. int Res = write(OutFd,OutQueue.begin(),OutQueue.length());
  332. if (Res <= 0)
  333. return MethodFailure();
  334. // Hmm.. this should never happen.
  335. if (Res < 0)
  336. return true;
  337. OutQueue.erase(0,Res);
  338. if (OutQueue.empty() == true)
  339. OutReady = false;
  340. return true;
  341. }
  342. /*}}}*/
  343. // Worker::InFdRead - In bound FD is ready /*{{{*/
  344. // ---------------------------------------------------------------------
  345. /* */
  346. bool pkgAcquire::Worker::InFdReady()
  347. {
  348. if (ReadMessages() == false)
  349. return false;
  350. RunMessages();
  351. return true;
  352. }
  353. /*}}}*/
  354. // Worker::MethodFailure - Called when the method fails /*{{{*/
  355. // ---------------------------------------------------------------------
  356. /* This is called when the method is belived to have failed, probably because
  357. read returned -1. */
  358. bool pkgAcquire::Worker::MethodFailure()
  359. {
  360. cerr << "Method " << Access << " has died unexpectedly!" << endl;
  361. if (waitpid(Process,0,0) != Process)
  362. _error->Warning("I waited but nothing was there!");
  363. Process = -1;
  364. close(InFd);
  365. close(OutFd);
  366. InFd = -1;
  367. OutFd = -1;
  368. OutReady = false;
  369. InReady = false;
  370. OutQueue = string();
  371. MessageQueue.erase(MessageQueue.begin(),MessageQueue.end());
  372. return false;
  373. }
  374. /*}}}*/