]> git.saurik.com Git - apt.git/blame - apt-pkg/acquire-worker.cc
* cmdline/apt-get.cc:
[apt.git] / apt-pkg / acquire-worker.cc
CommitLineData
0118833a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
24a0d63a 3// $Id: acquire-worker.cc,v 1.34 2001/05/22 04:42:54 jgg Exp $
0118833a
AL
4/* ######################################################################
5
6 Acquire Worker
7
3b5421b4
AL
8 The worker process can startup either as a Configuration prober
9 or as a queue runner. As a configuration prober it only reads the
10 configuration message and
11
0118833a
AL
12 ##################################################################### */
13 /*}}}*/
14// Include Files /*{{{*/
0118833a 15#include <apt-pkg/acquire-worker.h>
0a8a80e5 16#include <apt-pkg/acquire-item.h>
3b5421b4
AL
17#include <apt-pkg/configuration.h>
18#include <apt-pkg/error.h>
19#include <apt-pkg/fileutl.h>
cdcc6d34 20#include <apt-pkg/strutl.h>
3b5421b4 21
b2e465d6 22#include <apti18n.h>
24a0d63a
AL
23
24#include <iostream>
80a26ed1 25#include <sstream>
24a0d63a 26#include <fstream>
b2e465d6 27
8267fe24 28#include <sys/stat.h>
3b5421b4 29#include <unistd.h>
b0db36b1 30#include <fcntl.h>
3b5421b4 31#include <signal.h>
542ec555 32#include <stdio.h>
b0db36b1 33#include <errno.h>
3b5421b4
AL
34 /*}}}*/
35
24a0d63a
AL
36using namespace std;
37
3b5421b4
AL
38// Worker::Worker - Constructor for Queue startup /*{{{*/
39// ---------------------------------------------------------------------
40/* */
8267fe24
AL
41pkgAcquire::Worker::Worker(Queue *Q,MethodConfig *Cnf,
42 pkgAcquireStatus *Log) : Log(Log)
3b5421b4
AL
43{
44 OwnerQ = Q;
0a8a80e5
AL
45 Config = Cnf;
46 Access = Cnf->Access;
47 CurrentItem = 0;
18ef0a78
AL
48 TotalSize = 0;
49 CurrentSize = 0;
50
3b5421b4
AL
51 Construct();
52}
53 /*}}}*/
54// Worker::Worker - Constructor for method config startup /*{{{*/
55// ---------------------------------------------------------------------
56/* */
57pkgAcquire::Worker::Worker(MethodConfig *Cnf)
58{
59 OwnerQ = 0;
60 Config = Cnf;
61 Access = Cnf->Access;
0a8a80e5 62 CurrentItem = 0;
18ef0a78
AL
63 TotalSize = 0;
64 CurrentSize = 0;
0a8a80e5 65
3b5421b4
AL
66 Construct();
67}
68 /*}}}*/
69// Worker::Construct - Constructor helper /*{{{*/
70// ---------------------------------------------------------------------
71/* */
72void pkgAcquire::Worker::Construct()
73{
0a8a80e5
AL
74 NextQueue = 0;
75 NextAcquire = 0;
3b5421b4
AL
76 Process = -1;
77 InFd = -1;
78 OutFd = -1;
0a8a80e5
AL
79 OutReady = false;
80 InReady = false;
3b5421b4
AL
81 Debug = _config->FindB("Debug::pkgAcquire::Worker",false);
82}
83 /*}}}*/
84// Worker::~Worker - Destructor /*{{{*/
85// ---------------------------------------------------------------------
86/* */
87pkgAcquire::Worker::~Worker()
88{
89 close(InFd);
90 close(OutFd);
91
92 if (Process > 0)
0a8a80e5 93 {
8e5fc8f5
AL
94 /* Closing of stdin is the signal to exit and die when the process
95 indicates it needs cleanup */
96 if (Config->NeedsCleanup == false)
97 kill(Process,SIGINT);
ddc1d8d0 98 ExecWait(Process,Access.c_str(),true);
0a8a80e5 99 }
3b5421b4
AL
100}
101 /*}}}*/
102// Worker::Start - Start the worker process /*{{{*/
103// ---------------------------------------------------------------------
104/* This forks the method and inits the communication channel */
105bool pkgAcquire::Worker::Start()
106{
107 // Get the method path
108 string Method = _config->FindDir("Dir::Bin::Methods") + Access;
109 if (FileExists(Method) == false)
b2e465d6 110 return _error->Error(_("The method driver %s could not be found."),Method.c_str());
3b5421b4
AL
111
112 if (Debug == true)
113 clog << "Starting method '" << Method << '\'' << endl;
114
115 // Create the pipes
116 int Pipes[4] = {-1,-1,-1,-1};
117 if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0)
118 {
119 _error->Errno("pipe","Failed to create IPC pipe to subprocess");
120 for (int I = 0; I != 4; I++)
121 close(Pipes[I]);
122 return false;
123 }
8b89e57f 124 for (int I = 0; I != 4; I++)
4490f2de 125 SetCloseExec(Pipes[I],true);
8b89e57f 126
3b5421b4 127 // Fork off the process
54676e1a 128 Process = ExecFork();
3b5421b4
AL
129 if (Process == 0)
130 {
131 // Setup the FDs
132 dup2(Pipes[1],STDOUT_FILENO);
133 dup2(Pipes[2],STDIN_FILENO);
3b5421b4
AL
134 SetCloseExec(STDOUT_FILENO,false);
135 SetCloseExec(STDIN_FILENO,false);
136 SetCloseExec(STDERR_FILENO,false);
137
138 const char *Args[2];
139 Args[0] = Method.c_str();
140 Args[1] = 0;
141 execv(Args[0],(char **)Args);
142 cerr << "Failed to exec method " << Args[0] << endl;
0dbb95d8 143 _exit(100);
3b5421b4
AL
144 }
145
146 // Fix up our FDs
147 InFd = Pipes[0];
148 OutFd = Pipes[3];
149 SetNonBlock(Pipes[0],true);
150 SetNonBlock(Pipes[3],true);
151 close(Pipes[1]);
152 close(Pipes[2]);
0a8a80e5
AL
153 OutReady = false;
154 InReady = true;
3b5421b4
AL
155
156 // Read the configuration data
157 if (WaitFd(InFd) == false ||
158 ReadMessages() == false)
b2e465d6 159 return _error->Error(_("Method %s did not start correctly"),Method.c_str());
3b5421b4
AL
160
161 RunMessages();
8b89e57f
AL
162 if (OwnerQ != 0)
163 SendConfiguration();
3b5421b4
AL
164
165 return true;
166}
167 /*}}}*/
168// Worker::ReadMessages - Read all pending messages into the list /*{{{*/
169// ---------------------------------------------------------------------
0a8a80e5 170/* */
3b5421b4
AL
171bool pkgAcquire::Worker::ReadMessages()
172{
0a8a80e5
AL
173 if (::ReadMessages(InFd,MessageQueue) == false)
174 return MethodFailure();
3b5421b4
AL
175 return true;
176}
177 /*}}}*/
3b5421b4
AL
178// Worker::RunMessage - Empty the message queue /*{{{*/
179// ---------------------------------------------------------------------
180/* This takes the messages from the message queue and runs them through
181 the parsers in order. */
182bool pkgAcquire::Worker::RunMessages()
183{
184 while (MessageQueue.empty() == false)
185 {
186 string Message = MessageQueue.front();
187 MessageQueue.erase(MessageQueue.begin());
0a8a80e5
AL
188
189 if (Debug == true)
190 clog << " <- " << Access << ':' << QuoteString(Message,"\n") << endl;
3b5421b4
AL
191
192 // Fetch the message number
193 char *End;
194 int Number = strtol(Message.c_str(),&End,10);
195 if (End == Message.c_str())
196 return _error->Error("Invalid message from method %s: %s",Access.c_str(),Message.c_str());
197
c88edf1d
AL
198 string URI = LookupTag(Message,"URI");
199 pkgAcquire::Queue::QItem *Itm = 0;
200 if (URI.empty() == false)
201 Itm = OwnerQ->FindItem(URI,this);
196fd136
MV
202
203 // update used mirror
204 string UsedMirror = LookupTag(Message,"UsedMirror", "");
205 if (!UsedMirror.empty() &&
206 Itm &&
207 Itm->Description.find(" ") != string::npos)
208 {
209 Itm->Description.replace(0, Itm->Description.find(" "), UsedMirror);
210 // FIXME: will we need this as well?
211 //Itm->ShortDesc = UsedMirror;
212 }
bfd22fc0 213
3b5421b4
AL
214 // Determine the message number and dispatch
215 switch (Number)
216 {
0a8a80e5 217 // 100 Capabilities
3b5421b4
AL
218 case 100:
219 if (Capabilities(Message) == false)
220 return _error->Error("Unable to process Capabilities message from %s",Access.c_str());
221 break;
0a8a80e5
AL
222
223 // 101 Log
224 case 101:
225 if (Debug == true)
226 clog << " <- (log) " << LookupTag(Message,"Message") << endl;
227 break;
228
229 // 102 Status
230 case 102:
231 Status = LookupTag(Message,"Message");
232 break;
233
15d7e515
MV
234 // 103 Redirect
235 case 103:
236 {
237 if (Itm == 0)
238 {
239 _error->Error("Method gave invalid 103 Redirect message");
240 break;
241 }
242
243 string NewURI = LookupTag(Message,"New-URI",URI.c_str());
244 Itm->URI = NewURI;
245 break;
246 }
247
0a8a80e5
AL
248 // 200 URI Start
249 case 200:
c88edf1d
AL
250 {
251 if (Itm == 0)
252 {
93bf083d 253 _error->Error("Method gave invalid 200 URI Start message");
c88edf1d
AL
254 break;
255 }
8267fe24 256
c88edf1d
AL
257 CurrentItem = Itm;
258 CurrentSize = 0;
259 TotalSize = atoi(LookupTag(Message,"Size","0").c_str());
8b75eb1c 260 ResumePoint = atoi(LookupTag(Message,"Resume-Point","0").c_str());
8267fe24 261 Itm->Owner->Start(Message,atoi(LookupTag(Message,"Size","0").c_str()));
8b75eb1c 262
c5ccf175
AL
263 // Display update before completion
264 if (Log != 0 && Log->MorePulses == true)
265 Log->Pulse(Itm->Owner->GetOwner());
266
8267fe24
AL
267 if (Log != 0)
268 Log->Fetch(*Itm);
269
c88edf1d
AL
270 break;
271 }
0a8a80e5
AL
272
273 // 201 URI Done
274 case 201:
c88edf1d
AL
275 {
276 if (Itm == 0)
277 {
93bf083d 278 _error->Error("Method gave invalid 201 URI Done message");
c88edf1d
AL
279 break;
280 }
c5ccf175 281
bfd22fc0 282 pkgAcquire::Item *Owner = Itm->Owner;
8267fe24 283 pkgAcquire::ItemDesc Desc = *Itm;
c5ccf175
AL
284
285 // Display update before completion
286 if (Log != 0 && Log->MorePulses == true)
287 Log->Pulse(Owner->GetOwner());
288
be4401bf 289 OwnerQ->ItemDone(Itm);
73da43e9 290 unsigned long long const ServerSize = atoll(LookupTag(Message,"Size","0").c_str());
378fcbd6 291 if (TotalSize != 0 && ServerSize != TotalSize)
73da43e9 292 _error->Warning("Size of file %s is not what the server reported %s %llu",
378fcbd6 293 Owner->DestFile.c_str(), LookupTag(Message,"Size","0").c_str(),TotalSize);
bdae53f1 294
8a8feb29
MV
295 // see if there is a hash to verify
296 string RecivedHash;
297 HashString expectedHash(Owner->HashSum());
298 if(!expectedHash.empty())
299 {
300 string hashTag = expectedHash.HashType()+"-Hash";
95f45727
MV
301 string hashSum = LookupTag(Message, hashTag.c_str());
302 if(!hashSum.empty())
303 RecivedHash = expectedHash.HashType() + ":" + hashSum;
8a8feb29
MV
304 if(_config->FindB("Debug::pkgAcquire::Auth", false) == true)
305 {
306 clog << "201 URI Done: " << Owner->DescURI() << endl
307 << "RecivedHash: " << RecivedHash << endl
308 << "ExpectedHash: " << expectedHash.toStr()
309 << endl << endl;
310 }
311 }
378fcbd6 312 Owner->Done(Message, ServerSize, RecivedHash.c_str(), Config);
8267fe24
AL
313 ItemDone();
314
315 // Log that we are done
316 if (Log != 0)
317 {
318 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true ||
319 StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
c46824ce
AL
320 {
321 /* Hide 'hits' for local only sources - we also manage to
322 hide gets */
323 if (Config->LocalOnly == false)
324 Log->IMSHit(Desc);
325 }
8267fe24
AL
326 else
327 Log->Done(Desc);
18ef0a78 328 }
c88edf1d
AL
329 break;
330 }
0a8a80e5
AL
331
332 // 400 URI Failure
333 case 400:
c88edf1d
AL
334 {
335 if (Itm == 0)
336 {
93bf083d 337 _error->Error("Method gave invalid 400 URI Failure message");
c88edf1d
AL
338 break;
339 }
340
c5ccf175
AL
341 // Display update before completion
342 if (Log != 0 && Log->MorePulses == true)
343 Log->Pulse(Itm->Owner->GetOwner());
344
bfd22fc0 345 pkgAcquire::Item *Owner = Itm->Owner;
8267fe24 346 pkgAcquire::ItemDesc Desc = *Itm;
c88edf1d 347 OwnerQ->ItemDone(Itm);
7e5f33eb
MV
348
349 // set some status
350 if(LookupTag(Message,"FailReason") == "Timeout" ||
351 LookupTag(Message,"FailReason") == "TmpResolveFailure" ||
f0983ff2 352 LookupTag(Message,"FailReason") == "ResolveFailure" ||
7e5f33eb
MV
353 LookupTag(Message,"FailReason") == "ConnectionRefused")
354 Owner->Status = pkgAcquire::Item::StatTransientNetworkError;
355
7d8afa39 356 Owner->Failed(Message,Config);
8267fe24 357 ItemDone();
7d8afa39 358
8267fe24
AL
359 if (Log != 0)
360 Log->Fail(Desc);
7d8afa39 361
c88edf1d
AL
362 break;
363 }
0a8a80e5
AL
364
365 // 401 General Failure
366 case 401:
b2e465d6 367 _error->Error("Method %s General failure: %s",Access.c_str(),LookupTag(Message,"Message").c_str());
0a8a80e5 368 break;
542ec555
AL
369
370 // 403 Media Change
371 case 403:
372 MediaChange(Message);
373 break;
3b5421b4
AL
374 }
375 }
376 return true;
377}
378 /*}}}*/
379// Worker::Capabilities - 100 Capabilities handler /*{{{*/
380// ---------------------------------------------------------------------
381/* This parses the capabilities message and dumps it into the configuration
382 structure. */
383bool pkgAcquire::Worker::Capabilities(string Message)
384{
385 if (Config == 0)
386 return true;
387
388 Config->Version = LookupTag(Message,"Version");
389 Config->SingleInstance = StringToBool(LookupTag(Message,"Single-Instance"),false);
0a8a80e5
AL
390 Config->Pipeline = StringToBool(LookupTag(Message,"Pipeline"),false);
391 Config->SendConfig = StringToBool(LookupTag(Message,"Send-Config"),false);
e331f6ed 392 Config->LocalOnly = StringToBool(LookupTag(Message,"Local-Only"),false);
8e5fc8f5 393 Config->NeedsCleanup = StringToBool(LookupTag(Message,"Needs-Cleanup"),false);
459681d3 394 Config->Removable = StringToBool(LookupTag(Message,"Removable"),false);
3b5421b4
AL
395
396 // Some debug text
397 if (Debug == true)
398 {
399 clog << "Configured access method " << Config->Access << endl;
459681d3
AL
400 clog << "Version:" << Config->Version <<
401 " SingleInstance:" << Config->SingleInstance <<
402 " Pipeline:" << Config->Pipeline <<
403 " SendConfig:" << Config->SendConfig <<
404 " LocalOnly: " << Config->LocalOnly <<
405 " NeedsCleanup: " << Config->NeedsCleanup <<
406 " Removable: " << Config->Removable << endl;
3b5421b4
AL
407 }
408
542ec555
AL
409 return true;
410}
411 /*}}}*/
412// Worker::MediaChange - Request a media change /*{{{*/
413// ---------------------------------------------------------------------
414/* */
415bool pkgAcquire::Worker::MediaChange(string Message)
416{
80a26ed1
MV
417 int status_fd = _config->FindI("APT::Status-Fd",-1);
418 if(status_fd > 0)
419 {
420 string Media = LookupTag(Message,"Media");
421 string Drive = LookupTag(Message,"Drive");
422 ostringstream msg,status;
1a82c63e
MV
423 ioprintf(msg,_("Please insert the disc labeled: "
424 "'%s' "
425 "in the drive '%s' and press enter."),
426 Media.c_str(),Drive.c_str());
80a26ed1 427 status << "media-change: " // message
1a82c63e
MV
428 << Media << ":" // media
429 << Drive << ":" // drive
430 << msg.str() // l10n message
80a26ed1
MV
431 << endl;
432 write(status_fd, status.str().c_str(), status.str().size());
433 }
434
542ec555
AL
435 if (Log == 0 || Log->MediaChange(LookupTag(Message,"Media"),
436 LookupTag(Message,"Drive")) == false)
437 {
438 char S[300];
96bc43c4 439 snprintf(S,sizeof(S),"603 Media Changed\nFailed: true\n\n");
542ec555
AL
440 if (Debug == true)
441 clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl;
442 OutQueue += S;
443 OutReady = true;
444 return true;
445 }
446
447 char S[300];
96bc43c4 448 snprintf(S,sizeof(S),"603 Media Changed\n\n");
542ec555
AL
449 if (Debug == true)
450 clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl;
451 OutQueue += S;
452 OutReady = true;
3b5421b4
AL
453 return true;
454}
0118833a 455 /*}}}*/
0a8a80e5
AL
456// Worker::SendConfiguration - Send the config to the method /*{{{*/
457// ---------------------------------------------------------------------
458/* */
459bool pkgAcquire::Worker::SendConfiguration()
460{
461 if (Config->SendConfig == false)
462 return true;
463
464 if (OutFd == -1)
465 return false;
466
467 string Message = "601 Configuration\n";
468 Message.reserve(2000);
469
470 /* Write out all of the configuration directives by walking the
471 configuration tree */
472 const Configuration::Item *Top = _config->Tree(0);
473 for (; Top != 0;)
474 {
475 if (Top->Value.empty() == false)
476 {
b2e465d6 477 string Line = "Config-Item: " + QuoteString(Top->FullTag(),"=\"\n") + "=";
0a8a80e5
AL
478 Line += QuoteString(Top->Value,"\n") + '\n';
479 Message += Line;
480 }
481
482 if (Top->Child != 0)
483 {
484 Top = Top->Child;
485 continue;
486 }
487
488 while (Top != 0 && Top->Next == 0)
489 Top = Top->Parent;
490 if (Top != 0)
491 Top = Top->Next;
492 }
493 Message += '\n';
494
495 if (Debug == true)
496 clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
497 OutQueue += Message;
498 OutReady = true;
499
500 return true;
501}
502 /*}}}*/
503// Worker::QueueItem - Add an item to the outbound queue /*{{{*/
504// ---------------------------------------------------------------------
505/* Send a URI Acquire message to the method */
506bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem *Item)
507{
508 if (OutFd == -1)
509 return false;
510
511 string Message = "600 URI Acquire\n";
512 Message.reserve(300);
513 Message += "URI: " + Item->URI;
514 Message += "\nFilename: " + Item->Owner->DestFile;
515 Message += Item->Owner->Custom600Headers();
516 Message += "\n\n";
517
518 if (Debug == true)
519 clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
520 OutQueue += Message;
521 OutReady = true;
522
523 return true;
524}
525 /*}}}*/
526// Worker::OutFdRead - Out bound FD is ready /*{{{*/
527// ---------------------------------------------------------------------
528/* */
529bool pkgAcquire::Worker::OutFdReady()
530{
b0db36b1
AL
531 int Res;
532 do
533 {
24a0d63a 534 Res = write(OutFd,OutQueue.c_str(),OutQueue.length());
b0db36b1
AL
535 }
536 while (Res < 0 && errno == EINTR);
537
0a8a80e5
AL
538 if (Res <= 0)
539 return MethodFailure();
0a8a80e5
AL
540
541 OutQueue.erase(0,Res);
542 if (OutQueue.empty() == true)
543 OutReady = false;
544
545 return true;
546}
547 /*}}}*/
548// Worker::InFdRead - In bound FD is ready /*{{{*/
549// ---------------------------------------------------------------------
550/* */
551bool pkgAcquire::Worker::InFdReady()
552{
553 if (ReadMessages() == false)
554 return false;
555 RunMessages();
556 return true;
557}
558 /*}}}*/
559// Worker::MethodFailure - Called when the method fails /*{{{*/
560// ---------------------------------------------------------------------
561/* This is called when the method is belived to have failed, probably because
562 read returned -1. */
563bool pkgAcquire::Worker::MethodFailure()
564{
76d97c26
AL
565 _error->Error("Method %s has died unexpectedly!",Access.c_str());
566
ab7f4d7c
MV
567 // do not reap the child here to show meaningfull error to the user
568 ExecWait(Process,Access.c_str(),false);
0a8a80e5
AL
569 Process = -1;
570 close(InFd);
571 close(OutFd);
572 InFd = -1;
573 OutFd = -1;
574 OutReady = false;
575 InReady = false;
576 OutQueue = string();
577 MessageQueue.erase(MessageQueue.begin(),MessageQueue.end());
578
579 return false;
580}
581 /*}}}*/
8267fe24
AL
582// Worker::Pulse - Called periodically /*{{{*/
583// ---------------------------------------------------------------------
584/* */
585void pkgAcquire::Worker::Pulse()
586{
587 if (CurrentItem == 0)
588 return;
542ec555 589
8267fe24
AL
590 struct stat Buf;
591 if (stat(CurrentItem->Owner->DestFile.c_str(),&Buf) != 0)
592 return;
593 CurrentSize = Buf.st_size;
18ef0a78
AL
594
595 // Hmm? Should not happen...
596 if (CurrentSize > TotalSize && TotalSize != 0)
597 TotalSize = CurrentSize;
8267fe24
AL
598}
599 /*}}}*/
600// Worker::ItemDone - Called when the current item is finished /*{{{*/
601// ---------------------------------------------------------------------
602/* */
603void pkgAcquire::Worker::ItemDone()
604{
605 CurrentItem = 0;
606 CurrentSize = 0;
607 TotalSize = 0;
608 Status = string();
609}
610 /*}}}*/