acquire-worker.cc 14 KB

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