]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-worker.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-worker.cc,v 1.33 2001/03/23 01:47:14 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>
35 // Worker::Worker - Constructor for Queue startup /*{{{*/
36 // ---------------------------------------------------------------------
38 pkgAcquire::Worker::Worker(Queue
*Q
,MethodConfig
*Cnf
,
39 pkgAcquireStatus
*Log
) : Log(Log
)
51 // Worker::Worker - Constructor for method config startup /*{{{*/
52 // ---------------------------------------------------------------------
54 pkgAcquire::Worker::Worker(MethodConfig
*Cnf
)
66 // Worker::Construct - Constructor helper /*{{{*/
67 // ---------------------------------------------------------------------
69 void pkgAcquire::Worker::Construct()
78 Debug
= _config
->FindB("Debug::pkgAcquire::Worker",false);
81 // Worker::~Worker - Destructor /*{{{*/
82 // ---------------------------------------------------------------------
84 pkgAcquire::Worker::~Worker()
91 /* Closing of stdin is the signal to exit and die when the process
92 indicates it needs cleanup */
93 if (Config
->NeedsCleanup
== false)
95 ExecWait(Process
,Access
.c_str(),true);
99 // Worker::Start - Start the worker process /*{{{*/
100 // ---------------------------------------------------------------------
101 /* This forks the method and inits the communication channel */
102 bool pkgAcquire::Worker::Start()
104 // Get the method path
105 string Method
= _config
->FindDir("Dir::Bin::Methods") + Access
;
106 if (FileExists(Method
) == false)
107 return _error
->Error(_("The method driver %s could not be found."),Method
.c_str());
110 clog
<< "Starting method '" << Method
<< '\'' << endl
;
113 int Pipes
[4] = {-1,-1,-1,-1};
114 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
116 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
117 for (int I
= 0; I
!= 4; I
++)
121 for (int I
= 0; I
!= 4; I
++)
122 SetCloseExec(Pipes
[I
],true);
124 // Fork off the process
125 Process
= ExecFork();
129 dup2(Pipes
[1],STDOUT_FILENO
);
130 dup2(Pipes
[2],STDIN_FILENO
);
131 dup2(((filebuf
*)clog
.rdbuf())->fd(),STDERR_FILENO
);
132 SetCloseExec(STDOUT_FILENO
,false);
133 SetCloseExec(STDIN_FILENO
,false);
134 SetCloseExec(STDERR_FILENO
,false);
137 Args
[0] = Method
.c_str();
139 execv(Args
[0],(char **)Args
);
140 cerr
<< "Failed to exec method " << Args
[0] << endl
;
147 SetNonBlock(Pipes
[0],true);
148 SetNonBlock(Pipes
[3],true);
154 // Read the configuration data
155 if (WaitFd(InFd
) == false ||
156 ReadMessages() == false)
157 return _error
->Error(_("Method %s did not start correctly"),Method
.c_str());
166 // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
167 // ---------------------------------------------------------------------
169 bool pkgAcquire::Worker::ReadMessages()
171 if (::ReadMessages(InFd
,MessageQueue
) == false)
172 return MethodFailure();
176 // Worker::RunMessage - Empty the message queue /*{{{*/
177 // ---------------------------------------------------------------------
178 /* This takes the messages from the message queue and runs them through
179 the parsers in order. */
180 bool pkgAcquire::Worker::RunMessages()
182 while (MessageQueue
.empty() == false)
184 string Message
= MessageQueue
.front();
185 MessageQueue
.erase(MessageQueue
.begin());
188 clog
<< " <- " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
190 // Fetch the message number
192 int Number
= strtol(Message
.c_str(),&End
,10);
193 if (End
== Message
.c_str())
194 return _error
->Error("Invalid message from method %s: %s",Access
.c_str(),Message
.c_str());
196 string URI
= LookupTag(Message
,"URI");
197 pkgAcquire::Queue::QItem
*Itm
= 0;
198 if (URI
.empty() == false)
199 Itm
= OwnerQ
->FindItem(URI
,this);
201 // Determine the message number and dispatch
206 if (Capabilities(Message
) == false)
207 return _error
->Error("Unable to process Capabilities message from %s",Access
.c_str());
213 clog
<< " <- (log) " << LookupTag(Message
,"Message") << endl
;
218 Status
= LookupTag(Message
,"Message");
226 _error
->Error("Method gave invalid 200 URI Start message");
232 TotalSize
= atoi(LookupTag(Message
,"Size","0").c_str());
233 ResumePoint
= atoi(LookupTag(Message
,"Resume-Point","0").c_str());
234 Itm
->Owner
->Start(Message
,atoi(LookupTag(Message
,"Size","0").c_str()));
236 // Display update before completion
237 if (Log
!= 0 && Log
->MorePulses
== true)
238 Log
->Pulse(Itm
->Owner
->GetOwner());
251 _error
->Error("Method gave invalid 201 URI Done message");
255 pkgAcquire::Item
*Owner
= Itm
->Owner
;
256 pkgAcquire::ItemDesc Desc
= *Itm
;
258 // Display update before completion
259 if (Log
!= 0 && Log
->MorePulses
== true)
260 Log
->Pulse(Owner
->GetOwner());
262 OwnerQ
->ItemDone(Itm
);
263 if (TotalSize
!= 0 &&
264 (unsigned)atoi(LookupTag(Message
,"Size","0").c_str()) != TotalSize
)
265 _error
->Warning("Bizarre Error - File size is not what the server reported %s %lu",
266 LookupTag(Message
,"Size","0").c_str(),TotalSize
);
268 Owner
->Done(Message
,atoi(LookupTag(Message
,"Size","0").c_str()),
269 LookupTag(Message
,"MD5-Hash"),Config
);
272 // Log that we are done
275 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true ||
276 StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
278 /* Hide 'hits' for local only sources - we also manage to
280 if (Config
->LocalOnly
== false)
294 _error
->Error("Method gave invalid 400 URI Failure message");
298 // Display update before completion
299 if (Log
!= 0 && Log
->MorePulses
== true)
300 Log
->Pulse(Itm
->Owner
->GetOwner());
302 pkgAcquire::Item
*Owner
= Itm
->Owner
;
303 pkgAcquire::ItemDesc Desc
= *Itm
;
304 OwnerQ
->ItemDone(Itm
);
305 Owner
->Failed(Message
,Config
);
314 // 401 General Failure
316 _error
->Error("Method %s General failure: %s",Access
.c_str(),LookupTag(Message
,"Message").c_str());
321 MediaChange(Message
);
328 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
329 // ---------------------------------------------------------------------
330 /* This parses the capabilities message and dumps it into the configuration
332 bool pkgAcquire::Worker::Capabilities(string Message
)
337 Config
->Version
= LookupTag(Message
,"Version");
338 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
339 Config
->Pipeline
= StringToBool(LookupTag(Message
,"Pipeline"),false);
340 Config
->SendConfig
= StringToBool(LookupTag(Message
,"Send-Config"),false);
341 Config
->LocalOnly
= StringToBool(LookupTag(Message
,"Local-Only"),false);
342 Config
->NeedsCleanup
= StringToBool(LookupTag(Message
,"Needs-Cleanup"),false);
343 Config
->Removable
= StringToBool(LookupTag(Message
,"Removable"),false);
348 clog
<< "Configured access method " << Config
->Access
<< endl
;
349 clog
<< "Version:" << Config
->Version
<<
350 " SingleInstance:" << Config
->SingleInstance
<<
351 " Pipeline:" << Config
->Pipeline
<<
352 " SendConfig:" << Config
->SendConfig
<<
353 " LocalOnly: " << Config
->LocalOnly
<<
354 " NeedsCleanup: " << Config
->NeedsCleanup
<<
355 " Removable: " << Config
->Removable
<< endl
;
361 // Worker::MediaChange - Request a media change /*{{{*/
362 // ---------------------------------------------------------------------
364 bool pkgAcquire::Worker::MediaChange(string Message
)
366 if (Log
== 0 || Log
->MediaChange(LookupTag(Message
,"Media"),
367 LookupTag(Message
,"Drive")) == false)
370 snprintf(S
,sizeof(S
),"603 Media Changed\nFailed: true\n\n");
372 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
379 snprintf(S
,sizeof(S
),"603 Media Changed\n\n");
381 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
387 // Worker::SendConfiguration - Send the config to the method /*{{{*/
388 // ---------------------------------------------------------------------
390 bool pkgAcquire::Worker::SendConfiguration()
392 if (Config
->SendConfig
== false)
398 string Message
= "601 Configuration\n";
399 Message
.reserve(2000);
401 /* Write out all of the configuration directives by walking the
402 configuration tree */
403 const Configuration::Item
*Top
= _config
->Tree(0);
406 if (Top
->Value
.empty() == false)
408 string Line
= "Config-Item: " + QuoteString(Top
->FullTag(),"=\"\n") + "=";
409 Line
+= QuoteString(Top
->Value
,"\n") + '\n';
419 while (Top
!= 0 && Top
->Next
== 0)
427 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
434 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
435 // ---------------------------------------------------------------------
436 /* Send a URI Acquire message to the method */
437 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem
*Item
)
442 string Message
= "600 URI Acquire\n";
443 Message
.reserve(300);
444 Message
+= "URI: " + Item
->URI
;
445 Message
+= "\nFilename: " + Item
->Owner
->DestFile
;
446 Message
+= Item
->Owner
->Custom600Headers();
450 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
457 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
458 // ---------------------------------------------------------------------
460 bool pkgAcquire::Worker::OutFdReady()
465 Res
= write(OutFd
,OutQueue
.begin(),OutQueue
.length());
467 while (Res
< 0 && errno
== EINTR
);
470 return MethodFailure();
472 // Hmm.. this should never happen.
476 OutQueue
.erase(0,Res
);
477 if (OutQueue
.empty() == true)
483 // Worker::InFdRead - In bound FD is ready /*{{{*/
484 // ---------------------------------------------------------------------
486 bool pkgAcquire::Worker::InFdReady()
488 if (ReadMessages() == false)
494 // Worker::MethodFailure - Called when the method fails /*{{{*/
495 // ---------------------------------------------------------------------
496 /* This is called when the method is belived to have failed, probably because
498 bool pkgAcquire::Worker::MethodFailure()
500 _error
->Error("Method %s has died unexpectedly!",Access
.c_str());
502 ExecWait(Process
,Access
.c_str(),true);
511 MessageQueue
.erase(MessageQueue
.begin(),MessageQueue
.end());
516 // Worker::Pulse - Called periodically /*{{{*/
517 // ---------------------------------------------------------------------
519 void pkgAcquire::Worker::Pulse()
521 if (CurrentItem
== 0)
525 if (stat(CurrentItem
->Owner
->DestFile
.c_str(),&Buf
) != 0)
527 CurrentSize
= Buf
.st_size
;
529 // Hmm? Should not happen...
530 if (CurrentSize
> TotalSize
&& TotalSize
!= 0)
531 TotalSize
= CurrentSize
;
534 // Worker::ItemDone - Called when the current item is finished /*{{{*/
535 // ---------------------------------------------------------------------
537 void pkgAcquire::Worker::ItemDone()