acquire-worker.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-worker.cc,v 1.22 1999/05/23 06:47:43 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. ResumePoint = atoi(LookupTag(Message,"Resume-Point","0").c_str());
  204. Itm->Owner->Start(Message,atoi(LookupTag(Message,"Size","0").c_str()));
  205. if (Log != 0)
  206. Log->Fetch(*Itm);
  207. break;
  208. }
  209. // 201 URI Done
  210. case 201:
  211. {
  212. if (Itm == 0)
  213. {
  214. _error->Error("Method gave invalid 201 URI Done message");
  215. break;
  216. }
  217. pkgAcquire::Item *Owner = Itm->Owner;
  218. pkgAcquire::ItemDesc Desc = *Itm;
  219. OwnerQ->ItemDone(Itm);
  220. Owner->Done(Message,atoi(LookupTag(Message,"Size","0").c_str()),
  221. LookupTag(Message,"MD5-Hash"));
  222. ItemDone();
  223. // Log that we are done
  224. if (Log != 0)
  225. {
  226. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true ||
  227. StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
  228. {
  229. /* Hide 'hits' for local only sources - we also manage to
  230. hide gets */
  231. if (Config->LocalOnly == false)
  232. Log->IMSHit(Desc);
  233. }
  234. else
  235. Log->Done(Desc);
  236. }
  237. break;
  238. }
  239. // 400 URI Failure
  240. case 400:
  241. {
  242. if (Itm == 0)
  243. {
  244. _error->Error("Method gave invalid 400 URI Failure message");
  245. break;
  246. }
  247. pkgAcquire::Item *Owner = Itm->Owner;
  248. pkgAcquire::ItemDesc Desc = *Itm;
  249. OwnerQ->ItemDone(Itm);
  250. Owner->Failed(Message,Config);
  251. ItemDone();
  252. if (Log != 0)
  253. Log->Fail(Desc);
  254. break;
  255. }
  256. // 401 General Failure
  257. case 401:
  258. _error->Error("Method %s General failure: %s",LookupTag(Message,"Message").c_str());
  259. break;
  260. // 403 Media Change
  261. case 403:
  262. MediaChange(Message);
  263. break;
  264. }
  265. }
  266. return true;
  267. }
  268. /*}}}*/
  269. // Worker::Capabilities - 100 Capabilities handler /*{{{*/
  270. // ---------------------------------------------------------------------
  271. /* This parses the capabilities message and dumps it into the configuration
  272. structure. */
  273. bool pkgAcquire::Worker::Capabilities(string Message)
  274. {
  275. if (Config == 0)
  276. return true;
  277. Config->Version = LookupTag(Message,"Version");
  278. Config->SingleInstance = StringToBool(LookupTag(Message,"Single-Instance"),false);
  279. Config->Pipeline = StringToBool(LookupTag(Message,"Pipeline"),false);
  280. Config->SendConfig = StringToBool(LookupTag(Message,"Send-Config"),false);
  281. Config->LocalOnly = StringToBool(LookupTag(Message,"Local-Only"),false);
  282. // Some debug text
  283. if (Debug == true)
  284. {
  285. clog << "Configured access method " << Config->Access << endl;
  286. clog << "Version:" << Config->Version << " SingleInstance:" <<
  287. Config->SingleInstance <<
  288. " Pipeline:" << Config->Pipeline << " SendConfig:" <<
  289. Config->SendConfig << endl;
  290. }
  291. return true;
  292. }
  293. /*}}}*/
  294. // Worker::MediaChange - Request a media change /*{{{*/
  295. // ---------------------------------------------------------------------
  296. /* */
  297. bool pkgAcquire::Worker::MediaChange(string Message)
  298. {
  299. if (Log == 0 || Log->MediaChange(LookupTag(Message,"Media"),
  300. LookupTag(Message,"Drive")) == false)
  301. {
  302. char S[300];
  303. sprintf(S,"603 Media Changed\nFailed: true\n\n");
  304. if (Debug == true)
  305. clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl;
  306. OutQueue += S;
  307. OutReady = true;
  308. return true;
  309. }
  310. char S[300];
  311. sprintf(S,"603 Media Changed\n\n");
  312. if (Debug == true)
  313. clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl;
  314. OutQueue += S;
  315. OutReady = true;
  316. return true;
  317. }
  318. /*}}}*/
  319. // Worker::SendConfiguration - Send the config to the method /*{{{*/
  320. // ---------------------------------------------------------------------
  321. /* */
  322. bool pkgAcquire::Worker::SendConfiguration()
  323. {
  324. if (Config->SendConfig == false)
  325. return true;
  326. if (OutFd == -1)
  327. return false;
  328. string Message = "601 Configuration\n";
  329. Message.reserve(2000);
  330. /* Write out all of the configuration directives by walking the
  331. configuration tree */
  332. const Configuration::Item *Top = _config->Tree(0);
  333. for (; Top != 0;)
  334. {
  335. if (Top->Value.empty() == false)
  336. {
  337. string Line = "Config-Item: " + Top->FullTag() + "=";
  338. Line += QuoteString(Top->Value,"\n") + '\n';
  339. Message += Line;
  340. }
  341. if (Top->Child != 0)
  342. {
  343. Top = Top->Child;
  344. continue;
  345. }
  346. while (Top != 0 && Top->Next == 0)
  347. Top = Top->Parent;
  348. if (Top != 0)
  349. Top = Top->Next;
  350. }
  351. Message += '\n';
  352. if (Debug == true)
  353. clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
  354. OutQueue += Message;
  355. OutReady = true;
  356. return true;
  357. }
  358. /*}}}*/
  359. // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
  360. // ---------------------------------------------------------------------
  361. /* Send a URI Acquire message to the method */
  362. bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem *Item)
  363. {
  364. if (OutFd == -1)
  365. return false;
  366. string Message = "600 URI Acquire\n";
  367. Message.reserve(300);
  368. Message += "URI: " + Item->URI;
  369. Message += "\nFilename: " + Item->Owner->DestFile;
  370. Message += Item->Owner->Custom600Headers();
  371. Message += "\n\n";
  372. if (Debug == true)
  373. clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
  374. OutQueue += Message;
  375. OutReady = true;
  376. return true;
  377. }
  378. /*}}}*/
  379. // Worker::OutFdRead - Out bound FD is ready /*{{{*/
  380. // ---------------------------------------------------------------------
  381. /* */
  382. bool pkgAcquire::Worker::OutFdReady()
  383. {
  384. int Res;
  385. do
  386. {
  387. Res = write(OutFd,OutQueue.begin(),OutQueue.length());
  388. }
  389. while (Res < 0 && errno == EINTR);
  390. if (Res <= 0)
  391. return MethodFailure();
  392. // Hmm.. this should never happen.
  393. if (Res < 0)
  394. return true;
  395. OutQueue.erase(0,Res);
  396. if (OutQueue.empty() == true)
  397. OutReady = false;
  398. return true;
  399. }
  400. /*}}}*/
  401. // Worker::InFdRead - In bound FD is ready /*{{{*/
  402. // ---------------------------------------------------------------------
  403. /* */
  404. bool pkgAcquire::Worker::InFdReady()
  405. {
  406. if (ReadMessages() == false)
  407. return false;
  408. RunMessages();
  409. return true;
  410. }
  411. /*}}}*/
  412. // Worker::MethodFailure - Called when the method fails /*{{{*/
  413. // ---------------------------------------------------------------------
  414. /* This is called when the method is belived to have failed, probably because
  415. read returned -1. */
  416. bool pkgAcquire::Worker::MethodFailure()
  417. {
  418. _error->Error("Method %s has died unexpectedly!",Access.c_str());
  419. if (waitpid(Process,0,0) != Process)
  420. _error->Warning("I waited but nothing was there!");
  421. Process = -1;
  422. close(InFd);
  423. close(OutFd);
  424. InFd = -1;
  425. OutFd = -1;
  426. OutReady = false;
  427. InReady = false;
  428. OutQueue = string();
  429. MessageQueue.erase(MessageQueue.begin(),MessageQueue.end());
  430. return false;
  431. }
  432. /*}}}*/
  433. // Worker::Pulse - Called periodically /*{{{*/
  434. // ---------------------------------------------------------------------
  435. /* */
  436. void pkgAcquire::Worker::Pulse()
  437. {
  438. if (CurrentItem == 0)
  439. return;
  440. struct stat Buf;
  441. if (stat(CurrentItem->Owner->DestFile.c_str(),&Buf) != 0)
  442. return;
  443. CurrentSize = Buf.st_size;
  444. }
  445. /*}}}*/
  446. // Worker::ItemDone - Called when the current item is finished /*{{{*/
  447. // ---------------------------------------------------------------------
  448. /* */
  449. void pkgAcquire::Worker::ItemDone()
  450. {
  451. CurrentItem = 0;
  452. CurrentSize = 0;
  453. TotalSize = 0;
  454. Status = string();
  455. }
  456. /*}}}*/