acquire-worker.cc 13 KB

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