]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-worker.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-worker.cc,v 1.11 1998/11/09 01:09:23 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>
31 // Worker::Worker - Constructor for Queue startup /*{{{*/
32 // ---------------------------------------------------------------------
34 pkgAcquire::Worker::Worker(Queue
*Q
,MethodConfig
*Cnf
,
35 pkgAcquireStatus
*Log
) : Log(Log
)
45 // Worker::Worker - Constructor for method config startup /*{{{*/
46 // ---------------------------------------------------------------------
48 pkgAcquire::Worker::Worker(MethodConfig
*Cnf
)
58 // Worker::Construct - Constructor helper /*{{{*/
59 // ---------------------------------------------------------------------
61 void pkgAcquire::Worker::Construct()
70 Debug
= _config
->FindB("Debug::pkgAcquire::Worker",false);
73 // Worker::~Worker - Destructor /*{{{*/
74 // ---------------------------------------------------------------------
76 pkgAcquire::Worker::~Worker()
84 if (waitpid(Process
,0,0) != Process
)
85 _error
->Warning("I waited but nothing was there!");
89 // Worker::Start - Start the worker process /*{{{*/
90 // ---------------------------------------------------------------------
91 /* This forks the method and inits the communication channel */
92 bool pkgAcquire::Worker::Start()
94 // Get the method path
95 string Method
= _config
->FindDir("Dir::Bin::Methods") + Access
;
96 if (FileExists(Method
) == false)
97 return _error
->Error("The method driver %s could not be found.",Method
.c_str());
100 clog
<< "Starting method '" << Method
<< '\'' << endl
;
103 int Pipes
[4] = {-1,-1,-1,-1};
104 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
106 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
107 for (int I
= 0; I
!= 4; I
++)
111 for (int I
= 0; I
!= 4; I
++)
112 SetCloseExec(Pipes
[0],true);
114 // Fork off the process
118 cerr
<< "FATAL -> Failed to fork." << endl
;
122 // Spawn the subprocess
126 dup2(Pipes
[1],STDOUT_FILENO
);
127 dup2(Pipes
[2],STDIN_FILENO
);
128 dup2(((filebuf
*)clog
.rdbuf())->fd(),STDERR_FILENO
);
129 SetCloseExec(STDOUT_FILENO
,false);
130 SetCloseExec(STDIN_FILENO
,false);
131 SetCloseExec(STDERR_FILENO
,false);
134 Args
[0] = Method
.c_str();
136 execv(Args
[0],(char **)Args
);
137 cerr
<< "Failed to exec method " << Args
[0] << endl
;
144 SetNonBlock(Pipes
[0],true);
145 SetNonBlock(Pipes
[3],true);
151 // Read the configuration data
152 if (WaitFd(InFd
) == false ||
153 ReadMessages() == false)
154 return _error
->Error("Method %s did not start correctly",Method
.c_str());
163 // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
164 // ---------------------------------------------------------------------
166 bool pkgAcquire::Worker::ReadMessages()
168 if (::ReadMessages(InFd
,MessageQueue
) == false)
169 return MethodFailure();
173 // Worker::RunMessage - Empty the message queue /*{{{*/
174 // ---------------------------------------------------------------------
175 /* This takes the messages from the message queue and runs them through
176 the parsers in order. */
177 bool pkgAcquire::Worker::RunMessages()
179 while (MessageQueue
.empty() == false)
181 string Message
= MessageQueue
.front();
182 MessageQueue
.erase(MessageQueue
.begin());
185 clog
<< " <- " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
187 // Fetch the message number
189 int Number
= strtol(Message
.c_str(),&End
,10);
190 if (End
== Message
.c_str())
191 return _error
->Error("Invalid message from method %s: %s",Access
.c_str(),Message
.c_str());
193 string URI
= LookupTag(Message
,"URI");
194 pkgAcquire::Queue::QItem
*Itm
= 0;
195 if (URI
.empty() == false)
196 Itm
= OwnerQ
->FindItem(URI
,this);
198 // Determine the message number and dispatch
203 if (Capabilities(Message
) == false)
204 return _error
->Error("Unable to process Capabilities message from %s",Access
.c_str());
210 clog
<< " <- (log) " << LookupTag(Message
,"Message") << endl
;
215 Status
= LookupTag(Message
,"Message");
223 _error
->Error("Method gave invalid 200 URI Start message");
229 TotalSize
= atoi(LookupTag(Message
,"Size","0").c_str());
230 Itm
->Owner
->Start(Message
,atoi(LookupTag(Message
,"Size","0").c_str()));
243 _error
->Error("Method gave invalid 201 URI Done message");
247 pkgAcquire::Item
*Owner
= Itm
->Owner
;
248 pkgAcquire::ItemDesc Desc
= *Itm
;
249 OwnerQ
->ItemDone(Itm
);
250 Owner
->Done(Message
,atoi(LookupTag(Message
,"Size","0").c_str()),
251 LookupTag(Message
,"MD5-Hash"));
254 // Log that we are done
257 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true ||
258 StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
271 _error
->Error("Method gave invalid 400 URI Failure message");
275 pkgAcquire::Item
*Owner
= Itm
->Owner
;
276 pkgAcquire::ItemDesc Desc
= *Itm
;
277 OwnerQ
->ItemDone(Itm
);
278 Owner
->Failed(Message
);
287 // 401 General Failure
289 _error
->Error("Method %s General failure: %s",LookupTag(Message
,"Message").c_str());
296 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
297 // ---------------------------------------------------------------------
298 /* This parses the capabilities message and dumps it into the configuration
300 bool pkgAcquire::Worker::Capabilities(string Message
)
305 Config
->Version
= LookupTag(Message
,"Version");
306 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
307 Config
->PreScan
= StringToBool(LookupTag(Message
,"Pre-Scan"),false);
308 Config
->Pipeline
= StringToBool(LookupTag(Message
,"Pipeline"),false);
309 Config
->SendConfig
= StringToBool(LookupTag(Message
,"Send-Config"),false);
314 clog
<< "Configured access method " << Config
->Access
<< endl
;
315 clog
<< "Version:" << Config
->Version
<< " SingleInstance:" <<
316 Config
->SingleInstance
<< " PreScan: " << Config
->PreScan
<<
317 " Pipeline:" << Config
->Pipeline
<< " SendConfig:" <<
318 Config
->SendConfig
<< endl
;
324 // Worker::SendConfiguration - Send the config to the method /*{{{*/
325 // ---------------------------------------------------------------------
327 bool pkgAcquire::Worker::SendConfiguration()
329 if (Config
->SendConfig
== false)
335 string Message
= "601 Configuration\n";
336 Message
.reserve(2000);
338 /* Write out all of the configuration directives by walking the
339 configuration tree */
340 const Configuration::Item
*Top
= _config
->Tree(0);
343 if (Top
->Value
.empty() == false)
345 string Line
= "Config-Item: " + Top
->FullTag() + "=";
346 Line
+= QuoteString(Top
->Value
,"\n") + '\n';
356 while (Top
!= 0 && Top
->Next
== 0)
364 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
371 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
372 // ---------------------------------------------------------------------
373 /* Send a URI Acquire message to the method */
374 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem
*Item
)
379 string Message
= "600 URI Acquire\n";
380 Message
.reserve(300);
381 Message
+= "URI: " + Item
->URI
;
382 Message
+= "\nFilename: " + Item
->Owner
->DestFile
;
383 Message
+= Item
->Owner
->Custom600Headers();
387 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
394 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
395 // ---------------------------------------------------------------------
397 bool pkgAcquire::Worker::OutFdReady()
399 int Res
= write(OutFd
,OutQueue
.begin(),OutQueue
.length());
401 return MethodFailure();
403 // Hmm.. this should never happen.
407 OutQueue
.erase(0,Res
);
408 if (OutQueue
.empty() == true)
414 // Worker::InFdRead - In bound FD is ready /*{{{*/
415 // ---------------------------------------------------------------------
417 bool pkgAcquire::Worker::InFdReady()
419 if (ReadMessages() == false)
425 // Worker::MethodFailure - Called when the method fails /*{{{*/
426 // ---------------------------------------------------------------------
427 /* This is called when the method is belived to have failed, probably because
429 bool pkgAcquire::Worker::MethodFailure()
431 cerr
<< "Method " << Access
<< " has died unexpectedly!" << endl
;
432 if (waitpid(Process
,0,0) != Process
)
433 _error
->Warning("I waited but nothing was there!");
442 MessageQueue
.erase(MessageQueue
.begin(),MessageQueue
.end());
447 // Worker::Pulse - Called periodically /*{{{*/
448 // ---------------------------------------------------------------------
450 void pkgAcquire::Worker::Pulse()
452 if (CurrentItem
== 0)
457 if (stat(CurrentItem
->Owner
->DestFile
.c_str(),&Buf
) != 0)
459 CurrentSize
= Buf
.st_size
;
462 // Worker::ItemDone - Called when the current item is finished /*{{{*/
463 // ---------------------------------------------------------------------
465 void pkgAcquire::Worker::ItemDone()