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-worker.h>
18 #include <apt-pkg/acquire-item.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/fileutl.h>
22 #include <apt-pkg/strutl.h>
40 // Worker::Worker - Constructor for Queue startup /*{{{*/
41 // ---------------------------------------------------------------------
43 pkgAcquire::Worker::Worker(Queue
*Q
,MethodConfig
*Cnf
,
44 pkgAcquireStatus
*Log
) : Log(Log
)
56 // Worker::Worker - Constructor for method config startup /*{{{*/
57 // ---------------------------------------------------------------------
59 pkgAcquire::Worker::Worker(MethodConfig
*Cnf
)
71 // Worker::Construct - Constructor helper /*{{{*/
72 // ---------------------------------------------------------------------
74 void pkgAcquire::Worker::Construct()
83 Debug
= _config
->FindB("Debug::pkgAcquire::Worker",false);
86 // Worker::~Worker - Destructor /*{{{*/
87 // ---------------------------------------------------------------------
89 pkgAcquire::Worker::~Worker()
96 /* Closing of stdin is the signal to exit and die when the process
97 indicates it needs cleanup */
98 if (Config
->NeedsCleanup
== false)
100 ExecWait(Process
,Access
.c_str(),true);
104 // Worker::Start - Start the worker process /*{{{*/
105 // ---------------------------------------------------------------------
106 /* This forks the method and inits the communication channel */
107 bool pkgAcquire::Worker::Start()
109 // Get the method path
110 string Method
= _config
->FindDir("Dir::Bin::Methods") + Access
;
111 if (FileExists(Method
) == false)
113 _error
->Error(_("The method driver %s could not be found."),Method
.c_str());
114 if (Access
== "https")
115 _error
->Notice(_("Is the package %s installed?"), "apt-transport-https");
120 clog
<< "Starting method '" << Method
<< '\'' << endl
;
123 int Pipes
[4] = {-1,-1,-1,-1};
124 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
126 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
127 for (int I
= 0; I
!= 4; I
++)
131 for (int I
= 0; I
!= 4; I
++)
132 SetCloseExec(Pipes
[I
],true);
134 // Fork off the process
135 Process
= ExecFork();
139 dup2(Pipes
[1],STDOUT_FILENO
);
140 dup2(Pipes
[2],STDIN_FILENO
);
141 SetCloseExec(STDOUT_FILENO
,false);
142 SetCloseExec(STDIN_FILENO
,false);
143 SetCloseExec(STDERR_FILENO
,false);
146 Args
[0] = Method
.c_str();
148 execv(Args
[0],(char **)Args
);
149 cerr
<< "Failed to exec method " << Args
[0] << endl
;
156 SetNonBlock(Pipes
[0],true);
157 SetNonBlock(Pipes
[3],true);
163 // Read the configuration data
164 if (WaitFd(InFd
) == false ||
165 ReadMessages() == false)
166 return _error
->Error(_("Method %s did not start correctly"),Method
.c_str());
175 // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
176 // ---------------------------------------------------------------------
178 bool pkgAcquire::Worker::ReadMessages()
180 if (::ReadMessages(InFd
,MessageQueue
) == false)
181 return MethodFailure();
185 // Worker::RunMessage - Empty the message queue /*{{{*/
186 // ---------------------------------------------------------------------
187 /* This takes the messages from the message queue and runs them through
188 the parsers in order. */
189 bool pkgAcquire::Worker::RunMessages()
191 while (MessageQueue
.empty() == false)
193 string Message
= MessageQueue
.front();
194 MessageQueue
.erase(MessageQueue
.begin());
197 clog
<< " <- " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
199 // Fetch the message number
201 int Number
= strtol(Message
.c_str(),&End
,10);
202 if (End
== Message
.c_str())
203 return _error
->Error("Invalid message from method %s: %s",Access
.c_str(),Message
.c_str());
205 string URI
= LookupTag(Message
,"URI");
206 pkgAcquire::Queue::QItem
*Itm
= 0;
207 if (URI
.empty() == false)
208 Itm
= OwnerQ
->FindItem(URI
,this);
210 // update used mirror
211 string UsedMirror
= LookupTag(Message
,"UsedMirror", "");
212 if (!UsedMirror
.empty() &&
214 Itm
->Description
.find(" ") != string::npos
)
216 Itm
->Description
.replace(0, Itm
->Description
.find(" "), UsedMirror
);
217 // FIXME: will we need this as well?
218 //Itm->ShortDesc = UsedMirror;
221 // Determine the message number and dispatch
226 if (Capabilities(Message
) == false)
227 return _error
->Error("Unable to process Capabilities message from %s",Access
.c_str());
233 clog
<< " <- (log) " << LookupTag(Message
,"Message") << endl
;
238 Status
= LookupTag(Message
,"Message");
246 _error
->Error("Method gave invalid 103 Redirect message");
250 string NewURI
= LookupTag(Message
,"New-URI",URI
.c_str());
255 pkgAcquire::Item
*Owner
= Itm
->Owner
;
256 pkgAcquire::ItemDesc Desc
= *Itm
;
258 // Change the status so that it can be dequeued
259 Owner
->Status
= pkgAcquire::Item::StatIdle
;
260 // Mark the item as done (taking care of all queues)
261 // and then put it in the main queue again
262 OwnerQ
->ItemDone(Itm
);
263 OwnerQ
->Owner
->Enqueue(Desc
);
275 _error
->Error("Method gave invalid 200 URI Start message");
281 TotalSize
= strtoull(LookupTag(Message
,"Size","0").c_str(), NULL
, 10);
282 ResumePoint
= strtoull(LookupTag(Message
,"Resume-Point","0").c_str(), NULL
, 10);
283 Itm
->Owner
->Start(Message
,strtoull(LookupTag(Message
,"Size","0").c_str(), NULL
, 10));
285 // Display update before completion
286 if (Log
!= 0 && Log
->MorePulses
== true)
287 Log
->Pulse(Itm
->Owner
->GetOwner());
300 _error
->Error("Method gave invalid 201 URI Done message");
304 pkgAcquire::Item
*Owner
= Itm
->Owner
;
305 pkgAcquire::ItemDesc Desc
= *Itm
;
307 // Display update before completion
308 if (Log
!= 0 && Log
->MorePulses
== true)
309 Log
->Pulse(Owner
->GetOwner());
311 OwnerQ
->ItemDone(Itm
);
312 unsigned long long const ServerSize
= strtoull(LookupTag(Message
,"Size","0").c_str(), NULL
, 10);
313 bool isHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false) ||
314 StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false);
315 // Using the https method the server might return 200, but the
316 // If-Modified-Since condition is not satsified, libcurl will
317 // discard the download. In this case, however, TotalSize will be
318 // set to the actual size of the file, while ServerSize will be set
319 // to 0. Therefore, if the item is marked as a hit and the
320 // downloaded size (ServerSize) is 0, we ignore TotalSize.
321 if (TotalSize
!= 0 && (!isHit
|| ServerSize
!= 0) && ServerSize
!= TotalSize
)
322 _error
->Warning("Size of file %s is not what the server reported %s %llu",
323 Owner
->DestFile
.c_str(), LookupTag(Message
,"Size","0").c_str(),TotalSize
);
325 // see if there is a hash to verify
327 HashString
expectedHash(Owner
->HashSum());
328 if(!expectedHash
.empty())
330 string hashTag
= expectedHash
.HashType()+"-Hash";
331 string hashSum
= LookupTag(Message
, hashTag
.c_str());
333 RecivedHash
= expectedHash
.HashType() + ":" + hashSum
;
334 if(_config
->FindB("Debug::pkgAcquire::Auth", false) == true)
336 clog
<< "201 URI Done: " << Owner
->DescURI() << endl
337 << "RecivedHash: " << RecivedHash
<< endl
338 << "ExpectedHash: " << expectedHash
.toStr()
342 Owner
->Done(Message
, ServerSize
, RecivedHash
.c_str(), Config
);
345 // Log that we are done
350 /* Hide 'hits' for local only sources - we also manage to
352 if (Config
->LocalOnly
== false)
366 _error
->Error("Method gave invalid 400 URI Failure message");
370 // Display update before completion
371 if (Log
!= 0 && Log
->MorePulses
== true)
372 Log
->Pulse(Itm
->Owner
->GetOwner());
374 pkgAcquire::Item
*Owner
= Itm
->Owner
;
375 pkgAcquire::ItemDesc Desc
= *Itm
;
376 OwnerQ
->ItemDone(Itm
);
379 if(LookupTag(Message
,"FailReason") == "Timeout" ||
380 LookupTag(Message
,"FailReason") == "TmpResolveFailure" ||
381 LookupTag(Message
,"FailReason") == "ResolveFailure" ||
382 LookupTag(Message
,"FailReason") == "ConnectionRefused")
383 Owner
->Status
= pkgAcquire::Item::StatTransientNetworkError
;
385 Owner
->Failed(Message
,Config
);
394 // 401 General Failure
396 _error
->Error("Method %s General failure: %s",Access
.c_str(),LookupTag(Message
,"Message").c_str());
401 MediaChange(Message
);
408 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
409 // ---------------------------------------------------------------------
410 /* This parses the capabilities message and dumps it into the configuration
412 bool pkgAcquire::Worker::Capabilities(string Message
)
417 Config
->Version
= LookupTag(Message
,"Version");
418 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
419 Config
->Pipeline
= StringToBool(LookupTag(Message
,"Pipeline"),false);
420 Config
->SendConfig
= StringToBool(LookupTag(Message
,"Send-Config"),false);
421 Config
->LocalOnly
= StringToBool(LookupTag(Message
,"Local-Only"),false);
422 Config
->NeedsCleanup
= StringToBool(LookupTag(Message
,"Needs-Cleanup"),false);
423 Config
->Removable
= StringToBool(LookupTag(Message
,"Removable"),false);
428 clog
<< "Configured access method " << Config
->Access
<< endl
;
429 clog
<< "Version:" << Config
->Version
<<
430 " SingleInstance:" << Config
->SingleInstance
<<
431 " Pipeline:" << Config
->Pipeline
<<
432 " SendConfig:" << Config
->SendConfig
<<
433 " LocalOnly: " << Config
->LocalOnly
<<
434 " NeedsCleanup: " << Config
->NeedsCleanup
<<
435 " Removable: " << Config
->Removable
<< endl
;
441 // Worker::MediaChange - Request a media change /*{{{*/
442 // ---------------------------------------------------------------------
444 bool pkgAcquire::Worker::MediaChange(string Message
)
446 int status_fd
= _config
->FindI("APT::Status-Fd",-1);
449 string Media
= LookupTag(Message
,"Media");
450 string Drive
= LookupTag(Message
,"Drive");
451 ostringstream msg
,status
;
452 ioprintf(msg
,_("Please insert the disc labeled: "
454 "in the drive '%s' and press enter."),
455 Media
.c_str(),Drive
.c_str());
456 status
<< "media-change: " // message
457 << Media
<< ":" // media
458 << Drive
<< ":" // drive
459 << msg
.str() // l10n message
462 std::string
const dlstatus
= status
.str();
463 FileFd::Write(status_fd
, dlstatus
.c_str(), dlstatus
.size());
466 if (Log
== 0 || Log
->MediaChange(LookupTag(Message
,"Media"),
467 LookupTag(Message
,"Drive")) == false)
470 snprintf(S
,sizeof(S
),"603 Media Changed\nFailed: true\n\n");
472 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
479 snprintf(S
,sizeof(S
),"603 Media Changed\n\n");
481 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
487 // Worker::SendConfiguration - Send the config to the method /*{{{*/
488 // ---------------------------------------------------------------------
490 bool pkgAcquire::Worker::SendConfiguration()
492 if (Config
->SendConfig
== false)
498 /* Write out all of the configuration directives by walking the
499 configuration tree */
500 std::ostringstream Message
;
501 Message
<< "601 Configuration\n";
502 _config
->Dump(Message
, NULL
, "Config-Item: %F=%V\n", false);
506 clog
<< " -> " << Access
<< ':' << QuoteString(Message
.str(),"\n") << endl
;
507 OutQueue
+= Message
.str();
513 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
514 // ---------------------------------------------------------------------
515 /* Send a URI Acquire message to the method */
516 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem
*Item
)
521 string Message
= "600 URI Acquire\n";
522 Message
.reserve(300);
523 Message
+= "URI: " + Item
->URI
;
524 Message
+= "\nFilename: " + Item
->Owner
->DestFile
;
525 Message
+= Item
->Owner
->Custom600Headers();
529 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
536 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
537 // ---------------------------------------------------------------------
539 bool pkgAcquire::Worker::OutFdReady()
544 Res
= write(OutFd
,OutQueue
.c_str(),OutQueue
.length());
546 while (Res
< 0 && errno
== EINTR
);
549 return MethodFailure();
551 OutQueue
.erase(0,Res
);
552 if (OutQueue
.empty() == true)
558 // Worker::InFdRead - In bound FD is ready /*{{{*/
559 // ---------------------------------------------------------------------
561 bool pkgAcquire::Worker::InFdReady()
563 if (ReadMessages() == false)
569 // Worker::MethodFailure - Called when the method fails /*{{{*/
570 // ---------------------------------------------------------------------
571 /* This is called when the method is believed to have failed, probably because
573 bool pkgAcquire::Worker::MethodFailure()
575 _error
->Error("Method %s has died unexpectedly!",Access
.c_str());
577 // do not reap the child here to show meaningfull error to the user
578 ExecWait(Process
,Access
.c_str(),false);
587 MessageQueue
.erase(MessageQueue
.begin(),MessageQueue
.end());
592 // Worker::Pulse - Called periodically /*{{{*/
593 // ---------------------------------------------------------------------
595 void pkgAcquire::Worker::Pulse()
597 if (CurrentItem
== 0)
601 if (stat(CurrentItem
->Owner
->DestFile
.c_str(),&Buf
) != 0)
603 CurrentSize
= Buf
.st_size
;
605 // Hmm? Should not happen...
606 if (CurrentSize
> TotalSize
&& TotalSize
!= 0)
607 TotalSize
= CurrentSize
;
610 // Worker::ItemDone - Called when the current item is finished /*{{{*/
611 // ---------------------------------------------------------------------
613 void pkgAcquire::Worker::ItemDone()