acquire-worker.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-worker.cc,v 1.34 2001/05/22 04:42:54 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 <apt-pkg/strutl.h>
  21. #include <apti18n.h>
  22. #include <iostream>
  23. #include <fstream>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <signal.h>
  28. #include <stdio.h>
  29. #include <errno.h>
  30. /*}}}*/
  31. using namespace std;
  32. // Worker::Worker - Constructor for Queue startup /*{{{*/
  33. // ---------------------------------------------------------------------
  34. /* */
  35. pkgAcquire::Worker::Worker(Queue *Q,MethodConfig *Cnf,
  36. pkgAcquireStatus *Log) : Log(Log)
  37. {
  38. OwnerQ = Q;
  39. Config = Cnf;
  40. Access = Cnf->Access;
  41. CurrentItem = 0;
  42. TotalSize = 0;
  43. CurrentSize = 0;
  44. Construct();
  45. }
  46. /*}}}*/
  47. // Worker::Worker - Constructor for method config startup /*{{{*/
  48. // ---------------------------------------------------------------------
  49. /* */
  50. pkgAcquire::Worker::Worker(MethodConfig *Cnf)
  51. {
  52. OwnerQ = 0;
  53. Config = Cnf;
  54. Access = Cnf->Access;
  55. CurrentItem = 0;
  56. TotalSize = 0;
  57. CurrentSize = 0;
  58. Construct();
  59. }
  60. /*}}}*/
  61. // Worker::Construct - Constructor helper /*{{{*/
  62. // ---------------------------------------------------------------------
  63. /* */
  64. void pkgAcquire::Worker::Construct()
  65. {
  66. NextQueue = 0;
  67. NextAcquire = 0;
  68. Process = -1;
  69. InFd = -1;
  70. OutFd = -1;
  71. OutReady = false;
  72. InReady = false;
  73. Debug = _config->FindB("Debug::pkgAcquire::Worker",false);
  74. }
  75. /*}}}*/
  76. // Worker::~Worker - Destructor /*{{{*/
  77. // ---------------------------------------------------------------------
  78. /* */
  79. pkgAcquire::Worker::~Worker()
  80. {
  81. close(InFd);
  82. close(OutFd);
  83. if (Process > 0)
  84. {
  85. /* Closing of stdin is the signal to exit and die when the process
  86. indicates it needs cleanup */
  87. if (Config->NeedsCleanup == false)
  88. kill(Process,SIGINT);
  89. ExecWait(Process,Access.c_str(),true);
  90. }
  91. }
  92. /*}}}*/
  93. // Worker::Start - Start the worker process /*{{{*/
  94. // ---------------------------------------------------------------------
  95. /* This forks the method and inits the communication channel */
  96. bool pkgAcquire::Worker::Start()
  97. {
  98. // Get the method path
  99. string Method = _config->FindDir("Dir::Bin::Methods") + Access;
  100. if (FileExists(Method) == false)
  101. return _error->Error(_("The method driver %s could not be found."),Method.c_str());
  102. if (Debug == true)
  103. clog << "Starting method '" << Method << '\'' << endl;
  104. // Create the pipes
  105. int Pipes[4] = {-1,-1,-1,-1};
  106. if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0)
  107. {
  108. _error->Errno("pipe","Failed to create IPC pipe to subprocess");
  109. for (int I = 0; I != 4; I++)
  110. close(Pipes[I]);
  111. return false;
  112. }
  113. for (int I = 0; I != 4; I++)
  114. SetCloseExec(Pipes[I],true);
  115. // Fork off the process
  116. Process = ExecFork();
  117. if (Process == 0)
  118. {
  119. // Setup the FDs
  120. dup2(Pipes[1],STDOUT_FILENO);
  121. dup2(Pipes[2],STDIN_FILENO);
  122. SetCloseExec(STDOUT_FILENO,false);
  123. SetCloseExec(STDIN_FILENO,false);
  124. SetCloseExec(STDERR_FILENO,false);
  125. const char *Args[2];
  126. Args[0] = Method.c_str();
  127. Args[1] = 0;
  128. execv(Args[0],(char **)Args);
  129. cerr << "Failed to exec method " << Args[0] << endl;
  130. _exit(100);
  131. }
  132. // Fix up our FDs
  133. InFd = Pipes[0];
  134. OutFd = Pipes[3];
  135. SetNonBlock(Pipes[0],true);
  136. SetNonBlock(Pipes[3],true);
  137. close(Pipes[1]);
  138. close(Pipes[2]);
  139. OutReady = false;
  140. InReady = true;
  141. // Read the configuration data
  142. if (WaitFd(InFd) == false ||
  143. ReadMessages() == false)
  144. return _error->Error(_("Method %s did not start correctly"),Method.c_str());
  145. RunMessages();
  146. if (OwnerQ != 0)
  147. SendConfiguration();
  148. return true;
  149. }
  150. /*}}}*/
  151. // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
  152. // ---------------------------------------------------------------------
  153. /* */
  154. bool pkgAcquire::Worker::ReadMessages()
  155. {
  156. if (::ReadMessages(InFd,MessageQueue) == false)
  157. return MethodFailure();
  158. return true;
  159. }
  160. /*}}}*/
  161. // Worker::RunMessage - Empty the message queue /*{{{*/
  162. // ---------------------------------------------------------------------
  163. /* This takes the messages from the message queue and runs them through
  164. the parsers in order. */
  165. bool pkgAcquire::Worker::RunMessages()
  166. {
  167. while (MessageQueue.empty() == false)
  168. {
  169. string Message = MessageQueue.front();
  170. MessageQueue.erase(MessageQueue.begin());
  171. if (Debug == true)
  172. clog << " <- " << Access << ':' << QuoteString(Message,"\n") << endl;
  173. // Fetch the message number
  174. char *End;
  175. int Number = strtol(Message.c_str(),&End,10);
  176. if (End == Message.c_str())
  177. return _error->Error("Invalid message from method %s: %s",Access.c_str(),Message.c_str());
  178. string URI = LookupTag(Message,"URI");
  179. pkgAcquire::Queue::QItem *Itm = 0;
  180. if (URI.empty() == false)
  181. Itm = OwnerQ->FindItem(URI,this);
  182. // Determine the message number and dispatch
  183. switch (Number)
  184. {
  185. // 100 Capabilities
  186. case 100:
  187. if (Capabilities(Message) == false)
  188. return _error->Error("Unable to process Capabilities message from %s",Access.c_str());
  189. break;
  190. // 101 Log
  191. case 101:
  192. if (Debug == true)
  193. clog << " <- (log) " << LookupTag(Message,"Message") << endl;
  194. break;
  195. // 102 Status
  196. case 102:
  197. Status = LookupTag(Message,"Message");
  198. break;
  199. // 200 URI Start
  200. case 200:
  201. {
  202. if (Itm == 0)
  203. {
  204. _error->Error("Method gave invalid 200 URI Start message");
  205. break;
  206. }
  207. CurrentItem = Itm;
  208. CurrentSize = 0;
  209. TotalSize = atoi(LookupTag(Message,"Size","0").c_str());
  210. ResumePoint = atoi(LookupTag(Message,"Resume-Point","0").c_str());
  211. Itm->Owner->Start(Message,atoi(LookupTag(Message,"Size","0").c_str()));
  212. // Display update before completion
  213. if (Log != 0 && Log->MorePulses == true)
  214. Log->Pulse(Itm->Owner->GetOwner());
  215. if (Log != 0)
  216. Log->Fetch(*Itm);
  217. break;
  218. }
  219. // 201 URI Done
  220. case 201:
  221. {
  222. if (Itm == 0)
  223. {
  224. _error->Error("Method gave invalid 201 URI Done message");
  225. break;
  226. }
  227. pkgAcquire::Item *Owner = Itm->Owner;
  228. pkgAcquire::ItemDesc Desc = *Itm;
  229. // Display update before completion
  230. if (Log != 0 && Log->MorePulses == true)
  231. Log->Pulse(Owner->GetOwner());
  232. OwnerQ->ItemDone(Itm);
  233. if (TotalSize != 0 &&
  234. (unsigned)atoi(LookupTag(Message,"Size","0").c_str()) != TotalSize)
  235. _error->Warning("Bizarre Error - File size is not what the server reported %s %lu",
  236. LookupTag(Message,"Size","0").c_str(),TotalSize);
  237. Owner->Done(Message,atoi(LookupTag(Message,"Size","0").c_str()),
  238. LookupTag(Message,"MD5-Hash"),Config);
  239. ItemDone();
  240. // Log that we are done
  241. if (Log != 0)
  242. {
  243. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true ||
  244. StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
  245. {
  246. /* Hide 'hits' for local only sources - we also manage to
  247. hide gets */
  248. if (Config->LocalOnly == false)
  249. Log->IMSHit(Desc);
  250. }
  251. else
  252. Log->Done(Desc);
  253. }
  254. break;
  255. }
  256. // 400 URI Failure
  257. case 400:
  258. {
  259. if (Itm == 0)
  260. {
  261. _error->Error("Method gave invalid 400 URI Failure message");
  262. break;
  263. }
  264. // Display update before completion
  265. if (Log != 0 && Log->MorePulses == true)
  266. Log->Pulse(Itm->Owner->GetOwner());
  267. pkgAcquire::Item *Owner = Itm->Owner;
  268. pkgAcquire::ItemDesc Desc = *Itm;
  269. OwnerQ->ItemDone(Itm);
  270. Owner->Failed(Message,Config);
  271. ItemDone();
  272. if (Log != 0)
  273. Log->Fail(Desc);
  274. break;
  275. }
  276. // 401 General Failure
  277. case 401:
  278. _error->Error("Method %s General failure: %s",Access.c_str(),LookupTag(Message,"Message").c_str());
  279. break;
  280. // 403 Media Change
  281. case 403:
  282. MediaChange(Message);
  283. break;
  284. }
  285. }
  286. return true;
  287. }
  288. /*}}}*/
  289. // Worker::Capabilities - 100 Capabilities handler /*{{{*/
  290. // ---------------------------------------------------------------------
  291. /* This parses the capabilities message and dumps it into the configuration
  292. structure. */
  293. bool pkgAcquire::Worker::Capabilities(string Message)
  294. {
  295. if (Config == 0)
  296. return true;
  297. Config->Version = LookupTag(Message,"Version");
  298. Config->SingleInstance = StringToBool(LookupTag(Message,"Single-Instance"),false);
  299. Config->Pipeline = StringToBool(LookupTag(Message,"Pipeline"),false);
  300. Config->SendConfig = StringToBool(LookupTag(Message,"Send-Config"),false);
  301. Config->LocalOnly = StringToBool(LookupTag(Message,"Local-Only"),false);
  302. Config->NeedsCleanup = StringToBool(LookupTag(Message,"Needs-Cleanup"),false);
  303. Config->Removable = StringToBool(LookupTag(Message,"Removable"),false);
  304. // Some debug text
  305. if (Debug == true)
  306. {
  307. clog << "Configured access method " << Config->Access << endl;
  308. clog << "Version:" << Config->Version <<
  309. " SingleInstance:" << Config->SingleInstance <<
  310. " Pipeline:" << Config->Pipeline <<
  311. " SendConfig:" << Config->SendConfig <<
  312. " LocalOnly: " << Config->LocalOnly <<
  313. " NeedsCleanup: " << Config->NeedsCleanup <<
  314. " Removable: " << Config->Removable << endl;
  315. }
  316. return true;
  317. }
  318. /*}}}*/
  319. // Worker::MediaChange - Request a media change /*{{{*/
  320. // ---------------------------------------------------------------------
  321. /* */
  322. bool pkgAcquire::Worker::MediaChange(string Message)
  323. {
  324. if (Log == 0 || Log->MediaChange(LookupTag(Message,"Media"),
  325. LookupTag(Message,"Drive")) == false)
  326. {
  327. char S[300];
  328. snprintf(S,sizeof(S),"603 Media Changed\nFailed: true\n\n");
  329. if (Debug == true)
  330. clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl;
  331. OutQueue += S;
  332. OutReady = true;
  333. return true;
  334. }
  335. char S[300];
  336. snprintf(S,sizeof(S),"603 Media Changed\n\n");
  337. if (Debug == true)
  338. clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl;
  339. OutQueue += S;
  340. OutReady = true;
  341. return true;
  342. }
  343. /*}}}*/
  344. // Worker::SendConfiguration - Send the config to the method /*{{{*/
  345. // ---------------------------------------------------------------------
  346. /* */
  347. bool pkgAcquire::Worker::SendConfiguration()
  348. {
  349. if (Config->SendConfig == false)
  350. return true;
  351. if (OutFd == -1)
  352. return false;
  353. string Message = "601 Configuration\n";
  354. Message.reserve(2000);
  355. /* Write out all of the configuration directives by walking the
  356. configuration tree */
  357. const Configuration::Item *Top = _config->Tree(0);
  358. for (; Top != 0;)
  359. {
  360. if (Top->Value.empty() == false)
  361. {
  362. string Line = "Config-Item: " + QuoteString(Top->FullTag(),"=\"\n") + "=";
  363. Line += QuoteString(Top->Value,"\n") + '\n';
  364. Message += Line;
  365. }
  366. if (Top->Child != 0)
  367. {
  368. Top = Top->Child;
  369. continue;
  370. }
  371. while (Top != 0 && Top->Next == 0)
  372. Top = Top->Parent;
  373. if (Top != 0)
  374. Top = Top->Next;
  375. }
  376. Message += '\n';
  377. if (Debug == true)
  378. clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
  379. OutQueue += Message;
  380. OutReady = true;
  381. return true;
  382. }
  383. /*}}}*/
  384. // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
  385. // ---------------------------------------------------------------------
  386. /* Send a URI Acquire message to the method */
  387. bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem *Item)
  388. {
  389. if (OutFd == -1)
  390. return false;
  391. string Message = "600 URI Acquire\n";
  392. Message.reserve(300);
  393. Message += "URI: " + Item->URI;
  394. Message += "\nFilename: " + Item->Owner->DestFile;
  395. Message += Item->Owner->Custom600Headers();
  396. Message += "\n\n";
  397. if (Debug == true)
  398. clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
  399. OutQueue += Message;
  400. OutReady = true;
  401. return true;
  402. }
  403. /*}}}*/
  404. // Worker::OutFdRead - Out bound FD is ready /*{{{*/
  405. // ---------------------------------------------------------------------
  406. /* */
  407. bool pkgAcquire::Worker::OutFdReady()
  408. {
  409. int Res;
  410. do
  411. {
  412. Res = write(OutFd,OutQueue.c_str(),OutQueue.length());
  413. }
  414. while (Res < 0 && errno == EINTR);
  415. if (Res <= 0)
  416. return MethodFailure();
  417. // Hmm.. this should never happen.
  418. if (Res < 0)
  419. return true;
  420. OutQueue.erase(0,Res);
  421. if (OutQueue.empty() == true)
  422. OutReady = false;
  423. return true;
  424. }
  425. /*}}}*/
  426. // Worker::InFdRead - In bound FD is ready /*{{{*/
  427. // ---------------------------------------------------------------------
  428. /* */
  429. bool pkgAcquire::Worker::InFdReady()
  430. {
  431. if (ReadMessages() == false)
  432. return false;
  433. RunMessages();
  434. return true;
  435. }
  436. /*}}}*/
  437. // Worker::MethodFailure - Called when the method fails /*{{{*/
  438. // ---------------------------------------------------------------------
  439. /* This is called when the method is belived to have failed, probably because
  440. read returned -1. */
  441. bool pkgAcquire::Worker::MethodFailure()
  442. {
  443. _error->Error("Method %s has died unexpectedly!",Access.c_str());
  444. ExecWait(Process,Access.c_str(),true);
  445. Process = -1;
  446. close(InFd);
  447. close(OutFd);
  448. InFd = -1;
  449. OutFd = -1;
  450. OutReady = false;
  451. InReady = false;
  452. OutQueue = string();
  453. MessageQueue.erase(MessageQueue.begin(),MessageQueue.end());
  454. return false;
  455. }
  456. /*}}}*/
  457. // Worker::Pulse - Called periodically /*{{{*/
  458. // ---------------------------------------------------------------------
  459. /* */
  460. void pkgAcquire::Worker::Pulse()
  461. {
  462. if (CurrentItem == 0)
  463. return;
  464. struct stat Buf;
  465. if (stat(CurrentItem->Owner->DestFile.c_str(),&Buf) != 0)
  466. return;
  467. CurrentSize = Buf.st_size;
  468. // Hmm? Should not happen...
  469. if (CurrentSize > TotalSize && TotalSize != 0)
  470. TotalSize = CurrentSize;
  471. }
  472. /*}}}*/
  473. // Worker::ItemDone - Called when the current item is finished /*{{{*/
  474. // ---------------------------------------------------------------------
  475. /* */
  476. void pkgAcquire::Worker::ItemDone()
  477. {
  478. CurrentItem = 0;
  479. CurrentSize = 0;
  480. TotalSize = 0;
  481. Status = string();
  482. }
  483. /*}}}*/