acquire-worker.cc 15 KB

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