]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-worker.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-worker.cc,v 1.27 1999/08/28 01:49:56 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()
90 ExecWait(Process
,Access
.c_str(),true);
94 // Worker::Start - Start the worker process /*{{{*/
95 // ---------------------------------------------------------------------
96 /* This forks the method and inits the communication channel */
97 bool pkgAcquire::Worker::Start()
99 // Get the method path
100 string Method
= _config
->FindDir("Dir::Bin::Methods") + Access
;
101 if (FileExists(Method
) == false)
102 return _error
->Error("The method driver %s could not be found.",Method
.c_str());
105 clog
<< "Starting method '" << Method
<< '\'' << endl
;
108 int Pipes
[4] = {-1,-1,-1,-1};
109 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
111 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
112 for (int I
= 0; I
!= 4; I
++)
116 for (int I
= 0; I
!= 4; I
++)
117 SetCloseExec(Pipes
[0],true);
119 // Fork off the process
120 Process
= ExecFork();
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 ResumePoint
= atoi(LookupTag(Message
,"Resume-Point","0").c_str());
231 Itm
->Owner
->Start(Message
,atoi(LookupTag(Message
,"Size","0").c_str()));
244 _error
->Error("Method gave invalid 201 URI Done message");
248 pkgAcquire::Item
*Owner
= Itm
->Owner
;
249 pkgAcquire::ItemDesc Desc
= *Itm
;
250 OwnerQ
->ItemDone(Itm
);
251 if (TotalSize
!= 0 &&
252 (unsigned)atoi(LookupTag(Message
,"Size","0").c_str()) != TotalSize
)
253 _error
->Warning("Bizzar Error - File size is not what the server reported %s %u",
254 LookupTag(Message
,"Size","0").c_str(),TotalSize
);
256 Owner
->Done(Message
,atoi(LookupTag(Message
,"Size","0").c_str()),
257 LookupTag(Message
,"MD5-Hash"));
260 // Log that we are done
263 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true ||
264 StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
266 /* Hide 'hits' for local only sources - we also manage to
268 if (Config
->LocalOnly
== false)
282 _error
->Error("Method gave invalid 400 URI Failure message");
286 pkgAcquire::Item
*Owner
= Itm
->Owner
;
287 pkgAcquire::ItemDesc Desc
= *Itm
;
288 OwnerQ
->ItemDone(Itm
);
289 Owner
->Failed(Message
,Config
);
298 // 401 General Failure
300 _error
->Error("Method %s General failure: %s",LookupTag(Message
,"Message").c_str());
305 MediaChange(Message
);
312 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
313 // ---------------------------------------------------------------------
314 /* This parses the capabilities message and dumps it into the configuration
316 bool pkgAcquire::Worker::Capabilities(string Message
)
321 Config
->Version
= LookupTag(Message
,"Version");
322 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
323 Config
->Pipeline
= StringToBool(LookupTag(Message
,"Pipeline"),false);
324 Config
->SendConfig
= StringToBool(LookupTag(Message
,"Send-Config"),false);
325 Config
->LocalOnly
= StringToBool(LookupTag(Message
,"Local-Only"),false);
330 clog
<< "Configured access method " << Config
->Access
<< endl
;
331 clog
<< "Version:" << Config
->Version
<< " SingleInstance:" <<
332 Config
->SingleInstance
<<
333 " Pipeline:" << Config
->Pipeline
<< " SendConfig:" <<
334 Config
->SendConfig
<< endl
;
340 // Worker::MediaChange - Request a media change /*{{{*/
341 // ---------------------------------------------------------------------
343 bool pkgAcquire::Worker::MediaChange(string Message
)
345 if (Log
== 0 || Log
->MediaChange(LookupTag(Message
,"Media"),
346 LookupTag(Message
,"Drive")) == false)
349 sprintf(S
,"603 Media Changed\nFailed: true\n\n");
351 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
358 sprintf(S
,"603 Media Changed\n\n");
360 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
366 // Worker::SendConfiguration - Send the config to the method /*{{{*/
367 // ---------------------------------------------------------------------
369 bool pkgAcquire::Worker::SendConfiguration()
371 if (Config
->SendConfig
== false)
377 string Message
= "601 Configuration\n";
378 Message
.reserve(2000);
380 /* Write out all of the configuration directives by walking the
381 configuration tree */
382 const Configuration::Item
*Top
= _config
->Tree(0);
385 if (Top
->Value
.empty() == false)
387 string Line
= "Config-Item: " + Top
->FullTag() + "=";
388 Line
+= QuoteString(Top
->Value
,"\n") + '\n';
398 while (Top
!= 0 && Top
->Next
== 0)
406 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
413 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
414 // ---------------------------------------------------------------------
415 /* Send a URI Acquire message to the method */
416 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem
*Item
)
421 string Message
= "600 URI Acquire\n";
422 Message
.reserve(300);
423 Message
+= "URI: " + Item
->URI
;
424 Message
+= "\nFilename: " + Item
->Owner
->DestFile
;
425 Message
+= Item
->Owner
->Custom600Headers();
429 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
436 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
437 // ---------------------------------------------------------------------
439 bool pkgAcquire::Worker::OutFdReady()
444 Res
= write(OutFd
,OutQueue
.begin(),OutQueue
.length());
446 while (Res
< 0 && errno
== EINTR
);
449 return MethodFailure();
451 // Hmm.. this should never happen.
455 OutQueue
.erase(0,Res
);
456 if (OutQueue
.empty() == true)
462 // Worker::InFdRead - In bound FD is ready /*{{{*/
463 // ---------------------------------------------------------------------
465 bool pkgAcquire::Worker::InFdReady()
467 if (ReadMessages() == false)
473 // Worker::MethodFailure - Called when the method fails /*{{{*/
474 // ---------------------------------------------------------------------
475 /* This is called when the method is belived to have failed, probably because
477 bool pkgAcquire::Worker::MethodFailure()
479 _error
->Error("Method %s has died unexpectedly!",Access
.c_str());
481 ExecWait(Process
,Access
.c_str(),true);
490 MessageQueue
.erase(MessageQueue
.begin(),MessageQueue
.end());
495 // Worker::Pulse - Called periodically /*{{{*/
496 // ---------------------------------------------------------------------
498 void pkgAcquire::Worker::Pulse()
500 if (CurrentItem
== 0)
504 if (stat(CurrentItem
->Owner
->DestFile
.c_str(),&Buf
) != 0)
506 CurrentSize
= Buf
.st_size
;
508 // Hmm? Should not happen...
509 if (CurrentSize
> TotalSize
&& TotalSize
!= 0)
510 TotalSize
= CurrentSize
;
513 // Worker::ItemDone - Called when the current item is finished /*{{{*/
514 // ---------------------------------------------------------------------
516 void pkgAcquire::Worker::ItemDone()