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 /*{{{*/
16 #pragma implementation "apt-pkg/acquire-worker.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>
41 // Worker::Worker - Constructor for Queue startup /*{{{*/
42 // ---------------------------------------------------------------------
44 pkgAcquire::Worker::Worker(Queue
*Q
,MethodConfig
*Cnf
,
45 pkgAcquireStatus
*Log
) : Log(Log
)
57 // Worker::Worker - Constructor for method config startup /*{{{*/
58 // ---------------------------------------------------------------------
60 pkgAcquire::Worker::Worker(MethodConfig
*Cnf
)
72 // Worker::Construct - Constructor helper /*{{{*/
73 // ---------------------------------------------------------------------
75 void pkgAcquire::Worker::Construct()
84 Debug
= _config
->FindB("Debug::pkgAcquire::Worker",false);
87 // Worker::~Worker - Destructor /*{{{*/
88 // ---------------------------------------------------------------------
90 pkgAcquire::Worker::~Worker()
97 /* Closing of stdin is the signal to exit and die when the process
98 indicates it needs cleanup */
99 if (Config
->NeedsCleanup
== false)
100 kill(Process
,SIGINT
);
101 ExecWait(Process
,Access
.c_str(),true);
105 // Worker::Start - Start the worker process /*{{{*/
106 // ---------------------------------------------------------------------
107 /* This forks the method and inits the communication channel */
108 bool pkgAcquire::Worker::Start()
110 // Get the method path
111 string Method
= _config
->FindDir("Dir::Bin::Methods") + Access
;
112 if (FileExists(Method
) == false)
113 return _error
->Error(_("The method driver %s could not be found."),Method
.c_str());
116 clog
<< "Starting method '" << Method
<< '\'' << endl
;
119 int Pipes
[4] = {-1,-1,-1,-1};
120 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
122 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
123 for (int I
= 0; I
!= 4; I
++)
127 for (int I
= 0; I
!= 4; I
++)
128 SetCloseExec(Pipes
[I
],true);
130 // Fork off the process
131 Process
= ExecFork();
135 dup2(Pipes
[1],STDOUT_FILENO
);
136 dup2(Pipes
[2],STDIN_FILENO
);
137 SetCloseExec(STDOUT_FILENO
,false);
138 SetCloseExec(STDIN_FILENO
,false);
139 SetCloseExec(STDERR_FILENO
,false);
142 Args
[0] = Method
.c_str();
144 execv(Args
[0],(char **)Args
);
145 cerr
<< "Failed to exec method " << Args
[0] << endl
;
152 SetNonBlock(Pipes
[0],true);
153 SetNonBlock(Pipes
[3],true);
159 // Read the configuration data
160 if (WaitFd(InFd
) == false ||
161 ReadMessages() == false)
162 return _error
->Error(_("Method %s did not start correctly"),Method
.c_str());
171 // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
172 // ---------------------------------------------------------------------
174 bool pkgAcquire::Worker::ReadMessages()
176 if (::ReadMessages(InFd
,MessageQueue
) == false)
177 return MethodFailure();
181 // Worker::RunMessage - Empty the message queue /*{{{*/
182 // ---------------------------------------------------------------------
183 /* This takes the messages from the message queue and runs them through
184 the parsers in order. */
185 bool pkgAcquire::Worker::RunMessages()
187 while (MessageQueue
.empty() == false)
189 string Message
= MessageQueue
.front();
190 MessageQueue
.erase(MessageQueue
.begin());
193 clog
<< " <- " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
195 // Fetch the message number
197 int Number
= strtol(Message
.c_str(),&End
,10);
198 if (End
== Message
.c_str())
199 return _error
->Error("Invalid message from method %s: %s",Access
.c_str(),Message
.c_str());
201 string URI
= LookupTag(Message
,"URI");
202 pkgAcquire::Queue::QItem
*Itm
= 0;
203 if (URI
.empty() == false)
204 Itm
= OwnerQ
->FindItem(URI
,this);
206 // Determine the message number and dispatch
211 if (Capabilities(Message
) == false)
212 return _error
->Error("Unable to process Capabilities message from %s",Access
.c_str());
218 clog
<< " <- (log) " << LookupTag(Message
,"Message") << endl
;
223 Status
= LookupTag(Message
,"Message");
231 _error
->Error("Method gave invalid 200 URI Start message");
237 TotalSize
= atoi(LookupTag(Message
,"Size","0").c_str());
238 ResumePoint
= atoi(LookupTag(Message
,"Resume-Point","0").c_str());
239 Itm
->Owner
->Start(Message
,atoi(LookupTag(Message
,"Size","0").c_str()));
241 // Display update before completion
242 if (Log
!= 0 && Log
->MorePulses
== true)
243 Log
->Pulse(Itm
->Owner
->GetOwner());
256 _error
->Error("Method gave invalid 201 URI Done message");
260 pkgAcquire::Item
*Owner
= Itm
->Owner
;
261 pkgAcquire::ItemDesc Desc
= *Itm
;
263 // Display update before completion
264 if (Log
!= 0 && Log
->MorePulses
== true)
265 Log
->Pulse(Owner
->GetOwner());
267 OwnerQ
->ItemDone(Itm
);
268 if (TotalSize
!= 0 &&
269 (unsigned)atoi(LookupTag(Message
,"Size","0").c_str()) != TotalSize
)
270 _error
->Warning("Bizarre Error - File size is not what the server reported %s %lu",
271 LookupTag(Message
,"Size","0").c_str(),TotalSize
);
273 Owner
->Done(Message
,atoi(LookupTag(Message
,"Size","0").c_str()),
274 LookupTag(Message
,"MD5-Hash"),Config
);
277 // Log that we are done
280 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true ||
281 StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
283 /* Hide 'hits' for local only sources - we also manage to
285 if (Config
->LocalOnly
== false)
299 _error
->Error("Method gave invalid 400 URI Failure message");
303 // Display update before completion
304 if (Log
!= 0 && Log
->MorePulses
== true)
305 Log
->Pulse(Itm
->Owner
->GetOwner());
307 pkgAcquire::Item
*Owner
= Itm
->Owner
;
308 pkgAcquire::ItemDesc Desc
= *Itm
;
309 OwnerQ
->ItemDone(Itm
);
312 if(LookupTag(Message
,"FailReason") == "Timeout" ||
313 LookupTag(Message
,"FailReason") == "TmpResolveFailure" ||
314 LookupTag(Message
,"FailReason") == "ConnectionRefused")
315 Owner
->Status
= pkgAcquire::Item::StatTransientNetworkError
;
317 Owner
->Failed(Message
,Config
);
326 // 401 General Failure
328 _error
->Error("Method %s General failure: %s",Access
.c_str(),LookupTag(Message
,"Message").c_str());
333 MediaChange(Message
);
340 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
341 // ---------------------------------------------------------------------
342 /* This parses the capabilities message and dumps it into the configuration
344 bool pkgAcquire::Worker::Capabilities(string Message
)
349 Config
->Version
= LookupTag(Message
,"Version");
350 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
351 Config
->Pipeline
= StringToBool(LookupTag(Message
,"Pipeline"),false);
352 Config
->SendConfig
= StringToBool(LookupTag(Message
,"Send-Config"),false);
353 Config
->LocalOnly
= StringToBool(LookupTag(Message
,"Local-Only"),false);
354 Config
->NeedsCleanup
= StringToBool(LookupTag(Message
,"Needs-Cleanup"),false);
355 Config
->Removable
= StringToBool(LookupTag(Message
,"Removable"),false);
360 clog
<< "Configured access method " << Config
->Access
<< endl
;
361 clog
<< "Version:" << Config
->Version
<<
362 " SingleInstance:" << Config
->SingleInstance
<<
363 " Pipeline:" << Config
->Pipeline
<<
364 " SendConfig:" << Config
->SendConfig
<<
365 " LocalOnly: " << Config
->LocalOnly
<<
366 " NeedsCleanup: " << Config
->NeedsCleanup
<<
367 " Removable: " << Config
->Removable
<< endl
;
373 // Worker::MediaChange - Request a media change /*{{{*/
374 // ---------------------------------------------------------------------
376 bool pkgAcquire::Worker::MediaChange(string Message
)
378 int status_fd
= _config
->FindI("APT::Status-Fd",-1);
381 string Media
= LookupTag(Message
,"Media");
382 string Drive
= LookupTag(Message
,"Drive");
383 ostringstream msg
,status
;
384 ioprintf(msg
,_("Please insert the disc labeled: "
386 "in the drive '%s' and press enter."),
387 Media
.c_str(),Drive
.c_str());
388 status
<< "media-change: " // message
389 << Media
<< ":" // media
390 << Drive
<< ":" // drive
391 << msg
.str() // l10n message
393 write(status_fd
, status
.str().c_str(), status
.str().size());
396 if (Log
== 0 || Log
->MediaChange(LookupTag(Message
,"Media"),
397 LookupTag(Message
,"Drive")) == false)
400 snprintf(S
,sizeof(S
),"603 Media Changed\nFailed: true\n\n");
402 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
409 snprintf(S
,sizeof(S
),"603 Media Changed\n\n");
411 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
417 // Worker::SendConfiguration - Send the config to the method /*{{{*/
418 // ---------------------------------------------------------------------
420 bool pkgAcquire::Worker::SendConfiguration()
422 if (Config
->SendConfig
== false)
428 string Message
= "601 Configuration\n";
429 Message
.reserve(2000);
431 /* Write out all of the configuration directives by walking the
432 configuration tree */
433 const Configuration::Item
*Top
= _config
->Tree(0);
436 if (Top
->Value
.empty() == false)
438 string Line
= "Config-Item: " + QuoteString(Top
->FullTag(),"=\"\n") + "=";
439 Line
+= QuoteString(Top
->Value
,"\n") + '\n';
449 while (Top
!= 0 && Top
->Next
== 0)
457 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
464 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
465 // ---------------------------------------------------------------------
466 /* Send a URI Acquire message to the method */
467 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem
*Item
)
472 string Message
= "600 URI Acquire\n";
473 Message
.reserve(300);
474 Message
+= "URI: " + Item
->URI
;
475 Message
+= "\nFilename: " + Item
->Owner
->DestFile
;
476 Message
+= Item
->Owner
->Custom600Headers();
480 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
487 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
488 // ---------------------------------------------------------------------
490 bool pkgAcquire::Worker::OutFdReady()
495 Res
= write(OutFd
,OutQueue
.c_str(),OutQueue
.length());
497 while (Res
< 0 && errno
== EINTR
);
500 return MethodFailure();
502 // Hmm.. this should never happen.
506 OutQueue
.erase(0,Res
);
507 if (OutQueue
.empty() == true)
513 // Worker::InFdRead - In bound FD is ready /*{{{*/
514 // ---------------------------------------------------------------------
516 bool pkgAcquire::Worker::InFdReady()
518 if (ReadMessages() == false)
524 // Worker::MethodFailure - Called when the method fails /*{{{*/
525 // ---------------------------------------------------------------------
526 /* This is called when the method is belived to have failed, probably because
528 bool pkgAcquire::Worker::MethodFailure()
530 _error
->Error("Method %s has died unexpectedly!",Access
.c_str());
532 ExecWait(Process
,Access
.c_str(),true);
541 MessageQueue
.erase(MessageQueue
.begin(),MessageQueue
.end());
546 // Worker::Pulse - Called periodically /*{{{*/
547 // ---------------------------------------------------------------------
549 void pkgAcquire::Worker::Pulse()
551 if (CurrentItem
== 0)
555 if (stat(CurrentItem
->Owner
->DestFile
.c_str(),&Buf
) != 0)
557 CurrentSize
= Buf
.st_size
;
559 // Hmm? Should not happen...
560 if (CurrentSize
> TotalSize
&& TotalSize
!= 0)
561 TotalSize
= CurrentSize
;
564 // Worker::ItemDone - Called when the current item is finished /*{{{*/
565 // ---------------------------------------------------------------------
567 void pkgAcquire::Worker::ItemDone()