1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-worker.cc,v 1.34 2001/05/22 04:42:54 jgg Exp $
4 /* ######################################################################
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
12 ##################################################################### */
14 // Include Files /*{{{*/
17 #include <apt-pkg/acquire.h>
18 #include <apt-pkg/acquire-worker.h>
19 #include <apt-pkg/acquire-item.h>
20 #include <apt-pkg/configuration.h>
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/fileutl.h>
23 #include <apt-pkg/strutl.h>
24 #include <apt-pkg/hashes.h>
43 // Worker::Worker - Constructor for Queue startup /*{{{*/
44 // ---------------------------------------------------------------------
46 pkgAcquire::Worker::Worker(Queue
*Q
,MethodConfig
*Cnf
,
47 pkgAcquireStatus
*Log
) : Log(Log
)
59 // Worker::Worker - Constructor for method config startup /*{{{*/
60 // ---------------------------------------------------------------------
62 pkgAcquire::Worker::Worker(MethodConfig
*Cnf
)
74 // Worker::Construct - Constructor helper /*{{{*/
75 // ---------------------------------------------------------------------
77 void pkgAcquire::Worker::Construct()
86 Debug
= _config
->FindB("Debug::pkgAcquire::Worker",false);
89 // Worker::~Worker - Destructor /*{{{*/
90 // ---------------------------------------------------------------------
92 pkgAcquire::Worker::~Worker()
99 /* Closing of stdin is the signal to exit and die when the process
100 indicates it needs cleanup */
101 if (Config
->NeedsCleanup
== false)
102 kill(Process
,SIGINT
);
103 ExecWait(Process
,Access
.c_str(),true);
107 // Worker::Start - Start the worker process /*{{{*/
108 // ---------------------------------------------------------------------
109 /* This forks the method and inits the communication channel */
110 bool pkgAcquire::Worker::Start()
112 // Get the method path
113 string Method
= _config
->FindDir("Dir::Bin::Methods") + Access
;
114 if (FileExists(Method
) == false)
116 _error
->Error(_("The method driver %s could not be found."),Method
.c_str());
117 if (Access
== "https")
118 _error
->Notice(_("Is the package %s installed?"), "apt-transport-https");
123 clog
<< "Starting method '" << Method
<< '\'' << endl
;
126 int Pipes
[4] = {-1,-1,-1,-1};
127 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
129 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
130 for (int I
= 0; I
!= 4; I
++)
134 for (int I
= 0; I
!= 4; I
++)
135 SetCloseExec(Pipes
[I
],true);
137 // Fork off the process
138 Process
= ExecFork();
142 dup2(Pipes
[1],STDOUT_FILENO
);
143 dup2(Pipes
[2],STDIN_FILENO
);
144 SetCloseExec(STDOUT_FILENO
,false);
145 SetCloseExec(STDIN_FILENO
,false);
146 SetCloseExec(STDERR_FILENO
,false);
149 Args
[0] = Method
.c_str();
151 execv(Args
[0],(char **)Args
);
152 cerr
<< "Failed to exec method " << Args
[0] << endl
;
159 SetNonBlock(Pipes
[0],true);
160 SetNonBlock(Pipes
[3],true);
166 // Read the configuration data
167 if (WaitFd(InFd
) == false ||
168 ReadMessages() == false)
169 return _error
->Error(_("Method %s did not start correctly"),Method
.c_str());
178 // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
179 // ---------------------------------------------------------------------
181 bool pkgAcquire::Worker::ReadMessages()
183 if (::ReadMessages(InFd
,MessageQueue
) == false)
184 return MethodFailure();
188 // Worker::RunMessage - Empty the message queue /*{{{*/
189 // ---------------------------------------------------------------------
190 /* This takes the messages from the message queue and runs them through
191 the parsers in order. */
192 bool pkgAcquire::Worker::RunMessages()
194 while (MessageQueue
.empty() == false)
196 string Message
= MessageQueue
.front();
197 MessageQueue
.erase(MessageQueue
.begin());
200 clog
<< " <- " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
202 // Fetch the message number
204 int Number
= strtol(Message
.c_str(),&End
,10);
205 if (End
== Message
.c_str())
206 return _error
->Error("Invalid message from method %s: %s",Access
.c_str(),Message
.c_str());
208 string URI
= LookupTag(Message
,"URI");
209 pkgAcquire::Queue::QItem
*Itm
= 0;
210 if (URI
.empty() == false)
211 Itm
= OwnerQ
->FindItem(URI
,this);
213 // update used mirror
214 string UsedMirror
= LookupTag(Message
,"UsedMirror", "");
215 if (!UsedMirror
.empty() &&
217 Itm
->Description
.find(" ") != string::npos
)
219 Itm
->Description
.replace(0, Itm
->Description
.find(" "), UsedMirror
);
220 // FIXME: will we need this as well?
221 //Itm->ShortDesc = UsedMirror;
224 // Determine the message number and dispatch
229 if (Capabilities(Message
) == false)
230 return _error
->Error("Unable to process Capabilities message from %s",Access
.c_str());
236 clog
<< " <- (log) " << LookupTag(Message
,"Message") << endl
;
241 Status
= LookupTag(Message
,"Message");
249 _error
->Error("Method gave invalid 103 Redirect message");
253 string NewURI
= LookupTag(Message
,"New-URI",URI
.c_str());
258 pkgAcquire::Item
*Owner
= Itm
->Owner
;
259 pkgAcquire::ItemDesc Desc
= *Itm
;
261 // Change the status so that it can be dequeued
262 Owner
->Status
= pkgAcquire::Item::StatIdle
;
263 // Mark the item as done (taking care of all queues)
264 // and then put it in the main queue again
265 OwnerQ
->ItemDone(Itm
);
266 OwnerQ
->Owner
->Enqueue(Desc
);
278 _error
->Error("Method gave invalid 200 URI Start message");
284 TotalSize
= strtoull(LookupTag(Message
,"Size","0").c_str(), NULL
, 10);
285 ResumePoint
= strtoull(LookupTag(Message
,"Resume-Point","0").c_str(), NULL
, 10);
286 Itm
->Owner
->Start(Message
,strtoull(LookupTag(Message
,"Size","0").c_str(), NULL
, 10));
288 // Display update before completion
289 if (Log
!= 0 && Log
->MorePulses
== true)
290 Log
->Pulse(Itm
->Owner
->GetOwner());
303 _error
->Error("Method gave invalid 201 URI Done message");
307 pkgAcquire::Item
*Owner
= Itm
->Owner
;
308 pkgAcquire::ItemDesc Desc
= *Itm
;
310 // Display update before completion
311 if (Log
!= 0 && Log
->MorePulses
== true)
312 Log
->Pulse(Owner
->GetOwner());
314 OwnerQ
->ItemDone(Itm
);
315 unsigned long long const ServerSize
= strtoull(LookupTag(Message
,"Size","0").c_str(), NULL
, 10);
316 bool isHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false) ||
317 StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false);
318 // Using the https method the server might return 200, but the
319 // If-Modified-Since condition is not satsified, libcurl will
320 // discard the download. In this case, however, TotalSize will be
321 // set to the actual size of the file, while ServerSize will be set
322 // to 0. Therefore, if the item is marked as a hit and the
323 // downloaded size (ServerSize) is 0, we ignore TotalSize.
324 if (TotalSize
!= 0 && (!isHit
|| ServerSize
!= 0) && ServerSize
!= TotalSize
)
325 _error
->Warning("Size of file %s is not what the server reported %s %llu",
326 Owner
->DestFile
.c_str(), LookupTag(Message
,"Size","0").c_str(),TotalSize
);
328 // see if there is a hash to verify
329 HashStringList RecivedHashes
;
330 HashStringList expectedHashes
= Owner
->HashSums();
331 for (HashStringList::const_iterator hs
= expectedHashes
.begin(); hs
!= expectedHashes
.end(); ++hs
)
333 std::string
const tagname
= hs
->HashType() + "-Hash";
334 std::string
const hashsum
= LookupTag(Message
, tagname
.c_str());
335 if (hashsum
.empty() == false)
336 RecivedHashes
.push_back(HashString(hs
->HashType(), hashsum
));
339 if(_config
->FindB("Debug::pkgAcquire::Auth", false) == true)
341 std::clog
<< "201 URI Done: " << Owner
->DescURI() << endl
342 << "RecivedHash:" << endl
;
343 for (HashStringList::const_iterator hs
= RecivedHashes
.begin(); hs
!= RecivedHashes
.end(); ++hs
)
344 std::clog
<< "\t- " << hs
->toStr() << std::endl
;
345 std::clog
<< "ExpectedHash:" << endl
;
346 for (HashStringList::const_iterator hs
= expectedHashes
.begin(); hs
!= expectedHashes
.end(); ++hs
)
347 std::clog
<< "\t- " << hs
->toStr() << std::endl
;
350 Owner
->Done(Message
, ServerSize
, RecivedHashes
, Config
);
353 // Log that we are done
358 /* Hide 'hits' for local only sources - we also manage to
360 if (Config
->LocalOnly
== false)
374 _error
->Error("Method gave invalid 400 URI Failure message");
378 // Display update before completion
379 if (Log
!= 0 && Log
->MorePulses
== true)
380 Log
->Pulse(Itm
->Owner
->GetOwner());
382 pkgAcquire::Item
*Owner
= Itm
->Owner
;
383 pkgAcquire::ItemDesc Desc
= *Itm
;
384 OwnerQ
->ItemDone(Itm
);
387 if(LookupTag(Message
,"FailReason") == "Timeout" ||
388 LookupTag(Message
,"FailReason") == "TmpResolveFailure" ||
389 LookupTag(Message
,"FailReason") == "ResolveFailure" ||
390 LookupTag(Message
,"FailReason") == "ConnectionRefused")
391 Owner
->Status
= pkgAcquire::Item::StatTransientNetworkError
;
393 Owner
->Failed(Message
,Config
);
402 // 401 General Failure
404 _error
->Error("Method %s General failure: %s",Access
.c_str(),LookupTag(Message
,"Message").c_str());
409 MediaChange(Message
);
416 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
417 // ---------------------------------------------------------------------
418 /* This parses the capabilities message and dumps it into the configuration
420 bool pkgAcquire::Worker::Capabilities(string Message
)
425 Config
->Version
= LookupTag(Message
,"Version");
426 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
427 Config
->Pipeline
= StringToBool(LookupTag(Message
,"Pipeline"),false);
428 Config
->SendConfig
= StringToBool(LookupTag(Message
,"Send-Config"),false);
429 Config
->LocalOnly
= StringToBool(LookupTag(Message
,"Local-Only"),false);
430 Config
->NeedsCleanup
= StringToBool(LookupTag(Message
,"Needs-Cleanup"),false);
431 Config
->Removable
= StringToBool(LookupTag(Message
,"Removable"),false);
436 clog
<< "Configured access method " << Config
->Access
<< endl
;
437 clog
<< "Version:" << Config
->Version
<<
438 " SingleInstance:" << Config
->SingleInstance
<<
439 " Pipeline:" << Config
->Pipeline
<<
440 " SendConfig:" << Config
->SendConfig
<<
441 " LocalOnly: " << Config
->LocalOnly
<<
442 " NeedsCleanup: " << Config
->NeedsCleanup
<<
443 " Removable: " << Config
->Removable
<< endl
;
449 // Worker::MediaChange - Request a media change /*{{{*/
450 // ---------------------------------------------------------------------
452 bool pkgAcquire::Worker::MediaChange(string Message
)
454 int status_fd
= _config
->FindI("APT::Status-Fd",-1);
457 string Media
= LookupTag(Message
,"Media");
458 string Drive
= LookupTag(Message
,"Drive");
459 ostringstream msg
,status
;
460 ioprintf(msg
,_("Please insert the disc labeled: "
462 "in the drive '%s' and press enter."),
463 Media
.c_str(),Drive
.c_str());
464 status
<< "media-change: " // message
465 << Media
<< ":" // media
466 << Drive
<< ":" // drive
467 << msg
.str() // l10n message
470 std::string
const dlstatus
= status
.str();
471 FileFd::Write(status_fd
, dlstatus
.c_str(), dlstatus
.size());
474 if (Log
== 0 || Log
->MediaChange(LookupTag(Message
,"Media"),
475 LookupTag(Message
,"Drive")) == false)
478 snprintf(S
,sizeof(S
),"603 Media Changed\nFailed: true\n\n");
480 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
487 snprintf(S
,sizeof(S
),"603 Media Changed\n\n");
489 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
495 // Worker::SendConfiguration - Send the config to the method /*{{{*/
496 // ---------------------------------------------------------------------
498 bool pkgAcquire::Worker::SendConfiguration()
500 if (Config
->SendConfig
== false)
506 /* Write out all of the configuration directives by walking the
507 configuration tree */
508 std::ostringstream Message
;
509 Message
<< "601 Configuration\n";
510 _config
->Dump(Message
, NULL
, "Config-Item: %F=%V\n", false);
514 clog
<< " -> " << Access
<< ':' << QuoteString(Message
.str(),"\n") << endl
;
515 OutQueue
+= Message
.str();
521 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
522 // ---------------------------------------------------------------------
523 /* Send a URI Acquire message to the method */
524 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem
*Item
)
529 string Message
= "600 URI Acquire\n";
530 Message
.reserve(300);
531 Message
+= "URI: " + Item
->URI
;
532 Message
+= "\nFilename: " + Item
->Owner
->DestFile
;
533 HashStringList
const hsl
= Item
->Owner
->HashSums();
534 for (HashStringList::const_iterator hs
= hsl
.begin(); hs
!= hsl
.end(); ++hs
)
535 Message
+= "\nExpected-" + hs
->HashType() + ": " + hs
->HashValue();
536 Message
+= Item
->Owner
->Custom600Headers();
540 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
547 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
548 // ---------------------------------------------------------------------
550 bool pkgAcquire::Worker::OutFdReady()
555 Res
= write(OutFd
,OutQueue
.c_str(),OutQueue
.length());
557 while (Res
< 0 && errno
== EINTR
);
560 return MethodFailure();
562 OutQueue
.erase(0,Res
);
563 if (OutQueue
.empty() == true)
569 // Worker::InFdRead - In bound FD is ready /*{{{*/
570 // ---------------------------------------------------------------------
572 bool pkgAcquire::Worker::InFdReady()
574 if (ReadMessages() == false)
580 // Worker::MethodFailure - Called when the method fails /*{{{*/
581 // ---------------------------------------------------------------------
582 /* This is called when the method is believed to have failed, probably because
584 bool pkgAcquire::Worker::MethodFailure()
586 _error
->Error("Method %s has died unexpectedly!",Access
.c_str());
588 // do not reap the child here to show meaningfull error to the user
589 ExecWait(Process
,Access
.c_str(),false);
598 MessageQueue
.erase(MessageQueue
.begin(),MessageQueue
.end());
603 // Worker::Pulse - Called periodically /*{{{*/
604 // ---------------------------------------------------------------------
606 void pkgAcquire::Worker::Pulse()
608 if (CurrentItem
== 0)
612 if (stat(CurrentItem
->Owner
->DestFile
.c_str(),&Buf
) != 0)
614 CurrentSize
= Buf
.st_size
;
616 // Hmm? Should not happen...
617 if (CurrentSize
> TotalSize
&& TotalSize
!= 0)
618 TotalSize
= CurrentSize
;
621 // Worker::ItemDone - Called when the current item is finished /*{{{*/
622 // ---------------------------------------------------------------------
624 void pkgAcquire::Worker::ItemDone()