acquire-worker.cc 14 KB

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