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>
37 #include <sys/types.h>
46 static void ChangeOwnerAndPermissionOfFile(char const * const requester
, char const * const file
, char const * const user
, char const * const group
, mode_t
const mode
) /*{{{*/
48 if (getuid() == 0 && strlen(user
) != 0 && strlen(group
) != 0) // if we aren't root, we can't chown, so don't try it
50 // ensure the file is owned by root and has good permissions
51 struct passwd
const * const pw
= getpwnam(user
);
52 struct group
const * const gr
= getgrnam(group
);
53 if (pw
!= NULL
&& gr
!= NULL
&& chown(file
, pw
->pw_uid
, gr
->gr_gid
) != 0)
54 _error
->WarningE(requester
, "chown to %s:%s of file %s failed", user
, group
, file
);
56 if (chmod(file
, mode
) != 0)
57 _error
->WarningE(requester
, "chmod 0%o of file %s failed", mode
, file
);
60 // Worker::Worker - Constructor for Queue startup /*{{{*/
61 // ---------------------------------------------------------------------
63 pkgAcquire::Worker::Worker(Queue
*Q
,MethodConfig
*Cnf
,
64 pkgAcquireStatus
*Log
) : Log(Log
)
76 // Worker::Worker - Constructor for method config startup /*{{{*/
77 // ---------------------------------------------------------------------
79 pkgAcquire::Worker::Worker(MethodConfig
*Cnf
)
91 // Worker::Construct - Constructor helper /*{{{*/
92 // ---------------------------------------------------------------------
94 void pkgAcquire::Worker::Construct()
103 Debug
= _config
->FindB("Debug::pkgAcquire::Worker",false);
106 // Worker::~Worker - Destructor /*{{{*/
107 // ---------------------------------------------------------------------
109 pkgAcquire::Worker::~Worker()
116 /* Closing of stdin is the signal to exit and die when the process
117 indicates it needs cleanup */
118 if (Config
->NeedsCleanup
== false)
119 kill(Process
,SIGINT
);
120 ExecWait(Process
,Access
.c_str(),true);
124 // Worker::Start - Start the worker process /*{{{*/
125 // ---------------------------------------------------------------------
126 /* This forks the method and inits the communication channel */
127 bool pkgAcquire::Worker::Start()
129 // Get the method path
130 string Method
= _config
->FindDir("Dir::Bin::Methods") + Access
;
131 if (FileExists(Method
) == false)
133 _error
->Error(_("The method driver %s could not be found."),Method
.c_str());
134 if (Access
== "https")
135 _error
->Notice(_("Is the package %s installed?"), "apt-transport-https");
140 clog
<< "Starting method '" << Method
<< '\'' << endl
;
143 int Pipes
[4] = {-1,-1,-1,-1};
144 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
146 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
147 for (int I
= 0; I
!= 4; I
++)
151 for (int I
= 0; I
!= 4; I
++)
152 SetCloseExec(Pipes
[I
],true);
154 // Fork off the process
155 Process
= ExecFork();
159 dup2(Pipes
[1],STDOUT_FILENO
);
160 dup2(Pipes
[2],STDIN_FILENO
);
161 SetCloseExec(STDOUT_FILENO
,false);
162 SetCloseExec(STDIN_FILENO
,false);
163 SetCloseExec(STDERR_FILENO
,false);
166 Args
[0] = Method
.c_str();
168 execv(Args
[0],(char **)Args
);
169 cerr
<< "Failed to exec method " << Args
[0] << endl
;
176 SetNonBlock(Pipes
[0],true);
177 SetNonBlock(Pipes
[3],true);
183 // Read the configuration data
184 if (WaitFd(InFd
) == false ||
185 ReadMessages() == false)
186 return _error
->Error(_("Method %s did not start correctly"),Method
.c_str());
195 // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
196 // ---------------------------------------------------------------------
198 bool pkgAcquire::Worker::ReadMessages()
200 if (::ReadMessages(InFd
,MessageQueue
) == false)
201 return MethodFailure();
205 // Worker::RunMessage - Empty the message queue /*{{{*/
206 // ---------------------------------------------------------------------
207 /* This takes the messages from the message queue and runs them through
208 the parsers in order. */
209 bool pkgAcquire::Worker::RunMessages()
211 while (MessageQueue
.empty() == false)
213 string Message
= MessageQueue
.front();
214 MessageQueue
.erase(MessageQueue
.begin());
217 clog
<< " <- " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
219 // Fetch the message number
221 int Number
= strtol(Message
.c_str(),&End
,10);
222 if (End
== Message
.c_str())
223 return _error
->Error("Invalid message from method %s: %s",Access
.c_str(),Message
.c_str());
225 string URI
= LookupTag(Message
,"URI");
226 pkgAcquire::Queue::QItem
*Itm
= 0;
227 if (URI
.empty() == false)
228 Itm
= OwnerQ
->FindItem(URI
,this);
230 // update used mirror
231 string UsedMirror
= LookupTag(Message
,"UsedMirror", "");
232 if (!UsedMirror
.empty() &&
234 Itm
->Description
.find(" ") != string::npos
)
236 Itm
->Description
.replace(0, Itm
->Description
.find(" "), UsedMirror
);
237 // FIXME: will we need this as well?
238 //Itm->ShortDesc = UsedMirror;
241 // Determine the message number and dispatch
246 if (Capabilities(Message
) == false)
247 return _error
->Error("Unable to process Capabilities message from %s",Access
.c_str());
253 clog
<< " <- (log) " << LookupTag(Message
,"Message") << endl
;
258 Status
= LookupTag(Message
,"Message");
266 _error
->Error("Method gave invalid 103 Redirect message");
270 string NewURI
= LookupTag(Message
,"New-URI",URI
.c_str());
275 pkgAcquire::Item
*Owner
= Itm
->Owner
;
276 pkgAcquire::ItemDesc Desc
= *Itm
;
278 // Change the status so that it can be dequeued
279 Owner
->Status
= pkgAcquire::Item::StatIdle
;
280 // Mark the item as done (taking care of all queues)
281 // and then put it in the main queue again
282 OwnerQ
->ItemDone(Itm
);
283 OwnerQ
->Owner
->Enqueue(Desc
);
295 _error
->Error("Method gave invalid 200 URI Start message");
301 TotalSize
= strtoull(LookupTag(Message
,"Size","0").c_str(), NULL
, 10);
302 ResumePoint
= strtoull(LookupTag(Message
,"Resume-Point","0").c_str(), NULL
, 10);
303 Itm
->Owner
->Start(Message
,strtoull(LookupTag(Message
,"Size","0").c_str(), NULL
, 10));
305 // Display update before completion
306 if (Log
!= 0 && Log
->MorePulses
== true)
307 Log
->Pulse(Itm
->Owner
->GetOwner());
320 _error
->Error("Method gave invalid 201 URI Done message");
324 pkgAcquire::Item
*Owner
= Itm
->Owner
;
325 pkgAcquire::ItemDesc Desc
= *Itm
;
327 if (RealFileExists(Owner
->DestFile
))
328 ChangeOwnerAndPermissionOfFile("201::URIDone", Owner
->DestFile
.c_str(), "root", "root", 0644);
330 // Display update before completion
331 if (Log
!= 0 && Log
->MorePulses
== true)
332 Log
->Pulse(Owner
->GetOwner());
334 OwnerQ
->ItemDone(Itm
);
335 unsigned long long const ServerSize
= strtoull(LookupTag(Message
,"Size","0").c_str(), NULL
, 10);
336 bool isHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false) ||
337 StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false);
338 // Using the https method the server might return 200, but the
339 // If-Modified-Since condition is not satsified, libcurl will
340 // discard the download. In this case, however, TotalSize will be
341 // set to the actual size of the file, while ServerSize will be set
342 // to 0. Therefore, if the item is marked as a hit and the
343 // downloaded size (ServerSize) is 0, we ignore TotalSize.
344 if (TotalSize
!= 0 && (!isHit
|| ServerSize
!= 0) && ServerSize
!= TotalSize
)
345 _error
->Warning("Size of file %s is not what the server reported %s %llu",
346 Owner
->DestFile
.c_str(), LookupTag(Message
,"Size","0").c_str(),TotalSize
);
348 // see if there is a hash to verify
349 HashStringList RecivedHashes
;
350 HashStringList expectedHashes
= Owner
->HashSums();
351 for (HashStringList::const_iterator hs
= expectedHashes
.begin(); hs
!= expectedHashes
.end(); ++hs
)
353 std::string
const tagname
= hs
->HashType() + "-Hash";
354 std::string
const hashsum
= LookupTag(Message
, tagname
.c_str());
355 if (hashsum
.empty() == false)
356 RecivedHashes
.push_back(HashString(hs
->HashType(), hashsum
));
359 if(_config
->FindB("Debug::pkgAcquire::Auth", false) == true)
361 std::clog
<< "201 URI Done: " << Owner
->DescURI() << endl
362 << "RecivedHash:" << endl
;
363 for (HashStringList::const_iterator hs
= RecivedHashes
.begin(); hs
!= RecivedHashes
.end(); ++hs
)
364 std::clog
<< "\t- " << hs
->toStr() << std::endl
;
365 std::clog
<< "ExpectedHash:" << endl
;
366 for (HashStringList::const_iterator hs
= expectedHashes
.begin(); hs
!= expectedHashes
.end(); ++hs
)
367 std::clog
<< "\t- " << hs
->toStr() << std::endl
;
370 Owner
->Done(Message
, ServerSize
, RecivedHashes
, Config
);
373 // Log that we are done
378 /* Hide 'hits' for local only sources - we also manage to
380 if (Config
->LocalOnly
== false)
394 std::string
const msg
= LookupTag(Message
,"Message");
395 _error
->Error("Method gave invalid 400 URI Failure message: %s", msg
.c_str());
399 // Display update before completion
400 if (Log
!= 0 && Log
->MorePulses
== true)
401 Log
->Pulse(Itm
->Owner
->GetOwner());
403 pkgAcquire::Item
*Owner
= Itm
->Owner
;
404 pkgAcquire::ItemDesc Desc
= *Itm
;
406 if (RealFileExists(Owner
->DestFile
))
407 ChangeOwnerAndPermissionOfFile("400::URIFailure", Owner
->DestFile
.c_str(), "root", "root", 0644);
409 OwnerQ
->ItemDone(Itm
);
412 if(LookupTag(Message
,"FailReason") == "Timeout" ||
413 LookupTag(Message
,"FailReason") == "TmpResolveFailure" ||
414 LookupTag(Message
,"FailReason") == "ResolveFailure" ||
415 LookupTag(Message
,"FailReason") == "ConnectionRefused")
416 Owner
->Status
= pkgAcquire::Item::StatTransientNetworkError
;
418 Owner
->Failed(Message
,Config
);
427 // 401 General Failure
429 _error
->Error("Method %s General failure: %s",Access
.c_str(),LookupTag(Message
,"Message").c_str());
434 MediaChange(Message
);
441 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
442 // ---------------------------------------------------------------------
443 /* This parses the capabilities message and dumps it into the configuration
445 bool pkgAcquire::Worker::Capabilities(string Message
)
450 Config
->Version
= LookupTag(Message
,"Version");
451 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
452 Config
->Pipeline
= StringToBool(LookupTag(Message
,"Pipeline"),false);
453 Config
->SendConfig
= StringToBool(LookupTag(Message
,"Send-Config"),false);
454 Config
->LocalOnly
= StringToBool(LookupTag(Message
,"Local-Only"),false);
455 Config
->NeedsCleanup
= StringToBool(LookupTag(Message
,"Needs-Cleanup"),false);
456 Config
->Removable
= StringToBool(LookupTag(Message
,"Removable"),false);
461 clog
<< "Configured access method " << Config
->Access
<< endl
;
462 clog
<< "Version:" << Config
->Version
<<
463 " SingleInstance:" << Config
->SingleInstance
<<
464 " Pipeline:" << Config
->Pipeline
<<
465 " SendConfig:" << Config
->SendConfig
<<
466 " LocalOnly: " << Config
->LocalOnly
<<
467 " NeedsCleanup: " << Config
->NeedsCleanup
<<
468 " Removable: " << Config
->Removable
<< endl
;
474 // Worker::MediaChange - Request a media change /*{{{*/
475 // ---------------------------------------------------------------------
477 bool pkgAcquire::Worker::MediaChange(string Message
)
479 int status_fd
= _config
->FindI("APT::Status-Fd",-1);
482 string Media
= LookupTag(Message
,"Media");
483 string Drive
= LookupTag(Message
,"Drive");
484 ostringstream msg
,status
;
485 ioprintf(msg
,_("Please insert the disc labeled: "
487 "in the drive '%s' and press enter."),
488 Media
.c_str(),Drive
.c_str());
489 status
<< "media-change: " // message
490 << Media
<< ":" // media
491 << Drive
<< ":" // drive
492 << msg
.str() // l10n message
495 std::string
const dlstatus
= status
.str();
496 FileFd::Write(status_fd
, dlstatus
.c_str(), dlstatus
.size());
499 if (Log
== 0 || Log
->MediaChange(LookupTag(Message
,"Media"),
500 LookupTag(Message
,"Drive")) == false)
503 snprintf(S
,sizeof(S
),"603 Media Changed\nFailed: true\n\n");
505 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
512 snprintf(S
,sizeof(S
),"603 Media Changed\n\n");
514 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
520 // Worker::SendConfiguration - Send the config to the method /*{{{*/
521 // ---------------------------------------------------------------------
523 bool pkgAcquire::Worker::SendConfiguration()
525 if (Config
->SendConfig
== false)
531 /* Write out all of the configuration directives by walking the
532 configuration tree */
533 std::ostringstream Message
;
534 Message
<< "601 Configuration\n";
535 _config
->Dump(Message
, NULL
, "Config-Item: %F=%V\n", false);
539 clog
<< " -> " << Access
<< ':' << QuoteString(Message
.str(),"\n") << endl
;
540 OutQueue
+= Message
.str();
546 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
547 // ---------------------------------------------------------------------
548 /* Send a URI Acquire message to the method */
549 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem
*Item
)
554 string Message
= "600 URI Acquire\n";
555 Message
.reserve(300);
556 Message
+= "URI: " + Item
->URI
;
557 Message
+= "\nFilename: " + Item
->Owner
->DestFile
;
558 HashStringList
const hsl
= Item
->Owner
->HashSums();
559 for (HashStringList::const_iterator hs
= hsl
.begin(); hs
!= hsl
.end(); ++hs
)
560 Message
+= "\nExpected-" + hs
->HashType() + ": " + hs
->HashValue();
561 if(Item
->Owner
->FileSize
> 0)
564 strprintf(MaximumSize
, "%llu", Item
->Owner
->FileSize
);
565 Message
+= "\nMaximum-Size: " + MaximumSize
;
567 Message
+= Item
->Owner
->Custom600Headers();
570 if (RealFileExists(Item
->Owner
->DestFile
))
572 std::string SandboxUser
= _config
->Find("APT::Sandbox::User");
573 ChangeOwnerAndPermissionOfFile("Item::QueueURI", Item
->Owner
->DestFile
.c_str(),
574 SandboxUser
.c_str(), "root", 0600);
578 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
585 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
586 // ---------------------------------------------------------------------
588 bool pkgAcquire::Worker::OutFdReady()
593 Res
= write(OutFd
,OutQueue
.c_str(),OutQueue
.length());
595 while (Res
< 0 && errno
== EINTR
);
598 return MethodFailure();
600 OutQueue
.erase(0,Res
);
601 if (OutQueue
.empty() == true)
607 // Worker::InFdRead - In bound FD is ready /*{{{*/
608 // ---------------------------------------------------------------------
610 bool pkgAcquire::Worker::InFdReady()
612 if (ReadMessages() == false)
618 // Worker::MethodFailure - Called when the method fails /*{{{*/
619 // ---------------------------------------------------------------------
620 /* This is called when the method is believed to have failed, probably because
622 bool pkgAcquire::Worker::MethodFailure()
624 _error
->Error("Method %s has died unexpectedly!",Access
.c_str());
626 // do not reap the child here to show meaningfull error to the user
627 ExecWait(Process
,Access
.c_str(),false);
636 MessageQueue
.erase(MessageQueue
.begin(),MessageQueue
.end());
641 // Worker::Pulse - Called periodically /*{{{*/
642 // ---------------------------------------------------------------------
644 void pkgAcquire::Worker::Pulse()
646 if (CurrentItem
== 0)
650 if (stat(CurrentItem
->Owner
->DestFile
.c_str(),&Buf
) != 0)
652 CurrentSize
= Buf
.st_size
;
654 // Hmm? Should not happen...
655 if (CurrentSize
> TotalSize
&& TotalSize
!= 0)
656 TotalSize
= CurrentSize
;
659 // Worker::ItemDone - Called when the current item is finished /*{{{*/
660 // ---------------------------------------------------------------------
662 void pkgAcquire::Worker::ItemDone()