acquire-worker.cc 14 KB

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