]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-worker.cc
40092d5d6c65e237b01e87404511961c7fc6cd5d
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-worker.cc,v 1.29 2000/01/17 07:11:49 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>
33 // Worker::Worker - Constructor for Queue startup /*{{{*/
34 // ---------------------------------------------------------------------
36 pkgAcquire::Worker::Worker(Queue
*Q
,MethodConfig
*Cnf
,
37 pkgAcquireStatus
*Log
) : Log(Log
)
49 // Worker::Worker - Constructor for method config startup /*{{{*/
50 // ---------------------------------------------------------------------
52 pkgAcquire::Worker::Worker(MethodConfig
*Cnf
)
64 // Worker::Construct - Constructor helper /*{{{*/
65 // ---------------------------------------------------------------------
67 void pkgAcquire::Worker::Construct()
76 Debug
= _config
->FindB("Debug::pkgAcquire::Worker",false);
79 // Worker::~Worker - Destructor /*{{{*/
80 // ---------------------------------------------------------------------
82 pkgAcquire::Worker::~Worker()
89 /* Closing of stdin is the signal to exit and die when the process
90 indicates it needs cleanup */
91 if (Config
->NeedsCleanup
== false)
93 ExecWait(Process
,Access
.c_str(),true);
97 // Worker::Start - Start the worker process /*{{{*/
98 // ---------------------------------------------------------------------
99 /* This forks the method and inits the communication channel */
100 bool pkgAcquire::Worker::Start()
102 // Get the method path
103 string Method
= _config
->FindDir("Dir::Bin::Methods") + Access
;
104 if (FileExists(Method
) == false)
105 return _error
->Error("The method driver %s could not be found.",Method
.c_str());
108 clog
<< "Starting method '" << Method
<< '\'' << endl
;
111 int Pipes
[4] = {-1,-1,-1,-1};
112 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
114 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
115 for (int I
= 0; I
!= 4; I
++)
119 for (int I
= 0; I
!= 4; I
++)
120 SetCloseExec(Pipes
[0],true);
122 // Fork off the process
123 Process
= ExecFork();
125 // Spawn the subprocess
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()));
247 _error
->Error("Method gave invalid 201 URI Done message");
251 pkgAcquire::Item
*Owner
= Itm
->Owner
;
252 pkgAcquire::ItemDesc Desc
= *Itm
;
253 OwnerQ
->ItemDone(Itm
);
254 if (TotalSize
!= 0 &&
255 (unsigned)atoi(LookupTag(Message
,"Size","0").c_str()) != TotalSize
)
256 _error
->Warning("Bizzar Error - File size is not what the server reported %s %u",
257 LookupTag(Message
,"Size","0").c_str(),TotalSize
);
259 Owner
->Done(Message
,atoi(LookupTag(Message
,"Size","0").c_str()),
260 LookupTag(Message
,"MD5-Hash"),Config
);
263 // Log that we are done
266 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true ||
267 StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
269 /* Hide 'hits' for local only sources - we also manage to
271 if (Config
->LocalOnly
== false)
285 _error
->Error("Method gave invalid 400 URI Failure message");
289 pkgAcquire::Item
*Owner
= Itm
->Owner
;
290 pkgAcquire::ItemDesc Desc
= *Itm
;
291 OwnerQ
->ItemDone(Itm
);
292 Owner
->Failed(Message
,Config
);
301 // 401 General Failure
303 _error
->Error("Method %s General failure: %s",LookupTag(Message
,"Message").c_str());
308 MediaChange(Message
);
315 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
316 // ---------------------------------------------------------------------
317 /* This parses the capabilities message and dumps it into the configuration
319 bool pkgAcquire::Worker::Capabilities(string Message
)
324 Config
->Version
= LookupTag(Message
,"Version");
325 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
326 Config
->Pipeline
= StringToBool(LookupTag(Message
,"Pipeline"),false);
327 Config
->SendConfig
= StringToBool(LookupTag(Message
,"Send-Config"),false);
328 Config
->LocalOnly
= StringToBool(LookupTag(Message
,"Local-Only"),false);
329 Config
->NeedsCleanup
= StringToBool(LookupTag(Message
,"Needs-Cleanup"),false);
330 Config
->Removable
= StringToBool(LookupTag(Message
,"Removable"),false);
335 clog
<< "Configured access method " << Config
->Access
<< endl
;
336 clog
<< "Version:" << Config
->Version
<<
337 " SingleInstance:" << Config
->SingleInstance
<<
338 " Pipeline:" << Config
->Pipeline
<<
339 " SendConfig:" << Config
->SendConfig
<<
340 " LocalOnly: " << Config
->LocalOnly
<<
341 " NeedsCleanup: " << Config
->NeedsCleanup
<<
342 " Removable: " << Config
->Removable
<< endl
;
348 // Worker::MediaChange - Request a media change /*{{{*/
349 // ---------------------------------------------------------------------
351 bool pkgAcquire::Worker::MediaChange(string Message
)
353 if (Log
== 0 || Log
->MediaChange(LookupTag(Message
,"Media"),
354 LookupTag(Message
,"Drive")) == false)
357 sprintf(S
,"603 Media Changed\nFailed: true\n\n");
359 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
366 sprintf(S
,"603 Media Changed\n\n");
368 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
374 // Worker::SendConfiguration - Send the config to the method /*{{{*/
375 // ---------------------------------------------------------------------
377 bool pkgAcquire::Worker::SendConfiguration()
379 if (Config
->SendConfig
== false)
385 string Message
= "601 Configuration\n";
386 Message
.reserve(2000);
388 /* Write out all of the configuration directives by walking the
389 configuration tree */
390 const Configuration::Item
*Top
= _config
->Tree(0);
393 if (Top
->Value
.empty() == false)
395 string Line
= "Config-Item: " + Top
->FullTag() + "=";
396 Line
+= QuoteString(Top
->Value
,"\n") + '\n';
406 while (Top
!= 0 && Top
->Next
== 0)
414 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
421 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
422 // ---------------------------------------------------------------------
423 /* Send a URI Acquire message to the method */
424 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem
*Item
)
429 string Message
= "600 URI Acquire\n";
430 Message
.reserve(300);
431 Message
+= "URI: " + Item
->URI
;
432 Message
+= "\nFilename: " + Item
->Owner
->DestFile
;
433 Message
+= Item
->Owner
->Custom600Headers();
437 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
444 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
445 // ---------------------------------------------------------------------
447 bool pkgAcquire::Worker::OutFdReady()
452 Res
= write(OutFd
,OutQueue
.begin(),OutQueue
.length());
454 while (Res
< 0 && errno
== EINTR
);
457 return MethodFailure();
459 // Hmm.. this should never happen.
463 OutQueue
.erase(0,Res
);
464 if (OutQueue
.empty() == true)
470 // Worker::InFdRead - In bound FD is ready /*{{{*/
471 // ---------------------------------------------------------------------
473 bool pkgAcquire::Worker::InFdReady()
475 if (ReadMessages() == false)
481 // Worker::MethodFailure - Called when the method fails /*{{{*/
482 // ---------------------------------------------------------------------
483 /* This is called when the method is belived to have failed, probably because
485 bool pkgAcquire::Worker::MethodFailure()
487 _error
->Error("Method %s has died unexpectedly!",Access
.c_str());
489 ExecWait(Process
,Access
.c_str(),true);
498 MessageQueue
.erase(MessageQueue
.begin(),MessageQueue
.end());
503 // Worker::Pulse - Called periodically /*{{{*/
504 // ---------------------------------------------------------------------
506 void pkgAcquire::Worker::Pulse()
508 if (CurrentItem
== 0)
512 if (stat(CurrentItem
->Owner
->DestFile
.c_str(),&Buf
) != 0)
514 CurrentSize
= Buf
.st_size
;
516 // Hmm? Should not happen...
517 if (CurrentSize
> TotalSize
&& TotalSize
!= 0)
518 TotalSize
= CurrentSize
;
521 // Worker::ItemDone - Called when the current item is finished /*{{{*/
522 // ---------------------------------------------------------------------
524 void pkgAcquire::Worker::ItemDone()