acquire-worker.cc 13 KB

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