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)
112 return _error
->Error(_("The method driver %s could not be found."),Method
.c_str());
115 clog
<< "Starting method '" << Method
<< '\'' << endl
;
118 int Pipes
[4] = {-1,-1,-1,-1};
119 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
121 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
122 for (int I
= 0; I
!= 4; I
++)
126 for (int I
= 0; I
!= 4; I
++)
127 SetCloseExec(Pipes
[I
],true);
129 // Fork off the process
130 Process
= ExecFork();
134 dup2(Pipes
[1],STDOUT_FILENO
);
135 dup2(Pipes
[2],STDIN_FILENO
);
136 SetCloseExec(STDOUT_FILENO
,false);
137 SetCloseExec(STDIN_FILENO
,false);
138 SetCloseExec(STDERR_FILENO
,false);
141 Args
[0] = Method
.c_str();
143 execv(Args
[0],(char **)Args
);
144 cerr
<< "Failed to exec method " << Args
[0] << endl
;
151 SetNonBlock(Pipes
[0],true);
152 SetNonBlock(Pipes
[3],true);
158 // Read the configuration data
159 if (WaitFd(InFd
) == false ||
160 ReadMessages() == false)
161 return _error
->Error(_("Method %s did not start correctly"),Method
.c_str());
170 // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
171 // ---------------------------------------------------------------------
173 bool pkgAcquire::Worker::ReadMessages()
175 if (::ReadMessages(InFd
,MessageQueue
) == false)
176 return MethodFailure();
180 // Worker::RunMessage - Empty the message queue /*{{{*/
181 // ---------------------------------------------------------------------
182 /* This takes the messages from the message queue and runs them through
183 the parsers in order. */
184 bool pkgAcquire::Worker::RunMessages()
186 while (MessageQueue
.empty() == false)
188 string Message
= MessageQueue
.front();
189 MessageQueue
.erase(MessageQueue
.begin());
192 clog
<< " <- " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
194 // Fetch the message number
196 int Number
= strtol(Message
.c_str(),&End
,10);
197 if (End
== Message
.c_str())
198 return _error
->Error("Invalid message from method %s: %s",Access
.c_str(),Message
.c_str());
200 string URI
= LookupTag(Message
,"URI");
201 pkgAcquire::Queue::QItem
*Itm
= 0;
202 if (URI
.empty() == false)
203 Itm
= OwnerQ
->FindItem(URI
,this);
205 // update used mirror
206 string UsedMirror
= LookupTag(Message
,"UsedMirror", "");
207 if (!UsedMirror
.empty() &&
209 Itm
->Description
.find(" ") != string::npos
)
211 Itm
->Description
.replace(0, Itm
->Description
.find(" "), UsedMirror
);
212 // FIXME: will we need this as well?
213 //Itm->ShortDesc = UsedMirror;
216 // Determine the message number and dispatch
221 if (Capabilities(Message
) == false)
222 return _error
->Error("Unable to process Capabilities message from %s",Access
.c_str());
228 clog
<< " <- (log) " << LookupTag(Message
,"Message") << endl
;
233 Status
= LookupTag(Message
,"Message");
241 _error
->Error("Method gave invalid 103 Redirect message");
245 string NewURI
= LookupTag(Message
,"New-URI",URI
.c_str());
250 pkgAcquire::Item
*Owner
= Itm
->Owner
;
251 pkgAcquire::ItemDesc Desc
= *Itm
;
253 // Change the status so that it can be dequeued
254 Owner
->Status
= pkgAcquire::Item::StatIdle
;
255 // Mark the item as done (taking care of all queues)
256 // and then put it in the main queue again
257 OwnerQ
->ItemDone(Itm
);
258 OwnerQ
->Owner
->Enqueue(Desc
);
270 _error
->Error("Method gave invalid 200 URI Start message");
276 TotalSize
= strtoull(LookupTag(Message
,"Size","0").c_str(), NULL
, 10);
277 ResumePoint
= strtoull(LookupTag(Message
,"Resume-Point","0").c_str(), NULL
, 10);
278 Itm
->Owner
->Start(Message
,strtoull(LookupTag(Message
,"Size","0").c_str(), NULL
, 10));
280 // Display update before completion
281 if (Log
!= 0 && Log
->MorePulses
== true)
282 Log
->Pulse(Itm
->Owner
->GetOwner());
295 _error
->Error("Method gave invalid 201 URI Done message");
299 pkgAcquire::Item
*Owner
= Itm
->Owner
;
300 pkgAcquire::ItemDesc Desc
= *Itm
;
302 // Display update before completion
303 if (Log
!= 0 && Log
->MorePulses
== true)
304 Log
->Pulse(Owner
->GetOwner());
306 OwnerQ
->ItemDone(Itm
);
307 unsigned long long const ServerSize
= strtoull(LookupTag(Message
,"Size","0").c_str(), NULL
, 10);
308 if (TotalSize
!= 0 && ServerSize
!= TotalSize
)
309 _error
->Warning("Size of file %s is not what the server reported %s %llu",
310 Owner
->DestFile
.c_str(), LookupTag(Message
,"Size","0").c_str(),TotalSize
);
312 // see if there is a hash to verify
314 HashString
expectedHash(Owner
->HashSum());
315 if(!expectedHash
.empty())
317 string hashTag
= expectedHash
.HashType()+"-Hash";
318 string hashSum
= LookupTag(Message
, hashTag
.c_str());
320 RecivedHash
= expectedHash
.HashType() + ":" + hashSum
;
321 if(_config
->FindB("Debug::pkgAcquire::Auth", false) == true)
323 clog
<< "201 URI Done: " << Owner
->DescURI() << endl
324 << "RecivedHash: " << RecivedHash
<< endl
325 << "ExpectedHash: " << expectedHash
.toStr()
329 Owner
->Done(Message
, ServerSize
, RecivedHash
.c_str(), Config
);
332 // Log that we are done
335 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true ||
336 StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
338 /* Hide 'hits' for local only sources - we also manage to
340 if (Config
->LocalOnly
== false)
354 _error
->Error("Method gave invalid 400 URI Failure message");
358 // Display update before completion
359 if (Log
!= 0 && Log
->MorePulses
== true)
360 Log
->Pulse(Itm
->Owner
->GetOwner());
362 pkgAcquire::Item
*Owner
= Itm
->Owner
;
363 pkgAcquire::ItemDesc Desc
= *Itm
;
364 OwnerQ
->ItemDone(Itm
);
367 if(LookupTag(Message
,"FailReason") == "Timeout" ||
368 LookupTag(Message
,"FailReason") == "TmpResolveFailure" ||
369 LookupTag(Message
,"FailReason") == "ResolveFailure" ||
370 LookupTag(Message
,"FailReason") == "ConnectionRefused")
371 Owner
->Status
= pkgAcquire::Item::StatTransientNetworkError
;
373 Owner
->Failed(Message
,Config
);
382 // 401 General Failure
384 _error
->Error("Method %s General failure: %s",Access
.c_str(),LookupTag(Message
,"Message").c_str());
389 MediaChange(Message
);
396 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
397 // ---------------------------------------------------------------------
398 /* This parses the capabilities message and dumps it into the configuration
400 bool pkgAcquire::Worker::Capabilities(string Message
)
405 Config
->Version
= LookupTag(Message
,"Version");
406 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
407 Config
->Pipeline
= StringToBool(LookupTag(Message
,"Pipeline"),false);
408 Config
->SendConfig
= StringToBool(LookupTag(Message
,"Send-Config"),false);
409 Config
->LocalOnly
= StringToBool(LookupTag(Message
,"Local-Only"),false);
410 Config
->NeedsCleanup
= StringToBool(LookupTag(Message
,"Needs-Cleanup"),false);
411 Config
->Removable
= StringToBool(LookupTag(Message
,"Removable"),false);
416 clog
<< "Configured access method " << Config
->Access
<< endl
;
417 clog
<< "Version:" << Config
->Version
<<
418 " SingleInstance:" << Config
->SingleInstance
<<
419 " Pipeline:" << Config
->Pipeline
<<
420 " SendConfig:" << Config
->SendConfig
<<
421 " LocalOnly: " << Config
->LocalOnly
<<
422 " NeedsCleanup: " << Config
->NeedsCleanup
<<
423 " Removable: " << Config
->Removable
<< endl
;
429 // Worker::MediaChange - Request a media change /*{{{*/
430 // ---------------------------------------------------------------------
432 bool pkgAcquire::Worker::MediaChange(string Message
)
434 int status_fd
= _config
->FindI("APT::Status-Fd",-1);
437 string Media
= LookupTag(Message
,"Media");
438 string Drive
= LookupTag(Message
,"Drive");
439 ostringstream msg
,status
;
440 ioprintf(msg
,_("Please insert the disc labeled: "
442 "in the drive '%s' and press enter."),
443 Media
.c_str(),Drive
.c_str());
444 status
<< "media-change: " // message
445 << Media
<< ":" // media
446 << Drive
<< ":" // drive
447 << msg
.str() // l10n message
450 std::string
const dlstatus
= status
.str();
451 FileFd::Write(status_fd
, dlstatus
.c_str(), dlstatus
.size());
454 if (Log
== 0 || Log
->MediaChange(LookupTag(Message
,"Media"),
455 LookupTag(Message
,"Drive")) == false)
458 snprintf(S
,sizeof(S
),"603 Media Changed\nFailed: true\n\n");
460 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
467 snprintf(S
,sizeof(S
),"603 Media Changed\n\n");
469 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
475 // Worker::SendConfiguration - Send the config to the method /*{{{*/
476 // ---------------------------------------------------------------------
478 bool pkgAcquire::Worker::SendConfiguration()
480 if (Config
->SendConfig
== false)
486 /* Write out all of the configuration directives by walking the
487 configuration tree */
488 std::ostringstream Message
;
489 Message
<< "601 Configuration\n";
490 _config
->Dump(Message
, NULL
, "Config-Item: %F=%V\n", false);
494 clog
<< " -> " << Access
<< ':' << QuoteString(Message
.str(),"\n") << endl
;
495 OutQueue
+= Message
.str();
501 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
502 // ---------------------------------------------------------------------
503 /* Send a URI Acquire message to the method */
504 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem
*Item
)
509 string Message
= "600 URI Acquire\n";
510 Message
.reserve(300);
511 Message
+= "URI: " + Item
->URI
;
512 Message
+= "\nFilename: " + Item
->Owner
->DestFile
;
513 Message
+= Item
->Owner
->Custom600Headers();
517 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
524 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
525 // ---------------------------------------------------------------------
527 bool pkgAcquire::Worker::OutFdReady()
532 Res
= write(OutFd
,OutQueue
.c_str(),OutQueue
.length());
534 while (Res
< 0 && errno
== EINTR
);
537 return MethodFailure();
539 OutQueue
.erase(0,Res
);
540 if (OutQueue
.empty() == true)
546 // Worker::InFdRead - In bound FD is ready /*{{{*/
547 // ---------------------------------------------------------------------
549 bool pkgAcquire::Worker::InFdReady()
551 if (ReadMessages() == false)
557 // Worker::MethodFailure - Called when the method fails /*{{{*/
558 // ---------------------------------------------------------------------
559 /* This is called when the method is belived to have failed, probably because
561 bool pkgAcquire::Worker::MethodFailure()
563 _error
->Error("Method %s has died unexpectedly!",Access
.c_str());
565 // do not reap the child here to show meaningfull error to the user
566 ExecWait(Process
,Access
.c_str(),false);
575 MessageQueue
.erase(MessageQueue
.begin(),MessageQueue
.end());
580 // Worker::Pulse - Called periodically /*{{{*/
581 // ---------------------------------------------------------------------
583 void pkgAcquire::Worker::Pulse()
585 if (CurrentItem
== 0)
589 if (stat(CurrentItem
->Owner
->DestFile
.c_str(),&Buf
) != 0)
591 CurrentSize
= Buf
.st_size
;
593 // Hmm? Should not happen...
594 if (CurrentSize
> TotalSize
&& TotalSize
!= 0)
595 TotalSize
= CurrentSize
;
598 // Worker::ItemDone - Called when the current item is finished /*{{{*/
599 // ---------------------------------------------------------------------
601 void pkgAcquire::Worker::ItemDone()