1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-worker.cc,v 1.23 1999/07/26 17:46:07 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
)
47 // Worker::Worker - Constructor for method config startup /*{{{*/
48 // ---------------------------------------------------------------------
50 pkgAcquire::Worker::Worker(MethodConfig
*Cnf
)
60 // Worker::Construct - Constructor helper /*{{{*/
61 // ---------------------------------------------------------------------
63 void pkgAcquire::Worker::Construct()
72 Debug
= _config
->FindB("Debug::pkgAcquire::Worker",false);
75 // Worker::~Worker - Destructor /*{{{*/
76 // ---------------------------------------------------------------------
78 pkgAcquire::Worker::~Worker()
86 ExecWait(Process
,Access
.c_str(),true);
90 // Worker::Start - Start the worker process /*{{{*/
91 // ---------------------------------------------------------------------
92 /* This forks the method and inits the communication channel */
93 bool pkgAcquire::Worker::Start()
95 // Get the method path
96 string Method
= _config
->FindDir("Dir::Bin::Methods") + Access
;
97 if (FileExists(Method
) == false)
98 return _error
->Error("The method driver %s could not be found.",Method
.c_str());
101 clog
<< "Starting method '" << Method
<< '\'' << endl
;
104 int Pipes
[4] = {-1,-1,-1,-1};
105 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
107 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
108 for (int I
= 0; I
!= 4; I
++)
112 for (int I
= 0; I
!= 4; I
++)
113 SetCloseExec(Pipes
[0],true);
115 // Fork off the process
116 Process
= ExecFork();
118 // Spawn the subprocess
122 dup2(Pipes
[1],STDOUT_FILENO
);
123 dup2(Pipes
[2],STDIN_FILENO
);
124 dup2(((filebuf
*)clog
.rdbuf())->fd(),STDERR_FILENO
);
125 SetCloseExec(STDOUT_FILENO
,false);
126 SetCloseExec(STDIN_FILENO
,false);
127 SetCloseExec(STDERR_FILENO
,false);
130 Args
[0] = Method
.c_str();
132 execv(Args
[0],(char **)Args
);
133 cerr
<< "Failed to exec method " << Args
[0] << endl
;
140 SetNonBlock(Pipes
[0],true);
141 SetNonBlock(Pipes
[3],true);
147 // Read the configuration data
148 if (WaitFd(InFd
) == false ||
149 ReadMessages() == false)
150 return _error
->Error("Method %s did not start correctly",Method
.c_str());
159 // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
160 // ---------------------------------------------------------------------
162 bool pkgAcquire::Worker::ReadMessages()
164 if (::ReadMessages(InFd
,MessageQueue
) == false)
165 return MethodFailure();
169 // Worker::RunMessage - Empty the message queue /*{{{*/
170 // ---------------------------------------------------------------------
171 /* This takes the messages from the message queue and runs them through
172 the parsers in order. */
173 bool pkgAcquire::Worker::RunMessages()
175 while (MessageQueue
.empty() == false)
177 string Message
= MessageQueue
.front();
178 MessageQueue
.erase(MessageQueue
.begin());
181 clog
<< " <- " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
183 // Fetch the message number
185 int Number
= strtol(Message
.c_str(),&End
,10);
186 if (End
== Message
.c_str())
187 return _error
->Error("Invalid message from method %s: %s",Access
.c_str(),Message
.c_str());
189 string URI
= LookupTag(Message
,"URI");
190 pkgAcquire::Queue::QItem
*Itm
= 0;
191 if (URI
.empty() == false)
192 Itm
= OwnerQ
->FindItem(URI
,this);
194 // Determine the message number and dispatch
199 if (Capabilities(Message
) == false)
200 return _error
->Error("Unable to process Capabilities message from %s",Access
.c_str());
206 clog
<< " <- (log) " << LookupTag(Message
,"Message") << endl
;
211 Status
= LookupTag(Message
,"Message");
219 _error
->Error("Method gave invalid 200 URI Start message");
225 TotalSize
= atoi(LookupTag(Message
,"Size","0").c_str());
226 ResumePoint
= atoi(LookupTag(Message
,"Resume-Point","0").c_str());
227 Itm
->Owner
->Start(Message
,atoi(LookupTag(Message
,"Size","0").c_str()));
240 _error
->Error("Method gave invalid 201 URI Done message");
244 pkgAcquire::Item
*Owner
= Itm
->Owner
;
245 pkgAcquire::ItemDesc Desc
= *Itm
;
246 OwnerQ
->ItemDone(Itm
);
247 Owner
->Done(Message
,atoi(LookupTag(Message
,"Size","0").c_str()),
248 LookupTag(Message
,"MD5-Hash"));
251 // Log that we are done
254 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true ||
255 StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
257 /* Hide 'hits' for local only sources - we also manage to
259 if (Config
->LocalOnly
== false)
273 _error
->Error("Method gave invalid 400 URI Failure message");
277 pkgAcquire::Item
*Owner
= Itm
->Owner
;
278 pkgAcquire::ItemDesc Desc
= *Itm
;
279 OwnerQ
->ItemDone(Itm
);
280 Owner
->Failed(Message
,Config
);
289 // 401 General Failure
291 _error
->Error("Method %s General failure: %s",LookupTag(Message
,"Message").c_str());
296 MediaChange(Message
);
303 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
304 // ---------------------------------------------------------------------
305 /* This parses the capabilities message and dumps it into the configuration
307 bool pkgAcquire::Worker::Capabilities(string Message
)
312 Config
->Version
= LookupTag(Message
,"Version");
313 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
314 Config
->Pipeline
= StringToBool(LookupTag(Message
,"Pipeline"),false);
315 Config
->SendConfig
= StringToBool(LookupTag(Message
,"Send-Config"),false);
316 Config
->LocalOnly
= StringToBool(LookupTag(Message
,"Local-Only"),false);
321 clog
<< "Configured access method " << Config
->Access
<< endl
;
322 clog
<< "Version:" << Config
->Version
<< " SingleInstance:" <<
323 Config
->SingleInstance
<<
324 " Pipeline:" << Config
->Pipeline
<< " SendConfig:" <<
325 Config
->SendConfig
<< endl
;
331 // Worker::MediaChange - Request a media change /*{{{*/
332 // ---------------------------------------------------------------------
334 bool pkgAcquire::Worker::MediaChange(string Message
)
336 if (Log
== 0 || Log
->MediaChange(LookupTag(Message
,"Media"),
337 LookupTag(Message
,"Drive")) == false)
340 sprintf(S
,"603 Media Changed\nFailed: true\n\n");
342 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
349 sprintf(S
,"603 Media Changed\n\n");
351 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
357 // Worker::SendConfiguration - Send the config to the method /*{{{*/
358 // ---------------------------------------------------------------------
360 bool pkgAcquire::Worker::SendConfiguration()
362 if (Config
->SendConfig
== false)
368 string Message
= "601 Configuration\n";
369 Message
.reserve(2000);
371 /* Write out all of the configuration directives by walking the
372 configuration tree */
373 const Configuration::Item
*Top
= _config
->Tree(0);
376 if (Top
->Value
.empty() == false)
378 string Line
= "Config-Item: " + Top
->FullTag() + "=";
379 Line
+= QuoteString(Top
->Value
,"\n") + '\n';
389 while (Top
!= 0 && Top
->Next
== 0)
397 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
404 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
405 // ---------------------------------------------------------------------
406 /* Send a URI Acquire message to the method */
407 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem
*Item
)
412 string Message
= "600 URI Acquire\n";
413 Message
.reserve(300);
414 Message
+= "URI: " + Item
->URI
;
415 Message
+= "\nFilename: " + Item
->Owner
->DestFile
;
416 Message
+= Item
->Owner
->Custom600Headers();
420 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
427 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
428 // ---------------------------------------------------------------------
430 bool pkgAcquire::Worker::OutFdReady()
435 Res
= write(OutFd
,OutQueue
.begin(),OutQueue
.length());
437 while (Res
< 0 && errno
== EINTR
);
440 return MethodFailure();
442 // Hmm.. this should never happen.
446 OutQueue
.erase(0,Res
);
447 if (OutQueue
.empty() == true)
453 // Worker::InFdRead - In bound FD is ready /*{{{*/
454 // ---------------------------------------------------------------------
456 bool pkgAcquire::Worker::InFdReady()
458 if (ReadMessages() == false)
464 // Worker::MethodFailure - Called when the method fails /*{{{*/
465 // ---------------------------------------------------------------------
466 /* This is called when the method is belived to have failed, probably because
468 bool pkgAcquire::Worker::MethodFailure()
470 _error
->Error("Method %s has died unexpectedly!",Access
.c_str());
472 ExecWait(Process
,Access
.c_str(),true);
481 MessageQueue
.erase(MessageQueue
.begin(),MessageQueue
.end());
486 // Worker::Pulse - Called periodically /*{{{*/
487 // ---------------------------------------------------------------------
489 void pkgAcquire::Worker::Pulse()
491 if (CurrentItem
== 0)
495 if (stat(CurrentItem
->Owner
->DestFile
.c_str(),&Buf
) != 0)
497 CurrentSize
= Buf
.st_size
;
500 // Worker::ItemDone - Called when the current item is finished /*{{{*/
501 // ---------------------------------------------------------------------
503 void pkgAcquire::Worker::ItemDone()