acquire-worker.cc 13 KB

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