acquire-worker.cc 13 KB

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