acquire-worker.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-worker.cc,v 1.7 1998/10/26 07:11:45 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->Warning("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->Warning("Method gave invalid 400 URI Failure message");
  211. break;
  212. }
  213. Itm->Owner->Done(Message,atoi(LookupTag(Message,"Size","0").c_str()),
  214. LookupTag(Message,"MD5-Hash"));
  215. OwnerQ->ItemDone(Itm);
  216. break;
  217. }
  218. // 400 URI Failure
  219. case 400:
  220. {
  221. if (Itm == 0)
  222. {
  223. _error->Warning("Method gave invalid 400 URI Failure message");
  224. break;
  225. }
  226. Itm->Owner->Failed(Message);
  227. OwnerQ->ItemDone(Itm);
  228. break;
  229. }
  230. // 401 General Failure
  231. case 401:
  232. _error->Error("Method %s General failure: %s",LookupTag(Message,"Message").c_str());
  233. break;
  234. }
  235. }
  236. return true;
  237. }
  238. /*}}}*/
  239. // Worker::Capabilities - 100 Capabilities handler /*{{{*/
  240. // ---------------------------------------------------------------------
  241. /* This parses the capabilities message and dumps it into the configuration
  242. structure. */
  243. bool pkgAcquire::Worker::Capabilities(string Message)
  244. {
  245. if (Config == 0)
  246. return true;
  247. Config->Version = LookupTag(Message,"Version");
  248. Config->SingleInstance = StringToBool(LookupTag(Message,"Single-Instance"),false);
  249. Config->PreScan = StringToBool(LookupTag(Message,"Pre-Scan"),false);
  250. Config->Pipeline = StringToBool(LookupTag(Message,"Pipeline"),false);
  251. Config->SendConfig = StringToBool(LookupTag(Message,"Send-Config"),false);
  252. // Some debug text
  253. if (Debug == true)
  254. {
  255. clog << "Configured access method " << Config->Access << endl;
  256. clog << "Version:" << Config->Version << " SingleInstance:" <<
  257. Config->SingleInstance << " PreScan: " << Config->PreScan <<
  258. " Pipeline:" << Config->Pipeline << " SendConfig:" <<
  259. Config->SendConfig << endl;
  260. }
  261. return true;
  262. }
  263. /*}}}*/
  264. // Worker::SendConfiguration - Send the config to the method /*{{{*/
  265. // ---------------------------------------------------------------------
  266. /* */
  267. bool pkgAcquire::Worker::SendConfiguration()
  268. {
  269. if (Config->SendConfig == false)
  270. return true;
  271. if (OutFd == -1)
  272. return false;
  273. string Message = "601 Configuration\n";
  274. Message.reserve(2000);
  275. /* Write out all of the configuration directives by walking the
  276. configuration tree */
  277. const Configuration::Item *Top = _config->Tree(0);
  278. for (; Top != 0;)
  279. {
  280. if (Top->Value.empty() == false)
  281. {
  282. string Line = "Config-Item: " + Top->FullTag() + "=";
  283. Line += QuoteString(Top->Value,"\n") + '\n';
  284. Message += Line;
  285. }
  286. if (Top->Child != 0)
  287. {
  288. Top = Top->Child;
  289. continue;
  290. }
  291. while (Top != 0 && Top->Next == 0)
  292. Top = Top->Parent;
  293. if (Top != 0)
  294. Top = Top->Next;
  295. }
  296. Message += '\n';
  297. if (Debug == true)
  298. clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
  299. OutQueue += Message;
  300. OutReady = true;
  301. return true;
  302. }
  303. /*}}}*/
  304. // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
  305. // ---------------------------------------------------------------------
  306. /* Send a URI Acquire message to the method */
  307. bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem *Item)
  308. {
  309. if (OutFd == -1)
  310. return false;
  311. string Message = "600 URI Acquire\n";
  312. Message.reserve(300);
  313. Message += "URI: " + Item->URI;
  314. Message += "\nFilename: " + Item->Owner->DestFile;
  315. Message += Item->Owner->Custom600Headers();
  316. Message += "\n\n";
  317. if (Debug == true)
  318. clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
  319. OutQueue += Message;
  320. OutReady = true;
  321. return true;
  322. }
  323. /*}}}*/
  324. // Worker::OutFdRead - Out bound FD is ready /*{{{*/
  325. // ---------------------------------------------------------------------
  326. /* */
  327. bool pkgAcquire::Worker::OutFdReady()
  328. {
  329. int Res = write(OutFd,OutQueue.begin(),OutQueue.length());
  330. if (Res <= 0)
  331. return MethodFailure();
  332. // Hmm.. this should never happen.
  333. if (Res < 0)
  334. return true;
  335. OutQueue.erase(0,Res);
  336. if (OutQueue.empty() == true)
  337. OutReady = false;
  338. return true;
  339. }
  340. /*}}}*/
  341. // Worker::InFdRead - In bound FD is ready /*{{{*/
  342. // ---------------------------------------------------------------------
  343. /* */
  344. bool pkgAcquire::Worker::InFdReady()
  345. {
  346. if (ReadMessages() == false)
  347. return false;
  348. RunMessages();
  349. return true;
  350. }
  351. /*}}}*/
  352. // Worker::MethodFailure - Called when the method fails /*{{{*/
  353. // ---------------------------------------------------------------------
  354. /* This is called when the method is belived to have failed, probably because
  355. read returned -1. */
  356. bool pkgAcquire::Worker::MethodFailure()
  357. {
  358. cerr << "Method " << Access << " has died unexpectedly!" << endl;
  359. if (waitpid(Process,0,0) != Process)
  360. _error->Warning("I waited but nothing was there!");
  361. Process = -1;
  362. close(InFd);
  363. close(OutFd);
  364. InFd = -1;
  365. OutFd = -1;
  366. OutReady = false;
  367. InReady = false;
  368. OutQueue = string();
  369. MessageQueue.erase(MessageQueue.begin(),MessageQueue.end());
  370. return false;
  371. }
  372. /*}}}*/
  373. // InjectConfiguration - Configuration aid for methods /*{{{*/
  374. // ---------------------------------------------------------------------
  375. /* */
  376. bool pkgInjectConfiguration(string &Message,Configuration &Cnf)
  377. {
  378. const char *I = Message.begin();
  379. unsigned int Length = strlen("Config-Item");
  380. for (; I + Length < Message.end(); I++)
  381. {
  382. // Not a config item
  383. if (I[Length] != ':' || stringcasecmp(I,I+Length,"Config-Item") != 0)
  384. continue;
  385. I += Length + 1;
  386. for (; I < Message.end() && *I == ' '; I++);
  387. const char *Equals = I;
  388. for (; Equals < Message.end() && *Equals != '='; Equals++);
  389. const char *End = Equals;
  390. for (; End < Message.end() && *End != '\n'; End++);
  391. if (End == Equals)
  392. return false;
  393. Cnf.Set(string(I,Equals-I),string(Equals+1,End-Equals-1));
  394. I = End;
  395. }
  396. return true;
  397. }
  398. /*}}}*/